2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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/daemonize.h>
41 #include <common/config/session-config.h>
42 #include <common/dynamic-buffer.h>
43 #include <lttng/event-internal.h>
45 #include "lttng-sessiond.h"
46 #include "buffer-registry.h"
52 #include "event-notifier-error-accounting.h"
54 #include "kernel-consumer.h"
55 #include "lttng-ust-ctl.h"
56 #include "ust-consumer.h"
59 #include "health-sessiond.h"
60 #include "testpoint.h"
61 #include "notify-apps.h"
62 #include "agent-thread.h"
64 #include "notification-thread.h"
65 #include "notification-thread-commands.h"
66 #include "rotation-thread.h"
68 #include "ht-cleanup.h"
69 #include "sessiond-config.h"
75 #include "manage-apps.h"
76 #include "manage-kernel.h"
79 static const char *help_msg
=
80 #ifdef LTTNG_EMBED_HELP
81 #include <lttng-sessiond.8.h>
87 #define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
90 static int lockfile_fd
= -1;
91 static int opt_print_version
;
93 /* Set to 1 when a SIGUSR1 signal is received. */
94 static int recv_child_signal
;
96 /* Command line options */
97 static const struct option long_options
[] = {
98 { "client-sock", required_argument
, 0, 'c' },
99 { "apps-sock", required_argument
, 0, 'a' },
100 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
101 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
102 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
103 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
104 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
105 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
106 { "consumerd32-path", required_argument
, 0, '\0' },
107 { "consumerd32-libdir", required_argument
, 0, '\0' },
108 { "consumerd64-path", required_argument
, 0, '\0' },
109 { "consumerd64-libdir", required_argument
, 0, '\0' },
110 { "daemonize", no_argument
, 0, 'd' },
111 { "background", no_argument
, 0, 'b' },
112 { "sig-parent", no_argument
, 0, 'S' },
113 { "help", no_argument
, 0, 'h' },
114 { "group", required_argument
, 0, 'g' },
115 { "version", no_argument
, 0, 'V' },
116 { "quiet", no_argument
, 0, 'q' },
117 { "verbose", no_argument
, 0, 'v' },
118 { "verbose-consumer", no_argument
, 0, '\0' },
119 { "no-kernel", no_argument
, 0, '\0' },
120 { "pidfile", required_argument
, 0, 'p' },
121 { "agent-tcp-port", required_argument
, 0, '\0' },
122 { "config", required_argument
, 0, 'f' },
123 { "load", required_argument
, 0, 'l' },
124 { "kmod-probes", required_argument
, 0, '\0' },
125 { "extra-kmod-probes", required_argument
, 0, '\0' },
126 { "event-notifier-error-number-of-bucket", required_argument
, 0, '\0' },
130 /* Command line options to ignore from configuration file */
131 static const char *config_ignore_options
[] = { "help", "version", "config" };
134 * This pipe is used to inform the thread managing application communication
135 * that a command is queued and ready to be processed.
137 static int apps_cmd_pipe
[2] = { -1, -1 };
138 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
141 * UST registration command queue. This queue is tied with a futex and uses a N
142 * wakers / 1 waiter implemented and detailed in futex.c/.h
144 * The thread_registration_apps and thread_dispatch_ust_registration uses this
145 * queue along with the wait/wake scheme. The thread_manage_apps receives down
146 * the line new application socket and monitors it for any I/O error or clean
147 * close that triggers an unregistration of the application.
149 static struct ust_cmd_queue ust_cmd_queue
;
152 * Section name to look for in the daemon configuration file.
154 static const char * const config_section_name
= "sessiond";
156 /* Am I root or not. Set to 1 if the daemon is running as root */
160 * Stop all threads by closing the thread quit pipe.
162 static void stop_threads(void)
166 /* Stopping all threads */
167 DBG("Terminating all threads");
168 ret
= sessiond_notify_quit_pipe();
170 ERR("write error on thread quit pipe");
175 * Close every consumer sockets.
177 static void close_consumer_sockets(void)
181 if (the_kconsumer_data
.err_sock
>= 0) {
182 ret
= close(the_kconsumer_data
.err_sock
);
184 PERROR("kernel consumer err_sock close");
187 if (the_ustconsumer32_data
.err_sock
>= 0) {
188 ret
= close(the_ustconsumer32_data
.err_sock
);
190 PERROR("UST consumerd32 err_sock close");
193 if (the_ustconsumer64_data
.err_sock
>= 0) {
194 ret
= close(the_ustconsumer64_data
.err_sock
);
196 PERROR("UST consumerd64 err_sock close");
199 if (the_kconsumer_data
.cmd_sock
>= 0) {
200 ret
= close(the_kconsumer_data
.cmd_sock
);
202 PERROR("kernel consumer cmd_sock close");
205 if (the_ustconsumer32_data
.cmd_sock
>= 0) {
206 ret
= close(the_ustconsumer32_data
.cmd_sock
);
208 PERROR("UST consumerd32 cmd_sock close");
211 if (the_ustconsumer64_data
.cmd_sock
>= 0) {
212 ret
= close(the_ustconsumer64_data
.cmd_sock
);
214 PERROR("UST consumerd64 cmd_sock close");
217 if (the_kconsumer_data
.channel_monitor_pipe
>= 0) {
218 ret
= close(the_kconsumer_data
.channel_monitor_pipe
);
220 PERROR("kernel consumer channel monitor pipe close");
223 if (the_ustconsumer32_data
.channel_monitor_pipe
>= 0) {
224 ret
= close(the_ustconsumer32_data
.channel_monitor_pipe
);
226 PERROR("UST consumerd32 channel monitor pipe close");
229 if (the_ustconsumer64_data
.channel_monitor_pipe
>= 0) {
230 ret
= close(the_ustconsumer64_data
.channel_monitor_pipe
);
232 PERROR("UST consumerd64 channel monitor pipe close");
238 * Wait on consumer process termination.
240 * Need to be called with the consumer data lock held or from a context
241 * ensuring no concurrent access to data (e.g: cleanup).
243 static void wait_consumer(struct consumer_data
*consumer_data
)
248 if (consumer_data
->pid
<= 0) {
252 DBG("Waiting for complete teardown of consumerd (PID: %d)",
254 ret
= waitpid(consumer_data
->pid
, &status
, 0);
256 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
257 } else if (!WIFEXITED(status
)) {
258 ERR("consumerd termination with error: %d",
261 consumer_data
->pid
= 0;
265 * Cleanup the session daemon's data structures.
267 static void sessiond_cleanup(void)
270 struct ltt_session_list
*session_list
= session_get_list();
272 DBG("Cleanup sessiond");
275 * Close the thread quit pipe. It has already done its job,
276 * since we are now called.
278 sessiond_close_quit_pipe();
279 utils_close_pipe(apps_cmd_pipe
);
280 utils_close_pipe(apps_cmd_notify_pipe
);
281 utils_close_pipe(the_kernel_poll_pipe
);
283 ret
= remove(the_config
.pid_file_path
.value
);
285 PERROR("remove pidfile %s", the_config
.pid_file_path
.value
);
288 DBG("Removing sessiond and consumerd content of directory %s",
289 the_config
.rundir
.value
);
292 DBG("Removing %s", the_config
.pid_file_path
.value
);
293 (void) unlink(the_config
.pid_file_path
.value
);
295 DBG("Removing %s", the_config
.agent_port_file_path
.value
);
296 (void) unlink(the_config
.agent_port_file_path
.value
);
299 DBG("Removing %s", the_kconsumer_data
.err_unix_sock_path
);
300 (void) unlink(the_kconsumer_data
.err_unix_sock_path
);
302 DBG("Removing directory %s", the_config
.kconsumerd_path
.value
);
303 (void) rmdir(the_config
.kconsumerd_path
.value
);
305 /* ust consumerd 32 */
306 DBG("Removing %s", the_config
.consumerd32_err_unix_sock_path
.value
);
307 (void) unlink(the_config
.consumerd32_err_unix_sock_path
.value
);
309 DBG("Removing directory %s", the_config
.consumerd32_path
.value
);
310 (void) rmdir(the_config
.consumerd32_path
.value
);
312 /* ust consumerd 64 */
313 DBG("Removing %s", the_config
.consumerd64_err_unix_sock_path
.value
);
314 (void) unlink(the_config
.consumerd64_err_unix_sock_path
.value
);
316 DBG("Removing directory %s", the_config
.consumerd64_path
.value
);
317 (void) rmdir(the_config
.consumerd64_path
.value
);
319 pthread_mutex_destroy(&session_list
->lock
);
321 DBG("Cleaning up all per-event notifier domain agents");
322 agent_by_event_notifier_domain_ht_destroy();
324 DBG("Cleaning up all agent apps");
325 agent_app_ht_clean();
326 DBG("Closing all UST sockets");
327 ust_app_clean_list();
328 buffer_reg_destroy_registries();
330 close_consumer_sockets();
332 wait_consumer(&the_kconsumer_data
);
333 wait_consumer(&the_ustconsumer64_data
);
334 wait_consumer(&the_ustconsumer32_data
);
336 if (is_root
&& !the_config
.no_kernel
) {
337 cleanup_kernel_tracer();
341 * We do NOT rmdir rundir because there are other processes
342 * using it, for instance lttng-relayd, which can start in
343 * parallel with this teardown.
348 * Cleanup the daemon's option data structures.
350 static void sessiond_cleanup_options(void)
352 DBG("Cleaning up options");
354 sessiond_config_fini(&the_config
);
356 run_as_destroy_worker();
359 static int string_match(const char *str1
, const char *str2
)
361 return (str1
&& str2
) && !strcmp(str1
, str2
);
365 * Take an option from the getopt output and set it in the right variable to be
368 * Return 0 on success else a negative value.
370 static int set_option(int opt
, const char *arg
, const char *optname
)
374 if (string_match(optname
, "client-sock") || opt
== 'c') {
375 if (!arg
|| *arg
== '\0') {
379 if (lttng_is_setuid_setgid()) {
380 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
381 "-c, --client-sock");
383 config_string_set(&the_config
.client_unix_sock_path
,
385 if (!the_config
.client_unix_sock_path
.value
) {
390 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
391 if (!arg
|| *arg
== '\0') {
395 if (lttng_is_setuid_setgid()) {
396 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
399 config_string_set(&the_config
.apps_unix_sock_path
,
401 if (!the_config
.apps_unix_sock_path
.value
) {
406 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
407 the_config
.daemonize
= true;
408 } else if (string_match(optname
, "background") || opt
== 'b') {
409 the_config
.background
= true;
410 } else if (string_match(optname
, "group") || opt
== 'g') {
411 if (!arg
|| *arg
== '\0') {
415 if (lttng_is_setuid_setgid()) {
416 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
419 config_string_set(&the_config
.tracing_group_name
,
421 if (!the_config
.tracing_group_name
.value
) {
426 } else if (string_match(optname
, "help") || opt
== 'h') {
427 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
429 ERR("Cannot show --help for `lttng-sessiond`");
432 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
433 } else if (string_match(optname
, "version") || opt
== 'V') {
434 opt_print_version
= 1;
435 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
436 the_config
.sig_parent
= true;
437 } else if (string_match(optname
, "kconsumerd-err-sock")) {
438 if (!arg
|| *arg
== '\0') {
442 if (lttng_is_setuid_setgid()) {
443 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
444 "--kconsumerd-err-sock");
447 &the_config
.kconsumerd_err_unix_sock_path
,
449 if (!the_config
.kconsumerd_err_unix_sock_path
.value
) {
454 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
455 if (!arg
|| *arg
== '\0') {
459 if (lttng_is_setuid_setgid()) {
460 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
461 "--kconsumerd-cmd-sock");
464 &the_config
.kconsumerd_cmd_unix_sock_path
,
466 if (!the_config
.kconsumerd_cmd_unix_sock_path
.value
) {
471 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
472 if (!arg
|| *arg
== '\0') {
476 if (lttng_is_setuid_setgid()) {
477 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
478 "--ustconsumerd64-err-sock");
481 &the_config
.consumerd64_err_unix_sock_path
,
483 if (!the_config
.consumerd64_err_unix_sock_path
.value
) {
488 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
489 if (!arg
|| *arg
== '\0') {
493 if (lttng_is_setuid_setgid()) {
494 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
495 "--ustconsumerd64-cmd-sock");
498 &the_config
.consumerd64_cmd_unix_sock_path
,
500 if (!the_config
.consumerd64_cmd_unix_sock_path
.value
) {
505 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
506 if (!arg
|| *arg
== '\0') {
510 if (lttng_is_setuid_setgid()) {
511 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
512 "--ustconsumerd32-err-sock");
515 &the_config
.consumerd32_err_unix_sock_path
,
517 if (!the_config
.consumerd32_err_unix_sock_path
.value
) {
522 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
523 if (!arg
|| *arg
== '\0') {
527 if (lttng_is_setuid_setgid()) {
528 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
529 "--ustconsumerd32-cmd-sock");
532 &the_config
.consumerd32_cmd_unix_sock_path
,
534 if (!the_config
.consumerd32_cmd_unix_sock_path
.value
) {
539 } else if (string_match(optname
, "no-kernel")) {
540 the_config
.no_kernel
= true;
541 } else if (string_match(optname
, "quiet") || opt
== 'q') {
542 the_config
.quiet
= true;
543 } else if (string_match(optname
, "verbose") || opt
== 'v') {
544 /* Verbose level can increase using multiple -v */
546 /* Value obtained from config file */
547 the_config
.verbose
= config_parse_value(arg
);
549 /* -v used on command line */
550 the_config
.verbose
++;
552 /* Clamp value to [0, 3] */
553 the_config
.verbose
= the_config
.verbose
< 0 ?
555 (the_config
.verbose
<= 3 ? the_config
.verbose
:
557 } else if (string_match(optname
, "verbose-consumer")) {
559 the_config
.verbose_consumer
= config_parse_value(arg
);
561 the_config
.verbose_consumer
++;
563 } else if (string_match(optname
, "consumerd32-path")) {
564 if (!arg
|| *arg
== '\0') {
568 if (lttng_is_setuid_setgid()) {
569 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
570 "--consumerd32-path");
572 config_string_set(&the_config
.consumerd32_bin_path
,
574 if (!the_config
.consumerd32_bin_path
.value
) {
579 } else if (string_match(optname
, "consumerd32-libdir")) {
580 if (!arg
|| *arg
== '\0') {
584 if (lttng_is_setuid_setgid()) {
585 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
586 "--consumerd32-libdir");
588 config_string_set(&the_config
.consumerd32_lib_dir
,
590 if (!the_config
.consumerd32_lib_dir
.value
) {
595 } else if (string_match(optname
, "consumerd64-path")) {
596 if (!arg
|| *arg
== '\0') {
600 if (lttng_is_setuid_setgid()) {
601 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
602 "--consumerd64-path");
604 config_string_set(&the_config
.consumerd64_bin_path
,
606 if (!the_config
.consumerd64_bin_path
.value
) {
611 } else if (string_match(optname
, "consumerd64-libdir")) {
612 if (!arg
|| *arg
== '\0') {
616 if (lttng_is_setuid_setgid()) {
617 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
618 "--consumerd64-libdir");
620 config_string_set(&the_config
.consumerd64_lib_dir
,
622 if (!the_config
.consumerd64_lib_dir
.value
) {
627 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
628 if (!arg
|| *arg
== '\0') {
632 if (lttng_is_setuid_setgid()) {
633 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
637 &the_config
.pid_file_path
, strdup(arg
));
638 if (!the_config
.pid_file_path
.value
) {
643 } else if (string_match(optname
, "agent-tcp-port")) {
644 if (!arg
|| *arg
== '\0') {
648 if (lttng_is_setuid_setgid()) {
649 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
655 v
= strtoul(arg
, NULL
, 0);
656 if (errno
!= 0 || !isdigit(arg
[0])) {
657 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
660 if (v
== 0 || v
>= 65535) {
661 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
664 the_config
.agent_tcp_port
.begin
=
665 the_config
.agent_tcp_port
.end
= (int) v
;
666 DBG3("Agent TCP port set to non default: %i", (int) v
);
668 } else if (string_match(optname
, "load") || opt
== 'l') {
669 if (!arg
|| *arg
== '\0') {
673 if (lttng_is_setuid_setgid()) {
674 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
677 config_string_set(&the_config
.load_session_path
,
679 if (!the_config
.load_session_path
.value
) {
684 } else if (string_match(optname
, "kmod-probes")) {
685 if (!arg
|| *arg
== '\0') {
689 if (lttng_is_setuid_setgid()) {
690 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
693 config_string_set(&the_config
.kmod_probes_list
,
695 if (!the_config
.kmod_probes_list
.value
) {
700 } else if (string_match(optname
, "extra-kmod-probes")) {
701 if (!arg
|| *arg
== '\0') {
705 if (lttng_is_setuid_setgid()) {
706 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
707 "--extra-kmod-probes");
709 config_string_set(&the_config
.kmod_extra_probes_list
,
711 if (!the_config
.kmod_extra_probes_list
.value
) {
716 } else if (string_match(optname
, "event-notifier-error-number-of-bucket")) {
720 v
= strtoul(arg
, NULL
, 0);
721 if (errno
!= 0 || !isdigit(arg
[0])) {
722 ERR("Wrong value in --event-notifier-error-number-of-bucket parameter: %s", arg
);
725 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
726 ERR("Value out of range for --event-notifier-error-number-of-bucket parameter: %s", arg
);
729 the_config
.event_notifier_error_counter_bucket
= (int) v
;
730 DBG3("Number of event notifier error counter set to non default: %i",
731 the_config
.event_notifier_error_counter_bucket
);
733 } else if (string_match(optname
, "config") || opt
== 'f') {
734 /* This is handled in set_options() thus silent skip. */
737 /* Unknown option or other error.
738 * Error is printed by getopt, just return */
743 if (ret
== -EINVAL
) {
744 const char *opt_name
= "unknown";
747 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
749 if (opt
== long_options
[i
].val
) {
750 opt_name
= long_options
[i
].name
;
755 WARN("Invalid argument provided for option \"%s\", using default value.",
763 * config_entry_handler_cb used to handle options read from a config file.
764 * See config_entry_handler_cb comment in common/config/session-config.h for the
765 * return value conventions.
767 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
771 if (!entry
|| !entry
->name
|| !entry
->value
) {
776 /* Check if the option is to be ignored */
777 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
778 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
783 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
786 /* Ignore if not fully matched. */
787 if (strcmp(entry
->name
, long_options
[i
].name
)) {
792 * If the option takes no argument on the command line, we have to
793 * check if the value is "true". We support non-zero numeric values,
796 if (!long_options
[i
].has_arg
) {
797 ret
= config_parse_value(entry
->value
);
800 WARN("Invalid configuration value \"%s\" for option %s",
801 entry
->value
, entry
->name
);
803 /* False, skip boolean config option. */
808 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
812 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
818 static void print_version(void) {
819 fprintf(stdout
, "%s\n", VERSION
);
823 * daemon configuration loading and argument parsing
825 static int set_options(int argc
, char **argv
)
827 int ret
= 0, c
= 0, option_index
= 0;
828 int orig_optopt
= optopt
, orig_optind
= optind
;
830 const char *config_path
= NULL
;
832 optstring
= utils_generate_optstring(long_options
,
833 sizeof(long_options
) / sizeof(struct option
));
839 /* Check for the --config option */
840 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
841 &option_index
)) != -1) {
845 } else if (c
!= 'f') {
846 /* if not equal to --config option. */
850 if (lttng_is_setuid_setgid()) {
851 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
854 config_path
= utils_expand_path(optarg
);
856 ERR("Failed to resolve path: %s", optarg
);
861 ret
= config_get_section_entries(config_path
, config_section_name
,
862 config_entry_handler
, NULL
);
865 ERR("Invalid configuration option at line %i", ret
);
871 /* Reset getopt's global state */
872 optopt
= orig_optopt
;
873 optind
= orig_optind
;
877 * getopt_long() will not set option_index if it encounters a
880 c
= getopt_long(argc
, argv
, optstring
, long_options
,
887 * Pass NULL as the long option name if popt left the index
890 ret
= set_option(c
, optarg
,
891 option_index
< 0 ? NULL
:
892 long_options
[option_index
].name
);
904 * Create lockfile using the rundir and return its fd.
906 static int create_lockfile(void)
908 return utils_create_lock_file(the_config
.lock_file_path
.value
);
912 * Check if the global socket is available, and if a daemon is answering at the
913 * other side. If yes, error is returned.
915 * Also attempts to create and hold the lock file.
917 static int check_existing_daemon(void)
921 /* Is there anybody out there ? */
922 if (lttng_session_daemon_alive()) {
927 lockfile_fd
= create_lockfile();
928 if (lockfile_fd
< 0) {
936 static void sessiond_cleanup_lock_file(void)
941 * Cleanup lock file by deleting it and finaly closing it which will
942 * release the file system lock.
944 if (lockfile_fd
>= 0) {
945 ret
= remove(the_config
.lock_file_path
.value
);
947 PERROR("remove lock file");
949 ret
= close(lockfile_fd
);
951 PERROR("close lock file");
957 * Set the tracing group gid onto the client socket.
959 * Race window between mkdir and chown is OK because we are going from more
960 * permissive (root.root) to less permissive (root.tracing).
962 static int set_permissions(char *rundir
)
967 ret
= utils_get_group_id(
968 the_config
.tracing_group_name
.value
, true, &gid
);
970 /* Default to root group. */
974 /* Set lttng run dir */
975 ret
= chown(rundir
, 0, gid
);
977 ERR("Unable to set group on %s", rundir
);
982 * Ensure all applications and tracing group can search the run
983 * dir. Allow everyone to read the directory, since it does not
984 * buy us anything to hide its content.
986 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
988 ERR("Unable to set permissions on %s", rundir
);
992 /* lttng client socket path */
993 ret
= chown(the_config
.client_unix_sock_path
.value
, 0, gid
);
995 ERR("Unable to set group on %s",
996 the_config
.client_unix_sock_path
.value
);
1000 /* kconsumer error socket path */
1001 ret
= chown(the_kconsumer_data
.err_unix_sock_path
, 0, 0);
1003 ERR("Unable to set group on %s",
1004 the_kconsumer_data
.err_unix_sock_path
);
1008 /* 64-bit ustconsumer error socket path */
1009 ret
= chown(the_ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1011 ERR("Unable to set group on %s",
1012 the_ustconsumer64_data
.err_unix_sock_path
);
1016 /* 32-bit ustconsumer compat32 error socket path */
1017 ret
= chown(the_ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1019 ERR("Unable to set group on %s",
1020 the_ustconsumer32_data
.err_unix_sock_path
);
1024 DBG("All permissions are set");
1030 * Create the lttng run directory needed for all global sockets and pipe.
1032 static int create_lttng_rundir(void)
1036 DBG3("Creating LTTng run directory: %s", the_config
.rundir
.value
);
1038 ret
= mkdir(the_config
.rundir
.value
, S_IRWXU
);
1040 if (errno
!= EEXIST
) {
1041 ERR("Unable to create %s", the_config
.rundir
.value
);
1053 * Setup sockets and directory needed by the consumerds' communication with the
1056 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1061 switch (consumer_data
->type
) {
1062 case LTTNG_CONSUMER_KERNEL
:
1063 path
= the_config
.kconsumerd_path
.value
;
1065 case LTTNG_CONSUMER64_UST
:
1066 path
= the_config
.consumerd64_path
.value
;
1068 case LTTNG_CONSUMER32_UST
:
1069 path
= the_config
.consumerd32_path
.value
;
1072 ERR("Consumer type unknown");
1078 DBG2("Creating consumer directory: %s", path
);
1080 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1081 if (ret
< 0 && errno
!= EEXIST
) {
1083 ERR("Failed to create %s", path
);
1089 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
,
1092 /* Default to root group. */
1096 ret
= chown(path
, 0, gid
);
1098 ERR("Unable to set group on %s", path
);
1104 /* Create the consumerd error unix socket */
1105 consumer_data
->err_sock
=
1106 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1107 if (consumer_data
->err_sock
< 0) {
1108 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1114 * Set the CLOEXEC flag. Return code is useless because either way, the
1117 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1119 PERROR("utils_set_fd_cloexec");
1120 /* continue anyway */
1123 /* File permission MUST be 660 */
1124 ret
= chmod(consumer_data
->err_unix_sock_path
,
1125 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1127 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1137 * Signal handler for the daemon
1139 * Simply stop all worker threads, leaving main() return gracefully after
1140 * joining all threads and calling cleanup().
1142 static void sighandler(int sig
)
1146 DBG("SIGINT caught");
1150 DBG("SIGTERM caught");
1154 CMM_STORE_SHARED(recv_child_signal
, 1);
1162 * Setup signal handler for :
1163 * SIGINT, SIGTERM, SIGPIPE
1165 static int set_signal_handler(void)
1168 struct sigaction sa
;
1171 if ((ret
= sigemptyset(&sigset
)) < 0) {
1172 PERROR("sigemptyset");
1176 sa
.sa_mask
= sigset
;
1179 sa
.sa_handler
= sighandler
;
1180 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1181 PERROR("sigaction");
1185 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1186 PERROR("sigaction");
1190 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1191 PERROR("sigaction");
1195 sa
.sa_handler
= SIG_IGN
;
1196 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1197 PERROR("sigaction");
1201 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1207 * Set open files limit to unlimited. This daemon can open a large number of
1208 * file descriptors in order to consume multiple kernel traces.
1210 static void set_ulimit(void)
1215 /* The kernel does not allow an infinite limit for open files */
1216 lim
.rlim_cur
= 65535;
1217 lim
.rlim_max
= 65535;
1219 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1221 PERROR("failed to set open files limit");
1225 static int write_pidfile(void)
1227 return utils_create_pid_file(getpid(), the_config
.pid_file_path
.value
);
1230 static int set_clock_plugin_env(void)
1233 char *env_value
= NULL
;
1235 if (!the_config
.lttng_ust_clock_plugin
.value
) {
1239 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1240 the_config
.lttng_ust_clock_plugin
.value
);
1246 ret
= putenv(env_value
);
1249 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1253 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1254 the_config
.lttng_ust_clock_plugin
.value
);
1259 static void destroy_all_sessions_and_wait(void)
1261 struct ltt_session
*session
, *tmp
;
1262 struct ltt_session_list
*session_list
;
1264 session_list
= session_get_list();
1265 DBG("Initiating destruction of all sessions");
1267 if (!session_list
) {
1271 session_lock_list();
1272 /* Initiate the destruction of all sessions. */
1273 cds_list_for_each_entry_safe(session
, tmp
,
1274 &session_list
->head
, list
) {
1275 if (!session_get(session
)) {
1279 session_lock(session
);
1280 if (session
->destroyed
) {
1281 goto unlock_session
;
1283 (void) cmd_stop_trace(session
);
1284 (void) cmd_destroy_session(
1285 session
, the_notification_thread_handle
, NULL
);
1287 session_unlock(session
);
1288 session_put(session
);
1290 session_unlock_list();
1292 /* Wait for the destruction of all sessions to complete. */
1293 DBG("Waiting for the destruction of all sessions to complete");
1294 session_list_wait_empty();
1295 DBG("Destruction of all sessions completed");
1298 static void unregister_all_triggers(void)
1300 enum lttng_error_code ret_code
;
1301 enum lttng_trigger_status trigger_status
;
1302 struct lttng_triggers
*triggers
= NULL
;
1303 unsigned int trigger_count
, i
;
1304 const struct lttng_credentials creds
= {
1305 .uid
= LTTNG_OPTIONAL_INIT_VALUE(0),
1308 DBG("Unregistering all triggers");
1311 * List all triggers as "root" since we wish to unregister all triggers.
1313 ret_code
= notification_thread_command_list_triggers(
1314 the_notification_thread_handle
, creds
.uid
.value
,
1316 if (ret_code
!= LTTNG_OK
) {
1317 ERR("Failed to list triggers while unregistering all triggers");
1321 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
1322 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1324 for (i
= 0; i
< trigger_count
; i
++) {
1325 uid_t trigger_owner
;
1326 const char *trigger_name
;
1327 const struct lttng_trigger
*trigger
=
1328 lttng_triggers_get_at_index(triggers
, i
);
1332 trigger_status
= lttng_trigger_get_owner_uid(
1333 trigger
, &trigger_owner
);
1334 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1336 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
1337 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1339 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1340 (int) trigger_owner
, trigger_name
);
1342 ret_code
= cmd_unregister_trigger(&creds
, trigger
,
1343 the_notification_thread_handle
);
1344 if (ret_code
!= LTTNG_OK
) {
1345 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1346 (int) trigger_owner
, trigger_name
,
1347 lttng_strerror(-ret_code
));
1348 /* Continue to unregister the remaining triggers. */
1352 lttng_triggers_destroy(triggers
);
1355 static int run_as_worker_post_fork_cleanup(void *data
)
1357 struct sessiond_config
*sessiond_config
= data
;
1359 sessiond_config_fini(sessiond_config
);
1363 static int launch_run_as_worker(const char *procname
)
1366 * Clean-up before forking the run-as worker. Any dynamically
1367 * allocated memory of which the worker is not aware will
1368 * be leaked as the process forks a run-as worker (and performs
1369 * no exec*()). The same would apply to any opened fd.
1371 return run_as_create_worker(
1372 procname
, run_as_worker_post_fork_cleanup
, &the_config
);
1375 static void sessiond_uuid_log(void)
1377 char uuid_str
[LTTNG_UUID_STR_LEN
];
1379 lttng_uuid_to_str(the_sessiond_uuid
, uuid_str
);
1380 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1386 int main(int argc
, char **argv
)
1388 int ret
= 0, retval
= 0;
1389 const char *env_app_timeout
;
1390 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1391 *ust64_channel_monitor_pipe
= NULL
,
1392 *kernel_channel_monitor_pipe
= NULL
;
1393 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1394 struct timer_thread_parameters timer_thread_parameters
;
1395 /* Rotation thread handle. */
1396 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1397 /* Queue of rotation jobs populated by the sessiond-timer. */
1398 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1399 struct lttng_thread
*client_thread
= NULL
;
1400 struct lttng_thread
*notification_thread
= NULL
;
1401 struct lttng_thread
*register_apps_thread
= NULL
;
1403 logger_set_thread_name("Main", false);
1404 init_kernel_workarounds();
1406 rcu_register_thread();
1408 if (set_signal_handler()) {
1410 goto exit_set_signal_handler
;
1413 if (timer_signal_init()) {
1415 goto exit_set_signal_handler
;
1418 the_page_size
= sysconf(_SC_PAGESIZE
);
1419 if (the_page_size
< 0) {
1420 PERROR("sysconf _SC_PAGESIZE");
1421 the_page_size
= LONG_MAX
;
1422 WARN("Fallback page size to %ld", the_page_size
);
1425 ret
= sessiond_config_init(&the_config
);
1428 goto exit_set_signal_handler
;
1432 * Init config from environment variables.
1433 * Command line option override env configuration per-doc. Do env first.
1435 sessiond_config_apply_env_config(&the_config
);
1438 * Parse arguments and load the daemon configuration file.
1440 * We have an exit_options exit path to free memory reserved by
1441 * set_options. This is needed because the rest of sessiond_cleanup()
1442 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1443 * depends on set_options.
1446 if (set_options(argc
, argv
)) {
1452 * Resolve all paths received as arguments, configuration option, or
1453 * through environment variable as absolute paths. This is necessary
1454 * since daemonizing causes the sessiond's current working directory
1457 ret
= sessiond_config_resolve_paths(&the_config
);
1463 lttng_opt_verbose
= the_config
.verbose
;
1464 lttng_opt_quiet
= the_config
.quiet
;
1465 the_kconsumer_data
.err_unix_sock_path
=
1466 the_config
.kconsumerd_err_unix_sock_path
.value
;
1467 the_kconsumer_data
.cmd_unix_sock_path
=
1468 the_config
.kconsumerd_cmd_unix_sock_path
.value
;
1469 the_ustconsumer32_data
.err_unix_sock_path
=
1470 the_config
.consumerd32_err_unix_sock_path
.value
;
1471 the_ustconsumer32_data
.cmd_unix_sock_path
=
1472 the_config
.consumerd32_cmd_unix_sock_path
.value
;
1473 the_ustconsumer64_data
.err_unix_sock_path
=
1474 the_config
.consumerd64_err_unix_sock_path
.value
;
1475 the_ustconsumer64_data
.cmd_unix_sock_path
=
1476 the_config
.consumerd64_cmd_unix_sock_path
.value
;
1477 set_clock_plugin_env();
1479 sessiond_config_log(&the_config
);
1480 sessiond_uuid_log();
1482 if (opt_print_version
) {
1488 if (create_lttng_rundir()) {
1493 /* Abort launch if a session daemon is already running. */
1494 if (check_existing_daemon()) {
1495 ERR("A session daemon is already running.");
1501 if (the_config
.daemonize
|| the_config
.background
) {
1504 ret
= lttng_daemonize(&the_child_ppid
, &recv_child_signal
,
1505 !the_config
.background
);
1512 * We are in the child. Make sure all other file descriptors are
1513 * closed, in case we are called with more opened file
1514 * descriptors than the standard ones and the lock file.
1516 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1517 if (i
== lockfile_fd
) {
1524 if (launch_run_as_worker(argv
[0]) < 0) {
1525 goto exit_create_run_as_worker_cleanup
;
1529 * Starting from here, we can create threads. This needs to be after
1530 * lttng_daemonize due to RCU.
1534 * Initialize the health check subsystem. This call should set the
1535 * appropriate time values.
1537 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1538 if (!the_health_sessiond
) {
1539 PERROR("health_app_create error");
1544 /* Create thread to clean up RCU hash tables */
1545 ht_cleanup_thread
= launch_ht_cleanup_thread();
1546 if (!ht_cleanup_thread
) {
1551 /* Create thread quit pipe */
1552 if (sessiond_init_thread_quit_pipe()) {
1557 /* Check if daemon is UID = 0 */
1558 is_root
= !getuid();
1560 /* Create global run dir with root access */
1562 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1563 if (!kernel_channel_monitor_pipe
) {
1564 ERR("Failed to create kernel consumer channel monitor pipe");
1568 the_kconsumer_data
.channel_monitor_pipe
=
1569 lttng_pipe_release_writefd(
1570 kernel_channel_monitor_pipe
);
1571 if (the_kconsumer_data
.channel_monitor_pipe
< 0) {
1577 /* Set consumer initial state */
1578 the_kernel_consumerd_state
= CONSUMER_STOPPED
;
1579 the_ust_consumerd_state
= CONSUMER_STOPPED
;
1581 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1582 if (!ust32_channel_monitor_pipe
) {
1583 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1587 the_ustconsumer32_data
.channel_monitor_pipe
=
1588 lttng_pipe_release_writefd(ust32_channel_monitor_pipe
);
1589 if (the_ustconsumer32_data
.channel_monitor_pipe
< 0) {
1595 * The rotation_thread_timer_queue structure is shared between the
1596 * sessiond timer thread and the rotation thread. The main thread keeps
1597 * its ownership and destroys it when both threads have been joined.
1599 rotation_timer_queue
= rotation_thread_timer_queue_create();
1600 if (!rotation_timer_queue
) {
1604 timer_thread_parameters
.rotation_thread_job_queue
=
1605 rotation_timer_queue
;
1607 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1608 if (!ust64_channel_monitor_pipe
) {
1609 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1613 the_ustconsumer64_data
.channel_monitor_pipe
=
1614 lttng_pipe_release_writefd(ust64_channel_monitor_pipe
);
1615 if (the_ustconsumer64_data
.channel_monitor_pipe
< 0) {
1621 * Init UST app hash table. Alloc hash table before this point since
1622 * cleanup() can get called after that point.
1624 if (ust_app_ht_alloc()) {
1625 ERR("Failed to allocate UST app hash table");
1630 event_notifier_error_accounting_init(the_config
.event_notifier_error_counter_bucket
);
1633 * Initialize agent app hash table. We allocate the hash table here
1634 * since cleanup() can get called after this point.
1636 if (agent_app_ht_alloc()) {
1637 ERR("Failed to allocate Agent app hash table");
1642 if (agent_by_event_notifier_domain_ht_create()) {
1643 ERR("Failed to allocate per-event notifier domain agent hash table");
1648 * These actions must be executed as root. We do that *after* setting up
1649 * the sockets path because we MUST make the check for another daemon using
1650 * those paths *before* trying to set the kernel consumer sockets and init
1654 if (set_consumer_sockets(&the_kconsumer_data
)) {
1659 /* Setup kernel tracer */
1660 if (!the_config
.no_kernel
) {
1661 init_kernel_tracer();
1664 /* Set ulimit for open files */
1667 /* init lttng_fd tracking must be done after set_ulimit. */
1670 if (set_consumer_sockets(&the_ustconsumer64_data
)) {
1675 if (set_consumer_sockets(&the_ustconsumer32_data
)) {
1680 /* Get parent pid if -S, --sig-parent is specified. */
1681 if (the_config
.sig_parent
) {
1682 the_ppid
= getppid();
1685 /* Setup the kernel pipe for waking up the kernel thread */
1686 if (is_root
&& !the_config
.no_kernel
) {
1687 if (utils_create_pipe_cloexec(the_kernel_poll_pipe
)) {
1693 /* Setup the thread apps communication pipe. */
1694 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1699 /* Setup the thread apps notify communication pipe. */
1700 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1705 /* Initialize global buffer per UID and PID registry. */
1706 buffer_reg_init_uid_registry();
1707 buffer_reg_init_pid_registry();
1709 /* Init UST command queue. */
1710 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1714 /* Check for the application socket timeout env variable. */
1715 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1716 if (env_app_timeout
) {
1717 the_config
.app_socket_timeout
= atoi(env_app_timeout
);
1719 the_config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1722 ret
= write_pidfile();
1724 ERR("Error in write_pidfile");
1729 /* Initialize communication library */
1731 /* Initialize TCP timeout values */
1732 lttcomm_inet_init();
1734 /* Create health-check thread. */
1735 if (!launch_health_management_thread()) {
1740 /* notification_thread_data acquires the pipes' read side. */
1741 the_notification_thread_handle
= notification_thread_handle_create(
1742 ust32_channel_monitor_pipe
, ust64_channel_monitor_pipe
,
1743 kernel_channel_monitor_pipe
);
1744 if (!the_notification_thread_handle
) {
1746 ERR("Failed to create notification thread shared data");
1750 /* Create notification thread. */
1751 notification_thread
= launch_notification_thread(
1752 the_notification_thread_handle
);
1753 if (!notification_thread
) {
1758 /* Create timer thread. */
1759 if (!launch_timer_thread(&timer_thread_parameters
)) {
1764 /* rotation_thread_data acquires the pipes' read side. */
1765 rotation_thread_handle
= rotation_thread_handle_create(
1766 rotation_timer_queue
, the_notification_thread_handle
);
1767 if (!rotation_thread_handle
) {
1769 ERR("Failed to create rotation thread shared data");
1774 /* Create rotation thread. */
1775 if (!launch_rotation_thread(rotation_thread_handle
)) {
1780 /* Create thread to manage the client socket */
1781 client_thread
= launch_client_thread();
1782 if (!client_thread
) {
1787 /* Set credentials of the client socket and rundir */
1788 if (is_root
&& set_permissions(the_config
.rundir
.value
)) {
1793 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1794 apps_cmd_notify_pipe
[1])) {
1799 /* Create thread to manage application registration. */
1800 register_apps_thread
= launch_application_registration_thread(
1802 if (!register_apps_thread
) {
1807 /* Create thread to manage application socket */
1808 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1813 /* Create thread to manage application notify socket */
1814 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1819 /* Create agent management thread. */
1820 if (!launch_agent_management_thread()) {
1825 /* Don't start this thread if kernel tracing is not requested nor root */
1826 if (is_root
&& !the_config
.no_kernel
) {
1827 /* Create kernel thread to manage kernel event */
1828 if (!launch_kernel_management_thread(the_kernel_poll_pipe
[0])) {
1833 if (kernel_get_notification_fd() >= 0) {
1834 ret
= notification_thread_command_add_tracer_event_source(
1835 the_notification_thread_handle
,
1836 kernel_get_notification_fd(),
1837 LTTNG_DOMAIN_KERNEL
);
1838 if (ret
!= LTTNG_OK
) {
1839 ERR("Failed to add kernel trigger event source to notification thread");
1846 /* Load sessions. */
1847 ret
= config_load_session(
1848 the_config
.load_session_path
.value
, NULL
, 1, 1, NULL
);
1850 ERR("Session load failed: %s", error_get_str(ret
));
1855 /* Initialization completed. */
1856 sessiond_signal_parents();
1859 * This is where we start awaiting program completion (e.g. through
1860 * signal that asks threads to teardown).
1863 /* Initiate teardown once activity occurs on the quit pipe. */
1864 sessiond_wait_for_quit_pipe(-1);
1869 * Ensure that the client thread is no longer accepting new commands,
1870 * which could cause new sessions to be created.
1872 if (client_thread
) {
1873 lttng_thread_shutdown(client_thread
);
1874 lttng_thread_put(client_thread
);
1877 destroy_all_sessions_and_wait();
1880 * At this point no new trigger can be registered (no sessions are
1881 * running/rotating) and clients can't connect to the session daemon
1882 * anymore. Unregister all triggers.
1884 unregister_all_triggers();
1886 if (register_apps_thread
) {
1887 lttng_thread_shutdown(register_apps_thread
);
1888 lttng_thread_put(register_apps_thread
);
1890 lttng_thread_list_shutdown_orphans();
1893 * Wait for all pending call_rcu work to complete before tearing
1894 * down data structures. call_rcu worker may be trying to
1895 * perform lookups in those structures.
1899 * sessiond_cleanup() is called when no other thread is running, except
1900 * the ht_cleanup thread, which is needed to destroy the hash tables.
1902 rcu_thread_online();
1906 * Wait for all pending call_rcu work to complete before shutting down
1907 * the notification thread. This call_rcu work includes shutting down
1908 * UST apps and event notifier pipes.
1912 if (notification_thread
) {
1913 lttng_thread_shutdown(notification_thread
);
1914 lttng_thread_put(notification_thread
);
1918 * Error accounting teardown has to be done after the teardown of all
1919 * event notifier pipes to ensure that no tracer may try to use the
1920 * error accounting facilities.
1922 event_notifier_error_accounting_fini();
1925 * Unloading the kernel modules needs to be done after all kernel
1926 * ressources have been released. In our case, this includes the
1927 * notification fd, the event notifier group fd, error accounting fd,
1928 * all event and event notifier fds, etc.
1930 * In short, at this point, we need to have called close() on all fds
1931 * received from the kernel tracer.
1933 if (is_root
&& !the_config
.no_kernel
) {
1934 DBG("Unloading kernel modules");
1935 modprobe_remove_lttng_all();
1939 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1940 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1941 * the queue is empty before shutting down the clean-up thread.
1945 if (ht_cleanup_thread
) {
1946 lttng_thread_shutdown(ht_cleanup_thread
);
1947 lttng_thread_put(ht_cleanup_thread
);
1950 rcu_thread_offline();
1951 rcu_unregister_thread();
1953 if (rotation_thread_handle
) {
1954 rotation_thread_handle_destroy(rotation_thread_handle
);
1958 * After the rotation and timer thread have quit, we can safely destroy
1959 * the rotation_timer_queue.
1961 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1963 * The teardown of the notification system is performed after the
1964 * session daemon's teardown in order to allow it to be notified
1965 * of the active session and channels at the moment of the teardown.
1967 if (the_notification_thread_handle
) {
1968 notification_thread_handle_destroy(
1969 the_notification_thread_handle
);
1971 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1972 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1973 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1975 if (the_health_sessiond
) {
1976 health_app_destroy(the_health_sessiond
);
1978 exit_create_run_as_worker_cleanup
:
1980 sessiond_cleanup_lock_file();
1981 sessiond_cleanup_options();
1983 exit_set_signal_handler
: