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