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"
55 #include "lttng-ust-ctl.h"
56 #include "ust-consumer.h"
59 #include "health-sessiond.h"
60 #include "testpoint.h"
61 #include "notify-apps.h"
62 #include "agent-thread.h"
64 #include "notification-thread.h"
65 #include "notification-thread-commands.h"
66 #include "rotation-thread.h"
68 #include "ht-cleanup.h"
69 #include "sessiond-config.h"
75 #include "manage-apps.h"
76 #include "manage-kernel.h"
78 static const char *help_msg
=
79 #ifdef LTTNG_EMBED_HELP
80 #include <lttng-sessiond.8.h>
87 static int lockfile_fd
= -1;
88 static int opt_print_version
;
90 /* Set to 1 when a SIGUSR1 signal is received. */
91 static int recv_child_signal
;
93 /* Command line options */
94 static const struct option long_options
[] = {
95 { "client-sock", required_argument
, 0, 'c' },
96 { "apps-sock", required_argument
, 0, 'a' },
97 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
98 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
99 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
100 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
101 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
102 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
103 { "consumerd32-path", required_argument
, 0, '\0' },
104 { "consumerd32-libdir", required_argument
, 0, '\0' },
105 { "consumerd64-path", required_argument
, 0, '\0' },
106 { "consumerd64-libdir", required_argument
, 0, '\0' },
107 { "daemonize", no_argument
, 0, 'd' },
108 { "background", no_argument
, 0, 'b' },
109 { "sig-parent", no_argument
, 0, 'S' },
110 { "help", no_argument
, 0, 'h' },
111 { "group", required_argument
, 0, 'g' },
112 { "version", no_argument
, 0, 'V' },
113 { "quiet", no_argument
, 0, 'q' },
114 { "verbose", no_argument
, 0, 'v' },
115 { "verbose-consumer", no_argument
, 0, '\0' },
116 { "no-kernel", no_argument
, 0, '\0' },
117 { "pidfile", required_argument
, 0, 'p' },
118 { "agent-tcp-port", required_argument
, 0, '\0' },
119 { "config", required_argument
, 0, 'f' },
120 { "load", required_argument
, 0, 'l' },
121 { "kmod-probes", required_argument
, 0, '\0' },
122 { "extra-kmod-probes", required_argument
, 0, '\0' },
126 /* Command line options to ignore from configuration file */
127 static const char *config_ignore_options
[] = { "help", "version", "config" };
130 * This pipe is used to inform the thread managing application communication
131 * that a command is queued and ready to be processed.
133 static int apps_cmd_pipe
[2] = { -1, -1 };
134 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
137 * UST registration command queue. This queue is tied with a futex and uses a N
138 * wakers / 1 waiter implemented and detailed in futex.c/.h
140 * The thread_registration_apps and thread_dispatch_ust_registration uses this
141 * queue along with the wait/wake scheme. The thread_manage_apps receives down
142 * the line new application socket and monitors it for any I/O error or clean
143 * close that triggers an unregistration of the application.
145 static struct ust_cmd_queue ust_cmd_queue
;
148 * Section name to look for in the daemon configuration file.
150 static const char * const config_section_name
= "sessiond";
152 /* Am I root or not. Set to 1 if the daemon is running as root */
156 * Stop all threads by closing the thread quit pipe.
158 static void stop_threads(void)
162 /* Stopping all threads */
163 DBG("Terminating all threads");
164 ret
= sessiond_notify_quit_pipe();
166 ERR("write error on thread quit pipe");
171 * Close every consumer sockets.
173 static void close_consumer_sockets(void)
177 if (kconsumer_data
.err_sock
>= 0) {
178 ret
= close(kconsumer_data
.err_sock
);
180 PERROR("kernel consumer err_sock close");
183 if (ustconsumer32_data
.err_sock
>= 0) {
184 ret
= close(ustconsumer32_data
.err_sock
);
186 PERROR("UST consumerd32 err_sock close");
189 if (ustconsumer64_data
.err_sock
>= 0) {
190 ret
= close(ustconsumer64_data
.err_sock
);
192 PERROR("UST consumerd64 err_sock close");
195 if (kconsumer_data
.cmd_sock
>= 0) {
196 ret
= close(kconsumer_data
.cmd_sock
);
198 PERROR("kernel consumer cmd_sock close");
201 if (ustconsumer32_data
.cmd_sock
>= 0) {
202 ret
= close(ustconsumer32_data
.cmd_sock
);
204 PERROR("UST consumerd32 cmd_sock close");
207 if (ustconsumer64_data
.cmd_sock
>= 0) {
208 ret
= close(ustconsumer64_data
.cmd_sock
);
210 PERROR("UST consumerd64 cmd_sock close");
213 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
214 ret
= close(kconsumer_data
.channel_monitor_pipe
);
216 PERROR("kernel consumer channel monitor pipe close");
219 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
220 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
222 PERROR("UST consumerd32 channel monitor pipe close");
225 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
226 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
228 PERROR("UST consumerd64 channel monitor pipe close");
234 * Wait on consumer process termination.
236 * Need to be called with the consumer data lock held or from a context
237 * ensuring no concurrent access to data (e.g: cleanup).
239 static void wait_consumer(struct consumer_data
*consumer_data
)
244 if (consumer_data
->pid
<= 0) {
248 DBG("Waiting for complete teardown of consumerd (PID: %d)",
250 ret
= waitpid(consumer_data
->pid
, &status
, 0);
252 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
253 } else if (!WIFEXITED(status
)) {
254 ERR("consumerd termination with error: %d",
257 consumer_data
->pid
= 0;
261 * Cleanup the session daemon's data structures.
263 static void sessiond_cleanup(void)
266 struct ltt_session_list
*session_list
= session_get_list();
268 DBG("Cleanup sessiond");
271 * Close the thread quit pipe. It has already done its job,
272 * since we are now called.
274 sessiond_close_quit_pipe();
275 utils_close_pipe(apps_cmd_pipe
);
276 utils_close_pipe(apps_cmd_notify_pipe
);
277 utils_close_pipe(kernel_poll_pipe
);
279 ret
= remove(config
.pid_file_path
.value
);
281 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
284 DBG("Removing sessiond and consumerd content of directory %s",
285 config
.rundir
.value
);
288 DBG("Removing %s", config
.pid_file_path
.value
);
289 (void) unlink(config
.pid_file_path
.value
);
291 DBG("Removing %s", config
.agent_port_file_path
.value
);
292 (void) unlink(config
.agent_port_file_path
.value
);
295 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
296 (void) unlink(kconsumer_data
.err_unix_sock_path
);
298 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
299 (void) rmdir(config
.kconsumerd_path
.value
);
301 /* ust consumerd 32 */
302 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
303 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
305 DBG("Removing directory %s", config
.consumerd32_path
.value
);
306 (void) rmdir(config
.consumerd32_path
.value
);
308 /* ust consumerd 64 */
309 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
310 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
312 DBG("Removing directory %s", config
.consumerd64_path
.value
);
313 (void) rmdir(config
.consumerd64_path
.value
);
315 pthread_mutex_destroy(&session_list
->lock
);
317 DBG("Cleaning up all agent apps");
318 agent_app_ht_clean();
319 DBG("Closing all UST sockets");
320 ust_app_clean_list();
321 buffer_reg_destroy_registries();
323 close_consumer_sockets();
325 wait_consumer(&kconsumer_data
);
326 wait_consumer(&ustconsumer64_data
);
327 wait_consumer(&ustconsumer32_data
);
329 if (is_root
&& !config
.no_kernel
) {
330 cleanup_kernel_tracer();
334 * We do NOT rmdir rundir because there are other processes
335 * using it, for instance lttng-relayd, which can start in
336 * parallel with this teardown.
341 * Cleanup the daemon's option data structures.
343 static void sessiond_cleanup_options(void)
345 DBG("Cleaning up options");
347 sessiond_config_fini(&config
);
349 run_as_destroy_worker();
352 static int string_match(const char *str1
, const char *str2
)
354 return (str1
&& str2
) && !strcmp(str1
, str2
);
358 * Take an option from the getopt output and set it in the right variable to be
361 * Return 0 on success else a negative value.
363 static int set_option(int opt
, const char *arg
, const char *optname
)
367 if (string_match(optname
, "client-sock") || opt
== 'c') {
368 if (!arg
|| *arg
== '\0') {
372 if (lttng_is_setuid_setgid()) {
373 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
374 "-c, --client-sock");
376 config_string_set(&config
.client_unix_sock_path
,
378 if (!config
.client_unix_sock_path
.value
) {
383 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
384 if (!arg
|| *arg
== '\0') {
388 if (lttng_is_setuid_setgid()) {
389 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
392 config_string_set(&config
.apps_unix_sock_path
,
394 if (!config
.apps_unix_sock_path
.value
) {
399 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
400 config
.daemonize
= true;
401 } else if (string_match(optname
, "background") || opt
== 'b') {
402 config
.background
= true;
403 } else if (string_match(optname
, "group") || opt
== 'g') {
404 if (!arg
|| *arg
== '\0') {
408 if (lttng_is_setuid_setgid()) {
409 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
412 config_string_set(&config
.tracing_group_name
,
414 if (!config
.tracing_group_name
.value
) {
419 } else if (string_match(optname
, "help") || opt
== 'h') {
420 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
422 ERR("Cannot show --help for `lttng-sessiond`");
425 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
426 } else if (string_match(optname
, "version") || opt
== 'V') {
427 opt_print_version
= 1;
428 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
429 config
.sig_parent
= true;
430 } else if (string_match(optname
, "kconsumerd-err-sock")) {
431 if (!arg
|| *arg
== '\0') {
435 if (lttng_is_setuid_setgid()) {
436 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
437 "--kconsumerd-err-sock");
439 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
441 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
446 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
447 if (!arg
|| *arg
== '\0') {
451 if (lttng_is_setuid_setgid()) {
452 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
453 "--kconsumerd-cmd-sock");
455 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
457 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
462 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
463 if (!arg
|| *arg
== '\0') {
467 if (lttng_is_setuid_setgid()) {
468 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
469 "--ustconsumerd64-err-sock");
471 config_string_set(&config
.consumerd64_err_unix_sock_path
,
473 if (!config
.consumerd64_err_unix_sock_path
.value
) {
478 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
479 if (!arg
|| *arg
== '\0') {
483 if (lttng_is_setuid_setgid()) {
484 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
485 "--ustconsumerd64-cmd-sock");
487 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
489 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
494 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
495 if (!arg
|| *arg
== '\0') {
499 if (lttng_is_setuid_setgid()) {
500 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
501 "--ustconsumerd32-err-sock");
503 config_string_set(&config
.consumerd32_err_unix_sock_path
,
505 if (!config
.consumerd32_err_unix_sock_path
.value
) {
510 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
511 if (!arg
|| *arg
== '\0') {
515 if (lttng_is_setuid_setgid()) {
516 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
517 "--ustconsumerd32-cmd-sock");
519 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
521 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
526 } else if (string_match(optname
, "no-kernel")) {
527 config
.no_kernel
= true;
528 } else if (string_match(optname
, "quiet") || opt
== 'q') {
530 } else if (string_match(optname
, "verbose") || opt
== 'v') {
531 /* Verbose level can increase using multiple -v */
533 /* Value obtained from config file */
534 config
.verbose
= config_parse_value(arg
);
536 /* -v used on command line */
539 /* Clamp value to [0, 3] */
540 config
.verbose
= config
.verbose
< 0 ? 0 :
541 (config
.verbose
<= 3 ? config
.verbose
: 3);
542 } else if (string_match(optname
, "verbose-consumer")) {
544 config
.verbose_consumer
= config_parse_value(arg
);
546 config
.verbose_consumer
++;
548 } else if (string_match(optname
, "consumerd32-path")) {
549 if (!arg
|| *arg
== '\0') {
553 if (lttng_is_setuid_setgid()) {
554 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
555 "--consumerd32-path");
557 config_string_set(&config
.consumerd32_bin_path
,
559 if (!config
.consumerd32_bin_path
.value
) {
564 } else if (string_match(optname
, "consumerd32-libdir")) {
565 if (!arg
|| *arg
== '\0') {
569 if (lttng_is_setuid_setgid()) {
570 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
571 "--consumerd32-libdir");
573 config_string_set(&config
.consumerd32_lib_dir
,
575 if (!config
.consumerd32_lib_dir
.value
) {
580 } else if (string_match(optname
, "consumerd64-path")) {
581 if (!arg
|| *arg
== '\0') {
585 if (lttng_is_setuid_setgid()) {
586 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
587 "--consumerd64-path");
589 config_string_set(&config
.consumerd64_bin_path
,
591 if (!config
.consumerd64_bin_path
.value
) {
596 } else if (string_match(optname
, "consumerd64-libdir")) {
597 if (!arg
|| *arg
== '\0') {
601 if (lttng_is_setuid_setgid()) {
602 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
603 "--consumerd64-libdir");
605 config_string_set(&config
.consumerd64_lib_dir
,
607 if (!config
.consumerd64_lib_dir
.value
) {
612 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
613 if (!arg
|| *arg
== '\0') {
617 if (lttng_is_setuid_setgid()) {
618 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
621 config_string_set(&config
.pid_file_path
, strdup(arg
));
622 if (!config
.pid_file_path
.value
) {
627 } else if (string_match(optname
, "agent-tcp-port")) {
628 if (!arg
|| *arg
== '\0') {
632 if (lttng_is_setuid_setgid()) {
633 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
639 v
= strtoul(arg
, NULL
, 0);
640 if (errno
!= 0 || !isdigit(arg
[0])) {
641 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
644 if (v
== 0 || v
>= 65535) {
645 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
648 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
649 DBG3("Agent TCP port set to non default: %i", (int) v
);
651 } else if (string_match(optname
, "load") || opt
== 'l') {
652 if (!arg
|| *arg
== '\0') {
656 if (lttng_is_setuid_setgid()) {
657 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
660 config_string_set(&config
.load_session_path
, strdup(arg
));
661 if (!config
.load_session_path
.value
) {
666 } else if (string_match(optname
, "kmod-probes")) {
667 if (!arg
|| *arg
== '\0') {
671 if (lttng_is_setuid_setgid()) {
672 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
675 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
676 if (!config
.kmod_probes_list
.value
) {
681 } else if (string_match(optname
, "extra-kmod-probes")) {
682 if (!arg
|| *arg
== '\0') {
686 if (lttng_is_setuid_setgid()) {
687 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
688 "--extra-kmod-probes");
690 config_string_set(&config
.kmod_extra_probes_list
,
692 if (!config
.kmod_extra_probes_list
.value
) {
697 } else if (string_match(optname
, "config") || opt
== 'f') {
698 /* This is handled in set_options() thus silent skip. */
701 /* Unknown option or other error.
702 * Error is printed by getopt, just return */
707 if (ret
== -EINVAL
) {
708 const char *opt_name
= "unknown";
711 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
713 if (opt
== long_options
[i
].val
) {
714 opt_name
= long_options
[i
].name
;
719 WARN("Invalid argument provided for option \"%s\", using default value.",
727 * config_entry_handler_cb used to handle options read from a config file.
728 * See config_entry_handler_cb comment in common/config/session-config.h for the
729 * return value conventions.
731 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
735 if (!entry
|| !entry
->name
|| !entry
->value
) {
740 /* Check if the option is to be ignored */
741 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
742 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
747 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
750 /* Ignore if not fully matched. */
751 if (strcmp(entry
->name
, long_options
[i
].name
)) {
756 * If the option takes no argument on the command line, we have to
757 * check if the value is "true". We support non-zero numeric values,
760 if (!long_options
[i
].has_arg
) {
761 ret
= config_parse_value(entry
->value
);
764 WARN("Invalid configuration value \"%s\" for option %s",
765 entry
->value
, entry
->name
);
767 /* False, skip boolean config option. */
772 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
776 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
782 static void print_version(void) {
783 fprintf(stdout
, "%s\n", VERSION
);
787 * daemon configuration loading and argument parsing
789 static int set_options(int argc
, char **argv
)
791 int ret
= 0, c
= 0, option_index
= 0;
792 int orig_optopt
= optopt
, orig_optind
= optind
;
794 const char *config_path
= NULL
;
796 optstring
= utils_generate_optstring(long_options
,
797 sizeof(long_options
) / sizeof(struct option
));
803 /* Check for the --config option */
804 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
805 &option_index
)) != -1) {
809 } else if (c
!= 'f') {
810 /* if not equal to --config option. */
814 if (lttng_is_setuid_setgid()) {
815 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
818 config_path
= utils_expand_path(optarg
);
820 ERR("Failed to resolve path: %s", optarg
);
825 ret
= config_get_section_entries(config_path
, config_section_name
,
826 config_entry_handler
, NULL
);
829 ERR("Invalid configuration option at line %i", ret
);
835 /* Reset getopt's global state */
836 optopt
= orig_optopt
;
837 optind
= orig_optind
;
841 * getopt_long() will not set option_index if it encounters a
844 c
= getopt_long(argc
, argv
, optstring
, long_options
,
851 * Pass NULL as the long option name if popt left the index
854 ret
= set_option(c
, optarg
,
855 option_index
< 0 ? NULL
:
856 long_options
[option_index
].name
);
868 * Create lockfile using the rundir and return its fd.
870 static int create_lockfile(void)
872 return utils_create_lock_file(config
.lock_file_path
.value
);
876 * Check if the global socket is available, and if a daemon is answering at the
877 * other side. If yes, error is returned.
879 * Also attempts to create and hold the lock file.
881 static int check_existing_daemon(void)
885 /* Is there anybody out there ? */
886 if (lttng_session_daemon_alive()) {
891 lockfile_fd
= create_lockfile();
892 if (lockfile_fd
< 0) {
900 static void sessiond_cleanup_lock_file(void)
905 * Cleanup lock file by deleting it and finaly closing it which will
906 * release the file system lock.
908 if (lockfile_fd
>= 0) {
909 ret
= remove(config
.lock_file_path
.value
);
911 PERROR("remove lock file");
913 ret
= close(lockfile_fd
);
915 PERROR("close lock file");
921 * Set the tracing group gid onto the client socket.
923 * Race window between mkdir and chown is OK because we are going from more
924 * permissive (root.root) to less permissive (root.tracing).
926 static int set_permissions(char *rundir
)
931 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true, &gid
);
933 /* Default to root group. */
937 /* Set lttng run dir */
938 ret
= chown(rundir
, 0, gid
);
940 ERR("Unable to set group on %s", rundir
);
945 * Ensure all applications and tracing group can search the run
946 * dir. Allow everyone to read the directory, since it does not
947 * buy us anything to hide its content.
949 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
951 ERR("Unable to set permissions on %s", rundir
);
955 /* lttng client socket path */
956 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
958 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
962 /* kconsumer error socket path */
963 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
965 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
969 /* 64-bit ustconsumer error socket path */
970 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
972 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
976 /* 32-bit ustconsumer compat32 error socket path */
977 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
979 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
983 DBG("All permissions are set");
989 * Create the lttng run directory needed for all global sockets and pipe.
991 static int create_lttng_rundir(void)
995 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
997 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
999 if (errno
!= EEXIST
) {
1000 ERR("Unable to create %s", config
.rundir
.value
);
1012 * Setup sockets and directory needed by the consumerds' communication with the
1015 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1020 switch (consumer_data
->type
) {
1021 case LTTNG_CONSUMER_KERNEL
:
1022 path
= config
.kconsumerd_path
.value
;
1024 case LTTNG_CONSUMER64_UST
:
1025 path
= config
.consumerd64_path
.value
;
1027 case LTTNG_CONSUMER32_UST
:
1028 path
= config
.consumerd32_path
.value
;
1031 ERR("Consumer type unknown");
1037 DBG2("Creating consumer directory: %s", path
);
1039 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1040 if (ret
< 0 && errno
!= EEXIST
) {
1042 ERR("Failed to create %s", path
);
1048 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
1051 /* Default to root group. */
1055 ret
= chown(path
, 0, gid
);
1057 ERR("Unable to set group on %s", path
);
1063 /* Create the consumerd error unix socket */
1064 consumer_data
->err_sock
=
1065 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1066 if (consumer_data
->err_sock
< 0) {
1067 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1073 * Set the CLOEXEC flag. Return code is useless because either way, the
1076 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1078 PERROR("utils_set_fd_cloexec");
1079 /* continue anyway */
1082 /* File permission MUST be 660 */
1083 ret
= chmod(consumer_data
->err_unix_sock_path
,
1084 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1086 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1096 * Signal handler for the daemon
1098 * Simply stop all worker threads, leaving main() return gracefully after
1099 * joining all threads and calling cleanup().
1101 static void sighandler(int sig
)
1105 DBG("SIGINT caught");
1109 DBG("SIGTERM caught");
1113 CMM_STORE_SHARED(recv_child_signal
, 1);
1121 * Setup signal handler for :
1122 * SIGINT, SIGTERM, SIGPIPE
1124 static int set_signal_handler(void)
1127 struct sigaction sa
;
1130 if ((ret
= sigemptyset(&sigset
)) < 0) {
1131 PERROR("sigemptyset");
1135 sa
.sa_mask
= sigset
;
1138 sa
.sa_handler
= sighandler
;
1139 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1140 PERROR("sigaction");
1144 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1145 PERROR("sigaction");
1149 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1150 PERROR("sigaction");
1154 sa
.sa_handler
= SIG_IGN
;
1155 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1156 PERROR("sigaction");
1160 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1166 * Set open files limit to unlimited. This daemon can open a large number of
1167 * file descriptors in order to consume multiple kernel traces.
1169 static void set_ulimit(void)
1174 /* The kernel does not allow an infinite limit for open files */
1175 lim
.rlim_cur
= 65535;
1176 lim
.rlim_max
= 65535;
1178 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1180 PERROR("failed to set open files limit");
1184 static int write_pidfile(void)
1186 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1189 static int set_clock_plugin_env(void)
1192 char *env_value
= NULL
;
1194 if (!config
.lttng_ust_clock_plugin
.value
) {
1198 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1199 config
.lttng_ust_clock_plugin
.value
);
1205 ret
= putenv(env_value
);
1208 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1212 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1213 config
.lttng_ust_clock_plugin
.value
);
1218 static void destroy_all_sessions_and_wait(void)
1220 struct ltt_session
*session
, *tmp
;
1221 struct ltt_session_list
*session_list
;
1223 session_list
= session_get_list();
1224 DBG("Initiating destruction of all sessions");
1226 if (!session_list
) {
1230 session_lock_list();
1231 /* Initiate the destruction of all sessions. */
1232 cds_list_for_each_entry_safe(session
, tmp
,
1233 &session_list
->head
, list
) {
1234 if (!session_get(session
)) {
1238 session_lock(session
);
1239 if (session
->destroyed
) {
1240 goto unlock_session
;
1242 (void) cmd_stop_trace(session
);
1243 (void) cmd_destroy_session(session
, notification_thread_handle
,
1246 session_unlock(session
);
1247 session_put(session
);
1249 session_unlock_list();
1251 /* Wait for the destruction of all sessions to complete. */
1252 DBG("Waiting for the destruction of all sessions to complete");
1253 session_list_wait_empty();
1254 DBG("Destruction of all sessions completed");
1257 static int run_as_worker_post_fork_cleanup(void *data
)
1259 struct sessiond_config
*sessiond_config
= data
;
1261 sessiond_config_fini(sessiond_config
);
1265 static int launch_run_as_worker(const char *procname
)
1268 * Clean-up before forking the run-as worker. Any dynamically
1269 * allocated memory of which the worker is not aware will
1270 * be leaked as the process forks a run-as worker (and performs
1271 * no exec*()). The same would apply to any opened fd.
1273 return run_as_create_worker(procname
, run_as_worker_post_fork_cleanup
,
1277 static void sessiond_uuid_log(void)
1279 char uuid_str
[LTTNG_UUID_STR_LEN
];
1281 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
1282 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1288 int main(int argc
, char **argv
)
1290 int ret
= 0, retval
= 0;
1291 const char *env_app_timeout
;
1292 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1293 *ust64_channel_monitor_pipe
= NULL
,
1294 *kernel_channel_monitor_pipe
= NULL
;
1295 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1296 struct timer_thread_parameters timer_thread_parameters
;
1297 /* Rotation thread handle. */
1298 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1299 /* Queue of rotation jobs populated by the sessiond-timer. */
1300 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1301 struct lttng_thread
*client_thread
= NULL
;
1302 struct lttng_thread
*notification_thread
= NULL
;
1303 struct lttng_thread
*register_apps_thread
= NULL
;
1305 logger_set_thread_name("Main", false);
1306 init_kernel_workarounds();
1308 rcu_register_thread();
1310 if (set_signal_handler()) {
1312 goto exit_set_signal_handler
;
1315 if (timer_signal_init()) {
1317 goto exit_set_signal_handler
;
1320 page_size
= sysconf(_SC_PAGESIZE
);
1321 if (page_size
< 0) {
1322 PERROR("sysconf _SC_PAGESIZE");
1323 page_size
= LONG_MAX
;
1324 WARN("Fallback page size to %ld", page_size
);
1327 ret
= sessiond_config_init(&config
);
1330 goto exit_set_signal_handler
;
1334 * Init config from environment variables.
1335 * Command line option override env configuration per-doc. Do env first.
1337 sessiond_config_apply_env_config(&config
);
1340 * Parse arguments and load the daemon configuration file.
1342 * We have an exit_options exit path to free memory reserved by
1343 * set_options. This is needed because the rest of sessiond_cleanup()
1344 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1345 * depends on set_options.
1348 if (set_options(argc
, argv
)) {
1354 * Resolve all paths received as arguments, configuration option, or
1355 * through environment variable as absolute paths. This is necessary
1356 * since daemonizing causes the sessiond's current working directory
1359 ret
= sessiond_config_resolve_paths(&config
);
1365 lttng_opt_verbose
= config
.verbose
;
1366 lttng_opt_quiet
= config
.quiet
;
1367 kconsumer_data
.err_unix_sock_path
=
1368 config
.kconsumerd_err_unix_sock_path
.value
;
1369 kconsumer_data
.cmd_unix_sock_path
=
1370 config
.kconsumerd_cmd_unix_sock_path
.value
;
1371 ustconsumer32_data
.err_unix_sock_path
=
1372 config
.consumerd32_err_unix_sock_path
.value
;
1373 ustconsumer32_data
.cmd_unix_sock_path
=
1374 config
.consumerd32_cmd_unix_sock_path
.value
;
1375 ustconsumer64_data
.err_unix_sock_path
=
1376 config
.consumerd64_err_unix_sock_path
.value
;
1377 ustconsumer64_data
.cmd_unix_sock_path
=
1378 config
.consumerd64_cmd_unix_sock_path
.value
;
1379 set_clock_plugin_env();
1381 sessiond_config_log(&config
);
1382 sessiond_uuid_log();
1384 if (opt_print_version
) {
1390 if (create_lttng_rundir()) {
1395 /* Abort launch if a session daemon is already running. */
1396 if (check_existing_daemon()) {
1397 ERR("A session daemon is already running.");
1403 if (config
.daemonize
|| config
.background
) {
1406 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
1407 !config
.background
);
1414 * We are in the child. Make sure all other file descriptors are
1415 * closed, in case we are called with more opened file
1416 * descriptors than the standard ones and the lock file.
1418 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1419 if (i
== lockfile_fd
) {
1426 if (launch_run_as_worker(argv
[0]) < 0) {
1427 goto exit_create_run_as_worker_cleanup
;
1431 * Starting from here, we can create threads. This needs to be after
1432 * lttng_daemonize due to RCU.
1436 * Initialize the health check subsystem. This call should set the
1437 * appropriate time values.
1439 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1440 if (!health_sessiond
) {
1441 PERROR("health_app_create error");
1446 /* Create thread to clean up RCU hash tables */
1447 ht_cleanup_thread
= launch_ht_cleanup_thread();
1448 if (!ht_cleanup_thread
) {
1453 /* Create thread quit pipe */
1454 if (sessiond_init_thread_quit_pipe()) {
1459 /* Check if daemon is UID = 0 */
1460 is_root
= !getuid();
1462 /* Create global run dir with root access */
1464 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1465 if (!kernel_channel_monitor_pipe
) {
1466 ERR("Failed to create kernel consumer channel monitor pipe");
1470 kconsumer_data
.channel_monitor_pipe
=
1471 lttng_pipe_release_writefd(
1472 kernel_channel_monitor_pipe
);
1473 if (kconsumer_data
.channel_monitor_pipe
< 0) {
1479 /* Set consumer initial state */
1480 kernel_consumerd_state
= CONSUMER_STOPPED
;
1481 ust_consumerd_state
= CONSUMER_STOPPED
;
1483 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1484 if (!ust32_channel_monitor_pipe
) {
1485 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1489 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1490 ust32_channel_monitor_pipe
);
1491 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
1497 * The rotation_thread_timer_queue structure is shared between the
1498 * sessiond timer thread and the rotation thread. The main thread keeps
1499 * its ownership and destroys it when both threads have been joined.
1501 rotation_timer_queue
= rotation_thread_timer_queue_create();
1502 if (!rotation_timer_queue
) {
1506 timer_thread_parameters
.rotation_thread_job_queue
=
1507 rotation_timer_queue
;
1509 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1510 if (!ust64_channel_monitor_pipe
) {
1511 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1515 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1516 ust64_channel_monitor_pipe
);
1517 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
1523 * Init UST app hash table. Alloc hash table before this point since
1524 * cleanup() can get called after that point.
1526 if (ust_app_ht_alloc()) {
1527 ERR("Failed to allocate UST app hash table");
1533 * Initialize agent app hash table. We allocate the hash table here
1534 * since cleanup() can get called after this point.
1536 if (agent_app_ht_alloc()) {
1537 ERR("Failed to allocate Agent app hash table");
1543 * These actions must be executed as root. We do that *after* setting up
1544 * the sockets path because we MUST make the check for another daemon using
1545 * those paths *before* trying to set the kernel consumer sockets and init
1549 if (set_consumer_sockets(&kconsumer_data
)) {
1554 /* Setup kernel tracer */
1555 if (!config
.no_kernel
) {
1556 init_kernel_tracer();
1559 /* Set ulimit for open files */
1562 /* init lttng_fd tracking must be done after set_ulimit. */
1565 if (set_consumer_sockets(&ustconsumer64_data
)) {
1570 if (set_consumer_sockets(&ustconsumer32_data
)) {
1575 /* Get parent pid if -S, --sig-parent is specified. */
1576 if (config
.sig_parent
) {
1580 /* Setup the kernel pipe for waking up the kernel thread */
1581 if (is_root
&& !config
.no_kernel
) {
1582 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
1588 /* Setup the thread apps communication pipe. */
1589 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1594 /* Setup the thread apps notify communication pipe. */
1595 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1600 /* Initialize global buffer per UID and PID registry. */
1601 buffer_reg_init_uid_registry();
1602 buffer_reg_init_pid_registry();
1604 /* Init UST command queue. */
1605 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1609 /* Check for the application socket timeout env variable. */
1610 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1611 if (env_app_timeout
) {
1612 config
.app_socket_timeout
= atoi(env_app_timeout
);
1614 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1617 ret
= write_pidfile();
1619 ERR("Error in write_pidfile");
1624 /* Initialize communication library */
1626 /* Initialize TCP timeout values */
1627 lttcomm_inet_init();
1629 /* Create health-check thread. */
1630 if (!launch_health_management_thread()) {
1635 /* notification_thread_data acquires the pipes' read side. */
1636 notification_thread_handle
= notification_thread_handle_create(
1637 ust32_channel_monitor_pipe
,
1638 ust64_channel_monitor_pipe
,
1639 kernel_channel_monitor_pipe
);
1640 if (!notification_thread_handle
) {
1642 ERR("Failed to create notification thread shared data");
1646 /* Create notification thread. */
1647 notification_thread
= launch_notification_thread(
1648 notification_thread_handle
);
1649 if (!notification_thread
) {
1654 /* Create timer thread. */
1655 if (!launch_timer_thread(&timer_thread_parameters
)) {
1660 /* rotation_thread_data acquires the pipes' read side. */
1661 rotation_thread_handle
= rotation_thread_handle_create(
1662 rotation_timer_queue
,
1663 notification_thread_handle
);
1664 if (!rotation_thread_handle
) {
1666 ERR("Failed to create rotation thread shared data");
1671 /* Create rotation thread. */
1672 if (!launch_rotation_thread(rotation_thread_handle
)) {
1677 /* Create thread to manage the client socket */
1678 client_thread
= launch_client_thread();
1679 if (!client_thread
) {
1684 /* Set credentials of the client socket and rundir */
1685 if (is_root
&& set_permissions(config
.rundir
.value
)) {
1690 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1691 apps_cmd_notify_pipe
[1])) {
1696 /* Create thread to manage application registration. */
1697 register_apps_thread
= launch_application_registration_thread(
1699 if (!register_apps_thread
) {
1704 /* Create thread to manage application socket */
1705 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1710 /* Create thread to manage application notify socket */
1711 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1716 /* Create agent management thread. */
1717 if (!launch_agent_management_thread()) {
1722 /* Don't start this thread if kernel tracing is not requested nor root */
1723 if (is_root
&& !config
.no_kernel
) {
1724 /* Create kernel thread to manage kernel event */
1725 if (!launch_kernel_management_thread(kernel_poll_pipe
[0])) {
1731 /* Load sessions. */
1732 ret
= config_load_session(config
.load_session_path
.value
,
1735 ERR("Session load failed: %s", error_get_str(ret
));
1740 /* Initialization completed. */
1741 sessiond_signal_parents();
1744 * This is where we start awaiting program completion (e.g. through
1745 * signal that asks threads to teardown).
1748 /* Initiate teardown once activity occurs on the quit pipe. */
1749 sessiond_wait_for_quit_pipe(-1);
1753 * Ensure that the client thread is no longer accepting new commands,
1754 * which could cause new sessions to be created.
1756 if (client_thread
) {
1757 lttng_thread_shutdown(client_thread
);
1758 lttng_thread_put(client_thread
);
1761 destroy_all_sessions_and_wait();
1763 if (register_apps_thread
) {
1764 lttng_thread_shutdown(register_apps_thread
);
1765 lttng_thread_put(register_apps_thread
);
1767 lttng_thread_list_shutdown_orphans();
1770 * Wait for all pending call_rcu work to complete before tearing
1771 * down data structures. call_rcu worker may be trying to
1772 * perform lookups in those structures.
1776 * sessiond_cleanup() is called when no other thread is running, except
1777 * the ht_cleanup thread, which is needed to destroy the hash tables.
1779 rcu_thread_online();
1782 if (notification_thread
) {
1783 lttng_thread_shutdown(notification_thread
);
1784 lttng_thread_put(notification_thread
);
1788 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1789 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1790 * the queue is empty before shutting down the clean-up thread.
1794 if (ht_cleanup_thread
) {
1795 lttng_thread_shutdown(ht_cleanup_thread
);
1796 lttng_thread_put(ht_cleanup_thread
);
1799 rcu_thread_offline();
1800 rcu_unregister_thread();
1802 if (rotation_thread_handle
) {
1803 rotation_thread_handle_destroy(rotation_thread_handle
);
1807 * After the rotation and timer thread have quit, we can safely destroy
1808 * the rotation_timer_queue.
1810 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1812 * The teardown of the notification system is performed after the
1813 * session daemon's teardown in order to allow it to be notified
1814 * of the active session and channels at the moment of the teardown.
1816 if (notification_thread_handle
) {
1817 notification_thread_handle_destroy(notification_thread_handle
);
1819 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1820 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1821 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1823 if (health_sessiond
) {
1824 health_app_destroy(health_sessiond
);
1826 exit_create_run_as_worker_cleanup
:
1828 sessiond_cleanup_lock_file();
1829 sessiond_cleanup_options();
1831 exit_set_signal_handler
: