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/ini-config/ini-config.h>
43 #include <common/dynamic-buffer.h>
44 #include <lttng/event-internal.h>
46 #include "lttng-sessiond.h"
47 #include "buffer-registry.h"
53 #include "event-notifier-error-accounting.h"
55 #include "kernel-consumer.h"
56 #include "lttng-ust-ctl.h"
57 #include "ust-consumer.h"
60 #include "health-sessiond.h"
61 #include "testpoint.h"
62 #include "notify-apps.h"
63 #include "agent-thread.h"
65 #include "notification-thread.h"
66 #include "notification-thread-commands.h"
67 #include "rotation-thread.h"
69 #include "ht-cleanup.h"
70 #include "sessiond-config.h"
76 #include "manage-apps.h"
77 #include "manage-kernel.h"
79 #include "ust-sigbus.h"
81 static const char *help_msg
=
82 #ifdef LTTNG_EMBED_HELP
83 #include <lttng-sessiond.8.h>
89 #define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
90 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR \
91 "event-notifier-error-buffer-size"
92 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR \
93 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-kernel"
94 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR \
95 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-userspace"
99 static int lockfile_fd
= -1;
100 static int opt_print_version
;
102 /* Set to 1 when a SIGUSR1 signal is received. */
103 static int recv_child_signal
;
105 /* Command line options */
106 static const struct option long_options
[] = {
107 { "client-sock", required_argument
, 0, 'c' },
108 { "apps-sock", required_argument
, 0, 'a' },
109 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
110 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
111 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
112 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
113 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
114 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
115 { "consumerd32-path", required_argument
, 0, '\0' },
116 { "consumerd32-libdir", required_argument
, 0, '\0' },
117 { "consumerd64-path", required_argument
, 0, '\0' },
118 { "consumerd64-libdir", required_argument
, 0, '\0' },
119 { "daemonize", no_argument
, 0, 'd' },
120 { "background", no_argument
, 0, 'b' },
121 { "sig-parent", no_argument
, 0, 'S' },
122 { "help", no_argument
, 0, 'h' },
123 { "group", required_argument
, 0, 'g' },
124 { "version", no_argument
, 0, 'V' },
125 { "quiet", no_argument
, 0, 'q' },
126 { "verbose", no_argument
, 0, 'v' },
127 { "verbose-consumer", no_argument
, 0, '\0' },
128 { "no-kernel", no_argument
, 0, '\0' },
129 { "pidfile", required_argument
, 0, 'p' },
130 { "agent-tcp-port", required_argument
, 0, '\0' },
131 { "config", required_argument
, 0, 'f' },
132 { "load", required_argument
, 0, 'l' },
133 { "kmod-probes", required_argument
, 0, '\0' },
134 { "extra-kmod-probes", required_argument
, 0, '\0' },
135 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, required_argument
, 0, '\0' },
136 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, required_argument
, 0, '\0' },
140 /* Command line options to ignore from configuration file */
141 static const char *config_ignore_options
[] = { "help", "version", "config" };
144 * This pipe is used to inform the thread managing application communication
145 * that a command is queued and ready to be processed.
147 static int apps_cmd_pipe
[2] = { -1, -1 };
148 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
151 * UST registration command queue. This queue is tied with a futex and uses a N
152 * wakers / 1 waiter implemented and detailed in futex.c/.h
154 * The thread_registration_apps and thread_dispatch_ust_registration uses this
155 * queue along with the wait/wake scheme. The thread_manage_apps receives down
156 * the line new application socket and monitors it for any I/O error or clean
157 * close that triggers an unregistration of the application.
159 static struct ust_cmd_queue ust_cmd_queue
;
162 * Section name to look for in the daemon configuration file.
164 static const char * const config_section_name
= "sessiond";
166 /* Am I root or not. Set to 1 if the daemon is running as root */
170 * Stop all threads by closing the thread quit pipe.
172 static void stop_threads(void)
176 /* Stopping all threads */
177 DBG("Terminating all threads");
178 ret
= sessiond_notify_quit_pipe();
180 ERR("write error on thread quit pipe");
185 * Close every consumer sockets.
187 static void close_consumer_sockets(void)
191 if (the_kconsumer_data
.err_sock
>= 0) {
192 ret
= close(the_kconsumer_data
.err_sock
);
194 PERROR("kernel consumer err_sock close");
197 if (the_ustconsumer32_data
.err_sock
>= 0) {
198 ret
= close(the_ustconsumer32_data
.err_sock
);
200 PERROR("UST consumerd32 err_sock close");
203 if (the_ustconsumer64_data
.err_sock
>= 0) {
204 ret
= close(the_ustconsumer64_data
.err_sock
);
206 PERROR("UST consumerd64 err_sock close");
209 if (the_kconsumer_data
.cmd_sock
>= 0) {
210 ret
= close(the_kconsumer_data
.cmd_sock
);
212 PERROR("kernel consumer cmd_sock close");
215 if (the_ustconsumer32_data
.cmd_sock
>= 0) {
216 ret
= close(the_ustconsumer32_data
.cmd_sock
);
218 PERROR("UST consumerd32 cmd_sock close");
221 if (the_ustconsumer64_data
.cmd_sock
>= 0) {
222 ret
= close(the_ustconsumer64_data
.cmd_sock
);
224 PERROR("UST consumerd64 cmd_sock close");
227 if (the_kconsumer_data
.channel_monitor_pipe
>= 0) {
228 ret
= close(the_kconsumer_data
.channel_monitor_pipe
);
230 PERROR("kernel consumer channel monitor pipe close");
233 if (the_ustconsumer32_data
.channel_monitor_pipe
>= 0) {
234 ret
= close(the_ustconsumer32_data
.channel_monitor_pipe
);
236 PERROR("UST consumerd32 channel monitor pipe close");
239 if (the_ustconsumer64_data
.channel_monitor_pipe
>= 0) {
240 ret
= close(the_ustconsumer64_data
.channel_monitor_pipe
);
242 PERROR("UST consumerd64 channel monitor pipe close");
248 * Wait on consumer process termination.
250 * Need to be called with the consumer data lock held or from a context
251 * ensuring no concurrent access to data (e.g: cleanup).
253 static void wait_consumer(struct consumer_data
*consumer_data
)
258 if (consumer_data
->pid
<= 0) {
262 DBG("Waiting for complete teardown of consumerd (PID: %d)",
264 ret
= waitpid(consumer_data
->pid
, &status
, 0);
266 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
267 } else if (!WIFEXITED(status
)) {
268 ERR("consumerd termination with error: %d",
271 consumer_data
->pid
= 0;
275 * Cleanup the session daemon's data structures.
277 static void sessiond_cleanup(void)
280 struct ltt_session_list
*session_list
= session_get_list();
282 DBG("Cleanup sessiond");
285 * Close the thread quit pipe. It has already done its job,
286 * since we are now called.
288 sessiond_close_quit_pipe();
289 utils_close_pipe(apps_cmd_pipe
);
290 utils_close_pipe(apps_cmd_notify_pipe
);
291 utils_close_pipe(the_kernel_poll_pipe
);
293 ret
= remove(the_config
.pid_file_path
.value
);
295 PERROR("remove pidfile %s", the_config
.pid_file_path
.value
);
298 DBG("Removing sessiond and consumerd content of directory %s",
299 the_config
.rundir
.value
);
302 DBG("Removing %s", the_config
.pid_file_path
.value
);
303 (void) unlink(the_config
.pid_file_path
.value
);
305 DBG("Removing %s", the_config
.agent_port_file_path
.value
);
306 (void) unlink(the_config
.agent_port_file_path
.value
);
309 DBG("Removing %s", the_kconsumer_data
.err_unix_sock_path
);
310 (void) unlink(the_kconsumer_data
.err_unix_sock_path
);
312 DBG("Removing directory %s", the_config
.kconsumerd_path
.value
);
313 (void) rmdir(the_config
.kconsumerd_path
.value
);
315 /* ust consumerd 32 */
316 DBG("Removing %s", the_config
.consumerd32_err_unix_sock_path
.value
);
317 (void) unlink(the_config
.consumerd32_err_unix_sock_path
.value
);
319 DBG("Removing directory %s", the_config
.consumerd32_path
.value
);
320 (void) rmdir(the_config
.consumerd32_path
.value
);
322 /* ust consumerd 64 */
323 DBG("Removing %s", the_config
.consumerd64_err_unix_sock_path
.value
);
324 (void) unlink(the_config
.consumerd64_err_unix_sock_path
.value
);
326 DBG("Removing directory %s", the_config
.consumerd64_path
.value
);
327 (void) rmdir(the_config
.consumerd64_path
.value
);
329 pthread_mutex_destroy(&session_list
->lock
);
331 DBG("Cleaning up all per-event notifier domain agents");
332 agent_by_event_notifier_domain_ht_destroy();
334 DBG("Cleaning up all agent apps");
335 agent_app_ht_clean();
336 DBG("Closing all UST sockets");
337 ust_app_clean_list();
338 buffer_reg_destroy_registries();
340 close_consumer_sockets();
342 wait_consumer(&the_kconsumer_data
);
343 wait_consumer(&the_ustconsumer64_data
);
344 wait_consumer(&the_ustconsumer32_data
);
346 if (is_root
&& !the_config
.no_kernel
) {
347 cleanup_kernel_tracer();
351 * We do NOT rmdir rundir because there are other processes
352 * using it, for instance lttng-relayd, which can start in
353 * parallel with this teardown.
358 * Cleanup the daemon's option data structures.
360 static void sessiond_cleanup_options(void)
362 DBG("Cleaning up options");
364 sessiond_config_fini(&the_config
);
366 run_as_destroy_worker();
369 static int string_match(const char *str1
, const char *str2
)
371 return (str1
&& str2
) && !strcmp(str1
, str2
);
375 * Take an option from the getopt output and set it in the right variable to be
378 * Return 0 on success else a negative value.
380 static int set_option(int opt
, const char *arg
, const char *optname
)
384 if (string_match(optname
, "client-sock") || opt
== 'c') {
385 if (!arg
|| *arg
== '\0') {
389 if (lttng_is_setuid_setgid()) {
390 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
391 "-c, --client-sock");
393 config_string_set(&the_config
.client_unix_sock_path
,
395 if (!the_config
.client_unix_sock_path
.value
) {
400 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
401 if (!arg
|| *arg
== '\0') {
405 if (lttng_is_setuid_setgid()) {
406 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
409 config_string_set(&the_config
.apps_unix_sock_path
,
411 if (!the_config
.apps_unix_sock_path
.value
) {
416 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
417 the_config
.daemonize
= true;
418 } else if (string_match(optname
, "background") || opt
== 'b') {
419 the_config
.background
= true;
420 } else if (string_match(optname
, "group") || opt
== 'g') {
421 if (!arg
|| *arg
== '\0') {
425 if (lttng_is_setuid_setgid()) {
426 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
429 config_string_set(&the_config
.tracing_group_name
,
431 if (!the_config
.tracing_group_name
.value
) {
436 } else if (string_match(optname
, "help") || opt
== 'h') {
437 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
439 ERR("Cannot show --help for `lttng-sessiond`");
442 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
443 } else if (string_match(optname
, "version") || opt
== 'V') {
444 opt_print_version
= 1;
445 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
446 the_config
.sig_parent
= true;
447 } else if (string_match(optname
, "kconsumerd-err-sock")) {
448 if (!arg
|| *arg
== '\0') {
452 if (lttng_is_setuid_setgid()) {
453 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
454 "--kconsumerd-err-sock");
457 &the_config
.kconsumerd_err_unix_sock_path
,
459 if (!the_config
.kconsumerd_err_unix_sock_path
.value
) {
464 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
465 if (!arg
|| *arg
== '\0') {
469 if (lttng_is_setuid_setgid()) {
470 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
471 "--kconsumerd-cmd-sock");
474 &the_config
.kconsumerd_cmd_unix_sock_path
,
476 if (!the_config
.kconsumerd_cmd_unix_sock_path
.value
) {
481 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
482 if (!arg
|| *arg
== '\0') {
486 if (lttng_is_setuid_setgid()) {
487 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
488 "--ustconsumerd64-err-sock");
491 &the_config
.consumerd64_err_unix_sock_path
,
493 if (!the_config
.consumerd64_err_unix_sock_path
.value
) {
498 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
499 if (!arg
|| *arg
== '\0') {
503 if (lttng_is_setuid_setgid()) {
504 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
505 "--ustconsumerd64-cmd-sock");
508 &the_config
.consumerd64_cmd_unix_sock_path
,
510 if (!the_config
.consumerd64_cmd_unix_sock_path
.value
) {
515 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
516 if (!arg
|| *arg
== '\0') {
520 if (lttng_is_setuid_setgid()) {
521 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
522 "--ustconsumerd32-err-sock");
525 &the_config
.consumerd32_err_unix_sock_path
,
527 if (!the_config
.consumerd32_err_unix_sock_path
.value
) {
532 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
533 if (!arg
|| *arg
== '\0') {
537 if (lttng_is_setuid_setgid()) {
538 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
539 "--ustconsumerd32-cmd-sock");
542 &the_config
.consumerd32_cmd_unix_sock_path
,
544 if (!the_config
.consumerd32_cmd_unix_sock_path
.value
) {
549 } else if (string_match(optname
, "no-kernel")) {
550 the_config
.no_kernel
= true;
551 } else if (string_match(optname
, "quiet") || opt
== 'q') {
552 the_config
.quiet
= true;
553 } else if (string_match(optname
, "verbose") || opt
== 'v') {
554 /* Verbose level can increase using multiple -v */
556 /* Value obtained from config file */
557 the_config
.verbose
= config_parse_value(arg
);
559 /* -v used on command line */
560 the_config
.verbose
++;
562 /* Clamp value to [0, 3] */
563 the_config
.verbose
= the_config
.verbose
< 0 ?
565 (the_config
.verbose
<= 3 ? the_config
.verbose
:
567 } else if (string_match(optname
, "verbose-consumer")) {
569 the_config
.verbose_consumer
= config_parse_value(arg
);
571 the_config
.verbose_consumer
++;
573 } else if (string_match(optname
, "consumerd32-path")) {
574 if (!arg
|| *arg
== '\0') {
578 if (lttng_is_setuid_setgid()) {
579 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
580 "--consumerd32-path");
582 config_string_set(&the_config
.consumerd32_bin_path
,
584 if (!the_config
.consumerd32_bin_path
.value
) {
589 } else if (string_match(optname
, "consumerd32-libdir")) {
590 if (!arg
|| *arg
== '\0') {
594 if (lttng_is_setuid_setgid()) {
595 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
596 "--consumerd32-libdir");
598 config_string_set(&the_config
.consumerd32_lib_dir
,
600 if (!the_config
.consumerd32_lib_dir
.value
) {
605 } else if (string_match(optname
, "consumerd64-path")) {
606 if (!arg
|| *arg
== '\0') {
610 if (lttng_is_setuid_setgid()) {
611 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
612 "--consumerd64-path");
614 config_string_set(&the_config
.consumerd64_bin_path
,
616 if (!the_config
.consumerd64_bin_path
.value
) {
621 } else if (string_match(optname
, "consumerd64-libdir")) {
622 if (!arg
|| *arg
== '\0') {
626 if (lttng_is_setuid_setgid()) {
627 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
628 "--consumerd64-libdir");
630 config_string_set(&the_config
.consumerd64_lib_dir
,
632 if (!the_config
.consumerd64_lib_dir
.value
) {
637 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
638 if (!arg
|| *arg
== '\0') {
642 if (lttng_is_setuid_setgid()) {
643 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
647 &the_config
.pid_file_path
, strdup(arg
));
648 if (!the_config
.pid_file_path
.value
) {
653 } else if (string_match(optname
, "agent-tcp-port")) {
654 if (!arg
|| *arg
== '\0') {
658 if (lttng_is_setuid_setgid()) {
659 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
665 v
= strtoul(arg
, NULL
, 0);
666 if (errno
!= 0 || !isdigit(arg
[0])) {
667 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
670 if (v
== 0 || v
>= 65535) {
671 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
674 the_config
.agent_tcp_port
.begin
=
675 the_config
.agent_tcp_port
.end
= (int) v
;
676 DBG3("Agent TCP port set to non default: %i", (int) v
);
678 } else if (string_match(optname
, "load") || opt
== 'l') {
679 if (!arg
|| *arg
== '\0') {
683 if (lttng_is_setuid_setgid()) {
684 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
687 config_string_set(&the_config
.load_session_path
,
689 if (!the_config
.load_session_path
.value
) {
694 } else if (string_match(optname
, "kmod-probes")) {
695 if (!arg
|| *arg
== '\0') {
699 if (lttng_is_setuid_setgid()) {
700 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
703 config_string_set(&the_config
.kmod_probes_list
,
705 if (!the_config
.kmod_probes_list
.value
) {
710 } else if (string_match(optname
, "extra-kmod-probes")) {
711 if (!arg
|| *arg
== '\0') {
715 if (lttng_is_setuid_setgid()) {
716 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
717 "--extra-kmod-probes");
719 config_string_set(&the_config
.kmod_extra_probes_list
,
721 if (!the_config
.kmod_extra_probes_list
.value
) {
726 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
)) {
730 v
= strtoul(arg
, NULL
, 0);
731 if (errno
!= 0 || !isdigit(arg
[0])) {
732 ERR("Wrong value in --%s parameter: %s",
733 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
736 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
737 ERR("Value out of range for --%s parameter: %s",
738 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
741 the_config
.event_notifier_buffer_size_kernel
= (int) v
;
742 DBG3("Number of event notifier error buffer kernel size to non default: %i",
743 the_config
.event_notifier_buffer_size_kernel
);
745 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
)) {
749 v
= strtoul(arg
, NULL
, 0);
750 if (errno
!= 0 || !isdigit(arg
[0])) {
751 ERR("Wrong value in --%s parameter: %s",
752 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
755 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
756 ERR("Value out of range for --%s parameter: %s",
757 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
760 the_config
.event_notifier_buffer_size_userspace
= (int) v
;
761 DBG3("Number of event notifier error buffer userspace size to non default: %i",
762 the_config
.event_notifier_buffer_size_userspace
);
764 } else if (string_match(optname
, "config") || opt
== 'f') {
765 /* This is handled in set_options() thus silent skip. */
768 /* Unknown option or other error.
769 * Error is printed by getopt, just return */
774 if (ret
== -EINVAL
) {
775 const char *opt_name
= "unknown";
778 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
780 if (opt
== long_options
[i
].val
) {
781 opt_name
= long_options
[i
].name
;
786 WARN("Invalid argument provided for option \"%s\", using default value.",
794 * config_entry_handler_cb used to handle options read from a config file.
795 * See config_entry_handler_cb comment in common/config/session-config.h for the
796 * return value conventions.
798 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
802 if (!entry
|| !entry
->name
|| !entry
->value
) {
807 /* Check if the option is to be ignored */
808 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
809 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
814 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
817 /* Ignore if not fully matched. */
818 if (strcmp(entry
->name
, long_options
[i
].name
)) {
823 * If the option takes no argument on the command line, we have to
824 * check if the value is "true". We support non-zero numeric values,
827 if (!long_options
[i
].has_arg
) {
828 ret
= config_parse_value(entry
->value
);
831 WARN("Invalid configuration value \"%s\" for option %s",
832 entry
->value
, entry
->name
);
834 /* False, skip boolean config option. */
839 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
843 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
849 static void print_version(void) {
850 fprintf(stdout
, "%s\n", VERSION
);
854 * daemon configuration loading and argument parsing
856 static int set_options(int argc
, char **argv
)
858 int ret
= 0, c
= 0, option_index
= 0;
859 int orig_optopt
= optopt
, orig_optind
= optind
;
861 char *config_path
= NULL
;
863 optstring
= utils_generate_optstring(long_options
,
864 sizeof(long_options
) / sizeof(struct option
));
870 /* Check for the --config option */
871 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
872 &option_index
)) != -1) {
876 } else if (c
!= 'f') {
877 /* if not equal to --config option. */
881 if (lttng_is_setuid_setgid()) {
882 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
886 config_path
= utils_expand_path(optarg
);
888 ERR("Failed to resolve path: %s", optarg
);
893 ret
= config_get_section_entries(config_path
, config_section_name
,
894 config_entry_handler
, NULL
);
897 ERR("Invalid configuration option at line %i", ret
);
903 /* Reset getopt's global state */
904 optopt
= orig_optopt
;
905 optind
= orig_optind
;
909 * getopt_long() will not set option_index if it encounters a
912 c
= getopt_long(argc
, argv
, optstring
, long_options
,
919 * Pass NULL as the long option name if popt left the index
922 ret
= set_option(c
, optarg
,
923 option_index
< 0 ? NULL
:
924 long_options
[option_index
].name
);
937 * Create lockfile using the rundir and return its fd.
939 static int create_lockfile(void)
941 return utils_create_lock_file(the_config
.lock_file_path
.value
);
945 * Check if the global socket is available, and if a daemon is answering at the
946 * other side. If yes, error is returned.
948 * Also attempts to create and hold the lock file.
950 static int check_existing_daemon(void)
954 /* Is there anybody out there ? */
955 if (lttng_session_daemon_alive()) {
960 lockfile_fd
= create_lockfile();
961 if (lockfile_fd
< 0) {
969 static void sessiond_cleanup_lock_file(void)
974 * Cleanup lock file by deleting it and finaly closing it which will
975 * release the file system lock.
977 if (lockfile_fd
>= 0) {
978 ret
= remove(the_config
.lock_file_path
.value
);
980 PERROR("remove lock file");
982 ret
= close(lockfile_fd
);
984 PERROR("close lock file");
990 * Set the tracing group gid onto the client socket.
992 * Race window between mkdir and chown is OK because we are going from more
993 * permissive (root.root) to less permissive (root.tracing).
995 static int set_permissions(char *rundir
)
1000 ret
= utils_get_group_id(
1001 the_config
.tracing_group_name
.value
, true, &gid
);
1003 /* Default to root group. */
1007 /* Set lttng run dir */
1008 ret
= chown(rundir
, 0, gid
);
1010 ERR("Unable to set group on %s", rundir
);
1015 * Ensure all applications and tracing group can search the run
1016 * dir. Allow everyone to read the directory, since it does not
1017 * buy us anything to hide its content.
1019 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1021 ERR("Unable to set permissions on %s", rundir
);
1025 /* lttng client socket path */
1026 ret
= chown(the_config
.client_unix_sock_path
.value
, 0, gid
);
1028 ERR("Unable to set group on %s",
1029 the_config
.client_unix_sock_path
.value
);
1033 /* kconsumer error socket path */
1034 ret
= chown(the_kconsumer_data
.err_unix_sock_path
, 0, 0);
1036 ERR("Unable to set group on %s",
1037 the_kconsumer_data
.err_unix_sock_path
);
1041 /* 64-bit ustconsumer error socket path */
1042 ret
= chown(the_ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1044 ERR("Unable to set group on %s",
1045 the_ustconsumer64_data
.err_unix_sock_path
);
1049 /* 32-bit ustconsumer compat32 error socket path */
1050 ret
= chown(the_ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1052 ERR("Unable to set group on %s",
1053 the_ustconsumer32_data
.err_unix_sock_path
);
1057 DBG("All permissions are set");
1063 * Create the lttng run directory needed for all global sockets and pipe.
1065 static int create_lttng_rundir(void)
1069 DBG3("Creating LTTng run directory: %s", the_config
.rundir
.value
);
1071 ret
= mkdir(the_config
.rundir
.value
, S_IRWXU
);
1073 if (errno
!= EEXIST
) {
1074 ERR("Unable to create %s", the_config
.rundir
.value
);
1086 * Setup sockets and directory needed by the consumerds' communication with the
1089 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1094 switch (consumer_data
->type
) {
1095 case LTTNG_CONSUMER_KERNEL
:
1096 path
= the_config
.kconsumerd_path
.value
;
1098 case LTTNG_CONSUMER64_UST
:
1099 path
= the_config
.consumerd64_path
.value
;
1101 case LTTNG_CONSUMER32_UST
:
1102 path
= the_config
.consumerd32_path
.value
;
1105 ERR("Consumer type unknown");
1111 DBG2("Creating consumer directory: %s", path
);
1113 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1114 if (ret
< 0 && errno
!= EEXIST
) {
1116 ERR("Failed to create %s", path
);
1122 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
,
1125 /* Default to root group. */
1129 ret
= chown(path
, 0, gid
);
1131 ERR("Unable to set group on %s", path
);
1137 /* Create the consumerd error unix socket */
1138 consumer_data
->err_sock
=
1139 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1140 if (consumer_data
->err_sock
< 0) {
1141 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1147 * Set the CLOEXEC flag. Return code is useless because either way, the
1150 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1152 PERROR("utils_set_fd_cloexec");
1153 /* continue anyway */
1156 /* File permission MUST be 660 */
1157 ret
= chmod(consumer_data
->err_unix_sock_path
,
1158 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1160 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1170 * Signal handler for the daemon
1172 * Simply stop all worker threads, leaving main() return gracefully after
1173 * joining all threads and calling cleanup().
1175 static void sighandler(int sig
, siginfo_t
*siginfo
, void *arg
)
1179 DBG("SIGINT caught");
1183 DBG("SIGTERM caught");
1187 CMM_STORE_SHARED(recv_child_signal
, 1);
1192 const char msg
[] = "Received SIGBUS, aborting program.\n";
1194 lttng_ust_handle_sigbus(siginfo
->si_addr
);
1196 * If ustctl did not catch this signal (triggering a
1197 * siglongjmp), abort the program. Otherwise, the execution
1198 * will resume from the ust-ctl call which caused this error.
1200 * The return value is ignored since the program aborts anyhow.
1202 write_ret
= write(STDERR_FILENO
, msg
, sizeof(msg
));
1212 * Setup signal handler for :
1213 * SIGINT, SIGTERM, SIGPIPE
1215 static int set_signal_handler(void)
1218 struct sigaction sa
;
1221 if ((ret
= sigemptyset(&sigset
)) < 0) {
1222 PERROR("sigemptyset");
1226 sa
.sa_mask
= sigset
;
1227 sa
.sa_flags
= SA_SIGINFO
;
1229 sa
.sa_sigaction
= sighandler
;
1230 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1231 PERROR("sigaction");
1235 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1236 PERROR("sigaction");
1240 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1241 PERROR("sigaction");
1245 if ((ret
= sigaction(SIGBUS
, &sa
, NULL
)) < 0) {
1246 PERROR("sigaction");
1251 sa
.sa_handler
= SIG_IGN
;
1252 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1253 PERROR("sigaction");
1257 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE, SIGINT, and SIGBUS");
1263 * Set open files limit to unlimited. This daemon can open a large number of
1264 * file descriptors in order to consume multiple kernel traces.
1266 static void set_ulimit(void)
1271 /* The kernel does not allow an infinite limit for open files */
1272 lim
.rlim_cur
= 65535;
1273 lim
.rlim_max
= 65535;
1275 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1277 PERROR("failed to set open files limit");
1281 static int write_pidfile(void)
1283 return utils_create_pid_file(getpid(), the_config
.pid_file_path
.value
);
1286 static int set_clock_plugin_env(void)
1289 char *env_value
= NULL
;
1291 if (!the_config
.lttng_ust_clock_plugin
.value
) {
1295 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1296 the_config
.lttng_ust_clock_plugin
.value
);
1302 ret
= putenv(env_value
);
1305 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1309 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1310 the_config
.lttng_ust_clock_plugin
.value
);
1315 static void destroy_all_sessions_and_wait(void)
1317 struct ltt_session
*session
, *tmp
;
1318 struct ltt_session_list
*session_list
;
1320 session_list
= session_get_list();
1321 DBG("Initiating destruction of all sessions");
1323 if (!session_list
) {
1327 session_lock_list();
1328 /* Initiate the destruction of all sessions. */
1329 cds_list_for_each_entry_safe(session
, tmp
,
1330 &session_list
->head
, list
) {
1331 if (!session_get(session
)) {
1335 session_lock(session
);
1336 if (session
->destroyed
) {
1337 goto unlock_session
;
1339 (void) cmd_stop_trace(session
);
1340 (void) cmd_destroy_session(
1341 session
, the_notification_thread_handle
, NULL
);
1343 session_unlock(session
);
1344 session_put(session
);
1346 session_unlock_list();
1348 /* Wait for the destruction of all sessions to complete. */
1349 DBG("Waiting for the destruction of all sessions to complete");
1350 session_list_wait_empty();
1351 DBG("Destruction of all sessions completed");
1354 static void unregister_all_triggers(void)
1356 enum lttng_error_code ret_code
;
1357 enum lttng_trigger_status trigger_status
;
1358 struct lttng_triggers
*triggers
= NULL
;
1359 unsigned int trigger_count
, i
;
1360 const struct lttng_credentials creds
= {
1361 .uid
= LTTNG_OPTIONAL_INIT_VALUE(0),
1364 DBG("Unregistering all triggers");
1367 * List all triggers as "root" since we wish to unregister all triggers.
1369 ret_code
= notification_thread_command_list_triggers(
1370 the_notification_thread_handle
, creds
.uid
.value
,
1372 if (ret_code
!= LTTNG_OK
) {
1373 ERR("Failed to list triggers while unregistering all triggers");
1377 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
1378 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1380 for (i
= 0; i
< trigger_count
; i
++) {
1381 uid_t trigger_owner
;
1382 const char *trigger_name
;
1383 const struct lttng_trigger
*trigger
=
1384 lttng_triggers_get_at_index(triggers
, i
);
1386 LTTNG_ASSERT(trigger
);
1388 trigger_status
= lttng_trigger_get_owner_uid(
1389 trigger
, &trigger_owner
);
1390 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1392 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
1393 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
1394 trigger_name
: "(anonymous)";
1396 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1397 (int) trigger_owner
, trigger_name
);
1399 ret_code
= cmd_unregister_trigger(&creds
, trigger
,
1400 the_notification_thread_handle
);
1401 if (ret_code
!= LTTNG_OK
) {
1402 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1403 (int) trigger_owner
, trigger_name
,
1404 lttng_strerror(-ret_code
));
1405 /* Continue to unregister the remaining triggers. */
1409 lttng_triggers_destroy(triggers
);
1412 static int run_as_worker_post_fork_cleanup(void *data
)
1414 struct sessiond_config
*sessiond_config
= (struct sessiond_config
*) data
;
1416 sessiond_config_fini(sessiond_config
);
1420 static int launch_run_as_worker(const char *procname
)
1423 * Clean-up before forking the run-as worker. Any dynamically
1424 * allocated memory of which the worker is not aware will
1425 * be leaked as the process forks a run-as worker (and performs
1426 * no exec*()). The same would apply to any opened fd.
1428 return run_as_create_worker(
1429 procname
, run_as_worker_post_fork_cleanup
, &the_config
);
1432 static void sessiond_uuid_log(void)
1434 char uuid_str
[LTTNG_UUID_STR_LEN
];
1436 lttng_uuid_to_str(the_sessiond_uuid
, uuid_str
);
1437 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1443 int main(int argc
, char **argv
)
1445 int ret
= 0, retval
= 0;
1446 const char *env_app_timeout
;
1447 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1448 *ust64_channel_monitor_pipe
= NULL
,
1449 *kernel_channel_monitor_pipe
= NULL
;
1450 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1451 struct timer_thread_parameters timer_thread_parameters
;
1452 /* Rotation thread handle. */
1453 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1454 /* Queue of rotation jobs populated by the sessiond-timer. */
1455 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1456 struct lttng_thread
*client_thread
= NULL
;
1457 struct lttng_thread
*notification_thread
= NULL
;
1458 struct lttng_thread
*register_apps_thread
= NULL
;
1459 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
1461 logger_set_thread_name("Main", false);
1462 init_kernel_workarounds();
1464 rcu_register_thread();
1466 if (set_signal_handler()) {
1468 goto exit_set_signal_handler
;
1471 if (timer_signal_init()) {
1473 goto exit_set_signal_handler
;
1476 the_page_size
= sysconf(_SC_PAGE_SIZE
);
1477 if (the_page_size
< 0) {
1478 PERROR("sysconf _SC_PAGE_SIZE");
1479 the_page_size
= LONG_MAX
;
1480 WARN("Fallback page size to %ld", the_page_size
);
1483 ret
= sessiond_config_init(&the_config
);
1486 goto exit_set_signal_handler
;
1490 * Init config from environment variables.
1491 * Command line option override env configuration per-doc. Do env first.
1493 sessiond_config_apply_env_config(&the_config
);
1496 * Parse arguments and load the daemon configuration file.
1498 * We have an exit_options exit path to free memory reserved by
1499 * set_options. This is needed because the rest of sessiond_cleanup()
1500 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1501 * depends on set_options.
1504 if (set_options(argc
, argv
)) {
1510 * Resolve all paths received as arguments, configuration option, or
1511 * through environment variable as absolute paths. This is necessary
1512 * since daemonizing causes the sessiond's current working directory
1515 ret
= sessiond_config_resolve_paths(&the_config
);
1521 lttng_opt_verbose
= the_config
.verbose
;
1522 lttng_opt_quiet
= the_config
.quiet
;
1523 the_kconsumer_data
.err_unix_sock_path
=
1524 the_config
.kconsumerd_err_unix_sock_path
.value
;
1525 the_kconsumer_data
.cmd_unix_sock_path
=
1526 the_config
.kconsumerd_cmd_unix_sock_path
.value
;
1527 the_ustconsumer32_data
.err_unix_sock_path
=
1528 the_config
.consumerd32_err_unix_sock_path
.value
;
1529 the_ustconsumer32_data
.cmd_unix_sock_path
=
1530 the_config
.consumerd32_cmd_unix_sock_path
.value
;
1531 the_ustconsumer64_data
.err_unix_sock_path
=
1532 the_config
.consumerd64_err_unix_sock_path
.value
;
1533 the_ustconsumer64_data
.cmd_unix_sock_path
=
1534 the_config
.consumerd64_cmd_unix_sock_path
.value
;
1535 set_clock_plugin_env();
1537 sessiond_config_log(&the_config
);
1538 sessiond_uuid_log();
1540 if (opt_print_version
) {
1546 if (create_lttng_rundir()) {
1551 /* Abort launch if a session daemon is already running. */
1552 if (check_existing_daemon()) {
1553 ERR("A session daemon is already running.");
1559 if (the_config
.daemonize
|| the_config
.background
) {
1562 ret
= lttng_daemonize(&the_child_ppid
, &recv_child_signal
,
1563 !the_config
.background
);
1570 * We are in the child. Make sure all other file descriptors are
1571 * closed, in case we are called with more opened file
1572 * descriptors than the standard ones and the lock file.
1574 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1575 if (i
== lockfile_fd
) {
1582 if (launch_run_as_worker(argv
[0]) < 0) {
1583 goto exit_create_run_as_worker_cleanup
;
1587 * Starting from here, we can create threads. This needs to be after
1588 * lttng_daemonize due to RCU.
1592 * Initialize the health check subsystem. This call should set the
1593 * appropriate time values.
1595 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1596 if (!the_health_sessiond
) {
1597 PERROR("health_app_create error");
1602 /* Create thread to clean up RCU hash tables */
1603 ht_cleanup_thread
= launch_ht_cleanup_thread();
1604 if (!ht_cleanup_thread
) {
1609 /* Create thread quit pipe */
1610 if (sessiond_init_thread_quit_pipe()) {
1615 /* Check if daemon is UID = 0 */
1616 is_root
= !getuid();
1618 /* Create global run dir with root access */
1620 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1621 if (!kernel_channel_monitor_pipe
) {
1622 ERR("Failed to create kernel consumer channel monitor pipe");
1626 the_kconsumer_data
.channel_monitor_pipe
=
1627 lttng_pipe_release_writefd(
1628 kernel_channel_monitor_pipe
);
1629 if (the_kconsumer_data
.channel_monitor_pipe
< 0) {
1635 /* Set consumer initial state */
1636 the_kernel_consumerd_state
= CONSUMER_STOPPED
;
1637 the_ust_consumerd_state
= CONSUMER_STOPPED
;
1639 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1640 if (!ust32_channel_monitor_pipe
) {
1641 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1645 the_ustconsumer32_data
.channel_monitor_pipe
=
1646 lttng_pipe_release_writefd(ust32_channel_monitor_pipe
);
1647 if (the_ustconsumer32_data
.channel_monitor_pipe
< 0) {
1653 * The rotation_thread_timer_queue structure is shared between the
1654 * sessiond timer thread and the rotation thread. The main thread keeps
1655 * its ownership and destroys it when both threads have been joined.
1657 rotation_timer_queue
= rotation_thread_timer_queue_create();
1658 if (!rotation_timer_queue
) {
1662 timer_thread_parameters
.rotation_thread_job_queue
=
1663 rotation_timer_queue
;
1665 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1666 if (!ust64_channel_monitor_pipe
) {
1667 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1671 the_ustconsumer64_data
.channel_monitor_pipe
=
1672 lttng_pipe_release_writefd(ust64_channel_monitor_pipe
);
1673 if (the_ustconsumer64_data
.channel_monitor_pipe
< 0) {
1679 * Init UST app hash table. Alloc hash table before this point since
1680 * cleanup() can get called after that point.
1682 if (ust_app_ht_alloc()) {
1683 ERR("Failed to allocate UST app hash table");
1688 event_notifier_error_accounting_status
= event_notifier_error_accounting_init(
1689 the_config
.event_notifier_buffer_size_kernel
,
1690 the_config
.event_notifier_buffer_size_userspace
);
1691 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1692 ERR("Failed to initialize event notifier error accounting system");
1698 * Initialize agent app hash table. We allocate the hash table here
1699 * since cleanup() can get called after this point.
1701 if (agent_app_ht_alloc()) {
1702 ERR("Failed to allocate Agent app hash table");
1707 if (agent_by_event_notifier_domain_ht_create()) {
1708 ERR("Failed to allocate per-event notifier domain agent hash table");
1713 * These actions must be executed as root. We do that *after* setting up
1714 * the sockets path because we MUST make the check for another daemon using
1715 * those paths *before* trying to set the kernel consumer sockets and init
1719 if (set_consumer_sockets(&the_kconsumer_data
)) {
1724 /* Setup kernel tracer */
1725 if (!the_config
.no_kernel
) {
1726 init_kernel_tracer();
1729 /* Set ulimit for open files */
1732 /* init lttng_fd tracking must be done after set_ulimit. */
1735 if (set_consumer_sockets(&the_ustconsumer64_data
)) {
1740 if (set_consumer_sockets(&the_ustconsumer32_data
)) {
1745 /* Get parent pid if -S, --sig-parent is specified. */
1746 if (the_config
.sig_parent
) {
1747 the_ppid
= getppid();
1750 /* Setup the kernel pipe for waking up the kernel thread */
1751 if (is_root
&& !the_config
.no_kernel
) {
1752 if (utils_create_pipe_cloexec(the_kernel_poll_pipe
)) {
1758 /* Setup the thread apps communication pipe. */
1759 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1764 /* Setup the thread apps notify communication pipe. */
1765 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1770 /* Initialize global buffer per UID and PID registry. */
1771 buffer_reg_init_uid_registry();
1772 buffer_reg_init_pid_registry();
1774 /* Init UST command queue. */
1775 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1779 /* Check for the application socket timeout env variable. */
1780 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1781 if (env_app_timeout
) {
1782 the_config
.app_socket_timeout
= atoi(env_app_timeout
);
1784 the_config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1787 ret
= write_pidfile();
1789 ERR("Error in write_pidfile");
1794 /* Initialize communication library */
1796 /* Initialize TCP timeout values */
1797 lttcomm_inet_init();
1799 /* Create health-check thread. */
1800 if (!launch_health_management_thread()) {
1805 /* notification_thread_data acquires the pipes' read side. */
1806 the_notification_thread_handle
= notification_thread_handle_create(
1807 ust32_channel_monitor_pipe
, ust64_channel_monitor_pipe
,
1808 kernel_channel_monitor_pipe
);
1809 if (!the_notification_thread_handle
) {
1811 ERR("Failed to create notification thread shared data");
1815 /* Create notification thread. */
1816 notification_thread
= launch_notification_thread(
1817 the_notification_thread_handle
);
1818 if (!notification_thread
) {
1823 /* Create timer thread. */
1824 if (!launch_timer_thread(&timer_thread_parameters
)) {
1829 /* rotation_thread_data acquires the pipes' read side. */
1830 rotation_thread_handle
= rotation_thread_handle_create(
1831 rotation_timer_queue
, the_notification_thread_handle
);
1832 if (!rotation_thread_handle
) {
1834 ERR("Failed to create rotation thread shared data");
1839 /* Create rotation thread. */
1840 if (!launch_rotation_thread(rotation_thread_handle
)) {
1845 /* Create thread to manage the client socket */
1846 client_thread
= launch_client_thread();
1847 if (!client_thread
) {
1852 /* Set credentials of the client socket and rundir */
1853 if (is_root
&& set_permissions(the_config
.rundir
.value
)) {
1858 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1859 apps_cmd_notify_pipe
[1])) {
1864 /* Create thread to manage application registration. */
1865 register_apps_thread
= launch_application_registration_thread(
1867 if (!register_apps_thread
) {
1872 /* Create thread to manage application socket */
1873 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1878 /* Create thread to manage application notify socket */
1879 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1884 /* Create agent management thread. */
1885 if (!launch_agent_management_thread()) {
1890 /* Don't start this thread if kernel tracing is not requested nor root */
1891 if (is_root
&& !the_config
.no_kernel
) {
1892 /* Create kernel thread to manage kernel event */
1893 if (!launch_kernel_management_thread(the_kernel_poll_pipe
[0])) {
1898 if (kernel_get_notification_fd() >= 0) {
1899 ret
= notification_thread_command_add_tracer_event_source(
1900 the_notification_thread_handle
,
1901 kernel_get_notification_fd(),
1902 LTTNG_DOMAIN_KERNEL
);
1903 if (ret
!= LTTNG_OK
) {
1904 ERR("Failed to add kernel trigger event source to notification thread");
1911 /* Load sessions. */
1912 ret
= config_load_session(
1913 the_config
.load_session_path
.value
, NULL
, 1, 1, NULL
);
1915 ERR("Session load failed: %s", error_get_str(ret
));
1920 /* Initialization completed. */
1921 sessiond_signal_parents();
1924 * This is where we start awaiting program completion (e.g. through
1925 * signal that asks threads to teardown).
1928 /* Initiate teardown once activity occurs on the quit pipe. */
1929 sessiond_wait_for_quit_pipe(-1);
1934 * Ensure that the client thread is no longer accepting new commands,
1935 * which could cause new sessions to be created.
1937 if (client_thread
) {
1938 lttng_thread_shutdown(client_thread
);
1939 lttng_thread_put(client_thread
);
1942 destroy_all_sessions_and_wait();
1945 * At this point no new trigger can be registered (no sessions are
1946 * running/rotating) and clients can't connect to the session daemon
1947 * anymore. Unregister all triggers.
1949 unregister_all_triggers();
1951 if (register_apps_thread
) {
1952 lttng_thread_shutdown(register_apps_thread
);
1953 lttng_thread_put(register_apps_thread
);
1955 lttng_thread_list_shutdown_orphans();
1958 * Wait for all pending call_rcu work to complete before tearing
1959 * down data structures. call_rcu worker may be trying to
1960 * perform lookups in those structures.
1964 * sessiond_cleanup() is called when no other thread is running, except
1965 * the ht_cleanup thread, which is needed to destroy the hash tables.
1967 rcu_thread_online();
1971 * Wait for all pending call_rcu work to complete before shutting down
1972 * the notification thread. This call_rcu work includes shutting down
1973 * UST apps and event notifier pipes.
1977 if (notification_thread
) {
1978 lttng_thread_shutdown(notification_thread
);
1979 lttng_thread_put(notification_thread
);
1983 * Error accounting teardown has to be done after the teardown of all
1984 * event notifier pipes to ensure that no tracer may try to use the
1985 * error accounting facilities.
1987 event_notifier_error_accounting_fini();
1990 * Unloading the kernel modules needs to be done after all kernel
1991 * ressources have been released. In our case, this includes the
1992 * notification fd, the event notifier group fd, error accounting fd,
1993 * all event and event notifier fds, etc.
1995 * In short, at this point, we need to have called close() on all fds
1996 * received from the kernel tracer.
1998 if (is_root
&& !the_config
.no_kernel
) {
1999 DBG("Unloading kernel modules");
2000 modprobe_remove_lttng_all();
2004 * Ensure all prior call_rcu are done. call_rcu callbacks may push
2005 * hash tables to the ht_cleanup thread. Therefore, we ensure that
2006 * the queue is empty before shutting down the clean-up thread.
2010 if (ht_cleanup_thread
) {
2011 lttng_thread_shutdown(ht_cleanup_thread
);
2012 lttng_thread_put(ht_cleanup_thread
);
2015 rcu_thread_offline();
2016 rcu_unregister_thread();
2018 if (rotation_thread_handle
) {
2019 rotation_thread_handle_destroy(rotation_thread_handle
);
2023 * After the rotation and timer thread have quit, we can safely destroy
2024 * the rotation_timer_queue.
2026 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
2028 * The teardown of the notification system is performed after the
2029 * session daemon's teardown in order to allow it to be notified
2030 * of the active session and channels at the moment of the teardown.
2032 if (the_notification_thread_handle
) {
2033 notification_thread_handle_destroy(
2034 the_notification_thread_handle
);
2036 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
2037 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
2038 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
2040 if (the_health_sessiond
) {
2041 health_app_destroy(the_health_sessiond
);
2043 exit_create_run_as_worker_cleanup
:
2045 sessiond_cleanup_lock_file();
2046 sessiond_cleanup_options();
2048 exit_set_signal_handler
: