2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
22 #include <sys/mount.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <sys/types.h>
28 #include <urcu/uatomic.h>
32 #include <common/common.h>
33 #include <common/compat/socket.h>
34 #include <common/compat/getenv.h>
35 #include <common/defaults.h>
36 #include <common/kernel-consumer/kernel-consumer.h>
37 #include <common/futex.h>
38 #include <common/relayd/relayd.h>
39 #include <common/utils.h>
40 #include <common/daemonize.h>
41 #include <common/config/session-config.h>
42 #include <common/dynamic-buffer.h>
43 #include <lttng/event-internal.h>
45 #include "lttng-sessiond.h"
46 #include "buffer-registry.h"
52 #include "event-notifier-error-accounting.h"
54 #include "kernel-consumer.h"
55 #include "lttng-ust-ctl.h"
56 #include "ust-consumer.h"
59 #include "health-sessiond.h"
60 #include "testpoint.h"
61 #include "notify-apps.h"
62 #include "agent-thread.h"
64 #include "notification-thread.h"
65 #include "notification-thread-commands.h"
66 #include "rotation-thread.h"
68 #include "ht-cleanup.h"
69 #include "sessiond-config.h"
75 #include "manage-apps.h"
76 #include "manage-kernel.h"
79 static const char *help_msg
=
80 #ifdef LTTNG_EMBED_HELP
81 #include <lttng-sessiond.8.h>
87 #define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
88 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR \
89 "event-notifier-error-buffer-size"
90 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR \
91 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-kernel"
92 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR \
93 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-userspace"
97 static int lockfile_fd
= -1;
98 static int opt_print_version
;
100 /* Set to 1 when a SIGUSR1 signal is received. */
101 static int recv_child_signal
;
103 /* Command line options */
104 static const struct option long_options
[] = {
105 { "client-sock", required_argument
, 0, 'c' },
106 { "apps-sock", required_argument
, 0, 'a' },
107 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
108 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
109 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
110 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
111 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
112 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
113 { "consumerd32-path", required_argument
, 0, '\0' },
114 { "consumerd32-libdir", required_argument
, 0, '\0' },
115 { "consumerd64-path", required_argument
, 0, '\0' },
116 { "consumerd64-libdir", required_argument
, 0, '\0' },
117 { "daemonize", no_argument
, 0, 'd' },
118 { "background", no_argument
, 0, 'b' },
119 { "sig-parent", no_argument
, 0, 'S' },
120 { "help", no_argument
, 0, 'h' },
121 { "group", required_argument
, 0, 'g' },
122 { "version", no_argument
, 0, 'V' },
123 { "quiet", no_argument
, 0, 'q' },
124 { "verbose", no_argument
, 0, 'v' },
125 { "verbose-consumer", no_argument
, 0, '\0' },
126 { "no-kernel", no_argument
, 0, '\0' },
127 { "pidfile", required_argument
, 0, 'p' },
128 { "agent-tcp-port", required_argument
, 0, '\0' },
129 { "config", required_argument
, 0, 'f' },
130 { "load", required_argument
, 0, 'l' },
131 { "kmod-probes", required_argument
, 0, '\0' },
132 { "extra-kmod-probes", required_argument
, 0, '\0' },
133 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, required_argument
, 0, '\0' },
134 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, required_argument
, 0, '\0' },
138 /* Command line options to ignore from configuration file */
139 static const char *config_ignore_options
[] = { "help", "version", "config" };
142 * This pipe is used to inform the thread managing application communication
143 * that a command is queued and ready to be processed.
145 static int apps_cmd_pipe
[2] = { -1, -1 };
146 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
149 * UST registration command queue. This queue is tied with a futex and uses a N
150 * wakers / 1 waiter implemented and detailed in futex.c/.h
152 * The thread_registration_apps and thread_dispatch_ust_registration uses this
153 * queue along with the wait/wake scheme. The thread_manage_apps receives down
154 * the line new application socket and monitors it for any I/O error or clean
155 * close that triggers an unregistration of the application.
157 static struct ust_cmd_queue ust_cmd_queue
;
160 * Section name to look for in the daemon configuration file.
162 static const char * const config_section_name
= "sessiond";
164 /* Am I root or not. Set to 1 if the daemon is running as root */
168 * Stop all threads by closing the thread quit pipe.
170 static void stop_threads(void)
174 /* Stopping all threads */
175 DBG("Terminating all threads");
176 ret
= sessiond_notify_quit_pipe();
178 ERR("write error on thread quit pipe");
183 * Close every consumer sockets.
185 static void close_consumer_sockets(void)
189 if (the_kconsumer_data
.err_sock
>= 0) {
190 ret
= close(the_kconsumer_data
.err_sock
);
192 PERROR("kernel consumer err_sock close");
195 if (the_ustconsumer32_data
.err_sock
>= 0) {
196 ret
= close(the_ustconsumer32_data
.err_sock
);
198 PERROR("UST consumerd32 err_sock close");
201 if (the_ustconsumer64_data
.err_sock
>= 0) {
202 ret
= close(the_ustconsumer64_data
.err_sock
);
204 PERROR("UST consumerd64 err_sock close");
207 if (the_kconsumer_data
.cmd_sock
>= 0) {
208 ret
= close(the_kconsumer_data
.cmd_sock
);
210 PERROR("kernel consumer cmd_sock close");
213 if (the_ustconsumer32_data
.cmd_sock
>= 0) {
214 ret
= close(the_ustconsumer32_data
.cmd_sock
);
216 PERROR("UST consumerd32 cmd_sock close");
219 if (the_ustconsumer64_data
.cmd_sock
>= 0) {
220 ret
= close(the_ustconsumer64_data
.cmd_sock
);
222 PERROR("UST consumerd64 cmd_sock close");
225 if (the_kconsumer_data
.channel_monitor_pipe
>= 0) {
226 ret
= close(the_kconsumer_data
.channel_monitor_pipe
);
228 PERROR("kernel consumer channel monitor pipe close");
231 if (the_ustconsumer32_data
.channel_monitor_pipe
>= 0) {
232 ret
= close(the_ustconsumer32_data
.channel_monitor_pipe
);
234 PERROR("UST consumerd32 channel monitor pipe close");
237 if (the_ustconsumer64_data
.channel_monitor_pipe
>= 0) {
238 ret
= close(the_ustconsumer64_data
.channel_monitor_pipe
);
240 PERROR("UST consumerd64 channel monitor pipe close");
246 * Wait on consumer process termination.
248 * Need to be called with the consumer data lock held or from a context
249 * ensuring no concurrent access to data (e.g: cleanup).
251 static void wait_consumer(struct consumer_data
*consumer_data
)
256 if (consumer_data
->pid
<= 0) {
260 DBG("Waiting for complete teardown of consumerd (PID: %d)",
262 ret
= waitpid(consumer_data
->pid
, &status
, 0);
264 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
265 } else if (!WIFEXITED(status
)) {
266 ERR("consumerd termination with error: %d",
269 consumer_data
->pid
= 0;
273 * Cleanup the session daemon's data structures.
275 static void sessiond_cleanup(void)
278 struct ltt_session_list
*session_list
= session_get_list();
280 DBG("Cleanup sessiond");
283 * Close the thread quit pipe. It has already done its job,
284 * since we are now called.
286 sessiond_close_quit_pipe();
287 utils_close_pipe(apps_cmd_pipe
);
288 utils_close_pipe(apps_cmd_notify_pipe
);
289 utils_close_pipe(the_kernel_poll_pipe
);
291 ret
= remove(the_config
.pid_file_path
.value
);
293 PERROR("remove pidfile %s", the_config
.pid_file_path
.value
);
296 DBG("Removing sessiond and consumerd content of directory %s",
297 the_config
.rundir
.value
);
300 DBG("Removing %s", the_config
.pid_file_path
.value
);
301 (void) unlink(the_config
.pid_file_path
.value
);
303 DBG("Removing %s", the_config
.agent_port_file_path
.value
);
304 (void) unlink(the_config
.agent_port_file_path
.value
);
307 DBG("Removing %s", the_kconsumer_data
.err_unix_sock_path
);
308 (void) unlink(the_kconsumer_data
.err_unix_sock_path
);
310 DBG("Removing directory %s", the_config
.kconsumerd_path
.value
);
311 (void) rmdir(the_config
.kconsumerd_path
.value
);
313 /* ust consumerd 32 */
314 DBG("Removing %s", the_config
.consumerd32_err_unix_sock_path
.value
);
315 (void) unlink(the_config
.consumerd32_err_unix_sock_path
.value
);
317 DBG("Removing directory %s", the_config
.consumerd32_path
.value
);
318 (void) rmdir(the_config
.consumerd32_path
.value
);
320 /* ust consumerd 64 */
321 DBG("Removing %s", the_config
.consumerd64_err_unix_sock_path
.value
);
322 (void) unlink(the_config
.consumerd64_err_unix_sock_path
.value
);
324 DBG("Removing directory %s", the_config
.consumerd64_path
.value
);
325 (void) rmdir(the_config
.consumerd64_path
.value
);
327 pthread_mutex_destroy(&session_list
->lock
);
329 DBG("Cleaning up all per-event notifier domain agents");
330 agent_by_event_notifier_domain_ht_destroy();
332 DBG("Cleaning up all agent apps");
333 agent_app_ht_clean();
334 DBG("Closing all UST sockets");
335 ust_app_clean_list();
336 buffer_reg_destroy_registries();
338 close_consumer_sockets();
340 wait_consumer(&the_kconsumer_data
);
341 wait_consumer(&the_ustconsumer64_data
);
342 wait_consumer(&the_ustconsumer32_data
);
344 if (is_root
&& !the_config
.no_kernel
) {
345 cleanup_kernel_tracer();
349 * We do NOT rmdir rundir because there are other processes
350 * using it, for instance lttng-relayd, which can start in
351 * parallel with this teardown.
356 * Cleanup the daemon's option data structures.
358 static void sessiond_cleanup_options(void)
360 DBG("Cleaning up options");
362 sessiond_config_fini(&the_config
);
364 run_as_destroy_worker();
367 static int string_match(const char *str1
, const char *str2
)
369 return (str1
&& str2
) && !strcmp(str1
, str2
);
373 * Take an option from the getopt output and set it in the right variable to be
376 * Return 0 on success else a negative value.
378 static int set_option(int opt
, const char *arg
, const char *optname
)
382 if (string_match(optname
, "client-sock") || opt
== 'c') {
383 if (!arg
|| *arg
== '\0') {
387 if (lttng_is_setuid_setgid()) {
388 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
389 "-c, --client-sock");
391 config_string_set(&the_config
.client_unix_sock_path
,
393 if (!the_config
.client_unix_sock_path
.value
) {
398 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
399 if (!arg
|| *arg
== '\0') {
403 if (lttng_is_setuid_setgid()) {
404 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
407 config_string_set(&the_config
.apps_unix_sock_path
,
409 if (!the_config
.apps_unix_sock_path
.value
) {
414 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
415 the_config
.daemonize
= true;
416 } else if (string_match(optname
, "background") || opt
== 'b') {
417 the_config
.background
= true;
418 } else if (string_match(optname
, "group") || opt
== 'g') {
419 if (!arg
|| *arg
== '\0') {
423 if (lttng_is_setuid_setgid()) {
424 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
427 config_string_set(&the_config
.tracing_group_name
,
429 if (!the_config
.tracing_group_name
.value
) {
434 } else if (string_match(optname
, "help") || opt
== 'h') {
435 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
437 ERR("Cannot show --help for `lttng-sessiond`");
440 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
441 } else if (string_match(optname
, "version") || opt
== 'V') {
442 opt_print_version
= 1;
443 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
444 the_config
.sig_parent
= true;
445 } else if (string_match(optname
, "kconsumerd-err-sock")) {
446 if (!arg
|| *arg
== '\0') {
450 if (lttng_is_setuid_setgid()) {
451 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
452 "--kconsumerd-err-sock");
455 &the_config
.kconsumerd_err_unix_sock_path
,
457 if (!the_config
.kconsumerd_err_unix_sock_path
.value
) {
462 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
463 if (!arg
|| *arg
== '\0') {
467 if (lttng_is_setuid_setgid()) {
468 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
469 "--kconsumerd-cmd-sock");
472 &the_config
.kconsumerd_cmd_unix_sock_path
,
474 if (!the_config
.kconsumerd_cmd_unix_sock_path
.value
) {
479 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
480 if (!arg
|| *arg
== '\0') {
484 if (lttng_is_setuid_setgid()) {
485 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
486 "--ustconsumerd64-err-sock");
489 &the_config
.consumerd64_err_unix_sock_path
,
491 if (!the_config
.consumerd64_err_unix_sock_path
.value
) {
496 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
497 if (!arg
|| *arg
== '\0') {
501 if (lttng_is_setuid_setgid()) {
502 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
503 "--ustconsumerd64-cmd-sock");
506 &the_config
.consumerd64_cmd_unix_sock_path
,
508 if (!the_config
.consumerd64_cmd_unix_sock_path
.value
) {
513 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
514 if (!arg
|| *arg
== '\0') {
518 if (lttng_is_setuid_setgid()) {
519 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
520 "--ustconsumerd32-err-sock");
523 &the_config
.consumerd32_err_unix_sock_path
,
525 if (!the_config
.consumerd32_err_unix_sock_path
.value
) {
530 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
531 if (!arg
|| *arg
== '\0') {
535 if (lttng_is_setuid_setgid()) {
536 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
537 "--ustconsumerd32-cmd-sock");
540 &the_config
.consumerd32_cmd_unix_sock_path
,
542 if (!the_config
.consumerd32_cmd_unix_sock_path
.value
) {
547 } else if (string_match(optname
, "no-kernel")) {
548 the_config
.no_kernel
= true;
549 } else if (string_match(optname
, "quiet") || opt
== 'q') {
550 the_config
.quiet
= true;
551 } else if (string_match(optname
, "verbose") || opt
== 'v') {
552 /* Verbose level can increase using multiple -v */
554 /* Value obtained from config file */
555 the_config
.verbose
= config_parse_value(arg
);
557 /* -v used on command line */
558 the_config
.verbose
++;
560 /* Clamp value to [0, 3] */
561 the_config
.verbose
= the_config
.verbose
< 0 ?
563 (the_config
.verbose
<= 3 ? the_config
.verbose
:
565 } else if (string_match(optname
, "verbose-consumer")) {
567 the_config
.verbose_consumer
= config_parse_value(arg
);
569 the_config
.verbose_consumer
++;
571 } else if (string_match(optname
, "consumerd32-path")) {
572 if (!arg
|| *arg
== '\0') {
576 if (lttng_is_setuid_setgid()) {
577 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
578 "--consumerd32-path");
580 config_string_set(&the_config
.consumerd32_bin_path
,
582 if (!the_config
.consumerd32_bin_path
.value
) {
587 } else if (string_match(optname
, "consumerd32-libdir")) {
588 if (!arg
|| *arg
== '\0') {
592 if (lttng_is_setuid_setgid()) {
593 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
594 "--consumerd32-libdir");
596 config_string_set(&the_config
.consumerd32_lib_dir
,
598 if (!the_config
.consumerd32_lib_dir
.value
) {
603 } else if (string_match(optname
, "consumerd64-path")) {
604 if (!arg
|| *arg
== '\0') {
608 if (lttng_is_setuid_setgid()) {
609 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
610 "--consumerd64-path");
612 config_string_set(&the_config
.consumerd64_bin_path
,
614 if (!the_config
.consumerd64_bin_path
.value
) {
619 } else if (string_match(optname
, "consumerd64-libdir")) {
620 if (!arg
|| *arg
== '\0') {
624 if (lttng_is_setuid_setgid()) {
625 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
626 "--consumerd64-libdir");
628 config_string_set(&the_config
.consumerd64_lib_dir
,
630 if (!the_config
.consumerd64_lib_dir
.value
) {
635 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
636 if (!arg
|| *arg
== '\0') {
640 if (lttng_is_setuid_setgid()) {
641 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
645 &the_config
.pid_file_path
, strdup(arg
));
646 if (!the_config
.pid_file_path
.value
) {
651 } else if (string_match(optname
, "agent-tcp-port")) {
652 if (!arg
|| *arg
== '\0') {
656 if (lttng_is_setuid_setgid()) {
657 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
663 v
= strtoul(arg
, NULL
, 0);
664 if (errno
!= 0 || !isdigit(arg
[0])) {
665 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
668 if (v
== 0 || v
>= 65535) {
669 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
672 the_config
.agent_tcp_port
.begin
=
673 the_config
.agent_tcp_port
.end
= (int) v
;
674 DBG3("Agent TCP port set to non default: %i", (int) v
);
676 } else if (string_match(optname
, "load") || opt
== 'l') {
677 if (!arg
|| *arg
== '\0') {
681 if (lttng_is_setuid_setgid()) {
682 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
685 config_string_set(&the_config
.load_session_path
,
687 if (!the_config
.load_session_path
.value
) {
692 } else if (string_match(optname
, "kmod-probes")) {
693 if (!arg
|| *arg
== '\0') {
697 if (lttng_is_setuid_setgid()) {
698 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
701 config_string_set(&the_config
.kmod_probes_list
,
703 if (!the_config
.kmod_probes_list
.value
) {
708 } else if (string_match(optname
, "extra-kmod-probes")) {
709 if (!arg
|| *arg
== '\0') {
713 if (lttng_is_setuid_setgid()) {
714 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
715 "--extra-kmod-probes");
717 config_string_set(&the_config
.kmod_extra_probes_list
,
719 if (!the_config
.kmod_extra_probes_list
.value
) {
724 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
)) {
728 v
= strtoul(arg
, NULL
, 0);
729 if (errno
!= 0 || !isdigit(arg
[0])) {
730 ERR("Wrong value in --%s parameter: %s",
731 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
734 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
735 ERR("Value out of range for --%s parameter: %s",
736 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
739 the_config
.event_notifier_buffer_size_kernel
= (int) v
;
740 DBG3("Number of event notifier error buffer kernel size to non default: %i",
741 the_config
.event_notifier_buffer_size_kernel
);
743 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
)) {
747 v
= strtoul(arg
, NULL
, 0);
748 if (errno
!= 0 || !isdigit(arg
[0])) {
749 ERR("Wrong value in --%s parameter: %s",
750 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
753 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
754 ERR("Value out of range for --%s parameter: %s",
755 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
758 the_config
.event_notifier_buffer_size_userspace
= (int) v
;
759 DBG3("Number of event notifier error buffer userspace size to non default: %i",
760 the_config
.event_notifier_buffer_size_userspace
);
762 } else if (string_match(optname
, "config") || opt
== 'f') {
763 /* This is handled in set_options() thus silent skip. */
766 /* Unknown option or other error.
767 * Error is printed by getopt, just return */
772 if (ret
== -EINVAL
) {
773 const char *opt_name
= "unknown";
776 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
778 if (opt
== long_options
[i
].val
) {
779 opt_name
= long_options
[i
].name
;
784 WARN("Invalid argument provided for option \"%s\", using default value.",
792 * config_entry_handler_cb used to handle options read from a config file.
793 * See config_entry_handler_cb comment in common/config/session-config.h for the
794 * return value conventions.
796 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
800 if (!entry
|| !entry
->name
|| !entry
->value
) {
805 /* Check if the option is to be ignored */
806 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
807 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
812 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
815 /* Ignore if not fully matched. */
816 if (strcmp(entry
->name
, long_options
[i
].name
)) {
821 * If the option takes no argument on the command line, we have to
822 * check if the value is "true". We support non-zero numeric values,
825 if (!long_options
[i
].has_arg
) {
826 ret
= config_parse_value(entry
->value
);
829 WARN("Invalid configuration value \"%s\" for option %s",
830 entry
->value
, entry
->name
);
832 /* False, skip boolean config option. */
837 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
841 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
847 static void print_version(void) {
848 fprintf(stdout
, "%s\n", VERSION
);
852 * daemon configuration loading and argument parsing
854 static int set_options(int argc
, char **argv
)
856 int ret
= 0, c
= 0, option_index
= 0;
857 int orig_optopt
= optopt
, orig_optind
= optind
;
859 char *config_path
= NULL
;
861 optstring
= utils_generate_optstring(long_options
,
862 sizeof(long_options
) / sizeof(struct option
));
868 /* Check for the --config option */
869 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
870 &option_index
)) != -1) {
874 } else if (c
!= 'f') {
875 /* if not equal to --config option. */
879 if (lttng_is_setuid_setgid()) {
880 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
884 config_path
= utils_expand_path(optarg
);
886 ERR("Failed to resolve path: %s", optarg
);
891 ret
= config_get_section_entries(config_path
, config_section_name
,
892 config_entry_handler
, NULL
);
895 ERR("Invalid configuration option at line %i", ret
);
901 /* Reset getopt's global state */
902 optopt
= orig_optopt
;
903 optind
= orig_optind
;
907 * getopt_long() will not set option_index if it encounters a
910 c
= getopt_long(argc
, argv
, optstring
, long_options
,
917 * Pass NULL as the long option name if popt left the index
920 ret
= set_option(c
, optarg
,
921 option_index
< 0 ? NULL
:
922 long_options
[option_index
].name
);
935 * Create lockfile using the rundir and return its fd.
937 static int create_lockfile(void)
939 return utils_create_lock_file(the_config
.lock_file_path
.value
);
943 * Check if the global socket is available, and if a daemon is answering at the
944 * other side. If yes, error is returned.
946 * Also attempts to create and hold the lock file.
948 static int check_existing_daemon(void)
952 /* Is there anybody out there ? */
953 if (lttng_session_daemon_alive()) {
958 lockfile_fd
= create_lockfile();
959 if (lockfile_fd
< 0) {
967 static void sessiond_cleanup_lock_file(void)
972 * Cleanup lock file by deleting it and finaly closing it which will
973 * release the file system lock.
975 if (lockfile_fd
>= 0) {
976 ret
= remove(the_config
.lock_file_path
.value
);
978 PERROR("remove lock file");
980 ret
= close(lockfile_fd
);
982 PERROR("close lock file");
988 * Set the tracing group gid onto the client socket.
990 * Race window between mkdir and chown is OK because we are going from more
991 * permissive (root.root) to less permissive (root.tracing).
993 static int set_permissions(char *rundir
)
998 ret
= utils_get_group_id(
999 the_config
.tracing_group_name
.value
, true, &gid
);
1001 /* Default to root group. */
1005 /* Set lttng run dir */
1006 ret
= chown(rundir
, 0, gid
);
1008 ERR("Unable to set group on %s", rundir
);
1013 * Ensure all applications and tracing group can search the run
1014 * dir. Allow everyone to read the directory, since it does not
1015 * buy us anything to hide its content.
1017 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1019 ERR("Unable to set permissions on %s", rundir
);
1023 /* lttng client socket path */
1024 ret
= chown(the_config
.client_unix_sock_path
.value
, 0, gid
);
1026 ERR("Unable to set group on %s",
1027 the_config
.client_unix_sock_path
.value
);
1031 /* kconsumer error socket path */
1032 ret
= chown(the_kconsumer_data
.err_unix_sock_path
, 0, 0);
1034 ERR("Unable to set group on %s",
1035 the_kconsumer_data
.err_unix_sock_path
);
1039 /* 64-bit ustconsumer error socket path */
1040 ret
= chown(the_ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1042 ERR("Unable to set group on %s",
1043 the_ustconsumer64_data
.err_unix_sock_path
);
1047 /* 32-bit ustconsumer compat32 error socket path */
1048 ret
= chown(the_ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1050 ERR("Unable to set group on %s",
1051 the_ustconsumer32_data
.err_unix_sock_path
);
1055 DBG("All permissions are set");
1061 * Create the lttng run directory needed for all global sockets and pipe.
1063 static int create_lttng_rundir(void)
1067 DBG3("Creating LTTng run directory: %s", the_config
.rundir
.value
);
1069 ret
= mkdir(the_config
.rundir
.value
, S_IRWXU
);
1071 if (errno
!= EEXIST
) {
1072 ERR("Unable to create %s", the_config
.rundir
.value
);
1084 * Setup sockets and directory needed by the consumerds' communication with the
1087 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1092 switch (consumer_data
->type
) {
1093 case LTTNG_CONSUMER_KERNEL
:
1094 path
= the_config
.kconsumerd_path
.value
;
1096 case LTTNG_CONSUMER64_UST
:
1097 path
= the_config
.consumerd64_path
.value
;
1099 case LTTNG_CONSUMER32_UST
:
1100 path
= the_config
.consumerd32_path
.value
;
1103 ERR("Consumer type unknown");
1109 DBG2("Creating consumer directory: %s", path
);
1111 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1112 if (ret
< 0 && errno
!= EEXIST
) {
1114 ERR("Failed to create %s", path
);
1120 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
,
1123 /* Default to root group. */
1127 ret
= chown(path
, 0, gid
);
1129 ERR("Unable to set group on %s", path
);
1135 /* Create the consumerd error unix socket */
1136 consumer_data
->err_sock
=
1137 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1138 if (consumer_data
->err_sock
< 0) {
1139 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1145 * Set the CLOEXEC flag. Return code is useless because either way, the
1148 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1150 PERROR("utils_set_fd_cloexec");
1151 /* continue anyway */
1154 /* File permission MUST be 660 */
1155 ret
= chmod(consumer_data
->err_unix_sock_path
,
1156 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1158 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1168 * Signal handler for the daemon
1170 * Simply stop all worker threads, leaving main() return gracefully after
1171 * joining all threads and calling cleanup().
1173 static void sighandler(int sig
)
1177 DBG("SIGINT caught");
1181 DBG("SIGTERM caught");
1185 CMM_STORE_SHARED(recv_child_signal
, 1);
1193 * Setup signal handler for :
1194 * SIGINT, SIGTERM, SIGPIPE
1196 static int set_signal_handler(void)
1199 struct sigaction sa
;
1202 if ((ret
= sigemptyset(&sigset
)) < 0) {
1203 PERROR("sigemptyset");
1207 sa
.sa_mask
= sigset
;
1210 sa
.sa_handler
= sighandler
;
1211 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1212 PERROR("sigaction");
1216 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1217 PERROR("sigaction");
1221 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1222 PERROR("sigaction");
1226 sa
.sa_handler
= SIG_IGN
;
1227 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1228 PERROR("sigaction");
1232 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1238 * Set open files limit to unlimited. This daemon can open a large number of
1239 * file descriptors in order to consume multiple kernel traces.
1241 static void set_ulimit(void)
1246 /* The kernel does not allow an infinite limit for open files */
1247 lim
.rlim_cur
= 65535;
1248 lim
.rlim_max
= 65535;
1250 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1252 PERROR("failed to set open files limit");
1256 static int write_pidfile(void)
1258 return utils_create_pid_file(getpid(), the_config
.pid_file_path
.value
);
1261 static int set_clock_plugin_env(void)
1264 char *env_value
= NULL
;
1266 if (!the_config
.lttng_ust_clock_plugin
.value
) {
1270 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1271 the_config
.lttng_ust_clock_plugin
.value
);
1277 ret
= putenv(env_value
);
1280 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1284 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1285 the_config
.lttng_ust_clock_plugin
.value
);
1290 static void destroy_all_sessions_and_wait(void)
1292 struct ltt_session
*session
, *tmp
;
1293 struct ltt_session_list
*session_list
;
1295 session_list
= session_get_list();
1296 DBG("Initiating destruction of all sessions");
1298 if (!session_list
) {
1302 session_lock_list();
1303 /* Initiate the destruction of all sessions. */
1304 cds_list_for_each_entry_safe(session
, tmp
,
1305 &session_list
->head
, list
) {
1306 if (!session_get(session
)) {
1310 session_lock(session
);
1311 if (session
->destroyed
) {
1312 goto unlock_session
;
1314 (void) cmd_stop_trace(session
);
1315 (void) cmd_destroy_session(
1316 session
, the_notification_thread_handle
, NULL
);
1318 session_unlock(session
);
1319 session_put(session
);
1321 session_unlock_list();
1323 /* Wait for the destruction of all sessions to complete. */
1324 DBG("Waiting for the destruction of all sessions to complete");
1325 session_list_wait_empty();
1326 DBG("Destruction of all sessions completed");
1329 static void unregister_all_triggers(void)
1331 enum lttng_error_code ret_code
;
1332 enum lttng_trigger_status trigger_status
;
1333 struct lttng_triggers
*triggers
= NULL
;
1334 unsigned int trigger_count
, i
;
1335 const struct lttng_credentials creds
= {
1336 .uid
= LTTNG_OPTIONAL_INIT_VALUE(0),
1339 DBG("Unregistering all triggers");
1342 * List all triggers as "root" since we wish to unregister all triggers.
1344 ret_code
= notification_thread_command_list_triggers(
1345 the_notification_thread_handle
, creds
.uid
.value
,
1347 if (ret_code
!= LTTNG_OK
) {
1348 ERR("Failed to list triggers while unregistering all triggers");
1352 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
1353 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1355 for (i
= 0; i
< trigger_count
; i
++) {
1356 uid_t trigger_owner
;
1357 const char *trigger_name
;
1358 const struct lttng_trigger
*trigger
=
1359 lttng_triggers_get_at_index(triggers
, i
);
1363 trigger_status
= lttng_trigger_get_owner_uid(
1364 trigger
, &trigger_owner
);
1365 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1367 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
1368 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
1369 trigger_name
: "(anonymous)";
1371 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1372 (int) trigger_owner
, trigger_name
);
1374 ret_code
= cmd_unregister_trigger(&creds
, trigger
,
1375 the_notification_thread_handle
);
1376 if (ret_code
!= LTTNG_OK
) {
1377 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1378 (int) trigger_owner
, trigger_name
,
1379 lttng_strerror(-ret_code
));
1380 /* Continue to unregister the remaining triggers. */
1384 lttng_triggers_destroy(triggers
);
1387 static int run_as_worker_post_fork_cleanup(void *data
)
1389 struct sessiond_config
*sessiond_config
= data
;
1391 sessiond_config_fini(sessiond_config
);
1395 static int launch_run_as_worker(const char *procname
)
1398 * Clean-up before forking the run-as worker. Any dynamically
1399 * allocated memory of which the worker is not aware will
1400 * be leaked as the process forks a run-as worker (and performs
1401 * no exec*()). The same would apply to any opened fd.
1403 return run_as_create_worker(
1404 procname
, run_as_worker_post_fork_cleanup
, &the_config
);
1407 static void sessiond_uuid_log(void)
1409 char uuid_str
[LTTNG_UUID_STR_LEN
];
1411 lttng_uuid_to_str(the_sessiond_uuid
, uuid_str
);
1412 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1418 int main(int argc
, char **argv
)
1420 int ret
= 0, retval
= 0;
1421 const char *env_app_timeout
;
1422 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1423 *ust64_channel_monitor_pipe
= NULL
,
1424 *kernel_channel_monitor_pipe
= NULL
;
1425 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1426 struct timer_thread_parameters timer_thread_parameters
;
1427 /* Rotation thread handle. */
1428 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1429 /* Queue of rotation jobs populated by the sessiond-timer. */
1430 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1431 struct lttng_thread
*client_thread
= NULL
;
1432 struct lttng_thread
*notification_thread
= NULL
;
1433 struct lttng_thread
*register_apps_thread
= NULL
;
1434 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
1436 logger_set_thread_name("Main", false);
1437 init_kernel_workarounds();
1439 rcu_register_thread();
1441 if (set_signal_handler()) {
1443 goto exit_set_signal_handler
;
1446 if (timer_signal_init()) {
1448 goto exit_set_signal_handler
;
1451 the_page_size
= sysconf(_SC_PAGESIZE
);
1452 if (the_page_size
< 0) {
1453 PERROR("sysconf _SC_PAGESIZE");
1454 the_page_size
= LONG_MAX
;
1455 WARN("Fallback page size to %ld", the_page_size
);
1458 ret
= sessiond_config_init(&the_config
);
1461 goto exit_set_signal_handler
;
1465 * Init config from environment variables.
1466 * Command line option override env configuration per-doc. Do env first.
1468 sessiond_config_apply_env_config(&the_config
);
1471 * Parse arguments and load the daemon configuration file.
1473 * We have an exit_options exit path to free memory reserved by
1474 * set_options. This is needed because the rest of sessiond_cleanup()
1475 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1476 * depends on set_options.
1479 if (set_options(argc
, argv
)) {
1485 * Resolve all paths received as arguments, configuration option, or
1486 * through environment variable as absolute paths. This is necessary
1487 * since daemonizing causes the sessiond's current working directory
1490 ret
= sessiond_config_resolve_paths(&the_config
);
1496 lttng_opt_verbose
= the_config
.verbose
;
1497 lttng_opt_quiet
= the_config
.quiet
;
1498 the_kconsumer_data
.err_unix_sock_path
=
1499 the_config
.kconsumerd_err_unix_sock_path
.value
;
1500 the_kconsumer_data
.cmd_unix_sock_path
=
1501 the_config
.kconsumerd_cmd_unix_sock_path
.value
;
1502 the_ustconsumer32_data
.err_unix_sock_path
=
1503 the_config
.consumerd32_err_unix_sock_path
.value
;
1504 the_ustconsumer32_data
.cmd_unix_sock_path
=
1505 the_config
.consumerd32_cmd_unix_sock_path
.value
;
1506 the_ustconsumer64_data
.err_unix_sock_path
=
1507 the_config
.consumerd64_err_unix_sock_path
.value
;
1508 the_ustconsumer64_data
.cmd_unix_sock_path
=
1509 the_config
.consumerd64_cmd_unix_sock_path
.value
;
1510 set_clock_plugin_env();
1512 sessiond_config_log(&the_config
);
1513 sessiond_uuid_log();
1515 if (opt_print_version
) {
1521 if (create_lttng_rundir()) {
1526 /* Abort launch if a session daemon is already running. */
1527 if (check_existing_daemon()) {
1528 ERR("A session daemon is already running.");
1534 if (the_config
.daemonize
|| the_config
.background
) {
1537 ret
= lttng_daemonize(&the_child_ppid
, &recv_child_signal
,
1538 !the_config
.background
);
1545 * We are in the child. Make sure all other file descriptors are
1546 * closed, in case we are called with more opened file
1547 * descriptors than the standard ones and the lock file.
1549 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1550 if (i
== lockfile_fd
) {
1557 if (launch_run_as_worker(argv
[0]) < 0) {
1558 goto exit_create_run_as_worker_cleanup
;
1562 * Starting from here, we can create threads. This needs to be after
1563 * lttng_daemonize due to RCU.
1567 * Initialize the health check subsystem. This call should set the
1568 * appropriate time values.
1570 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1571 if (!the_health_sessiond
) {
1572 PERROR("health_app_create error");
1577 /* Create thread to clean up RCU hash tables */
1578 ht_cleanup_thread
= launch_ht_cleanup_thread();
1579 if (!ht_cleanup_thread
) {
1584 /* Create thread quit pipe */
1585 if (sessiond_init_thread_quit_pipe()) {
1590 /* Check if daemon is UID = 0 */
1591 is_root
= !getuid();
1593 /* Create global run dir with root access */
1595 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1596 if (!kernel_channel_monitor_pipe
) {
1597 ERR("Failed to create kernel consumer channel monitor pipe");
1601 the_kconsumer_data
.channel_monitor_pipe
=
1602 lttng_pipe_release_writefd(
1603 kernel_channel_monitor_pipe
);
1604 if (the_kconsumer_data
.channel_monitor_pipe
< 0) {
1610 /* Set consumer initial state */
1611 the_kernel_consumerd_state
= CONSUMER_STOPPED
;
1612 the_ust_consumerd_state
= CONSUMER_STOPPED
;
1614 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1615 if (!ust32_channel_monitor_pipe
) {
1616 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1620 the_ustconsumer32_data
.channel_monitor_pipe
=
1621 lttng_pipe_release_writefd(ust32_channel_monitor_pipe
);
1622 if (the_ustconsumer32_data
.channel_monitor_pipe
< 0) {
1628 * The rotation_thread_timer_queue structure is shared between the
1629 * sessiond timer thread and the rotation thread. The main thread keeps
1630 * its ownership and destroys it when both threads have been joined.
1632 rotation_timer_queue
= rotation_thread_timer_queue_create();
1633 if (!rotation_timer_queue
) {
1637 timer_thread_parameters
.rotation_thread_job_queue
=
1638 rotation_timer_queue
;
1640 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1641 if (!ust64_channel_monitor_pipe
) {
1642 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1646 the_ustconsumer64_data
.channel_monitor_pipe
=
1647 lttng_pipe_release_writefd(ust64_channel_monitor_pipe
);
1648 if (the_ustconsumer64_data
.channel_monitor_pipe
< 0) {
1654 * Init UST app hash table. Alloc hash table before this point since
1655 * cleanup() can get called after that point.
1657 if (ust_app_ht_alloc()) {
1658 ERR("Failed to allocate UST app hash table");
1663 event_notifier_error_accounting_status
= event_notifier_error_accounting_init(
1664 the_config
.event_notifier_buffer_size_kernel
,
1665 the_config
.event_notifier_buffer_size_userspace
);
1666 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1667 ERR("Failed to initialize event notifier error accounting system");
1673 * Initialize agent app hash table. We allocate the hash table here
1674 * since cleanup() can get called after this point.
1676 if (agent_app_ht_alloc()) {
1677 ERR("Failed to allocate Agent app hash table");
1682 if (agent_by_event_notifier_domain_ht_create()) {
1683 ERR("Failed to allocate per-event notifier domain agent hash table");
1688 * These actions must be executed as root. We do that *after* setting up
1689 * the sockets path because we MUST make the check for another daemon using
1690 * those paths *before* trying to set the kernel consumer sockets and init
1694 if (set_consumer_sockets(&the_kconsumer_data
)) {
1699 /* Setup kernel tracer */
1700 if (!the_config
.no_kernel
) {
1701 init_kernel_tracer();
1704 /* Set ulimit for open files */
1707 /* init lttng_fd tracking must be done after set_ulimit. */
1710 if (set_consumer_sockets(&the_ustconsumer64_data
)) {
1715 if (set_consumer_sockets(&the_ustconsumer32_data
)) {
1720 /* Get parent pid if -S, --sig-parent is specified. */
1721 if (the_config
.sig_parent
) {
1722 the_ppid
= getppid();
1725 /* Setup the kernel pipe for waking up the kernel thread */
1726 if (is_root
&& !the_config
.no_kernel
) {
1727 if (utils_create_pipe_cloexec(the_kernel_poll_pipe
)) {
1733 /* Setup the thread apps communication pipe. */
1734 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1739 /* Setup the thread apps notify communication pipe. */
1740 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1745 /* Initialize global buffer per UID and PID registry. */
1746 buffer_reg_init_uid_registry();
1747 buffer_reg_init_pid_registry();
1749 /* Init UST command queue. */
1750 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1754 /* Check for the application socket timeout env variable. */
1755 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1756 if (env_app_timeout
) {
1757 the_config
.app_socket_timeout
= atoi(env_app_timeout
);
1759 the_config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1762 ret
= write_pidfile();
1764 ERR("Error in write_pidfile");
1769 /* Initialize communication library */
1771 /* Initialize TCP timeout values */
1772 lttcomm_inet_init();
1774 /* Create health-check thread. */
1775 if (!launch_health_management_thread()) {
1780 /* notification_thread_data acquires the pipes' read side. */
1781 the_notification_thread_handle
= notification_thread_handle_create(
1782 ust32_channel_monitor_pipe
, ust64_channel_monitor_pipe
,
1783 kernel_channel_monitor_pipe
);
1784 if (!the_notification_thread_handle
) {
1786 ERR("Failed to create notification thread shared data");
1790 /* Create notification thread. */
1791 notification_thread
= launch_notification_thread(
1792 the_notification_thread_handle
);
1793 if (!notification_thread
) {
1798 /* Create timer thread. */
1799 if (!launch_timer_thread(&timer_thread_parameters
)) {
1804 /* rotation_thread_data acquires the pipes' read side. */
1805 rotation_thread_handle
= rotation_thread_handle_create(
1806 rotation_timer_queue
, the_notification_thread_handle
);
1807 if (!rotation_thread_handle
) {
1809 ERR("Failed to create rotation thread shared data");
1814 /* Create rotation thread. */
1815 if (!launch_rotation_thread(rotation_thread_handle
)) {
1820 /* Create thread to manage the client socket */
1821 client_thread
= launch_client_thread();
1822 if (!client_thread
) {
1827 /* Set credentials of the client socket and rundir */
1828 if (is_root
&& set_permissions(the_config
.rundir
.value
)) {
1833 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1834 apps_cmd_notify_pipe
[1])) {
1839 /* Create thread to manage application registration. */
1840 register_apps_thread
= launch_application_registration_thread(
1842 if (!register_apps_thread
) {
1847 /* Create thread to manage application socket */
1848 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1853 /* Create thread to manage application notify socket */
1854 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1859 /* Create agent management thread. */
1860 if (!launch_agent_management_thread()) {
1865 /* Don't start this thread if kernel tracing is not requested nor root */
1866 if (is_root
&& !the_config
.no_kernel
) {
1867 /* Create kernel thread to manage kernel event */
1868 if (!launch_kernel_management_thread(the_kernel_poll_pipe
[0])) {
1873 if (kernel_get_notification_fd() >= 0) {
1874 ret
= notification_thread_command_add_tracer_event_source(
1875 the_notification_thread_handle
,
1876 kernel_get_notification_fd(),
1877 LTTNG_DOMAIN_KERNEL
);
1878 if (ret
!= LTTNG_OK
) {
1879 ERR("Failed to add kernel trigger event source to notification thread");
1886 /* Load sessions. */
1887 ret
= config_load_session(
1888 the_config
.load_session_path
.value
, NULL
, 1, 1, NULL
);
1890 ERR("Session load failed: %s", error_get_str(ret
));
1895 /* Initialization completed. */
1896 sessiond_signal_parents();
1899 * This is where we start awaiting program completion (e.g. through
1900 * signal that asks threads to teardown).
1903 /* Initiate teardown once activity occurs on the quit pipe. */
1904 sessiond_wait_for_quit_pipe(-1);
1909 * Ensure that the client thread is no longer accepting new commands,
1910 * which could cause new sessions to be created.
1912 if (client_thread
) {
1913 lttng_thread_shutdown(client_thread
);
1914 lttng_thread_put(client_thread
);
1917 destroy_all_sessions_and_wait();
1920 * At this point no new trigger can be registered (no sessions are
1921 * running/rotating) and clients can't connect to the session daemon
1922 * anymore. Unregister all triggers.
1924 unregister_all_triggers();
1926 if (register_apps_thread
) {
1927 lttng_thread_shutdown(register_apps_thread
);
1928 lttng_thread_put(register_apps_thread
);
1930 lttng_thread_list_shutdown_orphans();
1933 * Wait for all pending call_rcu work to complete before tearing
1934 * down data structures. call_rcu worker may be trying to
1935 * perform lookups in those structures.
1939 * sessiond_cleanup() is called when no other thread is running, except
1940 * the ht_cleanup thread, which is needed to destroy the hash tables.
1942 rcu_thread_online();
1946 * Wait for all pending call_rcu work to complete before shutting down
1947 * the notification thread. This call_rcu work includes shutting down
1948 * UST apps and event notifier pipes.
1952 if (notification_thread
) {
1953 lttng_thread_shutdown(notification_thread
);
1954 lttng_thread_put(notification_thread
);
1958 * Error accounting teardown has to be done after the teardown of all
1959 * event notifier pipes to ensure that no tracer may try to use the
1960 * error accounting facilities.
1962 event_notifier_error_accounting_fini();
1965 * Unloading the kernel modules needs to be done after all kernel
1966 * ressources have been released. In our case, this includes the
1967 * notification fd, the event notifier group fd, error accounting fd,
1968 * all event and event notifier fds, etc.
1970 * In short, at this point, we need to have called close() on all fds
1971 * received from the kernel tracer.
1973 if (is_root
&& !the_config
.no_kernel
) {
1974 DBG("Unloading kernel modules");
1975 modprobe_remove_lttng_all();
1979 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1980 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1981 * the queue is empty before shutting down the clean-up thread.
1985 if (ht_cleanup_thread
) {
1986 lttng_thread_shutdown(ht_cleanup_thread
);
1987 lttng_thread_put(ht_cleanup_thread
);
1990 rcu_thread_offline();
1991 rcu_unregister_thread();
1993 if (rotation_thread_handle
) {
1994 rotation_thread_handle_destroy(rotation_thread_handle
);
1998 * After the rotation and timer thread have quit, we can safely destroy
1999 * the rotation_timer_queue.
2001 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
2003 * The teardown of the notification system is performed after the
2004 * session daemon's teardown in order to allow it to be notified
2005 * of the active session and channels at the moment of the teardown.
2007 if (the_notification_thread_handle
) {
2008 notification_thread_handle_destroy(
2009 the_notification_thread_handle
);
2011 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
2012 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
2013 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
2015 if (the_health_sessiond
) {
2016 health_app_destroy(the_health_sessiond
);
2018 exit_create_run_as_worker_cleanup
:
2020 sessiond_cleanup_lock_file();
2021 sessiond_cleanup_options();
2023 exit_set_signal_handler
: