2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
22 #include <sys/mount.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <sys/types.h>
28 #include <urcu/uatomic.h>
32 #include <common/common.hpp>
33 #include <common/compat/socket.hpp>
34 #include <common/compat/getenv.hpp>
35 #include <common/defaults.hpp>
36 #include <common/kernel-consumer/kernel-consumer.hpp>
37 #include <common/futex.hpp>
38 #include <common/relayd/relayd.hpp>
39 #include <common/utils.hpp>
40 #include <common/path.hpp>
41 #include <common/daemonize.hpp>
42 #include <common/config/session-config.hpp>
43 #include <common/ini-config/ini-config.hpp>
44 #include <common/dynamic-buffer.hpp>
45 #include <lttng/event-internal.hpp>
46 #include "lttng-sessiond.hpp"
47 #include "buffer-registry.hpp"
48 #include "channel.hpp"
50 #include "consumer.hpp"
51 #include "context.hpp"
53 #include "event-notifier-error-accounting.hpp"
55 #include "kernel-consumer.hpp"
56 #include "lttng-ust-ctl.hpp"
57 #include "ust-consumer.hpp"
59 #include "fd-limit.hpp"
60 #include "health-sessiond.hpp"
61 #include "testpoint.hpp"
62 #include "notify-apps.hpp"
63 #include "agent-thread.hpp"
65 #include "notification-thread.hpp"
66 #include "notification-thread-commands.hpp"
67 #include "rotation-thread.hpp"
69 #include "sessiond-config.hpp"
73 #include "dispatch.hpp"
74 #include "register.hpp"
75 #include "manage-apps.hpp"
76 #include "manage-kernel.hpp"
77 #include "modprobe.hpp"
78 #include "ust-sigbus.hpp"
80 static const char *help_msg
=
81 #ifdef LTTNG_EMBED_HELP
82 #include <lttng-sessiond.8.h>
88 #define EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
89 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR \
90 "event-notifier-error-buffer-size"
91 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR \
92 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-kernel"
93 #define EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR \
94 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_BASE_OPTION_STR "-userspace"
98 static int lockfile_fd
= -1;
99 static int opt_print_version
;
101 /* Set to 1 when a SIGUSR1 signal is received. */
102 static int recv_child_signal
;
104 /* Command line options */
105 static const struct option long_options
[] = {
106 { "client-sock", required_argument
, 0, 'c' },
107 { "apps-sock", required_argument
, 0, 'a' },
108 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
109 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
110 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
111 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
112 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
113 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
114 { "consumerd32-path", required_argument
, 0, '\0' },
115 { "consumerd32-libdir", required_argument
, 0, '\0' },
116 { "consumerd64-path", required_argument
, 0, '\0' },
117 { "consumerd64-libdir", required_argument
, 0, '\0' },
118 { "daemonize", no_argument
, 0, 'd' },
119 { "background", no_argument
, 0, 'b' },
120 { "sig-parent", no_argument
, 0, 'S' },
121 { "help", no_argument
, 0, 'h' },
122 { "group", required_argument
, 0, 'g' },
123 { "version", no_argument
, 0, 'V' },
124 { "quiet", no_argument
, 0, 'q' },
125 { "verbose", no_argument
, 0, 'v' },
126 { "verbose-consumer", no_argument
, 0, '\0' },
127 { "no-kernel", no_argument
, 0, '\0' },
128 { "pidfile", required_argument
, 0, 'p' },
129 { "agent-tcp-port", required_argument
, 0, '\0' },
130 { "config", required_argument
, 0, 'f' },
131 { "load", required_argument
, 0, 'l' },
132 { "kmod-probes", required_argument
, 0, '\0' },
133 { "extra-kmod-probes", required_argument
, 0, '\0' },
134 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, required_argument
, 0, '\0' },
135 { EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, required_argument
, 0, '\0' },
139 /* Command line options to ignore from configuration file */
140 static const char *config_ignore_options
[] = { "help", "version", "config" };
143 * This pipe is used to inform the thread managing application communication
144 * that a command is queued and ready to be processed.
146 static int apps_cmd_pipe
[2] = { -1, -1 };
147 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
150 * UST registration command queue. This queue is tied with a futex and uses a N
151 * wakers / 1 waiter implemented and detailed in futex.c/.h
153 * The thread_registration_apps and thread_dispatch_ust_registration uses this
154 * queue along with the wait/wake scheme. The thread_manage_apps receives down
155 * the line new application socket and monitors it for any I/O error or clean
156 * close that triggers an unregistration of the application.
158 static struct ust_cmd_queue ust_cmd_queue
;
161 * Section name to look for in the daemon configuration file.
163 static const char * const config_section_name
= "sessiond";
165 /* Am I root or not. Set to 1 if the daemon is running as root */
169 * Notify the main thread to initiate the teardown of the worker threads by
170 * writing to the main quit pipe.
172 static void notify_main_quit_pipe(void)
176 /* Stopping all threads */
177 DBG("Notify the main thread to terminate all worker threads");
178 ret
= sessiond_notify_main_quit_pipe();
180 ERR("write error on main quit pipe");
185 * Close every consumer sockets.
187 static void close_consumer_sockets(void)
191 if (the_kconsumer_data
.err_sock
>= 0) {
192 ret
= close(the_kconsumer_data
.err_sock
);
194 PERROR("kernel consumer err_sock close");
197 if (the_ustconsumer32_data
.err_sock
>= 0) {
198 ret
= close(the_ustconsumer32_data
.err_sock
);
200 PERROR("UST consumerd32 err_sock close");
203 if (the_ustconsumer64_data
.err_sock
>= 0) {
204 ret
= close(the_ustconsumer64_data
.err_sock
);
206 PERROR("UST consumerd64 err_sock close");
209 if (the_kconsumer_data
.cmd_sock
>= 0) {
210 ret
= close(the_kconsumer_data
.cmd_sock
);
212 PERROR("kernel consumer cmd_sock close");
215 if (the_ustconsumer32_data
.cmd_sock
>= 0) {
216 ret
= close(the_ustconsumer32_data
.cmd_sock
);
218 PERROR("UST consumerd32 cmd_sock close");
221 if (the_ustconsumer64_data
.cmd_sock
>= 0) {
222 ret
= close(the_ustconsumer64_data
.cmd_sock
);
224 PERROR("UST consumerd64 cmd_sock close");
227 if (the_kconsumer_data
.channel_monitor_pipe
>= 0) {
228 ret
= close(the_kconsumer_data
.channel_monitor_pipe
);
230 PERROR("kernel consumer channel monitor pipe close");
233 if (the_ustconsumer32_data
.channel_monitor_pipe
>= 0) {
234 ret
= close(the_ustconsumer32_data
.channel_monitor_pipe
);
236 PERROR("UST consumerd32 channel monitor pipe close");
239 if (the_ustconsumer64_data
.channel_monitor_pipe
>= 0) {
240 ret
= close(the_ustconsumer64_data
.channel_monitor_pipe
);
242 PERROR("UST consumerd64 channel monitor pipe close");
248 * Wait on consumer process termination.
250 * Need to be called with the consumer data lock held or from a context
251 * ensuring no concurrent access to data (e.g: cleanup).
253 static void wait_consumer(struct consumer_data
*consumer_data
)
258 if (consumer_data
->pid
<= 0) {
262 DBG("Waiting for complete teardown of consumerd (PID: %d)",
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",
271 consumer_data
->pid
= 0;
275 * Cleanup the session daemon's data structures.
277 static void sessiond_cleanup(void)
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",
301 the_config
.rundir
.value
);
304 DBG("Removing %s", the_config
.pid_file_path
.value
);
305 (void) unlink(the_config
.pid_file_path
.value
);
307 DBG("Removing %s", the_config
.agent_port_file_path
.value
);
308 (void) unlink(the_config
.agent_port_file_path
.value
);
311 DBG("Removing %s", the_kconsumer_data
.err_unix_sock_path
);
312 (void) unlink(the_kconsumer_data
.err_unix_sock_path
);
314 DBG("Removing directory %s", the_config
.kconsumerd_path
.value
);
315 (void) rmdir(the_config
.kconsumerd_path
.value
);
317 /* ust consumerd 32 */
318 DBG("Removing %s", the_config
.consumerd32_err_unix_sock_path
.value
);
319 (void) unlink(the_config
.consumerd32_err_unix_sock_path
.value
);
321 DBG("Removing directory %s", the_config
.consumerd32_path
.value
);
322 (void) rmdir(the_config
.consumerd32_path
.value
);
324 /* ust consumerd 64 */
325 DBG("Removing %s", the_config
.consumerd64_err_unix_sock_path
.value
);
326 (void) unlink(the_config
.consumerd64_err_unix_sock_path
.value
);
328 DBG("Removing directory %s", the_config
.consumerd64_path
.value
);
329 (void) rmdir(the_config
.consumerd64_path
.value
);
331 pthread_mutex_destroy(&session_list
->lock
);
333 DBG("Cleaning up all per-event notifier domain agents");
334 agent_by_event_notifier_domain_ht_destroy();
336 DBG("Cleaning up all agent apps");
337 agent_app_ht_clean();
338 DBG("Closing all UST sockets");
339 ust_app_clean_list();
340 buffer_reg_destroy_registries();
342 close_consumer_sockets();
344 wait_consumer(&the_kconsumer_data
);
345 wait_consumer(&the_ustconsumer64_data
);
346 wait_consumer(&the_ustconsumer32_data
);
348 if (is_root
&& !the_config
.no_kernel
) {
349 cleanup_kernel_tracer();
353 * We do NOT rmdir rundir because there are other processes
354 * using it, for instance lttng-relayd, which can start in
355 * parallel with this teardown.
360 * Cleanup the daemon's option data structures.
362 static void sessiond_cleanup_options(void)
364 DBG("Cleaning up options");
366 sessiond_config_fini(&the_config
);
368 run_as_destroy_worker();
371 static int string_match(const char *str1
, const char *str2
)
373 return (str1
&& str2
) && !strcmp(str1
, str2
);
377 * Take an option from the getopt output and set it in the right variable to be
380 * Return 0 on success else a negative value.
382 static int set_option(int opt
, const char *arg
, const char *optname
)
386 if (string_match(optname
, "client-sock") || opt
== 'c') {
387 if (!arg
|| *arg
== '\0') {
391 if (lttng_is_setuid_setgid()) {
392 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
393 "-c, --client-sock");
395 config_string_set(&the_config
.client_unix_sock_path
,
397 if (!the_config
.client_unix_sock_path
.value
) {
402 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
403 if (!arg
|| *arg
== '\0') {
407 if (lttng_is_setuid_setgid()) {
408 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
411 config_string_set(&the_config
.apps_unix_sock_path
,
413 if (!the_config
.apps_unix_sock_path
.value
) {
418 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
419 the_config
.daemonize
= true;
420 } else if (string_match(optname
, "background") || opt
== 'b') {
421 the_config
.background
= true;
422 } else if (string_match(optname
, "group") || opt
== 'g') {
423 if (!arg
|| *arg
== '\0') {
427 if (lttng_is_setuid_setgid()) {
428 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
431 config_string_set(&the_config
.tracing_group_name
,
433 if (!the_config
.tracing_group_name
.value
) {
438 } else if (string_match(optname
, "help") || opt
== 'h') {
439 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
441 ERR("Cannot show --help for `lttng-sessiond`");
444 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
445 } else if (string_match(optname
, "version") || opt
== 'V') {
446 opt_print_version
= 1;
447 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
448 the_config
.sig_parent
= true;
449 } else if (string_match(optname
, "kconsumerd-err-sock")) {
450 if (!arg
|| *arg
== '\0') {
454 if (lttng_is_setuid_setgid()) {
455 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
456 "--kconsumerd-err-sock");
459 &the_config
.kconsumerd_err_unix_sock_path
,
461 if (!the_config
.kconsumerd_err_unix_sock_path
.value
) {
466 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
467 if (!arg
|| *arg
== '\0') {
471 if (lttng_is_setuid_setgid()) {
472 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
473 "--kconsumerd-cmd-sock");
476 &the_config
.kconsumerd_cmd_unix_sock_path
,
478 if (!the_config
.kconsumerd_cmd_unix_sock_path
.value
) {
483 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
484 if (!arg
|| *arg
== '\0') {
488 if (lttng_is_setuid_setgid()) {
489 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
490 "--ustconsumerd64-err-sock");
493 &the_config
.consumerd64_err_unix_sock_path
,
495 if (!the_config
.consumerd64_err_unix_sock_path
.value
) {
500 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
501 if (!arg
|| *arg
== '\0') {
505 if (lttng_is_setuid_setgid()) {
506 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
507 "--ustconsumerd64-cmd-sock");
510 &the_config
.consumerd64_cmd_unix_sock_path
,
512 if (!the_config
.consumerd64_cmd_unix_sock_path
.value
) {
517 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
518 if (!arg
|| *arg
== '\0') {
522 if (lttng_is_setuid_setgid()) {
523 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
524 "--ustconsumerd32-err-sock");
527 &the_config
.consumerd32_err_unix_sock_path
,
529 if (!the_config
.consumerd32_err_unix_sock_path
.value
) {
534 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
535 if (!arg
|| *arg
== '\0') {
539 if (lttng_is_setuid_setgid()) {
540 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
541 "--ustconsumerd32-cmd-sock");
544 &the_config
.consumerd32_cmd_unix_sock_path
,
546 if (!the_config
.consumerd32_cmd_unix_sock_path
.value
) {
551 } else if (string_match(optname
, "no-kernel")) {
552 the_config
.no_kernel
= true;
553 } else if (string_match(optname
, "quiet") || opt
== 'q') {
554 the_config
.quiet
= true;
555 } else if (string_match(optname
, "verbose") || opt
== 'v') {
556 /* Verbose level can increase using multiple -v */
558 /* Value obtained from config file */
559 the_config
.verbose
= config_parse_value(arg
);
561 /* -v used on command line */
562 the_config
.verbose
++;
564 /* Clamp value to [0, 3] */
565 the_config
.verbose
= the_config
.verbose
< 0 ?
567 (the_config
.verbose
<= 3 ? the_config
.verbose
:
569 } else if (string_match(optname
, "verbose-consumer")) {
571 the_config
.verbose_consumer
= config_parse_value(arg
);
573 the_config
.verbose_consumer
++;
575 } else if (string_match(optname
, "consumerd32-path")) {
576 if (!arg
|| *arg
== '\0') {
580 if (lttng_is_setuid_setgid()) {
581 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
582 "--consumerd32-path");
584 config_string_set(&the_config
.consumerd32_bin_path
,
586 if (!the_config
.consumerd32_bin_path
.value
) {
591 } else if (string_match(optname
, "consumerd32-libdir")) {
592 if (!arg
|| *arg
== '\0') {
596 if (lttng_is_setuid_setgid()) {
597 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
598 "--consumerd32-libdir");
600 config_string_set(&the_config
.consumerd32_lib_dir
,
602 if (!the_config
.consumerd32_lib_dir
.value
) {
607 } else if (string_match(optname
, "consumerd64-path")) {
608 if (!arg
|| *arg
== '\0') {
612 if (lttng_is_setuid_setgid()) {
613 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
614 "--consumerd64-path");
616 config_string_set(&the_config
.consumerd64_bin_path
,
618 if (!the_config
.consumerd64_bin_path
.value
) {
623 } else if (string_match(optname
, "consumerd64-libdir")) {
624 if (!arg
|| *arg
== '\0') {
628 if (lttng_is_setuid_setgid()) {
629 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
630 "--consumerd64-libdir");
632 config_string_set(&the_config
.consumerd64_lib_dir
,
634 if (!the_config
.consumerd64_lib_dir
.value
) {
639 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
640 if (!arg
|| *arg
== '\0') {
644 if (lttng_is_setuid_setgid()) {
645 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
649 &the_config
.pid_file_path
, strdup(arg
));
650 if (!the_config
.pid_file_path
.value
) {
655 } else if (string_match(optname
, "agent-tcp-port")) {
656 if (!arg
|| *arg
== '\0') {
660 if (lttng_is_setuid_setgid()) {
661 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
667 v
= strtoul(arg
, NULL
, 0);
668 if (errno
!= 0 || !isdigit(arg
[0])) {
669 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
672 if (v
== 0 || v
>= 65535) {
673 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
676 the_config
.agent_tcp_port
.begin
=
677 the_config
.agent_tcp_port
.end
= (int) v
;
678 DBG3("Agent TCP port set to non default: %i", (int) v
);
680 } else if (string_match(optname
, "load") || opt
== 'l') {
681 if (!arg
|| *arg
== '\0') {
685 if (lttng_is_setuid_setgid()) {
686 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
689 config_string_set(&the_config
.load_session_path
,
691 if (!the_config
.load_session_path
.value
) {
696 } else if (string_match(optname
, "kmod-probes")) {
697 if (!arg
|| *arg
== '\0') {
701 if (lttng_is_setuid_setgid()) {
702 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
705 config_string_set(&the_config
.kmod_probes_list
,
707 if (!the_config
.kmod_probes_list
.value
) {
712 } else if (string_match(optname
, "extra-kmod-probes")) {
713 if (!arg
|| *arg
== '\0') {
717 if (lttng_is_setuid_setgid()) {
718 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
719 "--extra-kmod-probes");
721 config_string_set(&the_config
.kmod_extra_probes_list
,
723 if (!the_config
.kmod_extra_probes_list
.value
) {
728 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
)) {
732 v
= strtoul(arg
, NULL
, 0);
733 if (errno
!= 0 || !isdigit(arg
[0])) {
734 ERR("Wrong value in --%s parameter: %s",
735 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
738 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
739 ERR("Value out of range for --%s parameter: %s",
740 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_KERNEL_OPTION_STR
, arg
);
743 the_config
.event_notifier_buffer_size_kernel
= (int) v
;
744 DBG3("Number of event notifier error buffer kernel size to non default: %i",
745 the_config
.event_notifier_buffer_size_kernel
);
747 } else if (string_match(optname
, EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
)) {
751 v
= strtoul(arg
, NULL
, 0);
752 if (errno
!= 0 || !isdigit(arg
[0])) {
753 ERR("Wrong value in --%s parameter: %s",
754 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
757 if (v
== 0 || v
>= EVENT_NOTIFIER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
758 ERR("Value out of range for --%s parameter: %s",
759 EVENT_NOTIFIER_ERROR_BUFFER_SIZE_USERSPACE_OPTION_STR
, arg
);
762 the_config
.event_notifier_buffer_size_userspace
= (int) v
;
763 DBG3("Number of event notifier error buffer userspace size to non default: %i",
764 the_config
.event_notifier_buffer_size_userspace
);
766 } else if (string_match(optname
, "config") || opt
== 'f') {
767 /* This is handled in set_options() thus silent skip. */
770 /* Unknown option or other error.
771 * Error is printed by getopt, just return */
776 if (ret
== -EINVAL
) {
777 const char *opt_name
= "unknown";
780 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
782 if (opt
== long_options
[i
].val
) {
783 opt_name
= long_options
[i
].name
;
788 WARN("Invalid argument provided for option \"%s\", using default value.",
796 * config_entry_handler_cb used to handle options read from a config file.
797 * See config_entry_handler_cb comment in common/config/session-config.h for the
798 * return value conventions.
800 static int config_entry_handler(const struct config_entry
*entry
,
801 void *unused
__attribute__((unused
)))
805 if (!entry
|| !entry
->name
|| !entry
->value
) {
810 /* Check if the option is to be ignored */
811 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
812 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
817 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
820 /* Ignore if not fully matched. */
821 if (strcmp(entry
->name
, long_options
[i
].name
)) {
826 * If the option takes no argument on the command line, we have to
827 * check if the value is "true". We support non-zero numeric values,
830 if (!long_options
[i
].has_arg
) {
831 ret
= config_parse_value(entry
->value
);
834 WARN("Invalid configuration value \"%s\" for option %s",
835 entry
->value
, entry
->name
);
837 /* False, skip boolean config option. */
842 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
846 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
852 static void print_version(void) {
853 fprintf(stdout
, "%s\n", VERSION
);
857 * daemon configuration loading and argument parsing
859 static int set_options(int argc
, char **argv
)
861 int ret
= 0, c
= 0, option_index
= 0;
862 int orig_optopt
= optopt
, orig_optind
= optind
;
864 char *config_path
= NULL
;
866 optstring
= utils_generate_optstring(long_options
,
867 sizeof(long_options
) / sizeof(struct option
));
873 /* Check for the --config option */
874 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
875 &option_index
)) != -1) {
879 } else if (c
!= 'f') {
880 /* if not equal to --config option. */
884 if (lttng_is_setuid_setgid()) {
885 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
889 config_path
= utils_expand_path(optarg
);
891 ERR("Failed to resolve path: %s", optarg
);
896 ret
= config_get_section_entries(config_path
, config_section_name
,
897 config_entry_handler
, NULL
);
900 ERR("Invalid configuration option at line %i", ret
);
906 /* Reset getopt's global state */
907 optopt
= orig_optopt
;
908 optind
= orig_optind
;
912 * getopt_long() will not set option_index if it encounters a
915 c
= getopt_long(argc
, argv
, optstring
, long_options
,
922 * Pass NULL as the long option name if popt left the index
925 ret
= set_option(c
, optarg
,
926 option_index
< 0 ? NULL
:
927 long_options
[option_index
].name
);
940 * Create lockfile using the rundir and return its fd.
942 static int create_lockfile(void)
944 return utils_create_lock_file(the_config
.lock_file_path
.value
);
948 * Check if the global socket is available, and if a daemon is answering at the
949 * other side. If yes, error is returned.
951 * Also attempts to create and hold the lock file.
953 static int check_existing_daemon(void)
957 /* Is there anybody out there ? */
958 if (lttng_session_daemon_alive()) {
963 lockfile_fd
= create_lockfile();
964 if (lockfile_fd
< 0) {
972 static void sessiond_cleanup_lock_file(void)
977 * Cleanup lock file by deleting it and finaly closing it which will
978 * release the file system lock.
980 if (lockfile_fd
>= 0) {
981 ret
= remove(the_config
.lock_file_path
.value
);
983 PERROR("remove lock file");
985 ret
= close(lockfile_fd
);
987 PERROR("close lock file");
993 * Set the tracing group gid onto the client socket.
995 * Race window between mkdir and chown is OK because we are going from more
996 * permissive (root.root) to less permissive (root.tracing).
998 static int set_permissions(char *rundir
)
1003 ret
= utils_get_group_id(
1004 the_config
.tracing_group_name
.value
, true, &gid
);
1006 /* Default to root group. */
1010 /* Set lttng run dir */
1011 ret
= chown(rundir
, 0, gid
);
1013 ERR("Unable to set group on %s", rundir
);
1018 * Ensure all applications and tracing group can search the run
1019 * dir. Allow everyone to read the directory, since it does not
1020 * buy us anything to hide its content.
1022 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1024 ERR("Unable to set permissions on %s", rundir
);
1028 /* lttng client socket path */
1029 ret
= chown(the_config
.client_unix_sock_path
.value
, 0, gid
);
1031 ERR("Unable to set group on %s",
1032 the_config
.client_unix_sock_path
.value
);
1036 /* kconsumer error socket path */
1037 ret
= chown(the_kconsumer_data
.err_unix_sock_path
, 0, 0);
1039 ERR("Unable to set group on %s",
1040 the_kconsumer_data
.err_unix_sock_path
);
1044 /* 64-bit ustconsumer error socket path */
1045 ret
= chown(the_ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1047 ERR("Unable to set group on %s",
1048 the_ustconsumer64_data
.err_unix_sock_path
);
1052 /* 32-bit ustconsumer compat32 error socket path */
1053 ret
= chown(the_ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1055 ERR("Unable to set group on %s",
1056 the_ustconsumer32_data
.err_unix_sock_path
);
1060 DBG("All permissions are set");
1066 * Create the lttng run directory needed for all global sockets and pipe.
1068 static int create_lttng_rundir(void)
1072 DBG3("Creating LTTng run directory: %s", the_config
.rundir
.value
);
1074 ret
= mkdir(the_config
.rundir
.value
, S_IRWXU
);
1076 if (errno
!= EEXIST
) {
1077 ERR("Unable to create %s", the_config
.rundir
.value
);
1089 * Setup sockets and directory needed by the consumerds' communication with the
1092 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1097 switch (consumer_data
->type
) {
1098 case LTTNG_CONSUMER_KERNEL
:
1099 path
= the_config
.kconsumerd_path
.value
;
1101 case LTTNG_CONSUMER64_UST
:
1102 path
= the_config
.consumerd64_path
.value
;
1104 case LTTNG_CONSUMER32_UST
:
1105 path
= the_config
.consumerd32_path
.value
;
1108 ERR("Consumer type unknown");
1114 DBG2("Creating consumer directory: %s", path
);
1116 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1117 if (ret
< 0 && errno
!= EEXIST
) {
1119 ERR("Failed to create %s", path
);
1125 ret
= utils_get_group_id(the_config
.tracing_group_name
.value
,
1128 /* Default to root group. */
1132 ret
= chown(path
, 0, gid
);
1134 ERR("Unable to set group on %s", path
);
1140 /* Create the consumerd error unix socket */
1141 consumer_data
->err_sock
=
1142 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1143 if (consumer_data
->err_sock
< 0) {
1144 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1150 * Set the CLOEXEC flag. Return code is useless because either way, the
1153 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1155 PERROR("utils_set_fd_cloexec");
1156 /* continue anyway */
1159 /* File permission MUST be 660 */
1160 ret
= chmod(consumer_data
->err_unix_sock_path
,
1161 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1163 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1173 * Signal handler for the daemon
1175 * Simply stop all worker threads, leaving main() return gracefully after
1176 * joining all threads and calling cleanup().
1178 static void sighandler(int sig
, siginfo_t
*siginfo
,
1179 void *arg
__attribute__((unused
)))
1183 DBG("SIGINT caught");
1184 notify_main_quit_pipe();
1187 DBG("SIGTERM caught");
1188 notify_main_quit_pipe();
1191 CMM_STORE_SHARED(recv_child_signal
, 1);
1196 const char msg
[] = "Received SIGBUS, aborting program.\n";
1198 lttng_ust_handle_sigbus(siginfo
->si_addr
);
1200 * If ustctl did not catch this signal (triggering a
1201 * siglongjmp), abort the program. Otherwise, the execution
1202 * will resume from the ust-ctl call which caused this error.
1204 * The return value is ignored since the program aborts anyhow.
1206 write_ret
= write(STDERR_FILENO
, msg
, sizeof(msg
));
1216 * Setup signal handler for :
1217 * SIGINT, SIGTERM, SIGPIPE
1219 static int set_signal_handler(void)
1222 struct sigaction sa
;
1225 if ((ret
= sigemptyset(&sigset
)) < 0) {
1226 PERROR("sigemptyset");
1230 sa
.sa_mask
= sigset
;
1231 sa
.sa_flags
= SA_SIGINFO
;
1233 sa
.sa_sigaction
= sighandler
;
1234 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1235 PERROR("sigaction");
1239 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1240 PERROR("sigaction");
1244 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1245 PERROR("sigaction");
1249 if ((ret
= sigaction(SIGBUS
, &sa
, NULL
)) < 0) {
1250 PERROR("sigaction");
1255 sa
.sa_handler
= SIG_IGN
;
1256 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1257 PERROR("sigaction");
1261 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE, SIGINT, and SIGBUS");
1267 * Set open files limit to unlimited. This daemon can open a large number of
1268 * file descriptors in order to consume multiple kernel traces.
1270 static void set_ulimit(void)
1275 /* The kernel does not allow an infinite limit for open files */
1276 lim
.rlim_cur
= 65535;
1277 lim
.rlim_max
= 65535;
1279 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1281 PERROR("failed to set open files limit");
1285 static int write_pidfile(void)
1287 return utils_create_pid_file(getpid(), the_config
.pid_file_path
.value
);
1290 static int set_clock_plugin_env(void)
1293 char *env_value
= NULL
;
1295 if (!the_config
.lttng_ust_clock_plugin
.value
) {
1299 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1300 the_config
.lttng_ust_clock_plugin
.value
);
1306 ret
= putenv(env_value
);
1309 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1313 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1314 the_config
.lttng_ust_clock_plugin
.value
);
1319 static void destroy_all_sessions_and_wait(void)
1321 struct ltt_session
*session
, *tmp
;
1322 struct ltt_session_list
*session_list
;
1324 session_list
= session_get_list();
1325 DBG("Initiating destruction of all sessions");
1327 if (!session_list
) {
1331 session_lock_list();
1332 /* Initiate the destruction of all sessions. */
1333 cds_list_for_each_entry_safe(session
, tmp
,
1334 &session_list
->head
, list
) {
1335 if (!session_get(session
)) {
1339 session_lock(session
);
1340 if (session
->destroyed
) {
1341 goto unlock_session
;
1343 (void) cmd_stop_trace(session
);
1344 (void) cmd_destroy_session(
1345 session
, the_notification_thread_handle
, NULL
);
1347 session_unlock(session
);
1348 session_put(session
);
1350 session_unlock_list();
1352 /* Wait for the destruction of all sessions to complete. */
1353 DBG("Waiting for the destruction of all sessions to complete");
1354 session_list_wait_empty();
1355 DBG("Destruction of all sessions completed");
1358 static void unregister_all_triggers(void)
1360 enum lttng_error_code ret_code
;
1361 enum lttng_trigger_status trigger_status
;
1362 struct lttng_triggers
*triggers
= NULL
;
1363 unsigned int trigger_count
, i
;
1364 const struct lttng_credentials creds
= {
1365 .uid
= LTTNG_OPTIONAL_INIT_VALUE(0),
1366 .gid
= LTTNG_OPTIONAL_INIT_UNSET
,
1369 DBG("Unregistering all triggers");
1372 * List all triggers as "root" since we wish to unregister all triggers.
1374 ret_code
= notification_thread_command_list_triggers(
1375 the_notification_thread_handle
, creds
.uid
.value
,
1377 if (ret_code
!= LTTNG_OK
) {
1378 ERR("Failed to list triggers while unregistering all triggers");
1382 trigger_status
= lttng_triggers_get_count(triggers
, &trigger_count
);
1383 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1385 for (i
= 0; i
< trigger_count
; i
++) {
1386 uid_t trigger_owner
;
1387 const char *trigger_name
;
1388 const struct lttng_trigger
*trigger
=
1389 lttng_triggers_get_at_index(triggers
, i
);
1391 LTTNG_ASSERT(trigger
);
1393 trigger_status
= lttng_trigger_get_owner_uid(
1394 trigger
, &trigger_owner
);
1395 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
1397 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
1398 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
1399 trigger_name
: "(anonymous)";
1401 DBG("Unregistering trigger: trigger owner uid = %d, trigger name = '%s'",
1402 (int) trigger_owner
, trigger_name
);
1404 ret_code
= cmd_unregister_trigger(&creds
, trigger
,
1405 the_notification_thread_handle
);
1406 if (ret_code
!= LTTNG_OK
) {
1407 ERR("Failed to unregister trigger: trigger owner uid = %d, trigger name = '%s', error: '%s'",
1408 (int) trigger_owner
, trigger_name
,
1409 lttng_strerror(-ret_code
));
1410 /* Continue to unregister the remaining triggers. */
1414 lttng_triggers_destroy(triggers
);
1417 static int run_as_worker_post_fork_cleanup(void *data
)
1419 struct sessiond_config
*sessiond_config
= (struct sessiond_config
*) data
;
1421 sessiond_config_fini(sessiond_config
);
1425 static int launch_run_as_worker(const char *procname
)
1428 * Clean-up before forking the run-as worker. Any dynamically
1429 * allocated memory of which the worker is not aware will
1430 * be leaked as the process forks a run-as worker (and performs
1431 * no exec*()). The same would apply to any opened fd.
1433 return run_as_create_worker(
1434 procname
, run_as_worker_post_fork_cleanup
, &the_config
);
1437 static void sessiond_uuid_log(void)
1439 char uuid_str
[LTTNG_UUID_STR_LEN
];
1441 lttng_uuid_to_str(the_sessiond_uuid
, uuid_str
);
1442 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1448 int main(int argc
, char **argv
)
1450 int ret
= 0, retval
= 0;
1451 const char *env_app_timeout
;
1452 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1453 *ust64_channel_monitor_pipe
= NULL
,
1454 *kernel_channel_monitor_pipe
= NULL
;
1455 struct timer_thread_parameters timer_thread_parameters
;
1456 /* Rotation thread handle. */
1457 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1458 /* Queue of rotation jobs populated by the sessiond-timer. */
1459 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1460 struct lttng_thread
*client_thread
= NULL
;
1461 struct lttng_thread
*notification_thread
= NULL
;
1462 struct lttng_thread
*register_apps_thread
= NULL
;
1463 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
1465 logger_set_thread_name("Main", false);
1466 init_kernel_workarounds();
1468 rcu_register_thread();
1470 if (set_signal_handler()) {
1472 goto exit_set_signal_handler
;
1475 if (timer_signal_init()) {
1477 goto exit_set_signal_handler
;
1480 the_page_size
= sysconf(_SC_PAGE_SIZE
);
1481 if (the_page_size
< 0) {
1482 PERROR("sysconf _SC_PAGE_SIZE");
1483 the_page_size
= LONG_MAX
;
1484 WARN("Fallback page size to %ld", the_page_size
);
1487 ret
= sessiond_config_init(&the_config
);
1490 goto exit_set_signal_handler
;
1494 * Init config from environment variables.
1495 * Command line option override env configuration per-doc. Do env first.
1497 sessiond_config_apply_env_config(&the_config
);
1500 * Parse arguments and load the daemon configuration file.
1502 * We have an exit_options exit path to free memory reserved by
1506 if (set_options(argc
, argv
)) {
1512 * Resolve all paths received as arguments, configuration option, or
1513 * through environment variable as absolute paths. This is necessary
1514 * since daemonizing causes the sessiond's current working directory
1517 ret
= sessiond_config_resolve_paths(&the_config
);
1523 lttng_opt_verbose
= the_config
.verbose
;
1524 lttng_opt_quiet
= the_config
.quiet
;
1525 the_kconsumer_data
.err_unix_sock_path
=
1526 the_config
.kconsumerd_err_unix_sock_path
.value
;
1527 the_kconsumer_data
.cmd_unix_sock_path
=
1528 the_config
.kconsumerd_cmd_unix_sock_path
.value
;
1529 the_ustconsumer32_data
.err_unix_sock_path
=
1530 the_config
.consumerd32_err_unix_sock_path
.value
;
1531 the_ustconsumer32_data
.cmd_unix_sock_path
=
1532 the_config
.consumerd32_cmd_unix_sock_path
.value
;
1533 the_ustconsumer64_data
.err_unix_sock_path
=
1534 the_config
.consumerd64_err_unix_sock_path
.value
;
1535 the_ustconsumer64_data
.cmd_unix_sock_path
=
1536 the_config
.consumerd64_cmd_unix_sock_path
.value
;
1537 set_clock_plugin_env();
1539 sessiond_config_log(&the_config
);
1540 sessiond_uuid_log();
1542 if (opt_print_version
) {
1548 if (create_lttng_rundir()) {
1553 /* Abort launch if a session daemon is already running. */
1554 if (check_existing_daemon()) {
1555 ERR("A session daemon is already running.");
1561 if (the_config
.daemonize
|| the_config
.background
) {
1564 ret
= lttng_daemonize(&the_child_ppid
, &recv_child_signal
,
1565 !the_config
.background
);
1572 * We are in the child. Make sure all other file descriptors are
1573 * closed, in case we are called with more opened file
1574 * descriptors than the standard ones and the lock file.
1576 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1577 if (i
== lockfile_fd
) {
1584 if (launch_run_as_worker(argv
[0]) < 0) {
1585 goto exit_create_run_as_worker_cleanup
;
1589 * Starting from here, we can create threads. This needs to be after
1590 * lttng_daemonize due to RCU.
1594 * Initialize the health check subsystem. This call should set the
1595 * appropriate time values.
1597 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1598 if (!the_health_sessiond
) {
1599 PERROR("health_app_create error");
1604 /* Create main quit pipe */
1605 if (sessiond_init_main_quit_pipe()) {
1610 /* Check if daemon is UID = 0 */
1611 is_root
= !getuid();
1613 /* Create global run dir with root access */
1615 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1616 if (!kernel_channel_monitor_pipe
) {
1617 ERR("Failed to create kernel consumer channel monitor pipe");
1621 the_kconsumer_data
.channel_monitor_pipe
=
1622 lttng_pipe_release_writefd(
1623 kernel_channel_monitor_pipe
);
1624 if (the_kconsumer_data
.channel_monitor_pipe
< 0) {
1630 /* Set consumer initial state */
1631 the_kernel_consumerd_state
= CONSUMER_STOPPED
;
1632 the_ust_consumerd_state
= CONSUMER_STOPPED
;
1634 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1635 if (!ust32_channel_monitor_pipe
) {
1636 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1640 the_ustconsumer32_data
.channel_monitor_pipe
=
1641 lttng_pipe_release_writefd(ust32_channel_monitor_pipe
);
1642 if (the_ustconsumer32_data
.channel_monitor_pipe
< 0) {
1648 * The rotation_thread_timer_queue structure is shared between the
1649 * sessiond timer thread and the rotation thread. The main thread keeps
1650 * its ownership and destroys it when both threads have been joined.
1652 rotation_timer_queue
= rotation_thread_timer_queue_create();
1653 if (!rotation_timer_queue
) {
1657 timer_thread_parameters
.rotation_thread_job_queue
=
1658 rotation_timer_queue
;
1660 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1661 if (!ust64_channel_monitor_pipe
) {
1662 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1666 the_ustconsumer64_data
.channel_monitor_pipe
=
1667 lttng_pipe_release_writefd(ust64_channel_monitor_pipe
);
1668 if (the_ustconsumer64_data
.channel_monitor_pipe
< 0) {
1674 * Init UST app hash table. Alloc hash table before this point since
1675 * cleanup() can get called after that point.
1677 if (ust_app_ht_alloc()) {
1678 ERR("Failed to allocate UST app hash table");
1683 event_notifier_error_accounting_status
= event_notifier_error_accounting_init(
1684 the_config
.event_notifier_buffer_size_kernel
,
1685 the_config
.event_notifier_buffer_size_userspace
);
1686 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1687 ERR("Failed to initialize event notifier error accounting system");
1693 * Initialize agent app hash table. We allocate the hash table here
1694 * since cleanup() can get called after this point.
1696 if (agent_app_ht_alloc()) {
1697 ERR("Failed to allocate Agent app hash table");
1702 if (agent_by_event_notifier_domain_ht_create()) {
1703 ERR("Failed to allocate per-event notifier domain agent hash table");
1708 * These actions must be executed as root. We do that *after* setting up
1709 * the sockets path because we MUST make the check for another daemon using
1710 * those paths *before* trying to set the kernel consumer sockets and init
1714 if (set_consumer_sockets(&the_kconsumer_data
)) {
1719 /* Setup kernel tracer */
1720 if (!the_config
.no_kernel
) {
1721 init_kernel_tracer();
1724 /* Set ulimit for open files */
1727 /* init lttng_fd tracking must be done after set_ulimit. */
1730 if (set_consumer_sockets(&the_ustconsumer64_data
)) {
1735 if (set_consumer_sockets(&the_ustconsumer32_data
)) {
1740 /* Get parent pid if -S, --sig-parent is specified. */
1741 if (the_config
.sig_parent
) {
1742 the_ppid
= getppid();
1745 /* Setup the kernel pipe for waking up the kernel thread */
1746 if (is_root
&& !the_config
.no_kernel
) {
1747 if (utils_create_pipe_cloexec(the_kernel_poll_pipe
)) {
1753 /* Setup the thread apps communication pipe. */
1754 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1759 /* Setup the thread apps notify communication pipe. */
1760 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1765 /* Initialize global buffer per UID and PID registry. */
1766 buffer_reg_init_uid_registry();
1767 buffer_reg_init_pid_registry();
1769 /* Init UST command queue. */
1770 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1774 /* Check for the application socket timeout env variable. */
1775 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1776 if (env_app_timeout
) {
1777 the_config
.app_socket_timeout
= atoi(env_app_timeout
);
1779 the_config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1782 ret
= write_pidfile();
1784 ERR("Error in write_pidfile");
1789 /* Initialize communication library */
1791 /* Initialize TCP timeout values */
1792 lttcomm_inet_init();
1794 /* Create health-check thread. */
1795 if (!launch_health_management_thread()) {
1800 /* notification_thread_data acquires the pipes' read side. */
1801 the_notification_thread_handle
= notification_thread_handle_create(
1802 ust32_channel_monitor_pipe
, ust64_channel_monitor_pipe
,
1803 kernel_channel_monitor_pipe
);
1804 if (!the_notification_thread_handle
) {
1806 ERR("Failed to create notification thread shared data");
1810 /* Create notification thread. */
1811 notification_thread
= launch_notification_thread(
1812 the_notification_thread_handle
);
1813 if (!notification_thread
) {
1818 /* Create timer thread. */
1819 if (!launch_timer_thread(&timer_thread_parameters
)) {
1824 /* rotation_thread_data acquires the pipes' read side. */
1825 rotation_thread_handle
= rotation_thread_handle_create(
1826 rotation_timer_queue
, the_notification_thread_handle
);
1827 if (!rotation_thread_handle
) {
1829 ERR("Failed to create rotation thread shared data");
1833 /* Create rotation thread. */
1834 if (!launch_rotation_thread(rotation_thread_handle
)) {
1839 /* Create thread to manage the client socket */
1840 client_thread
= launch_client_thread();
1841 if (!client_thread
) {
1846 /* Set credentials of the client socket and rundir */
1847 if (is_root
&& set_permissions(the_config
.rundir
.value
)) {
1852 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1853 apps_cmd_notify_pipe
[1])) {
1858 /* Create thread to manage application registration. */
1859 register_apps_thread
= launch_application_registration_thread(
1861 if (!register_apps_thread
) {
1866 /* Create thread to manage application socket */
1867 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1872 /* Create thread to manage application notify socket */
1873 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1878 /* Create agent management thread. */
1879 if (!launch_agent_management_thread()) {
1884 /* Don't start this thread if kernel tracing is not requested nor root */
1885 if (is_root
&& !the_config
.no_kernel
) {
1886 /* Create kernel thread to manage kernel event */
1887 if (!launch_kernel_management_thread(the_kernel_poll_pipe
[0])) {
1892 if (kernel_get_notification_fd() >= 0) {
1893 ret
= notification_thread_command_add_tracer_event_source(
1894 the_notification_thread_handle
,
1895 kernel_get_notification_fd(),
1896 LTTNG_DOMAIN_KERNEL
);
1897 if (ret
!= LTTNG_OK
) {
1898 ERR("Failed to add kernel trigger event source to notification thread");
1905 /* Load sessions. */
1906 ret
= config_load_session(
1907 the_config
.load_session_path
.value
, NULL
, 1, 1, NULL
);
1909 ERR("Session load failed: %s", error_get_str(ret
));
1914 /* Initialization completed. */
1915 sessiond_signal_parents();
1918 * This is where we start awaiting program completion (e.g. through
1919 * signal that asks threads to teardown).
1922 /* Initiate teardown once activity occurs on the main quit pipe. */
1923 sessiond_wait_for_main_quit_pipe(-1);
1927 DBG("Terminating all threads");
1930 * Ensure that the client thread is no longer accepting new commands,
1931 * which could cause new sessions to be created.
1933 if (client_thread
) {
1934 lttng_thread_shutdown(client_thread
);
1935 lttng_thread_put(client_thread
);
1938 destroy_all_sessions_and_wait();
1941 * At this point no new trigger can be registered (no sessions are
1942 * running/rotating) and clients can't connect to the session daemon
1943 * anymore. Unregister all triggers.
1945 unregister_all_triggers();
1947 if (register_apps_thread
) {
1948 lttng_thread_shutdown(register_apps_thread
);
1949 lttng_thread_put(register_apps_thread
);
1951 lttng_thread_list_shutdown_orphans();
1954 * Wait for all pending call_rcu work to complete before tearing
1955 * down data structures. call_rcu worker may be trying to
1956 * perform lookups in those structures.
1960 rcu_thread_online();
1964 * Wait for all pending call_rcu work to complete before shutting down
1965 * the notification thread. This call_rcu work includes shutting down
1966 * UST apps and event notifier pipes.
1970 if (notification_thread
) {
1971 lttng_thread_shutdown(notification_thread
);
1972 lttng_thread_put(notification_thread
);
1976 * Error accounting teardown has to be done after the teardown of all
1977 * event notifier pipes to ensure that no tracer may try to use the
1978 * error accounting facilities.
1980 event_notifier_error_accounting_fini();
1983 * Unloading the kernel modules needs to be done after all kernel
1984 * ressources have been released. In our case, this includes the
1985 * notification fd, the event notifier group fd, error accounting fd,
1986 * all event and event notifier fds, etc.
1988 * In short, at this point, we need to have called close() on all fds
1989 * received from the kernel tracer.
1991 if (is_root
&& !the_config
.no_kernel
) {
1992 DBG("Unloading kernel modules");
1993 modprobe_remove_lttng_all();
1996 rcu_thread_offline();
1997 rcu_unregister_thread();
1999 if (rotation_thread_handle
) {
2000 rotation_thread_handle_destroy(rotation_thread_handle
);
2004 * After the rotation and timer thread have quit, we can safely destroy
2005 * the rotation_timer_queue.
2007 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
2009 * The teardown of the notification system is performed after the
2010 * session daemon's teardown in order to allow it to be notified
2011 * of the active session and channels at the moment of the teardown.
2013 if (the_notification_thread_handle
) {
2014 notification_thread_handle_destroy(
2015 the_notification_thread_handle
);
2017 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
2018 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
2019 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
2021 if (the_health_sessiond
) {
2022 health_app_destroy(the_health_sessiond
);
2024 exit_create_run_as_worker_cleanup
:
2026 sessiond_cleanup_lock_file();
2027 sessiond_cleanup_options();
2029 exit_set_signal_handler
: