2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
22 #include <sys/mount.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <sys/types.h>
28 #include <urcu/uatomic.h>
32 #include <common/common.h>
33 #include <common/compat/socket.h>
34 #include <common/compat/getenv.h>
35 #include <common/defaults.h>
36 #include <common/kernel-consumer/kernel-consumer.h>
37 #include <common/futex.h>
38 #include <common/relayd/relayd.h>
39 #include <common/utils.h>
40 #include <common/path.h>
41 #include <common/daemonize.h>
42 #include <common/config/session-config.h>
43 #include <common/dynamic-buffer.h>
44 #include <lttng/event-internal.h>
46 #include "lttng-sessiond.h"
47 #include "buffer-registry.h"
54 #include "kernel-consumer.h"
56 #include "lttng-ust-ctl.h"
57 #include "ust-consumer.h"
60 #include "health-sessiond.h"
61 #include "testpoint.h"
62 #include "notify-apps.h"
63 #include "agent-thread.h"
65 #include "notification-thread.h"
66 #include "notification-thread-commands.h"
67 #include "rotation-thread.h"
69 #include "ht-cleanup.h"
70 #include "sessiond-config.h"
76 #include "manage-apps.h"
77 #include "manage-kernel.h"
79 static const char *help_msg
=
80 #ifdef LTTNG_EMBED_HELP
81 #include <lttng-sessiond.8.h>
88 static int lockfile_fd
= -1;
89 static int opt_print_version
;
91 /* Set to 1 when a SIGUSR1 signal is received. */
92 static int recv_child_signal
;
94 /* Command line options */
95 static const struct option long_options
[] = {
96 { "client-sock", required_argument
, 0, 'c' },
97 { "apps-sock", required_argument
, 0, 'a' },
98 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
99 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
100 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
101 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
102 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
103 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
104 { "consumerd32-path", required_argument
, 0, '\0' },
105 { "consumerd32-libdir", required_argument
, 0, '\0' },
106 { "consumerd64-path", required_argument
, 0, '\0' },
107 { "consumerd64-libdir", required_argument
, 0, '\0' },
108 { "daemonize", no_argument
, 0, 'd' },
109 { "background", no_argument
, 0, 'b' },
110 { "sig-parent", no_argument
, 0, 'S' },
111 { "help", no_argument
, 0, 'h' },
112 { "group", required_argument
, 0, 'g' },
113 { "version", no_argument
, 0, 'V' },
114 { "quiet", no_argument
, 0, 'q' },
115 { "verbose", no_argument
, 0, 'v' },
116 { "verbose-consumer", no_argument
, 0, '\0' },
117 { "no-kernel", no_argument
, 0, '\0' },
118 { "pidfile", required_argument
, 0, 'p' },
119 { "agent-tcp-port", required_argument
, 0, '\0' },
120 { "config", required_argument
, 0, 'f' },
121 { "load", required_argument
, 0, 'l' },
122 { "kmod-probes", required_argument
, 0, '\0' },
123 { "extra-kmod-probes", required_argument
, 0, '\0' },
127 /* Command line options to ignore from configuration file */
128 static const char *config_ignore_options
[] = { "help", "version", "config" };
131 * This pipe is used to inform the thread managing application communication
132 * that a command is queued and ready to be processed.
134 static int apps_cmd_pipe
[2] = { -1, -1 };
135 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
138 * UST registration command queue. This queue is tied with a futex and uses a N
139 * wakers / 1 waiter implemented and detailed in futex.c/.h
141 * The thread_registration_apps and thread_dispatch_ust_registration uses this
142 * queue along with the wait/wake scheme. The thread_manage_apps receives down
143 * the line new application socket and monitors it for any I/O error or clean
144 * close that triggers an unregistration of the application.
146 static struct ust_cmd_queue ust_cmd_queue
;
149 * Section name to look for in the daemon configuration file.
151 static const char * const config_section_name
= "sessiond";
153 /* Am I root or not. Set to 1 if the daemon is running as root */
157 * Stop all threads by closing the thread quit pipe.
159 static void stop_threads(void)
163 /* Stopping all threads */
164 DBG("Terminating all threads");
165 ret
= sessiond_notify_quit_pipe();
167 ERR("write error on thread quit pipe");
172 * Close every consumer sockets.
174 static void close_consumer_sockets(void)
178 if (kconsumer_data
.err_sock
>= 0) {
179 ret
= close(kconsumer_data
.err_sock
);
181 PERROR("kernel consumer err_sock close");
184 if (ustconsumer32_data
.err_sock
>= 0) {
185 ret
= close(ustconsumer32_data
.err_sock
);
187 PERROR("UST consumerd32 err_sock close");
190 if (ustconsumer64_data
.err_sock
>= 0) {
191 ret
= close(ustconsumer64_data
.err_sock
);
193 PERROR("UST consumerd64 err_sock close");
196 if (kconsumer_data
.cmd_sock
>= 0) {
197 ret
= close(kconsumer_data
.cmd_sock
);
199 PERROR("kernel consumer cmd_sock close");
202 if (ustconsumer32_data
.cmd_sock
>= 0) {
203 ret
= close(ustconsumer32_data
.cmd_sock
);
205 PERROR("UST consumerd32 cmd_sock close");
208 if (ustconsumer64_data
.cmd_sock
>= 0) {
209 ret
= close(ustconsumer64_data
.cmd_sock
);
211 PERROR("UST consumerd64 cmd_sock close");
214 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
215 ret
= close(kconsumer_data
.channel_monitor_pipe
);
217 PERROR("kernel consumer channel monitor pipe close");
220 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
221 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
223 PERROR("UST consumerd32 channel monitor pipe close");
226 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
227 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
229 PERROR("UST consumerd64 channel monitor pipe close");
235 * Wait on consumer process termination.
237 * Need to be called with the consumer data lock held or from a context
238 * ensuring no concurrent access to data (e.g: cleanup).
240 static void wait_consumer(struct consumer_data
*consumer_data
)
245 if (consumer_data
->pid
<= 0) {
249 DBG("Waiting for complete teardown of consumerd (PID: %d)",
251 ret
= waitpid(consumer_data
->pid
, &status
, 0);
253 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
254 } else if (!WIFEXITED(status
)) {
255 ERR("consumerd termination with error: %d",
258 consumer_data
->pid
= 0;
262 * Cleanup the session daemon's data structures.
264 static void sessiond_cleanup(void)
267 struct ltt_session_list
*session_list
= session_get_list();
269 DBG("Cleanup sessiond");
272 * Close the thread quit pipe. It has already done its job,
273 * since we are now called.
275 sessiond_close_quit_pipe();
276 utils_close_pipe(apps_cmd_pipe
);
277 utils_close_pipe(apps_cmd_notify_pipe
);
278 utils_close_pipe(kernel_poll_pipe
);
280 ret
= remove(config
.pid_file_path
.value
);
282 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
285 DBG("Removing sessiond and consumerd content of directory %s",
286 config
.rundir
.value
);
289 DBG("Removing %s", config
.pid_file_path
.value
);
290 (void) unlink(config
.pid_file_path
.value
);
292 DBG("Removing %s", config
.agent_port_file_path
.value
);
293 (void) unlink(config
.agent_port_file_path
.value
);
296 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
297 (void) unlink(kconsumer_data
.err_unix_sock_path
);
299 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
300 (void) rmdir(config
.kconsumerd_path
.value
);
302 /* ust consumerd 32 */
303 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
304 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
306 DBG("Removing directory %s", config
.consumerd32_path
.value
);
307 (void) rmdir(config
.consumerd32_path
.value
);
309 /* ust consumerd 64 */
310 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
311 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
313 DBG("Removing directory %s", config
.consumerd64_path
.value
);
314 (void) rmdir(config
.consumerd64_path
.value
);
316 pthread_mutex_destroy(&session_list
->lock
);
318 DBG("Cleaning up all agent apps");
319 agent_app_ht_clean();
320 DBG("Closing all UST sockets");
321 ust_app_clean_list();
322 buffer_reg_destroy_registries();
324 close_consumer_sockets();
326 wait_consumer(&kconsumer_data
);
327 wait_consumer(&ustconsumer64_data
);
328 wait_consumer(&ustconsumer32_data
);
330 if (is_root
&& !config
.no_kernel
) {
331 cleanup_kernel_tracer();
335 * We do NOT rmdir rundir because there are other processes
336 * using it, for instance lttng-relayd, which can start in
337 * parallel with this teardown.
342 * Cleanup the daemon's option data structures.
344 static void sessiond_cleanup_options(void)
346 DBG("Cleaning up options");
348 sessiond_config_fini(&config
);
350 run_as_destroy_worker();
353 static int string_match(const char *str1
, const char *str2
)
355 return (str1
&& str2
) && !strcmp(str1
, str2
);
359 * Take an option from the getopt output and set it in the right variable to be
362 * Return 0 on success else a negative value.
364 static int set_option(int opt
, const char *arg
, const char *optname
)
368 if (string_match(optname
, "client-sock") || opt
== 'c') {
369 if (!arg
|| *arg
== '\0') {
373 if (lttng_is_setuid_setgid()) {
374 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
375 "-c, --client-sock");
377 config_string_set(&config
.client_unix_sock_path
,
379 if (!config
.client_unix_sock_path
.value
) {
384 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
385 if (!arg
|| *arg
== '\0') {
389 if (lttng_is_setuid_setgid()) {
390 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
393 config_string_set(&config
.apps_unix_sock_path
,
395 if (!config
.apps_unix_sock_path
.value
) {
400 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
401 config
.daemonize
= true;
402 } else if (string_match(optname
, "background") || opt
== 'b') {
403 config
.background
= true;
404 } else if (string_match(optname
, "group") || opt
== 'g') {
405 if (!arg
|| *arg
== '\0') {
409 if (lttng_is_setuid_setgid()) {
410 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
413 config_string_set(&config
.tracing_group_name
,
415 if (!config
.tracing_group_name
.value
) {
420 } else if (string_match(optname
, "help") || opt
== 'h') {
421 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
423 ERR("Cannot show --help for `lttng-sessiond`");
426 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
427 } else if (string_match(optname
, "version") || opt
== 'V') {
428 opt_print_version
= 1;
429 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
430 config
.sig_parent
= true;
431 } else if (string_match(optname
, "kconsumerd-err-sock")) {
432 if (!arg
|| *arg
== '\0') {
436 if (lttng_is_setuid_setgid()) {
437 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
438 "--kconsumerd-err-sock");
440 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
442 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
447 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
448 if (!arg
|| *arg
== '\0') {
452 if (lttng_is_setuid_setgid()) {
453 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
454 "--kconsumerd-cmd-sock");
456 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
458 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
463 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
464 if (!arg
|| *arg
== '\0') {
468 if (lttng_is_setuid_setgid()) {
469 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
470 "--ustconsumerd64-err-sock");
472 config_string_set(&config
.consumerd64_err_unix_sock_path
,
474 if (!config
.consumerd64_err_unix_sock_path
.value
) {
479 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
480 if (!arg
|| *arg
== '\0') {
484 if (lttng_is_setuid_setgid()) {
485 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
486 "--ustconsumerd64-cmd-sock");
488 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
490 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
495 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
496 if (!arg
|| *arg
== '\0') {
500 if (lttng_is_setuid_setgid()) {
501 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
502 "--ustconsumerd32-err-sock");
504 config_string_set(&config
.consumerd32_err_unix_sock_path
,
506 if (!config
.consumerd32_err_unix_sock_path
.value
) {
511 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
512 if (!arg
|| *arg
== '\0') {
516 if (lttng_is_setuid_setgid()) {
517 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
518 "--ustconsumerd32-cmd-sock");
520 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
522 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
527 } else if (string_match(optname
, "no-kernel")) {
528 config
.no_kernel
= true;
529 } else if (string_match(optname
, "quiet") || opt
== 'q') {
531 } else if (string_match(optname
, "verbose") || opt
== 'v') {
532 /* Verbose level can increase using multiple -v */
534 /* Value obtained from config file */
535 config
.verbose
= config_parse_value(arg
);
537 /* -v used on command line */
540 /* Clamp value to [0, 3] */
541 config
.verbose
= config
.verbose
< 0 ? 0 :
542 (config
.verbose
<= 3 ? config
.verbose
: 3);
543 } else if (string_match(optname
, "verbose-consumer")) {
545 config
.verbose_consumer
= config_parse_value(arg
);
547 config
.verbose_consumer
++;
549 } else if (string_match(optname
, "consumerd32-path")) {
550 if (!arg
|| *arg
== '\0') {
554 if (lttng_is_setuid_setgid()) {
555 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
556 "--consumerd32-path");
558 config_string_set(&config
.consumerd32_bin_path
,
560 if (!config
.consumerd32_bin_path
.value
) {
565 } else if (string_match(optname
, "consumerd32-libdir")) {
566 if (!arg
|| *arg
== '\0') {
570 if (lttng_is_setuid_setgid()) {
571 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
572 "--consumerd32-libdir");
574 config_string_set(&config
.consumerd32_lib_dir
,
576 if (!config
.consumerd32_lib_dir
.value
) {
581 } else if (string_match(optname
, "consumerd64-path")) {
582 if (!arg
|| *arg
== '\0') {
586 if (lttng_is_setuid_setgid()) {
587 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
588 "--consumerd64-path");
590 config_string_set(&config
.consumerd64_bin_path
,
592 if (!config
.consumerd64_bin_path
.value
) {
597 } else if (string_match(optname
, "consumerd64-libdir")) {
598 if (!arg
|| *arg
== '\0') {
602 if (lttng_is_setuid_setgid()) {
603 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
604 "--consumerd64-libdir");
606 config_string_set(&config
.consumerd64_lib_dir
,
608 if (!config
.consumerd64_lib_dir
.value
) {
613 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
614 if (!arg
|| *arg
== '\0') {
618 if (lttng_is_setuid_setgid()) {
619 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
622 config_string_set(&config
.pid_file_path
, strdup(arg
));
623 if (!config
.pid_file_path
.value
) {
628 } else if (string_match(optname
, "agent-tcp-port")) {
629 if (!arg
|| *arg
== '\0') {
633 if (lttng_is_setuid_setgid()) {
634 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
640 v
= strtoul(arg
, NULL
, 0);
641 if (errno
!= 0 || !isdigit(arg
[0])) {
642 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
645 if (v
== 0 || v
>= 65535) {
646 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
649 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
650 DBG3("Agent TCP port set to non default: %i", (int) v
);
652 } else if (string_match(optname
, "load") || opt
== 'l') {
653 if (!arg
|| *arg
== '\0') {
657 if (lttng_is_setuid_setgid()) {
658 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
661 config_string_set(&config
.load_session_path
, strdup(arg
));
662 if (!config
.load_session_path
.value
) {
667 } else if (string_match(optname
, "kmod-probes")) {
668 if (!arg
|| *arg
== '\0') {
672 if (lttng_is_setuid_setgid()) {
673 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
676 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
677 if (!config
.kmod_probes_list
.value
) {
682 } else if (string_match(optname
, "extra-kmod-probes")) {
683 if (!arg
|| *arg
== '\0') {
687 if (lttng_is_setuid_setgid()) {
688 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
689 "--extra-kmod-probes");
691 config_string_set(&config
.kmod_extra_probes_list
,
693 if (!config
.kmod_extra_probes_list
.value
) {
698 } else if (string_match(optname
, "config") || opt
== 'f') {
699 /* This is handled in set_options() thus silent skip. */
702 /* Unknown option or other error.
703 * Error is printed by getopt, just return */
708 if (ret
== -EINVAL
) {
709 const char *opt_name
= "unknown";
712 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
714 if (opt
== long_options
[i
].val
) {
715 opt_name
= long_options
[i
].name
;
720 WARN("Invalid argument provided for option \"%s\", using default value.",
728 * config_entry_handler_cb used to handle options read from a config file.
729 * See config_entry_handler_cb comment in common/config/session-config.h for the
730 * return value conventions.
732 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
736 if (!entry
|| !entry
->name
|| !entry
->value
) {
741 /* Check if the option is to be ignored */
742 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
743 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
748 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
751 /* Ignore if not fully matched. */
752 if (strcmp(entry
->name
, long_options
[i
].name
)) {
757 * If the option takes no argument on the command line, we have to
758 * check if the value is "true". We support non-zero numeric values,
761 if (!long_options
[i
].has_arg
) {
762 ret
= config_parse_value(entry
->value
);
765 WARN("Invalid configuration value \"%s\" for option %s",
766 entry
->value
, entry
->name
);
768 /* False, skip boolean config option. */
773 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
777 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
783 static void print_version(void) {
784 fprintf(stdout
, "%s\n", VERSION
);
788 * daemon configuration loading and argument parsing
790 static int set_options(int argc
, char **argv
)
792 int ret
= 0, c
= 0, option_index
= 0;
793 int orig_optopt
= optopt
, orig_optind
= optind
;
795 char *config_path
= NULL
;
797 optstring
= utils_generate_optstring(long_options
,
798 sizeof(long_options
) / sizeof(struct option
));
804 /* Check for the --config option */
805 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
806 &option_index
)) != -1) {
810 } else if (c
!= 'f') {
811 /* if not equal to --config option. */
815 if (lttng_is_setuid_setgid()) {
816 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
820 config_path
= utils_expand_path(optarg
);
822 ERR("Failed to resolve path: %s", optarg
);
827 ret
= config_get_section_entries(config_path
, config_section_name
,
828 config_entry_handler
, NULL
);
831 ERR("Invalid configuration option at line %i", ret
);
837 /* Reset getopt's global state */
838 optopt
= orig_optopt
;
839 optind
= orig_optind
;
843 * getopt_long() will not set option_index if it encounters a
846 c
= getopt_long(argc
, argv
, optstring
, long_options
,
853 * Pass NULL as the long option name if popt left the index
856 ret
= set_option(c
, optarg
,
857 option_index
< 0 ? NULL
:
858 long_options
[option_index
].name
);
871 * Create lockfile using the rundir and return its fd.
873 static int create_lockfile(void)
875 return utils_create_lock_file(config
.lock_file_path
.value
);
879 * Check if the global socket is available, and if a daemon is answering at the
880 * other side. If yes, error is returned.
882 * Also attempts to create and hold the lock file.
884 static int check_existing_daemon(void)
888 /* Is there anybody out there ? */
889 if (lttng_session_daemon_alive()) {
894 lockfile_fd
= create_lockfile();
895 if (lockfile_fd
< 0) {
903 static void sessiond_cleanup_lock_file(void)
908 * Cleanup lock file by deleting it and finaly closing it which will
909 * release the file system lock.
911 if (lockfile_fd
>= 0) {
912 ret
= remove(config
.lock_file_path
.value
);
914 PERROR("remove lock file");
916 ret
= close(lockfile_fd
);
918 PERROR("close lock file");
924 * Set the tracing group gid onto the client socket.
926 * Race window between mkdir and chown is OK because we are going from more
927 * permissive (root.root) to less permissive (root.tracing).
929 static int set_permissions(char *rundir
)
934 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true, &gid
);
936 /* Default to root group. */
940 /* Set lttng run dir */
941 ret
= chown(rundir
, 0, gid
);
943 ERR("Unable to set group on %s", rundir
);
948 * Ensure all applications and tracing group can search the run
949 * dir. Allow everyone to read the directory, since it does not
950 * buy us anything to hide its content.
952 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
954 ERR("Unable to set permissions on %s", rundir
);
958 /* lttng client socket path */
959 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
961 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
965 /* kconsumer error socket path */
966 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
968 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
972 /* 64-bit ustconsumer error socket path */
973 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
975 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
979 /* 32-bit ustconsumer compat32 error socket path */
980 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
982 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
986 DBG("All permissions are set");
992 * Create the lttng run directory needed for all global sockets and pipe.
994 static int create_lttng_rundir(void)
998 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
1000 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
1002 if (errno
!= EEXIST
) {
1003 ERR("Unable to create %s", config
.rundir
.value
);
1015 * Setup sockets and directory needed by the consumerds' communication with the
1018 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1023 switch (consumer_data
->type
) {
1024 case LTTNG_CONSUMER_KERNEL
:
1025 path
= config
.kconsumerd_path
.value
;
1027 case LTTNG_CONSUMER64_UST
:
1028 path
= config
.consumerd64_path
.value
;
1030 case LTTNG_CONSUMER32_UST
:
1031 path
= config
.consumerd32_path
.value
;
1034 ERR("Consumer type unknown");
1040 DBG2("Creating consumer directory: %s", path
);
1042 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1043 if (ret
< 0 && errno
!= EEXIST
) {
1045 ERR("Failed to create %s", path
);
1051 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
1054 /* Default to root group. */
1058 ret
= chown(path
, 0, gid
);
1060 ERR("Unable to set group on %s", path
);
1066 /* Create the consumerd error unix socket */
1067 consumer_data
->err_sock
=
1068 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1069 if (consumer_data
->err_sock
< 0) {
1070 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1076 * Set the CLOEXEC flag. Return code is useless because either way, the
1079 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1081 PERROR("utils_set_fd_cloexec");
1082 /* continue anyway */
1085 /* File permission MUST be 660 */
1086 ret
= chmod(consumer_data
->err_unix_sock_path
,
1087 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1089 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1099 * Signal handler for the daemon
1101 * Simply stop all worker threads, leaving main() return gracefully after
1102 * joining all threads and calling cleanup().
1104 static void sighandler(int sig
)
1108 DBG("SIGINT caught");
1112 DBG("SIGTERM caught");
1116 CMM_STORE_SHARED(recv_child_signal
, 1);
1124 * Setup signal handler for :
1125 * SIGINT, SIGTERM, SIGPIPE
1127 static int set_signal_handler(void)
1130 struct sigaction sa
;
1133 if ((ret
= sigemptyset(&sigset
)) < 0) {
1134 PERROR("sigemptyset");
1138 sa
.sa_mask
= sigset
;
1141 sa
.sa_handler
= sighandler
;
1142 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1143 PERROR("sigaction");
1147 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1148 PERROR("sigaction");
1152 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1153 PERROR("sigaction");
1157 sa
.sa_handler
= SIG_IGN
;
1158 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1159 PERROR("sigaction");
1163 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1169 * Set open files limit to unlimited. This daemon can open a large number of
1170 * file descriptors in order to consume multiple kernel traces.
1172 static void set_ulimit(void)
1177 /* The kernel does not allow an infinite limit for open files */
1178 lim
.rlim_cur
= 65535;
1179 lim
.rlim_max
= 65535;
1181 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1183 PERROR("failed to set open files limit");
1187 static int write_pidfile(void)
1189 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1192 static int set_clock_plugin_env(void)
1195 char *env_value
= NULL
;
1197 if (!config
.lttng_ust_clock_plugin
.value
) {
1201 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1202 config
.lttng_ust_clock_plugin
.value
);
1208 ret
= putenv(env_value
);
1211 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1215 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1216 config
.lttng_ust_clock_plugin
.value
);
1221 static void destroy_all_sessions_and_wait(void)
1223 struct ltt_session
*session
, *tmp
;
1224 struct ltt_session_list
*session_list
;
1226 session_list
= session_get_list();
1227 DBG("Initiating destruction of all sessions");
1229 if (!session_list
) {
1233 session_lock_list();
1234 /* Initiate the destruction of all sessions. */
1235 cds_list_for_each_entry_safe(session
, tmp
,
1236 &session_list
->head
, list
) {
1237 if (!session_get(session
)) {
1241 session_lock(session
);
1242 if (session
->destroyed
) {
1243 goto unlock_session
;
1245 (void) cmd_stop_trace(session
);
1246 (void) cmd_destroy_session(session
, notification_thread_handle
,
1249 session_unlock(session
);
1250 session_put(session
);
1252 session_unlock_list();
1254 /* Wait for the destruction of all sessions to complete. */
1255 DBG("Waiting for the destruction of all sessions to complete");
1256 session_list_wait_empty();
1257 DBG("Destruction of all sessions completed");
1260 static int run_as_worker_post_fork_cleanup(void *data
)
1262 struct sessiond_config
*sessiond_config
= data
;
1264 sessiond_config_fini(sessiond_config
);
1268 static int launch_run_as_worker(const char *procname
)
1271 * Clean-up before forking the run-as worker. Any dynamically
1272 * allocated memory of which the worker is not aware will
1273 * be leaked as the process forks a run-as worker (and performs
1274 * no exec*()). The same would apply to any opened fd.
1276 return run_as_create_worker(procname
, run_as_worker_post_fork_cleanup
,
1280 static void sessiond_uuid_log(void)
1282 char uuid_str
[LTTNG_UUID_STR_LEN
];
1284 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
1285 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1291 int main(int argc
, char **argv
)
1293 int ret
= 0, retval
= 0;
1294 const char *env_app_timeout
;
1295 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1296 *ust64_channel_monitor_pipe
= NULL
,
1297 *kernel_channel_monitor_pipe
= NULL
;
1298 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1299 struct timer_thread_parameters timer_thread_parameters
;
1300 /* Rotation thread handle. */
1301 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1302 /* Queue of rotation jobs populated by the sessiond-timer. */
1303 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1304 struct lttng_thread
*client_thread
= NULL
;
1305 struct lttng_thread
*notification_thread
= NULL
;
1306 struct lttng_thread
*register_apps_thread
= NULL
;
1308 init_kernel_workarounds();
1310 rcu_register_thread();
1312 if (set_signal_handler()) {
1314 goto exit_set_signal_handler
;
1317 if (timer_signal_init()) {
1319 goto exit_set_signal_handler
;
1322 page_size
= sysconf(_SC_PAGESIZE
);
1323 if (page_size
< 0) {
1324 PERROR("sysconf _SC_PAGESIZE");
1325 page_size
= LONG_MAX
;
1326 WARN("Fallback page size to %ld", page_size
);
1329 ret
= sessiond_config_init(&config
);
1332 goto exit_set_signal_handler
;
1336 * Init config from environment variables.
1337 * Command line option override env configuration per-doc. Do env first.
1339 sessiond_config_apply_env_config(&config
);
1342 * Parse arguments and load the daemon configuration file.
1344 * We have an exit_options exit path to free memory reserved by
1345 * set_options. This is needed because the rest of sessiond_cleanup()
1346 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1347 * depends on set_options.
1350 if (set_options(argc
, argv
)) {
1356 * Resolve all paths received as arguments, configuration option, or
1357 * through environment variable as absolute paths. This is necessary
1358 * since daemonizing causes the sessiond's current working directory
1361 ret
= sessiond_config_resolve_paths(&config
);
1367 lttng_opt_verbose
= config
.verbose
;
1368 lttng_opt_quiet
= config
.quiet
;
1369 kconsumer_data
.err_unix_sock_path
=
1370 config
.kconsumerd_err_unix_sock_path
.value
;
1371 kconsumer_data
.cmd_unix_sock_path
=
1372 config
.kconsumerd_cmd_unix_sock_path
.value
;
1373 ustconsumer32_data
.err_unix_sock_path
=
1374 config
.consumerd32_err_unix_sock_path
.value
;
1375 ustconsumer32_data
.cmd_unix_sock_path
=
1376 config
.consumerd32_cmd_unix_sock_path
.value
;
1377 ustconsumer64_data
.err_unix_sock_path
=
1378 config
.consumerd64_err_unix_sock_path
.value
;
1379 ustconsumer64_data
.cmd_unix_sock_path
=
1380 config
.consumerd64_cmd_unix_sock_path
.value
;
1381 set_clock_plugin_env();
1383 sessiond_config_log(&config
);
1384 sessiond_uuid_log();
1386 if (opt_print_version
) {
1392 if (create_lttng_rundir()) {
1397 /* Abort launch if a session daemon is already running. */
1398 if (check_existing_daemon()) {
1399 ERR("A session daemon is already running.");
1405 if (config
.daemonize
|| config
.background
) {
1408 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
1409 !config
.background
);
1416 * We are in the child. Make sure all other file descriptors are
1417 * closed, in case we are called with more opened file
1418 * descriptors than the standard ones and the lock file.
1420 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1421 if (i
== lockfile_fd
) {
1428 if (launch_run_as_worker(argv
[0]) < 0) {
1429 goto exit_create_run_as_worker_cleanup
;
1433 * Starting from here, we can create threads. This needs to be after
1434 * lttng_daemonize due to RCU.
1438 * Initialize the health check subsystem. This call should set the
1439 * appropriate time values.
1441 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1442 if (!health_sessiond
) {
1443 PERROR("health_app_create error");
1448 /* Create thread to clean up RCU hash tables */
1449 ht_cleanup_thread
= launch_ht_cleanup_thread();
1450 if (!ht_cleanup_thread
) {
1455 /* Create thread quit pipe */
1456 if (sessiond_init_thread_quit_pipe()) {
1461 /* Check if daemon is UID = 0 */
1462 is_root
= !getuid();
1464 /* Create global run dir with root access */
1466 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1467 if (!kernel_channel_monitor_pipe
) {
1468 ERR("Failed to create kernel consumer channel monitor pipe");
1472 kconsumer_data
.channel_monitor_pipe
=
1473 lttng_pipe_release_writefd(
1474 kernel_channel_monitor_pipe
);
1475 if (kconsumer_data
.channel_monitor_pipe
< 0) {
1481 /* Set consumer initial state */
1482 kernel_consumerd_state
= CONSUMER_STOPPED
;
1483 ust_consumerd_state
= CONSUMER_STOPPED
;
1485 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1486 if (!ust32_channel_monitor_pipe
) {
1487 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1491 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1492 ust32_channel_monitor_pipe
);
1493 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
1499 * The rotation_thread_timer_queue structure is shared between the
1500 * sessiond timer thread and the rotation thread. The main thread keeps
1501 * its ownership and destroys it when both threads have been joined.
1503 rotation_timer_queue
= rotation_thread_timer_queue_create();
1504 if (!rotation_timer_queue
) {
1508 timer_thread_parameters
.rotation_thread_job_queue
=
1509 rotation_timer_queue
;
1511 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1512 if (!ust64_channel_monitor_pipe
) {
1513 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1517 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1518 ust64_channel_monitor_pipe
);
1519 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
1525 * Init UST app hash table. Alloc hash table before this point since
1526 * cleanup() can get called after that point.
1528 if (ust_app_ht_alloc()) {
1529 ERR("Failed to allocate UST app hash table");
1535 * Initialize agent app hash table. We allocate the hash table here
1536 * since cleanup() can get called after this point.
1538 if (agent_app_ht_alloc()) {
1539 ERR("Failed to allocate Agent app hash table");
1545 * These actions must be executed as root. We do that *after* setting up
1546 * the sockets path because we MUST make the check for another daemon using
1547 * those paths *before* trying to set the kernel consumer sockets and init
1551 if (set_consumer_sockets(&kconsumer_data
)) {
1556 /* Setup kernel tracer */
1557 if (!config
.no_kernel
) {
1558 init_kernel_tracer();
1561 /* Set ulimit for open files */
1564 /* init lttng_fd tracking must be done after set_ulimit. */
1567 if (set_consumer_sockets(&ustconsumer64_data
)) {
1572 if (set_consumer_sockets(&ustconsumer32_data
)) {
1577 /* Get parent pid if -S, --sig-parent is specified. */
1578 if (config
.sig_parent
) {
1582 /* Setup the kernel pipe for waking up the kernel thread */
1583 if (is_root
&& !config
.no_kernel
) {
1584 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
1590 /* Setup the thread apps communication pipe. */
1591 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1596 /* Setup the thread apps notify communication pipe. */
1597 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1602 /* Initialize global buffer per UID and PID registry. */
1603 buffer_reg_init_uid_registry();
1604 buffer_reg_init_pid_registry();
1606 /* Init UST command queue. */
1607 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1611 /* Check for the application socket timeout env variable. */
1612 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1613 if (env_app_timeout
) {
1614 config
.app_socket_timeout
= atoi(env_app_timeout
);
1616 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1619 ret
= write_pidfile();
1621 ERR("Error in write_pidfile");
1626 /* Initialize communication library */
1628 /* Initialize TCP timeout values */
1629 lttcomm_inet_init();
1631 /* Create health-check thread. */
1632 if (!launch_health_management_thread()) {
1637 /* notification_thread_data acquires the pipes' read side. */
1638 notification_thread_handle
= notification_thread_handle_create(
1639 ust32_channel_monitor_pipe
,
1640 ust64_channel_monitor_pipe
,
1641 kernel_channel_monitor_pipe
);
1642 if (!notification_thread_handle
) {
1644 ERR("Failed to create notification thread shared data");
1648 /* Create notification thread. */
1649 notification_thread
= launch_notification_thread(
1650 notification_thread_handle
);
1651 if (!notification_thread
) {
1656 /* Create timer thread. */
1657 if (!launch_timer_thread(&timer_thread_parameters
)) {
1662 /* rotation_thread_data acquires the pipes' read side. */
1663 rotation_thread_handle
= rotation_thread_handle_create(
1664 rotation_timer_queue
,
1665 notification_thread_handle
);
1666 if (!rotation_thread_handle
) {
1668 ERR("Failed to create rotation thread shared data");
1673 /* Create rotation thread. */
1674 if (!launch_rotation_thread(rotation_thread_handle
)) {
1679 /* Create thread to manage the client socket */
1680 client_thread
= launch_client_thread();
1681 if (!client_thread
) {
1686 /* Set credentials of the client socket and rundir */
1687 if (is_root
&& set_permissions(config
.rundir
.value
)) {
1692 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1693 apps_cmd_notify_pipe
[1])) {
1698 /* Create thread to manage application registration. */
1699 register_apps_thread
= launch_application_registration_thread(
1701 if (!register_apps_thread
) {
1706 /* Create thread to manage application socket */
1707 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1712 /* Create thread to manage application notify socket */
1713 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1718 /* Create agent management thread. */
1719 if (!launch_agent_management_thread()) {
1724 /* Don't start this thread if kernel tracing is not requested nor root */
1725 if (is_root
&& !config
.no_kernel
) {
1726 /* Create kernel thread to manage kernel event */
1727 if (!launch_kernel_management_thread(kernel_poll_pipe
[0])) {
1733 /* Load sessions. */
1734 ret
= config_load_session(config
.load_session_path
.value
,
1737 ERR("Session load failed: %s", error_get_str(ret
));
1742 /* Initialization completed. */
1743 sessiond_signal_parents();
1746 * This is where we start awaiting program completion (e.g. through
1747 * signal that asks threads to teardown).
1750 /* Initiate teardown once activity occurs on the quit pipe. */
1751 sessiond_wait_for_quit_pipe(-1);
1755 * Ensure that the client thread is no longer accepting new commands,
1756 * which could cause new sessions to be created.
1758 if (client_thread
) {
1759 lttng_thread_shutdown(client_thread
);
1760 lttng_thread_put(client_thread
);
1763 destroy_all_sessions_and_wait();
1765 if (register_apps_thread
) {
1766 lttng_thread_shutdown(register_apps_thread
);
1767 lttng_thread_put(register_apps_thread
);
1769 lttng_thread_list_shutdown_orphans();
1772 * Wait for all pending call_rcu work to complete before tearing
1773 * down data structures. call_rcu worker may be trying to
1774 * perform lookups in those structures.
1778 * sessiond_cleanup() is called when no other thread is running, except
1779 * the ht_cleanup thread, which is needed to destroy the hash tables.
1781 rcu_thread_online();
1784 if (notification_thread
) {
1785 lttng_thread_shutdown(notification_thread
);
1786 lttng_thread_put(notification_thread
);
1790 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1791 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1792 * the queue is empty before shutting down the clean-up thread.
1796 if (ht_cleanup_thread
) {
1797 lttng_thread_shutdown(ht_cleanup_thread
);
1798 lttng_thread_put(ht_cleanup_thread
);
1801 rcu_thread_offline();
1802 rcu_unregister_thread();
1804 if (rotation_thread_handle
) {
1805 rotation_thread_handle_destroy(rotation_thread_handle
);
1809 * After the rotation and timer thread have quit, we can safely destroy
1810 * the rotation_timer_queue.
1812 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1814 * The teardown of the notification system is performed after the
1815 * session daemon's teardown in order to allow it to be notified
1816 * of the active session and channels at the moment of the teardown.
1818 if (notification_thread_handle
) {
1819 notification_thread_handle_destroy(notification_thread_handle
);
1821 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1822 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1823 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1825 if (health_sessiond
) {
1826 health_app_destroy(health_sessiond
);
1828 exit_create_run_as_worker_cleanup
:
1830 sessiond_cleanup_lock_file();
1831 sessiond_cleanup_options();
1833 exit_set_signal_handler
: