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 wait_consumer(&kconsumer_data
);
331 wait_consumer(&ustconsumer64_data
);
332 wait_consumer(&ustconsumer32_data
);
334 DBG("Cleaning up all agent apps");
335 agent_app_ht_clean();
337 DBG("Closing all UST sockets");
338 ust_app_clean_list();
339 buffer_reg_destroy_registries();
341 if (is_root
&& !config
.no_kernel
) {
342 DBG2("Closing kernel fd");
343 if (kernel_tracer_fd
>= 0) {
344 ret
= close(kernel_tracer_fd
);
349 DBG("Unloading kernel modules");
350 modprobe_remove_lttng_all();
354 close_consumer_sockets();
357 * We do NOT rmdir rundir because there are other processes
358 * using it, for instance lttng-relayd, which can start in
359 * parallel with this teardown.
364 * Cleanup the daemon's option data structures.
366 static void sessiond_cleanup_options(void)
368 DBG("Cleaning up options");
370 sessiond_config_fini(&config
);
372 run_as_destroy_worker();
376 * Setup necessary data for kernel tracer action.
378 static int init_kernel_tracer(void)
382 /* Modprobe lttng kernel modules */
383 ret
= modprobe_lttng_control();
388 /* Open debugfs lttng */
389 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
390 if (kernel_tracer_fd
< 0) {
391 DBG("Failed to open %s", module_proc_lttng
);
395 /* Validate kernel version */
396 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
397 &kernel_tracer_abi_version
);
402 ret
= modprobe_lttng_data();
407 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
414 WARN("Kernel tracer does not support buffer monitoring. "
415 "The monitoring timer of channels in the kernel domain "
416 "will be set to 0 (disabled).");
419 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
423 modprobe_remove_lttng_control();
424 ret
= close(kernel_tracer_fd
);
428 kernel_tracer_fd
= -1;
429 return LTTNG_ERR_KERN_VERSION
;
432 ret
= close(kernel_tracer_fd
);
438 modprobe_remove_lttng_control();
441 WARN("No kernel tracer available");
442 kernel_tracer_fd
= -1;
444 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
446 return LTTNG_ERR_KERN_NA
;
450 static int string_match(const char *str1
, const char *str2
)
452 return (str1
&& str2
) && !strcmp(str1
, str2
);
456 * Take an option from the getopt output and set it in the right variable to be
459 * Return 0 on success else a negative value.
461 static int set_option(int opt
, const char *arg
, const char *optname
)
465 if (string_match(optname
, "client-sock") || opt
== 'c') {
466 if (!arg
|| *arg
== '\0') {
470 if (lttng_is_setuid_setgid()) {
471 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
472 "-c, --client-sock");
474 config_string_set(&config
.client_unix_sock_path
,
476 if (!config
.client_unix_sock_path
.value
) {
481 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
482 if (!arg
|| *arg
== '\0') {
486 if (lttng_is_setuid_setgid()) {
487 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
490 config_string_set(&config
.apps_unix_sock_path
,
492 if (!config
.apps_unix_sock_path
.value
) {
497 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
498 config
.daemonize
= true;
499 } else if (string_match(optname
, "background") || opt
== 'b') {
500 config
.background
= true;
501 } else if (string_match(optname
, "group") || opt
== 'g') {
502 if (!arg
|| *arg
== '\0') {
506 if (lttng_is_setuid_setgid()) {
507 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
510 config_string_set(&config
.tracing_group_name
,
512 if (!config
.tracing_group_name
.value
) {
517 } else if (string_match(optname
, "help") || opt
== 'h') {
518 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
520 ERR("Cannot show --help for `lttng-sessiond`");
523 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
524 } else if (string_match(optname
, "version") || opt
== 'V') {
525 fprintf(stdout
, "%s\n", VERSION
);
527 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
528 config
.sig_parent
= true;
529 } else if (string_match(optname
, "kconsumerd-err-sock")) {
530 if (!arg
|| *arg
== '\0') {
534 if (lttng_is_setuid_setgid()) {
535 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
536 "--kconsumerd-err-sock");
538 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
540 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
545 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
546 if (!arg
|| *arg
== '\0') {
550 if (lttng_is_setuid_setgid()) {
551 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
552 "--kconsumerd-cmd-sock");
554 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
556 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
561 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
562 if (!arg
|| *arg
== '\0') {
566 if (lttng_is_setuid_setgid()) {
567 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
568 "--ustconsumerd64-err-sock");
570 config_string_set(&config
.consumerd64_err_unix_sock_path
,
572 if (!config
.consumerd64_err_unix_sock_path
.value
) {
577 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
578 if (!arg
|| *arg
== '\0') {
582 if (lttng_is_setuid_setgid()) {
583 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
584 "--ustconsumerd64-cmd-sock");
586 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
588 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
593 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
594 if (!arg
|| *arg
== '\0') {
598 if (lttng_is_setuid_setgid()) {
599 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
600 "--ustconsumerd32-err-sock");
602 config_string_set(&config
.consumerd32_err_unix_sock_path
,
604 if (!config
.consumerd32_err_unix_sock_path
.value
) {
609 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
610 if (!arg
|| *arg
== '\0') {
614 if (lttng_is_setuid_setgid()) {
615 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
616 "--ustconsumerd32-cmd-sock");
618 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
620 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
625 } else if (string_match(optname
, "no-kernel")) {
626 config
.no_kernel
= true;
627 } else if (string_match(optname
, "quiet") || opt
== 'q') {
629 } else if (string_match(optname
, "verbose") || opt
== 'v') {
630 /* Verbose level can increase using multiple -v */
632 /* Value obtained from config file */
633 config
.verbose
= config_parse_value(arg
);
635 /* -v used on command line */
638 /* Clamp value to [0, 3] */
639 config
.verbose
= config
.verbose
< 0 ? 0 :
640 (config
.verbose
<= 3 ? config
.verbose
: 3);
641 } else if (string_match(optname
, "verbose-consumer")) {
643 config
.verbose_consumer
= config_parse_value(arg
);
645 config
.verbose_consumer
++;
647 } else if (string_match(optname
, "consumerd32-path")) {
648 if (!arg
|| *arg
== '\0') {
652 if (lttng_is_setuid_setgid()) {
653 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
654 "--consumerd32-path");
656 config_string_set(&config
.consumerd32_bin_path
,
658 if (!config
.consumerd32_bin_path
.value
) {
663 } else if (string_match(optname
, "consumerd32-libdir")) {
664 if (!arg
|| *arg
== '\0') {
668 if (lttng_is_setuid_setgid()) {
669 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
670 "--consumerd32-libdir");
672 config_string_set(&config
.consumerd32_lib_dir
,
674 if (!config
.consumerd32_lib_dir
.value
) {
679 } else if (string_match(optname
, "consumerd64-path")) {
680 if (!arg
|| *arg
== '\0') {
684 if (lttng_is_setuid_setgid()) {
685 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
686 "--consumerd64-path");
688 config_string_set(&config
.consumerd64_bin_path
,
690 if (!config
.consumerd64_bin_path
.value
) {
695 } else if (string_match(optname
, "consumerd64-libdir")) {
696 if (!arg
|| *arg
== '\0') {
700 if (lttng_is_setuid_setgid()) {
701 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
702 "--consumerd64-libdir");
704 config_string_set(&config
.consumerd64_lib_dir
,
706 if (!config
.consumerd64_lib_dir
.value
) {
711 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
712 if (!arg
|| *arg
== '\0') {
716 if (lttng_is_setuid_setgid()) {
717 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
720 config_string_set(&config
.pid_file_path
, strdup(arg
));
721 if (!config
.pid_file_path
.value
) {
726 } else if (string_match(optname
, "agent-tcp-port")) {
727 if (!arg
|| *arg
== '\0') {
731 if (lttng_is_setuid_setgid()) {
732 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
738 v
= strtoul(arg
, NULL
, 0);
739 if (errno
!= 0 || !isdigit(arg
[0])) {
740 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
743 if (v
== 0 || v
>= 65535) {
744 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
747 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
748 DBG3("Agent TCP port set to non default: %i", (int) v
);
750 } else if (string_match(optname
, "load") || opt
== 'l') {
751 if (!arg
|| *arg
== '\0') {
755 if (lttng_is_setuid_setgid()) {
756 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
759 config_string_set(&config
.load_session_path
, strdup(arg
));
760 if (!config
.load_session_path
.value
) {
765 } else if (string_match(optname
, "kmod-probes")) {
766 if (!arg
|| *arg
== '\0') {
770 if (lttng_is_setuid_setgid()) {
771 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
774 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
775 if (!config
.kmod_probes_list
.value
) {
780 } else if (string_match(optname
, "extra-kmod-probes")) {
781 if (!arg
|| *arg
== '\0') {
785 if (lttng_is_setuid_setgid()) {
786 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
787 "--extra-kmod-probes");
789 config_string_set(&config
.kmod_extra_probes_list
,
791 if (!config
.kmod_extra_probes_list
.value
) {
796 } else if (string_match(optname
, "config") || opt
== 'f') {
797 /* This is handled in set_options() thus silent skip. */
800 /* Unknown option or other error.
801 * Error is printed by getopt, just return */
806 if (ret
== -EINVAL
) {
807 const char *opt_name
= "unknown";
810 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
812 if (opt
== long_options
[i
].val
) {
813 opt_name
= long_options
[i
].name
;
818 WARN("Invalid argument provided for option \"%s\", using default value.",
826 * config_entry_handler_cb used to handle options read from a config file.
827 * See config_entry_handler_cb comment in common/config/session-config.h for the
828 * return value conventions.
830 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
834 if (!entry
|| !entry
->name
|| !entry
->value
) {
839 /* Check if the option is to be ignored */
840 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
841 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
846 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
849 /* Ignore if not fully matched. */
850 if (strcmp(entry
->name
, long_options
[i
].name
)) {
855 * If the option takes no argument on the command line, we have to
856 * check if the value is "true". We support non-zero numeric values,
859 if (!long_options
[i
].has_arg
) {
860 ret
= config_parse_value(entry
->value
);
863 WARN("Invalid configuration value \"%s\" for option %s",
864 entry
->value
, entry
->name
);
866 /* False, skip boolean config option. */
871 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
875 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
882 * daemon configuration loading and argument parsing
884 static int set_options(int argc
, char **argv
)
886 int ret
= 0, c
= 0, option_index
= 0;
887 int orig_optopt
= optopt
, orig_optind
= optind
;
889 const char *config_path
= NULL
;
891 optstring
= utils_generate_optstring(long_options
,
892 sizeof(long_options
) / sizeof(struct option
));
898 /* Check for the --config option */
899 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
900 &option_index
)) != -1) {
904 } else if (c
!= 'f') {
905 /* if not equal to --config option. */
909 if (lttng_is_setuid_setgid()) {
910 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
913 config_path
= utils_expand_path(optarg
);
915 ERR("Failed to resolve path: %s", optarg
);
920 ret
= config_get_section_entries(config_path
, config_section_name
,
921 config_entry_handler
, NULL
);
924 ERR("Invalid configuration option at line %i", ret
);
930 /* Reset getopt's global state */
931 optopt
= orig_optopt
;
932 optind
= orig_optind
;
936 * getopt_long() will not set option_index if it encounters a
939 c
= getopt_long(argc
, argv
, optstring
, long_options
,
946 * Pass NULL as the long option name if popt left the index
949 ret
= set_option(c
, optarg
,
950 option_index
< 0 ? NULL
:
951 long_options
[option_index
].name
);
963 * Create lockfile using the rundir and return its fd.
965 static int create_lockfile(void)
967 return utils_create_lock_file(config
.lock_file_path
.value
);
971 * Check if the global socket is available, and if a daemon is answering at the
972 * other side. If yes, error is returned.
974 * Also attempts to create and hold the lock file.
976 static int check_existing_daemon(void)
980 /* Is there anybody out there ? */
981 if (lttng_session_daemon_alive()) {
986 lockfile_fd
= create_lockfile();
987 if (lockfile_fd
< 0) {
995 static void sessiond_cleanup_lock_file(void)
1000 * Cleanup lock file by deleting it and finaly closing it which will
1001 * release the file system lock.
1003 if (lockfile_fd
>= 0) {
1004 ret
= remove(config
.lock_file_path
.value
);
1006 PERROR("remove lock file");
1008 ret
= close(lockfile_fd
);
1010 PERROR("close lock file");
1016 * Set the tracing group gid onto the client socket.
1018 * Race window between mkdir and chown is OK because we are going from more
1019 * permissive (root.root) to less permissive (root.tracing).
1021 static int set_permissions(char *rundir
)
1026 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
1028 /* Set lttng run dir */
1029 ret
= chown(rundir
, 0, gid
);
1031 ERR("Unable to set group on %s", rundir
);
1036 * Ensure all applications and tracing group can search the run
1037 * dir. Allow everyone to read the directory, since it does not
1038 * buy us anything to hide its content.
1040 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1042 ERR("Unable to set permissions on %s", rundir
);
1046 /* lttng client socket path */
1047 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
1049 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
1053 /* kconsumer error socket path */
1054 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
1056 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
1060 /* 64-bit ustconsumer error socket path */
1061 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1063 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
1067 /* 32-bit ustconsumer compat32 error socket path */
1068 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1070 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
1074 DBG("All permissions are set");
1080 * Create the lttng run directory needed for all global sockets and pipe.
1082 static int create_lttng_rundir(void)
1086 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
1088 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
1090 if (errno
!= EEXIST
) {
1091 ERR("Unable to create %s", config
.rundir
.value
);
1103 * Setup sockets and directory needed by the consumerds' communication with the
1106 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1111 switch (consumer_data
->type
) {
1112 case LTTNG_CONSUMER_KERNEL
:
1113 path
= config
.kconsumerd_path
.value
;
1115 case LTTNG_CONSUMER64_UST
:
1116 path
= config
.consumerd64_path
.value
;
1118 case LTTNG_CONSUMER32_UST
:
1119 path
= config
.consumerd32_path
.value
;
1122 ERR("Consumer type unknown");
1128 DBG2("Creating consumer directory: %s", path
);
1130 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1131 if (ret
< 0 && errno
!= EEXIST
) {
1133 ERR("Failed to create %s", path
);
1137 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
1139 ERR("Unable to set group on %s", path
);
1145 /* Create the consumerd error unix socket */
1146 consumer_data
->err_sock
=
1147 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1148 if (consumer_data
->err_sock
< 0) {
1149 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1155 * Set the CLOEXEC flag. Return code is useless because either way, the
1158 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1160 PERROR("utils_set_fd_cloexec");
1161 /* continue anyway */
1164 /* File permission MUST be 660 */
1165 ret
= chmod(consumer_data
->err_unix_sock_path
,
1166 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1168 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1178 * Signal handler for the daemon
1180 * Simply stop all worker threads, leaving main() return gracefully after
1181 * joining all threads and calling cleanup().
1183 static void sighandler(int sig
)
1187 DBG("SIGINT caught");
1191 DBG("SIGTERM caught");
1195 CMM_STORE_SHARED(recv_child_signal
, 1);
1203 * Setup signal handler for :
1204 * SIGINT, SIGTERM, SIGPIPE
1206 static int set_signal_handler(void)
1209 struct sigaction sa
;
1212 if ((ret
= sigemptyset(&sigset
)) < 0) {
1213 PERROR("sigemptyset");
1217 sa
.sa_mask
= sigset
;
1220 sa
.sa_handler
= sighandler
;
1221 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1222 PERROR("sigaction");
1226 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1227 PERROR("sigaction");
1231 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1232 PERROR("sigaction");
1236 sa
.sa_handler
= SIG_IGN
;
1237 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1238 PERROR("sigaction");
1242 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1248 * Set open files limit to unlimited. This daemon can open a large number of
1249 * file descriptors in order to consume multiple kernel traces.
1251 static void set_ulimit(void)
1256 /* The kernel does not allow an infinite limit for open files */
1257 lim
.rlim_cur
= 65535;
1258 lim
.rlim_max
= 65535;
1260 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1262 PERROR("failed to set open files limit");
1266 static int write_pidfile(void)
1268 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1271 static int set_clock_plugin_env(void)
1274 char *env_value
= NULL
;
1276 if (!config
.lttng_ust_clock_plugin
.value
) {
1280 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1281 config
.lttng_ust_clock_plugin
.value
);
1287 ret
= putenv(env_value
);
1290 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1294 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1295 config
.lttng_ust_clock_plugin
.value
);
1300 static void destroy_all_sessions_and_wait(void)
1302 struct ltt_session
*session
, *tmp
;
1303 struct ltt_session_list
*session_list
;
1305 session_list
= session_get_list();
1306 DBG("Initiating destruction of all sessions");
1308 if (!session_list
) {
1312 session_lock_list();
1313 /* Initiate the destruction of all sessions. */
1314 cds_list_for_each_entry_safe(session
, tmp
,
1315 &session_list
->head
, list
) {
1316 if (!session_get(session
)) {
1320 session_lock(session
);
1321 if (session
->destroyed
) {
1322 goto unlock_session
;
1324 (void) cmd_stop_trace(session
);
1325 (void) cmd_destroy_session(session
,
1326 notification_thread_handle
);
1328 session_unlock(session
);
1329 session_put(session
);
1331 session_unlock_list();
1333 /* Wait for the destruction of all sessions to complete. */
1334 DBG("Waiting for the destruction of all sessions to complete");
1335 session_list_wait_empty();
1336 DBG("Destruction of all sessions completed");
1342 int main(int argc
, char **argv
)
1344 int ret
= 0, retval
= 0;
1345 const char *env_app_timeout
;
1346 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1347 *ust64_channel_monitor_pipe
= NULL
,
1348 *kernel_channel_monitor_pipe
= NULL
;
1349 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1350 struct timer_thread_parameters timer_thread_parameters
;
1351 /* Rotation thread handle. */
1352 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1353 /* Queue of rotation jobs populated by the sessiond-timer. */
1354 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1355 struct lttng_thread
*client_thread
= NULL
;
1356 struct lttng_thread
*notification_thread
= NULL
;
1358 init_kernel_workarounds();
1360 rcu_register_thread();
1362 if (set_signal_handler()) {
1364 goto exit_set_signal_handler
;
1367 if (timer_signal_init()) {
1369 goto exit_set_signal_handler
;
1372 page_size
= sysconf(_SC_PAGESIZE
);
1373 if (page_size
< 0) {
1374 PERROR("sysconf _SC_PAGESIZE");
1375 page_size
= LONG_MAX
;
1376 WARN("Fallback page size to %ld", page_size
);
1379 ret
= sessiond_config_init(&config
);
1382 goto exit_set_signal_handler
;
1386 * Init config from environment variables.
1387 * Command line option override env configuration per-doc. Do env first.
1389 sessiond_config_apply_env_config(&config
);
1392 * Parse arguments and load the daemon configuration file.
1394 * We have an exit_options exit path to free memory reserved by
1395 * set_options. This is needed because the rest of sessiond_cleanup()
1396 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1397 * depends on set_options.
1400 if (set_options(argc
, argv
)) {
1406 * Resolve all paths received as arguments, configuration option, or
1407 * through environment variable as absolute paths. This is necessary
1408 * since daemonizing causes the sessiond's current working directory
1411 ret
= sessiond_config_resolve_paths(&config
);
1417 lttng_opt_verbose
= config
.verbose
;
1418 lttng_opt_quiet
= config
.quiet
;
1419 kconsumer_data
.err_unix_sock_path
=
1420 config
.kconsumerd_err_unix_sock_path
.value
;
1421 kconsumer_data
.cmd_unix_sock_path
=
1422 config
.kconsumerd_cmd_unix_sock_path
.value
;
1423 ustconsumer32_data
.err_unix_sock_path
=
1424 config
.consumerd32_err_unix_sock_path
.value
;
1425 ustconsumer32_data
.cmd_unix_sock_path
=
1426 config
.consumerd32_cmd_unix_sock_path
.value
;
1427 ustconsumer64_data
.err_unix_sock_path
=
1428 config
.consumerd64_err_unix_sock_path
.value
;
1429 ustconsumer64_data
.cmd_unix_sock_path
=
1430 config
.consumerd64_cmd_unix_sock_path
.value
;
1431 set_clock_plugin_env();
1433 sessiond_config_log(&config
);
1435 if (create_lttng_rundir()) {
1440 /* Abort launch if a session daemon is already running. */
1441 if (check_existing_daemon()) {
1442 ERR("A session daemon is already running.");
1448 if (config
.daemonize
|| config
.background
) {
1451 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
1452 !config
.background
);
1459 * We are in the child. Make sure all other file descriptors are
1460 * closed, in case we are called with more opened file
1461 * descriptors than the standard ones and the lock file.
1463 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1464 if (i
== lockfile_fd
) {
1471 if (run_as_create_worker(argv
[0]) < 0) {
1472 goto exit_create_run_as_worker_cleanup
;
1476 * Starting from here, we can create threads. This needs to be after
1477 * lttng_daemonize due to RCU.
1481 * Initialize the health check subsystem. This call should set the
1482 * appropriate time values.
1484 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1485 if (!health_sessiond
) {
1486 PERROR("health_app_create error");
1488 goto exit_health_sessiond_cleanup
;
1491 /* Create thread to clean up RCU hash tables */
1492 ht_cleanup_thread
= launch_ht_cleanup_thread();
1493 if (!ht_cleanup_thread
) {
1495 goto exit_ht_cleanup
;
1498 /* Create thread quit pipe */
1499 if (sessiond_init_thread_quit_pipe()) {
1501 goto exit_init_data
;
1504 /* Check if daemon is UID = 0 */
1505 is_root
= !getuid();
1507 /* Create global run dir with root access */
1509 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1510 if (!kernel_channel_monitor_pipe
) {
1511 ERR("Failed to create kernel consumer channel monitor pipe");
1513 goto exit_init_data
;
1515 kconsumer_data
.channel_monitor_pipe
=
1516 lttng_pipe_release_writefd(
1517 kernel_channel_monitor_pipe
);
1518 if (kconsumer_data
.channel_monitor_pipe
< 0) {
1520 goto exit_init_data
;
1524 /* Set consumer initial state */
1525 kernel_consumerd_state
= CONSUMER_STOPPED
;
1526 ust_consumerd_state
= CONSUMER_STOPPED
;
1528 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1529 if (!ust32_channel_monitor_pipe
) {
1530 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1532 goto exit_init_data
;
1534 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1535 ust32_channel_monitor_pipe
);
1536 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
1538 goto exit_init_data
;
1542 * The rotation_thread_timer_queue structure is shared between the
1543 * sessiond timer thread and the rotation thread. The main thread keeps
1544 * its ownership and destroys it when both threads have been joined.
1546 rotation_timer_queue
= rotation_thread_timer_queue_create();
1547 if (!rotation_timer_queue
) {
1549 goto exit_init_data
;
1551 timer_thread_parameters
.rotation_thread_job_queue
=
1552 rotation_timer_queue
;
1554 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1555 if (!ust64_channel_monitor_pipe
) {
1556 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1558 goto exit_init_data
;
1560 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1561 ust64_channel_monitor_pipe
);
1562 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
1564 goto exit_init_data
;
1568 * Init UST app hash table. Alloc hash table before this point since
1569 * cleanup() can get called after that point.
1571 if (ust_app_ht_alloc()) {
1572 ERR("Failed to allocate UST app hash table");
1574 goto exit_init_data
;
1578 * Initialize agent app hash table. We allocate the hash table here
1579 * since cleanup() can get called after this point.
1581 if (agent_app_ht_alloc()) {
1582 ERR("Failed to allocate Agent app hash table");
1584 goto exit_init_data
;
1588 * These actions must be executed as root. We do that *after* setting up
1589 * the sockets path because we MUST make the check for another daemon using
1590 * those paths *before* trying to set the kernel consumer sockets and init
1594 if (set_consumer_sockets(&kconsumer_data
)) {
1596 goto exit_init_data
;
1599 /* Setup kernel tracer */
1600 if (!config
.no_kernel
) {
1601 init_kernel_tracer();
1602 if (kernel_tracer_fd
>= 0) {
1603 ret
= syscall_init_table();
1605 ERR("Unable to populate syscall table. "
1606 "Syscall tracing won't work "
1607 "for this session daemon.");
1612 /* Set ulimit for open files */
1615 /* init lttng_fd tracking must be done after set_ulimit. */
1618 if (set_consumer_sockets(&ustconsumer64_data
)) {
1620 goto exit_init_data
;
1623 if (set_consumer_sockets(&ustconsumer32_data
)) {
1625 goto exit_init_data
;
1628 /* Set credentials to socket */
1629 if (is_root
&& set_permissions(config
.rundir
.value
)) {
1631 goto exit_init_data
;
1634 /* Get parent pid if -S, --sig-parent is specified. */
1635 if (config
.sig_parent
) {
1639 /* Setup the kernel pipe for waking up the kernel thread */
1640 if (is_root
&& !config
.no_kernel
) {
1641 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
1643 goto exit_init_data
;
1647 /* Setup the thread apps communication pipe. */
1648 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1650 goto exit_init_data
;
1653 /* Setup the thread apps notify communication pipe. */
1654 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1656 goto exit_init_data
;
1659 /* Initialize global buffer per UID and PID registry. */
1660 buffer_reg_init_uid_registry();
1661 buffer_reg_init_pid_registry();
1663 /* Init UST command queue. */
1664 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1668 /* Check for the application socket timeout env variable. */
1669 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1670 if (env_app_timeout
) {
1671 config
.app_socket_timeout
= atoi(env_app_timeout
);
1673 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1676 ret
= write_pidfile();
1678 ERR("Error in write_pidfile");
1680 goto exit_init_data
;
1683 /* Initialize communication library */
1685 /* Initialize TCP timeout values */
1686 lttcomm_inet_init();
1688 /* Create health-check thread. */
1689 if (!launch_health_management_thread()) {
1694 /* notification_thread_data acquires the pipes' read side. */
1695 notification_thread_handle
= notification_thread_handle_create(
1696 ust32_channel_monitor_pipe
,
1697 ust64_channel_monitor_pipe
,
1698 kernel_channel_monitor_pipe
);
1699 if (!notification_thread_handle
) {
1701 ERR("Failed to create notification thread shared data");
1702 goto exit_notification
;
1705 /* Create notification thread. */
1706 notification_thread
= launch_notification_thread(
1707 notification_thread_handle
);
1708 if (!notification_thread
) {
1710 goto exit_notification
;
1713 /* Create timer thread. */
1714 if (!launch_timer_thread(&timer_thread_parameters
)) {
1716 goto exit_notification
;
1719 /* rotation_thread_data acquires the pipes' read side. */
1720 rotation_thread_handle
= rotation_thread_handle_create(
1721 rotation_timer_queue
,
1722 notification_thread_handle
);
1723 if (!rotation_thread_handle
) {
1725 ERR("Failed to create rotation thread shared data");
1730 /* Create rotation thread. */
1731 if (!launch_rotation_thread(rotation_thread_handle
)) {
1736 /* Create thread to manage the client socket */
1737 client_thread
= launch_client_thread();
1738 if (!client_thread
) {
1743 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1744 apps_cmd_notify_pipe
[1])) {
1749 /* Create thread to manage application registration. */
1750 if (!launch_application_registration_thread(&ust_cmd_queue
)) {
1755 /* Create thread to manage application socket */
1756 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1761 /* Create thread to manage application notify socket */
1762 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1764 goto exit_apps_notify
;
1767 /* Create agent management thread. */
1768 if (!launch_agent_management_thread()) {
1770 goto exit_agent_reg
;
1773 /* Don't start this thread if kernel tracing is not requested nor root */
1774 if (is_root
&& !config
.no_kernel
) {
1775 /* Create kernel thread to manage kernel event */
1776 if (!launch_kernel_management_thread(kernel_poll_pipe
[0])) {
1782 /* Load sessions. */
1783 ret
= config_load_session(config
.load_session_path
.value
,
1786 ERR("Session load failed: %s", error_get_str(ret
));
1788 goto exit_load_session
;
1791 /* Initialization completed. */
1792 sessiond_signal_parents();
1795 * This is where we start awaiting program completion (e.g. through
1796 * signal that asks threads to teardown).
1799 /* Initiate teardown once activity occurs on the quit pipe. */
1800 sessiond_wait_for_quit_pipe(-1U);
1803 * Ensure that the client thread is no longer accepting new commands,
1804 * which could cause new sessions to be created.
1806 if (!lttng_thread_shutdown(client_thread
)) {
1807 ERR("Failed to shutdown the client thread, continuing teardown");
1808 lttng_thread_put(client_thread
);
1809 client_thread
= NULL
;
1812 destroy_all_sessions_and_wait();
1823 lttng_thread_list_shutdown_orphans();
1826 if (client_thread
) {
1827 lttng_thread_put(client_thread
);
1831 * Wait for all pending call_rcu work to complete before tearing
1832 * down data structures. call_rcu worker may be trying to
1833 * perform lookups in those structures.
1837 * sessiond_cleanup() is called when no other thread is running, except
1838 * the ht_cleanup thread, which is needed to destroy the hash tables.
1840 rcu_thread_online();
1843 if (notification_thread
) {
1844 lttng_thread_shutdown(notification_thread
);
1845 lttng_thread_put(notification_thread
);
1849 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1850 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1851 * the queue is empty before shutting down the clean-up thread.
1855 if (ht_cleanup_thread
) {
1856 lttng_thread_shutdown(ht_cleanup_thread
);
1857 lttng_thread_put(ht_cleanup_thread
);
1860 rcu_thread_offline();
1861 rcu_unregister_thread();
1863 if (rotation_thread_handle
) {
1864 rotation_thread_handle_destroy(rotation_thread_handle
);
1868 * After the rotation and timer thread have quit, we can safely destroy
1869 * the rotation_timer_queue.
1871 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1873 * The teardown of the notification system is performed after the
1874 * session daemon's teardown in order to allow it to be notified
1875 * of the active session and channels at the moment of the teardown.
1877 if (notification_thread_handle
) {
1878 notification_thread_handle_destroy(notification_thread_handle
);
1880 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1881 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1882 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1885 health_app_destroy(health_sessiond
);
1886 exit_health_sessiond_cleanup
:
1887 exit_create_run_as_worker_cleanup
:
1890 sessiond_cleanup_lock_file();
1891 sessiond_cleanup_options();
1893 exit_set_signal_handler
: