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