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
11 #include "agent-thread.hpp"
13 #include "buffer-registry.hpp"
14 #include "channel.hpp"
17 #include "consumer.hpp"
18 #include "context.hpp"
19 #include "dispatch.hpp"
20 #include "event-notifier-error-accounting.hpp"
22 #include "fd-limit.hpp"
23 #include "health-sessiond.hpp"
24 #include "kernel-consumer.hpp"
26 #include "lttng-sessiond.hpp"
27 #include "lttng-ust-ctl.hpp"
28 #include "manage-apps.hpp"
29 #include "manage-kernel.hpp"
30 #include "modprobe.hpp"
31 #include "notification-thread-commands.hpp"
32 #include "notification-thread.hpp"
33 #include "notify-apps.hpp"
34 #include "register.hpp"
35 #include "rotation-thread.hpp"
37 #include "sessiond-config.hpp"
38 #include "testpoint.hpp"
41 #include "ust-consumer.hpp"
42 #include "ust-sigbus.hpp"
45 #include <common/common.hpp>
46 #include <common/compat/getenv.hpp>
47 #include <common/compat/socket.hpp>
48 #include <common/config/session-config.hpp>
49 #include <common/daemonize.hpp>
50 #include <common/defaults.hpp>
51 #include <common/dynamic-buffer.hpp>
52 #include <common/futex.hpp>
53 #include <common/ini-config/ini-config.hpp>
54 #include <common/kernel-consumer/kernel-consumer.hpp>
55 #include <common/lockfile.hpp>
56 #include <common/logging-utils.hpp>
57 #include <common/path.hpp>
58 #include <common/relayd/relayd.hpp>
59 #include <common/utils.hpp>
61 #include <lttng/event-internal.hpp>
75 #include <sys/mount.h>
76 #include <sys/resource.h>
77 #include <sys/socket.h>
79 #include <sys/types.h>
82 #include <urcu/uatomic.h>
84 static const char *help_msg
=
85 #ifdef LTTNG_EMBED_HELP
86 #include <lttng-sessiond.8.h>
92 #define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
93 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "event-notifier-error-buffer-size"
94 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR \
95 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-kernel"
96 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR \
97 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
, nullptr, 'c' },
109 { "apps-sock", required_argument
, nullptr, 'a' },
110 { "kconsumerd-cmd-sock", required_argument
, nullptr, '\0' },
111 { "kconsumerd-err-sock", required_argument
, nullptr, '\0' },
112 { "ustconsumerd32-cmd-sock", required_argument
, nullptr, '\0' },
113 { "ustconsumerd32-err-sock", required_argument
, nullptr, '\0' },
114 { "ustconsumerd64-cmd-sock", required_argument
, nullptr, '\0' },
115 { "ustconsumerd64-err-sock", required_argument
, nullptr, '\0' },
116 { "consumerd32-path", required_argument
, nullptr, '\0' },
117 { "consumerd32-libdir", required_argument
, nullptr, '\0' },
118 { "consumerd64-path", required_argument
, nullptr, '\0' },
119 { "consumerd64-libdir", required_argument
, nullptr, '\0' },
120 { "daemonize", no_argument
, nullptr, 'd' },
121 { "background", no_argument
, nullptr, 'b' },
122 { "sig-parent", no_argument
, nullptr, 'S' },
123 { "help", no_argument
, nullptr, 'h' },
124 { "group", required_argument
, nullptr, 'g' },
125 { "version", no_argument
, nullptr, 'V' },
126 { "quiet", no_argument
, nullptr, 'q' },
127 { "verbose", no_argument
, nullptr, 'v' },
128 { "verbose-consumer", no_argument
, nullptr, '\0' },
129 { "no-kernel", no_argument
, nullptr, '\0' },
130 { "pidfile", required_argument
, nullptr, 'p' },
131 { "agent-tcp-port", required_argument
, nullptr, '\0' },
132 { "config", required_argument
, nullptr, 'f' },
133 { "load", required_argument
, nullptr, 'l' },
134 { "kmod-probes", required_argument
, nullptr, '\0' },
135 { "extra-kmod-probes", required_argument
, nullptr, '\0' },
136 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, required_argument
, nullptr, '\0' },
137 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, required_argument
, nullptr, '\0' },
138 { nullptr, 0, nullptr, 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()
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()
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)", consumer_data
->pid
);
265 ret
= waitpid(consumer_data
->pid
, &status
, 0);
267 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
268 } else if (!WIFEXITED(status
)) {
269 ERR("consumerd termination with error: %d", WEXITSTATUS(ret
));
271 consumer_data
->pid
= 0;
275 * Cleanup the session daemon's data structures.
277 static void sessiond_cleanup()
281 DBG("Cleanup sessiond");
284 * Close the main quit pipe. It has already done its job, since we are
287 sessiond_close_main_quit_pipe();
289 /* Close all other pipes. */
290 utils_close_pipe(apps_cmd_pipe
);
291 utils_close_pipe(apps_cmd_notify_pipe
);
292 utils_close_pipe(the_kernel_poll_pipe
);
294 ret
= remove(the_config
.pid_file_path
.value
);
296 PERROR("remove pidfile %s", the_config
.pid_file_path
.value
);
299 DBG("Removing sessiond and consumerd content of directory %s", the_config
.rundir
.value
);
302 DBG("Removing %s", the_config
.pid_file_path
.value
);
303 (void) unlink(the_config
.pid_file_path
.value
);
305 DBG("Removing %s", the_config
.agent_port_file_path
.value
);
306 (void) unlink(the_config
.agent_port_file_path
.value
);
309 DBG("Removing %s", the_kconsumer_data
.err_unix_sock_path
);
310 (void) unlink(the_kconsumer_data
.err_unix_sock_path
);
312 DBG("Removing directory %s", the_config
.kconsumerd_path
.value
);
313 (void) rmdir(the_config
.kconsumerd_path
.value
);
315 /* ust consumerd 32 */
316 DBG("Removing %s", the_config
.consumerd32_err_unix_sock_path
.value
);
317 (void) unlink(the_config
.consumerd32_err_unix_sock_path
.value
);
319 DBG("Removing directory %s", the_config
.consumerd32_path
.value
);
320 (void) rmdir(the_config
.consumerd32_path
.value
);
322 /* ust consumerd 64 */
323 DBG("Removing %s", the_config
.consumerd64_err_unix_sock_path
.value
);
324 (void) unlink(the_config
.consumerd64_err_unix_sock_path
.value
);
326 DBG("Removing directory %s", the_config
.consumerd64_path
.value
);
327 (void) rmdir(the_config
.consumerd64_path
.value
);
329 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()
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
, strdup(arg
));
392 if (!the_config
.client_unix_sock_path
.value
) {
397 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
398 if (!arg
|| *arg
== '\0') {
402 if (lttng_is_setuid_setgid()) {
403 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
406 config_string_set(&the_config
.apps_unix_sock_path
, strdup(arg
));
407 if (!the_config
.apps_unix_sock_path
.value
) {
412 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
413 the_config
.daemonize
= true;
414 } else if (string_match(optname
, "background") || opt
== 'b') {
415 the_config
.background
= true;
416 } else if (string_match(optname
, "group") || opt
== 'g') {
417 if (!arg
|| *arg
== '\0') {
421 if (lttng_is_setuid_setgid()) {
422 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
425 config_string_set(&the_config
.tracing_group_name
, strdup(arg
));
426 if (!the_config
.tracing_group_name
.value
) {
431 } else if (string_match(optname
, "help") || opt
== 'h') {
432 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
434 ERR("Cannot show --help for `lttng-sessiond`");
437 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
438 } else if (string_match(optname
, "version") || opt
== 'V') {
439 opt_print_version
= 1;
440 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
441 the_config
.sig_parent
= true;
442 } else if (string_match(optname
, "kconsumerd-err-sock")) {
443 if (!arg
|| *arg
== '\0') {
447 if (lttng_is_setuid_setgid()) {
448 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
449 "--kconsumerd-err-sock");
451 config_string_set(&the_config
.kconsumerd_err_unix_sock_path
, strdup(arg
));
452 if (!the_config
.kconsumerd_err_unix_sock_path
.value
) {
457 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
458 if (!arg
|| *arg
== '\0') {
462 if (lttng_is_setuid_setgid()) {
463 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
464 "--kconsumerd-cmd-sock");
466 config_string_set(&the_config
.kconsumerd_cmd_unix_sock_path
, strdup(arg
));
467 if (!the_config
.kconsumerd_cmd_unix_sock_path
.value
) {
472 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
473 if (!arg
|| *arg
== '\0') {
477 if (lttng_is_setuid_setgid()) {
478 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
479 "--ustconsumerd64-err-sock");
481 config_string_set(&the_config
.consumerd64_err_unix_sock_path
, strdup(arg
));
482 if (!the_config
.consumerd64_err_unix_sock_path
.value
) {
487 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
488 if (!arg
|| *arg
== '\0') {
492 if (lttng_is_setuid_setgid()) {
493 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
494 "--ustconsumerd64-cmd-sock");
496 config_string_set(&the_config
.consumerd64_cmd_unix_sock_path
, strdup(arg
));
497 if (!the_config
.consumerd64_cmd_unix_sock_path
.value
) {
502 } else if (string_match(optname
, "ustconsumerd32-err-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 "--ustconsumerd32-err-sock");
511 config_string_set(&the_config
.consumerd32_err_unix_sock_path
, strdup(arg
));
512 if (!the_config
.consumerd32_err_unix_sock_path
.value
) {
517 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
518 if (!arg
|| *arg
== '\0') {
522 if (lttng_is_setuid_setgid()) {
523 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
524 "--ustconsumerd32-cmd-sock");
526 config_string_set(&the_config
.consumerd32_cmd_unix_sock_path
, strdup(arg
));
527 if (!the_config
.consumerd32_cmd_unix_sock_path
.value
) {
532 } else if (string_match(optname
, "no-kernel")) {
533 the_config
.no_kernel
= true;
534 } else if (string_match(optname
, "quiet") || opt
== 'q') {
535 the_config
.quiet
= true;
536 } else if (string_match(optname
, "verbose") || opt
== 'v') {
537 /* Verbose level can increase using multiple -v */
539 /* Value obtained from config file */
540 the_config
.verbose
= config_parse_value(arg
);
542 /* -v used on command line */
543 the_config
.verbose
++;
545 /* Clamp value to [0, 3] */
546 the_config
.verbose
= the_config
.verbose
< 0 ?
548 (the_config
.verbose
<= 3 ? the_config
.verbose
: 3);
549 } else if (string_match(optname
, "verbose-consumer")) {
551 the_config
.verbose_consumer
= config_parse_value(arg
);
553 the_config
.verbose_consumer
++;
555 } else if (string_match(optname
, "consumerd32-path")) {
556 if (!arg
|| *arg
== '\0') {
560 if (lttng_is_setuid_setgid()) {
561 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
562 "--consumerd32-path");
564 config_string_set(&the_config
.consumerd32_bin_path
, strdup(arg
));
565 if (!the_config
.consumerd32_bin_path
.value
) {
570 } else if (string_match(optname
, "consumerd32-libdir")) {
571 if (!arg
|| *arg
== '\0') {
575 if (lttng_is_setuid_setgid()) {
576 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
577 "--consumerd32-libdir");
579 config_string_set(&the_config
.consumerd32_lib_dir
, strdup(arg
));
580 if (!the_config
.consumerd32_lib_dir
.value
) {
585 } else if (string_match(optname
, "consumerd64-path")) {
586 if (!arg
|| *arg
== '\0') {
590 if (lttng_is_setuid_setgid()) {
591 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
592 "--consumerd64-path");
594 config_string_set(&the_config
.consumerd64_bin_path
, strdup(arg
));
595 if (!the_config
.consumerd64_bin_path
.value
) {
600 } else if (string_match(optname
, "consumerd64-libdir")) {
601 if (!arg
|| *arg
== '\0') {
605 if (lttng_is_setuid_setgid()) {
606 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
607 "--consumerd64-libdir");
609 config_string_set(&the_config
.consumerd64_lib_dir
, strdup(arg
));
610 if (!the_config
.consumerd64_lib_dir
.value
) {
615 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
616 if (!arg
|| *arg
== '\0') {
620 if (lttng_is_setuid_setgid()) {
621 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
624 config_string_set(&the_config
.pid_file_path
, strdup(arg
));
625 if (!the_config
.pid_file_path
.value
) {
630 } else if (string_match(optname
, "agent-tcp-port")) {
631 if (!arg
|| *arg
== '\0') {
635 if (lttng_is_setuid_setgid()) {
636 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
642 v
= strtoul(arg
, nullptr, 0);
643 if (errno
!= 0 || !isdigit(arg
[0])) {
644 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
647 if (v
== 0 || v
>= 65535) {
648 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
651 the_config
.agent_tcp_port
.begin
= the_config
.agent_tcp_port
.end
= (int) v
;
652 DBG3("Agent TCP port set to non default: %i", (int) v
);
654 } else if (string_match(optname
, "load") || opt
== 'l') {
655 if (!arg
|| *arg
== '\0') {
659 if (lttng_is_setuid_setgid()) {
660 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
663 config_string_set(&the_config
.load_session_path
, strdup(arg
));
664 if (!the_config
.load_session_path
.value
) {
669 } else if (string_match(optname
, "kmod-probes")) {
670 if (!arg
|| *arg
== '\0') {
674 if (lttng_is_setuid_setgid()) {
675 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
678 config_string_set(&the_config
.kmod_probes_list
, strdup(arg
));
679 if (!the_config
.kmod_probes_list
.value
) {
684 } else if (string_match(optname
, "extra-kmod-probes")) {
685 if (!arg
|| *arg
== '\0') {
689 if (lttng_is_setuid_setgid()) {
690 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
691 "--extra-kmod-probes");
693 config_string_set(&the_config
.kmod_extra_probes_list
, strdup(arg
));
694 if (!the_config
.kmod_extra_probes_list
.value
) {
699 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
)) {
703 v
= strtoul(arg
, nullptr, 0);
704 if (errno
!= 0 || !isdigit(arg
[0])) {
705 ERR("Wrong value in --%s parameter: %s",
706 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
,
710 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
711 ERR("Value out of range for --%s parameter: %s",
712 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
,
716 the_config
.event_notifier_buffer_size_kernel
= (int) v
;
717 DBG3("Number of event notifier error buffer kernel size to non default: %i",
718 the_config
.event_notifier_buffer_size_kernel
);
720 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
)) {
724 v
= strtoul(arg
, nullptr, 0);
725 if (errno
!= 0 || !isdigit(arg
[0])) {
726 ERR("Wrong value in --%s parameter: %s",
727 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
,
731 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
732 ERR("Value out of range for --%s parameter: %s",
733 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
,
737 the_config
.event_notifier_buffer_size_userspace
= (int) v
;
738 DBG3("Number of event notifier error buffer userspace size to non default: %i",
739 the_config
.event_notifier_buffer_size_userspace
);
741 } else if (string_match(optname
, "config") || opt
== 'f') {
742 /* This is handled in set_options() thus silent skip. */
745 /* Unknown option or other error.
746 * Error is printed by getopt, just return */
751 if (ret
== -EINVAL
) {
752 const char *opt_name
= "unknown";
755 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
); i
++) {
756 if (opt
== long_options
[i
].val
) {
757 opt_name
= long_options
[i
].name
;
762 WARN("Invalid argument provided for option \"%s\", using default value.", opt_name
);
769 * config_entry_handler_cb used to handle options read from a config file.
770 * See config_entry_handler_cb comment in common/config/session-config.h for the
771 * return value conventions.
773 static int config_entry_handler(const struct config_entry
*entry
,
774 void *unused
__attribute__((unused
)))
778 if (!entry
|| !entry
->name
|| !entry
->value
) {
783 /* Check if the option is to be ignored */
784 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
785 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
790 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
791 /* Ignore if not fully matched. */
792 if (strcmp(entry
->name
, long_options
[i
].name
) != 0) {
797 * If the option takes no argument on the command line, we have to
798 * check if the value is "true". We support non-zero numeric values,
801 if (!long_options
[i
].has_arg
) {
802 ret
= config_parse_value(entry
->value
);
805 WARN("Invalid configuration value \"%s\" for option %s",
809 /* False, skip boolean config option. */
814 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
818 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
824 static void print_version()
826 fprintf(stdout
, "%s\n", VERSION
);
830 * daemon configuration loading and argument parsing
832 static int set_options(int argc
, char **argv
)
834 int ret
= 0, c
= 0, option_index
= 0;
835 int orig_optopt
= optopt
, orig_optind
= optind
;
837 char *config_path
= nullptr;
839 optstring
= utils_generate_optstring(long_options
,
840 sizeof(long_options
) / sizeof(struct option
));
846 /* Check for the --config option */
847 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
)) != -1) {
851 } else if (c
!= 'f') {
852 /* if not equal to --config option. */
856 if (lttng_is_setuid_setgid()) {
857 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
861 config_path
= utils_expand_path(optarg
);
863 ERR("Failed to resolve path: %s", optarg
);
868 ret
= config_get_section_entries(
869 config_path
, config_section_name
, config_entry_handler
, nullptr);
872 ERR("Invalid configuration option at line %i", ret
);
878 /* Reset getopt's global state */
879 optopt
= orig_optopt
;
880 optind
= orig_optind
;
884 * getopt_long() will not set option_index if it encounters a
887 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
893 * Pass NULL as the long option name if popt left the index
897 c
, optarg
, option_index
< 0 ? nullptr : long_options
[option_index
].name
);
910 * Create lockfile using the rundir and return its fd.
912 static int create_lockfile()
914 return utils_create_lock_file(the_config
.lock_file_path
.value
);
918 * Check if the global socket is available, and if a daemon is answering at the
919 * other side. If yes, error is returned.
921 * Also attempts to create and hold the lock file.
923 static int check_existing_daemon()
927 /* Is there anybody out there ? */
928 if (lttng_session_daemon_alive()) {
933 lockfile_fd
= create_lockfile();
934 if (lockfile_fd
< 0) {
942 static void sessiond_cleanup_lock_file()
947 * Cleanup lock file by deleting it and finaly closing it which will
948 * release the file system lock.
950 if (lockfile_fd
>= 0) {
951 ret
= remove(the_config
.lock_file_path
.value
);
953 PERROR("remove lock file");
955 ret
= close(lockfile_fd
);
957 PERROR("close lock file");
963 * Set the tracing group gid onto the client socket.
965 * Race window between mkdir and chown is OK because we are going from more
966 * permissive (root.root) to less permissive (root.tracing).
968 static int set_permissions(char *rundir
)
973 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
, true, &gid
);
975 /* Default to root group. */
979 /* Set lttng run dir */
980 ret
= chown(rundir
, 0, gid
);
982 ERR("Unable to set group on %s", rundir
);
987 * Ensure all applications and tracing group can search the run
988 * dir. Allow everyone to read the directory, since it does not
989 * buy us anything to hide its content.
991 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
993 ERR("Unable to set permissions on %s", rundir
);
997 /* lttng client socket path */
998 ret
= chown(the_config
.client_unix_sock_path
.value
, 0, gid
);
1000 ERR("Unable to set group on %s", the_config
.client_unix_sock_path
.value
);
1004 /* kconsumer error socket path */
1005 ret
= chown(the_kconsumer_data
.err_unix_sock_path
, 0, 0);
1007 ERR("Unable to set group on %s", the_kconsumer_data
.err_unix_sock_path
);
1011 /* 64-bit ustconsumer error socket path */
1012 ret
= chown(the_ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1014 ERR("Unable to set group on %s", the_ustconsumer64_data
.err_unix_sock_path
);
1018 /* 32-bit ustconsumer compat32 error socket path */
1019 ret
= chown(the_ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1021 ERR("Unable to set group on %s", the_ustconsumer32_data
.err_unix_sock_path
);
1025 DBG("All permissions are set");
1031 * Create the lttng run directory needed for all global sockets and pipe.
1033 static int create_lttng_rundir()
1037 DBG3("Creating LTTng run directory: %s", the_config
.rundir
.value
);
1039 ret
= mkdir(the_config
.rundir
.value
, S_IRWXU
);
1041 if (errno
!= EEXIST
) {
1042 ERR("Unable to create %s", the_config
.rundir
.value
);
1054 * Setup sockets and directory needed by the consumerds' communication with the
1057 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1060 char *path
= nullptr;
1062 switch (consumer_data
->type
) {
1063 case LTTNG_CONSUMER_KERNEL
:
1064 path
= the_config
.kconsumerd_path
.value
;
1066 case LTTNG_CONSUMER64_UST
:
1067 path
= the_config
.consumerd64_path
.value
;
1069 case LTTNG_CONSUMER32_UST
:
1070 path
= the_config
.consumerd32_path
.value
;
1073 ERR("Consumer type unknown");
1079 DBG2("Creating consumer directory: %s", path
);
1081 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1082 if (ret
< 0 && errno
!= EEXIST
) {
1084 ERR("Failed to create %s", path
);
1090 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
, true, &gid
);
1092 /* Default to root group. */
1096 ret
= chown(path
, 0, gid
);
1098 ERR("Unable to set group on %s", path
);
1104 /* Create the consumerd error unix socket */
1105 consumer_data
->err_sock
= lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1106 if (consumer_data
->err_sock
< 0) {
1107 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1113 * Set the CLOEXEC flag. Return code is useless because either way, the
1116 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1118 PERROR("utils_set_fd_cloexec");
1119 /* continue anyway */
1122 /* File permission MUST be 660 */
1123 ret
= chmod(consumer_data
->err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1125 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1135 * Signal handler for the daemon
1137 * Simply stop all worker threads, leaving main() return gracefully after
1138 * joining all threads and calling cleanup().
1140 static void sighandler(int sig
, siginfo_t
*siginfo
, void *arg
__attribute__((unused
)))
1144 DBG("SIGINT caught");
1145 notify_main_quit_pipe();
1148 DBG("SIGTERM caught");
1149 notify_main_quit_pipe();
1152 CMM_STORE_SHARED(recv_child_signal
, 1);
1157 const char msg
[] = "Received SIGBUS, aborting program.\n";
1159 lttng_ust_handle_sigbus(siginfo
->si_addr
);
1161 * If ustctl did not catch this signal (triggering a
1162 * siglongjmp), abort the program. Otherwise, the execution
1163 * will resume from the ust-ctl call which caused this error.
1165 * The return value is ignored since the program aborts anyhow.
1167 write_ret
= write(STDERR_FILENO
, msg
, sizeof(msg
));
1177 * Setup signal handler for :
1178 * SIGINT, SIGTERM, SIGPIPE
1180 static int set_signal_handler()
1183 struct sigaction sa
;
1186 if ((ret
= sigemptyset(&sigset
)) < 0) {
1187 PERROR("sigemptyset");
1191 sa
.sa_mask
= sigset
;
1192 sa
.sa_flags
= SA_SIGINFO
;
1194 sa
.sa_sigaction
= sighandler
;
1195 if ((ret
= sigaction(SIGTERM
, &sa
, nullptr)) < 0) {
1196 PERROR("sigaction");
1200 if ((ret
= sigaction(SIGINT
, &sa
, nullptr)) < 0) {
1201 PERROR("sigaction");
1205 if ((ret
= sigaction(SIGUSR1
, &sa
, nullptr)) < 0) {
1206 PERROR("sigaction");
1210 if ((ret
= sigaction(SIGBUS
, &sa
, nullptr)) < 0) {
1211 PERROR("sigaction");
1216 sa
.sa_handler
= SIG_IGN
;
1217 if ((ret
= sigaction(SIGPIPE
, &sa
, nullptr)) < 0) {
1218 PERROR("sigaction");
1222 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE, SIGINT, and SIGBUS");
1228 * Set open files limit to unlimited. This daemon can open a large number of
1229 * file descriptors in order to consume multiple kernel traces.
1231 static void set_ulimit()
1236 /* The kernel does not allow an infinite limit for open files */
1237 lim
.rlim_cur
= 65535;
1238 lim
.rlim_max
= 65535;
1240 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1242 PERROR("failed to set open files limit");
1246 static int write_pidfile()
1248 return utils_create_pid_file(getpid(), the_config
.pid_file_path
.value
);
1251 static int set_clock_plugin_env()
1254 char *env_value
= nullptr;
1256 if (!the_config
.lttng_ust_clock_plugin
.value
) {
1261 &env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s", the_config
.lttng_ust_clock_plugin
.value
);
1267 ret
= putenv(env_value
);
1270 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1274 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1275 the_config
.lttng_ust_clock_plugin
.value
);
1280 static void destroy_all_sessions_and_wait()
1282 struct ltt_session
*raw_session_ptr
, *tmp
;
1283 struct ltt_session_list
*session_list
;
1285 DBG("Initiating destruction of all sessions");
1287 auto list_lock
= lttng::sessiond::lock_session_list();
1289 session_list
= session_get_list();
1290 if (!session_list
) {
1294 /* Initiate the destruction of all sessions. */
1295 cds_list_for_each_entry_safe (raw_session_ptr
, tmp
, &session_list
->head
, list
) {
1296 const auto session
= [raw_session_ptr
]() {
1297 session_get(raw_session_ptr
);
1298 raw_session_ptr
->lock();
1299 return ltt_session::locked_ref(*raw_session_ptr
);
1302 if (session
->destroyed
) {
1306 (void) cmd_stop_trace(session
);
1307 (void) cmd_destroy_session(session
, nullptr);
1310 /* Wait for the destruction of all sessions to complete. */
1311 DBG("Waiting for the destruction of all sessions to complete");
1312 session_list_wait_empty(std::move(list_lock
));
1313 DBG("Destruction of all sessions completed");
1316 static void unregister_all_triggers()
1318 enum lttng_error_code ret_code
;
1319 enum lttng_trigger_status trigger_status
;
1320 struct lttng_triggers
*triggers
= nullptr;
1321 unsigned int trigger_count
, i
;
1322 const struct lttng_credentials creds
= {
1323 .uid
= LTTNG_OPTIONAL_INIT_VALUE(0),
1324 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
1327 DBG("Unregistering all triggers");
1330 * List all triggers as "root" since we wish to unregister all triggers.
1332 ret_code
= notification_thread_command_list_triggers(
1333 the_notification_thread_handle
, creds
.uid
.value
, &triggers
);
1334 if (ret_code
!= LTTNG_OK
) {
1335 ERR("Failed to list triggers while unregistering all triggers");
1339 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
1340 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1342 for (i
= 0; i
< trigger_count
; i
++) {
1343 uid_t trigger_owner
;
1344 const char *trigger_name
;
1345 const struct lttng_trigger
*trigger
= lttng_triggers_get_at_index(triggers
, i
);
1347 LTTNG_ASSERT(trigger
);
1349 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
1350 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1352 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
1353 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
:
1356 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1357 (int) trigger_owner
,
1360 ret_code
= cmd_unregister_trigger(&creds
, trigger
, the_notification_thread_handle
);
1361 if (ret_code
!= LTTNG_OK
) {
1362 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1363 (int) trigger_owner
,
1365 lttng_strerror(-ret_code
));
1366 /* Continue to unregister the remaining triggers. */
1370 lttng_triggers_destroy(triggers
);
1373 static int run_as_worker_post_fork_cleanup(void *data
)
1375 struct sessiond_config
*sessiond_config
= (struct sessiond_config
*) data
;
1377 sessiond_config_fini(sessiond_config
);
1381 static int launch_run_as_worker(const char *procname
)
1384 * Clean-up before forking the run-as worker. Any dynamically
1385 * allocated memory of which the worker is not aware will
1386 * be leaked as the process forks a run-as worker (and performs
1387 * no exec*()). The same would apply to any opened fd.
1389 return run_as_create_worker(procname
, run_as_worker_post_fork_cleanup
, &the_config
);
1392 static void sessiond_uuid_log()
1394 char uuid_str
[LTTNG_UUID_STR_LEN
];
1396 lttng_uuid_to_str(the_sessiond_uuid
, uuid_str
);
1397 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1403 static int _main(int argc
, char **argv
)
1405 int ret
= 0, retval
= 0;
1406 const char *env_app_timeout
;
1407 struct lttng_pipe
*ust32_channel_monitor_pipe
= nullptr,
1408 *ust64_channel_monitor_pipe
= nullptr,
1409 *kernel_channel_monitor_pipe
= nullptr;
1410 struct timer_thread_parameters timer_thread_parameters
;
1411 /* Queue of rotation jobs populated by the sessiond-timer. */
1412 lttng::sessiond::rotation_thread_timer_queue
*rotation_timer_queue
= nullptr;
1413 struct lttng_thread
*client_thread
= nullptr;
1414 struct lttng_thread
*notification_thread
= nullptr;
1415 struct lttng_thread
*register_apps_thread
= nullptr;
1416 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
1418 logger_set_thread_name("Main", false);
1419 init_kernel_workarounds();
1421 rcu_register_thread();
1423 if (set_signal_handler()) {
1425 goto exit_set_signal_handler
;
1428 if (timer_signal_init()) {
1430 goto exit_set_signal_handler
;
1433 the_page_size
= sysconf(_SC_PAGE_SIZE
);
1434 if (the_page_size
< 0) {
1435 PERROR("sysconf _SC_PAGE_SIZE");
1436 the_page_size
= LONG_MAX
;
1437 WARN("Fallback page size to %ld", the_page_size
);
1440 ret
= sessiond_config_init(&the_config
);
1443 goto exit_set_signal_handler
;
1447 * Init config from environment variables.
1448 * Command line option override env configuration per-doc. Do env first.
1450 sessiond_config_apply_env_config(&the_config
);
1453 * Parse arguments and load the daemon configuration file.
1455 * We have an exit_options exit path to free memory reserved by
1459 if (set_options(argc
, argv
)) {
1465 * Resolve all paths received as arguments, configuration option, or
1466 * through environment variable as absolute paths. This is necessary
1467 * since daemonizing causes the sessiond's current working directory
1470 ret
= sessiond_config_resolve_paths(&the_config
);
1476 lttng_opt_verbose
= the_config
.verbose
;
1477 lttng_opt_quiet
= the_config
.quiet
;
1478 the_kconsumer_data
.err_unix_sock_path
= the_config
.kconsumerd_err_unix_sock_path
.value
;
1479 the_kconsumer_data
.cmd_unix_sock_path
= the_config
.kconsumerd_cmd_unix_sock_path
.value
;
1480 the_ustconsumer32_data
.err_unix_sock_path
= the_config
.consumerd32_err_unix_sock_path
.value
;
1481 the_ustconsumer32_data
.cmd_unix_sock_path
= the_config
.consumerd32_cmd_unix_sock_path
.value
;
1482 the_ustconsumer64_data
.err_unix_sock_path
= the_config
.consumerd64_err_unix_sock_path
.value
;
1483 the_ustconsumer64_data
.cmd_unix_sock_path
= the_config
.consumerd64_cmd_unix_sock_path
.value
;
1484 set_clock_plugin_env();
1486 sessiond_config_log(&the_config
);
1487 sessiond_uuid_log();
1488 lttng::logging::log_system_information(PRINT_DBG
);
1490 if (opt_print_version
) {
1496 if (create_lttng_rundir()) {
1501 /* Abort launch if a session daemon is already running. */
1502 if (check_existing_daemon()) {
1503 ERR("A session daemon is already running.");
1509 if (the_config
.daemonize
|| the_config
.background
) {
1512 ret
= lttng_daemonize(&the_child_ppid
, &recv_child_signal
, !the_config
.background
);
1519 * We are in the child. Make sure all other file descriptors are
1520 * closed, in case we are called with more opened file
1521 * descriptors than the standard ones and the lock file.
1523 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1524 if (i
== lockfile_fd
) {
1531 if (launch_run_as_worker(argv
[0]) < 0) {
1532 goto exit_create_run_as_worker_cleanup
;
1536 * Starting from here, we can create threads. This needs to be after
1537 * lttng_daemonize due to RCU.
1541 * Initialize the health check subsystem. This call should set the
1542 * appropriate time values.
1544 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1545 if (!the_health_sessiond
) {
1546 PERROR("health_app_create error");
1551 /* Create main quit pipe */
1552 if (sessiond_init_main_quit_pipe()) {
1557 /* Check if daemon is UID = 0 */
1558 is_root
= !getuid();
1560 /* Create global run dir with root access */
1562 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1563 if (!kernel_channel_monitor_pipe
) {
1564 ERR("Failed to create kernel consumer channel monitor pipe");
1568 the_kconsumer_data
.channel_monitor_pipe
=
1569 lttng_pipe_release_writefd(kernel_channel_monitor_pipe
);
1570 if (the_kconsumer_data
.channel_monitor_pipe
< 0) {
1576 /* Set consumer initial state */
1577 the_kernel_consumerd_state
= CONSUMER_STOPPED
;
1578 the_ust_consumerd_state
= CONSUMER_STOPPED
;
1580 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1581 if (!ust32_channel_monitor_pipe
) {
1582 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1586 the_ustconsumer32_data
.channel_monitor_pipe
=
1587 lttng_pipe_release_writefd(ust32_channel_monitor_pipe
);
1588 if (the_ustconsumer32_data
.channel_monitor_pipe
< 0) {
1594 * The rotation_thread_timer_queue structure is shared between the
1595 * sessiond timer thread and the rotation thread. The main thread keeps
1596 * its ownership and destroys it when both threads have been joined.
1598 rotation_timer_queue
= lttng::sessiond::rotation_thread_timer_queue_create();
1599 if (!rotation_timer_queue
) {
1603 timer_thread_parameters
.rotation_thread_job_queue
= rotation_timer_queue
;
1605 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1606 if (!ust64_channel_monitor_pipe
) {
1607 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1611 the_ustconsumer64_data
.channel_monitor_pipe
=
1612 lttng_pipe_release_writefd(ust64_channel_monitor_pipe
);
1613 if (the_ustconsumer64_data
.channel_monitor_pipe
< 0) {
1619 * Init UST app hash table. Alloc hash table before this point since
1620 * cleanup() can get called after that point.
1622 if (ust_app_ht_alloc()) {
1623 ERR("Failed to allocate UST app hash table");
1628 event_notifier_error_accounting_status
= event_notifier_error_accounting_init(
1629 the_config
.event_notifier_buffer_size_kernel
,
1630 the_config
.event_notifier_buffer_size_userspace
);
1631 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1632 ERR("Failed to initialize event notifier error accounting system");
1638 * Initialize agent app hash table. We allocate the hash table here
1639 * since cleanup() can get called after this point.
1641 if (agent_app_ht_alloc()) {
1642 ERR("Failed to allocate Agent app hash table");
1647 if (agent_by_event_notifier_domain_ht_create()) {
1648 ERR("Failed to allocate per-event notifier domain agent hash table");
1653 * These actions must be executed as root. We do that *after* setting up
1654 * the sockets path because we MUST make the check for another daemon using
1655 * those paths *before* trying to set the kernel consumer sockets and init
1659 if (set_consumer_sockets(&the_kconsumer_data
)) {
1664 /* Setup kernel tracer */
1665 if (!the_config
.no_kernel
) {
1666 init_kernel_tracer();
1669 /* Set ulimit for open files */
1672 /* init lttng_fd tracking must be done after set_ulimit. */
1675 if (set_consumer_sockets(&the_ustconsumer64_data
)) {
1680 if (set_consumer_sockets(&the_ustconsumer32_data
)) {
1685 /* Get parent pid if -S, --sig-parent is specified. */
1686 if (the_config
.sig_parent
) {
1687 the_ppid
= getppid();
1690 /* Setup the kernel pipe for waking up the kernel thread */
1691 if (is_root
&& !the_config
.no_kernel
) {
1692 if (utils_create_pipe_cloexec(the_kernel_poll_pipe
)) {
1698 /* Setup the thread apps communication pipe. */
1699 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1704 /* Setup the thread apps notify communication pipe. */
1705 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1710 /* Initialize global buffer per UID and PID registry. */
1711 buffer_reg_init_uid_registry();
1712 buffer_reg_init_pid_registry();
1714 /* Init UST command queue. */
1715 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1719 /* Check for the application socket timeout env variable. */
1720 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1721 if (env_app_timeout
) {
1722 the_config
.app_socket_timeout
= atoi(env_app_timeout
);
1724 the_config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1727 ret
= write_pidfile();
1729 ERR("Error in write_pidfile");
1734 /* Initialize communication library */
1736 /* Initialize TCP timeout values */
1737 lttcomm_inet_init();
1739 /* Create health-check thread. */
1740 if (!launch_health_management_thread()) {
1745 /* notification_thread_data acquires the pipes' read side. */
1746 the_notification_thread_handle
=
1747 notification_thread_handle_create(ust32_channel_monitor_pipe
,
1748 ust64_channel_monitor_pipe
,
1749 kernel_channel_monitor_pipe
);
1750 if (!the_notification_thread_handle
) {
1752 ERR("Failed to create notification thread shared data");
1756 /* Create notification thread. */
1757 notification_thread
= launch_notification_thread(the_notification_thread_handle
);
1758 if (!notification_thread
) {
1763 /* Create timer thread. */
1764 if (!launch_timer_thread(&timer_thread_parameters
)) {
1770 the_rotation_thread_handle
= lttng::make_unique
<lttng::sessiond::rotation_thread
>(
1771 *rotation_timer_queue
, *the_notification_thread_handle
);
1772 } catch (const std::exception
& e
) {
1774 ERR("Failed to create rotation thread: %s", e
.what());
1779 the_rotation_thread_handle
->launch_thread();
1780 } catch (const std::exception
& e
) {
1782 ERR("Failed to launch rotation thread: %s", e
.what());
1786 /* Create thread to manage the client socket */
1787 client_thread
= launch_client_thread();
1788 if (!client_thread
) {
1793 /* Set credentials of the client socket and rundir */
1794 if (is_root
&& set_permissions(the_config
.rundir
.value
)) {
1799 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1], apps_cmd_notify_pipe
[1])) {
1804 /* Create thread to manage application registration. */
1805 register_apps_thread
= launch_application_registration_thread(&ust_cmd_queue
);
1806 if (!register_apps_thread
) {
1811 /* Create thread to manage application socket */
1812 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1817 /* Create thread to manage application notify socket */
1818 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1823 /* Create agent management thread. */
1824 if (!launch_agent_management_thread()) {
1829 /* Don't start this thread if kernel tracing is not requested nor root */
1830 if (is_root
&& !the_config
.no_kernel
) {
1831 /* Create kernel thread to manage kernel event */
1832 if (!launch_kernel_management_thread(the_kernel_poll_pipe
[0])) {
1837 if (kernel_get_notification_fd() >= 0) {
1838 ret
= notification_thread_command_add_tracer_event_source(
1839 the_notification_thread_handle
,
1840 kernel_get_notification_fd(),
1841 LTTNG_DOMAIN_KERNEL
);
1842 if (ret
!= LTTNG_OK
) {
1843 ERR("Failed to add kernel trigger event source to notification thread");
1850 /* Load sessions. */
1851 ret
= config_load_session(the_config
.load_session_path
.value
, nullptr, 1, 1, nullptr);
1853 ERR("Session load failed: %s", error_get_str(ret
));
1858 /* Initialization completed. */
1859 sessiond_signal_parents();
1862 * This is where we start awaiting program completion (e.g. through
1863 * signal that asks threads to teardown).
1866 /* Initiate teardown once activity occurs on the main quit pipe. */
1867 sessiond_wait_for_main_quit_pipe(-1);
1871 DBG("Terminating all threads");
1874 * Ensure that the client thread is no longer accepting new commands,
1875 * which could cause new sessions to be created.
1877 if (client_thread
) {
1878 lttng_thread_shutdown(client_thread
);
1879 lttng_thread_put(client_thread
);
1882 destroy_all_sessions_and_wait();
1885 * At this point no new trigger can be registered (no sessions are
1886 * running/rotating) and clients can't connect to the session daemon
1887 * anymore. Unregister all triggers.
1889 unregister_all_triggers();
1891 if (register_apps_thread
) {
1892 lttng_thread_shutdown(register_apps_thread
);
1893 lttng_thread_put(register_apps_thread
);
1895 lttng_thread_list_shutdown_orphans();
1898 * Wait for all pending call_rcu work to complete before tearing
1899 * down data structures. call_rcu worker may be trying to
1900 * perform lookups in those structures.
1904 rcu_thread_online();
1908 * Wait for all pending call_rcu work to complete before shutting down
1909 * the notification thread. This call_rcu work includes shutting down
1910 * UST apps and event notifier pipes.
1914 if (notification_thread
) {
1915 lttng_thread_shutdown(notification_thread
);
1916 lttng_thread_put(notification_thread
);
1920 * Error accounting teardown has to be done after the teardown of all
1921 * event notifier pipes to ensure that no tracer may try to use the
1922 * error accounting facilities.
1924 event_notifier_error_accounting_fini();
1927 * Unloading the kernel modules needs to be done after all kernel
1928 * ressources have been released. In our case, this includes the
1929 * notification fd, the event notifier group fd, error accounting fd,
1930 * all event and event notifier fds, etc.
1932 * In short, at this point, we need to have called close() on all fds
1933 * received from the kernel tracer.
1935 if (is_root
&& !the_config
.no_kernel
) {
1936 DBG("Unloading kernel modules");
1937 modprobe_remove_lttng_all();
1940 rcu_thread_offline();
1941 rcu_unregister_thread();
1944 * After the rotation and timer thread have quit, we can safely destroy
1945 * the rotation_timer_queue.
1947 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1949 * The teardown of the notification system is performed after the
1950 * session daemon's teardown in order to allow it to be notified
1951 * of the active session and channels at the moment of the teardown.
1953 if (the_notification_thread_handle
) {
1954 notification_thread_handle_destroy(the_notification_thread_handle
);
1956 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1957 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1958 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1960 if (the_health_sessiond
) {
1961 health_app_destroy(the_health_sessiond
);
1963 exit_create_run_as_worker_cleanup
:
1965 sessiond_cleanup_lock_file();
1966 sessiond_cleanup_options();
1968 exit_set_signal_handler
:
1976 int main(int argc
, char **argv
)
1979 return _main(argc
, argv
);
1980 } catch (const std::exception
& e
) {
1981 ERR_FMT("Unhandled exception caught by main thread: {}", e
.what());