Add support for auto session creation
[lttng-tools.git] / ltt-sessiond / main.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
91d76f53 8 *
fac6795d
DG
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
91d76f53 13 *
fac6795d
DG
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
20#include <fcntl.h>
21#include <getopt.h>
22#include <grp.h>
23#include <limits.h>
24#include <pthread.h>
25#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/ipc.h>
30#include <sys/shm.h>
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/types.h>
34#include <unistd.h>
35
36#include <urcu/list.h> /* URCU list library (-lurcu) */
37#include <ust/ustctl.h> /* UST control lib (-lust) */
5b97ec60 38#include <lttng/lttng.h>
fac6795d
DG
39
40#include "liblttsessiondcomm.h"
41#include "ltt-sessiond.h"
75462a81 42#include "lttngerr.h"
5b74c7b1 43#include "session.h"
fda89c9b 44#include "trace.h"
91d76f53 45#include "traceable-app.h"
fac6795d 46
75462a81 47/* Const values */
686204ab
MD
48const char default_home_dir[] = DEFAULT_HOME_DIR;
49const char default_tracing_group[] = DEFAULT_TRACING_GROUP;
50const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
51const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
52
fac6795d 53/* Static functions */
fac6795d 54static int check_existing_daemon(void);
1fd70b72 55static int connect_app(pid_t pid);
fac6795d 56static int init_daemon_socket(void);
62bd06d8 57static int notify_apps(const char* name);
e065084a
DG
58static int process_client_msg(int sock, struct lttcomm_session_msg*);
59static int send_unix_sock(int sock, void *buf, size_t len);
62bd06d8
DG
60static int set_signal_handler(void);
61static int set_socket_perms(void);
aaf97519 62static int setup_data_buffer(char **buf, size_t size, struct lttcomm_lttng_msg *llm);
62bd06d8
DG
63static void cleanup(void);
64static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm);
65static void sighandler(int sig);
fac6795d 66
1fd70b72
DG
67static void *thread_manage_clients(void *data);
68static void *thread_manage_apps(void *data);
fac6795d 69
fac6795d 70/* Variables */
62bd06d8
DG
71int opt_verbose;
72int opt_quiet;
fac6795d
DG
73const char *progname;
74const char *opt_tracing_group;
5b8719f5 75static int opt_sig_parent;
fac6795d
DG
76static int opt_daemon;
77static int is_root; /* Set to 1 if the daemon is running as root */
5b8719f5 78static pid_t ppid;
fac6795d
DG
79
80static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
81static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
82
83static int client_socket;
84static int apps_socket;
85
86static struct ltt_session *current_session;
e065084a 87
fac6795d
DG
88/*
89 * thread_manage_apps
90 *
91 * This thread manage the application socket communication
92 */
93static void *thread_manage_apps(void *data)
94{
95 int sock, ret;
fac6795d
DG
96
97 /* TODO: Something more elegant is needed but fine for now */
686204ab 98 struct {
fac6795d 99 int reg; /* 1:register, 0:unregister */
686204ab
MD
100 pid_t pid;
101 uid_t uid;
102 } reg_msg;
fac6795d 103
e07ae692
DG
104 DBG("[thread] Manage apps started");
105
fac6795d
DG
106 /* Notify all applications to register */
107 notify_apps(default_global_apps_pipe);
108
109 ret = lttcomm_listen_unix_sock(apps_socket);
110 if (ret < 0) {
111 goto error;
112 }
113
114 while (1) {
115 /* Blocking call, waiting for transmission */
116 sock = lttcomm_accept_unix_sock(apps_socket);
117 if (sock < 0) {
118 goto error;
119 }
120
121 /* Basic recv here to handle the very simple data
122 * that the libust send to register (reg_msg).
123 */
124 ret = recv(sock, &reg_msg, sizeof(reg_msg), 0);
125 if (ret < 0) {
126 perror("recv");
127 continue;
128 }
129
130 /* Add application to the global traceable list */
131 if (reg_msg.reg == 1) {
132 /* Registering */
91d76f53
DG
133 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
134 if (ret < 0) {
135 /* register_traceable_app only return an error with
136 * ENOMEM. At this point, we better stop everything.
137 */
138 goto error;
139 }
fac6795d
DG
140 } else {
141 /* Unregistering */
91d76f53 142 unregister_traceable_app(reg_msg.pid);
fac6795d
DG
143 }
144 }
145
146error:
147
148 return NULL;
149}
150
151/*
152 * thread_manage_clients
153 *
154 * This thread manage all clients request using the unix
155 * client socket for communication.
156 */
157static void *thread_manage_clients(void *data)
158{
159 int sock, ret;
160 struct lttcomm_session_msg lsm;
fac6795d 161
e07ae692
DG
162 DBG("[thread] Manage client started");
163
fac6795d
DG
164 ret = lttcomm_listen_unix_sock(client_socket);
165 if (ret < 0) {
166 goto error;
167 }
168
5b8719f5
DG
169 /* Notify parent pid that we are ready
170 * to accept command for client side.
171 */
172 if (opt_sig_parent) {
173 kill(ppid, SIGCHLD);
174 }
175
fac6795d
DG
176 while (1) {
177 /* Blocking call, waiting for transmission */
178 sock = lttcomm_accept_unix_sock(client_socket);
179 if (sock < 0) {
180 goto error;
181 }
182
183 /*
184 * Data is received from the lttng client. The struct
185 * lttcomm_session_msg (lsm) contains the command and data
186 * request of the client.
187 */
188 ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm));
87378cf5 189 if (ret <= 0) {
fac6795d
DG
190 continue;
191 }
192
193 /* This function dispatch the work to the LTTng or UST libs
e065084a
DG
194 * and then sends back the response to the client. This is needed
195 * because there might be more then one lttcomm_lttng_msg to
196 * send out so process_client_msg do both jobs.
fac6795d 197 */
e065084a
DG
198 ret = process_client_msg(sock, &lsm);
199 if (ret < 0) {
200 /* Error detected but still accept command */
201 continue;
fac6795d
DG
202 }
203 }
204
205error:
206 return NULL;
207}
208
e065084a
DG
209/*
210 * send_unix_sock
211 *
212 * Send data on a unix socket using the liblttsessiondcomm API.
213 *
214 * Return lttcomm error code.
215 */
216static int send_unix_sock(int sock, void *buf, size_t len)
217{
218 /* Check valid length */
219 if (len <= 0) {
220 return -1;
221 }
222
223 return lttcomm_send_unix_sock(sock, buf, len);
224}
225
fac6795d
DG
226/*
227 * connect_app
228 *
229 * Return a socket connected to the libust communication socket
230 * of the application identified by the pid.
379473d2
DG
231 *
232 * If the pid is not found in the traceable list,
233 * return -1 to indicate error.
fac6795d
DG
234 */
235static int connect_app(pid_t pid)
236{
91d76f53
DG
237 int sock;
238 struct ltt_traceable_app *lta;
379473d2 239
e07ae692
DG
240 DBG("Connect to application pid %d", pid);
241
91d76f53
DG
242 lta = find_app_by_pid(pid);
243 if (lta == NULL) {
244 /* App not found */
7442b2ba 245 DBG("Application pid %d not found", pid);
379473d2
DG
246 return -1;
247 }
fac6795d 248
91d76f53 249 sock = ustctl_connect_pid(lta->pid);
fac6795d 250 if (sock < 0) {
75462a81 251 ERR("Fail connecting to the PID %d\n", pid);
fac6795d
DG
252 }
253
254 return sock;
255}
256
257/*
258 * notify_apps
259 *
260 * Notify apps by writing 42 to a named pipe using name.
261 * Every applications waiting for a ltt-sessiond will be notified
262 * and re-register automatically to the session daemon.
263 *
264 * Return open or write error value.
265 */
266static int notify_apps(const char *name)
267{
268 int fd;
269 int ret = -1;
270
e07ae692
DG
271 DBG("Notify the global application pipe");
272
fac6795d
DG
273 /* Try opening the global pipe */
274 fd = open(name, O_WRONLY);
275 if (fd < 0) {
276 goto error;
277 }
278
279 /* Notify by writing on the pipe */
280 ret = write(fd, "42", 2);
281 if (ret < 0) {
282 perror("write");
283 }
284
285error:
286 return ret;
287}
288
379473d2
DG
289/*
290 * ust_create_trace
291 *
292 * Create an userspace trace using pid.
293 * This trace is then appended to the current session
294 * ust trace list.
295 */
296static int ust_create_trace(pid_t pid)
297{
298 int sock, ret;
299 struct ltt_ust_trace *trace;
300
e07ae692
DG
301 DBG("Creating trace for pid %d", pid);
302
379473d2
DG
303 trace = malloc(sizeof(struct ltt_ust_trace));
304 if (trace == NULL) {
305 perror("malloc");
306 ret = -1;
307 goto error;
308 }
309
310 /* Init */
311 trace->pid = pid;
312 trace->shmid = 0;
1657e9bb
DG
313 /* NOTE: to be removed. Trace name will no longer be
314 * required for LTTng userspace tracer. For now, we set it
315 * to 'auto' for API compliance.
316 */
317 snprintf(trace->name, 5, "auto");
379473d2
DG
318
319 /* Connect to app using ustctl API */
320 sock = connect_app(pid);
321 if (sock < 0) {
322 ret = LTTCOMM_NO_TRACEABLE;
323 goto error;
324 }
325
1657e9bb 326 ret = ustctl_create_trace(sock, trace->name);
379473d2
DG
327 if (ret < 0) {
328 ret = LTTCOMM_CREATE_FAIL;
329 goto error;
330 }
331
332 /* Check if current session is valid */
333 if (current_session) {
334 cds_list_add(&trace->list, &current_session->ust_traces);
1657e9bb 335 current_session->ust_trace_count++;
379473d2
DG
336 }
337
338error:
339 return ret;
340}
341
ce3d728c
DG
342/*
343 * ust_start_trace
344 *
345 * Start a trace. This trace, identified by the pid, must be
346 * in the current session ust_traces list.
347 */
348static int ust_start_trace(pid_t pid)
349{
350 int sock, ret;
351 struct ltt_ust_trace *trace;
352
353 DBG("Starting trace for pid %d", pid);
354
355 trace = find_session_ust_trace_by_pid(current_session, pid);
356 if (trace == NULL) {
357 ret = LTTCOMM_NO_TRACE;
358 goto error;
359 }
360
361 /* Connect to app using ustctl API */
362 sock = connect_app(pid);
363 if (sock < 0) {
364 ret = LTTCOMM_NO_TRACEABLE;
365 goto error;
366 }
367
368 ret = ustctl_start_trace(sock, "auto");
369 if (ret < 0) {
370 ret = LTTCOMM_START_FAIL;
371 goto error;
372 }
373
374error:
375 return ret;
376}
377
520ff687
DG
378/*
379 * ust_stop_trace
380 *
381 * Stop a trace. This trace, identified by the pid, must be
382 * in the current session ust_traces list.
383 */
384static int ust_stop_trace(pid_t pid)
385{
386 int sock, ret;
387 struct ltt_ust_trace *trace;
388
389 DBG("Stopping trace for pid %d", pid);
390
391 trace = find_session_ust_trace_by_pid(current_session, pid);
392 if (trace == NULL) {
393 ret = LTTCOMM_NO_TRACE;
394 goto error;
395 }
396
397 /* Connect to app using ustctl API */
398 sock = connect_app(pid);
399 if (sock < 0) {
400 ret = LTTCOMM_NO_TRACEABLE;
401 goto error;
402 }
403
404 ret = ustctl_stop_trace(sock, trace->name);
405 if (ret < 0) {
406 ret = LTTCOMM_STOP_FAIL;
407 goto error;
408 }
409
410error:
411 return ret;
412}
413
e065084a
DG
414/*
415 * copy_common_data
416 *
417 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
418 */
419static void copy_common_data(struct lttcomm_lttng_msg *llm, struct lttcomm_session_msg *lsm)
420{
421 llm->cmd_type = lsm->cmd_type;
422 llm->pid = lsm->pid;
aaf97519
DG
423
424 /* Manage uuid */
8028d920
DG
425 if (!uuid_is_null(lsm->session_id)) {
426 uuid_copy(llm->session_id, lsm->session_id);
e065084a 427 }
aaf97519
DG
428
429 strncpy(llm->trace_name, lsm->trace_name, strlen(llm->trace_name));
8028d920 430 llm->trace_name[strlen(llm->trace_name) - 1] = '\0';
e065084a
DG
431}
432
ca95a216
DG
433/*
434 * setup_data_buffer
435 *
436 * Setup the outgoing data buffer for the response
437 * data allocating the right amount of memory.
438 *
439 * Return total size of the buffer pointed by buf.
440 */
aaf97519 441static int setup_data_buffer(char **buf, size_t s_data, struct lttcomm_lttng_msg *llm)
ca95a216
DG
442{
443 int ret = 0;
ca95a216
DG
444 size_t buf_size;
445
446 buf_size = sizeof(struct lttcomm_lttng_msg) + s_data;
aaf97519
DG
447 *buf = malloc(buf_size);
448 if (*buf == NULL) {
ca95a216
DG
449 perror("malloc");
450 ret = -1;
451 goto error;
452 }
453
454 /* Setup lttcomm_lttng_msg data and copy
455 * it to the newly allocated buffer.
456 */
457 llm->size_payload = s_data;
aaf97519 458 memcpy(*buf, llm, sizeof(struct lttcomm_lttng_msg));
ca95a216
DG
459
460 return buf_size;
461
462error:
463 return ret;
464}
465
fac6795d
DG
466/*
467 * process_client_msg
468 *
469 * This takes the lttcomm_session_msg struct and process the command requested
e065084a
DG
470 * by the client. It then creates response(s) and send it back to the
471 * given socket (sock).
fac6795d 472 *
e065084a 473 * Return any error encountered or 0 for success.
fac6795d 474 */
e065084a 475static int process_client_msg(int sock, struct lttcomm_session_msg *lsm)
fac6795d 476{
e065084a 477 int ret;
ca95a216 478 int buf_size;
7e6e59cd 479 size_t header_size;
aaf97519
DG
480 char *send_buf = NULL;
481 struct lttcomm_lttng_msg llm;
fac6795d 482
e07ae692
DG
483 DBG("Processing client message");
484
fac6795d
DG
485 /* Copy common data to identify the response
486 * on the lttng client side.
487 */
e065084a 488 copy_common_data(&llm, lsm);
fac6795d 489
379473d2
DG
490 /* Check command that needs a session */
491 if (lsm->cmd_type != LTTNG_CREATE_SESSION &&
492 lsm->cmd_type != LTTNG_LIST_SESSIONS &&
493 lsm->cmd_type != UST_LIST_APPS)
494 {
495 current_session = find_session_by_uuid(lsm->session_id);
496 if (current_session == NULL) {
497 ret = LTTCOMM_SELECT_SESS;
498 goto end;
499 }
500 }
501
fac6795d 502 /* Default return code.
e065084a 503 * In our world, everything is OK... right? ;)
fac6795d 504 */
e065084a 505 llm.ret_code = LTTCOMM_OK;
fac6795d 506
7e6e59cd
DG
507 header_size = sizeof(struct lttcomm_lttng_msg);
508
fac6795d
DG
509 /* Process by command type */
510 switch (lsm->cmd_type) {
aaf97519
DG
511 case LTTNG_CREATE_SESSION:
512 {
8028d920 513 ret = create_session(lsm->session_name, &llm.session_id);
aaf97519 514 if (ret < 0) {
27673bb6
DG
515 if (ret == -1) {
516 ret = LTTCOMM_EXIST_SESS;
517 } else {
518 ret = LTTCOMM_FATAL;
519 }
8028d920 520 goto end;
aaf97519
DG
521 }
522
aaf97519
DG
523 buf_size = setup_data_buffer(&send_buf, 0, &llm);
524 if (buf_size < 0) {
525 ret = LTTCOMM_FATAL;
8028d920 526 goto end;
aaf97519
DG
527 }
528
aaf97519
DG
529 break;
530 }
8028d920
DG
531 case LTTNG_DESTROY_SESSION:
532 {
533 ret = destroy_session(&lsm->session_id);
534 if (ret < 0) {
535 ret = LTTCOMM_NO_SESS;
536 } else {
537 ret = LTTCOMM_OK;
538 }
539
540 /* No auxiliary data so only send the llm struct. */
541 goto end;
542 }
1657e9bb
DG
543 case LTTNG_LIST_TRACES:
544 {
545 unsigned int trace_count = get_trace_count_per_session(current_session);
546
547 if (trace_count == 0) {
548 ret = LTTCOMM_NO_TRACE;
549 goto end;
550 }
551
552 buf_size = setup_data_buffer(&send_buf,
553 sizeof(struct lttng_trace) * trace_count, &llm);
554 if (buf_size < 0) {
555 ret = LTTCOMM_FATAL;
556 goto end;
557 }
558
559 get_traces_per_session(current_session, (struct lttng_trace *)(send_buf + header_size));
560 break;
561 }
df0da139
DG
562 case UST_CREATE_TRACE:
563 {
379473d2 564 ret = ust_create_trace(lsm->pid);
df0da139 565 if (ret < 0) {
ce3d728c
DG
566 /* If -1 is returned from ust_create_trace, malloc
567 * failed so it's pretty much a fatal error.
568 */
569 ret = LTTCOMM_FATAL;
379473d2 570 goto end;
df0da139
DG
571 }
572
379473d2 573 /* No auxiliary data so only send the llm struct. */
df0da139
DG
574 goto end;
575 }
fac6795d
DG
576 case UST_LIST_APPS:
577 {
91d76f53 578 unsigned int app_count = get_app_count();
ca95a216 579 /* Stop right now if no apps */
91d76f53 580 if (app_count == 0) {
e065084a 581 ret = LTTCOMM_NO_APPS;
8028d920 582 goto end;
e065084a
DG
583 }
584
ca95a216
DG
585 /* Setup data buffer and details for transmission */
586 buf_size = setup_data_buffer(&send_buf,
91d76f53 587 sizeof(pid_t) * app_count, &llm);
ca95a216
DG
588 if (buf_size < 0) {
589 ret = LTTCOMM_FATAL;
8028d920 590 goto end;
ca95a216
DG
591 }
592
91d76f53 593 get_app_list_pids((pid_t *)(send_buf + header_size));
ca95a216 594
fac6795d
DG
595 break;
596 }
ce3d728c
DG
597 case UST_START_TRACE:
598 {
599 ret = ust_start_trace(lsm->pid);
600
601 /* No auxiliary data so only send the llm struct. */
602 goto end;
603 }
520ff687
DG
604 case UST_STOP_TRACE:
605 {
606 ret = ust_stop_trace(lsm->pid);
607
608 /* No auxiliary data so only send the llm struct. */
609 goto end;
610 }
57167058
DG
611 case LTTNG_LIST_SESSIONS:
612 {
5b74c7b1 613 unsigned int session_count = get_session_count();
ca95a216
DG
614 /* Stop right now if no session */
615 if (session_count == 0) {
57167058 616 ret = LTTCOMM_NO_SESS;
8028d920 617 goto end;
57167058
DG
618 }
619
ca95a216
DG
620 /* Setup data buffer and details for transmission */
621 buf_size = setup_data_buffer(&send_buf,
622 (sizeof(struct lttng_session) * session_count), &llm);
623 if (buf_size < 0) {
624 ret = LTTCOMM_FATAL;
8028d920 625 goto end;
ca95a216
DG
626 }
627
5b74c7b1 628 get_lttng_session((struct lttng_session *)(send_buf + header_size));
ca95a216 629
57167058
DG
630 break;
631 }
fac6795d 632 default:
e065084a 633 {
fac6795d 634 /* Undefined command */
e065084a 635 ret = LTTCOMM_UND;
8028d920 636 goto end;
e065084a 637 }
fac6795d
DG
638 }
639
1fd70b72
DG
640 ret = send_unix_sock(sock, send_buf, buf_size);
641
ca95a216
DG
642 if (send_buf != NULL) {
643 free(send_buf);
644 }
645
e065084a
DG
646 return ret;
647
8028d920 648end:
7442b2ba 649 DBG("Return code to client %d", ret);
e065084a
DG
650 /* Notify client of error */
651 llm.ret_code = ret;
ca95a216 652 llm.size_payload = 0;
e065084a
DG
653 send_unix_sock(sock, (void*) &llm, sizeof(llm));
654
8028d920 655 return ret;
fac6795d
DG
656}
657
658/*
659 * usage function on stderr
660 */
661static void usage(void)
662{
b716ce68
DG
663 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
664 fprintf(stderr, " -h, --help Display this usage.\n");
665 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
666 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket.\n");
667 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
668 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
669 fprintf(stderr, " -V, --version Show version number.\n");
670 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
671 fprintf(stderr, " -q, --quiet No output at all.\n");
3f9947db 672 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
fac6795d
DG
673}
674
675/*
676 * daemon argument parsing
677 */
678static int parse_args(int argc, char **argv)
679{
680 int c;
681
682 static struct option long_options[] = {
683 { "client-sock", 1, 0, 'c' },
684 { "apps-sock", 1, 0, 'a' },
685 { "daemonize", 0, 0, 'd' },
5b8719f5 686 { "sig-parent", 0, 0, 'S' },
fac6795d
DG
687 { "help", 0, 0, 'h' },
688 { "group", 1, 0, 'g' },
689 { "version", 0, 0, 'V' },
75462a81 690 { "quiet", 0, 0, 'q' },
3f9947db 691 { "verbose", 0, 0, 'v' },
fac6795d
DG
692 { NULL, 0, 0, 0 }
693 };
694
695 while (1) {
696 int option_index = 0;
3f9947db 697 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:", long_options, &option_index);
fac6795d
DG
698 if (c == -1) {
699 break;
700 }
701
702 switch (c) {
703 case 0:
704 fprintf(stderr, "option %s", long_options[option_index].name);
705 if (optarg) {
706 fprintf(stderr, " with arg %s\n", optarg);
707 }
708 break;
b716ce68 709 case 'c':
fac6795d
DG
710 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
711 break;
712 case 'a':
713 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
714 break;
715 case 'd':
716 opt_daemon = 1;
717 break;
718 case 'g':
719 opt_tracing_group = strdup(optarg);
720 break;
721 case 'h':
722 usage();
723 exit(EXIT_FAILURE);
724 case 'V':
725 fprintf(stdout, "%s\n", VERSION);
726 exit(EXIT_SUCCESS);
5b8719f5
DG
727 case 'S':
728 opt_sig_parent = 1;
729 break;
75462a81
DG
730 case 'q':
731 opt_quiet = 1;
732 break;
3f9947db
DG
733 case 'v':
734 opt_verbose = 1;
735 break;
fac6795d
DG
736 default:
737 /* Unknown option or other error.
738 * Error is printed by getopt, just return */
739 return -1;
740 }
741 }
742
743 return 0;
744}
745
746/*
747 * init_daemon_socket
748 *
749 * Creates the two needed socket by the daemon.
62bd06d8
DG
750 * apps_socket - The communication socket for all UST apps.
751 * client_socket - The communication of the cli tool (lttng).
fac6795d
DG
752 */
753static int init_daemon_socket()
754{
755 int ret = 0;
756 mode_t old_umask;
757
758 old_umask = umask(0);
759
760 /* Create client tool unix socket */
761 client_socket = lttcomm_create_unix_sock(client_unix_sock_path);
762 if (client_socket < 0) {
763 ret = -1;
764 goto end;
765 }
766
767 /* File permission MUST be 660 */
768 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
769 if (ret < 0) {
770 perror("chmod");
771 goto end;
772 }
773
774 /* Create the application unix socket */
775 apps_socket = lttcomm_create_unix_sock(apps_unix_sock_path);
776 if (apps_socket < 0) {
777 ret = -1;
778 goto end;
779 }
780
781 /* File permission MUST be 660 */
782 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
783 if (ret < 0) {
784 perror("chmod");
785 goto end;
786 }
787
788end:
789 umask(old_umask);
790 return ret;
791}
792
793/*
794 * check_existing_daemon
795 *
796 * Check if the global socket is available.
62bd06d8 797 * If yes, error is returned.
fac6795d
DG
798 */
799static int check_existing_daemon()
800{
801 int ret;
802
803 ret = access(client_unix_sock_path, F_OK);
804 if (ret == 0) {
805 ret = access(apps_unix_sock_path, F_OK);
806 }
807
808 return ret;
809}
810
811/*
62bd06d8 812 * get_home_dir
fac6795d 813 *
62bd06d8
DG
814 * Return pointer to home directory path using
815 * the env variable HOME.
fac6795d 816 *
62bd06d8 817 * Default : /tmp
fac6795d
DG
818 */
819static const char *get_home_dir(void)
820{
821 const char *home_path;
822
686204ab 823 if ((home_path = (const char *) getenv("HOME")) == NULL) {
fac6795d
DG
824 home_path = default_home_dir;
825 }
826
827 return home_path;
828}
829
830/*
831 * set_socket_perms
832 *
833 * Set the tracing group gid onto the client socket.
834 */
835static int set_socket_perms(void)
836{
837 int ret;
838 struct group *grp;
839
840 /* Decide which group name to use */
841 (opt_tracing_group != NULL) ?
842 (grp = getgrnam(opt_tracing_group)) :
843 (grp = getgrnam(default_tracing_group));
844
845 if (grp == NULL) {
75462a81 846 ERR("Missing tracing group. Aborting execution.\n");
fac6795d
DG
847 ret = -1;
848 goto end;
849 }
850
851 ret = chown(client_unix_sock_path, 0, grp->gr_gid);
852 if (ret < 0) {
853 perror("chown");
854 }
855
e07ae692
DG
856 DBG("Sockets permissions set");
857
fac6795d
DG
858end:
859 return ret;
860}
861
fac6795d 862/*
62bd06d8 863 * set_signal_handler
fac6795d 864 *
62bd06d8 865 * Setup signal handler for :
fac6795d
DG
866 * SIGINT, SIGTERM, SIGPIPE
867 */
868static int set_signal_handler(void)
869{
870 int ret = 0;
871 struct sigaction sa;
872 sigset_t sigset;
873
874 if ((ret = sigemptyset(&sigset)) < 0) {
875 perror("sigemptyset");
876 return ret;
877 }
878
879 sa.sa_handler = sighandler;
880 sa.sa_mask = sigset;
881 sa.sa_flags = 0;
882 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
883 perror("sigaction");
884 return ret;
885 }
886
887 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
888 perror("sigaction");
889 return ret;
890 }
891
892 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
893 perror("sigaction");
894 return ret;
895 }
896
e07ae692
DG
897 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
898
fac6795d
DG
899 return ret;
900}
901
902/**
62bd06d8 903 * sighandler
fac6795d 904 *
62bd06d8 905 * Signal handler for the daemon
fac6795d
DG
906 */
907static void sighandler(int sig)
908{
909 switch (sig) {
910 case SIGPIPE:
e07ae692 911 DBG("SIGPIPE catched");
87378cf5 912 return;
fac6795d 913 case SIGINT:
e07ae692
DG
914 DBG("SIGINT catched");
915 cleanup();
916 break;
fac6795d 917 case SIGTERM:
e07ae692 918 DBG("SIGTERM catched");
fac6795d 919 cleanup();
686204ab 920 break;
fac6795d
DG
921 default:
922 break;
923 }
924
925 exit(EXIT_SUCCESS);
926}
927
928/*
62bd06d8 929 * cleanup
fac6795d 930 *
62bd06d8 931 * Cleanup the daemon on exit
fac6795d
DG
932 */
933static void cleanup()
934{
e07ae692
DG
935 DBG("Cleaning up");
936
fac6795d 937 /* <fun> */
b716ce68
DG
938 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
939 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
fac6795d
DG
940 /* </fun> */
941
942 unlink(client_unix_sock_path);
943 unlink(apps_unix_sock_path);
944}
945
946/*
947 * main
948 */
949int main(int argc, char **argv)
950{
951 int i;
952 int ret = 0;
953 void *status;
954 pthread_t threads[2];
955
956 /* Parse arguments */
957 progname = argv[0];
958 if ((ret = parse_args(argc, argv) < 0)) {
959 goto error;
960 }
961
962 /* Daemonize */
963 if (opt_daemon) {
53094c05
DG
964 ret = daemon(0, 0);
965 if (ret < 0) {
966 perror("daemon");
967 goto error;
968 }
fac6795d
DG
969 }
970
971 /* Check if daemon is UID = 0 */
972 is_root = !getuid();
973
974 /* Set all sockets path */
975 if (is_root) {
976 if (strlen(apps_unix_sock_path) == 0) {
977 (snprintf(apps_unix_sock_path, PATH_MAX,
978 DEFAULT_GLOBAL_APPS_UNIX_SOCK));
979 }
980
981 if (strlen(client_unix_sock_path) == 0) {
982 (snprintf(client_unix_sock_path, PATH_MAX,
983 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK));
984 }
985 } else {
986 if (strlen(apps_unix_sock_path) == 0) {
987 (snprintf(apps_unix_sock_path, PATH_MAX,
988 DEFAULT_HOME_APPS_UNIX_SOCK, get_home_dir()));
989 }
990
991 /* Set the cli tool unix socket path */
992 if (strlen(client_unix_sock_path) == 0) {
993 (snprintf(client_unix_sock_path, PATH_MAX,
994 DEFAULT_HOME_CLIENT_UNIX_SOCK, get_home_dir()));
995 }
996 }
997
998 /* See if daemon already exist. If any of the two
999 * socket needed by the daemon are present, this test fails
1000 */
1001 if ((ret = check_existing_daemon()) == 0) {
75462a81 1002 ERR("Already running daemon.\n");
ab118b20
DG
1003 /* We do not goto error because we must not
1004 * cleanup() because a daemon is already working.
1005 */
1006 return EXIT_FAILURE;
fac6795d
DG
1007 }
1008
1009 if (set_signal_handler() < 0) {
1010 goto error;
1011 }
1012
1013 /* Setup the two needed unix socket */
1014 if (init_daemon_socket() < 0) {
1015 goto error;
1016 }
1017
1018 /* Set credentials to socket */
1019 if (is_root && (set_socket_perms() < 0)) {
1020 goto error;
1021 }
1022
5b8719f5
DG
1023 /* Get parent pid if -S, --sig-parent is specified. */
1024 if (opt_sig_parent) {
1025 ppid = getppid();
1026 }
1027
fac6795d
DG
1028 while (1) {
1029 /* Create thread to manage the client socket */
1030 ret = pthread_create(&threads[0], NULL, thread_manage_clients, (void *) NULL);
1031 if (ret != 0) {
1032 perror("pthread_create");
1033 goto error;
1034 }
1035
1036 /* Create thread to manage application socket */
1037 ret = pthread_create(&threads[1], NULL, thread_manage_apps, (void *) NULL);
1038 if (ret != 0) {
1039 perror("pthread_create");
1040 goto error;
1041 }
1042
1043 for (i = 0; i < 2; i++) {
1044 ret = pthread_join(threads[i], &status);
1045 if (ret != 0) {
1046 perror("pthread_join");
1047 goto error;
1048 }
1049 }
1050 }
1051
1052 cleanup();
1053 return 0;
1054
1055error:
1056 cleanup();
1057
1058 return EXIT_FAILURE;
1059}
This page took 0.112291 seconds and 4 git commands to generate.