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/logging-utils.hpp>
56 #include <common/path.hpp>
57 #include <common/relayd/relayd.hpp>
58 #include <common/utils.hpp>
60 #include <lttng/event-internal.hpp>
74 #include <sys/mount.h>
75 #include <sys/resource.h>
76 #include <sys/socket.h>
78 #include <sys/types.h>
81 #include <urcu/uatomic.h>
83 static const char *help_msg
=
84 #ifdef LTTNG_EMBED_HELP
85 #include <lttng-sessiond.8.h>
91 #define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
92 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "event-notifier-error-buffer-size"
93 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR \
94 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-kernel"
95 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR \
96 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-userspace"
99 static int lockfile_fd
= -1;
100 static int opt_print_version
;
102 /* Set to 1 when a SIGUSR1 signal is received. */
103 static int recv_child_signal
;
105 /* Command line options */
106 static const struct option long_options
[] = {
107 { "client-sock", required_argument
, nullptr, 'c' },
108 { "apps-sock", required_argument
, nullptr, 'a' },
109 { "kconsumerd-cmd-sock", required_argument
, nullptr, '\0' },
110 { "kconsumerd-err-sock", required_argument
, nullptr, '\0' },
111 { "ustconsumerd32-cmd-sock", required_argument
, nullptr, '\0' },
112 { "ustconsumerd32-err-sock", required_argument
, nullptr, '\0' },
113 { "ustconsumerd64-cmd-sock", required_argument
, nullptr, '\0' },
114 { "ustconsumerd64-err-sock", required_argument
, nullptr, '\0' },
115 { "consumerd32-path", required_argument
, nullptr, '\0' },
116 { "consumerd32-libdir", required_argument
, nullptr, '\0' },
117 { "consumerd64-path", required_argument
, nullptr, '\0' },
118 { "consumerd64-libdir", required_argument
, nullptr, '\0' },
119 { "daemonize", no_argument
, nullptr, 'd' },
120 { "background", no_argument
, nullptr, 'b' },
121 { "sig-parent", no_argument
, nullptr, 'S' },
122 { "help", no_argument
, nullptr, 'h' },
123 { "group", required_argument
, nullptr, 'g' },
124 { "version", no_argument
, nullptr, 'V' },
125 { "quiet", no_argument
, nullptr, 'q' },
126 { "verbose", no_argument
, nullptr, 'v' },
127 { "verbose-consumer", no_argument
, nullptr, '\0' },
128 { "no-kernel", no_argument
, nullptr, '\0' },
129 { "pidfile", required_argument
, nullptr, 'p' },
130 { "agent-tcp-port", required_argument
, nullptr, '\0' },
131 { "config", required_argument
, nullptr, 'f' },
132 { "load", required_argument
, nullptr, 'l' },
133 { "kmod-probes", required_argument
, nullptr, '\0' },
134 { "extra-kmod-probes", required_argument
, nullptr, '\0' },
135 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, required_argument
, nullptr, '\0' },
136 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, required_argument
, nullptr, '\0' },
137 { nullptr, 0, nullptr, 0 }
140 /* Command line options to ignore from configuration file */
141 static const char *config_ignore_options
[] = { "help", "version", "config" };
144 * This pipe is used to inform the thread managing application communication
145 * that a command is queued and ready to be processed.
147 static int apps_cmd_pipe
[2] = { -1, -1 };
148 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
151 * UST registration command queue. This queue is tied with a futex and uses a N
152 * wakers / 1 waiter implemented and detailed in futex.c/.h
154 * The thread_registration_apps and thread_dispatch_ust_registration uses this
155 * queue along with the wait/wake scheme. The thread_manage_apps receives down
156 * the line new application socket and monitors it for any I/O error or clean
157 * close that triggers an unregistration of the application.
159 static struct ust_cmd_queue ust_cmd_queue
;
162 * Section name to look for in the daemon configuration file.
164 static const char *const config_section_name
= "sessiond";
166 /* Am I root or not. Set to 1 if the daemon is running as root */
170 * Notify the main thread to initiate the teardown of the worker threads by
171 * writing to the main quit pipe.
173 static void notify_main_quit_pipe()
177 /* Stopping all threads */
178 DBG("Notify the main thread to terminate all worker threads");
179 ret
= sessiond_notify_main_quit_pipe();
181 ERR("write error on main quit pipe");
186 * Close every consumer sockets.
188 static void close_consumer_sockets()
192 if (the_kconsumer_data
.err_sock
>= 0) {
193 ret
= close(the_kconsumer_data
.err_sock
);
195 PERROR("kernel consumer err_sock close");
198 if (the_ustconsumer32_data
.err_sock
>= 0) {
199 ret
= close(the_ustconsumer32_data
.err_sock
);
201 PERROR("UST consumerd32 err_sock close");
204 if (the_ustconsumer64_data
.err_sock
>= 0) {
205 ret
= close(the_ustconsumer64_data
.err_sock
);
207 PERROR("UST consumerd64 err_sock close");
210 if (the_kconsumer_data
.cmd_sock
>= 0) {
211 ret
= close(the_kconsumer_data
.cmd_sock
);
213 PERROR("kernel consumer cmd_sock close");
216 if (the_ustconsumer32_data
.cmd_sock
>= 0) {
217 ret
= close(the_ustconsumer32_data
.cmd_sock
);
219 PERROR("UST consumerd32 cmd_sock close");
222 if (the_ustconsumer64_data
.cmd_sock
>= 0) {
223 ret
= close(the_ustconsumer64_data
.cmd_sock
);
225 PERROR("UST consumerd64 cmd_sock close");
228 if (the_kconsumer_data
.channel_monitor_pipe
>= 0) {
229 ret
= close(the_kconsumer_data
.channel_monitor_pipe
);
231 PERROR("kernel consumer channel monitor pipe close");
234 if (the_ustconsumer32_data
.channel_monitor_pipe
>= 0) {
235 ret
= close(the_ustconsumer32_data
.channel_monitor_pipe
);
237 PERROR("UST consumerd32 channel monitor pipe close");
240 if (the_ustconsumer64_data
.channel_monitor_pipe
>= 0) {
241 ret
= close(the_ustconsumer64_data
.channel_monitor_pipe
);
243 PERROR("UST consumerd64 channel monitor pipe close");
249 * Wait on consumer process termination.
251 * Need to be called with the consumer data lock held or from a context
252 * ensuring no concurrent access to data (e.g: cleanup).
254 static void wait_consumer(struct consumer_data
*consumer_data
)
259 if (consumer_data
->pid
<= 0) {
263 DBG("Waiting for complete teardown of consumerd (PID: %d)", consumer_data
->pid
);
264 ret
= waitpid(consumer_data
->pid
, &status
, 0);
266 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
267 } else if (!WIFEXITED(status
)) {
268 ERR("consumerd termination with error: %d", WEXITSTATUS(ret
));
270 consumer_data
->pid
= 0;
274 * Cleanup the session daemon's data structures.
276 static void sessiond_cleanup()
279 struct ltt_session_list
*session_list
= session_get_list();
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 pthread_mutex_destroy(&session_list
->lock
);
331 DBG("Cleaning up all per-event notifier domain agents");
332 agent_by_event_notifier_domain_ht_destroy();
334 DBG("Cleaning up all agent apps");
335 agent_app_ht_clean();
336 DBG("Closing all UST sockets");
337 ust_app_clean_list();
338 buffer_reg_destroy_registries();
340 close_consumer_sockets();
342 wait_consumer(&the_kconsumer_data
);
343 wait_consumer(&the_ustconsumer64_data
);
344 wait_consumer(&the_ustconsumer32_data
);
346 if (is_root
&& !the_config
.no_kernel
) {
347 cleanup_kernel_tracer();
351 * We do NOT rmdir rundir because there are other processes
352 * using it, for instance lttng-relayd, which can start in
353 * parallel with this teardown.
358 * Cleanup the daemon's option data structures.
360 static void sessiond_cleanup_options()
362 DBG("Cleaning up options");
364 sessiond_config_fini(&the_config
);
366 run_as_destroy_worker();
369 static int string_match(const char *str1
, const char *str2
)
371 return (str1
&& str2
) && !strcmp(str1
, str2
);
375 * Take an option from the getopt output and set it in the right variable to be
378 * Return 0 on success else a negative value.
380 static int set_option(int opt
, const char *arg
, const char *optname
)
384 if (string_match(optname
, "client-sock") || opt
== 'c') {
385 if (!arg
|| *arg
== '\0') {
389 if (lttng_is_setuid_setgid()) {
390 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
391 "-c, --client-sock");
393 config_string_set(&the_config
.client_unix_sock_path
, strdup(arg
));
394 if (!the_config
.client_unix_sock_path
.value
) {
399 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
400 if (!arg
|| *arg
== '\0') {
404 if (lttng_is_setuid_setgid()) {
405 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
408 config_string_set(&the_config
.apps_unix_sock_path
, strdup(arg
));
409 if (!the_config
.apps_unix_sock_path
.value
) {
414 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
415 the_config
.daemonize
= true;
416 } else if (string_match(optname
, "background") || opt
== 'b') {
417 the_config
.background
= true;
418 } else if (string_match(optname
, "group") || opt
== 'g') {
419 if (!arg
|| *arg
== '\0') {
423 if (lttng_is_setuid_setgid()) {
424 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
427 config_string_set(&the_config
.tracing_group_name
, strdup(arg
));
428 if (!the_config
.tracing_group_name
.value
) {
433 } else if (string_match(optname
, "help") || opt
== 'h') {
434 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
436 ERR("Cannot show --help for `lttng-sessiond`");
439 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
440 } else if (string_match(optname
, "version") || opt
== 'V') {
441 opt_print_version
= 1;
442 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
443 the_config
.sig_parent
= true;
444 } else if (string_match(optname
, "kconsumerd-err-sock")) {
445 if (!arg
|| *arg
== '\0') {
449 if (lttng_is_setuid_setgid()) {
450 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
451 "--kconsumerd-err-sock");
453 config_string_set(&the_config
.kconsumerd_err_unix_sock_path
, strdup(arg
));
454 if (!the_config
.kconsumerd_err_unix_sock_path
.value
) {
459 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
460 if (!arg
|| *arg
== '\0') {
464 if (lttng_is_setuid_setgid()) {
465 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
466 "--kconsumerd-cmd-sock");
468 config_string_set(&the_config
.kconsumerd_cmd_unix_sock_path
, strdup(arg
));
469 if (!the_config
.kconsumerd_cmd_unix_sock_path
.value
) {
474 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
475 if (!arg
|| *arg
== '\0') {
479 if (lttng_is_setuid_setgid()) {
480 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
481 "--ustconsumerd64-err-sock");
483 config_string_set(&the_config
.consumerd64_err_unix_sock_path
, strdup(arg
));
484 if (!the_config
.consumerd64_err_unix_sock_path
.value
) {
489 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
490 if (!arg
|| *arg
== '\0') {
494 if (lttng_is_setuid_setgid()) {
495 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
496 "--ustconsumerd64-cmd-sock");
498 config_string_set(&the_config
.consumerd64_cmd_unix_sock_path
, strdup(arg
));
499 if (!the_config
.consumerd64_cmd_unix_sock_path
.value
) {
504 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
505 if (!arg
|| *arg
== '\0') {
509 if (lttng_is_setuid_setgid()) {
510 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
511 "--ustconsumerd32-err-sock");
513 config_string_set(&the_config
.consumerd32_err_unix_sock_path
, strdup(arg
));
514 if (!the_config
.consumerd32_err_unix_sock_path
.value
) {
519 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
520 if (!arg
|| *arg
== '\0') {
524 if (lttng_is_setuid_setgid()) {
525 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
526 "--ustconsumerd32-cmd-sock");
528 config_string_set(&the_config
.consumerd32_cmd_unix_sock_path
, strdup(arg
));
529 if (!the_config
.consumerd32_cmd_unix_sock_path
.value
) {
534 } else if (string_match(optname
, "no-kernel")) {
535 the_config
.no_kernel
= true;
536 } else if (string_match(optname
, "quiet") || opt
== 'q') {
537 the_config
.quiet
= true;
538 } else if (string_match(optname
, "verbose") || opt
== 'v') {
539 /* Verbose level can increase using multiple -v */
541 /* Value obtained from config file */
542 the_config
.verbose
= config_parse_value(arg
);
544 /* -v used on command line */
545 the_config
.verbose
++;
547 /* Clamp value to [0, 3] */
548 the_config
.verbose
= the_config
.verbose
< 0 ?
550 (the_config
.verbose
<= 3 ? the_config
.verbose
: 3);
551 } else if (string_match(optname
, "verbose-consumer")) {
553 the_config
.verbose_consumer
= config_parse_value(arg
);
555 the_config
.verbose_consumer
++;
557 } else if (string_match(optname
, "consumerd32-path")) {
558 if (!arg
|| *arg
== '\0') {
562 if (lttng_is_setuid_setgid()) {
563 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
564 "--consumerd32-path");
566 config_string_set(&the_config
.consumerd32_bin_path
, strdup(arg
));
567 if (!the_config
.consumerd32_bin_path
.value
) {
572 } else if (string_match(optname
, "consumerd32-libdir")) {
573 if (!arg
|| *arg
== '\0') {
577 if (lttng_is_setuid_setgid()) {
578 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
579 "--consumerd32-libdir");
581 config_string_set(&the_config
.consumerd32_lib_dir
, strdup(arg
));
582 if (!the_config
.consumerd32_lib_dir
.value
) {
587 } else if (string_match(optname
, "consumerd64-path")) {
588 if (!arg
|| *arg
== '\0') {
592 if (lttng_is_setuid_setgid()) {
593 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
594 "--consumerd64-path");
596 config_string_set(&the_config
.consumerd64_bin_path
, strdup(arg
));
597 if (!the_config
.consumerd64_bin_path
.value
) {
602 } else if (string_match(optname
, "consumerd64-libdir")) {
603 if (!arg
|| *arg
== '\0') {
607 if (lttng_is_setuid_setgid()) {
608 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
609 "--consumerd64-libdir");
611 config_string_set(&the_config
.consumerd64_lib_dir
, strdup(arg
));
612 if (!the_config
.consumerd64_lib_dir
.value
) {
617 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
618 if (!arg
|| *arg
== '\0') {
622 if (lttng_is_setuid_setgid()) {
623 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
626 config_string_set(&the_config
.pid_file_path
, strdup(arg
));
627 if (!the_config
.pid_file_path
.value
) {
632 } else if (string_match(optname
, "agent-tcp-port")) {
633 if (!arg
|| *arg
== '\0') {
637 if (lttng_is_setuid_setgid()) {
638 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
644 v
= strtoul(arg
, nullptr, 0);
645 if (errno
!= 0 || !isdigit(arg
[0])) {
646 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
649 if (v
== 0 || v
>= 65535) {
650 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
653 the_config
.agent_tcp_port
.begin
= the_config
.agent_tcp_port
.end
= (int) v
;
654 DBG3("Agent TCP port set to non default: %i", (int) v
);
656 } else if (string_match(optname
, "load") || opt
== 'l') {
657 if (!arg
|| *arg
== '\0') {
661 if (lttng_is_setuid_setgid()) {
662 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
665 config_string_set(&the_config
.load_session_path
, strdup(arg
));
666 if (!the_config
.load_session_path
.value
) {
671 } else if (string_match(optname
, "kmod-probes")) {
672 if (!arg
|| *arg
== '\0') {
676 if (lttng_is_setuid_setgid()) {
677 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
680 config_string_set(&the_config
.kmod_probes_list
, strdup(arg
));
681 if (!the_config
.kmod_probes_list
.value
) {
686 } else if (string_match(optname
, "extra-kmod-probes")) {
687 if (!arg
|| *arg
== '\0') {
691 if (lttng_is_setuid_setgid()) {
692 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
693 "--extra-kmod-probes");
695 config_string_set(&the_config
.kmod_extra_probes_list
, strdup(arg
));
696 if (!the_config
.kmod_extra_probes_list
.value
) {
701 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
)) {
705 v
= strtoul(arg
, nullptr, 0);
706 if (errno
!= 0 || !isdigit(arg
[0])) {
707 ERR("Wrong value in --%s parameter: %s",
708 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
,
712 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
713 ERR("Value out of range for --%s parameter: %s",
714 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
,
718 the_config
.event_notifier_buffer_size_kernel
= (int) v
;
719 DBG3("Number of event notifier error buffer kernel size to non default: %i",
720 the_config
.event_notifier_buffer_size_kernel
);
722 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
)) {
726 v
= strtoul(arg
, nullptr, 0);
727 if (errno
!= 0 || !isdigit(arg
[0])) {
728 ERR("Wrong value in --%s parameter: %s",
729 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
,
733 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
734 ERR("Value out of range for --%s parameter: %s",
735 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
,
739 the_config
.event_notifier_buffer_size_userspace
= (int) v
;
740 DBG3("Number of event notifier error buffer userspace size to non default: %i",
741 the_config
.event_notifier_buffer_size_userspace
);
743 } else if (string_match(optname
, "config") || opt
== 'f') {
744 /* This is handled in set_options() thus silent skip. */
747 /* Unknown option or other error.
748 * Error is printed by getopt, just return */
753 if (ret
== -EINVAL
) {
754 const char *opt_name
= "unknown";
757 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
); i
++) {
758 if (opt
== long_options
[i
].val
) {
759 opt_name
= long_options
[i
].name
;
764 WARN("Invalid argument provided for option \"%s\", using default value.", opt_name
);
771 * config_entry_handler_cb used to handle options read from a config file.
772 * See config_entry_handler_cb comment in common/config/session-config.h for the
773 * return value conventions.
775 static int config_entry_handler(const struct config_entry
*entry
,
776 void *unused
__attribute__((unused
)))
780 if (!entry
|| !entry
->name
|| !entry
->value
) {
785 /* Check if the option is to be ignored */
786 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
787 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
792 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1; i
++) {
793 /* Ignore if not fully matched. */
794 if (strcmp(entry
->name
, long_options
[i
].name
) != 0) {
799 * If the option takes no argument on the command line, we have to
800 * check if the value is "true". We support non-zero numeric values,
803 if (!long_options
[i
].has_arg
) {
804 ret
= config_parse_value(entry
->value
);
807 WARN("Invalid configuration value \"%s\" for option %s",
811 /* False, skip boolean config option. */
816 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
820 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
826 static void print_version()
828 fprintf(stdout
, "%s\n", VERSION
);
832 * daemon configuration loading and argument parsing
834 static int set_options(int argc
, char **argv
)
836 int ret
= 0, c
= 0, option_index
= 0;
837 int orig_optopt
= optopt
, orig_optind
= optind
;
839 char *config_path
= nullptr;
841 optstring
= utils_generate_optstring(long_options
,
842 sizeof(long_options
) / sizeof(struct option
));
848 /* Check for the --config option */
849 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
)) != -1) {
853 } else if (c
!= 'f') {
854 /* if not equal to --config option. */
858 if (lttng_is_setuid_setgid()) {
859 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
863 config_path
= utils_expand_path(optarg
);
865 ERR("Failed to resolve path: %s", optarg
);
870 ret
= config_get_section_entries(
871 config_path
, config_section_name
, config_entry_handler
, nullptr);
874 ERR("Invalid configuration option at line %i", ret
);
880 /* Reset getopt's global state */
881 optopt
= orig_optopt
;
882 optind
= orig_optind
;
886 * getopt_long() will not set option_index if it encounters a
889 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
895 * Pass NULL as the long option name if popt left the index
899 c
, optarg
, option_index
< 0 ? nullptr : long_options
[option_index
].name
);
912 * Create lockfile using the rundir and return its fd.
914 static int create_lockfile()
916 return utils_create_lock_file(the_config
.lock_file_path
.value
);
920 * Check if the global socket is available, and if a daemon is answering at the
921 * other side. If yes, error is returned.
923 * Also attempts to create and hold the lock file.
925 static int check_existing_daemon()
929 /* Is there anybody out there ? */
930 if (lttng_session_daemon_alive()) {
935 lockfile_fd
= create_lockfile();
936 if (lockfile_fd
< 0) {
944 static void sessiond_cleanup_lock_file()
949 * Cleanup lock file by deleting it and finaly closing it which will
950 * release the file system lock.
952 if (lockfile_fd
>= 0) {
953 ret
= remove(the_config
.lock_file_path
.value
);
955 PERROR("remove lock file");
957 ret
= close(lockfile_fd
);
959 PERROR("close lock file");
965 * Set the tracing group gid onto the client socket.
967 * Race window between mkdir and chown is OK because we are going from more
968 * permissive (root.root) to less permissive (root.tracing).
970 static int set_permissions(char *rundir
)
975 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
, true, &gid
);
977 /* Default to root group. */
981 /* Set lttng run dir */
982 ret
= chown(rundir
, 0, gid
);
984 ERR("Unable to set group on %s", rundir
);
989 * Ensure all applications and tracing group can search the run
990 * dir. Allow everyone to read the directory, since it does not
991 * buy us anything to hide its content.
993 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
995 ERR("Unable to set permissions on %s", rundir
);
999 /* lttng client socket path */
1000 ret
= chown(the_config
.client_unix_sock_path
.value
, 0, gid
);
1002 ERR("Unable to set group on %s", the_config
.client_unix_sock_path
.value
);
1006 /* kconsumer error socket path */
1007 ret
= chown(the_kconsumer_data
.err_unix_sock_path
, 0, 0);
1009 ERR("Unable to set group on %s", the_kconsumer_data
.err_unix_sock_path
);
1013 /* 64-bit ustconsumer error socket path */
1014 ret
= chown(the_ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1016 ERR("Unable to set group on %s", the_ustconsumer64_data
.err_unix_sock_path
);
1020 /* 32-bit ustconsumer compat32 error socket path */
1021 ret
= chown(the_ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1023 ERR("Unable to set group on %s", the_ustconsumer32_data
.err_unix_sock_path
);
1027 DBG("All permissions are set");
1033 * Create the lttng run directory needed for all global sockets and pipe.
1035 static int create_lttng_rundir()
1039 DBG3("Creating LTTng run directory: %s", the_config
.rundir
.value
);
1041 ret
= mkdir(the_config
.rundir
.value
, S_IRWXU
);
1043 if (errno
!= EEXIST
) {
1044 ERR("Unable to create %s", the_config
.rundir
.value
);
1056 * Setup sockets and directory needed by the consumerds' communication with the
1059 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1062 char *path
= nullptr;
1064 switch (consumer_data
->type
) {
1065 case LTTNG_CONSUMER_KERNEL
:
1066 path
= the_config
.kconsumerd_path
.value
;
1068 case LTTNG_CONSUMER64_UST
:
1069 path
= the_config
.consumerd64_path
.value
;
1071 case LTTNG_CONSUMER32_UST
:
1072 path
= the_config
.consumerd32_path
.value
;
1075 ERR("Consumer type unknown");
1081 DBG2("Creating consumer directory: %s", path
);
1083 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1084 if (ret
< 0 && errno
!= EEXIST
) {
1086 ERR("Failed to create %s", path
);
1092 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
, true, &gid
);
1094 /* Default to root group. */
1098 ret
= chown(path
, 0, gid
);
1100 ERR("Unable to set group on %s", path
);
1106 /* Create the consumerd error unix socket */
1107 consumer_data
->err_sock
= lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1108 if (consumer_data
->err_sock
< 0) {
1109 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1115 * Set the CLOEXEC flag. Return code is useless because either way, the
1118 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1120 PERROR("utils_set_fd_cloexec");
1121 /* continue anyway */
1124 /* File permission MUST be 660 */
1125 ret
= chmod(consumer_data
->err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1127 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1137 * Signal handler for the daemon
1139 * Simply stop all worker threads, leaving main() return gracefully after
1140 * joining all threads and calling cleanup().
1142 static void sighandler(int sig
, siginfo_t
*siginfo
, void *arg
__attribute__((unused
)))
1146 DBG("SIGINT caught");
1147 notify_main_quit_pipe();
1150 DBG("SIGTERM caught");
1151 notify_main_quit_pipe();
1154 CMM_STORE_SHARED(recv_child_signal
, 1);
1159 const char msg
[] = "Received SIGBUS, aborting program.\n";
1161 lttng_ust_handle_sigbus(siginfo
->si_addr
);
1163 * If ustctl did not catch this signal (triggering a
1164 * siglongjmp), abort the program. Otherwise, the execution
1165 * will resume from the ust-ctl call which caused this error.
1167 * The return value is ignored since the program aborts anyhow.
1169 write_ret
= write(STDERR_FILENO
, msg
, sizeof(msg
));
1179 * Setup signal handler for :
1180 * SIGINT, SIGTERM, SIGPIPE
1182 static int set_signal_handler()
1185 struct sigaction sa
;
1188 if ((ret
= sigemptyset(&sigset
)) < 0) {
1189 PERROR("sigemptyset");
1193 sa
.sa_mask
= sigset
;
1194 sa
.sa_flags
= SA_SIGINFO
;
1196 sa
.sa_sigaction
= sighandler
;
1197 if ((ret
= sigaction(SIGTERM
, &sa
, nullptr)) < 0) {
1198 PERROR("sigaction");
1202 if ((ret
= sigaction(SIGINT
, &sa
, nullptr)) < 0) {
1203 PERROR("sigaction");
1207 if ((ret
= sigaction(SIGUSR1
, &sa
, nullptr)) < 0) {
1208 PERROR("sigaction");
1212 if ((ret
= sigaction(SIGBUS
, &sa
, nullptr)) < 0) {
1213 PERROR("sigaction");
1218 sa
.sa_handler
= SIG_IGN
;
1219 if ((ret
= sigaction(SIGPIPE
, &sa
, nullptr)) < 0) {
1220 PERROR("sigaction");
1224 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE, SIGINT, and SIGBUS");
1230 * Set open files limit to unlimited. This daemon can open a large number of
1231 * file descriptors in order to consume multiple kernel traces.
1233 static void set_ulimit()
1238 /* The kernel does not allow an infinite limit for open files */
1239 lim
.rlim_cur
= 65535;
1240 lim
.rlim_max
= 65535;
1242 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1244 PERROR("failed to set open files limit");
1248 static int write_pidfile()
1250 return utils_create_pid_file(getpid(), the_config
.pid_file_path
.value
);
1253 static int set_clock_plugin_env()
1256 char *env_value
= nullptr;
1258 if (!the_config
.lttng_ust_clock_plugin
.value
) {
1263 &env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s", the_config
.lttng_ust_clock_plugin
.value
);
1269 ret
= putenv(env_value
);
1272 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1276 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1277 the_config
.lttng_ust_clock_plugin
.value
);
1282 static void destroy_all_sessions_and_wait()
1284 struct ltt_session
*session
, *tmp
;
1285 struct ltt_session_list
*session_list
;
1287 session_list
= session_get_list();
1288 DBG("Initiating destruction of all sessions");
1290 if (!session_list
) {
1294 session_lock_list();
1295 /* Initiate the destruction of all sessions. */
1296 cds_list_for_each_entry_safe (session
, tmp
, &session_list
->head
, list
) {
1297 if (!session_get(session
)) {
1301 session_lock(session
);
1302 if (session
->destroyed
) {
1303 goto unlock_session
;
1305 (void) cmd_stop_trace(session
);
1306 (void) cmd_destroy_session(session
, the_notification_thread_handle
, nullptr);
1308 session_unlock(session
);
1309 session_put(session
);
1311 session_unlock_list();
1313 /* Wait for the destruction of all sessions to complete. */
1314 DBG("Waiting for the destruction of all sessions to complete");
1315 session_list_wait_empty();
1316 DBG("Destruction of all sessions completed");
1319 static void unregister_all_triggers()
1321 enum lttng_error_code ret_code
;
1322 enum lttng_trigger_status trigger_status
;
1323 struct lttng_triggers
*triggers
= nullptr;
1324 unsigned int trigger_count
, i
;
1325 const struct lttng_credentials creds
= {
1326 .uid
= LTTNG_OPTIONAL_INIT_VALUE(0),
1327 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
1330 DBG("Unregistering all triggers");
1333 * List all triggers as "root" since we wish to unregister all triggers.
1335 ret_code
= notification_thread_command_list_triggers(
1336 the_notification_thread_handle
, creds
.uid
.value
, &triggers
);
1337 if (ret_code
!= LTTNG_OK
) {
1338 ERR("Failed to list triggers while unregistering all triggers");
1342 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
1343 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1345 for (i
= 0; i
< trigger_count
; i
++) {
1346 uid_t trigger_owner
;
1347 const char *trigger_name
;
1348 const struct lttng_trigger
*trigger
= lttng_triggers_get_at_index(triggers
, i
);
1350 LTTNG_ASSERT(trigger
);
1352 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
1353 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1355 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
1356 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
:
1359 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1360 (int) trigger_owner
,
1363 ret_code
= cmd_unregister_trigger(&creds
, trigger
, the_notification_thread_handle
);
1364 if (ret_code
!= LTTNG_OK
) {
1365 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1366 (int) trigger_owner
,
1368 lttng_strerror(-ret_code
));
1369 /* Continue to unregister the remaining triggers. */
1373 lttng_triggers_destroy(triggers
);
1376 static int run_as_worker_post_fork_cleanup(void *data
)
1378 struct sessiond_config
*sessiond_config
= (struct sessiond_config
*) data
;
1380 sessiond_config_fini(sessiond_config
);
1384 static int launch_run_as_worker(const char *procname
)
1387 * Clean-up before forking the run-as worker. Any dynamically
1388 * allocated memory of which the worker is not aware will
1389 * be leaked as the process forks a run-as worker (and performs
1390 * no exec*()). The same would apply to any opened fd.
1392 return run_as_create_worker(procname
, run_as_worker_post_fork_cleanup
, &the_config
);
1395 static void sessiond_uuid_log()
1397 char uuid_str
[LTTNG_UUID_STR_LEN
];
1399 lttng_uuid_to_str(the_sessiond_uuid
, uuid_str
);
1400 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1406 int main(int argc
, char **argv
)
1408 int ret
= 0, retval
= 0;
1409 const char *env_app_timeout
;
1410 struct lttng_pipe
*ust32_channel_monitor_pipe
= nullptr,
1411 *ust64_channel_monitor_pipe
= nullptr,
1412 *kernel_channel_monitor_pipe
= nullptr;
1413 struct timer_thread_parameters timer_thread_parameters
;
1414 /* Rotation thread handle. */
1415 struct rotation_thread_handle
*rotation_thread_handle
= nullptr;
1416 /* Queue of rotation jobs populated by the sessiond-timer. */
1417 struct rotation_thread_timer_queue
*rotation_timer_queue
= nullptr;
1418 struct lttng_thread
*client_thread
= nullptr;
1419 struct lttng_thread
*notification_thread
= nullptr;
1420 struct lttng_thread
*register_apps_thread
= nullptr;
1421 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
1423 logger_set_thread_name("Main", false);
1424 init_kernel_workarounds();
1426 rcu_register_thread();
1428 if (set_signal_handler()) {
1430 goto exit_set_signal_handler
;
1433 if (timer_signal_init()) {
1435 goto exit_set_signal_handler
;
1438 the_page_size
= sysconf(_SC_PAGE_SIZE
);
1439 if (the_page_size
< 0) {
1440 PERROR("sysconf _SC_PAGE_SIZE");
1441 the_page_size
= LONG_MAX
;
1442 WARN("Fallback page size to %ld", the_page_size
);
1445 ret
= sessiond_config_init(&the_config
);
1448 goto exit_set_signal_handler
;
1452 * Init config from environment variables.
1453 * Command line option override env configuration per-doc. Do env first.
1455 sessiond_config_apply_env_config(&the_config
);
1458 * Parse arguments and load the daemon configuration file.
1460 * We have an exit_options exit path to free memory reserved by
1464 if (set_options(argc
, argv
)) {
1470 * Resolve all paths received as arguments, configuration option, or
1471 * through environment variable as absolute paths. This is necessary
1472 * since daemonizing causes the sessiond's current working directory
1475 ret
= sessiond_config_resolve_paths(&the_config
);
1481 lttng_opt_verbose
= the_config
.verbose
;
1482 lttng_opt_quiet
= the_config
.quiet
;
1483 the_kconsumer_data
.err_unix_sock_path
= the_config
.kconsumerd_err_unix_sock_path
.value
;
1484 the_kconsumer_data
.cmd_unix_sock_path
= the_config
.kconsumerd_cmd_unix_sock_path
.value
;
1485 the_ustconsumer32_data
.err_unix_sock_path
= the_config
.consumerd32_err_unix_sock_path
.value
;
1486 the_ustconsumer32_data
.cmd_unix_sock_path
= the_config
.consumerd32_cmd_unix_sock_path
.value
;
1487 the_ustconsumer64_data
.err_unix_sock_path
= the_config
.consumerd64_err_unix_sock_path
.value
;
1488 the_ustconsumer64_data
.cmd_unix_sock_path
= the_config
.consumerd64_cmd_unix_sock_path
.value
;
1489 set_clock_plugin_env();
1491 sessiond_config_log(&the_config
);
1492 sessiond_uuid_log();
1493 lttng::logging::log_system_information(PRINT_DBG
);
1495 if (opt_print_version
) {
1501 if (create_lttng_rundir()) {
1506 /* Abort launch if a session daemon is already running. */
1507 if (check_existing_daemon()) {
1508 ERR("A session daemon is already running.");
1514 if (the_config
.daemonize
|| the_config
.background
) {
1517 ret
= lttng_daemonize(&the_child_ppid
, &recv_child_signal
, !the_config
.background
);
1524 * We are in the child. Make sure all other file descriptors are
1525 * closed, in case we are called with more opened file
1526 * descriptors than the standard ones and the lock file.
1528 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1529 if (i
== lockfile_fd
) {
1536 if (launch_run_as_worker(argv
[0]) < 0) {
1537 goto exit_create_run_as_worker_cleanup
;
1541 * Starting from here, we can create threads. This needs to be after
1542 * lttng_daemonize due to RCU.
1546 * Initialize the health check subsystem. This call should set the
1547 * appropriate time values.
1549 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1550 if (!the_health_sessiond
) {
1551 PERROR("health_app_create error");
1556 /* Create main quit pipe */
1557 if (sessiond_init_main_quit_pipe()) {
1562 /* Check if daemon is UID = 0 */
1563 is_root
= !getuid();
1565 /* Create global run dir with root access */
1567 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1568 if (!kernel_channel_monitor_pipe
) {
1569 ERR("Failed to create kernel consumer channel monitor pipe");
1573 the_kconsumer_data
.channel_monitor_pipe
=
1574 lttng_pipe_release_writefd(kernel_channel_monitor_pipe
);
1575 if (the_kconsumer_data
.channel_monitor_pipe
< 0) {
1581 /* Set consumer initial state */
1582 the_kernel_consumerd_state
= CONSUMER_STOPPED
;
1583 the_ust_consumerd_state
= CONSUMER_STOPPED
;
1585 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1586 if (!ust32_channel_monitor_pipe
) {
1587 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1591 the_ustconsumer32_data
.channel_monitor_pipe
=
1592 lttng_pipe_release_writefd(ust32_channel_monitor_pipe
);
1593 if (the_ustconsumer32_data
.channel_monitor_pipe
< 0) {
1599 * The rotation_thread_timer_queue structure is shared between the
1600 * sessiond timer thread and the rotation thread. The main thread keeps
1601 * its ownership and destroys it when both threads have been joined.
1603 rotation_timer_queue
= rotation_thread_timer_queue_create();
1604 if (!rotation_timer_queue
) {
1608 timer_thread_parameters
.rotation_thread_job_queue
= rotation_timer_queue
;
1610 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1611 if (!ust64_channel_monitor_pipe
) {
1612 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1616 the_ustconsumer64_data
.channel_monitor_pipe
=
1617 lttng_pipe_release_writefd(ust64_channel_monitor_pipe
);
1618 if (the_ustconsumer64_data
.channel_monitor_pipe
< 0) {
1624 * Init UST app hash table. Alloc hash table before this point since
1625 * cleanup() can get called after that point.
1627 if (ust_app_ht_alloc()) {
1628 ERR("Failed to allocate UST app hash table");
1633 event_notifier_error_accounting_status
= event_notifier_error_accounting_init(
1634 the_config
.event_notifier_buffer_size_kernel
,
1635 the_config
.event_notifier_buffer_size_userspace
);
1636 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1637 ERR("Failed to initialize event notifier error accounting system");
1643 * Initialize agent app hash table. We allocate the hash table here
1644 * since cleanup() can get called after this point.
1646 if (agent_app_ht_alloc()) {
1647 ERR("Failed to allocate Agent app hash table");
1652 if (agent_by_event_notifier_domain_ht_create()) {
1653 ERR("Failed to allocate per-event notifier domain agent hash table");
1658 * These actions must be executed as root. We do that *after* setting up
1659 * the sockets path because we MUST make the check for another daemon using
1660 * those paths *before* trying to set the kernel consumer sockets and init
1664 if (set_consumer_sockets(&the_kconsumer_data
)) {
1669 /* Setup kernel tracer */
1670 if (!the_config
.no_kernel
) {
1671 init_kernel_tracer();
1674 /* Set ulimit for open files */
1677 /* init lttng_fd tracking must be done after set_ulimit. */
1680 if (set_consumer_sockets(&the_ustconsumer64_data
)) {
1685 if (set_consumer_sockets(&the_ustconsumer32_data
)) {
1690 /* Get parent pid if -S, --sig-parent is specified. */
1691 if (the_config
.sig_parent
) {
1692 the_ppid
= getppid();
1695 /* Setup the kernel pipe for waking up the kernel thread */
1696 if (is_root
&& !the_config
.no_kernel
) {
1697 if (utils_create_pipe_cloexec(the_kernel_poll_pipe
)) {
1703 /* Setup the thread apps communication pipe. */
1704 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1709 /* Setup the thread apps notify communication pipe. */
1710 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1715 /* Initialize global buffer per UID and PID registry. */
1716 buffer_reg_init_uid_registry();
1717 buffer_reg_init_pid_registry();
1719 /* Init UST command queue. */
1720 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1724 /* Check for the application socket timeout env variable. */
1725 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1726 if (env_app_timeout
) {
1727 the_config
.app_socket_timeout
= atoi(env_app_timeout
);
1729 the_config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1732 ret
= write_pidfile();
1734 ERR("Error in write_pidfile");
1739 /* Initialize communication library */
1741 /* Initialize TCP timeout values */
1742 lttcomm_inet_init();
1744 /* Create health-check thread. */
1745 if (!launch_health_management_thread()) {
1750 /* notification_thread_data acquires the pipes' read side. */
1751 the_notification_thread_handle
=
1752 notification_thread_handle_create(ust32_channel_monitor_pipe
,
1753 ust64_channel_monitor_pipe
,
1754 kernel_channel_monitor_pipe
);
1755 if (!the_notification_thread_handle
) {
1757 ERR("Failed to create notification thread shared data");
1761 /* Create notification thread. */
1762 notification_thread
= launch_notification_thread(the_notification_thread_handle
);
1763 if (!notification_thread
) {
1768 /* Create timer thread. */
1769 if (!launch_timer_thread(&timer_thread_parameters
)) {
1774 /* rotation_thread_data acquires the pipes' read side. */
1775 rotation_thread_handle
=
1776 rotation_thread_handle_create(rotation_timer_queue
, the_notification_thread_handle
);
1777 if (!rotation_thread_handle
) {
1779 ERR("Failed to create rotation thread shared data");
1783 /* Create rotation thread. */
1784 if (!launch_rotation_thread(rotation_thread_handle
)) {
1789 /* Create thread to manage the client socket */
1790 client_thread
= launch_client_thread();
1791 if (!client_thread
) {
1796 /* Set credentials of the client socket and rundir */
1797 if (is_root
&& set_permissions(the_config
.rundir
.value
)) {
1802 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1], apps_cmd_notify_pipe
[1])) {
1807 /* Create thread to manage application registration. */
1808 register_apps_thread
= launch_application_registration_thread(&ust_cmd_queue
);
1809 if (!register_apps_thread
) {
1814 /* Create thread to manage application socket */
1815 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1820 /* Create thread to manage application notify socket */
1821 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1826 /* Create agent management thread. */
1827 if (!launch_agent_management_thread()) {
1832 /* Don't start this thread if kernel tracing is not requested nor root */
1833 if (is_root
&& !the_config
.no_kernel
) {
1834 /* Create kernel thread to manage kernel event */
1835 if (!launch_kernel_management_thread(the_kernel_poll_pipe
[0])) {
1840 if (kernel_get_notification_fd() >= 0) {
1841 ret
= notification_thread_command_add_tracer_event_source(
1842 the_notification_thread_handle
,
1843 kernel_get_notification_fd(),
1844 LTTNG_DOMAIN_KERNEL
);
1845 if (ret
!= LTTNG_OK
) {
1846 ERR("Failed to add kernel trigger event source to notification thread");
1853 /* Load sessions. */
1854 ret
= config_load_session(the_config
.load_session_path
.value
, nullptr, 1, 1, nullptr);
1856 ERR("Session load failed: %s", error_get_str(ret
));
1861 /* Initialization completed. */
1862 sessiond_signal_parents();
1865 * This is where we start awaiting program completion (e.g. through
1866 * signal that asks threads to teardown).
1869 /* Initiate teardown once activity occurs on the main quit pipe. */
1870 sessiond_wait_for_main_quit_pipe(-1);
1874 DBG("Terminating all threads");
1877 * Ensure that the client thread is no longer accepting new commands,
1878 * which could cause new sessions to be created.
1880 if (client_thread
) {
1881 lttng_thread_shutdown(client_thread
);
1882 lttng_thread_put(client_thread
);
1885 destroy_all_sessions_and_wait();
1888 * At this point no new trigger can be registered (no sessions are
1889 * running/rotating) and clients can't connect to the session daemon
1890 * anymore. Unregister all triggers.
1892 unregister_all_triggers();
1894 if (register_apps_thread
) {
1895 lttng_thread_shutdown(register_apps_thread
);
1896 lttng_thread_put(register_apps_thread
);
1898 lttng_thread_list_shutdown_orphans();
1901 * Wait for all pending call_rcu work to complete before tearing
1902 * down data structures. call_rcu worker may be trying to
1903 * perform lookups in those structures.
1907 rcu_thread_online();
1911 * Wait for all pending call_rcu work to complete before shutting down
1912 * the notification thread. This call_rcu work includes shutting down
1913 * UST apps and event notifier pipes.
1917 if (notification_thread
) {
1918 lttng_thread_shutdown(notification_thread
);
1919 lttng_thread_put(notification_thread
);
1923 * Error accounting teardown has to be done after the teardown of all
1924 * event notifier pipes to ensure that no tracer may try to use the
1925 * error accounting facilities.
1927 event_notifier_error_accounting_fini();
1930 * Unloading the kernel modules needs to be done after all kernel
1931 * ressources have been released. In our case, this includes the
1932 * notification fd, the event notifier group fd, error accounting fd,
1933 * all event and event notifier fds, etc.
1935 * In short, at this point, we need to have called close() on all fds
1936 * received from the kernel tracer.
1938 if (is_root
&& !the_config
.no_kernel
) {
1939 DBG("Unloading kernel modules");
1940 modprobe_remove_lttng_all();
1943 rcu_thread_offline();
1944 rcu_unregister_thread();
1946 if (rotation_thread_handle
) {
1947 rotation_thread_handle_destroy(rotation_thread_handle
);
1951 * After the rotation and timer thread have quit, we can safely destroy
1952 * the rotation_timer_queue.
1954 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1956 * The teardown of the notification system is performed after the
1957 * session daemon's teardown in order to allow it to be notified
1958 * of the active session and channels at the moment of the teardown.
1960 if (the_notification_thread_handle
) {
1961 notification_thread_handle_destroy(the_notification_thread_handle
);
1963 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1964 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1965 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1967 if (the_health_sessiond
) {
1968 health_app_destroy(the_health_sessiond
);
1970 exit_create_run_as_worker_cleanup
:
1972 sessiond_cleanup_lock_file();
1973 sessiond_cleanup_options();
1975 exit_set_signal_handler
: