2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
22 #include <sys/mount.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <sys/types.h>
28 #include <urcu/uatomic.h>
32 #include <common/common.h>
33 #include <common/compat/socket.h>
34 #include <common/compat/getenv.h>
35 #include <common/defaults.h>
36 #include <common/kernel-consumer/kernel-consumer.h>
37 #include <common/futex.h>
38 #include <common/relayd/relayd.h>
39 #include <common/utils.h>
40 #include <common/path.h>
41 #include <common/daemonize.h>
42 #include <common/config/session-config.h>
43 #include <common/ini-config/ini-config.h>
44 #include <common/dynamic-buffer.h>
45 #include <lttng/event-internal.h>
47 #include "lttng-sessiond.h"
48 #include "buffer-registry.h"
54 #include "event-notifier-error-accounting.h"
56 #include "kernel-consumer.h"
57 #include "lttng-ust-ctl.h"
58 #include "ust-consumer.h"
61 #include "health-sessiond.h"
62 #include "testpoint.h"
63 #include "notify-apps.h"
64 #include "agent-thread.h"
66 #include "notification-thread.h"
67 #include "notification-thread-commands.h"
68 #include "rotation-thread.h"
70 #include "ht-cleanup.h"
71 #include "sessiond-config.h"
77 #include "manage-apps.h"
78 #include "manage-kernel.h"
80 #include "ust-sigbus.h"
82 static const char *help_msg
=
83 #ifdef LTTNG_EMBED_HELP
84 #include <lttng-sessiond.8.h>
90 #define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
91 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR \
92 "event-notifier-error-buffer-size"
93 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR \
94 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-kernel"
95 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR \
96 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-userspace"
100 static int lockfile_fd
= -1;
101 static int opt_print_version
;
103 /* Set to 1 when a SIGUSR1 signal is received. */
104 static int recv_child_signal
;
106 /* Command line options */
107 static const struct option long_options
[] = {
108 { "client-sock", required_argument
, 0, 'c' },
109 { "apps-sock", required_argument
, 0, 'a' },
110 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
111 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
112 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
113 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
114 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
115 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
116 { "consumerd32-path", required_argument
, 0, '\0' },
117 { "consumerd32-libdir", required_argument
, 0, '\0' },
118 { "consumerd64-path", required_argument
, 0, '\0' },
119 { "consumerd64-libdir", required_argument
, 0, '\0' },
120 { "daemonize", no_argument
, 0, 'd' },
121 { "background", no_argument
, 0, 'b' },
122 { "sig-parent", no_argument
, 0, 'S' },
123 { "help", no_argument
, 0, 'h' },
124 { "group", required_argument
, 0, 'g' },
125 { "version", no_argument
, 0, 'V' },
126 { "quiet", no_argument
, 0, 'q' },
127 { "verbose", no_argument
, 0, 'v' },
128 { "verbose-consumer", no_argument
, 0, '\0' },
129 { "no-kernel", no_argument
, 0, '\0' },
130 { "pidfile", required_argument
, 0, 'p' },
131 { "agent-tcp-port", required_argument
, 0, '\0' },
132 { "config", required_argument
, 0, 'f' },
133 { "load", required_argument
, 0, 'l' },
134 { "kmod-probes", required_argument
, 0, '\0' },
135 { "extra-kmod-probes", required_argument
, 0, '\0' },
136 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, required_argument
, 0, '\0' },
137 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, required_argument
, 0, '\0' },
141 /* Command line options to ignore from configuration file */
142 static const char *config_ignore_options
[] = { "help", "version", "config" };
145 * This pipe is used to inform the thread managing application communication
146 * that a command is queued and ready to be processed.
148 static int apps_cmd_pipe
[2] = { -1, -1 };
149 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
152 * UST registration command queue. This queue is tied with a futex and uses a N
153 * wakers / 1 waiter implemented and detailed in futex.c/.h
155 * The thread_registration_apps and thread_dispatch_ust_registration uses this
156 * queue along with the wait/wake scheme. The thread_manage_apps receives down
157 * the line new application socket and monitors it for any I/O error or clean
158 * close that triggers an unregistration of the application.
160 static struct ust_cmd_queue ust_cmd_queue
;
163 * Section name to look for in the daemon configuration file.
165 static const char * const config_section_name
= "sessiond";
167 /* Am I root or not. Set to 1 if the daemon is running as root */
171 * Stop all threads by closing the thread quit pipe.
173 static void stop_threads(void)
177 /* Stopping all threads */
178 DBG("Terminating all threads");
179 ret
= sessiond_notify_quit_pipe();
181 ERR("write error on thread quit pipe");
186 * Close every consumer sockets.
188 static void close_consumer_sockets(void)
192 if (the_kconsumer_data
.err_sock
>= 0) {
193 ret
= close(the_kconsumer_data
.err_sock
);
195 PERROR("kernel consumer err_sock close");
198 if (the_ustconsumer32_data
.err_sock
>= 0) {
199 ret
= close(the_ustconsumer32_data
.err_sock
);
201 PERROR("UST consumerd32 err_sock close");
204 if (the_ustconsumer64_data
.err_sock
>= 0) {
205 ret
= close(the_ustconsumer64_data
.err_sock
);
207 PERROR("UST consumerd64 err_sock close");
210 if (the_kconsumer_data
.cmd_sock
>= 0) {
211 ret
= close(the_kconsumer_data
.cmd_sock
);
213 PERROR("kernel consumer cmd_sock close");
216 if (the_ustconsumer32_data
.cmd_sock
>= 0) {
217 ret
= close(the_ustconsumer32_data
.cmd_sock
);
219 PERROR("UST consumerd32 cmd_sock close");
222 if (the_ustconsumer64_data
.cmd_sock
>= 0) {
223 ret
= close(the_ustconsumer64_data
.cmd_sock
);
225 PERROR("UST consumerd64 cmd_sock close");
228 if (the_kconsumer_data
.channel_monitor_pipe
>= 0) {
229 ret
= close(the_kconsumer_data
.channel_monitor_pipe
);
231 PERROR("kernel consumer channel monitor pipe close");
234 if (the_ustconsumer32_data
.channel_monitor_pipe
>= 0) {
235 ret
= close(the_ustconsumer32_data
.channel_monitor_pipe
);
237 PERROR("UST consumerd32 channel monitor pipe close");
240 if (the_ustconsumer64_data
.channel_monitor_pipe
>= 0) {
241 ret
= close(the_ustconsumer64_data
.channel_monitor_pipe
);
243 PERROR("UST consumerd64 channel monitor pipe close");
249 * Wait on consumer process termination.
251 * Need to be called with the consumer data lock held or from a context
252 * ensuring no concurrent access to data (e.g: cleanup).
254 static void wait_consumer(struct consumer_data
*consumer_data
)
259 if (consumer_data
->pid
<= 0) {
263 DBG("Waiting for complete teardown of consumerd (PID: %d)",
265 ret
= waitpid(consumer_data
->pid
, &status
, 0);
267 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
268 } else if (!WIFEXITED(status
)) {
269 ERR("consumerd termination with error: %d",
272 consumer_data
->pid
= 0;
276 * Cleanup the session daemon's data structures.
278 static void sessiond_cleanup(void)
281 struct ltt_session_list
*session_list
= session_get_list();
283 DBG("Cleanup sessiond");
286 * Close the thread quit pipe. It has already done its job,
287 * since we are now called.
289 sessiond_close_quit_pipe();
290 utils_close_pipe(apps_cmd_pipe
);
291 utils_close_pipe(apps_cmd_notify_pipe
);
292 utils_close_pipe(the_kernel_poll_pipe
);
294 ret
= remove(the_config
.pid_file_path
.value
);
296 PERROR("remove pidfile %s", the_config
.pid_file_path
.value
);
299 DBG("Removing sessiond and consumerd content of directory %s",
300 the_config
.rundir
.value
);
303 DBG("Removing %s", the_config
.pid_file_path
.value
);
304 (void) unlink(the_config
.pid_file_path
.value
);
306 DBG("Removing %s", the_config
.agent_port_file_path
.value
);
307 (void) unlink(the_config
.agent_port_file_path
.value
);
310 DBG("Removing %s", the_kconsumer_data
.err_unix_sock_path
);
311 (void) unlink(the_kconsumer_data
.err_unix_sock_path
);
313 DBG("Removing directory %s", the_config
.kconsumerd_path
.value
);
314 (void) rmdir(the_config
.kconsumerd_path
.value
);
316 /* ust consumerd 32 */
317 DBG("Removing %s", the_config
.consumerd32_err_unix_sock_path
.value
);
318 (void) unlink(the_config
.consumerd32_err_unix_sock_path
.value
);
320 DBG("Removing directory %s", the_config
.consumerd32_path
.value
);
321 (void) rmdir(the_config
.consumerd32_path
.value
);
323 /* ust consumerd 64 */
324 DBG("Removing %s", the_config
.consumerd64_err_unix_sock_path
.value
);
325 (void) unlink(the_config
.consumerd64_err_unix_sock_path
.value
);
327 DBG("Removing directory %s", the_config
.consumerd64_path
.value
);
328 (void) rmdir(the_config
.consumerd64_path
.value
);
330 pthread_mutex_destroy(&session_list
->lock
);
332 DBG("Cleaning up all per-event notifier domain agents");
333 agent_by_event_notifier_domain_ht_destroy();
335 DBG("Cleaning up all agent apps");
336 agent_app_ht_clean();
337 DBG("Closing all UST sockets");
338 ust_app_clean_list();
339 buffer_reg_destroy_registries();
341 close_consumer_sockets();
343 wait_consumer(&the_kconsumer_data
);
344 wait_consumer(&the_ustconsumer64_data
);
345 wait_consumer(&the_ustconsumer32_data
);
347 if (is_root
&& !the_config
.no_kernel
) {
348 cleanup_kernel_tracer();
352 * We do NOT rmdir rundir because there are other processes
353 * using it, for instance lttng-relayd, which can start in
354 * parallel with this teardown.
359 * Cleanup the daemon's option data structures.
361 static void sessiond_cleanup_options(void)
363 DBG("Cleaning up options");
365 sessiond_config_fini(&the_config
);
367 run_as_destroy_worker();
370 static int string_match(const char *str1
, const char *str2
)
372 return (str1
&& str2
) && !strcmp(str1
, str2
);
376 * Take an option from the getopt output and set it in the right variable to be
379 * Return 0 on success else a negative value.
381 static int set_option(int opt
, const char *arg
, const char *optname
)
385 if (string_match(optname
, "client-sock") || opt
== 'c') {
386 if (!arg
|| *arg
== '\0') {
390 if (lttng_is_setuid_setgid()) {
391 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
392 "-c, --client-sock");
394 config_string_set(&the_config
.client_unix_sock_path
,
396 if (!the_config
.client_unix_sock_path
.value
) {
401 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
402 if (!arg
|| *arg
== '\0') {
406 if (lttng_is_setuid_setgid()) {
407 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
410 config_string_set(&the_config
.apps_unix_sock_path
,
412 if (!the_config
.apps_unix_sock_path
.value
) {
417 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
418 the_config
.daemonize
= true;
419 } else if (string_match(optname
, "background") || opt
== 'b') {
420 the_config
.background
= true;
421 } else if (string_match(optname
, "group") || opt
== 'g') {
422 if (!arg
|| *arg
== '\0') {
426 if (lttng_is_setuid_setgid()) {
427 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
430 config_string_set(&the_config
.tracing_group_name
,
432 if (!the_config
.tracing_group_name
.value
) {
437 } else if (string_match(optname
, "help") || opt
== 'h') {
438 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
440 ERR("Cannot show --help for `lttng-sessiond`");
443 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
444 } else if (string_match(optname
, "version") || opt
== 'V') {
445 opt_print_version
= 1;
446 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
447 the_config
.sig_parent
= true;
448 } else if (string_match(optname
, "kconsumerd-err-sock")) {
449 if (!arg
|| *arg
== '\0') {
453 if (lttng_is_setuid_setgid()) {
454 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
455 "--kconsumerd-err-sock");
458 &the_config
.kconsumerd_err_unix_sock_path
,
460 if (!the_config
.kconsumerd_err_unix_sock_path
.value
) {
465 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
466 if (!arg
|| *arg
== '\0') {
470 if (lttng_is_setuid_setgid()) {
471 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
472 "--kconsumerd-cmd-sock");
475 &the_config
.kconsumerd_cmd_unix_sock_path
,
477 if (!the_config
.kconsumerd_cmd_unix_sock_path
.value
) {
482 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
483 if (!arg
|| *arg
== '\0') {
487 if (lttng_is_setuid_setgid()) {
488 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
489 "--ustconsumerd64-err-sock");
492 &the_config
.consumerd64_err_unix_sock_path
,
494 if (!the_config
.consumerd64_err_unix_sock_path
.value
) {
499 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
500 if (!arg
|| *arg
== '\0') {
504 if (lttng_is_setuid_setgid()) {
505 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
506 "--ustconsumerd64-cmd-sock");
509 &the_config
.consumerd64_cmd_unix_sock_path
,
511 if (!the_config
.consumerd64_cmd_unix_sock_path
.value
) {
516 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
517 if (!arg
|| *arg
== '\0') {
521 if (lttng_is_setuid_setgid()) {
522 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
523 "--ustconsumerd32-err-sock");
526 &the_config
.consumerd32_err_unix_sock_path
,
528 if (!the_config
.consumerd32_err_unix_sock_path
.value
) {
533 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
534 if (!arg
|| *arg
== '\0') {
538 if (lttng_is_setuid_setgid()) {
539 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
540 "--ustconsumerd32-cmd-sock");
543 &the_config
.consumerd32_cmd_unix_sock_path
,
545 if (!the_config
.consumerd32_cmd_unix_sock_path
.value
) {
550 } else if (string_match(optname
, "no-kernel")) {
551 the_config
.no_kernel
= true;
552 } else if (string_match(optname
, "quiet") || opt
== 'q') {
553 the_config
.quiet
= true;
554 } else if (string_match(optname
, "verbose") || opt
== 'v') {
555 /* Verbose level can increase using multiple -v */
557 /* Value obtained from config file */
558 the_config
.verbose
= config_parse_value(arg
);
560 /* -v used on command line */
561 the_config
.verbose
++;
563 /* Clamp value to [0, 3] */
564 the_config
.verbose
= the_config
.verbose
< 0 ?
566 (the_config
.verbose
<= 3 ? the_config
.verbose
:
568 } else if (string_match(optname
, "verbose-consumer")) {
570 the_config
.verbose_consumer
= config_parse_value(arg
);
572 the_config
.verbose_consumer
++;
574 } else if (string_match(optname
, "consumerd32-path")) {
575 if (!arg
|| *arg
== '\0') {
579 if (lttng_is_setuid_setgid()) {
580 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
581 "--consumerd32-path");
583 config_string_set(&the_config
.consumerd32_bin_path
,
585 if (!the_config
.consumerd32_bin_path
.value
) {
590 } else if (string_match(optname
, "consumerd32-libdir")) {
591 if (!arg
|| *arg
== '\0') {
595 if (lttng_is_setuid_setgid()) {
596 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
597 "--consumerd32-libdir");
599 config_string_set(&the_config
.consumerd32_lib_dir
,
601 if (!the_config
.consumerd32_lib_dir
.value
) {
606 } else if (string_match(optname
, "consumerd64-path")) {
607 if (!arg
|| *arg
== '\0') {
611 if (lttng_is_setuid_setgid()) {
612 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
613 "--consumerd64-path");
615 config_string_set(&the_config
.consumerd64_bin_path
,
617 if (!the_config
.consumerd64_bin_path
.value
) {
622 } else if (string_match(optname
, "consumerd64-libdir")) {
623 if (!arg
|| *arg
== '\0') {
627 if (lttng_is_setuid_setgid()) {
628 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
629 "--consumerd64-libdir");
631 config_string_set(&the_config
.consumerd64_lib_dir
,
633 if (!the_config
.consumerd64_lib_dir
.value
) {
638 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
639 if (!arg
|| *arg
== '\0') {
643 if (lttng_is_setuid_setgid()) {
644 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
648 &the_config
.pid_file_path
, strdup(arg
));
649 if (!the_config
.pid_file_path
.value
) {
654 } else if (string_match(optname
, "agent-tcp-port")) {
655 if (!arg
|| *arg
== '\0') {
659 if (lttng_is_setuid_setgid()) {
660 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
666 v
= strtoul(arg
, NULL
, 0);
667 if (errno
!= 0 || !isdigit(arg
[0])) {
668 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
671 if (v
== 0 || v
>= 65535) {
672 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
675 the_config
.agent_tcp_port
.begin
=
676 the_config
.agent_tcp_port
.end
= (int) v
;
677 DBG3("Agent TCP port set to non default: %i", (int) v
);
679 } else if (string_match(optname
, "load") || opt
== 'l') {
680 if (!arg
|| *arg
== '\0') {
684 if (lttng_is_setuid_setgid()) {
685 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
688 config_string_set(&the_config
.load_session_path
,
690 if (!the_config
.load_session_path
.value
) {
695 } else if (string_match(optname
, "kmod-probes")) {
696 if (!arg
|| *arg
== '\0') {
700 if (lttng_is_setuid_setgid()) {
701 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
704 config_string_set(&the_config
.kmod_probes_list
,
706 if (!the_config
.kmod_probes_list
.value
) {
711 } else if (string_match(optname
, "extra-kmod-probes")) {
712 if (!arg
|| *arg
== '\0') {
716 if (lttng_is_setuid_setgid()) {
717 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
718 "--extra-kmod-probes");
720 config_string_set(&the_config
.kmod_extra_probes_list
,
722 if (!the_config
.kmod_extra_probes_list
.value
) {
727 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
)) {
731 v
= strtoul(arg
, NULL
, 0);
732 if (errno
!= 0 || !isdigit(arg
[0])) {
733 ERR("Wrong value in --%s parameter: %s",
734 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
737 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
738 ERR("Value out of range for --%s parameter: %s",
739 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
742 the_config
.event_notifier_buffer_size_kernel
= (int) v
;
743 DBG3("Number of event notifier error buffer kernel size to non default: %i",
744 the_config
.event_notifier_buffer_size_kernel
);
746 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
)) {
750 v
= strtoul(arg
, NULL
, 0);
751 if (errno
!= 0 || !isdigit(arg
[0])) {
752 ERR("Wrong value in --%s parameter: %s",
753 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
756 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
757 ERR("Value out of range for --%s parameter: %s",
758 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
761 the_config
.event_notifier_buffer_size_userspace
= (int) v
;
762 DBG3("Number of event notifier error buffer userspace size to non default: %i",
763 the_config
.event_notifier_buffer_size_userspace
);
765 } else if (string_match(optname
, "config") || opt
== 'f') {
766 /* This is handled in set_options() thus silent skip. */
769 /* Unknown option or other error.
770 * Error is printed by getopt, just return */
775 if (ret
== -EINVAL
) {
776 const char *opt_name
= "unknown";
779 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
781 if (opt
== long_options
[i
].val
) {
782 opt_name
= long_options
[i
].name
;
787 WARN("Invalid argument provided for option \"%s\", using default value.",
795 * config_entry_handler_cb used to handle options read from a config file.
796 * See config_entry_handler_cb comment in common/config/session-config.h for the
797 * return value conventions.
799 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
803 if (!entry
|| !entry
->name
|| !entry
->value
) {
808 /* Check if the option is to be ignored */
809 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
810 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
815 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
818 /* Ignore if not fully matched. */
819 if (strcmp(entry
->name
, long_options
[i
].name
)) {
824 * If the option takes no argument on the command line, we have to
825 * check if the value is "true". We support non-zero numeric values,
828 if (!long_options
[i
].has_arg
) {
829 ret
= config_parse_value(entry
->value
);
832 WARN("Invalid configuration value \"%s\" for option %s",
833 entry
->value
, entry
->name
);
835 /* False, skip boolean config option. */
840 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
844 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
850 static void print_version(void) {
851 fprintf(stdout
, "%s\n", VERSION
);
855 * daemon configuration loading and argument parsing
857 static int set_options(int argc
, char **argv
)
859 int ret
= 0, c
= 0, option_index
= 0;
860 int orig_optopt
= optopt
, orig_optind
= optind
;
862 char *config_path
= NULL
;
864 optstring
= utils_generate_optstring(long_options
,
865 sizeof(long_options
) / sizeof(struct option
));
871 /* Check for the --config option */
872 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
873 &option_index
)) != -1) {
877 } else if (c
!= 'f') {
878 /* if not equal to --config option. */
882 if (lttng_is_setuid_setgid()) {
883 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
887 config_path
= utils_expand_path(optarg
);
889 ERR("Failed to resolve path: %s", optarg
);
894 ret
= config_get_section_entries(config_path
, config_section_name
,
895 config_entry_handler
, NULL
);
898 ERR("Invalid configuration option at line %i", ret
);
904 /* Reset getopt's global state */
905 optopt
= orig_optopt
;
906 optind
= orig_optind
;
910 * getopt_long() will not set option_index if it encounters a
913 c
= getopt_long(argc
, argv
, optstring
, long_options
,
920 * Pass NULL as the long option name if popt left the index
923 ret
= set_option(c
, optarg
,
924 option_index
< 0 ? NULL
:
925 long_options
[option_index
].name
);
938 * Create lockfile using the rundir and return its fd.
940 static int create_lockfile(void)
942 return utils_create_lock_file(the_config
.lock_file_path
.value
);
946 * Check if the global socket is available, and if a daemon is answering at the
947 * other side. If yes, error is returned.
949 * Also attempts to create and hold the lock file.
951 static int check_existing_daemon(void)
955 /* Is there anybody out there ? */
956 if (lttng_session_daemon_alive()) {
961 lockfile_fd
= create_lockfile();
962 if (lockfile_fd
< 0) {
970 static void sessiond_cleanup_lock_file(void)
975 * Cleanup lock file by deleting it and finaly closing it which will
976 * release the file system lock.
978 if (lockfile_fd
>= 0) {
979 ret
= remove(the_config
.lock_file_path
.value
);
981 PERROR("remove lock file");
983 ret
= close(lockfile_fd
);
985 PERROR("close lock file");
991 * Set the tracing group gid onto the client socket.
993 * Race window between mkdir and chown is OK because we are going from more
994 * permissive (root.root) to less permissive (root.tracing).
996 static int set_permissions(char *rundir
)
1001 ret
= utils_get_group_id(
1002 the_config
.tracing_group_name
.value
, true, &gid
);
1004 /* Default to root group. */
1008 /* Set lttng run dir */
1009 ret
= chown(rundir
, 0, gid
);
1011 ERR("Unable to set group on %s", rundir
);
1016 * Ensure all applications and tracing group can search the run
1017 * dir. Allow everyone to read the directory, since it does not
1018 * buy us anything to hide its content.
1020 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1022 ERR("Unable to set permissions on %s", rundir
);
1026 /* lttng client socket path */
1027 ret
= chown(the_config
.client_unix_sock_path
.value
, 0, gid
);
1029 ERR("Unable to set group on %s",
1030 the_config
.client_unix_sock_path
.value
);
1034 /* kconsumer error socket path */
1035 ret
= chown(the_kconsumer_data
.err_unix_sock_path
, 0, 0);
1037 ERR("Unable to set group on %s",
1038 the_kconsumer_data
.err_unix_sock_path
);
1042 /* 64-bit ustconsumer error socket path */
1043 ret
= chown(the_ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1045 ERR("Unable to set group on %s",
1046 the_ustconsumer64_data
.err_unix_sock_path
);
1050 /* 32-bit ustconsumer compat32 error socket path */
1051 ret
= chown(the_ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1053 ERR("Unable to set group on %s",
1054 the_ustconsumer32_data
.err_unix_sock_path
);
1058 DBG("All permissions are set");
1064 * Create the lttng run directory needed for all global sockets and pipe.
1066 static int create_lttng_rundir(void)
1070 DBG3("Creating LTTng run directory: %s", the_config
.rundir
.value
);
1072 ret
= mkdir(the_config
.rundir
.value
, S_IRWXU
);
1074 if (errno
!= EEXIST
) {
1075 ERR("Unable to create %s", the_config
.rundir
.value
);
1087 * Setup sockets and directory needed by the consumerds' communication with the
1090 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1095 switch (consumer_data
->type
) {
1096 case LTTNG_CONSUMER_KERNEL
:
1097 path
= the_config
.kconsumerd_path
.value
;
1099 case LTTNG_CONSUMER64_UST
:
1100 path
= the_config
.consumerd64_path
.value
;
1102 case LTTNG_CONSUMER32_UST
:
1103 path
= the_config
.consumerd32_path
.value
;
1106 ERR("Consumer type unknown");
1112 DBG2("Creating consumer directory: %s", path
);
1114 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1115 if (ret
< 0 && errno
!= EEXIST
) {
1117 ERR("Failed to create %s", path
);
1123 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
,
1126 /* Default to root group. */
1130 ret
= chown(path
, 0, gid
);
1132 ERR("Unable to set group on %s", path
);
1138 /* Create the consumerd error unix socket */
1139 consumer_data
->err_sock
=
1140 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1141 if (consumer_data
->err_sock
< 0) {
1142 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1148 * Set the CLOEXEC flag. Return code is useless because either way, the
1151 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1153 PERROR("utils_set_fd_cloexec");
1154 /* continue anyway */
1157 /* File permission MUST be 660 */
1158 ret
= chmod(consumer_data
->err_unix_sock_path
,
1159 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1161 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1171 * Signal handler for the daemon
1173 * Simply stop all worker threads, leaving main() return gracefully after
1174 * joining all threads and calling cleanup().
1176 static void sighandler(int sig
, siginfo_t
*siginfo
, void *arg
)
1180 DBG("SIGINT caught");
1184 DBG("SIGTERM caught");
1188 CMM_STORE_SHARED(recv_child_signal
, 1);
1193 const char msg
[] = "Received SIGBUS, aborting program.\n";
1195 lttng_ust_handle_sigbus(siginfo
->si_addr
);
1197 * If ustctl did not catch this signal (triggering a
1198 * siglongjmp), abort the program. Otherwise, the execution
1199 * will resume from the ust-ctl call which caused this error.
1201 * The return value is ignored since the program aborts anyhow.
1203 write_ret
= write(STDERR_FILENO
, msg
, sizeof(msg
));
1213 * Setup signal handler for :
1214 * SIGINT, SIGTERM, SIGPIPE
1216 static int set_signal_handler(void)
1219 struct sigaction sa
;
1222 if ((ret
= sigemptyset(&sigset
)) < 0) {
1223 PERROR("sigemptyset");
1227 sa
.sa_mask
= sigset
;
1228 sa
.sa_flags
= SA_SIGINFO
;
1230 sa
.sa_sigaction
= sighandler
;
1231 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1232 PERROR("sigaction");
1236 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1237 PERROR("sigaction");
1241 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1242 PERROR("sigaction");
1246 if ((ret
= sigaction(SIGBUS
, &sa
, NULL
)) < 0) {
1247 PERROR("sigaction");
1252 sa
.sa_handler
= SIG_IGN
;
1253 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1254 PERROR("sigaction");
1258 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE, SIGINT, and SIGBUS");
1264 * Set open files limit to unlimited. This daemon can open a large number of
1265 * file descriptors in order to consume multiple kernel traces.
1267 static void set_ulimit(void)
1272 /* The kernel does not allow an infinite limit for open files */
1273 lim
.rlim_cur
= 65535;
1274 lim
.rlim_max
= 65535;
1276 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1278 PERROR("failed to set open files limit");
1282 static int write_pidfile(void)
1284 return utils_create_pid_file(getpid(), the_config
.pid_file_path
.value
);
1287 static int set_clock_plugin_env(void)
1290 char *env_value
= NULL
;
1292 if (!the_config
.lttng_ust_clock_plugin
.value
) {
1296 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1297 the_config
.lttng_ust_clock_plugin
.value
);
1303 ret
= putenv(env_value
);
1306 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1310 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1311 the_config
.lttng_ust_clock_plugin
.value
);
1316 static void destroy_all_sessions_and_wait(void)
1318 struct ltt_session
*session
, *tmp
;
1319 struct ltt_session_list
*session_list
;
1321 session_list
= session_get_list();
1322 DBG("Initiating destruction of all sessions");
1324 if (!session_list
) {
1328 session_lock_list();
1329 /* Initiate the destruction of all sessions. */
1330 cds_list_for_each_entry_safe(session
, tmp
,
1331 &session_list
->head
, list
) {
1332 if (!session_get(session
)) {
1336 session_lock(session
);
1337 if (session
->destroyed
) {
1338 goto unlock_session
;
1340 (void) cmd_stop_trace(session
);
1341 (void) cmd_destroy_session(
1342 session
, the_notification_thread_handle
, NULL
);
1344 session_unlock(session
);
1345 session_put(session
);
1347 session_unlock_list();
1349 /* Wait for the destruction of all sessions to complete. */
1350 DBG("Waiting for the destruction of all sessions to complete");
1351 session_list_wait_empty();
1352 DBG("Destruction of all sessions completed");
1355 static void unregister_all_triggers(void)
1357 enum lttng_error_code ret_code
;
1358 enum lttng_trigger_status trigger_status
;
1359 struct lttng_triggers
*triggers
= NULL
;
1360 unsigned int trigger_count
, i
;
1361 const struct lttng_credentials creds
= {
1362 .uid
= LTTNG_OPTIONAL_INIT_VALUE(0),
1365 DBG("Unregistering all triggers");
1368 * List all triggers as "root" since we wish to unregister all triggers.
1370 ret_code
= notification_thread_command_list_triggers(
1371 the_notification_thread_handle
, creds
.uid
.value
,
1373 if (ret_code
!= LTTNG_OK
) {
1374 ERR("Failed to list triggers while unregistering all triggers");
1378 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
1379 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1381 for (i
= 0; i
< trigger_count
; i
++) {
1382 uid_t trigger_owner
;
1383 const char *trigger_name
;
1384 const struct lttng_trigger
*trigger
=
1385 lttng_triggers_get_at_index(triggers
, i
);
1387 LTTNG_ASSERT(trigger
);
1389 trigger_status
= lttng_trigger_get_owner_uid(
1390 trigger
, &trigger_owner
);
1391 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1393 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
1394 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
1395 trigger_name
: "(anonymous)";
1397 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1398 (int) trigger_owner
, trigger_name
);
1400 ret_code
= cmd_unregister_trigger(&creds
, trigger
,
1401 the_notification_thread_handle
);
1402 if (ret_code
!= LTTNG_OK
) {
1403 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1404 (int) trigger_owner
, trigger_name
,
1405 lttng_strerror(-ret_code
));
1406 /* Continue to unregister the remaining triggers. */
1410 lttng_triggers_destroy(triggers
);
1413 static int run_as_worker_post_fork_cleanup(void *data
)
1415 struct sessiond_config
*sessiond_config
= (struct sessiond_config
*) data
;
1417 sessiond_config_fini(sessiond_config
);
1421 static int launch_run_as_worker(const char *procname
)
1424 * Clean-up before forking the run-as worker. Any dynamically
1425 * allocated memory of which the worker is not aware will
1426 * be leaked as the process forks a run-as worker (and performs
1427 * no exec*()). The same would apply to any opened fd.
1429 return run_as_create_worker(
1430 procname
, run_as_worker_post_fork_cleanup
, &the_config
);
1433 static void sessiond_uuid_log(void)
1435 char uuid_str
[LTTNG_UUID_STR_LEN
];
1437 lttng_uuid_to_str(the_sessiond_uuid
, uuid_str
);
1438 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1444 int main(int argc
, char **argv
)
1446 int ret
= 0, retval
= 0;
1447 const char *env_app_timeout
;
1448 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1449 *ust64_channel_monitor_pipe
= NULL
,
1450 *kernel_channel_monitor_pipe
= NULL
;
1451 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1452 struct timer_thread_parameters timer_thread_parameters
;
1453 /* Rotation thread handle. */
1454 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1455 /* Queue of rotation jobs populated by the sessiond-timer. */
1456 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1457 struct lttng_thread
*client_thread
= NULL
;
1458 struct lttng_thread
*notification_thread
= NULL
;
1459 struct lttng_thread
*register_apps_thread
= NULL
;
1460 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
1462 logger_set_thread_name("Main", false);
1463 init_kernel_workarounds();
1465 rcu_register_thread();
1467 if (set_signal_handler()) {
1469 goto exit_set_signal_handler
;
1472 if (timer_signal_init()) {
1474 goto exit_set_signal_handler
;
1477 the_page_size
= sysconf(_SC_PAGE_SIZE
);
1478 if (the_page_size
< 0) {
1479 PERROR("sysconf _SC_PAGE_SIZE");
1480 the_page_size
= LONG_MAX
;
1481 WARN("Fallback page size to %ld", the_page_size
);
1484 ret
= sessiond_config_init(&the_config
);
1487 goto exit_set_signal_handler
;
1491 * Init config from environment variables.
1492 * Command line option override env configuration per-doc. Do env first.
1494 sessiond_config_apply_env_config(&the_config
);
1497 * Parse arguments and load the daemon configuration file.
1499 * We have an exit_options exit path to free memory reserved by
1500 * set_options. This is needed because the rest of sessiond_cleanup()
1501 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1502 * depends on set_options.
1505 if (set_options(argc
, argv
)) {
1511 * Resolve all paths received as arguments, configuration option, or
1512 * through environment variable as absolute paths. This is necessary
1513 * since daemonizing causes the sessiond's current working directory
1516 ret
= sessiond_config_resolve_paths(&the_config
);
1522 lttng_opt_verbose
= the_config
.verbose
;
1523 lttng_opt_quiet
= the_config
.quiet
;
1524 the_kconsumer_data
.err_unix_sock_path
=
1525 the_config
.kconsumerd_err_unix_sock_path
.value
;
1526 the_kconsumer_data
.cmd_unix_sock_path
=
1527 the_config
.kconsumerd_cmd_unix_sock_path
.value
;
1528 the_ustconsumer32_data
.err_unix_sock_path
=
1529 the_config
.consumerd32_err_unix_sock_path
.value
;
1530 the_ustconsumer32_data
.cmd_unix_sock_path
=
1531 the_config
.consumerd32_cmd_unix_sock_path
.value
;
1532 the_ustconsumer64_data
.err_unix_sock_path
=
1533 the_config
.consumerd64_err_unix_sock_path
.value
;
1534 the_ustconsumer64_data
.cmd_unix_sock_path
=
1535 the_config
.consumerd64_cmd_unix_sock_path
.value
;
1536 set_clock_plugin_env();
1538 sessiond_config_log(&the_config
);
1539 sessiond_uuid_log();
1541 if (opt_print_version
) {
1547 if (create_lttng_rundir()) {
1552 /* Abort launch if a session daemon is already running. */
1553 if (check_existing_daemon()) {
1554 ERR("A session daemon is already running.");
1560 if (the_config
.daemonize
|| the_config
.background
) {
1563 ret
= lttng_daemonize(&the_child_ppid
, &recv_child_signal
,
1564 !the_config
.background
);
1571 * We are in the child. Make sure all other file descriptors are
1572 * closed, in case we are called with more opened file
1573 * descriptors than the standard ones and the lock file.
1575 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1576 if (i
== lockfile_fd
) {
1583 if (launch_run_as_worker(argv
[0]) < 0) {
1584 goto exit_create_run_as_worker_cleanup
;
1588 * Starting from here, we can create threads. This needs to be after
1589 * lttng_daemonize due to RCU.
1593 * Initialize the health check subsystem. This call should set the
1594 * appropriate time values.
1596 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1597 if (!the_health_sessiond
) {
1598 PERROR("health_app_create error");
1603 /* Create thread to clean up RCU hash tables */
1604 ht_cleanup_thread
= launch_ht_cleanup_thread();
1605 if (!ht_cleanup_thread
) {
1610 /* Create thread quit pipe */
1611 if (sessiond_init_thread_quit_pipe()) {
1616 /* Check if daemon is UID = 0 */
1617 is_root
= !getuid();
1619 /* Create global run dir with root access */
1621 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1622 if (!kernel_channel_monitor_pipe
) {
1623 ERR("Failed to create kernel consumer channel monitor pipe");
1627 the_kconsumer_data
.channel_monitor_pipe
=
1628 lttng_pipe_release_writefd(
1629 kernel_channel_monitor_pipe
);
1630 if (the_kconsumer_data
.channel_monitor_pipe
< 0) {
1636 /* Set consumer initial state */
1637 the_kernel_consumerd_state
= CONSUMER_STOPPED
;
1638 the_ust_consumerd_state
= CONSUMER_STOPPED
;
1640 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1641 if (!ust32_channel_monitor_pipe
) {
1642 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1646 the_ustconsumer32_data
.channel_monitor_pipe
=
1647 lttng_pipe_release_writefd(ust32_channel_monitor_pipe
);
1648 if (the_ustconsumer32_data
.channel_monitor_pipe
< 0) {
1654 * The rotation_thread_timer_queue structure is shared between the
1655 * sessiond timer thread and the rotation thread. The main thread keeps
1656 * its ownership and destroys it when both threads have been joined.
1658 rotation_timer_queue
= rotation_thread_timer_queue_create();
1659 if (!rotation_timer_queue
) {
1663 timer_thread_parameters
.rotation_thread_job_queue
=
1664 rotation_timer_queue
;
1666 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1667 if (!ust64_channel_monitor_pipe
) {
1668 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1672 the_ustconsumer64_data
.channel_monitor_pipe
=
1673 lttng_pipe_release_writefd(ust64_channel_monitor_pipe
);
1674 if (the_ustconsumer64_data
.channel_monitor_pipe
< 0) {
1680 * Init UST app hash table. Alloc hash table before this point since
1681 * cleanup() can get called after that point.
1683 if (ust_app_ht_alloc()) {
1684 ERR("Failed to allocate UST app hash table");
1689 event_notifier_error_accounting_status
= event_notifier_error_accounting_init(
1690 the_config
.event_notifier_buffer_size_kernel
,
1691 the_config
.event_notifier_buffer_size_userspace
);
1692 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1693 ERR("Failed to initialize event notifier error accounting system");
1699 * Initialize agent app hash table. We allocate the hash table here
1700 * since cleanup() can get called after this point.
1702 if (agent_app_ht_alloc()) {
1703 ERR("Failed to allocate Agent app hash table");
1708 if (agent_by_event_notifier_domain_ht_create()) {
1709 ERR("Failed to allocate per-event notifier domain agent hash table");
1714 * These actions must be executed as root. We do that *after* setting up
1715 * the sockets path because we MUST make the check for another daemon using
1716 * those paths *before* trying to set the kernel consumer sockets and init
1720 if (set_consumer_sockets(&the_kconsumer_data
)) {
1725 /* Setup kernel tracer */
1726 if (!the_config
.no_kernel
) {
1727 init_kernel_tracer();
1730 /* Set ulimit for open files */
1733 /* init lttng_fd tracking must be done after set_ulimit. */
1736 if (set_consumer_sockets(&the_ustconsumer64_data
)) {
1741 if (set_consumer_sockets(&the_ustconsumer32_data
)) {
1746 /* Get parent pid if -S, --sig-parent is specified. */
1747 if (the_config
.sig_parent
) {
1748 the_ppid
= getppid();
1751 /* Setup the kernel pipe for waking up the kernel thread */
1752 if (is_root
&& !the_config
.no_kernel
) {
1753 if (utils_create_pipe_cloexec(the_kernel_poll_pipe
)) {
1759 /* Setup the thread apps communication pipe. */
1760 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1765 /* Setup the thread apps notify communication pipe. */
1766 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1771 /* Initialize global buffer per UID and PID registry. */
1772 buffer_reg_init_uid_registry();
1773 buffer_reg_init_pid_registry();
1775 /* Init UST command queue. */
1776 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1780 /* Check for the application socket timeout env variable. */
1781 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1782 if (env_app_timeout
) {
1783 the_config
.app_socket_timeout
= atoi(env_app_timeout
);
1785 the_config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1788 ret
= write_pidfile();
1790 ERR("Error in write_pidfile");
1795 /* Initialize communication library */
1797 /* Initialize TCP timeout values */
1798 lttcomm_inet_init();
1800 /* Create health-check thread. */
1801 if (!launch_health_management_thread()) {
1806 /* notification_thread_data acquires the pipes' read side. */
1807 the_notification_thread_handle
= notification_thread_handle_create(
1808 ust32_channel_monitor_pipe
, ust64_channel_monitor_pipe
,
1809 kernel_channel_monitor_pipe
);
1810 if (!the_notification_thread_handle
) {
1812 ERR("Failed to create notification thread shared data");
1816 /* Create notification thread. */
1817 notification_thread
= launch_notification_thread(
1818 the_notification_thread_handle
);
1819 if (!notification_thread
) {
1824 /* Create timer thread. */
1825 if (!launch_timer_thread(&timer_thread_parameters
)) {
1830 /* rotation_thread_data acquires the pipes' read side. */
1831 rotation_thread_handle
= rotation_thread_handle_create(
1832 rotation_timer_queue
, the_notification_thread_handle
);
1833 if (!rotation_thread_handle
) {
1835 ERR("Failed to create rotation thread shared data");
1840 /* Create rotation thread. */
1841 if (!launch_rotation_thread(rotation_thread_handle
)) {
1846 /* Create thread to manage the client socket */
1847 client_thread
= launch_client_thread();
1848 if (!client_thread
) {
1853 /* Set credentials of the client socket and rundir */
1854 if (is_root
&& set_permissions(the_config
.rundir
.value
)) {
1859 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1860 apps_cmd_notify_pipe
[1])) {
1865 /* Create thread to manage application registration. */
1866 register_apps_thread
= launch_application_registration_thread(
1868 if (!register_apps_thread
) {
1873 /* Create thread to manage application socket */
1874 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1879 /* Create thread to manage application notify socket */
1880 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1885 /* Create agent management thread. */
1886 if (!launch_agent_management_thread()) {
1891 /* Don't start this thread if kernel tracing is not requested nor root */
1892 if (is_root
&& !the_config
.no_kernel
) {
1893 /* Create kernel thread to manage kernel event */
1894 if (!launch_kernel_management_thread(the_kernel_poll_pipe
[0])) {
1899 if (kernel_get_notification_fd() >= 0) {
1900 ret
= notification_thread_command_add_tracer_event_source(
1901 the_notification_thread_handle
,
1902 kernel_get_notification_fd(),
1903 LTTNG_DOMAIN_KERNEL
);
1904 if (ret
!= LTTNG_OK
) {
1905 ERR("Failed to add kernel trigger event source to notification thread");
1912 /* Load sessions. */
1913 ret
= config_load_session(
1914 the_config
.load_session_path
.value
, NULL
, 1, 1, NULL
);
1916 ERR("Session load failed: %s", error_get_str(ret
));
1921 /* Initialization completed. */
1922 sessiond_signal_parents();
1925 * This is where we start awaiting program completion (e.g. through
1926 * signal that asks threads to teardown).
1929 /* Initiate teardown once activity occurs on the quit pipe. */
1930 sessiond_wait_for_quit_pipe(-1);
1935 * Ensure that the client thread is no longer accepting new commands,
1936 * which could cause new sessions to be created.
1938 if (client_thread
) {
1939 lttng_thread_shutdown(client_thread
);
1940 lttng_thread_put(client_thread
);
1943 destroy_all_sessions_and_wait();
1946 * At this point no new trigger can be registered (no sessions are
1947 * running/rotating) and clients can't connect to the session daemon
1948 * anymore. Unregister all triggers.
1950 unregister_all_triggers();
1952 if (register_apps_thread
) {
1953 lttng_thread_shutdown(register_apps_thread
);
1954 lttng_thread_put(register_apps_thread
);
1956 lttng_thread_list_shutdown_orphans();
1959 * Wait for all pending call_rcu work to complete before tearing
1960 * down data structures. call_rcu worker may be trying to
1961 * perform lookups in those structures.
1965 * sessiond_cleanup() is called when no other thread is running, except
1966 * the ht_cleanup thread, which is needed to destroy the hash tables.
1968 rcu_thread_online();
1972 * Wait for all pending call_rcu work to complete before shutting down
1973 * the notification thread. This call_rcu work includes shutting down
1974 * UST apps and event notifier pipes.
1978 if (notification_thread
) {
1979 lttng_thread_shutdown(notification_thread
);
1980 lttng_thread_put(notification_thread
);
1984 * Error accounting teardown has to be done after the teardown of all
1985 * event notifier pipes to ensure that no tracer may try to use the
1986 * error accounting facilities.
1988 event_notifier_error_accounting_fini();
1991 * Unloading the kernel modules needs to be done after all kernel
1992 * ressources have been released. In our case, this includes the
1993 * notification fd, the event notifier group fd, error accounting fd,
1994 * all event and event notifier fds, etc.
1996 * In short, at this point, we need to have called close() on all fds
1997 * received from the kernel tracer.
1999 if (is_root
&& !the_config
.no_kernel
) {
2000 DBG("Unloading kernel modules");
2001 modprobe_remove_lttng_all();
2005 * Ensure all prior call_rcu are done. call_rcu callbacks may push
2006 * hash tables to the ht_cleanup thread. Therefore, we ensure that
2007 * the queue is empty before shutting down the clean-up thread.
2011 if (ht_cleanup_thread
) {
2012 lttng_thread_shutdown(ht_cleanup_thread
);
2013 lttng_thread_put(ht_cleanup_thread
);
2016 rcu_thread_offline();
2017 rcu_unregister_thread();
2019 if (rotation_thread_handle
) {
2020 rotation_thread_handle_destroy(rotation_thread_handle
);
2024 * After the rotation and timer thread have quit, we can safely destroy
2025 * the rotation_timer_queue.
2027 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
2029 * The teardown of the notification system is performed after the
2030 * session daemon's teardown in order to allow it to be notified
2031 * of the active session and channels at the moment of the teardown.
2033 if (the_notification_thread_handle
) {
2034 notification_thread_handle_destroy(
2035 the_notification_thread_handle
);
2037 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
2038 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
2039 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
2041 if (the_health_sessiond
) {
2042 health_app_destroy(the_health_sessiond
);
2044 exit_create_run_as_worker_cleanup
:
2046 sessiond_cleanup_lock_file();
2047 sessiond_cleanup_options();
2049 exit_set_signal_handler
: