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()
280 struct ltt_session_list
*session_list
= session_get_list();
282 DBG("Cleanup sessiond");
285 * Close the main quit pipe. It has already done its job, since we are
288 sessiond_close_main_quit_pipe();
290 /* Close all other pipes. */
291 utils_close_pipe(apps_cmd_pipe
);
292 utils_close_pipe(apps_cmd_notify_pipe
);
293 utils_close_pipe(the_kernel_poll_pipe
);
295 ret
= remove(the_config
.pid_file_path
.value
);
297 PERROR("remove pidfile %s", the_config
.pid_file_path
.value
);
300 DBG("Removing sessiond and consumerd content of directory %s", the_config
.rundir
.value
);
303 DBG("Removing %s", the_config
.pid_file_path
.value
);
304 (void) unlink(the_config
.pid_file_path
.value
);
306 DBG("Removing %s", the_config
.agent_port_file_path
.value
);
307 (void) unlink(the_config
.agent_port_file_path
.value
);
310 DBG("Removing %s", the_kconsumer_data
.err_unix_sock_path
);
311 (void) unlink(the_kconsumer_data
.err_unix_sock_path
);
313 DBG("Removing directory %s", the_config
.kconsumerd_path
.value
);
314 (void) rmdir(the_config
.kconsumerd_path
.value
);
316 /* ust consumerd 32 */
317 DBG("Removing %s", the_config
.consumerd32_err_unix_sock_path
.value
);
318 (void) unlink(the_config
.consumerd32_err_unix_sock_path
.value
);
320 DBG("Removing directory %s", the_config
.consumerd32_path
.value
);
321 (void) rmdir(the_config
.consumerd32_path
.value
);
323 /* ust consumerd 64 */
324 DBG("Removing %s", the_config
.consumerd64_err_unix_sock_path
.value
);
325 (void) unlink(the_config
.consumerd64_err_unix_sock_path
.value
);
327 DBG("Removing directory %s", the_config
.consumerd64_path
.value
);
328 (void) rmdir(the_config
.consumerd64_path
.value
);
330 pthread_mutex_destroy(&session_list
->lock
);
332 DBG("Cleaning up all per-event notifier domain agents");
333 agent_by_event_notifier_domain_ht_destroy();
335 DBG("Cleaning up all agent apps");
336 agent_app_ht_clean();
337 DBG("Closing all UST sockets");
338 ust_app_clean_list();
339 buffer_reg_destroy_registries();
341 close_consumer_sockets();
343 wait_consumer(&the_kconsumer_data
);
344 wait_consumer(&the_ustconsumer64_data
);
345 wait_consumer(&the_ustconsumer32_data
);
347 if (is_root
&& !the_config
.no_kernel
) {
348 cleanup_kernel_tracer();
352 * We do NOT rmdir rundir because there are other processes
353 * using it, for instance lttng-relayd, which can start in
354 * parallel with this teardown.
359 * Cleanup the daemon's option data structures.
361 static void sessiond_cleanup_options()
363 DBG("Cleaning up options");
365 sessiond_config_fini(&the_config
);
367 run_as_destroy_worker();
370 static int string_match(const char *str1
, const char *str2
)
372 return (str1
&& str2
) && !strcmp(str1
, str2
);
376 * Take an option from the getopt output and set it in the right variable to be
379 * Return 0 on success else a negative value.
381 static int set_option(int opt
, const char *arg
, const char *optname
)
385 if (string_match(optname
, "client-sock") || opt
== 'c') {
386 if (!arg
|| *arg
== '\0') {
390 if (lttng_is_setuid_setgid()) {
391 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
392 "-c, --client-sock");
394 config_string_set(&the_config
.client_unix_sock_path
, strdup(arg
));
395 if (!the_config
.client_unix_sock_path
.value
) {
400 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
401 if (!arg
|| *arg
== '\0') {
405 if (lttng_is_setuid_setgid()) {
406 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
409 config_string_set(&the_config
.apps_unix_sock_path
, strdup(arg
));
410 if (!the_config
.apps_unix_sock_path
.value
) {
415 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
416 the_config
.daemonize
= true;
417 } else if (string_match(optname
, "background") || opt
== 'b') {
418 the_config
.background
= true;
419 } else if (string_match(optname
, "group") || opt
== 'g') {
420 if (!arg
|| *arg
== '\0') {
424 if (lttng_is_setuid_setgid()) {
425 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
428 config_string_set(&the_config
.tracing_group_name
, strdup(arg
));
429 if (!the_config
.tracing_group_name
.value
) {
434 } else if (string_match(optname
, "help") || opt
== 'h') {
435 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
437 ERR("Cannot show --help for `lttng-sessiond`");
440 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
441 } else if (string_match(optname
, "version") || opt
== 'V') {
442 opt_print_version
= 1;
443 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
444 the_config
.sig_parent
= true;
445 } else if (string_match(optname
, "kconsumerd-err-sock")) {
446 if (!arg
|| *arg
== '\0') {
450 if (lttng_is_setuid_setgid()) {
451 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
452 "--kconsumerd-err-sock");
454 config_string_set(&the_config
.kconsumerd_err_unix_sock_path
, strdup(arg
));
455 if (!the_config
.kconsumerd_err_unix_sock_path
.value
) {
460 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
461 if (!arg
|| *arg
== '\0') {
465 if (lttng_is_setuid_setgid()) {
466 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
467 "--kconsumerd-cmd-sock");
469 config_string_set(&the_config
.kconsumerd_cmd_unix_sock_path
, strdup(arg
));
470 if (!the_config
.kconsumerd_cmd_unix_sock_path
.value
) {
475 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
476 if (!arg
|| *arg
== '\0') {
480 if (lttng_is_setuid_setgid()) {
481 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
482 "--ustconsumerd64-err-sock");
484 config_string_set(&the_config
.consumerd64_err_unix_sock_path
, strdup(arg
));
485 if (!the_config
.consumerd64_err_unix_sock_path
.value
) {
490 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
491 if (!arg
|| *arg
== '\0') {
495 if (lttng_is_setuid_setgid()) {
496 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
497 "--ustconsumerd64-cmd-sock");
499 config_string_set(&the_config
.consumerd64_cmd_unix_sock_path
, strdup(arg
));
500 if (!the_config
.consumerd64_cmd_unix_sock_path
.value
) {
505 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
506 if (!arg
|| *arg
== '\0') {
510 if (lttng_is_setuid_setgid()) {
511 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
512 "--ustconsumerd32-err-sock");
514 config_string_set(&the_config
.consumerd32_err_unix_sock_path
, strdup(arg
));
515 if (!the_config
.consumerd32_err_unix_sock_path
.value
) {
520 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
521 if (!arg
|| *arg
== '\0') {
525 if (lttng_is_setuid_setgid()) {
526 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
527 "--ustconsumerd32-cmd-sock");
529 config_string_set(&the_config
.consumerd32_cmd_unix_sock_path
, strdup(arg
));
530 if (!the_config
.consumerd32_cmd_unix_sock_path
.value
) {
535 } else if (string_match(optname
, "no-kernel")) {
536 the_config
.no_kernel
= true;
537 } else if (string_match(optname
, "quiet") || opt
== 'q') {
538 the_config
.quiet
= true;
539 } else if (string_match(optname
, "verbose") || opt
== 'v') {
540 /* Verbose level can increase using multiple -v */
542 /* Value obtained from config file */
543 the_config
.verbose
= config_parse_value(arg
);
545 /* -v used on command line */
546 the_config
.verbose
++;
548 /* Clamp value to [0, 3] */
549 the_config
.verbose
= the_config
.verbose
< 0 ?
551 (the_config
.verbose
<= 3 ? the_config
.verbose
: 3);
552 } else if (string_match(optname
, "verbose-consumer")) {
554 the_config
.verbose_consumer
= config_parse_value(arg
);
556 the_config
.verbose_consumer
++;
558 } else if (string_match(optname
, "consumerd32-path")) {
559 if (!arg
|| *arg
== '\0') {
563 if (lttng_is_setuid_setgid()) {
564 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
565 "--consumerd32-path");
567 config_string_set(&the_config
.consumerd32_bin_path
, strdup(arg
));
568 if (!the_config
.consumerd32_bin_path
.value
) {
573 } else if (string_match(optname
, "consumerd32-libdir")) {
574 if (!arg
|| *arg
== '\0') {
578 if (lttng_is_setuid_setgid()) {
579 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
580 "--consumerd32-libdir");
582 config_string_set(&the_config
.consumerd32_lib_dir
, strdup(arg
));
583 if (!the_config
.consumerd32_lib_dir
.value
) {
588 } else if (string_match(optname
, "consumerd64-path")) {
589 if (!arg
|| *arg
== '\0') {
593 if (lttng_is_setuid_setgid()) {
594 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
595 "--consumerd64-path");
597 config_string_set(&the_config
.consumerd64_bin_path
, strdup(arg
));
598 if (!the_config
.consumerd64_bin_path
.value
) {
603 } else if (string_match(optname
, "consumerd64-libdir")) {
604 if (!arg
|| *arg
== '\0') {
608 if (lttng_is_setuid_setgid()) {
609 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
610 "--consumerd64-libdir");
612 config_string_set(&the_config
.consumerd64_lib_dir
, strdup(arg
));
613 if (!the_config
.consumerd64_lib_dir
.value
) {
618 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
619 if (!arg
|| *arg
== '\0') {
623 if (lttng_is_setuid_setgid()) {
624 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
627 config_string_set(&the_config
.pid_file_path
, strdup(arg
));
628 if (!the_config
.pid_file_path
.value
) {
633 } else if (string_match(optname
, "agent-tcp-port")) {
634 if (!arg
|| *arg
== '\0') {
638 if (lttng_is_setuid_setgid()) {
639 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
645 v
= strtoul(arg
, nullptr, 0);
646 if (errno
!= 0 || !isdigit(arg
[0])) {
647 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
650 if (v
== 0 || v
>= 65535) {
651 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
654 the_config
.agent_tcp_port
.begin
= the_config
.agent_tcp_port
.end
= (int) v
;
655 DBG3("Agent TCP port set to non default: %i", (int) v
);
657 } else if (string_match(optname
, "load") || opt
== 'l') {
658 if (!arg
|| *arg
== '\0') {
662 if (lttng_is_setuid_setgid()) {
663 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
666 config_string_set(&the_config
.load_session_path
, strdup(arg
));
667 if (!the_config
.load_session_path
.value
) {
672 } else if (string_match(optname
, "kmod-probes")) {
673 if (!arg
|| *arg
== '\0') {
677 if (lttng_is_setuid_setgid()) {
678 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
681 config_string_set(&the_config
.kmod_probes_list
, strdup(arg
));
682 if (!the_config
.kmod_probes_list
.value
) {
687 } else if (string_match(optname
, "extra-kmod-probes")) {
688 if (!arg
|| *arg
== '\0') {
692 if (lttng_is_setuid_setgid()) {
693 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
694 "--extra-kmod-probes");
696 config_string_set(&the_config
.kmod_extra_probes_list
, strdup(arg
));
697 if (!the_config
.kmod_extra_probes_list
.value
) {
702 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
)) {
706 v
= strtoul(arg
, nullptr, 0);
707 if (errno
!= 0 || !isdigit(arg
[0])) {
708 ERR("Wrong value in --%s parameter: %s",
709 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
,
713 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
714 ERR("Value out of range for --%s parameter: %s",
715 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
,
719 the_config
.event_notifier_buffer_size_kernel
= (int) v
;
720 DBG3("Number of event notifier error buffer kernel size to non default: %i",
721 the_config
.event_notifier_buffer_size_kernel
);
723 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
)) {
727 v
= strtoul(arg
, nullptr, 0);
728 if (errno
!= 0 || !isdigit(arg
[0])) {
729 ERR("Wrong value in --%s parameter: %s",
730 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
,
734 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
735 ERR("Value out of range for --%s parameter: %s",
736 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
,
740 the_config
.event_notifier_buffer_size_userspace
= (int) v
;
741 DBG3("Number of event notifier error buffer userspace size to non default: %i",
742 the_config
.event_notifier_buffer_size_userspace
);
744 } else if (string_match(optname
, "config") || opt
== 'f') {
745 /* This is handled in set_options() thus silent skip. */
748 /* Unknown option or other error.
749 * Error is printed by getopt, just return */
754 if (ret
== -EINVAL
) {
755 const char *opt_name
= "unknown";
758 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
); i
++) {
759 if (opt
== long_options
[i
].val
) {
760 opt_name
= long_options
[i
].name
;
765 WARN("Invalid argument provided for option \"%s\", using default value.", opt_name
);
772 * config_entry_handler_cb used to handle options read from a config file.
773 * See config_entry_handler_cb comment in common/config/session-config.h for the
774 * return value conventions.
776 static int config_entry_handler(const struct config_entry
*entry
,
777 void *unused
__attribute__((unused
)))
781 if (!entry
|| !entry
->name
|| !entry
->value
) {
786 /* Check if the option is to be ignored */
787 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
788 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
793 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
794 /* Ignore if not fully matched. */
795 if (strcmp(entry
->name
, long_options
[i
].name
) != 0) {
800 * If the option takes no argument on the command line, we have to
801 * check if the value is "true". We support non-zero numeric values,
804 if (!long_options
[i
].has_arg
) {
805 ret
= config_parse_value(entry
->value
);
808 WARN("Invalid configuration value \"%s\" for option %s",
812 /* False, skip boolean config option. */
817 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
821 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
827 static void print_version()
829 fprintf(stdout
, "%s\n", VERSION
);
833 * daemon configuration loading and argument parsing
835 static int set_options(int argc
, char **argv
)
837 int ret
= 0, c
= 0, option_index
= 0;
838 int orig_optopt
= optopt
, orig_optind
= optind
;
840 char *config_path
= nullptr;
842 optstring
= utils_generate_optstring(long_options
,
843 sizeof(long_options
) / sizeof(struct option
));
849 /* Check for the --config option */
850 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
)) != -1) {
854 } else if (c
!= 'f') {
855 /* if not equal to --config option. */
859 if (lttng_is_setuid_setgid()) {
860 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
864 config_path
= utils_expand_path(optarg
);
866 ERR("Failed to resolve path: %s", optarg
);
871 ret
= config_get_section_entries(
872 config_path
, config_section_name
, config_entry_handler
, nullptr);
875 ERR("Invalid configuration option at line %i", ret
);
881 /* Reset getopt's global state */
882 optopt
= orig_optopt
;
883 optind
= orig_optind
;
887 * getopt_long() will not set option_index if it encounters a
890 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
896 * Pass NULL as the long option name if popt left the index
900 c
, optarg
, option_index
< 0 ? nullptr : long_options
[option_index
].name
);
913 * Create lockfile using the rundir and return its fd.
915 static int create_lockfile()
917 return utils_create_lock_file(the_config
.lock_file_path
.value
);
921 * Check if the global socket is available, and if a daemon is answering at the
922 * other side. If yes, error is returned.
924 * Also attempts to create and hold the lock file.
926 static int check_existing_daemon()
930 /* Is there anybody out there ? */
931 if (lttng_session_daemon_alive()) {
936 lockfile_fd
= create_lockfile();
937 if (lockfile_fd
< 0) {
945 static void sessiond_cleanup_lock_file()
950 * Cleanup lock file by deleting it and finaly closing it which will
951 * release the file system lock.
953 if (lockfile_fd
>= 0) {
954 ret
= remove(the_config
.lock_file_path
.value
);
956 PERROR("remove lock file");
958 ret
= close(lockfile_fd
);
960 PERROR("close lock file");
966 * Set the tracing group gid onto the client socket.
968 * Race window between mkdir and chown is OK because we are going from more
969 * permissive (root.root) to less permissive (root.tracing).
971 static int set_permissions(char *rundir
)
976 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
, true, &gid
);
978 /* Default to root group. */
982 /* Set lttng run dir */
983 ret
= chown(rundir
, 0, gid
);
985 ERR("Unable to set group on %s", rundir
);
990 * Ensure all applications and tracing group can search the run
991 * dir. Allow everyone to read the directory, since it does not
992 * buy us anything to hide its content.
994 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
996 ERR("Unable to set permissions on %s", rundir
);
1000 /* lttng client socket path */
1001 ret
= chown(the_config
.client_unix_sock_path
.value
, 0, gid
);
1003 ERR("Unable to set group on %s", the_config
.client_unix_sock_path
.value
);
1007 /* kconsumer error socket path */
1008 ret
= chown(the_kconsumer_data
.err_unix_sock_path
, 0, 0);
1010 ERR("Unable to set group on %s", the_kconsumer_data
.err_unix_sock_path
);
1014 /* 64-bit ustconsumer error socket path */
1015 ret
= chown(the_ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1017 ERR("Unable to set group on %s", the_ustconsumer64_data
.err_unix_sock_path
);
1021 /* 32-bit ustconsumer compat32 error socket path */
1022 ret
= chown(the_ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1024 ERR("Unable to set group on %s", the_ustconsumer32_data
.err_unix_sock_path
);
1028 DBG("All permissions are set");
1034 * Create the lttng run directory needed for all global sockets and pipe.
1036 static int create_lttng_rundir()
1040 DBG3("Creating LTTng run directory: %s", the_config
.rundir
.value
);
1042 ret
= mkdir(the_config
.rundir
.value
, S_IRWXU
);
1044 if (errno
!= EEXIST
) {
1045 ERR("Unable to create %s", the_config
.rundir
.value
);
1057 * Setup sockets and directory needed by the consumerds' communication with the
1060 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1063 char *path
= nullptr;
1065 switch (consumer_data
->type
) {
1066 case LTTNG_CONSUMER_KERNEL
:
1067 path
= the_config
.kconsumerd_path
.value
;
1069 case LTTNG_CONSUMER64_UST
:
1070 path
= the_config
.consumerd64_path
.value
;
1072 case LTTNG_CONSUMER32_UST
:
1073 path
= the_config
.consumerd32_path
.value
;
1076 ERR("Consumer type unknown");
1082 DBG2("Creating consumer directory: %s", path
);
1084 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1085 if (ret
< 0 && errno
!= EEXIST
) {
1087 ERR("Failed to create %s", path
);
1093 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
, true, &gid
);
1095 /* Default to root group. */
1099 ret
= chown(path
, 0, gid
);
1101 ERR("Unable to set group on %s", path
);
1107 /* Create the consumerd error unix socket */
1108 consumer_data
->err_sock
= lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1109 if (consumer_data
->err_sock
< 0) {
1110 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1116 * Set the CLOEXEC flag. Return code is useless because either way, the
1119 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1121 PERROR("utils_set_fd_cloexec");
1122 /* continue anyway */
1125 /* File permission MUST be 660 */
1126 ret
= chmod(consumer_data
->err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1128 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1138 * Signal handler for the daemon
1140 * Simply stop all worker threads, leaving main() return gracefully after
1141 * joining all threads and calling cleanup().
1143 static void sighandler(int sig
, siginfo_t
*siginfo
, void *arg
__attribute__((unused
)))
1147 DBG("SIGINT caught");
1148 notify_main_quit_pipe();
1151 DBG("SIGTERM caught");
1152 notify_main_quit_pipe();
1155 CMM_STORE_SHARED(recv_child_signal
, 1);
1160 const char msg
[] = "Received SIGBUS, aborting program.\n";
1162 lttng_ust_handle_sigbus(siginfo
->si_addr
);
1164 * If ustctl did not catch this signal (triggering a
1165 * siglongjmp), abort the program. Otherwise, the execution
1166 * will resume from the ust-ctl call which caused this error.
1168 * The return value is ignored since the program aborts anyhow.
1170 write_ret
= write(STDERR_FILENO
, msg
, sizeof(msg
));
1180 * Setup signal handler for :
1181 * SIGINT, SIGTERM, SIGPIPE
1183 static int set_signal_handler()
1186 struct sigaction sa
;
1189 if ((ret
= sigemptyset(&sigset
)) < 0) {
1190 PERROR("sigemptyset");
1194 sa
.sa_mask
= sigset
;
1195 sa
.sa_flags
= SA_SIGINFO
;
1197 sa
.sa_sigaction
= sighandler
;
1198 if ((ret
= sigaction(SIGTERM
, &sa
, nullptr)) < 0) {
1199 PERROR("sigaction");
1203 if ((ret
= sigaction(SIGINT
, &sa
, nullptr)) < 0) {
1204 PERROR("sigaction");
1208 if ((ret
= sigaction(SIGUSR1
, &sa
, nullptr)) < 0) {
1209 PERROR("sigaction");
1213 if ((ret
= sigaction(SIGBUS
, &sa
, nullptr)) < 0) {
1214 PERROR("sigaction");
1219 sa
.sa_handler
= SIG_IGN
;
1220 if ((ret
= sigaction(SIGPIPE
, &sa
, nullptr)) < 0) {
1221 PERROR("sigaction");
1225 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE, SIGINT, and SIGBUS");
1231 * Set open files limit to unlimited. This daemon can open a large number of
1232 * file descriptors in order to consume multiple kernel traces.
1234 static void set_ulimit()
1239 /* The kernel does not allow an infinite limit for open files */
1240 lim
.rlim_cur
= 65535;
1241 lim
.rlim_max
= 65535;
1243 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1245 PERROR("failed to set open files limit");
1249 static int write_pidfile()
1251 return utils_create_pid_file(getpid(), the_config
.pid_file_path
.value
);
1254 static int set_clock_plugin_env()
1257 char *env_value
= nullptr;
1259 if (!the_config
.lttng_ust_clock_plugin
.value
) {
1264 &env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s", the_config
.lttng_ust_clock_plugin
.value
);
1270 ret
= putenv(env_value
);
1273 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1277 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1278 the_config
.lttng_ust_clock_plugin
.value
);
1283 static void destroy_all_sessions_and_wait()
1285 struct ltt_session
*session
, *tmp
;
1286 struct ltt_session_list
*session_list
;
1288 session_list
= session_get_list();
1289 DBG("Initiating destruction of all sessions");
1291 if (!session_list
) {
1295 session_lock_list();
1296 /* Initiate the destruction of all sessions. */
1297 cds_list_for_each_entry_safe (session
, tmp
, &session_list
->head
, list
) {
1298 if (!session_get(session
)) {
1302 session_lock(session
);
1303 if (session
->destroyed
) {
1304 goto unlock_session
;
1306 (void) cmd_stop_trace(session
);
1307 (void) cmd_destroy_session(session
, nullptr);
1309 session_unlock(session
);
1310 session_put(session
);
1312 session_unlock_list();
1314 /* Wait for the destruction of all sessions to complete. */
1315 DBG("Waiting for the destruction of all sessions to complete");
1316 session_list_wait_empty();
1317 DBG("Destruction of all sessions completed");
1320 static void unregister_all_triggers()
1322 enum lttng_error_code ret_code
;
1323 enum lttng_trigger_status trigger_status
;
1324 struct lttng_triggers
*triggers
= nullptr;
1325 unsigned int trigger_count
, i
;
1326 const struct lttng_credentials creds
= {
1327 .uid
= LTTNG_OPTIONAL_INIT_VALUE(0),
1328 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
1331 DBG("Unregistering all triggers");
1334 * List all triggers as "root" since we wish to unregister all triggers.
1336 ret_code
= notification_thread_command_list_triggers(
1337 the_notification_thread_handle
, creds
.uid
.value
, &triggers
);
1338 if (ret_code
!= LTTNG_OK
) {
1339 ERR("Failed to list triggers while unregistering all triggers");
1343 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
1344 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1346 for (i
= 0; i
< trigger_count
; i
++) {
1347 uid_t trigger_owner
;
1348 const char *trigger_name
;
1349 const struct lttng_trigger
*trigger
= lttng_triggers_get_at_index(triggers
, i
);
1351 LTTNG_ASSERT(trigger
);
1353 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
1354 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1356 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
1357 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
:
1360 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1361 (int) trigger_owner
,
1364 ret_code
= cmd_unregister_trigger(&creds
, trigger
, the_notification_thread_handle
);
1365 if (ret_code
!= LTTNG_OK
) {
1366 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1367 (int) trigger_owner
,
1369 lttng_strerror(-ret_code
));
1370 /* Continue to unregister the remaining triggers. */
1374 lttng_triggers_destroy(triggers
);
1377 static int run_as_worker_post_fork_cleanup(void *data
)
1379 struct sessiond_config
*sessiond_config
= (struct sessiond_config
*) data
;
1381 sessiond_config_fini(sessiond_config
);
1385 static int launch_run_as_worker(const char *procname
)
1388 * Clean-up before forking the run-as worker. Any dynamically
1389 * allocated memory of which the worker is not aware will
1390 * be leaked as the process forks a run-as worker (and performs
1391 * no exec*()). The same would apply to any opened fd.
1393 return run_as_create_worker(procname
, run_as_worker_post_fork_cleanup
, &the_config
);
1396 static void sessiond_uuid_log()
1398 char uuid_str
[LTTNG_UUID_STR_LEN
];
1400 lttng_uuid_to_str(the_sessiond_uuid
, uuid_str
);
1401 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1407 static int _main(int argc
, char **argv
)
1409 int ret
= 0, retval
= 0;
1410 const char *env_app_timeout
;
1411 struct lttng_pipe
*ust32_channel_monitor_pipe
= nullptr,
1412 *ust64_channel_monitor_pipe
= nullptr,
1413 *kernel_channel_monitor_pipe
= nullptr;
1414 struct timer_thread_parameters timer_thread_parameters
;
1415 /* Queue of rotation jobs populated by the sessiond-timer. */
1416 lttng::sessiond::rotation_thread_timer_queue
*rotation_timer_queue
= nullptr;
1417 struct lttng_thread
*client_thread
= nullptr;
1418 struct lttng_thread
*notification_thread
= nullptr;
1419 struct lttng_thread
*register_apps_thread
= nullptr;
1420 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
1422 logger_set_thread_name("Main", false);
1423 init_kernel_workarounds();
1425 rcu_register_thread();
1427 if (set_signal_handler()) {
1429 goto exit_set_signal_handler
;
1432 if (timer_signal_init()) {
1434 goto exit_set_signal_handler
;
1437 the_page_size
= sysconf(_SC_PAGE_SIZE
);
1438 if (the_page_size
< 0) {
1439 PERROR("sysconf _SC_PAGE_SIZE");
1440 the_page_size
= LONG_MAX
;
1441 WARN("Fallback page size to %ld", the_page_size
);
1444 ret
= sessiond_config_init(&the_config
);
1447 goto exit_set_signal_handler
;
1451 * Init config from environment variables.
1452 * Command line option override env configuration per-doc. Do env first.
1454 sessiond_config_apply_env_config(&the_config
);
1457 * Parse arguments and load the daemon configuration file.
1459 * We have an exit_options exit path to free memory reserved by
1463 if (set_options(argc
, argv
)) {
1469 * Resolve all paths received as arguments, configuration option, or
1470 * through environment variable as absolute paths. This is necessary
1471 * since daemonizing causes the sessiond's current working directory
1474 ret
= sessiond_config_resolve_paths(&the_config
);
1480 lttng_opt_verbose
= the_config
.verbose
;
1481 lttng_opt_quiet
= the_config
.quiet
;
1482 the_kconsumer_data
.err_unix_sock_path
= the_config
.kconsumerd_err_unix_sock_path
.value
;
1483 the_kconsumer_data
.cmd_unix_sock_path
= the_config
.kconsumerd_cmd_unix_sock_path
.value
;
1484 the_ustconsumer32_data
.err_unix_sock_path
= the_config
.consumerd32_err_unix_sock_path
.value
;
1485 the_ustconsumer32_data
.cmd_unix_sock_path
= the_config
.consumerd32_cmd_unix_sock_path
.value
;
1486 the_ustconsumer64_data
.err_unix_sock_path
= the_config
.consumerd64_err_unix_sock_path
.value
;
1487 the_ustconsumer64_data
.cmd_unix_sock_path
= the_config
.consumerd64_cmd_unix_sock_path
.value
;
1488 set_clock_plugin_env();
1490 sessiond_config_log(&the_config
);
1491 sessiond_uuid_log();
1492 lttng::logging::log_system_information(PRINT_DBG
);
1494 if (opt_print_version
) {
1500 if (create_lttng_rundir()) {
1505 /* Abort launch if a session daemon is already running. */
1506 if (check_existing_daemon()) {
1507 ERR("A session daemon is already running.");
1513 if (the_config
.daemonize
|| the_config
.background
) {
1516 ret
= lttng_daemonize(&the_child_ppid
, &recv_child_signal
, !the_config
.background
);
1523 * We are in the child. Make sure all other file descriptors are
1524 * closed, in case we are called with more opened file
1525 * descriptors than the standard ones and the lock file.
1527 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1528 if (i
== lockfile_fd
) {
1535 if (launch_run_as_worker(argv
[0]) < 0) {
1536 goto exit_create_run_as_worker_cleanup
;
1540 * Starting from here, we can create threads. This needs to be after
1541 * lttng_daemonize due to RCU.
1545 * Initialize the health check subsystem. This call should set the
1546 * appropriate time values.
1548 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1549 if (!the_health_sessiond
) {
1550 PERROR("health_app_create error");
1555 /* Create main quit pipe */
1556 if (sessiond_init_main_quit_pipe()) {
1561 /* Check if daemon is UID = 0 */
1562 is_root
= !getuid();
1564 /* Create global run dir with root access */
1566 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1567 if (!kernel_channel_monitor_pipe
) {
1568 ERR("Failed to create kernel consumer channel monitor pipe");
1572 the_kconsumer_data
.channel_monitor_pipe
=
1573 lttng_pipe_release_writefd(kernel_channel_monitor_pipe
);
1574 if (the_kconsumer_data
.channel_monitor_pipe
< 0) {
1580 /* Set consumer initial state */
1581 the_kernel_consumerd_state
= CONSUMER_STOPPED
;
1582 the_ust_consumerd_state
= CONSUMER_STOPPED
;
1584 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1585 if (!ust32_channel_monitor_pipe
) {
1586 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1590 the_ustconsumer32_data
.channel_monitor_pipe
=
1591 lttng_pipe_release_writefd(ust32_channel_monitor_pipe
);
1592 if (the_ustconsumer32_data
.channel_monitor_pipe
< 0) {
1598 * The rotation_thread_timer_queue structure is shared between the
1599 * sessiond timer thread and the rotation thread. The main thread keeps
1600 * its ownership and destroys it when both threads have been joined.
1602 rotation_timer_queue
= lttng::sessiond::rotation_thread_timer_queue_create();
1603 if (!rotation_timer_queue
) {
1607 timer_thread_parameters
.rotation_thread_job_queue
= rotation_timer_queue
;
1609 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1610 if (!ust64_channel_monitor_pipe
) {
1611 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1615 the_ustconsumer64_data
.channel_monitor_pipe
=
1616 lttng_pipe_release_writefd(ust64_channel_monitor_pipe
);
1617 if (the_ustconsumer64_data
.channel_monitor_pipe
< 0) {
1623 * Init UST app hash table. Alloc hash table before this point since
1624 * cleanup() can get called after that point.
1626 if (ust_app_ht_alloc()) {
1627 ERR("Failed to allocate UST app hash table");
1632 event_notifier_error_accounting_status
= event_notifier_error_accounting_init(
1633 the_config
.event_notifier_buffer_size_kernel
,
1634 the_config
.event_notifier_buffer_size_userspace
);
1635 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1636 ERR("Failed to initialize event notifier error accounting system");
1642 * Initialize agent app hash table. We allocate the hash table here
1643 * since cleanup() can get called after this point.
1645 if (agent_app_ht_alloc()) {
1646 ERR("Failed to allocate Agent app hash table");
1651 if (agent_by_event_notifier_domain_ht_create()) {
1652 ERR("Failed to allocate per-event notifier domain agent hash table");
1657 * These actions must be executed as root. We do that *after* setting up
1658 * the sockets path because we MUST make the check for another daemon using
1659 * those paths *before* trying to set the kernel consumer sockets and init
1663 if (set_consumer_sockets(&the_kconsumer_data
)) {
1668 /* Setup kernel tracer */
1669 if (!the_config
.no_kernel
) {
1670 init_kernel_tracer();
1673 /* Set ulimit for open files */
1676 /* init lttng_fd tracking must be done after set_ulimit. */
1679 if (set_consumer_sockets(&the_ustconsumer64_data
)) {
1684 if (set_consumer_sockets(&the_ustconsumer32_data
)) {
1689 /* Get parent pid if -S, --sig-parent is specified. */
1690 if (the_config
.sig_parent
) {
1691 the_ppid
= getppid();
1694 /* Setup the kernel pipe for waking up the kernel thread */
1695 if (is_root
&& !the_config
.no_kernel
) {
1696 if (utils_create_pipe_cloexec(the_kernel_poll_pipe
)) {
1702 /* Setup the thread apps communication pipe. */
1703 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1708 /* Setup the thread apps notify communication pipe. */
1709 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1714 /* Initialize global buffer per UID and PID registry. */
1715 buffer_reg_init_uid_registry();
1716 buffer_reg_init_pid_registry();
1718 /* Init UST command queue. */
1719 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1723 /* Check for the application socket timeout env variable. */
1724 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1725 if (env_app_timeout
) {
1726 the_config
.app_socket_timeout
= atoi(env_app_timeout
);
1728 the_config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1731 ret
= write_pidfile();
1733 ERR("Error in write_pidfile");
1738 /* Initialize communication library */
1740 /* Initialize TCP timeout values */
1741 lttcomm_inet_init();
1743 /* Create health-check thread. */
1744 if (!launch_health_management_thread()) {
1749 /* notification_thread_data acquires the pipes' read side. */
1750 the_notification_thread_handle
=
1751 notification_thread_handle_create(ust32_channel_monitor_pipe
,
1752 ust64_channel_monitor_pipe
,
1753 kernel_channel_monitor_pipe
);
1754 if (!the_notification_thread_handle
) {
1756 ERR("Failed to create notification thread shared data");
1760 /* Create notification thread. */
1761 notification_thread
= launch_notification_thread(the_notification_thread_handle
);
1762 if (!notification_thread
) {
1767 /* Create timer thread. */
1768 if (!launch_timer_thread(&timer_thread_parameters
)) {
1774 the_rotation_thread_handle
= lttng::make_unique
<lttng::sessiond::rotation_thread
>(
1775 *rotation_timer_queue
, *the_notification_thread_handle
);
1776 } catch (const std::exception
& e
) {
1778 ERR("Failed to create rotation thread: %s", e
.what());
1783 the_rotation_thread_handle
->launch_thread();
1784 } catch (const std::exception
& e
) {
1786 ERR("Failed to launch rotation thread: %s", e
.what());
1790 /* Create thread to manage the client socket */
1791 client_thread
= launch_client_thread();
1792 if (!client_thread
) {
1797 /* Set credentials of the client socket and rundir */
1798 if (is_root
&& set_permissions(the_config
.rundir
.value
)) {
1803 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1], apps_cmd_notify_pipe
[1])) {
1808 /* Create thread to manage application registration. */
1809 register_apps_thread
= launch_application_registration_thread(&ust_cmd_queue
);
1810 if (!register_apps_thread
) {
1815 /* Create thread to manage application socket */
1816 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1821 /* Create thread to manage application notify socket */
1822 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1827 /* Create agent management thread. */
1828 if (!launch_agent_management_thread()) {
1833 /* Don't start this thread if kernel tracing is not requested nor root */
1834 if (is_root
&& !the_config
.no_kernel
) {
1835 /* Create kernel thread to manage kernel event */
1836 if (!launch_kernel_management_thread(the_kernel_poll_pipe
[0])) {
1841 if (kernel_get_notification_fd() >= 0) {
1842 ret
= notification_thread_command_add_tracer_event_source(
1843 the_notification_thread_handle
,
1844 kernel_get_notification_fd(),
1845 LTTNG_DOMAIN_KERNEL
);
1846 if (ret
!= LTTNG_OK
) {
1847 ERR("Failed to add kernel trigger event source to notification thread");
1854 /* Load sessions. */
1855 ret
= config_load_session(the_config
.load_session_path
.value
, nullptr, 1, 1, nullptr);
1857 ERR("Session load failed: %s", error_get_str(ret
));
1862 /* Initialization completed. */
1863 sessiond_signal_parents();
1866 * This is where we start awaiting program completion (e.g. through
1867 * signal that asks threads to teardown).
1870 /* Initiate teardown once activity occurs on the main quit pipe. */
1871 sessiond_wait_for_main_quit_pipe(-1);
1875 DBG("Terminating all threads");
1878 * Ensure that the client thread is no longer accepting new commands,
1879 * which could cause new sessions to be created.
1881 if (client_thread
) {
1882 lttng_thread_shutdown(client_thread
);
1883 lttng_thread_put(client_thread
);
1886 destroy_all_sessions_and_wait();
1889 * At this point no new trigger can be registered (no sessions are
1890 * running/rotating) and clients can't connect to the session daemon
1891 * anymore. Unregister all triggers.
1893 unregister_all_triggers();
1895 if (register_apps_thread
) {
1896 lttng_thread_shutdown(register_apps_thread
);
1897 lttng_thread_put(register_apps_thread
);
1899 lttng_thread_list_shutdown_orphans();
1902 * Wait for all pending call_rcu work to complete before tearing
1903 * down data structures. call_rcu worker may be trying to
1904 * perform lookups in those structures.
1908 rcu_thread_online();
1912 * Wait for all pending call_rcu work to complete before shutting down
1913 * the notification thread. This call_rcu work includes shutting down
1914 * UST apps and event notifier pipes.
1918 if (notification_thread
) {
1919 lttng_thread_shutdown(notification_thread
);
1920 lttng_thread_put(notification_thread
);
1924 * Error accounting teardown has to be done after the teardown of all
1925 * event notifier pipes to ensure that no tracer may try to use the
1926 * error accounting facilities.
1928 event_notifier_error_accounting_fini();
1931 * Unloading the kernel modules needs to be done after all kernel
1932 * ressources have been released. In our case, this includes the
1933 * notification fd, the event notifier group fd, error accounting fd,
1934 * all event and event notifier fds, etc.
1936 * In short, at this point, we need to have called close() on all fds
1937 * received from the kernel tracer.
1939 if (is_root
&& !the_config
.no_kernel
) {
1940 DBG("Unloading kernel modules");
1941 modprobe_remove_lttng_all();
1944 rcu_thread_offline();
1945 rcu_unregister_thread();
1948 * After the rotation and timer thread have quit, we can safely destroy
1949 * the rotation_timer_queue.
1951 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1953 * The teardown of the notification system is performed after the
1954 * session daemon's teardown in order to allow it to be notified
1955 * of the active session and channels at the moment of the teardown.
1957 if (the_notification_thread_handle
) {
1958 notification_thread_handle_destroy(the_notification_thread_handle
);
1960 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1961 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1962 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1964 if (the_health_sessiond
) {
1965 health_app_destroy(the_health_sessiond
);
1967 exit_create_run_as_worker_cleanup
:
1969 sessiond_cleanup_lock_file();
1970 sessiond_cleanup_options();
1972 exit_set_signal_handler
:
1980 int main(int argc
, char **argv
)
1983 return _main(argc
, argv
);
1984 } catch (const std::exception
& e
) {
1985 ERR_FMT("Unhandled exception caught by main thread: %s", e
.what());