2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
22 #include <sys/mount.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <sys/types.h>
28 #include <urcu/uatomic.h>
32 #include <common/common.hpp>
33 #include <common/compat/socket.hpp>
34 #include <common/compat/getenv.hpp>
35 #include <common/defaults.hpp>
36 #include <common/kernel-consumer/kernel-consumer.hpp>
37 #include <common/futex.hpp>
38 #include <common/relayd/relayd.hpp>
39 #include <common/utils.hpp>
40 #include <common/path.hpp>
41 #include <common/daemonize.hpp>
42 #include <common/config/session-config.hpp>
43 #include <common/ini-config/ini-config.hpp>
44 #include <common/dynamic-buffer.hpp>
45 #include <common/logging-utils.hpp>
47 #include <lttng/event-internal.hpp>
48 #include "lttng-sessiond.hpp"
49 #include "buffer-registry.hpp"
50 #include "channel.hpp"
52 #include "consumer.hpp"
53 #include "context.hpp"
55 #include "event-notifier-error-accounting.hpp"
57 #include "kernel-consumer.hpp"
58 #include "lttng-ust-ctl.hpp"
59 #include "ust-consumer.hpp"
61 #include "fd-limit.hpp"
62 #include "health-sessiond.hpp"
63 #include "testpoint.hpp"
64 #include "notify-apps.hpp"
65 #include "agent-thread.hpp"
67 #include "notification-thread.hpp"
68 #include "notification-thread-commands.hpp"
69 #include "rotation-thread.hpp"
71 #include "sessiond-config.hpp"
75 #include "dispatch.hpp"
76 #include "register.hpp"
77 #include "manage-apps.hpp"
78 #include "manage-kernel.hpp"
79 #include "modprobe.hpp"
80 #include "ust-sigbus.hpp"
82 static const char *help_msg
=
83 #ifdef LTTNG_EMBED_HELP
84 #include <lttng-sessiond.8.h>
90 #define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
91 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR \
92 "event-notifier-error-buffer-size"
93 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR \
94 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-kernel"
95 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR \
96 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-userspace"
100 static int lockfile_fd
= -1;
101 static int opt_print_version
;
103 /* Set to 1 when a SIGUSR1 signal is received. */
104 static int recv_child_signal
;
106 /* Command line options */
107 static const struct option long_options
[] = {
108 { "client-sock", required_argument
, 0, 'c' },
109 { "apps-sock", required_argument
, 0, 'a' },
110 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
111 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
112 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
113 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
114 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
115 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
116 { "consumerd32-path", required_argument
, 0, '\0' },
117 { "consumerd32-libdir", required_argument
, 0, '\0' },
118 { "consumerd64-path", required_argument
, 0, '\0' },
119 { "consumerd64-libdir", required_argument
, 0, '\0' },
120 { "daemonize", no_argument
, 0, 'd' },
121 { "background", no_argument
, 0, 'b' },
122 { "sig-parent", no_argument
, 0, 'S' },
123 { "help", no_argument
, 0, 'h' },
124 { "group", required_argument
, 0, 'g' },
125 { "version", no_argument
, 0, 'V' },
126 { "quiet", no_argument
, 0, 'q' },
127 { "verbose", no_argument
, 0, 'v' },
128 { "verbose-consumer", no_argument
, 0, '\0' },
129 { "no-kernel", no_argument
, 0, '\0' },
130 { "pidfile", required_argument
, 0, 'p' },
131 { "agent-tcp-port", required_argument
, 0, '\0' },
132 { "config", required_argument
, 0, 'f' },
133 { "load", required_argument
, 0, 'l' },
134 { "kmod-probes", required_argument
, 0, '\0' },
135 { "extra-kmod-probes", required_argument
, 0, '\0' },
136 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, required_argument
, 0, '\0' },
137 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, required_argument
, 0, '\0' },
141 /* Command line options to ignore from configuration file */
142 static const char *config_ignore_options
[] = { "help", "version", "config" };
145 * This pipe is used to inform the thread managing application communication
146 * that a command is queued and ready to be processed.
148 static int apps_cmd_pipe
[2] = { -1, -1 };
149 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
152 * UST registration command queue. This queue is tied with a futex and uses a N
153 * wakers / 1 waiter implemented and detailed in futex.c/.h
155 * The thread_registration_apps and thread_dispatch_ust_registration uses this
156 * queue along with the wait/wake scheme. The thread_manage_apps receives down
157 * the line new application socket and monitors it for any I/O error or clean
158 * close that triggers an unregistration of the application.
160 static struct ust_cmd_queue ust_cmd_queue
;
163 * Section name to look for in the daemon configuration file.
165 static const char * const config_section_name
= "sessiond";
167 /* Am I root or not. Set to 1 if the daemon is running as root */
171 * Notify the main thread to initiate the teardown of the worker threads by
172 * writing to the main quit pipe.
174 static void notify_main_quit_pipe(void)
178 /* Stopping all threads */
179 DBG("Notify the main thread to terminate all worker threads");
180 ret
= sessiond_notify_main_quit_pipe();
182 ERR("write error on main quit pipe");
187 * Close every consumer sockets.
189 static void close_consumer_sockets(void)
193 if (the_kconsumer_data
.err_sock
>= 0) {
194 ret
= close(the_kconsumer_data
.err_sock
);
196 PERROR("kernel consumer err_sock close");
199 if (the_ustconsumer32_data
.err_sock
>= 0) {
200 ret
= close(the_ustconsumer32_data
.err_sock
);
202 PERROR("UST consumerd32 err_sock close");
205 if (the_ustconsumer64_data
.err_sock
>= 0) {
206 ret
= close(the_ustconsumer64_data
.err_sock
);
208 PERROR("UST consumerd64 err_sock close");
211 if (the_kconsumer_data
.cmd_sock
>= 0) {
212 ret
= close(the_kconsumer_data
.cmd_sock
);
214 PERROR("kernel consumer cmd_sock close");
217 if (the_ustconsumer32_data
.cmd_sock
>= 0) {
218 ret
= close(the_ustconsumer32_data
.cmd_sock
);
220 PERROR("UST consumerd32 cmd_sock close");
223 if (the_ustconsumer64_data
.cmd_sock
>= 0) {
224 ret
= close(the_ustconsumer64_data
.cmd_sock
);
226 PERROR("UST consumerd64 cmd_sock close");
229 if (the_kconsumer_data
.channel_monitor_pipe
>= 0) {
230 ret
= close(the_kconsumer_data
.channel_monitor_pipe
);
232 PERROR("kernel consumer channel monitor pipe close");
235 if (the_ustconsumer32_data
.channel_monitor_pipe
>= 0) {
236 ret
= close(the_ustconsumer32_data
.channel_monitor_pipe
);
238 PERROR("UST consumerd32 channel monitor pipe close");
241 if (the_ustconsumer64_data
.channel_monitor_pipe
>= 0) {
242 ret
= close(the_ustconsumer64_data
.channel_monitor_pipe
);
244 PERROR("UST consumerd64 channel monitor pipe close");
250 * Wait on consumer process termination.
252 * Need to be called with the consumer data lock held or from a context
253 * ensuring no concurrent access to data (e.g: cleanup).
255 static void wait_consumer(struct consumer_data
*consumer_data
)
260 if (consumer_data
->pid
<= 0) {
264 DBG("Waiting for complete teardown of consumerd (PID: %d)",
266 ret
= waitpid(consumer_data
->pid
, &status
, 0);
268 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
269 } else if (!WIFEXITED(status
)) {
270 ERR("consumerd termination with error: %d",
273 consumer_data
->pid
= 0;
277 * Cleanup the session daemon's data structures.
279 static void sessiond_cleanup(void)
282 struct ltt_session_list
*session_list
= session_get_list();
284 DBG("Cleanup sessiond");
287 * Close the main quit pipe. It has already done its job, since we are
290 sessiond_close_main_quit_pipe();
292 /* Close all other pipes. */
293 utils_close_pipe(apps_cmd_pipe
);
294 utils_close_pipe(apps_cmd_notify_pipe
);
295 utils_close_pipe(the_kernel_poll_pipe
);
297 ret
= remove(the_config
.pid_file_path
.value
);
299 PERROR("remove pidfile %s", the_config
.pid_file_path
.value
);
302 DBG("Removing sessiond and consumerd content of directory %s",
303 the_config
.rundir
.value
);
306 DBG("Removing %s", the_config
.pid_file_path
.value
);
307 (void) unlink(the_config
.pid_file_path
.value
);
309 DBG("Removing %s", the_config
.agent_port_file_path
.value
);
310 (void) unlink(the_config
.agent_port_file_path
.value
);
313 DBG("Removing %s", the_kconsumer_data
.err_unix_sock_path
);
314 (void) unlink(the_kconsumer_data
.err_unix_sock_path
);
316 DBG("Removing directory %s", the_config
.kconsumerd_path
.value
);
317 (void) rmdir(the_config
.kconsumerd_path
.value
);
319 /* ust consumerd 32 */
320 DBG("Removing %s", the_config
.consumerd32_err_unix_sock_path
.value
);
321 (void) unlink(the_config
.consumerd32_err_unix_sock_path
.value
);
323 DBG("Removing directory %s", the_config
.consumerd32_path
.value
);
324 (void) rmdir(the_config
.consumerd32_path
.value
);
326 /* ust consumerd 64 */
327 DBG("Removing %s", the_config
.consumerd64_err_unix_sock_path
.value
);
328 (void) unlink(the_config
.consumerd64_err_unix_sock_path
.value
);
330 DBG("Removing directory %s", the_config
.consumerd64_path
.value
);
331 (void) rmdir(the_config
.consumerd64_path
.value
);
333 pthread_mutex_destroy(&session_list
->lock
);
335 DBG("Cleaning up all per-event notifier domain agents");
336 agent_by_event_notifier_domain_ht_destroy();
338 DBG("Cleaning up all agent apps");
339 agent_app_ht_clean();
340 DBG("Closing all UST sockets");
341 ust_app_clean_list();
342 buffer_reg_destroy_registries();
344 close_consumer_sockets();
346 wait_consumer(&the_kconsumer_data
);
347 wait_consumer(&the_ustconsumer64_data
);
348 wait_consumer(&the_ustconsumer32_data
);
350 if (is_root
&& !the_config
.no_kernel
) {
351 cleanup_kernel_tracer();
355 * We do NOT rmdir rundir because there are other processes
356 * using it, for instance lttng-relayd, which can start in
357 * parallel with this teardown.
362 * Cleanup the daemon's option data structures.
364 static void sessiond_cleanup_options(void)
366 DBG("Cleaning up options");
368 sessiond_config_fini(&the_config
);
370 run_as_destroy_worker();
373 static int string_match(const char *str1
, const char *str2
)
375 return (str1
&& str2
) && !strcmp(str1
, str2
);
379 * Take an option from the getopt output and set it in the right variable to be
382 * Return 0 on success else a negative value.
384 static int set_option(int opt
, const char *arg
, const char *optname
)
388 if (string_match(optname
, "client-sock") || opt
== 'c') {
389 if (!arg
|| *arg
== '\0') {
393 if (lttng_is_setuid_setgid()) {
394 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
395 "-c, --client-sock");
397 config_string_set(&the_config
.client_unix_sock_path
,
399 if (!the_config
.client_unix_sock_path
.value
) {
404 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
405 if (!arg
|| *arg
== '\0') {
409 if (lttng_is_setuid_setgid()) {
410 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
413 config_string_set(&the_config
.apps_unix_sock_path
,
415 if (!the_config
.apps_unix_sock_path
.value
) {
420 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
421 the_config
.daemonize
= true;
422 } else if (string_match(optname
, "background") || opt
== 'b') {
423 the_config
.background
= true;
424 } else if (string_match(optname
, "group") || opt
== 'g') {
425 if (!arg
|| *arg
== '\0') {
429 if (lttng_is_setuid_setgid()) {
430 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
433 config_string_set(&the_config
.tracing_group_name
,
435 if (!the_config
.tracing_group_name
.value
) {
440 } else if (string_match(optname
, "help") || opt
== 'h') {
441 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
443 ERR("Cannot show --help for `lttng-sessiond`");
446 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
447 } else if (string_match(optname
, "version") || opt
== 'V') {
448 opt_print_version
= 1;
449 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
450 the_config
.sig_parent
= true;
451 } else if (string_match(optname
, "kconsumerd-err-sock")) {
452 if (!arg
|| *arg
== '\0') {
456 if (lttng_is_setuid_setgid()) {
457 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
458 "--kconsumerd-err-sock");
461 &the_config
.kconsumerd_err_unix_sock_path
,
463 if (!the_config
.kconsumerd_err_unix_sock_path
.value
) {
468 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
469 if (!arg
|| *arg
== '\0') {
473 if (lttng_is_setuid_setgid()) {
474 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
475 "--kconsumerd-cmd-sock");
478 &the_config
.kconsumerd_cmd_unix_sock_path
,
480 if (!the_config
.kconsumerd_cmd_unix_sock_path
.value
) {
485 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
486 if (!arg
|| *arg
== '\0') {
490 if (lttng_is_setuid_setgid()) {
491 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
492 "--ustconsumerd64-err-sock");
495 &the_config
.consumerd64_err_unix_sock_path
,
497 if (!the_config
.consumerd64_err_unix_sock_path
.value
) {
502 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
503 if (!arg
|| *arg
== '\0') {
507 if (lttng_is_setuid_setgid()) {
508 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
509 "--ustconsumerd64-cmd-sock");
512 &the_config
.consumerd64_cmd_unix_sock_path
,
514 if (!the_config
.consumerd64_cmd_unix_sock_path
.value
) {
519 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
520 if (!arg
|| *arg
== '\0') {
524 if (lttng_is_setuid_setgid()) {
525 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
526 "--ustconsumerd32-err-sock");
529 &the_config
.consumerd32_err_unix_sock_path
,
531 if (!the_config
.consumerd32_err_unix_sock_path
.value
) {
536 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
537 if (!arg
|| *arg
== '\0') {
541 if (lttng_is_setuid_setgid()) {
542 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
543 "--ustconsumerd32-cmd-sock");
546 &the_config
.consumerd32_cmd_unix_sock_path
,
548 if (!the_config
.consumerd32_cmd_unix_sock_path
.value
) {
553 } else if (string_match(optname
, "no-kernel")) {
554 the_config
.no_kernel
= true;
555 } else if (string_match(optname
, "quiet") || opt
== 'q') {
556 the_config
.quiet
= true;
557 } else if (string_match(optname
, "verbose") || opt
== 'v') {
558 /* Verbose level can increase using multiple -v */
560 /* Value obtained from config file */
561 the_config
.verbose
= config_parse_value(arg
);
563 /* -v used on command line */
564 the_config
.verbose
++;
566 /* Clamp value to [0, 3] */
567 the_config
.verbose
= the_config
.verbose
< 0 ?
569 (the_config
.verbose
<= 3 ? the_config
.verbose
:
571 } else if (string_match(optname
, "verbose-consumer")) {
573 the_config
.verbose_consumer
= config_parse_value(arg
);
575 the_config
.verbose_consumer
++;
577 } else if (string_match(optname
, "consumerd32-path")) {
578 if (!arg
|| *arg
== '\0') {
582 if (lttng_is_setuid_setgid()) {
583 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
584 "--consumerd32-path");
586 config_string_set(&the_config
.consumerd32_bin_path
,
588 if (!the_config
.consumerd32_bin_path
.value
) {
593 } else if (string_match(optname
, "consumerd32-libdir")) {
594 if (!arg
|| *arg
== '\0') {
598 if (lttng_is_setuid_setgid()) {
599 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
600 "--consumerd32-libdir");
602 config_string_set(&the_config
.consumerd32_lib_dir
,
604 if (!the_config
.consumerd32_lib_dir
.value
) {
609 } else if (string_match(optname
, "consumerd64-path")) {
610 if (!arg
|| *arg
== '\0') {
614 if (lttng_is_setuid_setgid()) {
615 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
616 "--consumerd64-path");
618 config_string_set(&the_config
.consumerd64_bin_path
,
620 if (!the_config
.consumerd64_bin_path
.value
) {
625 } else if (string_match(optname
, "consumerd64-libdir")) {
626 if (!arg
|| *arg
== '\0') {
630 if (lttng_is_setuid_setgid()) {
631 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
632 "--consumerd64-libdir");
634 config_string_set(&the_config
.consumerd64_lib_dir
,
636 if (!the_config
.consumerd64_lib_dir
.value
) {
641 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
642 if (!arg
|| *arg
== '\0') {
646 if (lttng_is_setuid_setgid()) {
647 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
651 &the_config
.pid_file_path
, strdup(arg
));
652 if (!the_config
.pid_file_path
.value
) {
657 } else if (string_match(optname
, "agent-tcp-port")) {
658 if (!arg
|| *arg
== '\0') {
662 if (lttng_is_setuid_setgid()) {
663 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
669 v
= strtoul(arg
, NULL
, 0);
670 if (errno
!= 0 || !isdigit(arg
[0])) {
671 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
674 if (v
== 0 || v
>= 65535) {
675 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
678 the_config
.agent_tcp_port
.begin
=
679 the_config
.agent_tcp_port
.end
= (int) v
;
680 DBG3("Agent TCP port set to non default: %i", (int) v
);
682 } else if (string_match(optname
, "load") || opt
== 'l') {
683 if (!arg
|| *arg
== '\0') {
687 if (lttng_is_setuid_setgid()) {
688 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
691 config_string_set(&the_config
.load_session_path
,
693 if (!the_config
.load_session_path
.value
) {
698 } else if (string_match(optname
, "kmod-probes")) {
699 if (!arg
|| *arg
== '\0') {
703 if (lttng_is_setuid_setgid()) {
704 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
707 config_string_set(&the_config
.kmod_probes_list
,
709 if (!the_config
.kmod_probes_list
.value
) {
714 } else if (string_match(optname
, "extra-kmod-probes")) {
715 if (!arg
|| *arg
== '\0') {
719 if (lttng_is_setuid_setgid()) {
720 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
721 "--extra-kmod-probes");
723 config_string_set(&the_config
.kmod_extra_probes_list
,
725 if (!the_config
.kmod_extra_probes_list
.value
) {
730 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
)) {
734 v
= strtoul(arg
, NULL
, 0);
735 if (errno
!= 0 || !isdigit(arg
[0])) {
736 ERR("Wrong value in --%s parameter: %s",
737 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
740 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
741 ERR("Value out of range for --%s parameter: %s",
742 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
745 the_config
.event_notifier_buffer_size_kernel
= (int) v
;
746 DBG3("Number of event notifier error buffer kernel size to non default: %i",
747 the_config
.event_notifier_buffer_size_kernel
);
749 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
)) {
753 v
= strtoul(arg
, NULL
, 0);
754 if (errno
!= 0 || !isdigit(arg
[0])) {
755 ERR("Wrong value in --%s parameter: %s",
756 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
759 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
760 ERR("Value out of range for --%s parameter: %s",
761 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
764 the_config
.event_notifier_buffer_size_userspace
= (int) v
;
765 DBG3("Number of event notifier error buffer userspace size to non default: %i",
766 the_config
.event_notifier_buffer_size_userspace
);
768 } else if (string_match(optname
, "config") || opt
== 'f') {
769 /* This is handled in set_options() thus silent skip. */
772 /* Unknown option or other error.
773 * Error is printed by getopt, just return */
778 if (ret
== -EINVAL
) {
779 const char *opt_name
= "unknown";
782 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
784 if (opt
== long_options
[i
].val
) {
785 opt_name
= long_options
[i
].name
;
790 WARN("Invalid argument provided for option \"%s\", using default value.",
798 * config_entry_handler_cb used to handle options read from a config file.
799 * See config_entry_handler_cb comment in common/config/session-config.h for the
800 * return value conventions.
802 static int config_entry_handler(const struct config_entry
*entry
,
803 void *unused
__attribute__((unused
)))
807 if (!entry
|| !entry
->name
|| !entry
->value
) {
812 /* Check if the option is to be ignored */
813 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
814 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
819 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
822 /* Ignore if not fully matched. */
823 if (strcmp(entry
->name
, long_options
[i
].name
)) {
828 * If the option takes no argument on the command line, we have to
829 * check if the value is "true". We support non-zero numeric values,
832 if (!long_options
[i
].has_arg
) {
833 ret
= config_parse_value(entry
->value
);
836 WARN("Invalid configuration value \"%s\" for option %s",
837 entry
->value
, entry
->name
);
839 /* False, skip boolean config option. */
844 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
848 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
854 static void print_version(void) {
855 fprintf(stdout
, "%s\n", VERSION
);
859 * daemon configuration loading and argument parsing
861 static int set_options(int argc
, char **argv
)
863 int ret
= 0, c
= 0, option_index
= 0;
864 int orig_optopt
= optopt
, orig_optind
= optind
;
866 char *config_path
= NULL
;
868 optstring
= utils_generate_optstring(long_options
,
869 sizeof(long_options
) / sizeof(struct option
));
875 /* Check for the --config option */
876 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
877 &option_index
)) != -1) {
881 } else if (c
!= 'f') {
882 /* if not equal to --config option. */
886 if (lttng_is_setuid_setgid()) {
887 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
891 config_path
= utils_expand_path(optarg
);
893 ERR("Failed to resolve path: %s", optarg
);
898 ret
= config_get_section_entries(config_path
, config_section_name
,
899 config_entry_handler
, NULL
);
902 ERR("Invalid configuration option at line %i", ret
);
908 /* Reset getopt's global state */
909 optopt
= orig_optopt
;
910 optind
= orig_optind
;
914 * getopt_long() will not set option_index if it encounters a
917 c
= getopt_long(argc
, argv
, optstring
, long_options
,
924 * Pass NULL as the long option name if popt left the index
927 ret
= set_option(c
, optarg
,
928 option_index
< 0 ? NULL
:
929 long_options
[option_index
].name
);
942 * Create lockfile using the rundir and return its fd.
944 static int create_lockfile(void)
946 return utils_create_lock_file(the_config
.lock_file_path
.value
);
950 * Check if the global socket is available, and if a daemon is answering at the
951 * other side. If yes, error is returned.
953 * Also attempts to create and hold the lock file.
955 static int check_existing_daemon(void)
959 /* Is there anybody out there ? */
960 if (lttng_session_daemon_alive()) {
965 lockfile_fd
= create_lockfile();
966 if (lockfile_fd
< 0) {
974 static void sessiond_cleanup_lock_file(void)
979 * Cleanup lock file by deleting it and finaly closing it which will
980 * release the file system lock.
982 if (lockfile_fd
>= 0) {
983 ret
= remove(the_config
.lock_file_path
.value
);
985 PERROR("remove lock file");
987 ret
= close(lockfile_fd
);
989 PERROR("close lock file");
995 * Set the tracing group gid onto the client socket.
997 * Race window between mkdir and chown is OK because we are going from more
998 * permissive (root.root) to less permissive (root.tracing).
1000 static int set_permissions(char *rundir
)
1005 ret
= utils_get_group_id(
1006 the_config
.tracing_group_name
.value
, true, &gid
);
1008 /* Default to root group. */
1012 /* Set lttng run dir */
1013 ret
= chown(rundir
, 0, gid
);
1015 ERR("Unable to set group on %s", rundir
);
1020 * Ensure all applications and tracing group can search the run
1021 * dir. Allow everyone to read the directory, since it does not
1022 * buy us anything to hide its content.
1024 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1026 ERR("Unable to set permissions on %s", rundir
);
1030 /* lttng client socket path */
1031 ret
= chown(the_config
.client_unix_sock_path
.value
, 0, gid
);
1033 ERR("Unable to set group on %s",
1034 the_config
.client_unix_sock_path
.value
);
1038 /* kconsumer error socket path */
1039 ret
= chown(the_kconsumer_data
.err_unix_sock_path
, 0, 0);
1041 ERR("Unable to set group on %s",
1042 the_kconsumer_data
.err_unix_sock_path
);
1046 /* 64-bit ustconsumer error socket path */
1047 ret
= chown(the_ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1049 ERR("Unable to set group on %s",
1050 the_ustconsumer64_data
.err_unix_sock_path
);
1054 /* 32-bit ustconsumer compat32 error socket path */
1055 ret
= chown(the_ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1057 ERR("Unable to set group on %s",
1058 the_ustconsumer32_data
.err_unix_sock_path
);
1062 DBG("All permissions are set");
1068 * Create the lttng run directory needed for all global sockets and pipe.
1070 static int create_lttng_rundir(void)
1074 DBG3("Creating LTTng run directory: %s", the_config
.rundir
.value
);
1076 ret
= mkdir(the_config
.rundir
.value
, S_IRWXU
);
1078 if (errno
!= EEXIST
) {
1079 ERR("Unable to create %s", the_config
.rundir
.value
);
1091 * Setup sockets and directory needed by the consumerds' communication with the
1094 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1099 switch (consumer_data
->type
) {
1100 case LTTNG_CONSUMER_KERNEL
:
1101 path
= the_config
.kconsumerd_path
.value
;
1103 case LTTNG_CONSUMER64_UST
:
1104 path
= the_config
.consumerd64_path
.value
;
1106 case LTTNG_CONSUMER32_UST
:
1107 path
= the_config
.consumerd32_path
.value
;
1110 ERR("Consumer type unknown");
1116 DBG2("Creating consumer directory: %s", path
);
1118 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1119 if (ret
< 0 && errno
!= EEXIST
) {
1121 ERR("Failed to create %s", path
);
1127 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
,
1130 /* Default to root group. */
1134 ret
= chown(path
, 0, gid
);
1136 ERR("Unable to set group on %s", path
);
1142 /* Create the consumerd error unix socket */
1143 consumer_data
->err_sock
=
1144 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1145 if (consumer_data
->err_sock
< 0) {
1146 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1152 * Set the CLOEXEC flag. Return code is useless because either way, the
1155 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1157 PERROR("utils_set_fd_cloexec");
1158 /* continue anyway */
1161 /* File permission MUST be 660 */
1162 ret
= chmod(consumer_data
->err_unix_sock_path
,
1163 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1165 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1175 * Signal handler for the daemon
1177 * Simply stop all worker threads, leaving main() return gracefully after
1178 * joining all threads and calling cleanup().
1180 static void sighandler(int sig
, siginfo_t
*siginfo
,
1181 void *arg
__attribute__((unused
)))
1185 DBG("SIGINT caught");
1186 notify_main_quit_pipe();
1189 DBG("SIGTERM caught");
1190 notify_main_quit_pipe();
1193 CMM_STORE_SHARED(recv_child_signal
, 1);
1198 const char msg
[] = "Received SIGBUS, aborting program.\n";
1200 lttng_ust_handle_sigbus(siginfo
->si_addr
);
1202 * If ustctl did not catch this signal (triggering a
1203 * siglongjmp), abort the program. Otherwise, the execution
1204 * will resume from the ust-ctl call which caused this error.
1206 * The return value is ignored since the program aborts anyhow.
1208 write_ret
= write(STDERR_FILENO
, msg
, sizeof(msg
));
1218 * Setup signal handler for :
1219 * SIGINT, SIGTERM, SIGPIPE
1221 static int set_signal_handler(void)
1224 struct sigaction sa
;
1227 if ((ret
= sigemptyset(&sigset
)) < 0) {
1228 PERROR("sigemptyset");
1232 sa
.sa_mask
= sigset
;
1233 sa
.sa_flags
= SA_SIGINFO
;
1235 sa
.sa_sigaction
= sighandler
;
1236 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1237 PERROR("sigaction");
1241 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1242 PERROR("sigaction");
1246 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1247 PERROR("sigaction");
1251 if ((ret
= sigaction(SIGBUS
, &sa
, NULL
)) < 0) {
1252 PERROR("sigaction");
1257 sa
.sa_handler
= SIG_IGN
;
1258 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1259 PERROR("sigaction");
1263 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE, SIGINT, and SIGBUS");
1269 * Set open files limit to unlimited. This daemon can open a large number of
1270 * file descriptors in order to consume multiple kernel traces.
1272 static void set_ulimit(void)
1277 /* The kernel does not allow an infinite limit for open files */
1278 lim
.rlim_cur
= 65535;
1279 lim
.rlim_max
= 65535;
1281 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1283 PERROR("failed to set open files limit");
1287 static int write_pidfile(void)
1289 return utils_create_pid_file(getpid(), the_config
.pid_file_path
.value
);
1292 static int set_clock_plugin_env(void)
1295 char *env_value
= NULL
;
1297 if (!the_config
.lttng_ust_clock_plugin
.value
) {
1301 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1302 the_config
.lttng_ust_clock_plugin
.value
);
1308 ret
= putenv(env_value
);
1311 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1315 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1316 the_config
.lttng_ust_clock_plugin
.value
);
1321 static void destroy_all_sessions_and_wait(void)
1323 struct ltt_session
*session
, *tmp
;
1324 struct ltt_session_list
*session_list
;
1326 session_list
= session_get_list();
1327 DBG("Initiating destruction of all sessions");
1329 if (!session_list
) {
1333 session_lock_list();
1334 /* Initiate the destruction of all sessions. */
1335 cds_list_for_each_entry_safe(session
, tmp
,
1336 &session_list
->head
, list
) {
1337 if (!session_get(session
)) {
1341 session_lock(session
);
1342 if (session
->destroyed
) {
1343 goto unlock_session
;
1345 (void) cmd_stop_trace(session
);
1346 (void) cmd_destroy_session(
1347 session
, the_notification_thread_handle
, NULL
);
1349 session_unlock(session
);
1350 session_put(session
);
1352 session_unlock_list();
1354 /* Wait for the destruction of all sessions to complete. */
1355 DBG("Waiting for the destruction of all sessions to complete");
1356 session_list_wait_empty();
1357 DBG("Destruction of all sessions completed");
1360 static void unregister_all_triggers(void)
1362 enum lttng_error_code ret_code
;
1363 enum lttng_trigger_status trigger_status
;
1364 struct lttng_triggers
*triggers
= NULL
;
1365 unsigned int trigger_count
, i
;
1366 const struct lttng_credentials creds
= {
1367 .uid
= LTTNG_OPTIONAL_INIT_VALUE(0),
1368 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
1371 DBG("Unregistering all triggers");
1374 * List all triggers as "root" since we wish to unregister all triggers.
1376 ret_code
= notification_thread_command_list_triggers(
1377 the_notification_thread_handle
, creds
.uid
.value
,
1379 if (ret_code
!= LTTNG_OK
) {
1380 ERR("Failed to list triggers while unregistering all triggers");
1384 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
1385 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1387 for (i
= 0; i
< trigger_count
; i
++) {
1388 uid_t trigger_owner
;
1389 const char *trigger_name
;
1390 const struct lttng_trigger
*trigger
=
1391 lttng_triggers_get_at_index(triggers
, i
);
1393 LTTNG_ASSERT(trigger
);
1395 trigger_status
= lttng_trigger_get_owner_uid(
1396 trigger
, &trigger_owner
);
1397 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1399 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
1400 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
1401 trigger_name
: "(anonymous)";
1403 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1404 (int) trigger_owner
, trigger_name
);
1406 ret_code
= cmd_unregister_trigger(&creds
, trigger
,
1407 the_notification_thread_handle
);
1408 if (ret_code
!= LTTNG_OK
) {
1409 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1410 (int) trigger_owner
, trigger_name
,
1411 lttng_strerror(-ret_code
));
1412 /* Continue to unregister the remaining triggers. */
1416 lttng_triggers_destroy(triggers
);
1419 static int run_as_worker_post_fork_cleanup(void *data
)
1421 struct sessiond_config
*sessiond_config
= (struct sessiond_config
*) data
;
1423 sessiond_config_fini(sessiond_config
);
1427 static int launch_run_as_worker(const char *procname
)
1430 * Clean-up before forking the run-as worker. Any dynamically
1431 * allocated memory of which the worker is not aware will
1432 * be leaked as the process forks a run-as worker (and performs
1433 * no exec*()). The same would apply to any opened fd.
1435 return run_as_create_worker(
1436 procname
, run_as_worker_post_fork_cleanup
, &the_config
);
1439 static void sessiond_uuid_log(void)
1441 char uuid_str
[LTTNG_UUID_STR_LEN
];
1443 lttng_uuid_to_str(the_sessiond_uuid
, uuid_str
);
1444 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1450 int main(int argc
, char **argv
)
1452 int ret
= 0, retval
= 0;
1453 const char *env_app_timeout
;
1454 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1455 *ust64_channel_monitor_pipe
= NULL
,
1456 *kernel_channel_monitor_pipe
= NULL
;
1457 struct timer_thread_parameters timer_thread_parameters
;
1458 /* Rotation thread handle. */
1459 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1460 /* Queue of rotation jobs populated by the sessiond-timer. */
1461 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1462 struct lttng_thread
*client_thread
= NULL
;
1463 struct lttng_thread
*notification_thread
= NULL
;
1464 struct lttng_thread
*register_apps_thread
= NULL
;
1465 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
1467 logger_set_thread_name("Main", false);
1468 init_kernel_workarounds();
1470 rcu_register_thread();
1472 if (set_signal_handler()) {
1474 goto exit_set_signal_handler
;
1477 if (timer_signal_init()) {
1479 goto exit_set_signal_handler
;
1482 the_page_size
= sysconf(_SC_PAGE_SIZE
);
1483 if (the_page_size
< 0) {
1484 PERROR("sysconf _SC_PAGE_SIZE");
1485 the_page_size
= LONG_MAX
;
1486 WARN("Fallback page size to %ld", the_page_size
);
1489 ret
= sessiond_config_init(&the_config
);
1492 goto exit_set_signal_handler
;
1496 * Init config from environment variables.
1497 * Command line option override env configuration per-doc. Do env first.
1499 sessiond_config_apply_env_config(&the_config
);
1502 * Parse arguments and load the daemon configuration file.
1504 * We have an exit_options exit path to free memory reserved by
1508 if (set_options(argc
, argv
)) {
1514 * Resolve all paths received as arguments, configuration option, or
1515 * through environment variable as absolute paths. This is necessary
1516 * since daemonizing causes the sessiond's current working directory
1519 ret
= sessiond_config_resolve_paths(&the_config
);
1525 lttng_opt_verbose
= the_config
.verbose
;
1526 lttng_opt_quiet
= the_config
.quiet
;
1527 the_kconsumer_data
.err_unix_sock_path
=
1528 the_config
.kconsumerd_err_unix_sock_path
.value
;
1529 the_kconsumer_data
.cmd_unix_sock_path
=
1530 the_config
.kconsumerd_cmd_unix_sock_path
.value
;
1531 the_ustconsumer32_data
.err_unix_sock_path
=
1532 the_config
.consumerd32_err_unix_sock_path
.value
;
1533 the_ustconsumer32_data
.cmd_unix_sock_path
=
1534 the_config
.consumerd32_cmd_unix_sock_path
.value
;
1535 the_ustconsumer64_data
.err_unix_sock_path
=
1536 the_config
.consumerd64_err_unix_sock_path
.value
;
1537 the_ustconsumer64_data
.cmd_unix_sock_path
=
1538 the_config
.consumerd64_cmd_unix_sock_path
.value
;
1539 set_clock_plugin_env();
1541 sessiond_config_log(&the_config
);
1542 sessiond_uuid_log();
1543 lttng::logging::log_system_information(PRINT_DBG
);
1545 if (opt_print_version
) {
1551 if (create_lttng_rundir()) {
1556 /* Abort launch if a session daemon is already running. */
1557 if (check_existing_daemon()) {
1558 ERR("A session daemon is already running.");
1564 if (the_config
.daemonize
|| the_config
.background
) {
1567 ret
= lttng_daemonize(&the_child_ppid
, &recv_child_signal
,
1568 !the_config
.background
);
1575 * We are in the child. Make sure all other file descriptors are
1576 * closed, in case we are called with more opened file
1577 * descriptors than the standard ones and the lock file.
1579 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1580 if (i
== lockfile_fd
) {
1587 if (launch_run_as_worker(argv
[0]) < 0) {
1588 goto exit_create_run_as_worker_cleanup
;
1592 * Starting from here, we can create threads. This needs to be after
1593 * lttng_daemonize due to RCU.
1597 * Initialize the health check subsystem. This call should set the
1598 * appropriate time values.
1600 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1601 if (!the_health_sessiond
) {
1602 PERROR("health_app_create error");
1607 /* Create main quit pipe */
1608 if (sessiond_init_main_quit_pipe()) {
1613 /* Check if daemon is UID = 0 */
1614 is_root
= !getuid();
1616 /* Create global run dir with root access */
1618 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1619 if (!kernel_channel_monitor_pipe
) {
1620 ERR("Failed to create kernel consumer channel monitor pipe");
1624 the_kconsumer_data
.channel_monitor_pipe
=
1625 lttng_pipe_release_writefd(
1626 kernel_channel_monitor_pipe
);
1627 if (the_kconsumer_data
.channel_monitor_pipe
< 0) {
1633 /* Set consumer initial state */
1634 the_kernel_consumerd_state
= CONSUMER_STOPPED
;
1635 the_ust_consumerd_state
= CONSUMER_STOPPED
;
1637 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1638 if (!ust32_channel_monitor_pipe
) {
1639 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1643 the_ustconsumer32_data
.channel_monitor_pipe
=
1644 lttng_pipe_release_writefd(ust32_channel_monitor_pipe
);
1645 if (the_ustconsumer32_data
.channel_monitor_pipe
< 0) {
1651 * The rotation_thread_timer_queue structure is shared between the
1652 * sessiond timer thread and the rotation thread. The main thread keeps
1653 * its ownership and destroys it when both threads have been joined.
1655 rotation_timer_queue
= rotation_thread_timer_queue_create();
1656 if (!rotation_timer_queue
) {
1660 timer_thread_parameters
.rotation_thread_job_queue
=
1661 rotation_timer_queue
;
1663 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1664 if (!ust64_channel_monitor_pipe
) {
1665 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1669 the_ustconsumer64_data
.channel_monitor_pipe
=
1670 lttng_pipe_release_writefd(ust64_channel_monitor_pipe
);
1671 if (the_ustconsumer64_data
.channel_monitor_pipe
< 0) {
1677 * Init UST app hash table. Alloc hash table before this point since
1678 * cleanup() can get called after that point.
1680 if (ust_app_ht_alloc()) {
1681 ERR("Failed to allocate UST app hash table");
1686 event_notifier_error_accounting_status
= event_notifier_error_accounting_init(
1687 the_config
.event_notifier_buffer_size_kernel
,
1688 the_config
.event_notifier_buffer_size_userspace
);
1689 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1690 ERR("Failed to initialize event notifier error accounting system");
1696 * Initialize agent app hash table. We allocate the hash table here
1697 * since cleanup() can get called after this point.
1699 if (agent_app_ht_alloc()) {
1700 ERR("Failed to allocate Agent app hash table");
1705 if (agent_by_event_notifier_domain_ht_create()) {
1706 ERR("Failed to allocate per-event notifier domain agent hash table");
1711 * These actions must be executed as root. We do that *after* setting up
1712 * the sockets path because we MUST make the check for another daemon using
1713 * those paths *before* trying to set the kernel consumer sockets and init
1717 if (set_consumer_sockets(&the_kconsumer_data
)) {
1722 /* Setup kernel tracer */
1723 if (!the_config
.no_kernel
) {
1724 init_kernel_tracer();
1727 /* Set ulimit for open files */
1730 /* init lttng_fd tracking must be done after set_ulimit. */
1733 if (set_consumer_sockets(&the_ustconsumer64_data
)) {
1738 if (set_consumer_sockets(&the_ustconsumer32_data
)) {
1743 /* Get parent pid if -S, --sig-parent is specified. */
1744 if (the_config
.sig_parent
) {
1745 the_ppid
= getppid();
1748 /* Setup the kernel pipe for waking up the kernel thread */
1749 if (is_root
&& !the_config
.no_kernel
) {
1750 if (utils_create_pipe_cloexec(the_kernel_poll_pipe
)) {
1756 /* Setup the thread apps communication pipe. */
1757 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1762 /* Setup the thread apps notify communication pipe. */
1763 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1768 /* Initialize global buffer per UID and PID registry. */
1769 buffer_reg_init_uid_registry();
1770 buffer_reg_init_pid_registry();
1772 /* Init UST command queue. */
1773 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1777 /* Check for the application socket timeout env variable. */
1778 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1779 if (env_app_timeout
) {
1780 the_config
.app_socket_timeout
= atoi(env_app_timeout
);
1782 the_config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1785 ret
= write_pidfile();
1787 ERR("Error in write_pidfile");
1792 /* Initialize communication library */
1794 /* Initialize TCP timeout values */
1795 lttcomm_inet_init();
1797 /* Create health-check thread. */
1798 if (!launch_health_management_thread()) {
1803 /* notification_thread_data acquires the pipes' read side. */
1804 the_notification_thread_handle
= notification_thread_handle_create(
1805 ust32_channel_monitor_pipe
, ust64_channel_monitor_pipe
,
1806 kernel_channel_monitor_pipe
);
1807 if (!the_notification_thread_handle
) {
1809 ERR("Failed to create notification thread shared data");
1813 /* Create notification thread. */
1814 notification_thread
= launch_notification_thread(
1815 the_notification_thread_handle
);
1816 if (!notification_thread
) {
1821 /* Create timer thread. */
1822 if (!launch_timer_thread(&timer_thread_parameters
)) {
1827 /* rotation_thread_data acquires the pipes' read side. */
1828 rotation_thread_handle
= rotation_thread_handle_create(
1829 rotation_timer_queue
, the_notification_thread_handle
);
1830 if (!rotation_thread_handle
) {
1832 ERR("Failed to create rotation thread shared data");
1836 /* Create rotation thread. */
1837 if (!launch_rotation_thread(rotation_thread_handle
)) {
1842 /* Create thread to manage the client socket */
1843 client_thread
= launch_client_thread();
1844 if (!client_thread
) {
1849 /* Set credentials of the client socket and rundir */
1850 if (is_root
&& set_permissions(the_config
.rundir
.value
)) {
1855 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1856 apps_cmd_notify_pipe
[1])) {
1861 /* Create thread to manage application registration. */
1862 register_apps_thread
= launch_application_registration_thread(
1864 if (!register_apps_thread
) {
1869 /* Create thread to manage application socket */
1870 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1875 /* Create thread to manage application notify socket */
1876 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1881 /* Create agent management thread. */
1882 if (!launch_agent_management_thread()) {
1887 /* Don't start this thread if kernel tracing is not requested nor root */
1888 if (is_root
&& !the_config
.no_kernel
) {
1889 /* Create kernel thread to manage kernel event */
1890 if (!launch_kernel_management_thread(the_kernel_poll_pipe
[0])) {
1895 if (kernel_get_notification_fd() >= 0) {
1896 ret
= notification_thread_command_add_tracer_event_source(
1897 the_notification_thread_handle
,
1898 kernel_get_notification_fd(),
1899 LTTNG_DOMAIN_KERNEL
);
1900 if (ret
!= LTTNG_OK
) {
1901 ERR("Failed to add kernel trigger event source to notification thread");
1908 /* Load sessions. */
1909 ret
= config_load_session(
1910 the_config
.load_session_path
.value
, NULL
, 1, 1, NULL
);
1912 ERR("Session load failed: %s", error_get_str(ret
));
1917 /* Initialization completed. */
1918 sessiond_signal_parents();
1921 * This is where we start awaiting program completion (e.g. through
1922 * signal that asks threads to teardown).
1925 /* Initiate teardown once activity occurs on the main quit pipe. */
1926 sessiond_wait_for_main_quit_pipe(-1);
1930 DBG("Terminating all threads");
1933 * Ensure that the client thread is no longer accepting new commands,
1934 * which could cause new sessions to be created.
1936 if (client_thread
) {
1937 lttng_thread_shutdown(client_thread
);
1938 lttng_thread_put(client_thread
);
1941 destroy_all_sessions_and_wait();
1944 * At this point no new trigger can be registered (no sessions are
1945 * running/rotating) and clients can't connect to the session daemon
1946 * anymore. Unregister all triggers.
1948 unregister_all_triggers();
1950 if (register_apps_thread
) {
1951 lttng_thread_shutdown(register_apps_thread
);
1952 lttng_thread_put(register_apps_thread
);
1954 lttng_thread_list_shutdown_orphans();
1957 * Wait for all pending call_rcu work to complete before tearing
1958 * down data structures. call_rcu worker may be trying to
1959 * perform lookups in those structures.
1963 rcu_thread_online();
1967 * Wait for all pending call_rcu work to complete before shutting down
1968 * the notification thread. This call_rcu work includes shutting down
1969 * UST apps and event notifier pipes.
1973 if (notification_thread
) {
1974 lttng_thread_shutdown(notification_thread
);
1975 lttng_thread_put(notification_thread
);
1979 * Error accounting teardown has to be done after the teardown of all
1980 * event notifier pipes to ensure that no tracer may try to use the
1981 * error accounting facilities.
1983 event_notifier_error_accounting_fini();
1986 * Unloading the kernel modules needs to be done after all kernel
1987 * ressources have been released. In our case, this includes the
1988 * notification fd, the event notifier group fd, error accounting fd,
1989 * all event and event notifier fds, etc.
1991 * In short, at this point, we need to have called close() on all fds
1992 * received from the kernel tracer.
1994 if (is_root
&& !the_config
.no_kernel
) {
1995 DBG("Unloading kernel modules");
1996 modprobe_remove_lttng_all();
1999 rcu_thread_offline();
2000 rcu_unregister_thread();
2002 if (rotation_thread_handle
) {
2003 rotation_thread_handle_destroy(rotation_thread_handle
);
2007 * After the rotation and timer thread have quit, we can safely destroy
2008 * the rotation_timer_queue.
2010 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
2012 * The teardown of the notification system is performed after the
2013 * session daemon's teardown in order to allow it to be notified
2014 * of the active session and channels at the moment of the teardown.
2016 if (the_notification_thread_handle
) {
2017 notification_thread_handle_destroy(
2018 the_notification_thread_handle
);
2020 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
2021 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
2022 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
2024 if (the_health_sessiond
) {
2025 health_app_destroy(the_health_sessiond
);
2027 exit_create_run_as_worker_cleanup
:
2029 sessiond_cleanup_lock_file();
2030 sessiond_cleanup_options();
2032 exit_set_signal_handler
: