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"
78 #include "ust-sigbus.h"
80 static const char *help_msg
=
81 #ifdef LTTNG_EMBED_HELP
82 #include <lttng-sessiond.8.h>
88 #define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
89 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR \
90 "event-notifier-error-buffer-size"
91 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR \
92 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-kernel"
93 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR \
94 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-userspace"
98 static int lockfile_fd
= -1;
99 static int opt_print_version
;
101 /* Set to 1 when a SIGUSR1 signal is received. */
102 static int recv_child_signal
;
104 /* Command line options */
105 static const struct option long_options
[] = {
106 { "client-sock", required_argument
, 0, 'c' },
107 { "apps-sock", required_argument
, 0, 'a' },
108 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
109 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
110 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
111 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
112 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
113 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
114 { "consumerd32-path", required_argument
, 0, '\0' },
115 { "consumerd32-libdir", required_argument
, 0, '\0' },
116 { "consumerd64-path", required_argument
, 0, '\0' },
117 { "consumerd64-libdir", required_argument
, 0, '\0' },
118 { "daemonize", no_argument
, 0, 'd' },
119 { "background", no_argument
, 0, 'b' },
120 { "sig-parent", no_argument
, 0, 'S' },
121 { "help", no_argument
, 0, 'h' },
122 { "group", required_argument
, 0, 'g' },
123 { "version", no_argument
, 0, 'V' },
124 { "quiet", no_argument
, 0, 'q' },
125 { "verbose", no_argument
, 0, 'v' },
126 { "verbose-consumer", no_argument
, 0, '\0' },
127 { "no-kernel", no_argument
, 0, '\0' },
128 { "pidfile", required_argument
, 0, 'p' },
129 { "agent-tcp-port", required_argument
, 0, '\0' },
130 { "config", required_argument
, 0, 'f' },
131 { "load", required_argument
, 0, 'l' },
132 { "kmod-probes", required_argument
, 0, '\0' },
133 { "extra-kmod-probes", required_argument
, 0, '\0' },
134 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, required_argument
, 0, '\0' },
135 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, required_argument
, 0, '\0' },
139 /* Command line options to ignore from configuration file */
140 static const char *config_ignore_options
[] = { "help", "version", "config" };
143 * This pipe is used to inform the thread managing application communication
144 * that a command is queued and ready to be processed.
146 static int apps_cmd_pipe
[2] = { -1, -1 };
147 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
150 * UST registration command queue. This queue is tied with a futex and uses a N
151 * wakers / 1 waiter implemented and detailed in futex.c/.h
153 * The thread_registration_apps and thread_dispatch_ust_registration uses this
154 * queue along with the wait/wake scheme. The thread_manage_apps receives down
155 * the line new application socket and monitors it for any I/O error or clean
156 * close that triggers an unregistration of the application.
158 static struct ust_cmd_queue ust_cmd_queue
;
161 * Section name to look for in the daemon configuration file.
163 static const char * const config_section_name
= "sessiond";
165 /* Am I root or not. Set to 1 if the daemon is running as root */
169 * Stop all threads by closing the thread quit pipe.
171 static void stop_threads(void)
175 /* Stopping all threads */
176 DBG("Terminating all threads");
177 ret
= sessiond_notify_quit_pipe();
179 ERR("write error on thread quit pipe");
184 * Close every consumer sockets.
186 static void close_consumer_sockets(void)
190 if (the_kconsumer_data
.err_sock
>= 0) {
191 ret
= close(the_kconsumer_data
.err_sock
);
193 PERROR("kernel consumer err_sock close");
196 if (the_ustconsumer32_data
.err_sock
>= 0) {
197 ret
= close(the_ustconsumer32_data
.err_sock
);
199 PERROR("UST consumerd32 err_sock close");
202 if (the_ustconsumer64_data
.err_sock
>= 0) {
203 ret
= close(the_ustconsumer64_data
.err_sock
);
205 PERROR("UST consumerd64 err_sock close");
208 if (the_kconsumer_data
.cmd_sock
>= 0) {
209 ret
= close(the_kconsumer_data
.cmd_sock
);
211 PERROR("kernel consumer cmd_sock close");
214 if (the_ustconsumer32_data
.cmd_sock
>= 0) {
215 ret
= close(the_ustconsumer32_data
.cmd_sock
);
217 PERROR("UST consumerd32 cmd_sock close");
220 if (the_ustconsumer64_data
.cmd_sock
>= 0) {
221 ret
= close(the_ustconsumer64_data
.cmd_sock
);
223 PERROR("UST consumerd64 cmd_sock close");
226 if (the_kconsumer_data
.channel_monitor_pipe
>= 0) {
227 ret
= close(the_kconsumer_data
.channel_monitor_pipe
);
229 PERROR("kernel consumer channel monitor pipe close");
232 if (the_ustconsumer32_data
.channel_monitor_pipe
>= 0) {
233 ret
= close(the_ustconsumer32_data
.channel_monitor_pipe
);
235 PERROR("UST consumerd32 channel monitor pipe close");
238 if (the_ustconsumer64_data
.channel_monitor_pipe
>= 0) {
239 ret
= close(the_ustconsumer64_data
.channel_monitor_pipe
);
241 PERROR("UST consumerd64 channel monitor pipe close");
247 * Wait on consumer process termination.
249 * Need to be called with the consumer data lock held or from a context
250 * ensuring no concurrent access to data (e.g: cleanup).
252 static void wait_consumer(struct consumer_data
*consumer_data
)
257 if (consumer_data
->pid
<= 0) {
261 DBG("Waiting for complete teardown of consumerd (PID: %d)",
263 ret
= waitpid(consumer_data
->pid
, &status
, 0);
265 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
266 } else if (!WIFEXITED(status
)) {
267 ERR("consumerd termination with error: %d",
270 consumer_data
->pid
= 0;
274 * Cleanup the session daemon's data structures.
276 static void sessiond_cleanup(void)
279 struct ltt_session_list
*session_list
= session_get_list();
281 DBG("Cleanup sessiond");
284 * Close the thread quit pipe. It has already done its job,
285 * since we are now called.
287 sessiond_close_quit_pipe();
288 utils_close_pipe(apps_cmd_pipe
);
289 utils_close_pipe(apps_cmd_notify_pipe
);
290 utils_close_pipe(the_kernel_poll_pipe
);
292 ret
= remove(the_config
.pid_file_path
.value
);
294 PERROR("remove pidfile %s", the_config
.pid_file_path
.value
);
297 DBG("Removing sessiond and consumerd content of directory %s",
298 the_config
.rundir
.value
);
301 DBG("Removing %s", the_config
.pid_file_path
.value
);
302 (void) unlink(the_config
.pid_file_path
.value
);
304 DBG("Removing %s", the_config
.agent_port_file_path
.value
);
305 (void) unlink(the_config
.agent_port_file_path
.value
);
308 DBG("Removing %s", the_kconsumer_data
.err_unix_sock_path
);
309 (void) unlink(the_kconsumer_data
.err_unix_sock_path
);
311 DBG("Removing directory %s", the_config
.kconsumerd_path
.value
);
312 (void) rmdir(the_config
.kconsumerd_path
.value
);
314 /* ust consumerd 32 */
315 DBG("Removing %s", the_config
.consumerd32_err_unix_sock_path
.value
);
316 (void) unlink(the_config
.consumerd32_err_unix_sock_path
.value
);
318 DBG("Removing directory %s", the_config
.consumerd32_path
.value
);
319 (void) rmdir(the_config
.consumerd32_path
.value
);
321 /* ust consumerd 64 */
322 DBG("Removing %s", the_config
.consumerd64_err_unix_sock_path
.value
);
323 (void) unlink(the_config
.consumerd64_err_unix_sock_path
.value
);
325 DBG("Removing directory %s", the_config
.consumerd64_path
.value
);
326 (void) rmdir(the_config
.consumerd64_path
.value
);
328 pthread_mutex_destroy(&session_list
->lock
);
330 DBG("Cleaning up all per-event notifier domain agents");
331 agent_by_event_notifier_domain_ht_destroy();
333 DBG("Cleaning up all agent apps");
334 agent_app_ht_clean();
335 DBG("Closing all UST sockets");
336 ust_app_clean_list();
337 buffer_reg_destroy_registries();
339 close_consumer_sockets();
341 wait_consumer(&the_kconsumer_data
);
342 wait_consumer(&the_ustconsumer64_data
);
343 wait_consumer(&the_ustconsumer32_data
);
345 if (is_root
&& !the_config
.no_kernel
) {
346 cleanup_kernel_tracer();
350 * We do NOT rmdir rundir because there are other processes
351 * using it, for instance lttng-relayd, which can start in
352 * parallel with this teardown.
357 * Cleanup the daemon's option data structures.
359 static void sessiond_cleanup_options(void)
361 DBG("Cleaning up options");
363 sessiond_config_fini(&the_config
);
365 run_as_destroy_worker();
368 static int string_match(const char *str1
, const char *str2
)
370 return (str1
&& str2
) && !strcmp(str1
, str2
);
374 * Take an option from the getopt output and set it in the right variable to be
377 * Return 0 on success else a negative value.
379 static int set_option(int opt
, const char *arg
, const char *optname
)
383 if (string_match(optname
, "client-sock") || opt
== 'c') {
384 if (!arg
|| *arg
== '\0') {
388 if (lttng_is_setuid_setgid()) {
389 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
390 "-c, --client-sock");
392 config_string_set(&the_config
.client_unix_sock_path
,
394 if (!the_config
.client_unix_sock_path
.value
) {
399 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
400 if (!arg
|| *arg
== '\0') {
404 if (lttng_is_setuid_setgid()) {
405 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
408 config_string_set(&the_config
.apps_unix_sock_path
,
410 if (!the_config
.apps_unix_sock_path
.value
) {
415 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
416 the_config
.daemonize
= true;
417 } else if (string_match(optname
, "background") || opt
== 'b') {
418 the_config
.background
= true;
419 } else if (string_match(optname
, "group") || opt
== 'g') {
420 if (!arg
|| *arg
== '\0') {
424 if (lttng_is_setuid_setgid()) {
425 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
428 config_string_set(&the_config
.tracing_group_name
,
430 if (!the_config
.tracing_group_name
.value
) {
435 } else if (string_match(optname
, "help") || opt
== 'h') {
436 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
438 ERR("Cannot show --help for `lttng-sessiond`");
441 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
442 } else if (string_match(optname
, "version") || opt
== 'V') {
443 opt_print_version
= 1;
444 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
445 the_config
.sig_parent
= true;
446 } else if (string_match(optname
, "kconsumerd-err-sock")) {
447 if (!arg
|| *arg
== '\0') {
451 if (lttng_is_setuid_setgid()) {
452 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
453 "--kconsumerd-err-sock");
456 &the_config
.kconsumerd_err_unix_sock_path
,
458 if (!the_config
.kconsumerd_err_unix_sock_path
.value
) {
463 } else if (string_match(optname
, "kconsumerd-cmd-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 "--kconsumerd-cmd-sock");
473 &the_config
.kconsumerd_cmd_unix_sock_path
,
475 if (!the_config
.kconsumerd_cmd_unix_sock_path
.value
) {
480 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
481 if (!arg
|| *arg
== '\0') {
485 if (lttng_is_setuid_setgid()) {
486 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
487 "--ustconsumerd64-err-sock");
490 &the_config
.consumerd64_err_unix_sock_path
,
492 if (!the_config
.consumerd64_err_unix_sock_path
.value
) {
497 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
498 if (!arg
|| *arg
== '\0') {
502 if (lttng_is_setuid_setgid()) {
503 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
504 "--ustconsumerd64-cmd-sock");
507 &the_config
.consumerd64_cmd_unix_sock_path
,
509 if (!the_config
.consumerd64_cmd_unix_sock_path
.value
) {
514 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
515 if (!arg
|| *arg
== '\0') {
519 if (lttng_is_setuid_setgid()) {
520 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
521 "--ustconsumerd32-err-sock");
524 &the_config
.consumerd32_err_unix_sock_path
,
526 if (!the_config
.consumerd32_err_unix_sock_path
.value
) {
531 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
532 if (!arg
|| *arg
== '\0') {
536 if (lttng_is_setuid_setgid()) {
537 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
538 "--ustconsumerd32-cmd-sock");
541 &the_config
.consumerd32_cmd_unix_sock_path
,
543 if (!the_config
.consumerd32_cmd_unix_sock_path
.value
) {
548 } else if (string_match(optname
, "no-kernel")) {
549 the_config
.no_kernel
= true;
550 } else if (string_match(optname
, "quiet") || opt
== 'q') {
551 the_config
.quiet
= true;
552 } else if (string_match(optname
, "verbose") || opt
== 'v') {
553 /* Verbose level can increase using multiple -v */
555 /* Value obtained from config file */
556 the_config
.verbose
= config_parse_value(arg
);
558 /* -v used on command line */
559 the_config
.verbose
++;
561 /* Clamp value to [0, 3] */
562 the_config
.verbose
= the_config
.verbose
< 0 ?
564 (the_config
.verbose
<= 3 ? the_config
.verbose
:
566 } else if (string_match(optname
, "verbose-consumer")) {
568 the_config
.verbose_consumer
= config_parse_value(arg
);
570 the_config
.verbose_consumer
++;
572 } else if (string_match(optname
, "consumerd32-path")) {
573 if (!arg
|| *arg
== '\0') {
577 if (lttng_is_setuid_setgid()) {
578 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
579 "--consumerd32-path");
581 config_string_set(&the_config
.consumerd32_bin_path
,
583 if (!the_config
.consumerd32_bin_path
.value
) {
588 } else if (string_match(optname
, "consumerd32-libdir")) {
589 if (!arg
|| *arg
== '\0') {
593 if (lttng_is_setuid_setgid()) {
594 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
595 "--consumerd32-libdir");
597 config_string_set(&the_config
.consumerd32_lib_dir
,
599 if (!the_config
.consumerd32_lib_dir
.value
) {
604 } else if (string_match(optname
, "consumerd64-path")) {
605 if (!arg
|| *arg
== '\0') {
609 if (lttng_is_setuid_setgid()) {
610 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
611 "--consumerd64-path");
613 config_string_set(&the_config
.consumerd64_bin_path
,
615 if (!the_config
.consumerd64_bin_path
.value
) {
620 } else if (string_match(optname
, "consumerd64-libdir")) {
621 if (!arg
|| *arg
== '\0') {
625 if (lttng_is_setuid_setgid()) {
626 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
627 "--consumerd64-libdir");
629 config_string_set(&the_config
.consumerd64_lib_dir
,
631 if (!the_config
.consumerd64_lib_dir
.value
) {
636 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
637 if (!arg
|| *arg
== '\0') {
641 if (lttng_is_setuid_setgid()) {
642 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
646 &the_config
.pid_file_path
, strdup(arg
));
647 if (!the_config
.pid_file_path
.value
) {
652 } else if (string_match(optname
, "agent-tcp-port")) {
653 if (!arg
|| *arg
== '\0') {
657 if (lttng_is_setuid_setgid()) {
658 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
664 v
= strtoul(arg
, NULL
, 0);
665 if (errno
!= 0 || !isdigit(arg
[0])) {
666 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
669 if (v
== 0 || v
>= 65535) {
670 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
673 the_config
.agent_tcp_port
.begin
=
674 the_config
.agent_tcp_port
.end
= (int) v
;
675 DBG3("Agent TCP port set to non default: %i", (int) v
);
677 } else if (string_match(optname
, "load") || opt
== 'l') {
678 if (!arg
|| *arg
== '\0') {
682 if (lttng_is_setuid_setgid()) {
683 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
686 config_string_set(&the_config
.load_session_path
,
688 if (!the_config
.load_session_path
.value
) {
693 } else if (string_match(optname
, "kmod-probes")) {
694 if (!arg
|| *arg
== '\0') {
698 if (lttng_is_setuid_setgid()) {
699 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
702 config_string_set(&the_config
.kmod_probes_list
,
704 if (!the_config
.kmod_probes_list
.value
) {
709 } else if (string_match(optname
, "extra-kmod-probes")) {
710 if (!arg
|| *arg
== '\0') {
714 if (lttng_is_setuid_setgid()) {
715 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
716 "--extra-kmod-probes");
718 config_string_set(&the_config
.kmod_extra_probes_list
,
720 if (!the_config
.kmod_extra_probes_list
.value
) {
725 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
)) {
729 v
= strtoul(arg
, NULL
, 0);
730 if (errno
!= 0 || !isdigit(arg
[0])) {
731 ERR("Wrong value in --%s parameter: %s",
732 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
735 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
736 ERR("Value out of range for --%s parameter: %s",
737 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
740 the_config
.event_notifier_buffer_size_kernel
= (int) v
;
741 DBG3("Number of event notifier error buffer kernel size to non default: %i",
742 the_config
.event_notifier_buffer_size_kernel
);
744 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
)) {
748 v
= strtoul(arg
, NULL
, 0);
749 if (errno
!= 0 || !isdigit(arg
[0])) {
750 ERR("Wrong value in --%s parameter: %s",
751 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
754 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
755 ERR("Value out of range for --%s parameter: %s",
756 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
759 the_config
.event_notifier_buffer_size_userspace
= (int) v
;
760 DBG3("Number of event notifier error buffer userspace size to non default: %i",
761 the_config
.event_notifier_buffer_size_userspace
);
763 } else if (string_match(optname
, "config") || opt
== 'f') {
764 /* This is handled in set_options() thus silent skip. */
767 /* Unknown option or other error.
768 * Error is printed by getopt, just return */
773 if (ret
== -EINVAL
) {
774 const char *opt_name
= "unknown";
777 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
779 if (opt
== long_options
[i
].val
) {
780 opt_name
= long_options
[i
].name
;
785 WARN("Invalid argument provided for option \"%s\", using default value.",
793 * config_entry_handler_cb used to handle options read from a config file.
794 * See config_entry_handler_cb comment in common/config/session-config.h for the
795 * return value conventions.
797 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
801 if (!entry
|| !entry
->name
|| !entry
->value
) {
806 /* Check if the option is to be ignored */
807 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
808 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
813 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
816 /* Ignore if not fully matched. */
817 if (strcmp(entry
->name
, long_options
[i
].name
)) {
822 * If the option takes no argument on the command line, we have to
823 * check if the value is "true". We support non-zero numeric values,
826 if (!long_options
[i
].has_arg
) {
827 ret
= config_parse_value(entry
->value
);
830 WARN("Invalid configuration value \"%s\" for option %s",
831 entry
->value
, entry
->name
);
833 /* False, skip boolean config option. */
838 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
842 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
848 static void print_version(void) {
849 fprintf(stdout
, "%s\n", VERSION
);
853 * daemon configuration loading and argument parsing
855 static int set_options(int argc
, char **argv
)
857 int ret
= 0, c
= 0, option_index
= 0;
858 int orig_optopt
= optopt
, orig_optind
= optind
;
860 char *config_path
= NULL
;
862 optstring
= utils_generate_optstring(long_options
,
863 sizeof(long_options
) / sizeof(struct option
));
869 /* Check for the --config option */
870 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
871 &option_index
)) != -1) {
875 } else if (c
!= 'f') {
876 /* if not equal to --config option. */
880 if (lttng_is_setuid_setgid()) {
881 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
885 config_path
= utils_expand_path(optarg
);
887 ERR("Failed to resolve path: %s", optarg
);
892 ret
= config_get_section_entries(config_path
, config_section_name
,
893 config_entry_handler
, NULL
);
896 ERR("Invalid configuration option at line %i", ret
);
902 /* Reset getopt's global state */
903 optopt
= orig_optopt
;
904 optind
= orig_optind
;
908 * getopt_long() will not set option_index if it encounters a
911 c
= getopt_long(argc
, argv
, optstring
, long_options
,
918 * Pass NULL as the long option name if popt left the index
921 ret
= set_option(c
, optarg
,
922 option_index
< 0 ? NULL
:
923 long_options
[option_index
].name
);
936 * Create lockfile using the rundir and return its fd.
938 static int create_lockfile(void)
940 return utils_create_lock_file(the_config
.lock_file_path
.value
);
944 * Check if the global socket is available, and if a daemon is answering at the
945 * other side. If yes, error is returned.
947 * Also attempts to create and hold the lock file.
949 static int check_existing_daemon(void)
953 /* Is there anybody out there ? */
954 if (lttng_session_daemon_alive()) {
959 lockfile_fd
= create_lockfile();
960 if (lockfile_fd
< 0) {
968 static void sessiond_cleanup_lock_file(void)
973 * Cleanup lock file by deleting it and finaly closing it which will
974 * release the file system lock.
976 if (lockfile_fd
>= 0) {
977 ret
= remove(the_config
.lock_file_path
.value
);
979 PERROR("remove lock file");
981 ret
= close(lockfile_fd
);
983 PERROR("close lock file");
989 * Set the tracing group gid onto the client socket.
991 * Race window between mkdir and chown is OK because we are going from more
992 * permissive (root.root) to less permissive (root.tracing).
994 static int set_permissions(char *rundir
)
999 ret
= utils_get_group_id(
1000 the_config
.tracing_group_name
.value
, true, &gid
);
1002 /* Default to root group. */
1006 /* Set lttng run dir */
1007 ret
= chown(rundir
, 0, gid
);
1009 ERR("Unable to set group on %s", rundir
);
1014 * Ensure all applications and tracing group can search the run
1015 * dir. Allow everyone to read the directory, since it does not
1016 * buy us anything to hide its content.
1018 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1020 ERR("Unable to set permissions on %s", rundir
);
1024 /* lttng client socket path */
1025 ret
= chown(the_config
.client_unix_sock_path
.value
, 0, gid
);
1027 ERR("Unable to set group on %s",
1028 the_config
.client_unix_sock_path
.value
);
1032 /* kconsumer error socket path */
1033 ret
= chown(the_kconsumer_data
.err_unix_sock_path
, 0, 0);
1035 ERR("Unable to set group on %s",
1036 the_kconsumer_data
.err_unix_sock_path
);
1040 /* 64-bit ustconsumer error socket path */
1041 ret
= chown(the_ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1043 ERR("Unable to set group on %s",
1044 the_ustconsumer64_data
.err_unix_sock_path
);
1048 /* 32-bit ustconsumer compat32 error socket path */
1049 ret
= chown(the_ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1051 ERR("Unable to set group on %s",
1052 the_ustconsumer32_data
.err_unix_sock_path
);
1056 DBG("All permissions are set");
1062 * Create the lttng run directory needed for all global sockets and pipe.
1064 static int create_lttng_rundir(void)
1068 DBG3("Creating LTTng run directory: %s", the_config
.rundir
.value
);
1070 ret
= mkdir(the_config
.rundir
.value
, S_IRWXU
);
1072 if (errno
!= EEXIST
) {
1073 ERR("Unable to create %s", the_config
.rundir
.value
);
1085 * Setup sockets and directory needed by the consumerds' communication with the
1088 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1093 switch (consumer_data
->type
) {
1094 case LTTNG_CONSUMER_KERNEL
:
1095 path
= the_config
.kconsumerd_path
.value
;
1097 case LTTNG_CONSUMER64_UST
:
1098 path
= the_config
.consumerd64_path
.value
;
1100 case LTTNG_CONSUMER32_UST
:
1101 path
= the_config
.consumerd32_path
.value
;
1104 ERR("Consumer type unknown");
1110 DBG2("Creating consumer directory: %s", path
);
1112 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1113 if (ret
< 0 && errno
!= EEXIST
) {
1115 ERR("Failed to create %s", path
);
1121 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
,
1124 /* Default to root group. */
1128 ret
= chown(path
, 0, gid
);
1130 ERR("Unable to set group on %s", path
);
1136 /* Create the consumerd error unix socket */
1137 consumer_data
->err_sock
=
1138 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1139 if (consumer_data
->err_sock
< 0) {
1140 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1146 * Set the CLOEXEC flag. Return code is useless because either way, the
1149 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1151 PERROR("utils_set_fd_cloexec");
1152 /* continue anyway */
1155 /* File permission MUST be 660 */
1156 ret
= chmod(consumer_data
->err_unix_sock_path
,
1157 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1159 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1169 * Signal handler for the daemon
1171 * Simply stop all worker threads, leaving main() return gracefully after
1172 * joining all threads and calling cleanup().
1174 static void sighandler(int sig
, siginfo_t
*siginfo
, void *arg
)
1178 DBG("SIGINT caught");
1182 DBG("SIGTERM caught");
1186 CMM_STORE_SHARED(recv_child_signal
, 1);
1191 const char msg
[] = "Received SIGBUS, aborting program.\n";
1193 lttng_ust_handle_sigbus(siginfo
->si_addr
);
1195 * If ustctl did not catch this signal (triggering a
1196 * siglongjmp), abort the program. Otherwise, the execution
1197 * will resume from the ust-ctl call which caused this error.
1199 * The return value is ignored since the program aborts anyhow.
1201 write_ret
= write(STDERR_FILENO
, msg
, sizeof(msg
));
1211 * Setup signal handler for :
1212 * SIGINT, SIGTERM, SIGPIPE
1214 static int set_signal_handler(void)
1217 struct sigaction sa
;
1220 if ((ret
= sigemptyset(&sigset
)) < 0) {
1221 PERROR("sigemptyset");
1225 sa
.sa_mask
= sigset
;
1226 sa
.sa_flags
= SA_SIGINFO
;
1228 sa
.sa_sigaction
= sighandler
;
1229 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1230 PERROR("sigaction");
1234 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1235 PERROR("sigaction");
1239 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1240 PERROR("sigaction");
1244 if ((ret
= sigaction(SIGBUS
, &sa
, NULL
)) < 0) {
1245 PERROR("sigaction");
1250 sa
.sa_handler
= SIG_IGN
;
1251 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1252 PERROR("sigaction");
1256 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE, SIGINT, and SIGBUS");
1262 * Set open files limit to unlimited. This daemon can open a large number of
1263 * file descriptors in order to consume multiple kernel traces.
1265 static void set_ulimit(void)
1270 /* The kernel does not allow an infinite limit for open files */
1271 lim
.rlim_cur
= 65535;
1272 lim
.rlim_max
= 65535;
1274 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1276 PERROR("failed to set open files limit");
1280 static int write_pidfile(void)
1282 return utils_create_pid_file(getpid(), the_config
.pid_file_path
.value
);
1285 static int set_clock_plugin_env(void)
1288 char *env_value
= NULL
;
1290 if (!the_config
.lttng_ust_clock_plugin
.value
) {
1294 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1295 the_config
.lttng_ust_clock_plugin
.value
);
1301 ret
= putenv(env_value
);
1304 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1308 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1309 the_config
.lttng_ust_clock_plugin
.value
);
1314 static void destroy_all_sessions_and_wait(void)
1316 struct ltt_session
*session
, *tmp
;
1317 struct ltt_session_list
*session_list
;
1319 session_list
= session_get_list();
1320 DBG("Initiating destruction of all sessions");
1322 if (!session_list
) {
1326 session_lock_list();
1327 /* Initiate the destruction of all sessions. */
1328 cds_list_for_each_entry_safe(session
, tmp
,
1329 &session_list
->head
, list
) {
1330 if (!session_get(session
)) {
1334 session_lock(session
);
1335 if (session
->destroyed
) {
1336 goto unlock_session
;
1338 (void) cmd_stop_trace(session
);
1339 (void) cmd_destroy_session(
1340 session
, the_notification_thread_handle
, NULL
);
1342 session_unlock(session
);
1343 session_put(session
);
1345 session_unlock_list();
1347 /* Wait for the destruction of all sessions to complete. */
1348 DBG("Waiting for the destruction of all sessions to complete");
1349 session_list_wait_empty();
1350 DBG("Destruction of all sessions completed");
1353 static void unregister_all_triggers(void)
1355 enum lttng_error_code ret_code
;
1356 enum lttng_trigger_status trigger_status
;
1357 struct lttng_triggers
*triggers
= NULL
;
1358 unsigned int trigger_count
, i
;
1359 const struct lttng_credentials creds
= {
1360 .uid
= LTTNG_OPTIONAL_INIT_VALUE(0),
1363 DBG("Unregistering all triggers");
1366 * List all triggers as "root" since we wish to unregister all triggers.
1368 ret_code
= notification_thread_command_list_triggers(
1369 the_notification_thread_handle
, creds
.uid
.value
,
1371 if (ret_code
!= LTTNG_OK
) {
1372 ERR("Failed to list triggers while unregistering all triggers");
1376 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
1377 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1379 for (i
= 0; i
< trigger_count
; i
++) {
1380 uid_t trigger_owner
;
1381 const char *trigger_name
;
1382 const struct lttng_trigger
*trigger
=
1383 lttng_triggers_get_at_index(triggers
, i
);
1387 trigger_status
= lttng_trigger_get_owner_uid(
1388 trigger
, &trigger_owner
);
1389 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1391 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
1392 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
1393 trigger_name
: "(anonymous)";
1395 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1396 (int) trigger_owner
, trigger_name
);
1398 ret_code
= cmd_unregister_trigger(&creds
, trigger
,
1399 the_notification_thread_handle
);
1400 if (ret_code
!= LTTNG_OK
) {
1401 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1402 (int) trigger_owner
, trigger_name
,
1403 lttng_strerror(-ret_code
));
1404 /* Continue to unregister the remaining triggers. */
1408 lttng_triggers_destroy(triggers
);
1411 static int run_as_worker_post_fork_cleanup(void *data
)
1413 struct sessiond_config
*sessiond_config
= data
;
1415 sessiond_config_fini(sessiond_config
);
1419 static int launch_run_as_worker(const char *procname
)
1422 * Clean-up before forking the run-as worker. Any dynamically
1423 * allocated memory of which the worker is not aware will
1424 * be leaked as the process forks a run-as worker (and performs
1425 * no exec*()). The same would apply to any opened fd.
1427 return run_as_create_worker(
1428 procname
, run_as_worker_post_fork_cleanup
, &the_config
);
1431 static void sessiond_uuid_log(void)
1433 char uuid_str
[LTTNG_UUID_STR_LEN
];
1435 lttng_uuid_to_str(the_sessiond_uuid
, uuid_str
);
1436 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1442 int main(int argc
, char **argv
)
1444 int ret
= 0, retval
= 0;
1445 const char *env_app_timeout
;
1446 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1447 *ust64_channel_monitor_pipe
= NULL
,
1448 *kernel_channel_monitor_pipe
= NULL
;
1449 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1450 struct timer_thread_parameters timer_thread_parameters
;
1451 /* Rotation thread handle. */
1452 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1453 /* Queue of rotation jobs populated by the sessiond-timer. */
1454 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1455 struct lttng_thread
*client_thread
= NULL
;
1456 struct lttng_thread
*notification_thread
= NULL
;
1457 struct lttng_thread
*register_apps_thread
= NULL
;
1458 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
1460 logger_set_thread_name("Main", false);
1461 init_kernel_workarounds();
1463 rcu_register_thread();
1465 if (set_signal_handler()) {
1467 goto exit_set_signal_handler
;
1470 if (timer_signal_init()) {
1472 goto exit_set_signal_handler
;
1475 the_page_size
= sysconf(_SC_PAGESIZE
);
1476 if (the_page_size
< 0) {
1477 PERROR("sysconf _SC_PAGESIZE");
1478 the_page_size
= LONG_MAX
;
1479 WARN("Fallback page size to %ld", the_page_size
);
1482 ret
= sessiond_config_init(&the_config
);
1485 goto exit_set_signal_handler
;
1489 * Init config from environment variables.
1490 * Command line option override env configuration per-doc. Do env first.
1492 sessiond_config_apply_env_config(&the_config
);
1495 * Parse arguments and load the daemon configuration file.
1497 * We have an exit_options exit path to free memory reserved by
1498 * set_options. This is needed because the rest of sessiond_cleanup()
1499 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1500 * depends on set_options.
1503 if (set_options(argc
, argv
)) {
1509 * Resolve all paths received as arguments, configuration option, or
1510 * through environment variable as absolute paths. This is necessary
1511 * since daemonizing causes the sessiond's current working directory
1514 ret
= sessiond_config_resolve_paths(&the_config
);
1520 lttng_opt_verbose
= the_config
.verbose
;
1521 lttng_opt_quiet
= the_config
.quiet
;
1522 the_kconsumer_data
.err_unix_sock_path
=
1523 the_config
.kconsumerd_err_unix_sock_path
.value
;
1524 the_kconsumer_data
.cmd_unix_sock_path
=
1525 the_config
.kconsumerd_cmd_unix_sock_path
.value
;
1526 the_ustconsumer32_data
.err_unix_sock_path
=
1527 the_config
.consumerd32_err_unix_sock_path
.value
;
1528 the_ustconsumer32_data
.cmd_unix_sock_path
=
1529 the_config
.consumerd32_cmd_unix_sock_path
.value
;
1530 the_ustconsumer64_data
.err_unix_sock_path
=
1531 the_config
.consumerd64_err_unix_sock_path
.value
;
1532 the_ustconsumer64_data
.cmd_unix_sock_path
=
1533 the_config
.consumerd64_cmd_unix_sock_path
.value
;
1534 set_clock_plugin_env();
1536 sessiond_config_log(&the_config
);
1537 sessiond_uuid_log();
1539 if (opt_print_version
) {
1545 if (create_lttng_rundir()) {
1550 /* Abort launch if a session daemon is already running. */
1551 if (check_existing_daemon()) {
1552 ERR("A session daemon is already running.");
1558 if (the_config
.daemonize
|| the_config
.background
) {
1561 ret
= lttng_daemonize(&the_child_ppid
, &recv_child_signal
,
1562 !the_config
.background
);
1569 * We are in the child. Make sure all other file descriptors are
1570 * closed, in case we are called with more opened file
1571 * descriptors than the standard ones and the lock file.
1573 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1574 if (i
== lockfile_fd
) {
1581 if (launch_run_as_worker(argv
[0]) < 0) {
1582 goto exit_create_run_as_worker_cleanup
;
1586 * Starting from here, we can create threads. This needs to be after
1587 * lttng_daemonize due to RCU.
1591 * Initialize the health check subsystem. This call should set the
1592 * appropriate time values.
1594 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1595 if (!the_health_sessiond
) {
1596 PERROR("health_app_create error");
1601 /* Create thread to clean up RCU hash tables */
1602 ht_cleanup_thread
= launch_ht_cleanup_thread();
1603 if (!ht_cleanup_thread
) {
1608 /* Create thread quit pipe */
1609 if (sessiond_init_thread_quit_pipe()) {
1614 /* Check if daemon is UID = 0 */
1615 is_root
= !getuid();
1617 /* Create global run dir with root access */
1619 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1620 if (!kernel_channel_monitor_pipe
) {
1621 ERR("Failed to create kernel consumer channel monitor pipe");
1625 the_kconsumer_data
.channel_monitor_pipe
=
1626 lttng_pipe_release_writefd(
1627 kernel_channel_monitor_pipe
);
1628 if (the_kconsumer_data
.channel_monitor_pipe
< 0) {
1634 /* Set consumer initial state */
1635 the_kernel_consumerd_state
= CONSUMER_STOPPED
;
1636 the_ust_consumerd_state
= CONSUMER_STOPPED
;
1638 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1639 if (!ust32_channel_monitor_pipe
) {
1640 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1644 the_ustconsumer32_data
.channel_monitor_pipe
=
1645 lttng_pipe_release_writefd(ust32_channel_monitor_pipe
);
1646 if (the_ustconsumer32_data
.channel_monitor_pipe
< 0) {
1652 * The rotation_thread_timer_queue structure is shared between the
1653 * sessiond timer thread and the rotation thread. The main thread keeps
1654 * its ownership and destroys it when both threads have been joined.
1656 rotation_timer_queue
= rotation_thread_timer_queue_create();
1657 if (!rotation_timer_queue
) {
1661 timer_thread_parameters
.rotation_thread_job_queue
=
1662 rotation_timer_queue
;
1664 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1665 if (!ust64_channel_monitor_pipe
) {
1666 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1670 the_ustconsumer64_data
.channel_monitor_pipe
=
1671 lttng_pipe_release_writefd(ust64_channel_monitor_pipe
);
1672 if (the_ustconsumer64_data
.channel_monitor_pipe
< 0) {
1678 * Init UST app hash table. Alloc hash table before this point since
1679 * cleanup() can get called after that point.
1681 if (ust_app_ht_alloc()) {
1682 ERR("Failed to allocate UST app hash table");
1687 event_notifier_error_accounting_status
= event_notifier_error_accounting_init(
1688 the_config
.event_notifier_buffer_size_kernel
,
1689 the_config
.event_notifier_buffer_size_userspace
);
1690 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1691 ERR("Failed to initialize event notifier error accounting system");
1697 * Initialize agent app hash table. We allocate the hash table here
1698 * since cleanup() can get called after this point.
1700 if (agent_app_ht_alloc()) {
1701 ERR("Failed to allocate Agent app hash table");
1706 if (agent_by_event_notifier_domain_ht_create()) {
1707 ERR("Failed to allocate per-event notifier domain agent hash table");
1712 * These actions must be executed as root. We do that *after* setting up
1713 * the sockets path because we MUST make the check for another daemon using
1714 * those paths *before* trying to set the kernel consumer sockets and init
1718 if (set_consumer_sockets(&the_kconsumer_data
)) {
1723 /* Setup kernel tracer */
1724 if (!the_config
.no_kernel
) {
1725 init_kernel_tracer();
1728 /* Set ulimit for open files */
1731 /* init lttng_fd tracking must be done after set_ulimit. */
1734 if (set_consumer_sockets(&the_ustconsumer64_data
)) {
1739 if (set_consumer_sockets(&the_ustconsumer32_data
)) {
1744 /* Get parent pid if -S, --sig-parent is specified. */
1745 if (the_config
.sig_parent
) {
1746 the_ppid
= getppid();
1749 /* Setup the kernel pipe for waking up the kernel thread */
1750 if (is_root
&& !the_config
.no_kernel
) {
1751 if (utils_create_pipe_cloexec(the_kernel_poll_pipe
)) {
1757 /* Setup the thread apps communication pipe. */
1758 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1763 /* Setup the thread apps notify communication pipe. */
1764 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1769 /* Initialize global buffer per UID and PID registry. */
1770 buffer_reg_init_uid_registry();
1771 buffer_reg_init_pid_registry();
1773 /* Init UST command queue. */
1774 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1778 /* Check for the application socket timeout env variable. */
1779 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1780 if (env_app_timeout
) {
1781 the_config
.app_socket_timeout
= atoi(env_app_timeout
);
1783 the_config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1786 ret
= write_pidfile();
1788 ERR("Error in write_pidfile");
1793 /* Initialize communication library */
1795 /* Initialize TCP timeout values */
1796 lttcomm_inet_init();
1798 /* Create health-check thread. */
1799 if (!launch_health_management_thread()) {
1804 /* notification_thread_data acquires the pipes' read side. */
1805 the_notification_thread_handle
= notification_thread_handle_create(
1806 ust32_channel_monitor_pipe
, ust64_channel_monitor_pipe
,
1807 kernel_channel_monitor_pipe
);
1808 if (!the_notification_thread_handle
) {
1810 ERR("Failed to create notification thread shared data");
1814 /* Create notification thread. */
1815 notification_thread
= launch_notification_thread(
1816 the_notification_thread_handle
);
1817 if (!notification_thread
) {
1822 /* Create timer thread. */
1823 if (!launch_timer_thread(&timer_thread_parameters
)) {
1828 /* rotation_thread_data acquires the pipes' read side. */
1829 rotation_thread_handle
= rotation_thread_handle_create(
1830 rotation_timer_queue
, the_notification_thread_handle
);
1831 if (!rotation_thread_handle
) {
1833 ERR("Failed to create rotation thread shared data");
1838 /* Create rotation thread. */
1839 if (!launch_rotation_thread(rotation_thread_handle
)) {
1844 /* Create thread to manage the client socket */
1845 client_thread
= launch_client_thread();
1846 if (!client_thread
) {
1851 /* Set credentials of the client socket and rundir */
1852 if (is_root
&& set_permissions(the_config
.rundir
.value
)) {
1857 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1858 apps_cmd_notify_pipe
[1])) {
1863 /* Create thread to manage application registration. */
1864 register_apps_thread
= launch_application_registration_thread(
1866 if (!register_apps_thread
) {
1871 /* Create thread to manage application socket */
1872 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1877 /* Create thread to manage application notify socket */
1878 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1883 /* Create agent management thread. */
1884 if (!launch_agent_management_thread()) {
1889 /* Don't start this thread if kernel tracing is not requested nor root */
1890 if (is_root
&& !the_config
.no_kernel
) {
1891 /* Create kernel thread to manage kernel event */
1892 if (!launch_kernel_management_thread(the_kernel_poll_pipe
[0])) {
1897 if (kernel_get_notification_fd() >= 0) {
1898 ret
= notification_thread_command_add_tracer_event_source(
1899 the_notification_thread_handle
,
1900 kernel_get_notification_fd(),
1901 LTTNG_DOMAIN_KERNEL
);
1902 if (ret
!= LTTNG_OK
) {
1903 ERR("Failed to add kernel trigger event source to notification thread");
1910 /* Load sessions. */
1911 ret
= config_load_session(
1912 the_config
.load_session_path
.value
, NULL
, 1, 1, NULL
);
1914 ERR("Session load failed: %s", error_get_str(ret
));
1919 /* Initialization completed. */
1920 sessiond_signal_parents();
1923 * This is where we start awaiting program completion (e.g. through
1924 * signal that asks threads to teardown).
1927 /* Initiate teardown once activity occurs on the quit pipe. */
1928 sessiond_wait_for_quit_pipe(-1);
1933 * Ensure that the client thread is no longer accepting new commands,
1934 * which could cause new sessions to be created.
1936 if (client_thread
) {
1937 lttng_thread_shutdown(client_thread
);
1938 lttng_thread_put(client_thread
);
1941 destroy_all_sessions_and_wait();
1944 * At this point no new trigger can be registered (no sessions are
1945 * running/rotating) and clients can't connect to the session daemon
1946 * anymore. Unregister all triggers.
1948 unregister_all_triggers();
1950 if (register_apps_thread
) {
1951 lttng_thread_shutdown(register_apps_thread
);
1952 lttng_thread_put(register_apps_thread
);
1954 lttng_thread_list_shutdown_orphans();
1957 * Wait for all pending call_rcu work to complete before tearing
1958 * down data structures. call_rcu worker may be trying to
1959 * perform lookups in those structures.
1963 * sessiond_cleanup() is called when no other thread is running, except
1964 * the ht_cleanup thread, which is needed to destroy the hash tables.
1966 rcu_thread_online();
1970 * Wait for all pending call_rcu work to complete before shutting down
1971 * the notification thread. This call_rcu work includes shutting down
1972 * UST apps and event notifier pipes.
1976 if (notification_thread
) {
1977 lttng_thread_shutdown(notification_thread
);
1978 lttng_thread_put(notification_thread
);
1982 * Error accounting teardown has to be done after the teardown of all
1983 * event notifier pipes to ensure that no tracer may try to use the
1984 * error accounting facilities.
1986 event_notifier_error_accounting_fini();
1989 * Unloading the kernel modules needs to be done after all kernel
1990 * ressources have been released. In our case, this includes the
1991 * notification fd, the event notifier group fd, error accounting fd,
1992 * all event and event notifier fds, etc.
1994 * In short, at this point, we need to have called close() on all fds
1995 * received from the kernel tracer.
1997 if (is_root
&& !the_config
.no_kernel
) {
1998 DBG("Unloading kernel modules");
1999 modprobe_remove_lttng_all();
2003 * Ensure all prior call_rcu are done. call_rcu callbacks may push
2004 * hash tables to the ht_cleanup thread. Therefore, we ensure that
2005 * the queue is empty before shutting down the clean-up thread.
2009 if (ht_cleanup_thread
) {
2010 lttng_thread_shutdown(ht_cleanup_thread
);
2011 lttng_thread_put(ht_cleanup_thread
);
2014 rcu_thread_offline();
2015 rcu_unregister_thread();
2017 if (rotation_thread_handle
) {
2018 rotation_thread_handle_destroy(rotation_thread_handle
);
2022 * After the rotation and timer thread have quit, we can safely destroy
2023 * the rotation_timer_queue.
2025 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
2027 * The teardown of the notification system is performed after the
2028 * session daemon's teardown in order to allow it to be notified
2029 * of the active session and channels at the moment of the teardown.
2031 if (the_notification_thread_handle
) {
2032 notification_thread_handle_destroy(
2033 the_notification_thread_handle
);
2035 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
2036 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
2037 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
2039 if (the_health_sessiond
) {
2040 health_app_destroy(the_health_sessiond
);
2042 exit_create_run_as_worker_cleanup
:
2044 sessiond_cleanup_lock_file();
2045 sessiond_cleanup_options();
2047 exit_set_signal_handler
: