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