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.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/lockfile.h>
41 #include <common/path.h>
42 #include <common/daemonize.h>
43 #include <common/config/session-config.h>
44 #include <common/dynamic-buffer.h>
45 #include <lttng/event-internal.h>
47 #include "lttng-sessiond.h"
48 #include "buffer-registry.h"
55 #include "kernel-consumer.h"
57 #include "lttng-ust-ctl.h"
58 #include "ust-consumer.h"
61 #include "health-sessiond.h"
62 #include "testpoint.h"
63 #include "notify-apps.h"
64 #include "agent-thread.h"
66 #include "notification-thread.h"
67 #include "notification-thread-commands.h"
68 #include "rotation-thread.h"
70 #include "ht-cleanup.h"
71 #include "sessiond-config.h"
77 #include "manage-apps.h"
78 #include "manage-kernel.h"
80 static const char *help_msg
=
81 #ifdef LTTNG_EMBED_HELP
82 #include <lttng-sessiond.8.h>
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' },
128 /* Command line options to ignore from configuration file */
129 static const char *config_ignore_options
[] = { "help", "version", "config" };
132 * This pipe is used to inform the thread managing application communication
133 * that a command is queued and ready to be processed.
135 static int apps_cmd_pipe
[2] = { -1, -1 };
136 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
139 * UST registration command queue. This queue is tied with a futex and uses a N
140 * wakers / 1 waiter implemented and detailed in futex.c/.h
142 * The thread_registration_apps and thread_dispatch_ust_registration uses this
143 * queue along with the wait/wake scheme. The thread_manage_apps receives down
144 * the line new application socket and monitors it for any I/O error or clean
145 * close that triggers an unregistration of the application.
147 static struct ust_cmd_queue ust_cmd_queue
;
150 * Section name to look for in the daemon configuration file.
152 static const char * const config_section_name
= "sessiond";
154 /* Am I root or not. Set to 1 if the daemon is running as root */
158 * Stop all threads by closing the thread quit pipe.
160 static void stop_threads(void)
164 /* Stopping all threads */
165 DBG("Terminating all threads");
166 ret
= sessiond_notify_quit_pipe();
168 ERR("write error on thread quit pipe");
173 * Close every consumer sockets.
175 static void close_consumer_sockets(void)
179 if (kconsumer_data
.err_sock
>= 0) {
180 ret
= close(kconsumer_data
.err_sock
);
182 PERROR("kernel consumer err_sock close");
185 if (ustconsumer32_data
.err_sock
>= 0) {
186 ret
= close(ustconsumer32_data
.err_sock
);
188 PERROR("UST consumerd32 err_sock close");
191 if (ustconsumer64_data
.err_sock
>= 0) {
192 ret
= close(ustconsumer64_data
.err_sock
);
194 PERROR("UST consumerd64 err_sock close");
197 if (kconsumer_data
.cmd_sock
>= 0) {
198 ret
= close(kconsumer_data
.cmd_sock
);
200 PERROR("kernel consumer cmd_sock close");
203 if (ustconsumer32_data
.cmd_sock
>= 0) {
204 ret
= close(ustconsumer32_data
.cmd_sock
);
206 PERROR("UST consumerd32 cmd_sock close");
209 if (ustconsumer64_data
.cmd_sock
>= 0) {
210 ret
= close(ustconsumer64_data
.cmd_sock
);
212 PERROR("UST consumerd64 cmd_sock close");
215 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
216 ret
= close(kconsumer_data
.channel_monitor_pipe
);
218 PERROR("kernel consumer channel monitor pipe close");
221 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
222 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
224 PERROR("UST consumerd32 channel monitor pipe close");
227 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
228 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
230 PERROR("UST consumerd64 channel monitor pipe close");
236 * Wait on consumer process termination.
238 * Need to be called with the consumer data lock held or from a context
239 * ensuring no concurrent access to data (e.g: cleanup).
241 static void wait_consumer(struct consumer_data
*consumer_data
)
246 if (consumer_data
->pid
<= 0) {
250 DBG("Waiting for complete teardown of consumerd (PID: %d)",
252 ret
= waitpid(consumer_data
->pid
, &status
, 0);
254 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
255 } else if (!WIFEXITED(status
)) {
256 ERR("consumerd termination with error: %d",
259 consumer_data
->pid
= 0;
263 * Cleanup the session daemon's data structures.
265 static void sessiond_cleanup(void)
268 struct ltt_session_list
*session_list
= session_get_list();
270 DBG("Cleanup sessiond");
273 * Close the thread quit pipe. It has already done its job,
274 * since we are now called.
276 sessiond_close_quit_pipe();
277 utils_close_pipe(apps_cmd_pipe
);
278 utils_close_pipe(apps_cmd_notify_pipe
);
279 utils_close_pipe(kernel_poll_pipe
);
281 ret
= remove(config
.pid_file_path
.value
);
283 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
286 DBG("Removing sessiond and consumerd content of directory %s",
287 config
.rundir
.value
);
290 DBG("Removing %s", config
.pid_file_path
.value
);
291 (void) unlink(config
.pid_file_path
.value
);
293 DBG("Removing %s", config
.agent_port_file_path
.value
);
294 (void) unlink(config
.agent_port_file_path
.value
);
297 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
298 (void) unlink(kconsumer_data
.err_unix_sock_path
);
300 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
301 (void) rmdir(config
.kconsumerd_path
.value
);
303 /* ust consumerd 32 */
304 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
305 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
307 DBG("Removing directory %s", config
.consumerd32_path
.value
);
308 (void) rmdir(config
.consumerd32_path
.value
);
310 /* ust consumerd 64 */
311 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
312 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
314 DBG("Removing directory %s", config
.consumerd64_path
.value
);
315 (void) rmdir(config
.consumerd64_path
.value
);
317 pthread_mutex_destroy(&session_list
->lock
);
319 DBG("Cleaning up all agent apps");
320 agent_app_ht_clean();
321 DBG("Closing all UST sockets");
322 ust_app_clean_list();
323 buffer_reg_destroy_registries();
325 close_consumer_sockets();
327 wait_consumer(&kconsumer_data
);
328 wait_consumer(&ustconsumer64_data
);
329 wait_consumer(&ustconsumer32_data
);
331 if (is_root
&& !config
.no_kernel
) {
332 cleanup_kernel_tracer();
336 * We do NOT rmdir rundir because there are other processes
337 * using it, for instance lttng-relayd, which can start in
338 * parallel with this teardown.
343 * Cleanup the daemon's option data structures.
345 static void sessiond_cleanup_options(void)
347 DBG("Cleaning up options");
349 sessiond_config_fini(&config
);
351 run_as_destroy_worker();
354 static int string_match(const char *str1
, const char *str2
)
356 return (str1
&& str2
) && !strcmp(str1
, str2
);
360 * Take an option from the getopt output and set it in the right variable to be
363 * Return 0 on success else a negative value.
365 static int set_option(int opt
, const char *arg
, const char *optname
)
369 if (string_match(optname
, "client-sock") || opt
== 'c') {
370 if (!arg
|| *arg
== '\0') {
374 if (lttng_is_setuid_setgid()) {
375 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
376 "-c, --client-sock");
378 config_string_set(&config
.client_unix_sock_path
,
380 if (!config
.client_unix_sock_path
.value
) {
385 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
386 if (!arg
|| *arg
== '\0') {
390 if (lttng_is_setuid_setgid()) {
391 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
394 config_string_set(&config
.apps_unix_sock_path
,
396 if (!config
.apps_unix_sock_path
.value
) {
401 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
402 config
.daemonize
= true;
403 } else if (string_match(optname
, "background") || opt
== 'b') {
404 config
.background
= true;
405 } else if (string_match(optname
, "group") || opt
== 'g') {
406 if (!arg
|| *arg
== '\0') {
410 if (lttng_is_setuid_setgid()) {
411 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
414 config_string_set(&config
.tracing_group_name
,
416 if (!config
.tracing_group_name
.value
) {
421 } else if (string_match(optname
, "help") || opt
== 'h') {
422 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
424 ERR("Cannot show --help for `lttng-sessiond`");
427 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
428 } else if (string_match(optname
, "version") || opt
== 'V') {
429 opt_print_version
= 1;
430 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
431 config
.sig_parent
= true;
432 } else if (string_match(optname
, "kconsumerd-err-sock")) {
433 if (!arg
|| *arg
== '\0') {
437 if (lttng_is_setuid_setgid()) {
438 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
439 "--kconsumerd-err-sock");
441 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
443 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
448 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
449 if (!arg
|| *arg
== '\0') {
453 if (lttng_is_setuid_setgid()) {
454 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
455 "--kconsumerd-cmd-sock");
457 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
459 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
464 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
465 if (!arg
|| *arg
== '\0') {
469 if (lttng_is_setuid_setgid()) {
470 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
471 "--ustconsumerd64-err-sock");
473 config_string_set(&config
.consumerd64_err_unix_sock_path
,
475 if (!config
.consumerd64_err_unix_sock_path
.value
) {
480 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
481 if (!arg
|| *arg
== '\0') {
485 if (lttng_is_setuid_setgid()) {
486 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
487 "--ustconsumerd64-cmd-sock");
489 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
491 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
496 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
497 if (!arg
|| *arg
== '\0') {
501 if (lttng_is_setuid_setgid()) {
502 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
503 "--ustconsumerd32-err-sock");
505 config_string_set(&config
.consumerd32_err_unix_sock_path
,
507 if (!config
.consumerd32_err_unix_sock_path
.value
) {
512 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
513 if (!arg
|| *arg
== '\0') {
517 if (lttng_is_setuid_setgid()) {
518 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
519 "--ustconsumerd32-cmd-sock");
521 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
523 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
528 } else if (string_match(optname
, "no-kernel")) {
529 config
.no_kernel
= true;
530 } else if (string_match(optname
, "quiet") || opt
== 'q') {
532 } else if (string_match(optname
, "verbose") || opt
== 'v') {
533 /* Verbose level can increase using multiple -v */
535 /* Value obtained from config file */
536 config
.verbose
= config_parse_value(arg
);
538 /* -v used on command line */
541 /* Clamp value to [0, 3] */
542 config
.verbose
= config
.verbose
< 0 ? 0 :
543 (config
.verbose
<= 3 ? config
.verbose
: 3);
544 } else if (string_match(optname
, "verbose-consumer")) {
546 config
.verbose_consumer
= config_parse_value(arg
);
548 config
.verbose_consumer
++;
550 } else if (string_match(optname
, "consumerd32-path")) {
551 if (!arg
|| *arg
== '\0') {
555 if (lttng_is_setuid_setgid()) {
556 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
557 "--consumerd32-path");
559 config_string_set(&config
.consumerd32_bin_path
,
561 if (!config
.consumerd32_bin_path
.value
) {
566 } else if (string_match(optname
, "consumerd32-libdir")) {
567 if (!arg
|| *arg
== '\0') {
571 if (lttng_is_setuid_setgid()) {
572 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
573 "--consumerd32-libdir");
575 config_string_set(&config
.consumerd32_lib_dir
,
577 if (!config
.consumerd32_lib_dir
.value
) {
582 } else if (string_match(optname
, "consumerd64-path")) {
583 if (!arg
|| *arg
== '\0') {
587 if (lttng_is_setuid_setgid()) {
588 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
589 "--consumerd64-path");
591 config_string_set(&config
.consumerd64_bin_path
,
593 if (!config
.consumerd64_bin_path
.value
) {
598 } else if (string_match(optname
, "consumerd64-libdir")) {
599 if (!arg
|| *arg
== '\0') {
603 if (lttng_is_setuid_setgid()) {
604 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
605 "--consumerd64-libdir");
607 config_string_set(&config
.consumerd64_lib_dir
,
609 if (!config
.consumerd64_lib_dir
.value
) {
614 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
615 if (!arg
|| *arg
== '\0') {
619 if (lttng_is_setuid_setgid()) {
620 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
623 config_string_set(&config
.pid_file_path
, strdup(arg
));
624 if (!config
.pid_file_path
.value
) {
629 } else if (string_match(optname
, "agent-tcp-port")) {
630 if (!arg
|| *arg
== '\0') {
634 if (lttng_is_setuid_setgid()) {
635 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
641 v
= strtoul(arg
, NULL
, 0);
642 if (errno
!= 0 || !isdigit(arg
[0])) {
643 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
646 if (v
== 0 || v
>= 65535) {
647 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
650 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
651 DBG3("Agent TCP port set to non default: %i", (int) v
);
653 } else if (string_match(optname
, "load") || opt
== 'l') {
654 if (!arg
|| *arg
== '\0') {
658 if (lttng_is_setuid_setgid()) {
659 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
662 config_string_set(&config
.load_session_path
, strdup(arg
));
663 if (!config
.load_session_path
.value
) {
668 } else if (string_match(optname
, "kmod-probes")) {
669 if (!arg
|| *arg
== '\0') {
673 if (lttng_is_setuid_setgid()) {
674 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
677 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
678 if (!config
.kmod_probes_list
.value
) {
683 } else if (string_match(optname
, "extra-kmod-probes")) {
684 if (!arg
|| *arg
== '\0') {
688 if (lttng_is_setuid_setgid()) {
689 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
690 "--extra-kmod-probes");
692 config_string_set(&config
.kmod_extra_probes_list
,
694 if (!config
.kmod_extra_probes_list
.value
) {
699 } else if (string_match(optname
, "config") || opt
== 'f') {
700 /* This is handled in set_options() thus silent skip. */
703 /* Unknown option or other error.
704 * Error is printed by getopt, just return */
709 if (ret
== -EINVAL
) {
710 const char *opt_name
= "unknown";
713 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
715 if (opt
== long_options
[i
].val
) {
716 opt_name
= long_options
[i
].name
;
721 WARN("Invalid argument provided for option \"%s\", using default value.",
729 * config_entry_handler_cb used to handle options read from a config file.
730 * See config_entry_handler_cb comment in common/config/session-config.h for the
731 * return value conventions.
733 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
737 if (!entry
|| !entry
->name
|| !entry
->value
) {
742 /* Check if the option is to be ignored */
743 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
744 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
749 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
752 /* Ignore if not fully matched. */
753 if (strcmp(entry
->name
, long_options
[i
].name
)) {
758 * If the option takes no argument on the command line, we have to
759 * check if the value is "true". We support non-zero numeric values,
762 if (!long_options
[i
].has_arg
) {
763 ret
= config_parse_value(entry
->value
);
766 WARN("Invalid configuration value \"%s\" for option %s",
767 entry
->value
, entry
->name
);
769 /* False, skip boolean config option. */
774 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
778 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
784 static void print_version(void) {
785 fprintf(stdout
, "%s\n", VERSION
);
789 * daemon configuration loading and argument parsing
791 static int set_options(int argc
, char **argv
)
793 int ret
= 0, c
= 0, option_index
= 0;
794 int orig_optopt
= optopt
, orig_optind
= optind
;
796 char *config_path
= NULL
;
798 optstring
= utils_generate_optstring(long_options
,
799 sizeof(long_options
) / sizeof(struct option
));
805 /* Check for the --config option */
806 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
807 &option_index
)) != -1) {
811 } else if (c
!= 'f') {
812 /* if not equal to --config option. */
816 if (lttng_is_setuid_setgid()) {
817 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
821 config_path
= utils_expand_path(optarg
);
823 ERR("Failed to resolve path: %s", optarg
);
828 ret
= config_get_section_entries(config_path
, config_section_name
,
829 config_entry_handler
, NULL
);
832 ERR("Invalid configuration option at line %i", ret
);
838 /* Reset getopt's global state */
839 optopt
= orig_optopt
;
840 optind
= orig_optind
;
844 * getopt_long() will not set option_index if it encounters a
847 c
= getopt_long(argc
, argv
, optstring
, long_options
,
854 * Pass NULL as the long option name if popt left the index
857 ret
= set_option(c
, optarg
,
858 option_index
< 0 ? NULL
:
859 long_options
[option_index
].name
);
872 * Create lockfile using the rundir and return its fd.
874 static int create_lockfile(void)
876 return utils_create_lock_file(config
.lock_file_path
.value
);
880 * Check if the global socket is available, and if a daemon is answering at the
881 * other side. If yes, error is returned.
883 * Also attempts to create and hold the lock file.
885 static int check_existing_daemon(void)
889 /* Is there anybody out there ? */
890 if (lttng_session_daemon_alive()) {
895 lockfile_fd
= create_lockfile();
896 if (lockfile_fd
< 0) {
904 static void sessiond_cleanup_lock_file(void)
909 * Cleanup lock file by deleting it and finaly closing it which will
910 * release the file system lock.
912 if (lockfile_fd
>= 0) {
913 ret
= remove(config
.lock_file_path
.value
);
915 PERROR("remove lock file");
917 ret
= close(lockfile_fd
);
919 PERROR("close lock file");
925 * Set the tracing group gid onto the client socket.
927 * Race window between mkdir and chown is OK because we are going from more
928 * permissive (root.root) to less permissive (root.tracing).
930 static int set_permissions(char *rundir
)
935 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true, &gid
);
937 /* Default to root group. */
941 /* Set lttng run dir */
942 ret
= chown(rundir
, 0, gid
);
944 ERR("Unable to set group on %s", rundir
);
949 * Ensure all applications and tracing group can search the run
950 * dir. Allow everyone to read the directory, since it does not
951 * buy us anything to hide its content.
953 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
955 ERR("Unable to set permissions on %s", rundir
);
959 /* lttng client socket path */
960 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
962 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
966 /* kconsumer error socket path */
967 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
969 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
973 /* 64-bit ustconsumer error socket path */
974 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
976 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
980 /* 32-bit ustconsumer compat32 error socket path */
981 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
983 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
987 DBG("All permissions are set");
993 * Create the lttng run directory needed for all global sockets and pipe.
995 static int create_lttng_rundir(void)
999 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
1001 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
1003 if (errno
!= EEXIST
) {
1004 ERR("Unable to create %s", config
.rundir
.value
);
1016 * Setup sockets and directory needed by the consumerds' communication with the
1019 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1024 switch (consumer_data
->type
) {
1025 case LTTNG_CONSUMER_KERNEL
:
1026 path
= config
.kconsumerd_path
.value
;
1028 case LTTNG_CONSUMER64_UST
:
1029 path
= config
.consumerd64_path
.value
;
1031 case LTTNG_CONSUMER32_UST
:
1032 path
= config
.consumerd32_path
.value
;
1035 ERR("Consumer type unknown");
1041 DBG2("Creating consumer directory: %s", path
);
1043 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1044 if (ret
< 0 && errno
!= EEXIST
) {
1046 ERR("Failed to create %s", path
);
1052 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
1055 /* Default to root group. */
1059 ret
= chown(path
, 0, gid
);
1061 ERR("Unable to set group on %s", path
);
1067 /* Create the consumerd error unix socket */
1068 consumer_data
->err_sock
=
1069 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1070 if (consumer_data
->err_sock
< 0) {
1071 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1077 * Set the CLOEXEC flag. Return code is useless because either way, the
1080 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1082 PERROR("utils_set_fd_cloexec");
1083 /* continue anyway */
1086 /* File permission MUST be 660 */
1087 ret
= chmod(consumer_data
->err_unix_sock_path
,
1088 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1090 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1100 * Signal handler for the daemon
1102 * Simply stop all worker threads, leaving main() return gracefully after
1103 * joining all threads and calling cleanup().
1105 static void sighandler(int sig
)
1109 DBG("SIGINT caught");
1113 DBG("SIGTERM caught");
1117 CMM_STORE_SHARED(recv_child_signal
, 1);
1125 * Setup signal handler for :
1126 * SIGINT, SIGTERM, SIGPIPE
1128 static int set_signal_handler(void)
1131 struct sigaction sa
;
1134 if ((ret
= sigemptyset(&sigset
)) < 0) {
1135 PERROR("sigemptyset");
1139 sa
.sa_mask
= sigset
;
1142 sa
.sa_handler
= sighandler
;
1143 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1144 PERROR("sigaction");
1148 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1149 PERROR("sigaction");
1153 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1154 PERROR("sigaction");
1158 sa
.sa_handler
= SIG_IGN
;
1159 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1160 PERROR("sigaction");
1164 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1170 * Set open files limit to unlimited. This daemon can open a large number of
1171 * file descriptors in order to consume multiple kernel traces.
1173 static void set_ulimit(void)
1178 /* The kernel does not allow an infinite limit for open files */
1179 lim
.rlim_cur
= 65535;
1180 lim
.rlim_max
= 65535;
1182 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1184 PERROR("failed to set open files limit");
1188 static int write_pidfile(void)
1190 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1193 static int set_clock_plugin_env(void)
1196 char *env_value
= NULL
;
1198 if (!config
.lttng_ust_clock_plugin
.value
) {
1202 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1203 config
.lttng_ust_clock_plugin
.value
);
1209 ret
= putenv(env_value
);
1212 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1216 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1217 config
.lttng_ust_clock_plugin
.value
);
1222 static void destroy_all_sessions_and_wait(void)
1224 struct ltt_session
*session
, *tmp
;
1225 struct ltt_session_list
*session_list
;
1227 session_list
= session_get_list();
1228 DBG("Initiating destruction of all sessions");
1230 if (!session_list
) {
1234 session_lock_list();
1235 /* Initiate the destruction of all sessions. */
1236 cds_list_for_each_entry_safe(session
, tmp
,
1237 &session_list
->head
, list
) {
1238 if (!session_get(session
)) {
1242 session_lock(session
);
1243 if (session
->destroyed
) {
1244 goto unlock_session
;
1246 (void) cmd_stop_trace(session
);
1247 (void) cmd_destroy_session(session
, notification_thread_handle
,
1250 session_unlock(session
);
1251 session_put(session
);
1253 session_unlock_list();
1255 /* Wait for the destruction of all sessions to complete. */
1256 DBG("Waiting for the destruction of all sessions to complete");
1257 session_list_wait_empty();
1258 DBG("Destruction of all sessions completed");
1261 static int run_as_worker_post_fork_cleanup(void *data
)
1263 struct sessiond_config
*sessiond_config
= data
;
1265 sessiond_config_fini(sessiond_config
);
1269 static int launch_run_as_worker(const char *procname
)
1272 * Clean-up before forking the run-as worker. Any dynamically
1273 * allocated memory of which the worker is not aware will
1274 * be leaked as the process forks a run-as worker (and performs
1275 * no exec*()). The same would apply to any opened fd.
1277 return run_as_create_worker(procname
, run_as_worker_post_fork_cleanup
,
1281 static void sessiond_uuid_log(void)
1283 char uuid_str
[LTTNG_UUID_STR_LEN
];
1285 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
1286 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1292 int main(int argc
, char **argv
)
1294 int ret
= 0, retval
= 0;
1295 const char *env_app_timeout
;
1296 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1297 *ust64_channel_monitor_pipe
= NULL
,
1298 *kernel_channel_monitor_pipe
= NULL
;
1299 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1300 struct timer_thread_parameters timer_thread_parameters
;
1301 /* Rotation thread handle. */
1302 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1303 /* Queue of rotation jobs populated by the sessiond-timer. */
1304 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1305 struct lttng_thread
*client_thread
= NULL
;
1306 struct lttng_thread
*notification_thread
= NULL
;
1307 struct lttng_thread
*register_apps_thread
= NULL
;
1309 init_kernel_workarounds();
1311 rcu_register_thread();
1313 if (set_signal_handler()) {
1315 goto exit_set_signal_handler
;
1318 if (timer_signal_init()) {
1320 goto exit_set_signal_handler
;
1323 page_size
= sysconf(_SC_PAGESIZE
);
1324 if (page_size
< 0) {
1325 PERROR("sysconf _SC_PAGESIZE");
1326 page_size
= LONG_MAX
;
1327 WARN("Fallback page size to %ld", page_size
);
1330 ret
= sessiond_config_init(&config
);
1333 goto exit_set_signal_handler
;
1337 * Init config from environment variables.
1338 * Command line option override env configuration per-doc. Do env first.
1340 sessiond_config_apply_env_config(&config
);
1343 * Parse arguments and load the daemon configuration file.
1345 * We have an exit_options exit path to free memory reserved by
1346 * set_options. This is needed because the rest of sessiond_cleanup()
1347 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1348 * depends on set_options.
1351 if (set_options(argc
, argv
)) {
1357 * Resolve all paths received as arguments, configuration option, or
1358 * through environment variable as absolute paths. This is necessary
1359 * since daemonizing causes the sessiond's current working directory
1362 ret
= sessiond_config_resolve_paths(&config
);
1368 lttng_opt_verbose
= config
.verbose
;
1369 lttng_opt_quiet
= config
.quiet
;
1370 kconsumer_data
.err_unix_sock_path
=
1371 config
.kconsumerd_err_unix_sock_path
.value
;
1372 kconsumer_data
.cmd_unix_sock_path
=
1373 config
.kconsumerd_cmd_unix_sock_path
.value
;
1374 ustconsumer32_data
.err_unix_sock_path
=
1375 config
.consumerd32_err_unix_sock_path
.value
;
1376 ustconsumer32_data
.cmd_unix_sock_path
=
1377 config
.consumerd32_cmd_unix_sock_path
.value
;
1378 ustconsumer64_data
.err_unix_sock_path
=
1379 config
.consumerd64_err_unix_sock_path
.value
;
1380 ustconsumer64_data
.cmd_unix_sock_path
=
1381 config
.consumerd64_cmd_unix_sock_path
.value
;
1382 set_clock_plugin_env();
1384 sessiond_config_log(&config
);
1385 sessiond_uuid_log();
1387 if (opt_print_version
) {
1393 if (create_lttng_rundir()) {
1398 /* Abort launch if a session daemon is already running. */
1399 if (check_existing_daemon()) {
1400 ERR("A session daemon is already running.");
1406 if (config
.daemonize
|| config
.background
) {
1409 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
1410 !config
.background
);
1417 * We are in the child. Make sure all other file descriptors are
1418 * closed, in case we are called with more opened file
1419 * descriptors than the standard ones and the lock file.
1421 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1422 if (i
== lockfile_fd
) {
1429 if (launch_run_as_worker(argv
[0]) < 0) {
1430 goto exit_create_run_as_worker_cleanup
;
1434 * Starting from here, we can create threads. This needs to be after
1435 * lttng_daemonize due to RCU.
1439 * Initialize the health check subsystem. This call should set the
1440 * appropriate time values.
1442 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1443 if (!health_sessiond
) {
1444 PERROR("health_app_create error");
1449 /* Create thread to clean up RCU hash tables */
1450 ht_cleanup_thread
= launch_ht_cleanup_thread();
1451 if (!ht_cleanup_thread
) {
1456 /* Create thread quit pipe */
1457 if (sessiond_init_thread_quit_pipe()) {
1462 /* Check if daemon is UID = 0 */
1463 is_root
= !getuid();
1465 /* Create global run dir with root access */
1467 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1468 if (!kernel_channel_monitor_pipe
) {
1469 ERR("Failed to create kernel consumer channel monitor pipe");
1473 kconsumer_data
.channel_monitor_pipe
=
1474 lttng_pipe_release_writefd(
1475 kernel_channel_monitor_pipe
);
1476 if (kconsumer_data
.channel_monitor_pipe
< 0) {
1482 /* Set consumer initial state */
1483 kernel_consumerd_state
= CONSUMER_STOPPED
;
1484 ust_consumerd_state
= CONSUMER_STOPPED
;
1486 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1487 if (!ust32_channel_monitor_pipe
) {
1488 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1492 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1493 ust32_channel_monitor_pipe
);
1494 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
1500 * The rotation_thread_timer_queue structure is shared between the
1501 * sessiond timer thread and the rotation thread. The main thread keeps
1502 * its ownership and destroys it when both threads have been joined.
1504 rotation_timer_queue
= rotation_thread_timer_queue_create();
1505 if (!rotation_timer_queue
) {
1509 timer_thread_parameters
.rotation_thread_job_queue
=
1510 rotation_timer_queue
;
1512 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1513 if (!ust64_channel_monitor_pipe
) {
1514 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1518 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1519 ust64_channel_monitor_pipe
);
1520 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
1526 * Init UST app hash table. Alloc hash table before this point since
1527 * cleanup() can get called after that point.
1529 if (ust_app_ht_alloc()) {
1530 ERR("Failed to allocate UST app hash table");
1536 * Initialize agent app hash table. We allocate the hash table here
1537 * since cleanup() can get called after this point.
1539 if (agent_app_ht_alloc()) {
1540 ERR("Failed to allocate Agent app hash table");
1546 * These actions must be executed as root. We do that *after* setting up
1547 * the sockets path because we MUST make the check for another daemon using
1548 * those paths *before* trying to set the kernel consumer sockets and init
1552 if (set_consumer_sockets(&kconsumer_data
)) {
1557 /* Setup kernel tracer */
1558 if (!config
.no_kernel
) {
1559 init_kernel_tracer();
1562 /* Set ulimit for open files */
1565 /* init lttng_fd tracking must be done after set_ulimit. */
1568 if (set_consumer_sockets(&ustconsumer64_data
)) {
1573 if (set_consumer_sockets(&ustconsumer32_data
)) {
1578 /* Get parent pid if -S, --sig-parent is specified. */
1579 if (config
.sig_parent
) {
1583 /* Setup the kernel pipe for waking up the kernel thread */
1584 if (is_root
&& !config
.no_kernel
) {
1585 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
1591 /* Setup the thread apps communication pipe. */
1592 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1597 /* Setup the thread apps notify communication pipe. */
1598 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1603 /* Initialize global buffer per UID and PID registry. */
1604 buffer_reg_init_uid_registry();
1605 buffer_reg_init_pid_registry();
1607 /* Init UST command queue. */
1608 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1612 /* Check for the application socket timeout env variable. */
1613 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1614 if (env_app_timeout
) {
1615 config
.app_socket_timeout
= atoi(env_app_timeout
);
1617 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1620 ret
= write_pidfile();
1622 ERR("Error in write_pidfile");
1627 /* Initialize communication library */
1629 /* Initialize TCP timeout values */
1630 lttcomm_inet_init();
1632 /* Create health-check thread. */
1633 if (!launch_health_management_thread()) {
1638 /* notification_thread_data acquires the pipes' read side. */
1639 notification_thread_handle
= notification_thread_handle_create(
1640 ust32_channel_monitor_pipe
,
1641 ust64_channel_monitor_pipe
,
1642 kernel_channel_monitor_pipe
);
1643 if (!notification_thread_handle
) {
1645 ERR("Failed to create notification thread shared data");
1649 /* Create notification thread. */
1650 notification_thread
= launch_notification_thread(
1651 notification_thread_handle
);
1652 if (!notification_thread
) {
1657 /* Create timer thread. */
1658 if (!launch_timer_thread(&timer_thread_parameters
)) {
1663 /* rotation_thread_data acquires the pipes' read side. */
1664 rotation_thread_handle
= rotation_thread_handle_create(
1665 rotation_timer_queue
,
1666 notification_thread_handle
);
1667 if (!rotation_thread_handle
) {
1669 ERR("Failed to create rotation thread shared data");
1674 /* Create rotation thread. */
1675 if (!launch_rotation_thread(rotation_thread_handle
)) {
1680 /* Create thread to manage the client socket */
1681 client_thread
= launch_client_thread();
1682 if (!client_thread
) {
1687 /* Set credentials of the client socket and rundir */
1688 if (is_root
&& set_permissions(config
.rundir
.value
)) {
1693 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1694 apps_cmd_notify_pipe
[1])) {
1699 /* Create thread to manage application registration. */
1700 register_apps_thread
= launch_application_registration_thread(
1702 if (!register_apps_thread
) {
1707 /* Create thread to manage application socket */
1708 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1713 /* Create thread to manage application notify socket */
1714 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1719 /* Create agent management thread. */
1720 if (!launch_agent_management_thread()) {
1725 /* Don't start this thread if kernel tracing is not requested nor root */
1726 if (is_root
&& !config
.no_kernel
) {
1727 /* Create kernel thread to manage kernel event */
1728 if (!launch_kernel_management_thread(kernel_poll_pipe
[0])) {
1734 /* Load sessions. */
1735 ret
= config_load_session(config
.load_session_path
.value
,
1738 ERR("Session load failed: %s", error_get_str(ret
));
1743 /* Initialization completed. */
1744 sessiond_signal_parents();
1747 * This is where we start awaiting program completion (e.g. through
1748 * signal that asks threads to teardown).
1751 /* Initiate teardown once activity occurs on the quit pipe. */
1752 sessiond_wait_for_quit_pipe(-1);
1756 * Ensure that the client thread is no longer accepting new commands,
1757 * which could cause new sessions to be created.
1759 if (client_thread
) {
1760 lttng_thread_shutdown(client_thread
);
1761 lttng_thread_put(client_thread
);
1764 destroy_all_sessions_and_wait();
1766 if (register_apps_thread
) {
1767 lttng_thread_shutdown(register_apps_thread
);
1768 lttng_thread_put(register_apps_thread
);
1770 lttng_thread_list_shutdown_orphans();
1773 * Wait for all pending call_rcu work to complete before tearing
1774 * down data structures. call_rcu worker may be trying to
1775 * perform lookups in those structures.
1779 * sessiond_cleanup() is called when no other thread is running, except
1780 * the ht_cleanup thread, which is needed to destroy the hash tables.
1782 rcu_thread_online();
1785 if (notification_thread
) {
1786 lttng_thread_shutdown(notification_thread
);
1787 lttng_thread_put(notification_thread
);
1791 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1792 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1793 * the queue is empty before shutting down the clean-up thread.
1797 if (ht_cleanup_thread
) {
1798 lttng_thread_shutdown(ht_cleanup_thread
);
1799 lttng_thread_put(ht_cleanup_thread
);
1802 rcu_thread_offline();
1803 rcu_unregister_thread();
1805 if (rotation_thread_handle
) {
1806 rotation_thread_handle_destroy(rotation_thread_handle
);
1810 * After the rotation and timer thread have quit, we can safely destroy
1811 * the rotation_timer_queue.
1813 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1815 * The teardown of the notification system is performed after the
1816 * session daemon's teardown in order to allow it to be notified
1817 * of the active session and channels at the moment of the teardown.
1819 if (notification_thread_handle
) {
1820 notification_thread_handle_destroy(notification_thread_handle
);
1822 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1823 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1824 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1826 if (health_sessiond
) {
1827 health_app_destroy(health_sessiond
);
1829 exit_create_run_as_worker_cleanup
:
1831 sessiond_cleanup_lock_file();
1832 sessiond_cleanup_options();
1834 exit_set_signal_handler
: