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"
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;
99 /* Set to 1 when a SIGUSR1 signal is received. */
100 static int recv_child_signal
;
102 /* Command line options */
103 static const struct option long_options
[] = {
104 { "client-sock", required_argument
, 0, 'c' },
105 { "apps-sock", required_argument
, 0, 'a' },
106 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
107 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
108 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
109 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
110 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
111 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
112 { "consumerd32-path", required_argument
, 0, '\0' },
113 { "consumerd32-libdir", required_argument
, 0, '\0' },
114 { "consumerd64-path", required_argument
, 0, '\0' },
115 { "consumerd64-libdir", required_argument
, 0, '\0' },
116 { "daemonize", no_argument
, 0, 'd' },
117 { "background", no_argument
, 0, 'b' },
118 { "sig-parent", no_argument
, 0, 'S' },
119 { "help", no_argument
, 0, 'h' },
120 { "group", required_argument
, 0, 'g' },
121 { "version", no_argument
, 0, 'V' },
122 { "quiet", no_argument
, 0, 'q' },
123 { "verbose", no_argument
, 0, 'v' },
124 { "verbose-consumer", no_argument
, 0, '\0' },
125 { "no-kernel", no_argument
, 0, '\0' },
126 { "pidfile", required_argument
, 0, 'p' },
127 { "agent-tcp-port", required_argument
, 0, '\0' },
128 { "config", required_argument
, 0, 'f' },
129 { "load", required_argument
, 0, 'l' },
130 { "kmod-probes", required_argument
, 0, '\0' },
131 { "extra-kmod-probes", required_argument
, 0, '\0' },
135 /* Command line options to ignore from configuration file */
136 static const char *config_ignore_options
[] = { "help", "version", "config" };
139 * This pipe is used to inform the thread managing application communication
140 * that a command is queued and ready to be processed.
142 static int apps_cmd_pipe
[2] = { -1, -1 };
143 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
146 * UST registration command queue. This queue is tied with a futex and uses a N
147 * wakers / 1 waiter implemented and detailed in futex.c/.h
149 * The thread_registration_apps and thread_dispatch_ust_registration uses this
150 * queue along with the wait/wake scheme. The thread_manage_apps receives down
151 * the line new application socket and monitors it for any I/O error or clean
152 * close that triggers an unregistration of the application.
154 static struct ust_cmd_queue ust_cmd_queue
;
157 * Section name to look for in the daemon configuration file.
159 static const char * const config_section_name
= "sessiond";
161 /* Am I root or not. Set to 1 if the daemon is running as root */
165 * Stop all threads by closing the thread quit pipe.
167 static void stop_threads(void)
171 /* Stopping all threads */
172 DBG("Terminating all threads");
173 ret
= sessiond_notify_quit_pipe();
175 ERR("write error on thread quit pipe");
180 * Close every consumer sockets.
182 static void close_consumer_sockets(void)
186 if (kconsumer_data
.err_sock
>= 0) {
187 ret
= close(kconsumer_data
.err_sock
);
189 PERROR("kernel consumer err_sock close");
192 if (ustconsumer32_data
.err_sock
>= 0) {
193 ret
= close(ustconsumer32_data
.err_sock
);
195 PERROR("UST consumerd32 err_sock close");
198 if (ustconsumer64_data
.err_sock
>= 0) {
199 ret
= close(ustconsumer64_data
.err_sock
);
201 PERROR("UST consumerd64 err_sock close");
204 if (kconsumer_data
.cmd_sock
>= 0) {
205 ret
= close(kconsumer_data
.cmd_sock
);
207 PERROR("kernel consumer cmd_sock close");
210 if (ustconsumer32_data
.cmd_sock
>= 0) {
211 ret
= close(ustconsumer32_data
.cmd_sock
);
213 PERROR("UST consumerd32 cmd_sock close");
216 if (ustconsumer64_data
.cmd_sock
>= 0) {
217 ret
= close(ustconsumer64_data
.cmd_sock
);
219 PERROR("UST consumerd64 cmd_sock close");
222 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
223 ret
= close(kconsumer_data
.channel_monitor_pipe
);
225 PERROR("kernel consumer channel monitor pipe close");
228 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
229 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
231 PERROR("UST consumerd32 channel monitor pipe close");
234 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
235 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
237 PERROR("UST consumerd64 channel monitor pipe close");
243 * Wait on consumer process termination.
245 * Need to be called with the consumer data lock held or from a context
246 * ensuring no concurrent access to data (e.g: cleanup).
248 static void wait_consumer(struct consumer_data
*consumer_data
)
253 if (consumer_data
->pid
<= 0) {
257 DBG("Waiting for complete teardown of consumerd (PID: %d)",
259 ret
= waitpid(consumer_data
->pid
, &status
, 0);
261 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
262 } else if (!WIFEXITED(status
)) {
263 ERR("consumerd termination with error: %d",
266 consumer_data
->pid
= 0;
270 * Cleanup the session daemon's data structures.
272 static void sessiond_cleanup(void)
275 struct ltt_session_list
*session_list
= session_get_list();
277 DBG("Cleanup sessiond");
280 * Close the thread quit pipe. It has already done its job,
281 * since we are now called.
283 sessiond_close_quit_pipe();
284 utils_close_pipe(apps_cmd_pipe
);
285 utils_close_pipe(apps_cmd_notify_pipe
);
286 utils_close_pipe(kernel_poll_pipe
);
288 ret
= remove(config
.pid_file_path
.value
);
290 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
293 DBG("Removing sessiond and consumerd content of directory %s",
294 config
.rundir
.value
);
297 DBG("Removing %s", config
.pid_file_path
.value
);
298 (void) unlink(config
.pid_file_path
.value
);
300 DBG("Removing %s", config
.agent_port_file_path
.value
);
301 (void) unlink(config
.agent_port_file_path
.value
);
304 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
305 (void) unlink(kconsumer_data
.err_unix_sock_path
);
307 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
308 (void) rmdir(config
.kconsumerd_path
.value
);
310 /* ust consumerd 32 */
311 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
312 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
314 DBG("Removing directory %s", config
.consumerd32_path
.value
);
315 (void) rmdir(config
.consumerd32_path
.value
);
317 /* ust consumerd 64 */
318 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
319 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
321 DBG("Removing directory %s", config
.consumerd64_path
.value
);
322 (void) rmdir(config
.consumerd64_path
.value
);
324 pthread_mutex_destroy(&session_list
->lock
);
326 DBG("Cleaning up all agent apps");
327 agent_app_ht_clean();
328 DBG("Closing all UST sockets");
329 ust_app_clean_list();
330 buffer_reg_destroy_registries();
332 close_consumer_sockets();
334 wait_consumer(&kconsumer_data
);
335 wait_consumer(&ustconsumer64_data
);
336 wait_consumer(&ustconsumer32_data
);
338 if (is_root
&& !config
.no_kernel
) {
339 cleanup_kernel_tracer();
343 * We do NOT rmdir rundir because there are other processes
344 * using it, for instance lttng-relayd, which can start in
345 * parallel with this teardown.
350 * Cleanup the daemon's option data structures.
352 static void sessiond_cleanup_options(void)
354 DBG("Cleaning up options");
356 sessiond_config_fini(&config
);
358 run_as_destroy_worker();
361 static int string_match(const char *str1
, const char *str2
)
363 return (str1
&& str2
) && !strcmp(str1
, str2
);
367 * Take an option from the getopt output and set it in the right variable to be
370 * Return 0 on success else a negative value.
372 static int set_option(int opt
, const char *arg
, const char *optname
)
376 if (string_match(optname
, "client-sock") || opt
== 'c') {
377 if (!arg
|| *arg
== '\0') {
381 if (lttng_is_setuid_setgid()) {
382 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
383 "-c, --client-sock");
385 config_string_set(&config
.client_unix_sock_path
,
387 if (!config
.client_unix_sock_path
.value
) {
392 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
393 if (!arg
|| *arg
== '\0') {
397 if (lttng_is_setuid_setgid()) {
398 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
401 config_string_set(&config
.apps_unix_sock_path
,
403 if (!config
.apps_unix_sock_path
.value
) {
408 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
409 config
.daemonize
= true;
410 } else if (string_match(optname
, "background") || opt
== 'b') {
411 config
.background
= true;
412 } else if (string_match(optname
, "group") || opt
== 'g') {
413 if (!arg
|| *arg
== '\0') {
417 if (lttng_is_setuid_setgid()) {
418 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
421 config_string_set(&config
.tracing_group_name
,
423 if (!config
.tracing_group_name
.value
) {
428 } else if (string_match(optname
, "help") || opt
== 'h') {
429 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
431 ERR("Cannot show --help for `lttng-sessiond`");
434 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
435 } else if (string_match(optname
, "version") || opt
== 'V') {
436 fprintf(stdout
, "%s\n", VERSION
);
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
);
793 * daemon configuration loading and argument parsing
795 static int set_options(int argc
, char **argv
)
797 int ret
= 0, c
= 0, option_index
= 0;
798 int orig_optopt
= optopt
, orig_optind
= optind
;
800 const char *config_path
= NULL
;
802 optstring
= utils_generate_optstring(long_options
,
803 sizeof(long_options
) / sizeof(struct option
));
809 /* Check for the --config option */
810 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
811 &option_index
)) != -1) {
815 } else if (c
!= 'f') {
816 /* if not equal to --config option. */
820 if (lttng_is_setuid_setgid()) {
821 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
824 config_path
= utils_expand_path(optarg
);
826 ERR("Failed to resolve path: %s", optarg
);
831 ret
= config_get_section_entries(config_path
, config_section_name
,
832 config_entry_handler
, NULL
);
835 ERR("Invalid configuration option at line %i", ret
);
841 /* Reset getopt's global state */
842 optopt
= orig_optopt
;
843 optind
= orig_optind
;
847 * getopt_long() will not set option_index if it encounters a
850 c
= getopt_long(argc
, argv
, optstring
, long_options
,
857 * Pass NULL as the long option name if popt left the index
860 ret
= set_option(c
, optarg
,
861 option_index
< 0 ? NULL
:
862 long_options
[option_index
].name
);
874 * Create lockfile using the rundir and return its fd.
876 static int create_lockfile(void)
878 return utils_create_lock_file(config
.lock_file_path
.value
);
882 * Check if the global socket is available, and if a daemon is answering at the
883 * other side. If yes, error is returned.
885 * Also attempts to create and hold the lock file.
887 static int check_existing_daemon(void)
891 /* Is there anybody out there ? */
892 if (lttng_session_daemon_alive()) {
897 lockfile_fd
= create_lockfile();
898 if (lockfile_fd
< 0) {
906 static void sessiond_cleanup_lock_file(void)
911 * Cleanup lock file by deleting it and finaly closing it which will
912 * release the file system lock.
914 if (lockfile_fd
>= 0) {
915 ret
= remove(config
.lock_file_path
.value
);
917 PERROR("remove lock file");
919 ret
= close(lockfile_fd
);
921 PERROR("close lock file");
927 * Set the tracing group gid onto the client socket.
929 * Race window between mkdir and chown is OK because we are going from more
930 * permissive (root.root) to less permissive (root.tracing).
932 static int set_permissions(char *rundir
)
937 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true, &gid
);
939 /* Default to root group. */
943 /* Set lttng run dir */
944 ret
= chown(rundir
, 0, gid
);
946 ERR("Unable to set group on %s", rundir
);
951 * Ensure all applications and tracing group can search the run
952 * dir. Allow everyone to read the directory, since it does not
953 * buy us anything to hide its content.
955 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
957 ERR("Unable to set permissions on %s", rundir
);
961 /* lttng client socket path */
962 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
964 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
968 /* kconsumer error socket path */
969 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
971 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
975 /* 64-bit ustconsumer error socket path */
976 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
978 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
982 /* 32-bit ustconsumer compat32 error socket path */
983 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
985 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
989 DBG("All permissions are set");
995 * Create the lttng run directory needed for all global sockets and pipe.
997 static int create_lttng_rundir(void)
1001 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
1003 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
1005 if (errno
!= EEXIST
) {
1006 ERR("Unable to create %s", config
.rundir
.value
);
1018 * Setup sockets and directory needed by the consumerds' communication with the
1021 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1026 switch (consumer_data
->type
) {
1027 case LTTNG_CONSUMER_KERNEL
:
1028 path
= config
.kconsumerd_path
.value
;
1030 case LTTNG_CONSUMER64_UST
:
1031 path
= config
.consumerd64_path
.value
;
1033 case LTTNG_CONSUMER32_UST
:
1034 path
= config
.consumerd32_path
.value
;
1037 ERR("Consumer type unknown");
1043 DBG2("Creating consumer directory: %s", path
);
1045 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1046 if (ret
< 0 && errno
!= EEXIST
) {
1048 ERR("Failed to create %s", path
);
1054 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
1057 /* Default to root group. */
1061 ret
= chown(path
, 0, gid
);
1063 ERR("Unable to set group on %s", path
);
1069 /* Create the consumerd error unix socket */
1070 consumer_data
->err_sock
=
1071 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1072 if (consumer_data
->err_sock
< 0) {
1073 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1079 * Set the CLOEXEC flag. Return code is useless because either way, the
1082 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1084 PERROR("utils_set_fd_cloexec");
1085 /* continue anyway */
1088 /* File permission MUST be 660 */
1089 ret
= chmod(consumer_data
->err_unix_sock_path
,
1090 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1092 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1102 * Signal handler for the daemon
1104 * Simply stop all worker threads, leaving main() return gracefully after
1105 * joining all threads and calling cleanup().
1107 static void sighandler(int sig
)
1111 DBG("SIGINT caught");
1115 DBG("SIGTERM caught");
1119 CMM_STORE_SHARED(recv_child_signal
, 1);
1127 * Setup signal handler for :
1128 * SIGINT, SIGTERM, SIGPIPE
1130 static int set_signal_handler(void)
1133 struct sigaction sa
;
1136 if ((ret
= sigemptyset(&sigset
)) < 0) {
1137 PERROR("sigemptyset");
1141 sa
.sa_mask
= sigset
;
1144 sa
.sa_handler
= sighandler
;
1145 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1146 PERROR("sigaction");
1150 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1151 PERROR("sigaction");
1155 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1156 PERROR("sigaction");
1160 sa
.sa_handler
= SIG_IGN
;
1161 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1162 PERROR("sigaction");
1166 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1172 * Set open files limit to unlimited. This daemon can open a large number of
1173 * file descriptors in order to consume multiple kernel traces.
1175 static void set_ulimit(void)
1180 /* The kernel does not allow an infinite limit for open files */
1181 lim
.rlim_cur
= 65535;
1182 lim
.rlim_max
= 65535;
1184 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1186 PERROR("failed to set open files limit");
1190 static int write_pidfile(void)
1192 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1195 static int set_clock_plugin_env(void)
1198 char *env_value
= NULL
;
1200 if (!config
.lttng_ust_clock_plugin
.value
) {
1204 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1205 config
.lttng_ust_clock_plugin
.value
);
1211 ret
= putenv(env_value
);
1214 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1218 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1219 config
.lttng_ust_clock_plugin
.value
);
1224 static void destroy_all_sessions_and_wait(void)
1226 struct ltt_session
*session
, *tmp
;
1227 struct ltt_session_list
*session_list
;
1229 session_list
= session_get_list();
1230 DBG("Initiating destruction of all sessions");
1232 if (!session_list
) {
1236 session_lock_list();
1237 /* Initiate the destruction of all sessions. */
1238 cds_list_for_each_entry_safe(session
, tmp
,
1239 &session_list
->head
, list
) {
1240 if (!session_get(session
)) {
1244 session_lock(session
);
1245 if (session
->destroyed
) {
1246 goto unlock_session
;
1248 (void) cmd_stop_trace(session
);
1249 (void) cmd_destroy_session(session
, notification_thread_handle
,
1252 session_unlock(session
);
1253 session_put(session
);
1255 session_unlock_list();
1257 /* Wait for the destruction of all sessions to complete. */
1258 DBG("Waiting for the destruction of all sessions to complete");
1259 session_list_wait_empty();
1260 DBG("Destruction of all sessions completed");
1263 static int run_as_worker_post_fork_cleanup(void *data
)
1265 struct sessiond_config
*sessiond_config
= data
;
1267 sessiond_config_fini(sessiond_config
);
1271 static int launch_run_as_worker(const char *procname
)
1274 * Clean-up before forking the run-as worker. Any dynamically
1275 * allocated memory of which the worker is not aware will
1276 * be leaked as the process forks a run-as worker (and performs
1277 * no exec*()). The same would apply to any opened fd.
1279 return run_as_create_worker(procname
, run_as_worker_post_fork_cleanup
,
1283 static void sessiond_uuid_log(void)
1285 char uuid_str
[UUID_STR_LEN
];
1287 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
1288 DBG("Starting lttng-sessiond {%s}", uuid_str
);
1294 int main(int argc
, char **argv
)
1296 int ret
= 0, retval
= 0;
1297 const char *env_app_timeout
;
1298 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1299 *ust64_channel_monitor_pipe
= NULL
,
1300 *kernel_channel_monitor_pipe
= NULL
;
1301 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1302 struct timer_thread_parameters timer_thread_parameters
;
1303 /* Rotation thread handle. */
1304 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1305 /* Queue of rotation jobs populated by the sessiond-timer. */
1306 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1307 struct lttng_thread
*client_thread
= NULL
;
1308 struct lttng_thread
*notification_thread
= NULL
;
1309 struct lttng_thread
*register_apps_thread
= NULL
;
1311 init_kernel_workarounds();
1313 rcu_register_thread();
1315 if (set_signal_handler()) {
1317 goto exit_set_signal_handler
;
1320 if (timer_signal_init()) {
1322 goto exit_set_signal_handler
;
1325 page_size
= sysconf(_SC_PAGESIZE
);
1326 if (page_size
< 0) {
1327 PERROR("sysconf _SC_PAGESIZE");
1328 page_size
= LONG_MAX
;
1329 WARN("Fallback page size to %ld", page_size
);
1332 ret
= sessiond_config_init(&config
);
1335 goto exit_set_signal_handler
;
1339 * Init config from environment variables.
1340 * Command line option override env configuration per-doc. Do env first.
1342 sessiond_config_apply_env_config(&config
);
1345 * Parse arguments and load the daemon configuration file.
1347 * We have an exit_options exit path to free memory reserved by
1348 * set_options. This is needed because the rest of sessiond_cleanup()
1349 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1350 * depends on set_options.
1353 if (set_options(argc
, argv
)) {
1359 * Resolve all paths received as arguments, configuration option, or
1360 * through environment variable as absolute paths. This is necessary
1361 * since daemonizing causes the sessiond's current working directory
1364 ret
= sessiond_config_resolve_paths(&config
);
1370 lttng_opt_verbose
= config
.verbose
;
1371 lttng_opt_quiet
= config
.quiet
;
1372 kconsumer_data
.err_unix_sock_path
=
1373 config
.kconsumerd_err_unix_sock_path
.value
;
1374 kconsumer_data
.cmd_unix_sock_path
=
1375 config
.kconsumerd_cmd_unix_sock_path
.value
;
1376 ustconsumer32_data
.err_unix_sock_path
=
1377 config
.consumerd32_err_unix_sock_path
.value
;
1378 ustconsumer32_data
.cmd_unix_sock_path
=
1379 config
.consumerd32_cmd_unix_sock_path
.value
;
1380 ustconsumer64_data
.err_unix_sock_path
=
1381 config
.consumerd64_err_unix_sock_path
.value
;
1382 ustconsumer64_data
.cmd_unix_sock_path
=
1383 config
.consumerd64_cmd_unix_sock_path
.value
;
1384 set_clock_plugin_env();
1386 sessiond_config_log(&config
);
1387 sessiond_uuid_log();
1389 if (create_lttng_rundir()) {
1394 /* Abort launch if a session daemon is already running. */
1395 if (check_existing_daemon()) {
1396 ERR("A session daemon is already running.");
1402 if (config
.daemonize
|| config
.background
) {
1405 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
1406 !config
.background
);
1413 * We are in the child. Make sure all other file descriptors are
1414 * closed, in case we are called with more opened file
1415 * descriptors than the standard ones and the lock file.
1417 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1418 if (i
== lockfile_fd
) {
1425 if (launch_run_as_worker(argv
[0]) < 0) {
1426 goto exit_create_run_as_worker_cleanup
;
1430 * Starting from here, we can create threads. This needs to be after
1431 * lttng_daemonize due to RCU.
1435 * Initialize the health check subsystem. This call should set the
1436 * appropriate time values.
1438 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1439 if (!health_sessiond
) {
1440 PERROR("health_app_create error");
1445 /* Create thread to clean up RCU hash tables */
1446 ht_cleanup_thread
= launch_ht_cleanup_thread();
1447 if (!ht_cleanup_thread
) {
1452 /* Create thread quit pipe */
1453 if (sessiond_init_thread_quit_pipe()) {
1458 /* Check if daemon is UID = 0 */
1459 is_root
= !getuid();
1461 /* Create global run dir with root access */
1463 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1464 if (!kernel_channel_monitor_pipe
) {
1465 ERR("Failed to create kernel consumer channel monitor pipe");
1469 kconsumer_data
.channel_monitor_pipe
=
1470 lttng_pipe_release_writefd(
1471 kernel_channel_monitor_pipe
);
1472 if (kconsumer_data
.channel_monitor_pipe
< 0) {
1478 /* Set consumer initial state */
1479 kernel_consumerd_state
= CONSUMER_STOPPED
;
1480 ust_consumerd_state
= CONSUMER_STOPPED
;
1482 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1483 if (!ust32_channel_monitor_pipe
) {
1484 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1488 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1489 ust32_channel_monitor_pipe
);
1490 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
1496 * The rotation_thread_timer_queue structure is shared between the
1497 * sessiond timer thread and the rotation thread. The main thread keeps
1498 * its ownership and destroys it when both threads have been joined.
1500 rotation_timer_queue
= rotation_thread_timer_queue_create();
1501 if (!rotation_timer_queue
) {
1505 timer_thread_parameters
.rotation_thread_job_queue
=
1506 rotation_timer_queue
;
1508 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1509 if (!ust64_channel_monitor_pipe
) {
1510 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1514 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1515 ust64_channel_monitor_pipe
);
1516 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
1522 * Init UST app hash table. Alloc hash table before this point since
1523 * cleanup() can get called after that point.
1525 if (ust_app_ht_alloc()) {
1526 ERR("Failed to allocate UST app hash table");
1532 * Initialize agent app hash table. We allocate the hash table here
1533 * since cleanup() can get called after this point.
1535 if (agent_app_ht_alloc()) {
1536 ERR("Failed to allocate Agent app hash table");
1542 * These actions must be executed as root. We do that *after* setting up
1543 * the sockets path because we MUST make the check for another daemon using
1544 * those paths *before* trying to set the kernel consumer sockets and init
1548 if (set_consumer_sockets(&kconsumer_data
)) {
1553 /* Setup kernel tracer */
1554 if (!config
.no_kernel
) {
1555 init_kernel_tracer();
1558 /* Set ulimit for open files */
1561 /* init lttng_fd tracking must be done after set_ulimit. */
1564 if (set_consumer_sockets(&ustconsumer64_data
)) {
1569 if (set_consumer_sockets(&ustconsumer32_data
)) {
1574 /* Get parent pid if -S, --sig-parent is specified. */
1575 if (config
.sig_parent
) {
1579 /* Setup the kernel pipe for waking up the kernel thread */
1580 if (is_root
&& !config
.no_kernel
) {
1581 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
1587 /* Setup the thread apps communication pipe. */
1588 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
1593 /* Setup the thread apps notify communication pipe. */
1594 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
1599 /* Initialize global buffer per UID and PID registry. */
1600 buffer_reg_init_uid_registry();
1601 buffer_reg_init_pid_registry();
1603 /* Init UST command queue. */
1604 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1608 /* Check for the application socket timeout env variable. */
1609 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
1610 if (env_app_timeout
) {
1611 config
.app_socket_timeout
= atoi(env_app_timeout
);
1613 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
1616 ret
= write_pidfile();
1618 ERR("Error in write_pidfile");
1623 /* Initialize communication library */
1625 /* Initialize TCP timeout values */
1626 lttcomm_inet_init();
1628 /* Create health-check thread. */
1629 if (!launch_health_management_thread()) {
1634 /* notification_thread_data acquires the pipes' read side. */
1635 notification_thread_handle
= notification_thread_handle_create(
1636 ust32_channel_monitor_pipe
,
1637 ust64_channel_monitor_pipe
,
1638 kernel_channel_monitor_pipe
);
1639 if (!notification_thread_handle
) {
1641 ERR("Failed to create notification thread shared data");
1645 /* Create notification thread. */
1646 notification_thread
= launch_notification_thread(
1647 notification_thread_handle
);
1648 if (!notification_thread
) {
1653 /* Create timer thread. */
1654 if (!launch_timer_thread(&timer_thread_parameters
)) {
1659 /* rotation_thread_data acquires the pipes' read side. */
1660 rotation_thread_handle
= rotation_thread_handle_create(
1661 rotation_timer_queue
,
1662 notification_thread_handle
);
1663 if (!rotation_thread_handle
) {
1665 ERR("Failed to create rotation thread shared data");
1670 /* Create rotation thread. */
1671 if (!launch_rotation_thread(rotation_thread_handle
)) {
1676 /* Create thread to manage the client socket */
1677 client_thread
= launch_client_thread();
1678 if (!client_thread
) {
1683 /* Set credentials of the client socket and rundir */
1684 if (is_root
&& set_permissions(config
.rundir
.value
)) {
1689 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
1690 apps_cmd_notify_pipe
[1])) {
1695 /* Create thread to manage application registration. */
1696 register_apps_thread
= launch_application_registration_thread(
1698 if (!register_apps_thread
) {
1703 /* Create thread to manage application socket */
1704 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
1709 /* Create thread to manage application notify socket */
1710 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
1715 /* Create agent management thread. */
1716 if (!launch_agent_management_thread()) {
1721 /* Don't start this thread if kernel tracing is not requested nor root */
1722 if (is_root
&& !config
.no_kernel
) {
1723 /* Create kernel thread to manage kernel event */
1724 if (!launch_kernel_management_thread(kernel_poll_pipe
[0])) {
1730 /* Load sessions. */
1731 ret
= config_load_session(config
.load_session_path
.value
,
1734 ERR("Session load failed: %s", error_get_str(ret
));
1739 /* Initialization completed. */
1740 sessiond_signal_parents();
1743 * This is where we start awaiting program completion (e.g. through
1744 * signal that asks threads to teardown).
1747 /* Initiate teardown once activity occurs on the quit pipe. */
1748 sessiond_wait_for_quit_pipe(-1);
1752 * Ensure that the client thread is no longer accepting new commands,
1753 * which could cause new sessions to be created.
1755 if (client_thread
) {
1756 lttng_thread_shutdown(client_thread
);
1757 lttng_thread_put(client_thread
);
1760 destroy_all_sessions_and_wait();
1762 if (register_apps_thread
) {
1763 lttng_thread_shutdown(register_apps_thread
);
1764 lttng_thread_put(register_apps_thread
);
1766 lttng_thread_list_shutdown_orphans();
1769 * Wait for all pending call_rcu work to complete before tearing
1770 * down data structures. call_rcu worker may be trying to
1771 * perform lookups in those structures.
1775 * sessiond_cleanup() is called when no other thread is running, except
1776 * the ht_cleanup thread, which is needed to destroy the hash tables.
1778 rcu_thread_online();
1781 if (notification_thread
) {
1782 lttng_thread_shutdown(notification_thread
);
1783 lttng_thread_put(notification_thread
);
1787 * Ensure all prior call_rcu are done. call_rcu callbacks may push
1788 * hash tables to the ht_cleanup thread. Therefore, we ensure that
1789 * the queue is empty before shutting down the clean-up thread.
1793 if (ht_cleanup_thread
) {
1794 lttng_thread_shutdown(ht_cleanup_thread
);
1795 lttng_thread_put(ht_cleanup_thread
);
1798 rcu_thread_offline();
1799 rcu_unregister_thread();
1801 if (rotation_thread_handle
) {
1802 rotation_thread_handle_destroy(rotation_thread_handle
);
1806 * After the rotation and timer thread have quit, we can safely destroy
1807 * the rotation_timer_queue.
1809 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
1811 * The teardown of the notification system is performed after the
1812 * session daemon's teardown in order to allow it to be notified
1813 * of the active session and channels at the moment of the teardown.
1815 if (notification_thread_handle
) {
1816 notification_thread_handle_destroy(notification_thread_handle
);
1818 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
1819 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
1820 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
1822 health_app_destroy(health_sessiond
);
1823 exit_create_run_as_worker_cleanup
:
1825 sessiond_cleanup_lock_file();
1826 sessiond_cleanup_options();
1828 exit_set_signal_handler
: