2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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.h>
33 #include <common/compat/socket.h>
34 #include <common/compat/getenv.h>
35 #include <common/defaults.h>
36 #include <common/kernel-consumer/kernel-consumer.h>
37 #include <common/futex.h>
38 #include <common/relayd/relayd.h>
39 #include <common/utils.h>
40 #include <common/daemonize.h>
41 #include <common/config/session-config.h>
42 #include <common/dynamic-buffer.h>
43 #include <lttng/event-internal.h>
45 #include "lttng-sessiond.h"
46 #include "buffer-registry.h"
53 #include "kernel-consumer.h"
54 #include "lttng-ust-ctl.h"
55 #include "ust-consumer.h"
58 #include "health-sessiond.h"
59 #include "testpoint.h"
60 #include "notify-apps.h"
61 #include "agent-thread.h"
63 #include "notification-thread.h"
64 #include "notification-thread-commands.h"
65 #include "rotation-thread.h"
67 #include "ht-cleanup.h"
68 #include "sessiond-config.h"
74 #include "manage-apps.h"
75 #include "manage-kernel.h"
76 #include "trigger-error-accounting.h"
78 static const char *help_msg
=
79 #ifdef LTTNG_EMBED_HELP
80 #include <lttng-sessiond.8.h>
86 #define TRIGGER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX 65535
89 static int lockfile_fd
= -1;
90 static int opt_print_version
;
92 /* Set to 1 when a SIGUSR1 signal is received. */
93 static int recv_child_signal
;
95 /* Command line options */
96 static const struct option long_options
[] = {
97 { "client-sock", required_argument
, 0, 'c' },
98 { "apps-sock", required_argument
, 0, 'a' },
99 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
100 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
101 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
102 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
103 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
104 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
105 { "consumerd32-path", required_argument
, 0, '\0' },
106 { "consumerd32-libdir", required_argument
, 0, '\0' },
107 { "consumerd64-path", required_argument
, 0, '\0' },
108 { "consumerd64-libdir", required_argument
, 0, '\0' },
109 { "daemonize", no_argument
, 0, 'd' },
110 { "background", no_argument
, 0, 'b' },
111 { "sig-parent", no_argument
, 0, 'S' },
112 { "help", no_argument
, 0, 'h' },
113 { "group", required_argument
, 0, 'g' },
114 { "version", no_argument
, 0, 'V' },
115 { "quiet", no_argument
, 0, 'q' },
116 { "verbose", no_argument
, 0, 'v' },
117 { "verbose-consumer", no_argument
, 0, '\0' },
118 { "no-kernel", no_argument
, 0, '\0' },
119 { "pidfile", required_argument
, 0, 'p' },
120 { "agent-tcp-port", required_argument
, 0, '\0' },
121 { "config", required_argument
, 0, 'f' },
122 { "load", required_argument
, 0, 'l' },
123 { "kmod-probes", required_argument
, 0, '\0' },
124 { "extra-kmod-probes", required_argument
, 0, '\0' },
125 { "trigger-error-number-of-bucket", required_argument
, 0, '\0' },
129 /* Command line options to ignore from configuration file */
130 static const char *config_ignore_options
[] = { "help", "version", "config" };
133 * This pipe is used to inform the thread managing application communication
134 * that a command is queued and ready to be processed.
136 static int apps_cmd_pipe
[2] = { -1, -1 };
137 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
140 * UST registration command queue. This queue is tied with a futex and uses a N
141 * wakers / 1 waiter implemented and detailed in futex.c/.h
143 * The thread_registration_apps and thread_dispatch_ust_registration uses this
144 * queue along with the wait/wake scheme. The thread_manage_apps receives down
145 * the line new application socket and monitors it for any I/O error or clean
146 * close that triggers an unregistration of the application.
148 static struct ust_cmd_queue ust_cmd_queue
;
151 * Section name to look for in the daemon configuration file.
153 static const char * const config_section_name
= "sessiond";
155 /* Am I root or not. Set to 1 if the daemon is running as root */
159 * Stop all threads by closing the thread quit pipe.
161 static void stop_threads(void)
165 /* Stopping all threads */
166 DBG("Terminating all threads");
167 ret
= sessiond_notify_quit_pipe();
169 ERR("write error on thread quit pipe");
174 * Close every consumer sockets.
176 static void close_consumer_sockets(void)
180 if (kconsumer_data
.err_sock
>= 0) {
181 ret
= close(kconsumer_data
.err_sock
);
183 PERROR("kernel consumer err_sock close");
186 if (ustconsumer32_data
.err_sock
>= 0) {
187 ret
= close(ustconsumer32_data
.err_sock
);
189 PERROR("UST consumerd32 err_sock close");
192 if (ustconsumer64_data
.err_sock
>= 0) {
193 ret
= close(ustconsumer64_data
.err_sock
);
195 PERROR("UST consumerd64 err_sock close");
198 if (kconsumer_data
.cmd_sock
>= 0) {
199 ret
= close(kconsumer_data
.cmd_sock
);
201 PERROR("kernel consumer cmd_sock close");
204 if (ustconsumer32_data
.cmd_sock
>= 0) {
205 ret
= close(ustconsumer32_data
.cmd_sock
);
207 PERROR("UST consumerd32 cmd_sock close");
210 if (ustconsumer64_data
.cmd_sock
>= 0) {
211 ret
= close(ustconsumer64_data
.cmd_sock
);
213 PERROR("UST consumerd64 cmd_sock close");
216 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
217 ret
= close(kconsumer_data
.channel_monitor_pipe
);
219 PERROR("kernel consumer channel monitor pipe close");
222 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
223 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
225 PERROR("UST consumerd32 channel monitor pipe close");
228 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
229 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
231 PERROR("UST consumerd64 channel monitor pipe close");
237 * Wait on consumer process termination.
239 * Need to be called with the consumer data lock held or from a context
240 * ensuring no concurrent access to data (e.g: cleanup).
242 static void wait_consumer(struct consumer_data
*consumer_data
)
247 if (consumer_data
->pid
<= 0) {
251 DBG("Waiting for complete teardown of consumerd (PID: %d)",
253 ret
= waitpid(consumer_data
->pid
, &status
, 0);
255 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
256 } else if (!WIFEXITED(status
)) {
257 ERR("consumerd termination with error: %d",
260 consumer_data
->pid
= 0;
264 * Cleanup the session daemon's data structures.
266 static void sessiond_cleanup(void)
269 struct ltt_session_list
*session_list
= session_get_list();
271 DBG("Cleanup sessiond");
274 * Close the thread quit pipe. It has already done its job,
275 * since we are now called.
277 sessiond_close_quit_pipe();
278 utils_close_pipe(apps_cmd_pipe
);
279 utils_close_pipe(apps_cmd_notify_pipe
);
280 utils_close_pipe(kernel_poll_pipe
);
282 ret
= remove(config
.pid_file_path
.value
);
284 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
287 DBG("Removing sessiond and consumerd content of directory %s",
288 config
.rundir
.value
);
291 DBG("Removing %s", config
.pid_file_path
.value
);
292 (void) unlink(config
.pid_file_path
.value
);
294 DBG("Removing %s", config
.agent_port_file_path
.value
);
295 (void) unlink(config
.agent_port_file_path
.value
);
298 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
299 (void) unlink(kconsumer_data
.err_unix_sock_path
);
301 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
302 (void) rmdir(config
.kconsumerd_path
.value
);
304 /* ust consumerd 32 */
305 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
306 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
308 DBG("Removing directory %s", config
.consumerd32_path
.value
);
309 (void) rmdir(config
.consumerd32_path
.value
);
311 /* ust consumerd 64 */
312 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
313 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
315 DBG("Removing directory %s", config
.consumerd64_path
.value
);
316 (void) rmdir(config
.consumerd64_path
.value
);
318 pthread_mutex_destroy(&session_list
->lock
);
320 DBG("Cleaning up all trigger agents");
321 trigger_agent_ht_clean();
323 DBG("Cleaning up all agent apps");
324 agent_app_ht_clean();
325 DBG("Closing all UST sockets");
326 ust_app_clean_list();
327 buffer_reg_destroy_registries();
329 close_consumer_sockets();
331 wait_consumer(&kconsumer_data
);
332 wait_consumer(&ustconsumer64_data
);
333 wait_consumer(&ustconsumer32_data
);
335 if (is_root
&& !config
.no_kernel
) {
336 cleanup_kernel_tracer();
340 * We do NOT rmdir rundir because there are other processes
341 * using it, for instance lttng-relayd, which can start in
342 * parallel with this teardown.
347 * Cleanup the daemon's option data structures.
349 static void sessiond_cleanup_options(void)
351 DBG("Cleaning up options");
353 sessiond_config_fini(&config
);
355 run_as_destroy_worker();
358 static int string_match(const char *str1
, const char *str2
)
360 return (str1
&& str2
) && !strcmp(str1
, str2
);
364 * Take an option from the getopt output and set it in the right variable to be
367 * Return 0 on success else a negative value.
369 static int set_option(int opt
, const char *arg
, const char *optname
)
373 if (string_match(optname
, "client-sock") || opt
== 'c') {
374 if (!arg
|| *arg
== '\0') {
378 if (lttng_is_setuid_setgid()) {
379 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
380 "-c, --client-sock");
382 config_string_set(&config
.client_unix_sock_path
,
384 if (!config
.client_unix_sock_path
.value
) {
389 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
390 if (!arg
|| *arg
== '\0') {
394 if (lttng_is_setuid_setgid()) {
395 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
398 config_string_set(&config
.apps_unix_sock_path
,
400 if (!config
.apps_unix_sock_path
.value
) {
405 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
406 config
.daemonize
= true;
407 } else if (string_match(optname
, "background") || opt
== 'b') {
408 config
.background
= true;
409 } else if (string_match(optname
, "group") || opt
== 'g') {
410 if (!arg
|| *arg
== '\0') {
414 if (lttng_is_setuid_setgid()) {
415 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
418 config_string_set(&config
.tracing_group_name
,
420 if (!config
.tracing_group_name
.value
) {
425 } else if (string_match(optname
, "help") || opt
== 'h') {
426 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
428 ERR("Cannot show --help for `lttng-sessiond`");
431 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
432 } else if (string_match(optname
, "version") || opt
== 'V') {
433 opt_print_version
= 1;
434 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
435 config
.sig_parent
= true;
436 } else if (string_match(optname
, "kconsumerd-err-sock")) {
437 if (!arg
|| *arg
== '\0') {
441 if (lttng_is_setuid_setgid()) {
442 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
443 "--kconsumerd-err-sock");
445 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
447 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
452 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
453 if (!arg
|| *arg
== '\0') {
457 if (lttng_is_setuid_setgid()) {
458 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
459 "--kconsumerd-cmd-sock");
461 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
463 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
468 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
469 if (!arg
|| *arg
== '\0') {
473 if (lttng_is_setuid_setgid()) {
474 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
475 "--ustconsumerd64-err-sock");
477 config_string_set(&config
.consumerd64_err_unix_sock_path
,
479 if (!config
.consumerd64_err_unix_sock_path
.value
) {
484 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
485 if (!arg
|| *arg
== '\0') {
489 if (lttng_is_setuid_setgid()) {
490 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
491 "--ustconsumerd64-cmd-sock");
493 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
495 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
500 } else if (string_match(optname
, "ustconsumerd32-err-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 "--ustconsumerd32-err-sock");
509 config_string_set(&config
.consumerd32_err_unix_sock_path
,
511 if (!config
.consumerd32_err_unix_sock_path
.value
) {
516 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
517 if (!arg
|| *arg
== '\0') {
521 if (lttng_is_setuid_setgid()) {
522 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
523 "--ustconsumerd32-cmd-sock");
525 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
527 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
532 } else if (string_match(optname
, "no-kernel")) {
533 config
.no_kernel
= true;
534 } else if (string_match(optname
, "quiet") || opt
== 'q') {
536 } else if (string_match(optname
, "verbose") || opt
== 'v') {
537 /* Verbose level can increase using multiple -v */
539 /* Value obtained from config file */
540 config
.verbose
= config_parse_value(arg
);
542 /* -v used on command line */
545 /* Clamp value to [0, 3] */
546 config
.verbose
= config
.verbose
< 0 ? 0 :
547 (config
.verbose
<= 3 ? config
.verbose
: 3);
548 } else if (string_match(optname
, "verbose-consumer")) {
550 config
.verbose_consumer
= config_parse_value(arg
);
552 config
.verbose_consumer
++;
554 } else if (string_match(optname
, "consumerd32-path")) {
555 if (!arg
|| *arg
== '\0') {
559 if (lttng_is_setuid_setgid()) {
560 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
561 "--consumerd32-path");
563 config_string_set(&config
.consumerd32_bin_path
,
565 if (!config
.consumerd32_bin_path
.value
) {
570 } else if (string_match(optname
, "consumerd32-libdir")) {
571 if (!arg
|| *arg
== '\0') {
575 if (lttng_is_setuid_setgid()) {
576 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
577 "--consumerd32-libdir");
579 config_string_set(&config
.consumerd32_lib_dir
,
581 if (!config
.consumerd32_lib_dir
.value
) {
586 } else if (string_match(optname
, "consumerd64-path")) {
587 if (!arg
|| *arg
== '\0') {
591 if (lttng_is_setuid_setgid()) {
592 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
593 "--consumerd64-path");
595 config_string_set(&config
.consumerd64_bin_path
,
597 if (!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(&config
.consumerd64_lib_dir
,
613 if (!config
.consumerd64_lib_dir
.value
) {
618 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
619 if (!arg
|| *arg
== '\0') {
623 if (lttng_is_setuid_setgid()) {
624 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
627 config_string_set(&config
.pid_file_path
, strdup(arg
));
628 if (!config
.pid_file_path
.value
) {
633 } else if (string_match(optname
, "agent-tcp-port")) {
634 if (!arg
|| *arg
== '\0') {
638 if (lttng_is_setuid_setgid()) {
639 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
645 v
= strtoul(arg
, NULL
, 0);
646 if (errno
!= 0 || !isdigit(arg
[0])) {
647 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
650 if (v
== 0 || v
>= 65535) {
651 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
654 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
655 DBG3("Agent TCP port set to non default: %i", (int) v
);
657 } else if (string_match(optname
, "load") || opt
== 'l') {
658 if (!arg
|| *arg
== '\0') {
662 if (lttng_is_setuid_setgid()) {
663 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
666 config_string_set(&config
.load_session_path
, strdup(arg
));
667 if (!config
.load_session_path
.value
) {
672 } else if (string_match(optname
, "kmod-probes")) {
673 if (!arg
|| *arg
== '\0') {
677 if (lttng_is_setuid_setgid()) {
678 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
681 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
682 if (!config
.kmod_probes_list
.value
) {
687 } else if (string_match(optname
, "extra-kmod-probes")) {
688 if (!arg
|| *arg
== '\0') {
692 if (lttng_is_setuid_setgid()) {
693 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
694 "--extra-kmod-probes");
696 config_string_set(&config
.kmod_extra_probes_list
,
698 if (!config
.kmod_extra_probes_list
.value
) {
703 } else if (string_match(optname
, "trigger-error-number-of-bucket")) {
707 v
= strtoul(arg
, NULL
, 0);
708 if (errno
!= 0 || !isdigit(arg
[0])) {
709 ERR("Wrong value in --trigger-error-number-of-bucket parameter: %s", arg
);
712 if (v
== 0 || v
>= TRIGGER_ERROR_COUNTER_NUMBER_OF_BUCKET_MAX
) {
713 ERR("Value out of range for --trigger-error-number-of-bucket parameter: %s", arg
);
716 config
.trigger_error_counter_bucket
= (int) v
;
717 DBG3("Number of error counter set to non default: %i",
718 config
.trigger_error_counter_bucket
);
720 } else if (string_match(optname
, "config") || opt
== 'f') {
721 /* This is handled in set_options() thus silent skip. */
724 /* Unknown option or other error.
725 * Error is printed by getopt, just return */
730 if (ret
== -EINVAL
) {
731 const char *opt_name
= "unknown";
734 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
736 if (opt
== long_options
[i
].val
) {
737 opt_name
= long_options
[i
].name
;
742 WARN("Invalid argument provided for option \"%s\", using default value.",
750 * config_entry_handler_cb used to handle options read from a config file.
751 * See config_entry_handler_cb comment in common/config/session-config.h for the
752 * return value conventions.
754 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
758 if (!entry
|| !entry
->name
|| !entry
->value
) {
763 /* Check if the option is to be ignored */
764 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
765 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
770 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
773 /* Ignore if not fully matched. */
774 if (strcmp(entry
->name
, long_options
[i
].name
)) {
779 * If the option takes no argument on the command line, we have to
780 * check if the value is "true". We support non-zero numeric values,
783 if (!long_options
[i
].has_arg
) {
784 ret
= config_parse_value(entry
->value
);
787 WARN("Invalid configuration value \"%s\" for option %s",
788 entry
->value
, entry
->name
);
790 /* False, skip boolean config option. */
795 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
799 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
805 static void print_version(void) {
806 fprintf(stdout
, "%s\n", VERSION
);
810 * daemon configuration loading and argument parsing
812 static int set_options(int argc
, char **argv
)
814 int ret
= 0, c
= 0, option_index
= 0;
815 int orig_optopt
= optopt
, orig_optind
= optind
;
817 const char *config_path
= NULL
;
819 optstring
= utils_generate_optstring(long_options
,
820 sizeof(long_options
) / sizeof(struct option
));
826 /* Check for the --config option */
827 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
828 &option_index
)) != -1) {
832 } else if (c
!= 'f') {
833 /* if not equal to --config option. */
837 if (lttng_is_setuid_setgid()) {
838 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
841 config_path
= utils_expand_path(optarg
);
843 ERR("Failed to resolve path: %s", optarg
);
848 ret
= config_get_section_entries(config_path
, config_section_name
,
849 config_entry_handler
, NULL
);
852 ERR("Invalid configuration option at line %i", ret
);
858 /* Reset getopt's global state */
859 optopt
= orig_optopt
;
860 optind
= orig_optind
;
864 * getopt_long() will not set option_index if it encounters a
867 c
= getopt_long(argc
, argv
, optstring
, long_options
,
874 * Pass NULL as the long option name if popt left the index
877 ret
= set_option(c
, optarg
,
878 option_index
< 0 ? NULL
:
879 long_options
[option_index
].name
);
891 * Create lockfile using the rundir and return its fd.
893 static int create_lockfile(void)
895 return utils_create_lock_file(config
.lock_file_path
.value
);
899 * Check if the global socket is available, and if a daemon is answering at the
900 * other side. If yes, error is returned.
902 * Also attempts to create and hold the lock file.
904 static int check_existing_daemon(void)
908 /* Is there anybody out there ? */
909 if (lttng_session_daemon_alive()) {
914 lockfile_fd
= create_lockfile();
915 if (lockfile_fd
< 0) {
923 static void sessiond_cleanup_lock_file(void)
928 * Cleanup lock file by deleting it and finaly closing it which will
929 * release the file system lock.
931 if (lockfile_fd
>= 0) {
932 ret
= remove(config
.lock_file_path
.value
);
934 PERROR("remove lock file");
936 ret
= close(lockfile_fd
);
938 PERROR("close lock file");
944 * Set the tracing group gid onto the client socket.
946 * Race window between mkdir and chown is OK because we are going from more
947 * permissive (root.root) to less permissive (root.tracing).
949 static int set_permissions(char *rundir
)
954 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true, &gid
);
956 /* Default to root group. */
960 /* Set lttng run dir */
961 ret
= chown(rundir
, 0, gid
);
963 ERR("Unable to set group on %s", rundir
);
968 * Ensure all applications and tracing group can search the run
969 * dir. Allow everyone to read the directory, since it does not
970 * buy us anything to hide its content.
972 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
974 ERR("Unable to set permissions on %s", rundir
);
978 /* lttng client socket path */
979 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
981 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
985 /* kconsumer error socket path */
986 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
988 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
992 /* 64-bit ustconsumer error socket path */
993 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
995 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
999 /* 32-bit ustconsumer compat32 error socket path */
1000 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1002 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
1006 DBG("All permissions are set");
1012 * Create the lttng run directory needed for all global sockets and pipe.
1014 static int create_lttng_rundir(void)
1018 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
1020 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
1022 if (errno
!= EEXIST
) {
1023 ERR("Unable to create %s", config
.rundir
.value
);
1035 * Setup sockets and directory needed by the consumerds' communication with the
1038 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1043 switch (consumer_data
->type
) {
1044 case LTTNG_CONSUMER_KERNEL
:
1045 path
= config
.kconsumerd_path
.value
;
1047 case LTTNG_CONSUMER64_UST
:
1048 path
= config
.consumerd64_path
.value
;
1050 case LTTNG_CONSUMER32_UST
:
1051 path
= config
.consumerd32_path
.value
;
1054 ERR("Consumer type unknown");
1060 DBG2("Creating consumer directory: %s", path
);
1062 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1063 if (ret
< 0 && errno
!= EEXIST
) {
1065 ERR("Failed to create %s", path
);
1071 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
1074 /* Default to root group. */
1078 ret
= chown(path
, 0, gid
);
1080 ERR("Unable to set group on %s", path
);
1086 /* Create the consumerd error unix socket */
1087 consumer_data
->err_sock
=
1088 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1089 if (consumer_data
->err_sock
< 0) {
1090 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1096 * Set the CLOEXEC flag. Return code is useless because either way, the
1099 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1101 PERROR("utils_set_fd_cloexec");
1102 /* continue anyway */
1105 /* File permission MUST be 660 */
1106 ret
= chmod(consumer_data
->err_unix_sock_path
,
1107 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1109 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1119 * Signal handler for the daemon
1121 * Simply stop all worker threads, leaving main() return gracefully after
1122 * joining all threads and calling cleanup().
1124 static void sighandler(int sig
)
1128 DBG("SIGINT caught");
1132 DBG("SIGTERM caught");
1136 CMM_STORE_SHARED(recv_child_signal
, 1);
1144 * Setup signal handler for :
1145 * SIGINT, SIGTERM, SIGPIPE
1147 static int set_signal_handler(void)
1150 struct sigaction sa
;
1153 if ((ret
= sigemptyset(&sigset
)) < 0) {
1154 PERROR("sigemptyset");
1158 sa
.sa_mask
= sigset
;
1161 sa
.sa_handler
= sighandler
;
1162 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1163 PERROR("sigaction");
1167 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1168 PERROR("sigaction");
1172 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1173 PERROR("sigaction");
1177 sa
.sa_handler
= SIG_IGN
;
1178 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1179 PERROR("sigaction");
1183 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1189 * Set open files limit to unlimited. This daemon can open a large number of
1190 * file descriptors in order to consume multiple kernel traces.
1192 static void set_ulimit(void)
1197 /* The kernel does not allow an infinite limit for open files */
1198 lim
.rlim_cur
= 65535;
1199 lim
.rlim_max
= 65535;
1201 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1203 PERROR("failed to set open files limit");
1207 static int write_pidfile(void)
1209 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1212 static int set_clock_plugin_env(void)
1215 char *env_value
= NULL
;
1217 if (!config
.lttng_ust_clock_plugin
.value
) {
1221 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1222 config
.lttng_ust_clock_plugin
.value
);
1228 ret
= putenv(env_value
);
1231 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1235 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1236 config
.lttng_ust_clock_plugin
.value
);
1241 static void destroy_all_sessions_and_wait(void)
1243 struct ltt_session
*session
, *tmp
;
1244 struct ltt_session_list
*session_list
;
1246 session_list
= session_get_list();
1247 DBG("Initiating destruction of all sessions");
1249 if (!session_list
) {
1253 session_lock_list();
1254 /* Initiate the destruction of all sessions. */
1255 cds_list_for_each_entry_safe(session
, tmp
,
1256 &session_list
->head
, list
) {
1257 if (!session_get(session
)) {
1261 session_lock(session
);
1262 if (session
->destroyed
) {
1263 goto unlock_session
;
1265 (void) cmd_stop_trace(session
);
1266 (void) cmd_destroy_session(session
, notification_thread_handle
,
1269 session_unlock(session
);
1270 session_put(session
);
1272 session_unlock_list();
1274 /* Wait for the destruction of all sessions to complete. */
1275 DBG("Waiting for the destruction of all sessions to complete");
1276 session_list_wait_empty();
1277 DBG("Destruction of all sessions completed");
1280 static int run_as_worker_post_fork_cleanup(void *data
)
1282 struct sessiond_config
*sessiond_config
= data
;
1284 sessiond_config_fini(sessiond_config
);
1288 static int launch_run_as_worker(const char *procname
)
1291 * Clean-up before forking the run-as worker. Any dynamically
1292 * allocated memory of which the worker is not aware will
1293 * be leaked as the process forks a run-as worker (and performs
1294 * no exec*()). The same would apply to any opened fd.
1296 return run_as_create_worker(procname
, run_as_worker_post_fork_cleanup
,
1300 static void sessiond_uuid_log(void)
1302 char uuid_str
[LTTNG_UUID_STR_LEN
];
1304 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
1305 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1311 int main(int argc
, char **argv
)
1313 int ret
= 0, retval
= 0;
1314 const char *env_app_timeout
;
1315 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1316 *ust64_channel_monitor_pipe
= NULL
,
1317 *kernel_channel_monitor_pipe
= NULL
;
1318 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1319 struct timer_thread_parameters timer_thread_parameters
;
1320 /* Rotation thread handle. */
1321 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1322 /* Queue of rotation jobs populated by the sessiond-timer. */
1323 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1324 struct lttng_thread
*client_thread
= NULL
;
1325 struct lttng_thread
*notification_thread
= NULL
;
1326 struct lttng_thread
*register_apps_thread
= NULL
;
1328 logger_set_thread_name("Main", false);
1329 init_kernel_workarounds();
1331 rcu_register_thread();
1333 if (set_signal_handler()) {
1335 goto exit_set_signal_handler
;
1338 if (timer_signal_init()) {
1340 goto exit_set_signal_handler
;
1343 page_size
= sysconf(_SC_PAGESIZE
);
1344 if (page_size
< 0) {
1345 PERROR("sysconf _SC_PAGESIZE");
1346 page_size
= LONG_MAX
;
1347 WARN("Fallback page size to %ld", page_size
);
1350 ret
= sessiond_config_init(&config
);
1353 goto exit_set_signal_handler
;
1357 * Init config from environment variables.
1358 * Command line option override env configuration per-doc. Do env first.
1360 sessiond_config_apply_env_config(&config
);
1363 * Parse arguments and load the daemon configuration file.
1365 * We have an exit_options exit path to free memory reserved by
1366 * set_options. This is needed because the rest of sessiond_cleanup()
1367 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1368 * depends on set_options.
1371 if (set_options(argc
, argv
)) {
1377 * Resolve all paths received as arguments, configuration option, or
1378 * through environment variable as absolute paths. This is necessary
1379 * since daemonizing causes the sessiond's current working directory
1382 ret
= sessiond_config_resolve_paths(&config
);
1388 lttng_opt_verbose
= config
.verbose
;
1389 lttng_opt_quiet
= config
.quiet
;
1390 kconsumer_data
.err_unix_sock_path
=
1391 config
.kconsumerd_err_unix_sock_path
.value
;
1392 kconsumer_data
.cmd_unix_sock_path
=
1393 config
.kconsumerd_cmd_unix_sock_path
.value
;
1394 ustconsumer32_data
.err_unix_sock_path
=
1395 config
.consumerd32_err_unix_sock_path
.value
;
1396 ustconsumer32_data
.cmd_unix_sock_path
=
1397 config
.consumerd32_cmd_unix_sock_path
.value
;
1398 ustconsumer64_data
.err_unix_sock_path
=
1399 config
.consumerd64_err_unix_sock_path
.value
;
1400 ustconsumer64_data
.cmd_unix_sock_path
=
1401 config
.consumerd64_cmd_unix_sock_path
.value
;
1402 set_clock_plugin_env();
1404 sessiond_config_log(&config
);
1405 sessiond_uuid_log();
1407 if (opt_print_version
) {
1413 if (create_lttng_rundir()) {
1418 /* Abort launch if a session daemon is already running. */
1419 if (check_existing_daemon()) {
1420 ERR("A session daemon is already running.");
1426 if (config
.daemonize
|| config
.background
) {
1429 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
1430 !config
.background
);
1437 * We are in the child. Make sure all other file descriptors are
1438 * closed, in case we are called with more opened file
1439 * descriptors than the standard ones and the lock file.
1441 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1442 if (i
== lockfile_fd
) {
1449 if (launch_run_as_worker(argv
[0]) < 0) {
1450 goto exit_create_run_as_worker_cleanup
;
1454 * Starting from here, we can create threads. This needs to be after
1455 * lttng_daemonize due to RCU.
1459 * Initialize the health check subsystem. This call should set the
1460 * appropriate time values.
1462 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1463 if (!health_sessiond
) {
1464 PERROR("health_app_create error");
1469 /* Create thread to clean up RCU hash tables */
1470 ht_cleanup_thread
= launch_ht_cleanup_thread();
1471 if (!ht_cleanup_thread
) {
1476 /* Create thread quit pipe */
1477 if (sessiond_init_thread_quit_pipe()) {
1482 /* Check if daemon is UID = 0 */
1483 is_root
= !getuid();
1485 /* Create global run dir with root access */
1487 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1488 if (!kernel_channel_monitor_pipe
) {
1489 ERR("Failed to create kernel consumer channel monitor pipe");
1493 kconsumer_data
.channel_monitor_pipe
=
1494 lttng_pipe_release_writefd(
1495 kernel_channel_monitor_pipe
);
1496 if (kconsumer_data
.channel_monitor_pipe
< 0) {
1502 /* Set consumer initial state */
1503 kernel_consumerd_state
= CONSUMER_STOPPED
;
1504 ust_consumerd_state
= CONSUMER_STOPPED
;
1506 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1507 if (!ust32_channel_monitor_pipe
) {
1508 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1512 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1513 ust32_channel_monitor_pipe
);
1514 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
1520 * The rotation_thread_timer_queue structure is shared between the
1521 * sessiond timer thread and the rotation thread. The main thread keeps
1522 * its ownership and destroys it when both threads have been joined.
1524 rotation_timer_queue
= rotation_thread_timer_queue_create();
1525 if (!rotation_timer_queue
) {
1529 timer_thread_parameters
.rotation_thread_job_queue
=
1530 rotation_timer_queue
;
1532 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1533 if (!ust64_channel_monitor_pipe
) {
1534 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1538 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1539 ust64_channel_monitor_pipe
);
1540 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
1546 * Init UST app hash table. Alloc hash table before this point since
1547 * cleanup() can get called after that point.
1549 if (ust_app_ht_alloc()) {
1550 ERR("Failed to allocate UST app hash table");
1555 trigger_error_accounting_init(config
.trigger_error_counter_bucket
);
1558 * Initialize agent app hash table. We allocate the hash table here
1559 * since cleanup() can get called after this point.
1561 if (agent_app_ht_alloc()) {
1562 ERR("Failed to allocate Agent app hash table");
1567 if (trigger_agent_ht_alloc()) {
1568 ERR("Failed to allocate trigger agent hash table");
1573 * These actions must be executed as root. We do that *after* setting up
1574 * the sockets path because we MUST make the check for another daemon using
1575 * those paths *before* trying to set the kernel consumer sockets and init
1579 if (set_consumer_sockets(&kconsumer_data
)) {
1584 /* Setup kernel tracer */
1585 if (!config
.no_kernel
) {
1586 init_kernel_tracer();
1589 /* Set ulimit for open files */
1592 /* init lttng_fd tracking must be done after set_ulimit. */
1595 if (set_consumer_sockets(&ustconsumer64_data
)) {
1600 if (set_consumer_sockets(&ustconsumer32_data
)) {
1605 /* Get parent pid if -S, --sig-parent is specified. */
1606 if (config
.sig_parent
) {
1610 /* Setup the kernel pipe for waking up the kernel thread */
1611 if (is_root
&& !config
.no_kernel
) {
1612 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
1618 /* Setup the thread apps communication pipe. */
1619 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1624 /* Setup the thread apps notify communication pipe. */
1625 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1630 /* Initialize global buffer per UID and PID registry. */
1631 buffer_reg_init_uid_registry();
1632 buffer_reg_init_pid_registry();
1634 /* Init UST command queue. */
1635 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1639 /* Check for the application socket timeout env variable. */
1640 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1641 if (env_app_timeout
) {
1642 config
.app_socket_timeout
= atoi(env_app_timeout
);
1644 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1647 ret
= write_pidfile();
1649 ERR("Error in write_pidfile");
1654 /* Initialize communication library */
1656 /* Initialize TCP timeout values */
1657 lttcomm_inet_init();
1659 /* Create health-check thread. */
1660 if (!launch_health_management_thread()) {
1665 /* notification_thread_data acquires the pipes' read side. */
1666 notification_thread_handle
= notification_thread_handle_create(
1667 ust32_channel_monitor_pipe
,
1668 ust64_channel_monitor_pipe
,
1669 kernel_channel_monitor_pipe
,
1670 kernel_get_notification_fd());
1671 if (!notification_thread_handle
) {
1673 ERR("Failed to create notification thread shared data");
1677 /* Create notification thread. */
1678 notification_thread
= launch_notification_thread(
1679 notification_thread_handle
);
1680 if (!notification_thread
) {
1685 /* Create timer thread. */
1686 if (!launch_timer_thread(&timer_thread_parameters
)) {
1691 /* rotation_thread_data acquires the pipes' read side. */
1692 rotation_thread_handle
= rotation_thread_handle_create(
1693 rotation_timer_queue
,
1694 notification_thread_handle
);
1695 if (!rotation_thread_handle
) {
1697 ERR("Failed to create rotation thread shared data");
1702 /* Create rotation thread. */
1703 if (!launch_rotation_thread(rotation_thread_handle
)) {
1708 /* Create thread to manage the client socket */
1709 client_thread
= launch_client_thread();
1710 if (!client_thread
) {
1715 /* Set credentials of the client socket and rundir */
1716 if (is_root
&& set_permissions(config
.rundir
.value
)) {
1721 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1722 apps_cmd_notify_pipe
[1])) {
1727 /* Create thread to manage application registration. */
1728 register_apps_thread
= launch_application_registration_thread(
1730 if (!register_apps_thread
) {
1735 /* Create thread to manage application socket */
1736 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1741 /* Create thread to manage application notify socket */
1742 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1747 /* Create agent management thread. */
1748 if (!launch_agent_management_thread()) {
1753 /* Don't start this thread if kernel tracing is not requested nor root */
1754 if (is_root
&& !config
.no_kernel
) {
1755 /* Create kernel thread to manage kernel event */
1756 if (!launch_kernel_management_thread(kernel_poll_pipe
[0])) {
1761 if (kernel_get_notification_fd() > -1) {
1762 ret
= notification_thread_command_add_application(
1763 notification_thread_handle
, kernel_get_notification_fd(), LTTNG_DOMAIN_KERNEL
);
1764 if (ret
!= LTTNG_OK
) {
1765 ERR("Failed to add kernel trigger event source to notification thread");
1772 /* Load sessions. */
1773 ret
= config_load_session(config
.load_session_path
.value
,
1776 ERR("Session load failed: %s", error_get_str(ret
));
1781 /* Initialization completed. */
1782 sessiond_signal_parents();
1785 * This is where we start awaiting program completion (e.g. through
1786 * signal that asks threads to teardown).
1789 /* Initiate teardown once activity occurs on the quit pipe. */
1790 sessiond_wait_for_quit_pipe(-1);
1794 trigger_error_accounting_fini();
1796 * Ensure that the client thread is no longer accepting new commands,
1797 * which could cause new sessions to be created.
1799 if (client_thread
) {
1800 lttng_thread_shutdown(client_thread
);
1801 lttng_thread_put(client_thread
);
1804 destroy_all_sessions_and_wait();
1806 if (register_apps_thread
) {
1807 lttng_thread_shutdown(register_apps_thread
);
1808 lttng_thread_put(register_apps_thread
);
1810 lttng_thread_list_shutdown_orphans();
1813 * Wait for all pending call_rcu work to complete before tearing
1814 * down data structures. call_rcu worker may be trying to
1815 * perform lookups in those structures.
1819 * sessiond_cleanup() is called when no other thread is running, except
1820 * the ht_cleanup thread, which is needed to destroy the hash tables.
1822 rcu_thread_online();
1825 if (notification_thread
) {
1826 lttng_thread_shutdown(notification_thread
);
1827 lttng_thread_put(notification_thread
);
1831 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1832 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1833 * the queue is empty before shutting down the clean-up thread.
1837 if (ht_cleanup_thread
) {
1838 lttng_thread_shutdown(ht_cleanup_thread
);
1839 lttng_thread_put(ht_cleanup_thread
);
1842 rcu_thread_offline();
1843 rcu_unregister_thread();
1845 if (rotation_thread_handle
) {
1846 rotation_thread_handle_destroy(rotation_thread_handle
);
1850 * After the rotation and timer thread have quit, we can safely destroy
1851 * the rotation_timer_queue.
1853 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1855 * The teardown of the notification system is performed after the
1856 * session daemon's teardown in order to allow it to be notified
1857 * of the active session and channels at the moment of the teardown.
1859 if (notification_thread_handle
) {
1860 notification_thread_handle_destroy(notification_thread_handle
);
1862 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1863 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1864 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1866 if (health_sessiond
) {
1867 health_app_destroy(health_sessiond
);
1869 exit_create_run_as_worker_cleanup
:
1871 sessiond_cleanup_lock_file();
1872 sessiond_cleanup_options();
1874 exit_set_signal_handler
: