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