2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
52 #include <common/dynamic-buffer.h>
53 #include <lttng/event-internal.h>
55 #include "lttng-sessiond.h"
56 #include "buffer-registry.h"
63 #include "kernel-consumer.h"
67 #include "ust-consumer.h"
70 #include "health-sessiond.h"
71 #include "testpoint.h"
72 #include "notify-apps.h"
73 #include "agent-thread.h"
75 #include "notification-thread.h"
76 #include "notification-thread-commands.h"
77 #include "rotation-thread.h"
78 #include "lttng-syscall.h"
80 #include "ht-cleanup.h"
81 #include "sessiond-config.h"
87 #include "manage-apps.h"
88 #include "manage-kernel.h"
90 static const char *help_msg
=
91 #ifdef LTTNG_EMBED_HELP
92 #include <lttng-sessiond.8.h>
99 static int lockfile_fd
= -1;
101 /* Set to 1 when a SIGUSR1 signal is received. */
102 static int recv_child_signal
;
104 /* Command line options */
105 static const struct option long_options
[] = {
106 { "client-sock", required_argument
, 0, 'c' },
107 { "apps-sock", required_argument
, 0, 'a' },
108 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
109 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
110 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
111 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
112 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
113 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
114 { "consumerd32-path", required_argument
, 0, '\0' },
115 { "consumerd32-libdir", required_argument
, 0, '\0' },
116 { "consumerd64-path", required_argument
, 0, '\0' },
117 { "consumerd64-libdir", required_argument
, 0, '\0' },
118 { "daemonize", no_argument
, 0, 'd' },
119 { "background", no_argument
, 0, 'b' },
120 { "sig-parent", no_argument
, 0, 'S' },
121 { "help", no_argument
, 0, 'h' },
122 { "group", required_argument
, 0, 'g' },
123 { "version", no_argument
, 0, 'V' },
124 { "quiet", no_argument
, 0, 'q' },
125 { "verbose", no_argument
, 0, 'v' },
126 { "verbose-consumer", no_argument
, 0, '\0' },
127 { "no-kernel", no_argument
, 0, '\0' },
128 { "pidfile", required_argument
, 0, 'p' },
129 { "agent-tcp-port", required_argument
, 0, '\0' },
130 { "config", required_argument
, 0, 'f' },
131 { "load", required_argument
, 0, 'l' },
132 { "kmod-probes", required_argument
, 0, '\0' },
133 { "extra-kmod-probes", required_argument
, 0, '\0' },
137 /* Command line options to ignore from configuration file */
138 static const char *config_ignore_options
[] = { "help", "version", "config" };
141 * This pipe is used to inform the thread managing application communication
142 * that a command is queued and ready to be processed.
144 static int apps_cmd_pipe
[2] = { -1, -1 };
145 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
148 * UST registration command queue. This queue is tied with a futex and uses a N
149 * wakers / 1 waiter implemented and detailed in futex.c/.h
151 * The thread_registration_apps and thread_dispatch_ust_registration uses this
152 * queue along with the wait/wake scheme. The thread_manage_apps receives down
153 * the line new application socket and monitors it for any I/O error or clean
154 * close that triggers an unregistration of the application.
156 static struct ust_cmd_queue ust_cmd_queue
;
158 static const char *module_proc_lttng
= "/proc/lttng";
161 * Section name to look for in the daemon configuration file.
163 static const char * const config_section_name
= "sessiond";
165 /* Am I root or not. Set to 1 if the daemon is running as root */
169 * Stop all threads by closing the thread quit pipe.
171 static void stop_threads(void)
175 /* Stopping all threads */
176 DBG("Terminating all threads");
177 ret
= sessiond_notify_quit_pipe();
179 ERR("write error on thread quit pipe");
184 * Close every consumer sockets.
186 static void close_consumer_sockets(void)
190 if (kconsumer_data
.err_sock
>= 0) {
191 ret
= close(kconsumer_data
.err_sock
);
193 PERROR("kernel consumer err_sock close");
196 if (ustconsumer32_data
.err_sock
>= 0) {
197 ret
= close(ustconsumer32_data
.err_sock
);
199 PERROR("UST consumerd32 err_sock close");
202 if (ustconsumer64_data
.err_sock
>= 0) {
203 ret
= close(ustconsumer64_data
.err_sock
);
205 PERROR("UST consumerd64 err_sock close");
208 if (kconsumer_data
.cmd_sock
>= 0) {
209 ret
= close(kconsumer_data
.cmd_sock
);
211 PERROR("kernel consumer cmd_sock close");
214 if (ustconsumer32_data
.cmd_sock
>= 0) {
215 ret
= close(ustconsumer32_data
.cmd_sock
);
217 PERROR("UST consumerd32 cmd_sock close");
220 if (ustconsumer64_data
.cmd_sock
>= 0) {
221 ret
= close(ustconsumer64_data
.cmd_sock
);
223 PERROR("UST consumerd64 cmd_sock close");
226 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
227 ret
= close(kconsumer_data
.channel_monitor_pipe
);
229 PERROR("kernel consumer channel monitor pipe close");
232 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
233 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
235 PERROR("UST consumerd32 channel monitor pipe close");
238 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
239 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
241 PERROR("UST consumerd64 channel monitor pipe close");
247 * Wait on consumer process termination.
249 * Need to be called with the consumer data lock held or from a context
250 * ensuring no concurrent access to data (e.g: cleanup).
252 static void wait_consumer(struct consumer_data
*consumer_data
)
257 if (consumer_data
->pid
<= 0) {
261 DBG("Waiting for complete teardown of consumerd (PID: %d)",
263 ret
= waitpid(consumer_data
->pid
, &status
, 0);
265 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
266 } else if (!WIFEXITED(status
)) {
267 ERR("consumerd termination with error: %d",
270 consumer_data
->pid
= 0;
274 * Cleanup the session daemon's data structures.
276 static void sessiond_cleanup(void)
279 struct ltt_session_list
*session_list
= session_get_list();
281 DBG("Cleanup sessiond");
284 * Close the thread quit pipe. It has already done its job,
285 * since we are now called.
287 sessiond_close_quit_pipe();
288 utils_close_pipe(apps_cmd_pipe
);
289 utils_close_pipe(apps_cmd_notify_pipe
);
290 utils_close_pipe(kernel_poll_pipe
);
292 ret
= remove(config
.pid_file_path
.value
);
294 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
297 DBG("Removing sessiond and consumerd content of directory %s",
298 config
.rundir
.value
);
301 DBG("Removing %s", config
.pid_file_path
.value
);
302 (void) unlink(config
.pid_file_path
.value
);
304 DBG("Removing %s", config
.agent_port_file_path
.value
);
305 (void) unlink(config
.agent_port_file_path
.value
);
308 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
309 (void) unlink(kconsumer_data
.err_unix_sock_path
);
311 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
312 (void) rmdir(config
.kconsumerd_path
.value
);
314 /* ust consumerd 32 */
315 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
316 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
318 DBG("Removing directory %s", config
.consumerd32_path
.value
);
319 (void) rmdir(config
.consumerd32_path
.value
);
321 /* ust consumerd 64 */
322 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
323 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
325 DBG("Removing directory %s", config
.consumerd64_path
.value
);
326 (void) rmdir(config
.consumerd64_path
.value
);
328 pthread_mutex_destroy(&session_list
->lock
);
330 DBG("Cleaning up all agent apps");
331 agent_app_ht_clean();
332 DBG("Closing all UST sockets");
333 ust_app_clean_list();
334 buffer_reg_destroy_registries();
336 close_consumer_sockets();
338 wait_consumer(&kconsumer_data
);
339 wait_consumer(&ustconsumer64_data
);
340 wait_consumer(&ustconsumer32_data
);
342 if (is_root
&& !config
.no_kernel
) {
343 DBG2("Closing kernel fd");
344 if (kernel_tracer_fd
>= 0) {
345 ret
= close(kernel_tracer_fd
);
350 DBG("Unloading kernel modules");
351 modprobe_remove_lttng_all();
356 * We do NOT rmdir rundir because there are other processes
357 * using it, for instance lttng-relayd, which can start in
358 * parallel with this teardown.
363 * Cleanup the daemon's option data structures.
365 static void sessiond_cleanup_options(void)
367 DBG("Cleaning up options");
369 sessiond_config_fini(&config
);
371 run_as_destroy_worker();
375 * Setup necessary data for kernel tracer action.
377 static int init_kernel_tracer(void)
381 /* Modprobe lttng kernel modules */
382 ret
= modprobe_lttng_control();
387 /* Open debugfs lttng */
388 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
389 if (kernel_tracer_fd
< 0) {
390 DBG("Failed to open %s", module_proc_lttng
);
394 /* Validate kernel version */
395 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
396 &kernel_tracer_abi_version
);
401 ret
= modprobe_lttng_data();
406 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
413 WARN("Kernel tracer does not support buffer monitoring. "
414 "The monitoring timer of channels in the kernel domain "
415 "will be set to 0 (disabled).");
418 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
422 modprobe_remove_lttng_control();
423 ret
= close(kernel_tracer_fd
);
427 kernel_tracer_fd
= -1;
428 return LTTNG_ERR_KERN_VERSION
;
431 ret
= close(kernel_tracer_fd
);
437 modprobe_remove_lttng_control();
440 WARN("No kernel tracer available");
441 kernel_tracer_fd
= -1;
443 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
445 return LTTNG_ERR_KERN_NA
;
449 static int string_match(const char *str1
, const char *str2
)
451 return (str1
&& str2
) && !strcmp(str1
, str2
);
455 * Take an option from the getopt output and set it in the right variable to be
458 * Return 0 on success else a negative value.
460 static int set_option(int opt
, const char *arg
, const char *optname
)
464 if (string_match(optname
, "client-sock") || opt
== 'c') {
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 "-c, --client-sock");
473 config_string_set(&config
.client_unix_sock_path
,
475 if (!config
.client_unix_sock_path
.value
) {
480 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
481 if (!arg
|| *arg
== '\0') {
485 if (lttng_is_setuid_setgid()) {
486 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
489 config_string_set(&config
.apps_unix_sock_path
,
491 if (!config
.apps_unix_sock_path
.value
) {
496 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
497 config
.daemonize
= true;
498 } else if (string_match(optname
, "background") || opt
== 'b') {
499 config
.background
= true;
500 } else if (string_match(optname
, "group") || opt
== 'g') {
501 if (!arg
|| *arg
== '\0') {
505 if (lttng_is_setuid_setgid()) {
506 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
509 config_string_set(&config
.tracing_group_name
,
511 if (!config
.tracing_group_name
.value
) {
516 } else if (string_match(optname
, "help") || opt
== 'h') {
517 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
519 ERR("Cannot show --help for `lttng-sessiond`");
522 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
523 } else if (string_match(optname
, "version") || opt
== 'V') {
524 fprintf(stdout
, "%s\n", VERSION
);
526 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
527 config
.sig_parent
= true;
528 } else if (string_match(optname
, "kconsumerd-err-sock")) {
529 if (!arg
|| *arg
== '\0') {
533 if (lttng_is_setuid_setgid()) {
534 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
535 "--kconsumerd-err-sock");
537 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
539 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
544 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
545 if (!arg
|| *arg
== '\0') {
549 if (lttng_is_setuid_setgid()) {
550 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
551 "--kconsumerd-cmd-sock");
553 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
555 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
560 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
561 if (!arg
|| *arg
== '\0') {
565 if (lttng_is_setuid_setgid()) {
566 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
567 "--ustconsumerd64-err-sock");
569 config_string_set(&config
.consumerd64_err_unix_sock_path
,
571 if (!config
.consumerd64_err_unix_sock_path
.value
) {
576 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
577 if (!arg
|| *arg
== '\0') {
581 if (lttng_is_setuid_setgid()) {
582 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
583 "--ustconsumerd64-cmd-sock");
585 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
587 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
592 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
593 if (!arg
|| *arg
== '\0') {
597 if (lttng_is_setuid_setgid()) {
598 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
599 "--ustconsumerd32-err-sock");
601 config_string_set(&config
.consumerd32_err_unix_sock_path
,
603 if (!config
.consumerd32_err_unix_sock_path
.value
) {
608 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
609 if (!arg
|| *arg
== '\0') {
613 if (lttng_is_setuid_setgid()) {
614 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
615 "--ustconsumerd32-cmd-sock");
617 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
619 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
624 } else if (string_match(optname
, "no-kernel")) {
625 config
.no_kernel
= true;
626 } else if (string_match(optname
, "quiet") || opt
== 'q') {
628 } else if (string_match(optname
, "verbose") || opt
== 'v') {
629 /* Verbose level can increase using multiple -v */
631 /* Value obtained from config file */
632 config
.verbose
= config_parse_value(arg
);
634 /* -v used on command line */
637 /* Clamp value to [0, 3] */
638 config
.verbose
= config
.verbose
< 0 ? 0 :
639 (config
.verbose
<= 3 ? config
.verbose
: 3);
640 } else if (string_match(optname
, "verbose-consumer")) {
642 config
.verbose_consumer
= config_parse_value(arg
);
644 config
.verbose_consumer
++;
646 } else if (string_match(optname
, "consumerd32-path")) {
647 if (!arg
|| *arg
== '\0') {
651 if (lttng_is_setuid_setgid()) {
652 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
653 "--consumerd32-path");
655 config_string_set(&config
.consumerd32_bin_path
,
657 if (!config
.consumerd32_bin_path
.value
) {
662 } else if (string_match(optname
, "consumerd32-libdir")) {
663 if (!arg
|| *arg
== '\0') {
667 if (lttng_is_setuid_setgid()) {
668 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
669 "--consumerd32-libdir");
671 config_string_set(&config
.consumerd32_lib_dir
,
673 if (!config
.consumerd32_lib_dir
.value
) {
678 } else if (string_match(optname
, "consumerd64-path")) {
679 if (!arg
|| *arg
== '\0') {
683 if (lttng_is_setuid_setgid()) {
684 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
685 "--consumerd64-path");
687 config_string_set(&config
.consumerd64_bin_path
,
689 if (!config
.consumerd64_bin_path
.value
) {
694 } else if (string_match(optname
, "consumerd64-libdir")) {
695 if (!arg
|| *arg
== '\0') {
699 if (lttng_is_setuid_setgid()) {
700 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
701 "--consumerd64-libdir");
703 config_string_set(&config
.consumerd64_lib_dir
,
705 if (!config
.consumerd64_lib_dir
.value
) {
710 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
711 if (!arg
|| *arg
== '\0') {
715 if (lttng_is_setuid_setgid()) {
716 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
719 config_string_set(&config
.pid_file_path
, strdup(arg
));
720 if (!config
.pid_file_path
.value
) {
725 } else if (string_match(optname
, "agent-tcp-port")) {
726 if (!arg
|| *arg
== '\0') {
730 if (lttng_is_setuid_setgid()) {
731 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
737 v
= strtoul(arg
, NULL
, 0);
738 if (errno
!= 0 || !isdigit(arg
[0])) {
739 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
742 if (v
== 0 || v
>= 65535) {
743 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
746 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
747 DBG3("Agent TCP port set to non default: %i", (int) v
);
749 } else if (string_match(optname
, "load") || opt
== 'l') {
750 if (!arg
|| *arg
== '\0') {
754 if (lttng_is_setuid_setgid()) {
755 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
758 config_string_set(&config
.load_session_path
, strdup(arg
));
759 if (!config
.load_session_path
.value
) {
764 } else if (string_match(optname
, "kmod-probes")) {
765 if (!arg
|| *arg
== '\0') {
769 if (lttng_is_setuid_setgid()) {
770 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
773 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
774 if (!config
.kmod_probes_list
.value
) {
779 } else if (string_match(optname
, "extra-kmod-probes")) {
780 if (!arg
|| *arg
== '\0') {
784 if (lttng_is_setuid_setgid()) {
785 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
786 "--extra-kmod-probes");
788 config_string_set(&config
.kmod_extra_probes_list
,
790 if (!config
.kmod_extra_probes_list
.value
) {
795 } else if (string_match(optname
, "config") || opt
== 'f') {
796 /* This is handled in set_options() thus silent skip. */
799 /* Unknown option or other error.
800 * Error is printed by getopt, just return */
805 if (ret
== -EINVAL
) {
806 const char *opt_name
= "unknown";
809 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
811 if (opt
== long_options
[i
].val
) {
812 opt_name
= long_options
[i
].name
;
817 WARN("Invalid argument provided for option \"%s\", using default value.",
825 * config_entry_handler_cb used to handle options read from a config file.
826 * See config_entry_handler_cb comment in common/config/session-config.h for the
827 * return value conventions.
829 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
833 if (!entry
|| !entry
->name
|| !entry
->value
) {
838 /* Check if the option is to be ignored */
839 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
840 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
845 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
848 /* Ignore if not fully matched. */
849 if (strcmp(entry
->name
, long_options
[i
].name
)) {
854 * If the option takes no argument on the command line, we have to
855 * check if the value is "true". We support non-zero numeric values,
858 if (!long_options
[i
].has_arg
) {
859 ret
= config_parse_value(entry
->value
);
862 WARN("Invalid configuration value \"%s\" for option %s",
863 entry
->value
, entry
->name
);
865 /* False, skip boolean config option. */
870 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
874 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
881 * daemon configuration loading and argument parsing
883 static int set_options(int argc
, char **argv
)
885 int ret
= 0, c
= 0, option_index
= 0;
886 int orig_optopt
= optopt
, orig_optind
= optind
;
888 const char *config_path
= NULL
;
890 optstring
= utils_generate_optstring(long_options
,
891 sizeof(long_options
) / sizeof(struct option
));
897 /* Check for the --config option */
898 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
899 &option_index
)) != -1) {
903 } else if (c
!= 'f') {
904 /* if not equal to --config option. */
908 if (lttng_is_setuid_setgid()) {
909 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
912 config_path
= utils_expand_path(optarg
);
914 ERR("Failed to resolve path: %s", optarg
);
919 ret
= config_get_section_entries(config_path
, config_section_name
,
920 config_entry_handler
, NULL
);
923 ERR("Invalid configuration option at line %i", ret
);
929 /* Reset getopt's global state */
930 optopt
= orig_optopt
;
931 optind
= orig_optind
;
935 * getopt_long() will not set option_index if it encounters a
938 c
= getopt_long(argc
, argv
, optstring
, long_options
,
945 * Pass NULL as the long option name if popt left the index
948 ret
= set_option(c
, optarg
,
949 option_index
< 0 ? NULL
:
950 long_options
[option_index
].name
);
962 * Create lockfile using the rundir and return its fd.
964 static int create_lockfile(void)
966 return utils_create_lock_file(config
.lock_file_path
.value
);
970 * Check if the global socket is available, and if a daemon is answering at the
971 * other side. If yes, error is returned.
973 * Also attempts to create and hold the lock file.
975 static int check_existing_daemon(void)
979 /* Is there anybody out there ? */
980 if (lttng_session_daemon_alive()) {
985 lockfile_fd
= create_lockfile();
986 if (lockfile_fd
< 0) {
994 static void sessiond_cleanup_lock_file(void)
999 * Cleanup lock file by deleting it and finaly closing it which will
1000 * release the file system lock.
1002 if (lockfile_fd
>= 0) {
1003 ret
= remove(config
.lock_file_path
.value
);
1005 PERROR("remove lock file");
1007 ret
= close(lockfile_fd
);
1009 PERROR("close lock file");
1015 * Set the tracing group gid onto the client socket.
1017 * Race window between mkdir and chown is OK because we are going from more
1018 * permissive (root.root) to less permissive (root.tracing).
1020 static int set_permissions(char *rundir
)
1025 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true, &gid
);
1027 /* Default to root group. */
1031 /* Set lttng run dir */
1032 ret
= chown(rundir
, 0, gid
);
1034 ERR("Unable to set group on %s", rundir
);
1039 * Ensure all applications and tracing group can search the run
1040 * dir. Allow everyone to read the directory, since it does not
1041 * buy us anything to hide its content.
1043 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1045 ERR("Unable to set permissions on %s", rundir
);
1049 /* lttng client socket path */
1050 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
1052 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
1056 /* kconsumer error socket path */
1057 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
1059 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
1063 /* 64-bit ustconsumer error socket path */
1064 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1066 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
1070 /* 32-bit ustconsumer compat32 error socket path */
1071 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1073 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
1077 DBG("All permissions are set");
1083 * Create the lttng run directory needed for all global sockets and pipe.
1085 static int create_lttng_rundir(void)
1089 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
1091 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
1093 if (errno
!= EEXIST
) {
1094 ERR("Unable to create %s", config
.rundir
.value
);
1106 * Setup sockets and directory needed by the consumerds' communication with the
1109 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1114 switch (consumer_data
->type
) {
1115 case LTTNG_CONSUMER_KERNEL
:
1116 path
= config
.kconsumerd_path
.value
;
1118 case LTTNG_CONSUMER64_UST
:
1119 path
= config
.consumerd64_path
.value
;
1121 case LTTNG_CONSUMER32_UST
:
1122 path
= config
.consumerd32_path
.value
;
1125 ERR("Consumer type unknown");
1131 DBG2("Creating consumer directory: %s", path
);
1133 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1134 if (ret
< 0 && errno
!= EEXIST
) {
1136 ERR("Failed to create %s", path
);
1142 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
1145 /* Default to root group. */
1149 ret
= chown(path
, 0, gid
);
1151 ERR("Unable to set group on %s", path
);
1157 /* Create the consumerd error unix socket */
1158 consumer_data
->err_sock
=
1159 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1160 if (consumer_data
->err_sock
< 0) {
1161 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1167 * Set the CLOEXEC flag. Return code is useless because either way, the
1170 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1172 PERROR("utils_set_fd_cloexec");
1173 /* continue anyway */
1176 /* File permission MUST be 660 */
1177 ret
= chmod(consumer_data
->err_unix_sock_path
,
1178 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1180 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1190 * Signal handler for the daemon
1192 * Simply stop all worker threads, leaving main() return gracefully after
1193 * joining all threads and calling cleanup().
1195 static void sighandler(int sig
)
1199 DBG("SIGINT caught");
1203 DBG("SIGTERM caught");
1207 CMM_STORE_SHARED(recv_child_signal
, 1);
1215 * Setup signal handler for :
1216 * SIGINT, SIGTERM, SIGPIPE
1218 static int set_signal_handler(void)
1221 struct sigaction sa
;
1224 if ((ret
= sigemptyset(&sigset
)) < 0) {
1225 PERROR("sigemptyset");
1229 sa
.sa_mask
= sigset
;
1232 sa
.sa_handler
= sighandler
;
1233 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1234 PERROR("sigaction");
1238 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1239 PERROR("sigaction");
1243 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1244 PERROR("sigaction");
1248 sa
.sa_handler
= SIG_IGN
;
1249 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1250 PERROR("sigaction");
1254 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1260 * Set open files limit to unlimited. This daemon can open a large number of
1261 * file descriptors in order to consume multiple kernel traces.
1263 static void set_ulimit(void)
1268 /* The kernel does not allow an infinite limit for open files */
1269 lim
.rlim_cur
= 65535;
1270 lim
.rlim_max
= 65535;
1272 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1274 PERROR("failed to set open files limit");
1278 static int write_pidfile(void)
1280 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1283 static int set_clock_plugin_env(void)
1286 char *env_value
= NULL
;
1288 if (!config
.lttng_ust_clock_plugin
.value
) {
1292 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1293 config
.lttng_ust_clock_plugin
.value
);
1299 ret
= putenv(env_value
);
1302 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1306 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1307 config
.lttng_ust_clock_plugin
.value
);
1312 static void destroy_all_sessions_and_wait(void)
1314 struct ltt_session
*session
, *tmp
;
1315 struct ltt_session_list
*session_list
;
1317 session_list
= session_get_list();
1318 DBG("Initiating destruction of all sessions");
1320 if (!session_list
) {
1324 session_lock_list();
1325 /* Initiate the destruction of all sessions. */
1326 cds_list_for_each_entry_safe(session
, tmp
,
1327 &session_list
->head
, list
) {
1328 if (!session_get(session
)) {
1332 session_lock(session
);
1333 if (session
->destroyed
) {
1334 goto unlock_session
;
1336 (void) cmd_stop_trace(session
);
1337 (void) cmd_destroy_session(session
, notification_thread_handle
,
1340 session_unlock(session
);
1341 session_put(session
);
1343 session_unlock_list();
1345 /* Wait for the destruction of all sessions to complete. */
1346 DBG("Waiting for the destruction of all sessions to complete");
1347 session_list_wait_empty();
1348 DBG("Destruction of all sessions completed");
1351 static int run_as_worker_post_fork_cleanup(void *data
)
1353 struct sessiond_config
*sessiond_config
= data
;
1355 sessiond_config_fini(sessiond_config
);
1359 static int launch_run_as_worker(const char *procname
)
1362 * Clean-up before forking the run-as worker. Any dynamically
1363 * allocated memory of which the worker is not aware will
1364 * be leaked as the process forks a run-as worker (and performs
1365 * no exec*()). The same would apply to any opened fd.
1367 return run_as_create_worker(procname
, run_as_worker_post_fork_cleanup
,
1371 static void sessiond_uuid_log(void)
1373 char uuid_str
[UUID_STR_LEN
];
1375 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
1376 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1382 int main(int argc
, char **argv
)
1384 int ret
= 0, retval
= 0;
1385 const char *env_app_timeout
;
1386 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1387 *ust64_channel_monitor_pipe
= NULL
,
1388 *kernel_channel_monitor_pipe
= NULL
;
1389 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1390 struct timer_thread_parameters timer_thread_parameters
;
1391 /* Rotation thread handle. */
1392 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1393 /* Queue of rotation jobs populated by the sessiond-timer. */
1394 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1395 struct lttng_thread
*client_thread
= NULL
;
1396 struct lttng_thread
*notification_thread
= NULL
;
1397 struct lttng_thread
*register_apps_thread
= NULL
;
1399 init_kernel_workarounds();
1401 rcu_register_thread();
1403 if (set_signal_handler()) {
1405 goto exit_set_signal_handler
;
1408 if (timer_signal_init()) {
1410 goto exit_set_signal_handler
;
1413 page_size
= sysconf(_SC_PAGESIZE
);
1414 if (page_size
< 0) {
1415 PERROR("sysconf _SC_PAGESIZE");
1416 page_size
= LONG_MAX
;
1417 WARN("Fallback page size to %ld", page_size
);
1420 ret
= sessiond_config_init(&config
);
1423 goto exit_set_signal_handler
;
1427 * Init config from environment variables.
1428 * Command line option override env configuration per-doc. Do env first.
1430 sessiond_config_apply_env_config(&config
);
1433 * Parse arguments and load the daemon configuration file.
1435 * We have an exit_options exit path to free memory reserved by
1436 * set_options. This is needed because the rest of sessiond_cleanup()
1437 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1438 * depends on set_options.
1441 if (set_options(argc
, argv
)) {
1447 * Resolve all paths received as arguments, configuration option, or
1448 * through environment variable as absolute paths. This is necessary
1449 * since daemonizing causes the sessiond's current working directory
1452 ret
= sessiond_config_resolve_paths(&config
);
1458 lttng_opt_verbose
= config
.verbose
;
1459 lttng_opt_quiet
= config
.quiet
;
1460 kconsumer_data
.err_unix_sock_path
=
1461 config
.kconsumerd_err_unix_sock_path
.value
;
1462 kconsumer_data
.cmd_unix_sock_path
=
1463 config
.kconsumerd_cmd_unix_sock_path
.value
;
1464 ustconsumer32_data
.err_unix_sock_path
=
1465 config
.consumerd32_err_unix_sock_path
.value
;
1466 ustconsumer32_data
.cmd_unix_sock_path
=
1467 config
.consumerd32_cmd_unix_sock_path
.value
;
1468 ustconsumer64_data
.err_unix_sock_path
=
1469 config
.consumerd64_err_unix_sock_path
.value
;
1470 ustconsumer64_data
.cmd_unix_sock_path
=
1471 config
.consumerd64_cmd_unix_sock_path
.value
;
1472 set_clock_plugin_env();
1474 sessiond_config_log(&config
);
1475 sessiond_uuid_log();
1477 if (create_lttng_rundir()) {
1482 /* Abort launch if a session daemon is already running. */
1483 if (check_existing_daemon()) {
1484 ERR("A session daemon is already running.");
1490 if (config
.daemonize
|| config
.background
) {
1493 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
1494 !config
.background
);
1501 * We are in the child. Make sure all other file descriptors are
1502 * closed, in case we are called with more opened file
1503 * descriptors than the standard ones and the lock file.
1505 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1506 if (i
== lockfile_fd
) {
1513 if (launch_run_as_worker(argv
[0]) < 0) {
1514 goto exit_create_run_as_worker_cleanup
;
1518 * Starting from here, we can create threads. This needs to be after
1519 * lttng_daemonize due to RCU.
1523 * Initialize the health check subsystem. This call should set the
1524 * appropriate time values.
1526 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1527 if (!health_sessiond
) {
1528 PERROR("health_app_create error");
1533 /* Create thread to clean up RCU hash tables */
1534 ht_cleanup_thread
= launch_ht_cleanup_thread();
1535 if (!ht_cleanup_thread
) {
1540 /* Create thread quit pipe */
1541 if (sessiond_init_thread_quit_pipe()) {
1546 /* Check if daemon is UID = 0 */
1547 is_root
= !getuid();
1549 /* Create global run dir with root access */
1551 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1552 if (!kernel_channel_monitor_pipe
) {
1553 ERR("Failed to create kernel consumer channel monitor pipe");
1557 kconsumer_data
.channel_monitor_pipe
=
1558 lttng_pipe_release_writefd(
1559 kernel_channel_monitor_pipe
);
1560 if (kconsumer_data
.channel_monitor_pipe
< 0) {
1566 /* Set consumer initial state */
1567 kernel_consumerd_state
= CONSUMER_STOPPED
;
1568 ust_consumerd_state
= CONSUMER_STOPPED
;
1570 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1571 if (!ust32_channel_monitor_pipe
) {
1572 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1576 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1577 ust32_channel_monitor_pipe
);
1578 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
1584 * The rotation_thread_timer_queue structure is shared between the
1585 * sessiond timer thread and the rotation thread. The main thread keeps
1586 * its ownership and destroys it when both threads have been joined.
1588 rotation_timer_queue
= rotation_thread_timer_queue_create();
1589 if (!rotation_timer_queue
) {
1593 timer_thread_parameters
.rotation_thread_job_queue
=
1594 rotation_timer_queue
;
1596 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1597 if (!ust64_channel_monitor_pipe
) {
1598 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1602 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1603 ust64_channel_monitor_pipe
);
1604 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
1610 * Init UST app hash table. Alloc hash table before this point since
1611 * cleanup() can get called after that point.
1613 if (ust_app_ht_alloc()) {
1614 ERR("Failed to allocate UST app hash table");
1620 * Initialize agent app hash table. We allocate the hash table here
1621 * since cleanup() can get called after this point.
1623 if (agent_app_ht_alloc()) {
1624 ERR("Failed to allocate Agent app hash table");
1630 * These actions must be executed as root. We do that *after* setting up
1631 * the sockets path because we MUST make the check for another daemon using
1632 * those paths *before* trying to set the kernel consumer sockets and init
1636 if (set_consumer_sockets(&kconsumer_data
)) {
1641 /* Setup kernel tracer */
1642 if (!config
.no_kernel
) {
1643 init_kernel_tracer();
1644 if (kernel_tracer_fd
>= 0) {
1645 ret
= syscall_init_table();
1647 ERR("Unable to populate syscall table. "
1648 "Syscall tracing won't work "
1649 "for this session daemon.");
1654 /* Set ulimit for open files */
1657 /* init lttng_fd tracking must be done after set_ulimit. */
1660 if (set_consumer_sockets(&ustconsumer64_data
)) {
1665 if (set_consumer_sockets(&ustconsumer32_data
)) {
1670 /* Get parent pid if -S, --sig-parent is specified. */
1671 if (config
.sig_parent
) {
1675 /* Setup the kernel pipe for waking up the kernel thread */
1676 if (is_root
&& !config
.no_kernel
) {
1677 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
1683 /* Setup the thread apps communication pipe. */
1684 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1689 /* Setup the thread apps notify communication pipe. */
1690 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1695 /* Initialize global buffer per UID and PID registry. */
1696 buffer_reg_init_uid_registry();
1697 buffer_reg_init_pid_registry();
1699 /* Init UST command queue. */
1700 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1704 /* Check for the application socket timeout env variable. */
1705 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1706 if (env_app_timeout
) {
1707 config
.app_socket_timeout
= atoi(env_app_timeout
);
1709 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1712 ret
= write_pidfile();
1714 ERR("Error in write_pidfile");
1719 /* Initialize communication library */
1721 /* Initialize TCP timeout values */
1722 lttcomm_inet_init();
1724 /* Create health-check thread. */
1725 if (!launch_health_management_thread()) {
1730 /* notification_thread_data acquires the pipes' read side. */
1731 notification_thread_handle
= notification_thread_handle_create(
1732 ust32_channel_monitor_pipe
,
1733 ust64_channel_monitor_pipe
,
1734 kernel_channel_monitor_pipe
);
1735 if (!notification_thread_handle
) {
1737 ERR("Failed to create notification thread shared data");
1741 /* Create notification thread. */
1742 notification_thread
= launch_notification_thread(
1743 notification_thread_handle
);
1744 if (!notification_thread
) {
1749 /* Create timer thread. */
1750 if (!launch_timer_thread(&timer_thread_parameters
)) {
1755 /* rotation_thread_data acquires the pipes' read side. */
1756 rotation_thread_handle
= rotation_thread_handle_create(
1757 rotation_timer_queue
,
1758 notification_thread_handle
);
1759 if (!rotation_thread_handle
) {
1761 ERR("Failed to create rotation thread shared data");
1766 /* Create rotation thread. */
1767 if (!launch_rotation_thread(rotation_thread_handle
)) {
1772 /* Create thread to manage the client socket */
1773 client_thread
= launch_client_thread();
1774 if (!client_thread
) {
1779 /* Set credentials of the client socket and rundir */
1780 if (is_root
&& set_permissions(config
.rundir
.value
)) {
1785 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1786 apps_cmd_notify_pipe
[1])) {
1791 /* Create thread to manage application registration. */
1792 register_apps_thread
= launch_application_registration_thread(
1794 if (!register_apps_thread
) {
1799 /* Create thread to manage application socket */
1800 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1805 /* Create thread to manage application notify socket */
1806 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1811 /* Create agent management thread. */
1812 if (!launch_agent_management_thread()) {
1817 /* Don't start this thread if kernel tracing is not requested nor root */
1818 if (is_root
&& !config
.no_kernel
) {
1819 /* Create kernel thread to manage kernel event */
1820 if (!launch_kernel_management_thread(kernel_poll_pipe
[0])) {
1826 /* Load sessions. */
1827 ret
= config_load_session(config
.load_session_path
.value
,
1830 ERR("Session load failed: %s", error_get_str(ret
));
1835 /* Initialization completed. */
1836 sessiond_signal_parents();
1839 * This is where we start awaiting program completion (e.g. through
1840 * signal that asks threads to teardown).
1843 /* Initiate teardown once activity occurs on the quit pipe. */
1844 sessiond_wait_for_quit_pipe(-1U);
1848 * Ensure that the client thread is no longer accepting new commands,
1849 * which could cause new sessions to be created.
1851 if (client_thread
) {
1852 lttng_thread_shutdown(client_thread
);
1853 lttng_thread_put(client_thread
);
1856 destroy_all_sessions_and_wait();
1858 if (register_apps_thread
) {
1859 lttng_thread_shutdown(register_apps_thread
);
1860 lttng_thread_put(register_apps_thread
);
1862 lttng_thread_list_shutdown_orphans();
1865 * Wait for all pending call_rcu work to complete before tearing
1866 * down data structures. call_rcu worker may be trying to
1867 * perform lookups in those structures.
1871 * sessiond_cleanup() is called when no other thread is running, except
1872 * the ht_cleanup thread, which is needed to destroy the hash tables.
1874 rcu_thread_online();
1877 if (notification_thread
) {
1878 lttng_thread_shutdown(notification_thread
);
1879 lttng_thread_put(notification_thread
);
1883 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1884 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1885 * the queue is empty before shutting down the clean-up thread.
1889 if (ht_cleanup_thread
) {
1890 lttng_thread_shutdown(ht_cleanup_thread
);
1891 lttng_thread_put(ht_cleanup_thread
);
1894 rcu_thread_offline();
1895 rcu_unregister_thread();
1897 if (rotation_thread_handle
) {
1898 rotation_thread_handle_destroy(rotation_thread_handle
);
1902 * After the rotation and timer thread have quit, we can safely destroy
1903 * the rotation_timer_queue.
1905 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1907 * The teardown of the notification system is performed after the
1908 * session daemon's teardown in order to allow it to be notified
1909 * of the active session and channels at the moment of the teardown.
1911 if (notification_thread_handle
) {
1912 notification_thread_handle_destroy(notification_thread_handle
);
1914 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1915 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1916 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1918 health_app_destroy(health_sessiond
);
1919 exit_create_run_as_worker_cleanup
:
1921 sessiond_cleanup_lock_file();
1922 sessiond_cleanup_options();
1924 exit_set_signal_handler
: