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"
65 #include "lttng-ust-ctl.h"
66 #include "ust-consumer.h"
69 #include "health-sessiond.h"
70 #include "testpoint.h"
71 #include "notify-apps.h"
72 #include "agent-thread.h"
74 #include "notification-thread.h"
75 #include "notification-thread-commands.h"
76 #include "rotation-thread.h"
78 #include "ht-cleanup.h"
79 #include "sessiond-config.h"
85 #include "manage-apps.h"
86 #include "manage-kernel.h"
88 static const char *help_msg
=
89 #ifdef LTTNG_EMBED_HELP
90 #include <lttng-sessiond.8.h>
97 static int lockfile_fd
= -1;
98 static int opt_print_version
;
100 /* Set to 1 when a SIGUSR1 signal is received. */
101 static int recv_child_signal
;
103 /* Command line options */
104 static const struct option long_options
[] = {
105 { "client-sock", required_argument
, 0, 'c' },
106 { "apps-sock", required_argument
, 0, 'a' },
107 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
108 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
109 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
110 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
111 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
112 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
113 { "consumerd32-path", required_argument
, 0, '\0' },
114 { "consumerd32-libdir", required_argument
, 0, '\0' },
115 { "consumerd64-path", required_argument
, 0, '\0' },
116 { "consumerd64-libdir", required_argument
, 0, '\0' },
117 { "daemonize", no_argument
, 0, 'd' },
118 { "background", no_argument
, 0, 'b' },
119 { "sig-parent", no_argument
, 0, 'S' },
120 { "help", no_argument
, 0, 'h' },
121 { "group", required_argument
, 0, 'g' },
122 { "version", no_argument
, 0, 'V' },
123 { "quiet", no_argument
, 0, 'q' },
124 { "verbose", no_argument
, 0, 'v' },
125 { "verbose-consumer", no_argument
, 0, '\0' },
126 { "no-kernel", no_argument
, 0, '\0' },
127 { "pidfile", required_argument
, 0, 'p' },
128 { "agent-tcp-port", required_argument
, 0, '\0' },
129 { "config", required_argument
, 0, 'f' },
130 { "load", required_argument
, 0, 'l' },
131 { "kmod-probes", required_argument
, 0, '\0' },
132 { "extra-kmod-probes", required_argument
, 0, '\0' },
136 /* Command line options to ignore from configuration file */
137 static const char *config_ignore_options
[] = { "help", "version", "config" };
140 * This pipe is used to inform the thread managing application communication
141 * that a command is queued and ready to be processed.
143 static int apps_cmd_pipe
[2] = { -1, -1 };
144 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
147 * UST registration command queue. This queue is tied with a futex and uses a N
148 * wakers / 1 waiter implemented and detailed in futex.c/.h
150 * The thread_registration_apps and thread_dispatch_ust_registration uses this
151 * queue along with the wait/wake scheme. The thread_manage_apps receives down
152 * the line new application socket and monitors it for any I/O error or clean
153 * close that triggers an unregistration of the application.
155 static struct ust_cmd_queue ust_cmd_queue
;
158 * Section name to look for in the daemon configuration file.
160 static const char * const config_section_name
= "sessiond";
162 /* Am I root or not. Set to 1 if the daemon is running as root */
166 * Stop all threads by closing the thread quit pipe.
168 static void stop_threads(void)
172 /* Stopping all threads */
173 DBG("Terminating all threads");
174 ret
= sessiond_notify_quit_pipe();
176 ERR("write error on thread quit pipe");
181 * Close every consumer sockets.
183 static void close_consumer_sockets(void)
187 if (kconsumer_data
.err_sock
>= 0) {
188 ret
= close(kconsumer_data
.err_sock
);
190 PERROR("kernel consumer err_sock close");
193 if (ustconsumer32_data
.err_sock
>= 0) {
194 ret
= close(ustconsumer32_data
.err_sock
);
196 PERROR("UST consumerd32 err_sock close");
199 if (ustconsumer64_data
.err_sock
>= 0) {
200 ret
= close(ustconsumer64_data
.err_sock
);
202 PERROR("UST consumerd64 err_sock close");
205 if (kconsumer_data
.cmd_sock
>= 0) {
206 ret
= close(kconsumer_data
.cmd_sock
);
208 PERROR("kernel consumer cmd_sock close");
211 if (ustconsumer32_data
.cmd_sock
>= 0) {
212 ret
= close(ustconsumer32_data
.cmd_sock
);
214 PERROR("UST consumerd32 cmd_sock close");
217 if (ustconsumer64_data
.cmd_sock
>= 0) {
218 ret
= close(ustconsumer64_data
.cmd_sock
);
220 PERROR("UST consumerd64 cmd_sock close");
223 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
224 ret
= close(kconsumer_data
.channel_monitor_pipe
);
226 PERROR("kernel consumer channel monitor pipe close");
229 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
230 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
232 PERROR("UST consumerd32 channel monitor pipe close");
235 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
236 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
238 PERROR("UST consumerd64 channel monitor pipe close");
244 * Wait on consumer process termination.
246 * Need to be called with the consumer data lock held or from a context
247 * ensuring no concurrent access to data (e.g: cleanup).
249 static void wait_consumer(struct consumer_data
*consumer_data
)
254 if (consumer_data
->pid
<= 0) {
258 DBG("Waiting for complete teardown of consumerd (PID: %d)",
260 ret
= waitpid(consumer_data
->pid
, &status
, 0);
262 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
263 } else if (!WIFEXITED(status
)) {
264 ERR("consumerd termination with error: %d",
267 consumer_data
->pid
= 0;
271 * Cleanup the session daemon's data structures.
273 static void sessiond_cleanup(void)
276 struct ltt_session_list
*session_list
= session_get_list();
278 DBG("Cleanup sessiond");
281 * Close the thread quit pipe. It has already done its job,
282 * since we are now called.
284 sessiond_close_quit_pipe();
285 utils_close_pipe(apps_cmd_pipe
);
286 utils_close_pipe(apps_cmd_notify_pipe
);
287 utils_close_pipe(kernel_poll_pipe
);
289 ret
= remove(config
.pid_file_path
.value
);
291 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
294 DBG("Removing sessiond and consumerd content of directory %s",
295 config
.rundir
.value
);
298 DBG("Removing %s", config
.pid_file_path
.value
);
299 (void) unlink(config
.pid_file_path
.value
);
301 DBG("Removing %s", config
.agent_port_file_path
.value
);
302 (void) unlink(config
.agent_port_file_path
.value
);
305 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
306 (void) unlink(kconsumer_data
.err_unix_sock_path
);
308 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
309 (void) rmdir(config
.kconsumerd_path
.value
);
311 /* ust consumerd 32 */
312 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
313 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
315 DBG("Removing directory %s", config
.consumerd32_path
.value
);
316 (void) rmdir(config
.consumerd32_path
.value
);
318 /* ust consumerd 64 */
319 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
320 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
322 DBG("Removing directory %s", config
.consumerd64_path
.value
);
323 (void) rmdir(config
.consumerd64_path
.value
);
325 pthread_mutex_destroy(&session_list
->lock
);
327 DBG("Cleaning up all agent apps");
328 agent_app_ht_clean();
329 DBG("Closing all UST sockets");
330 ust_app_clean_list();
331 buffer_reg_destroy_registries();
333 close_consumer_sockets();
335 wait_consumer(&kconsumer_data
);
336 wait_consumer(&ustconsumer64_data
);
337 wait_consumer(&ustconsumer32_data
);
339 if (is_root
&& !config
.no_kernel
) {
340 cleanup_kernel_tracer();
344 * We do NOT rmdir rundir because there are other processes
345 * using it, for instance lttng-relayd, which can start in
346 * parallel with this teardown.
351 * Cleanup the daemon's option data structures.
353 static void sessiond_cleanup_options(void)
355 DBG("Cleaning up options");
357 sessiond_config_fini(&config
);
359 run_as_destroy_worker();
362 static int string_match(const char *str1
, const char *str2
)
364 return (str1
&& str2
) && !strcmp(str1
, str2
);
368 * Take an option from the getopt output and set it in the right variable to be
371 * Return 0 on success else a negative value.
373 static int set_option(int opt
, const char *arg
, const char *optname
)
377 if (string_match(optname
, "client-sock") || opt
== 'c') {
378 if (!arg
|| *arg
== '\0') {
382 if (lttng_is_setuid_setgid()) {
383 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
384 "-c, --client-sock");
386 config_string_set(&config
.client_unix_sock_path
,
388 if (!config
.client_unix_sock_path
.value
) {
393 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
394 if (!arg
|| *arg
== '\0') {
398 if (lttng_is_setuid_setgid()) {
399 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
402 config_string_set(&config
.apps_unix_sock_path
,
404 if (!config
.apps_unix_sock_path
.value
) {
409 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
410 config
.daemonize
= true;
411 } else if (string_match(optname
, "background") || opt
== 'b') {
412 config
.background
= true;
413 } else if (string_match(optname
, "group") || opt
== 'g') {
414 if (!arg
|| *arg
== '\0') {
418 if (lttng_is_setuid_setgid()) {
419 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
422 config_string_set(&config
.tracing_group_name
,
424 if (!config
.tracing_group_name
.value
) {
429 } else if (string_match(optname
, "help") || opt
== 'h') {
430 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
432 ERR("Cannot show --help for `lttng-sessiond`");
435 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
436 } else if (string_match(optname
, "version") || opt
== 'V') {
437 opt_print_version
= 1;
438 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
439 config
.sig_parent
= true;
440 } else if (string_match(optname
, "kconsumerd-err-sock")) {
441 if (!arg
|| *arg
== '\0') {
445 if (lttng_is_setuid_setgid()) {
446 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
447 "--kconsumerd-err-sock");
449 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
451 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
456 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
457 if (!arg
|| *arg
== '\0') {
461 if (lttng_is_setuid_setgid()) {
462 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
463 "--kconsumerd-cmd-sock");
465 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
467 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
472 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
473 if (!arg
|| *arg
== '\0') {
477 if (lttng_is_setuid_setgid()) {
478 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
479 "--ustconsumerd64-err-sock");
481 config_string_set(&config
.consumerd64_err_unix_sock_path
,
483 if (!config
.consumerd64_err_unix_sock_path
.value
) {
488 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
489 if (!arg
|| *arg
== '\0') {
493 if (lttng_is_setuid_setgid()) {
494 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
495 "--ustconsumerd64-cmd-sock");
497 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
499 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
504 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
505 if (!arg
|| *arg
== '\0') {
509 if (lttng_is_setuid_setgid()) {
510 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
511 "--ustconsumerd32-err-sock");
513 config_string_set(&config
.consumerd32_err_unix_sock_path
,
515 if (!config
.consumerd32_err_unix_sock_path
.value
) {
520 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
521 if (!arg
|| *arg
== '\0') {
525 if (lttng_is_setuid_setgid()) {
526 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
527 "--ustconsumerd32-cmd-sock");
529 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
531 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
536 } else if (string_match(optname
, "no-kernel")) {
537 config
.no_kernel
= true;
538 } else if (string_match(optname
, "quiet") || opt
== 'q') {
540 } else if (string_match(optname
, "verbose") || opt
== 'v') {
541 /* Verbose level can increase using multiple -v */
543 /* Value obtained from config file */
544 config
.verbose
= config_parse_value(arg
);
546 /* -v used on command line */
549 /* Clamp value to [0, 3] */
550 config
.verbose
= config
.verbose
< 0 ? 0 :
551 (config
.verbose
<= 3 ? config
.verbose
: 3);
552 } else if (string_match(optname
, "verbose-consumer")) {
554 config
.verbose_consumer
= config_parse_value(arg
);
556 config
.verbose_consumer
++;
558 } else if (string_match(optname
, "consumerd32-path")) {
559 if (!arg
|| *arg
== '\0') {
563 if (lttng_is_setuid_setgid()) {
564 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
565 "--consumerd32-path");
567 config_string_set(&config
.consumerd32_bin_path
,
569 if (!config
.consumerd32_bin_path
.value
) {
574 } else if (string_match(optname
, "consumerd32-libdir")) {
575 if (!arg
|| *arg
== '\0') {
579 if (lttng_is_setuid_setgid()) {
580 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
581 "--consumerd32-libdir");
583 config_string_set(&config
.consumerd32_lib_dir
,
585 if (!config
.consumerd32_lib_dir
.value
) {
590 } else if (string_match(optname
, "consumerd64-path")) {
591 if (!arg
|| *arg
== '\0') {
595 if (lttng_is_setuid_setgid()) {
596 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
597 "--consumerd64-path");
599 config_string_set(&config
.consumerd64_bin_path
,
601 if (!config
.consumerd64_bin_path
.value
) {
606 } else if (string_match(optname
, "consumerd64-libdir")) {
607 if (!arg
|| *arg
== '\0') {
611 if (lttng_is_setuid_setgid()) {
612 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
613 "--consumerd64-libdir");
615 config_string_set(&config
.consumerd64_lib_dir
,
617 if (!config
.consumerd64_lib_dir
.value
) {
622 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
623 if (!arg
|| *arg
== '\0') {
627 if (lttng_is_setuid_setgid()) {
628 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
631 config_string_set(&config
.pid_file_path
, strdup(arg
));
632 if (!config
.pid_file_path
.value
) {
637 } else if (string_match(optname
, "agent-tcp-port")) {
638 if (!arg
|| *arg
== '\0') {
642 if (lttng_is_setuid_setgid()) {
643 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
649 v
= strtoul(arg
, NULL
, 0);
650 if (errno
!= 0 || !isdigit(arg
[0])) {
651 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
654 if (v
== 0 || v
>= 65535) {
655 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
658 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
659 DBG3("Agent TCP port set to non default: %i", (int) v
);
661 } else if (string_match(optname
, "load") || opt
== 'l') {
662 if (!arg
|| *arg
== '\0') {
666 if (lttng_is_setuid_setgid()) {
667 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
670 config_string_set(&config
.load_session_path
, strdup(arg
));
671 if (!config
.load_session_path
.value
) {
676 } else if (string_match(optname
, "kmod-probes")) {
677 if (!arg
|| *arg
== '\0') {
681 if (lttng_is_setuid_setgid()) {
682 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
685 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
686 if (!config
.kmod_probes_list
.value
) {
691 } else if (string_match(optname
, "extra-kmod-probes")) {
692 if (!arg
|| *arg
== '\0') {
696 if (lttng_is_setuid_setgid()) {
697 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
698 "--extra-kmod-probes");
700 config_string_set(&config
.kmod_extra_probes_list
,
702 if (!config
.kmod_extra_probes_list
.value
) {
707 } else if (string_match(optname
, "config") || opt
== 'f') {
708 /* This is handled in set_options() thus silent skip. */
711 /* Unknown option or other error.
712 * Error is printed by getopt, just return */
717 if (ret
== -EINVAL
) {
718 const char *opt_name
= "unknown";
721 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
723 if (opt
== long_options
[i
].val
) {
724 opt_name
= long_options
[i
].name
;
729 WARN("Invalid argument provided for option \"%s\", using default value.",
737 * config_entry_handler_cb used to handle options read from a config file.
738 * See config_entry_handler_cb comment in common/config/session-config.h for the
739 * return value conventions.
741 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
745 if (!entry
|| !entry
->name
|| !entry
->value
) {
750 /* Check if the option is to be ignored */
751 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
752 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
757 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
760 /* Ignore if not fully matched. */
761 if (strcmp(entry
->name
, long_options
[i
].name
)) {
766 * If the option takes no argument on the command line, we have to
767 * check if the value is "true". We support non-zero numeric values,
770 if (!long_options
[i
].has_arg
) {
771 ret
= config_parse_value(entry
->value
);
774 WARN("Invalid configuration value \"%s\" for option %s",
775 entry
->value
, entry
->name
);
777 /* False, skip boolean config option. */
782 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
786 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
792 static void print_version(void) {
793 fprintf(stdout
, "%s\n", VERSION
);
797 * daemon configuration loading and argument parsing
799 static int set_options(int argc
, char **argv
)
801 int ret
= 0, c
= 0, option_index
= 0;
802 int orig_optopt
= optopt
, orig_optind
= optind
;
804 const char *config_path
= NULL
;
806 optstring
= utils_generate_optstring(long_options
,
807 sizeof(long_options
) / sizeof(struct option
));
813 /* Check for the --config option */
814 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
815 &option_index
)) != -1) {
819 } else if (c
!= 'f') {
820 /* if not equal to --config option. */
824 if (lttng_is_setuid_setgid()) {
825 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
828 config_path
= utils_expand_path(optarg
);
830 ERR("Failed to resolve path: %s", optarg
);
835 ret
= config_get_section_entries(config_path
, config_section_name
,
836 config_entry_handler
, NULL
);
839 ERR("Invalid configuration option at line %i", ret
);
845 /* Reset getopt's global state */
846 optopt
= orig_optopt
;
847 optind
= orig_optind
;
851 * getopt_long() will not set option_index if it encounters a
854 c
= getopt_long(argc
, argv
, optstring
, long_options
,
861 * Pass NULL as the long option name if popt left the index
864 ret
= set_option(c
, optarg
,
865 option_index
< 0 ? NULL
:
866 long_options
[option_index
].name
);
878 * Create lockfile using the rundir and return its fd.
880 static int create_lockfile(void)
882 return utils_create_lock_file(config
.lock_file_path
.value
);
886 * Check if the global socket is available, and if a daemon is answering at the
887 * other side. If yes, error is returned.
889 * Also attempts to create and hold the lock file.
891 static int check_existing_daemon(void)
895 /* Is there anybody out there ? */
896 if (lttng_session_daemon_alive()) {
901 lockfile_fd
= create_lockfile();
902 if (lockfile_fd
< 0) {
910 static void sessiond_cleanup_lock_file(void)
915 * Cleanup lock file by deleting it and finaly closing it which will
916 * release the file system lock.
918 if (lockfile_fd
>= 0) {
919 ret
= remove(config
.lock_file_path
.value
);
921 PERROR("remove lock file");
923 ret
= close(lockfile_fd
);
925 PERROR("close lock file");
931 * Set the tracing group gid onto the client socket.
933 * Race window between mkdir and chown is OK because we are going from more
934 * permissive (root.root) to less permissive (root.tracing).
936 static int set_permissions(char *rundir
)
941 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true, &gid
);
943 /* Default to root group. */
947 /* Set lttng run dir */
948 ret
= chown(rundir
, 0, gid
);
950 ERR("Unable to set group on %s", rundir
);
955 * Ensure all applications and tracing group can search the run
956 * dir. Allow everyone to read the directory, since it does not
957 * buy us anything to hide its content.
959 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
961 ERR("Unable to set permissions on %s", rundir
);
965 /* lttng client socket path */
966 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
968 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
972 /* kconsumer error socket path */
973 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
975 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
979 /* 64-bit ustconsumer error socket path */
980 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
982 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
986 /* 32-bit ustconsumer compat32 error socket path */
987 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
989 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
993 DBG("All permissions are set");
999 * Create the lttng run directory needed for all global sockets and pipe.
1001 static int create_lttng_rundir(void)
1005 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
1007 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
1009 if (errno
!= EEXIST
) {
1010 ERR("Unable to create %s", config
.rundir
.value
);
1022 * Setup sockets and directory needed by the consumerds' communication with the
1025 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1030 switch (consumer_data
->type
) {
1031 case LTTNG_CONSUMER_KERNEL
:
1032 path
= config
.kconsumerd_path
.value
;
1034 case LTTNG_CONSUMER64_UST
:
1035 path
= config
.consumerd64_path
.value
;
1037 case LTTNG_CONSUMER32_UST
:
1038 path
= config
.consumerd32_path
.value
;
1041 ERR("Consumer type unknown");
1047 DBG2("Creating consumer directory: %s", path
);
1049 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1050 if (ret
< 0 && errno
!= EEXIST
) {
1052 ERR("Failed to create %s", path
);
1058 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
1061 /* Default to root group. */
1065 ret
= chown(path
, 0, gid
);
1067 ERR("Unable to set group on %s", path
);
1073 /* Create the consumerd error unix socket */
1074 consumer_data
->err_sock
=
1075 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1076 if (consumer_data
->err_sock
< 0) {
1077 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1083 * Set the CLOEXEC flag. Return code is useless because either way, the
1086 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1088 PERROR("utils_set_fd_cloexec");
1089 /* continue anyway */
1092 /* File permission MUST be 660 */
1093 ret
= chmod(consumer_data
->err_unix_sock_path
,
1094 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1096 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1106 * Signal handler for the daemon
1108 * Simply stop all worker threads, leaving main() return gracefully after
1109 * joining all threads and calling cleanup().
1111 static void sighandler(int sig
)
1115 DBG("SIGINT caught");
1119 DBG("SIGTERM caught");
1123 CMM_STORE_SHARED(recv_child_signal
, 1);
1131 * Setup signal handler for :
1132 * SIGINT, SIGTERM, SIGPIPE
1134 static int set_signal_handler(void)
1137 struct sigaction sa
;
1140 if ((ret
= sigemptyset(&sigset
)) < 0) {
1141 PERROR("sigemptyset");
1145 sa
.sa_mask
= sigset
;
1148 sa
.sa_handler
= sighandler
;
1149 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1150 PERROR("sigaction");
1154 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1155 PERROR("sigaction");
1159 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1160 PERROR("sigaction");
1164 sa
.sa_handler
= SIG_IGN
;
1165 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1166 PERROR("sigaction");
1170 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1176 * Set open files limit to unlimited. This daemon can open a large number of
1177 * file descriptors in order to consume multiple kernel traces.
1179 static void set_ulimit(void)
1184 /* The kernel does not allow an infinite limit for open files */
1185 lim
.rlim_cur
= 65535;
1186 lim
.rlim_max
= 65535;
1188 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1190 PERROR("failed to set open files limit");
1194 static int write_pidfile(void)
1196 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1199 static int set_clock_plugin_env(void)
1202 char *env_value
= NULL
;
1204 if (!config
.lttng_ust_clock_plugin
.value
) {
1208 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1209 config
.lttng_ust_clock_plugin
.value
);
1215 ret
= putenv(env_value
);
1218 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1222 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1223 config
.lttng_ust_clock_plugin
.value
);
1228 static void destroy_all_sessions_and_wait(void)
1230 struct ltt_session
*session
, *tmp
;
1231 struct ltt_session_list
*session_list
;
1233 session_list
= session_get_list();
1234 DBG("Initiating destruction of all sessions");
1236 if (!session_list
) {
1240 session_lock_list();
1241 /* Initiate the destruction of all sessions. */
1242 cds_list_for_each_entry_safe(session
, tmp
,
1243 &session_list
->head
, list
) {
1244 if (!session_get(session
)) {
1248 session_lock(session
);
1249 if (session
->destroyed
) {
1250 goto unlock_session
;
1252 (void) cmd_stop_trace(session
);
1253 (void) cmd_destroy_session(session
, notification_thread_handle
,
1256 session_unlock(session
);
1257 session_put(session
);
1259 session_unlock_list();
1261 /* Wait for the destruction of all sessions to complete. */
1262 DBG("Waiting for the destruction of all sessions to complete");
1263 session_list_wait_empty();
1264 DBG("Destruction of all sessions completed");
1267 static int run_as_worker_post_fork_cleanup(void *data
)
1269 struct sessiond_config
*sessiond_config
= data
;
1271 sessiond_config_fini(sessiond_config
);
1275 static int launch_run_as_worker(const char *procname
)
1278 * Clean-up before forking the run-as worker. Any dynamically
1279 * allocated memory of which the worker is not aware will
1280 * be leaked as the process forks a run-as worker (and performs
1281 * no exec*()). The same would apply to any opened fd.
1283 return run_as_create_worker(procname
, run_as_worker_post_fork_cleanup
,
1287 static void sessiond_uuid_log(void)
1289 char uuid_str
[LTTNG_UUID_STR_LEN
];
1291 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
1292 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1298 int main(int argc
, char **argv
)
1300 int ret
= 0, retval
= 0;
1301 const char *env_app_timeout
;
1302 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1303 *ust64_channel_monitor_pipe
= NULL
,
1304 *kernel_channel_monitor_pipe
= NULL
;
1305 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1306 struct timer_thread_parameters timer_thread_parameters
;
1307 /* Rotation thread handle. */
1308 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1309 /* Queue of rotation jobs populated by the sessiond-timer. */
1310 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1311 struct lttng_thread
*client_thread
= NULL
;
1312 struct lttng_thread
*notification_thread
= NULL
;
1313 struct lttng_thread
*register_apps_thread
= NULL
;
1315 init_kernel_workarounds();
1317 rcu_register_thread();
1319 if (set_signal_handler()) {
1321 goto exit_set_signal_handler
;
1324 if (timer_signal_init()) {
1326 goto exit_set_signal_handler
;
1329 page_size
= sysconf(_SC_PAGESIZE
);
1330 if (page_size
< 0) {
1331 PERROR("sysconf _SC_PAGESIZE");
1332 page_size
= LONG_MAX
;
1333 WARN("Fallback page size to %ld", page_size
);
1336 ret
= sessiond_config_init(&config
);
1339 goto exit_set_signal_handler
;
1343 * Init config from environment variables.
1344 * Command line option override env configuration per-doc. Do env first.
1346 sessiond_config_apply_env_config(&config
);
1349 * Parse arguments and load the daemon configuration file.
1351 * We have an exit_options exit path to free memory reserved by
1352 * set_options. This is needed because the rest of sessiond_cleanup()
1353 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1354 * depends on set_options.
1357 if (set_options(argc
, argv
)) {
1363 * Resolve all paths received as arguments, configuration option, or
1364 * through environment variable as absolute paths. This is necessary
1365 * since daemonizing causes the sessiond's current working directory
1368 ret
= sessiond_config_resolve_paths(&config
);
1374 lttng_opt_verbose
= config
.verbose
;
1375 lttng_opt_quiet
= config
.quiet
;
1376 kconsumer_data
.err_unix_sock_path
=
1377 config
.kconsumerd_err_unix_sock_path
.value
;
1378 kconsumer_data
.cmd_unix_sock_path
=
1379 config
.kconsumerd_cmd_unix_sock_path
.value
;
1380 ustconsumer32_data
.err_unix_sock_path
=
1381 config
.consumerd32_err_unix_sock_path
.value
;
1382 ustconsumer32_data
.cmd_unix_sock_path
=
1383 config
.consumerd32_cmd_unix_sock_path
.value
;
1384 ustconsumer64_data
.err_unix_sock_path
=
1385 config
.consumerd64_err_unix_sock_path
.value
;
1386 ustconsumer64_data
.cmd_unix_sock_path
=
1387 config
.consumerd64_cmd_unix_sock_path
.value
;
1388 set_clock_plugin_env();
1390 sessiond_config_log(&config
);
1391 sessiond_uuid_log();
1393 if (opt_print_version
) {
1399 if (create_lttng_rundir()) {
1404 /* Abort launch if a session daemon is already running. */
1405 if (check_existing_daemon()) {
1406 ERR("A session daemon is already running.");
1412 if (config
.daemonize
|| config
.background
) {
1415 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
1416 !config
.background
);
1423 * We are in the child. Make sure all other file descriptors are
1424 * closed, in case we are called with more opened file
1425 * descriptors than the standard ones and the lock file.
1427 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1428 if (i
== lockfile_fd
) {
1435 if (launch_run_as_worker(argv
[0]) < 0) {
1436 goto exit_create_run_as_worker_cleanup
;
1440 * Starting from here, we can create threads. This needs to be after
1441 * lttng_daemonize due to RCU.
1445 * Initialize the health check subsystem. This call should set the
1446 * appropriate time values.
1448 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1449 if (!health_sessiond
) {
1450 PERROR("health_app_create error");
1455 /* Create thread to clean up RCU hash tables */
1456 ht_cleanup_thread
= launch_ht_cleanup_thread();
1457 if (!ht_cleanup_thread
) {
1462 /* Create thread quit pipe */
1463 if (sessiond_init_thread_quit_pipe()) {
1468 /* Check if daemon is UID = 0 */
1469 is_root
= !getuid();
1471 /* Create global run dir with root access */
1473 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1474 if (!kernel_channel_monitor_pipe
) {
1475 ERR("Failed to create kernel consumer channel monitor pipe");
1479 kconsumer_data
.channel_monitor_pipe
=
1480 lttng_pipe_release_writefd(
1481 kernel_channel_monitor_pipe
);
1482 if (kconsumer_data
.channel_monitor_pipe
< 0) {
1488 /* Set consumer initial state */
1489 kernel_consumerd_state
= CONSUMER_STOPPED
;
1490 ust_consumerd_state
= CONSUMER_STOPPED
;
1492 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1493 if (!ust32_channel_monitor_pipe
) {
1494 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1498 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1499 ust32_channel_monitor_pipe
);
1500 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
1506 * The rotation_thread_timer_queue structure is shared between the
1507 * sessiond timer thread and the rotation thread. The main thread keeps
1508 * its ownership and destroys it when both threads have been joined.
1510 rotation_timer_queue
= rotation_thread_timer_queue_create();
1511 if (!rotation_timer_queue
) {
1515 timer_thread_parameters
.rotation_thread_job_queue
=
1516 rotation_timer_queue
;
1518 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1519 if (!ust64_channel_monitor_pipe
) {
1520 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1524 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1525 ust64_channel_monitor_pipe
);
1526 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
1532 * Init UST app hash table. Alloc hash table before this point since
1533 * cleanup() can get called after that point.
1535 if (ust_app_ht_alloc()) {
1536 ERR("Failed to allocate UST app hash table");
1542 * Initialize agent app hash table. We allocate the hash table here
1543 * since cleanup() can get called after this point.
1545 if (agent_app_ht_alloc()) {
1546 ERR("Failed to allocate Agent app hash table");
1552 * These actions must be executed as root. We do that *after* setting up
1553 * the sockets path because we MUST make the check for another daemon using
1554 * those paths *before* trying to set the kernel consumer sockets and init
1558 if (set_consumer_sockets(&kconsumer_data
)) {
1563 /* Setup kernel tracer */
1564 if (!config
.no_kernel
) {
1565 init_kernel_tracer();
1568 /* Set ulimit for open files */
1571 /* init lttng_fd tracking must be done after set_ulimit. */
1574 if (set_consumer_sockets(&ustconsumer64_data
)) {
1579 if (set_consumer_sockets(&ustconsumer32_data
)) {
1584 /* Get parent pid if -S, --sig-parent is specified. */
1585 if (config
.sig_parent
) {
1589 /* Setup the kernel pipe for waking up the kernel thread */
1590 if (is_root
&& !config
.no_kernel
) {
1591 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
1597 /* Setup the thread apps communication pipe. */
1598 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1603 /* Setup the thread apps notify communication pipe. */
1604 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1609 /* Initialize global buffer per UID and PID registry. */
1610 buffer_reg_init_uid_registry();
1611 buffer_reg_init_pid_registry();
1613 /* Init UST command queue. */
1614 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1618 /* Check for the application socket timeout env variable. */
1619 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1620 if (env_app_timeout
) {
1621 config
.app_socket_timeout
= atoi(env_app_timeout
);
1623 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1626 ret
= write_pidfile();
1628 ERR("Error in write_pidfile");
1633 /* Initialize communication library */
1635 /* Initialize TCP timeout values */
1636 lttcomm_inet_init();
1638 /* Create health-check thread. */
1639 if (!launch_health_management_thread()) {
1644 /* notification_thread_data acquires the pipes' read side. */
1645 notification_thread_handle
= notification_thread_handle_create(
1646 ust32_channel_monitor_pipe
,
1647 ust64_channel_monitor_pipe
,
1648 kernel_channel_monitor_pipe
);
1649 if (!notification_thread_handle
) {
1651 ERR("Failed to create notification thread shared data");
1655 /* Create notification thread. */
1656 notification_thread
= launch_notification_thread(
1657 notification_thread_handle
);
1658 if (!notification_thread
) {
1663 /* Create timer thread. */
1664 if (!launch_timer_thread(&timer_thread_parameters
)) {
1669 /* rotation_thread_data acquires the pipes' read side. */
1670 rotation_thread_handle
= rotation_thread_handle_create(
1671 rotation_timer_queue
,
1672 notification_thread_handle
);
1673 if (!rotation_thread_handle
) {
1675 ERR("Failed to create rotation thread shared data");
1680 /* Create rotation thread. */
1681 if (!launch_rotation_thread(rotation_thread_handle
)) {
1686 /* Create thread to manage the client socket */
1687 client_thread
= launch_client_thread();
1688 if (!client_thread
) {
1693 /* Set credentials of the client socket and rundir */
1694 if (is_root
&& set_permissions(config
.rundir
.value
)) {
1699 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1700 apps_cmd_notify_pipe
[1])) {
1705 /* Create thread to manage application registration. */
1706 register_apps_thread
= launch_application_registration_thread(
1708 if (!register_apps_thread
) {
1713 /* Create thread to manage application socket */
1714 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1719 /* Create thread to manage application notify socket */
1720 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1725 /* Create agent management thread. */
1726 if (!launch_agent_management_thread()) {
1731 /* Don't start this thread if kernel tracing is not requested nor root */
1732 if (is_root
&& !config
.no_kernel
) {
1733 /* Create kernel thread to manage kernel event */
1734 if (!launch_kernel_management_thread(kernel_poll_pipe
[0])) {
1740 /* Load sessions. */
1741 ret
= config_load_session(config
.load_session_path
.value
,
1744 ERR("Session load failed: %s", error_get_str(ret
));
1749 /* Initialization completed. */
1750 sessiond_signal_parents();
1753 * This is where we start awaiting program completion (e.g. through
1754 * signal that asks threads to teardown).
1757 /* Initiate teardown once activity occurs on the quit pipe. */
1758 sessiond_wait_for_quit_pipe(-1);
1762 * Ensure that the client thread is no longer accepting new commands,
1763 * which could cause new sessions to be created.
1765 if (client_thread
) {
1766 lttng_thread_shutdown(client_thread
);
1767 lttng_thread_put(client_thread
);
1770 destroy_all_sessions_and_wait();
1772 if (register_apps_thread
) {
1773 lttng_thread_shutdown(register_apps_thread
);
1774 lttng_thread_put(register_apps_thread
);
1776 lttng_thread_list_shutdown_orphans();
1779 * Wait for all pending call_rcu work to complete before tearing
1780 * down data structures. call_rcu worker may be trying to
1781 * perform lookups in those structures.
1785 * sessiond_cleanup() is called when no other thread is running, except
1786 * the ht_cleanup thread, which is needed to destroy the hash tables.
1788 rcu_thread_online();
1791 if (notification_thread
) {
1792 lttng_thread_shutdown(notification_thread
);
1793 lttng_thread_put(notification_thread
);
1797 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1798 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1799 * the queue is empty before shutting down the clean-up thread.
1803 if (ht_cleanup_thread
) {
1804 lttng_thread_shutdown(ht_cleanup_thread
);
1805 lttng_thread_put(ht_cleanup_thread
);
1808 rcu_thread_offline();
1809 rcu_unregister_thread();
1811 if (rotation_thread_handle
) {
1812 rotation_thread_handle_destroy(rotation_thread_handle
);
1816 * After the rotation and timer thread have quit, we can safely destroy
1817 * the rotation_timer_queue.
1819 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1821 * The teardown of the notification system is performed after the
1822 * session daemon's teardown in order to allow it to be notified
1823 * of the active session and channels at the moment of the teardown.
1825 if (notification_thread_handle
) {
1826 notification_thread_handle_destroy(notification_thread_handle
);
1828 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1829 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1830 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1832 if (health_sessiond
) {
1833 health_app_destroy(health_sessiond
);
1835 exit_create_run_as_worker_cleanup
:
1837 sessiond_cleanup_lock_file();
1838 sessiond_cleanup_options();
1840 exit_set_signal_handler
: