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 "ust-thread.h"
73 #include "agent-thread.h"
75 #include "load-session-thread.h"
76 #include "notification-thread.h"
77 #include "notification-thread-commands.h"
78 #include "rotation-thread.h"
79 #include "lttng-syscall.h"
81 #include "ht-cleanup.h"
82 #include "sessiond-config.h"
88 #include "manage-apps.h"
89 #include "manage-kernel.h"
91 static const char *help_msg
=
92 #ifdef LTTNG_EMBED_HELP
93 #include <lttng-sessiond.8.h>
100 static int lockfile_fd
= -1;
102 /* Set to 1 when a SIGUSR1 signal is received. */
103 static int recv_child_signal
;
105 /* Command line options */
106 static const struct option long_options
[] = {
107 { "client-sock", required_argument
, 0, 'c' },
108 { "apps-sock", required_argument
, 0, 'a' },
109 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
110 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
111 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
112 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
113 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
114 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
115 { "consumerd32-path", required_argument
, 0, '\0' },
116 { "consumerd32-libdir", required_argument
, 0, '\0' },
117 { "consumerd64-path", required_argument
, 0, '\0' },
118 { "consumerd64-libdir", required_argument
, 0, '\0' },
119 { "daemonize", no_argument
, 0, 'd' },
120 { "background", no_argument
, 0, 'b' },
121 { "sig-parent", no_argument
, 0, 'S' },
122 { "help", no_argument
, 0, 'h' },
123 { "group", required_argument
, 0, 'g' },
124 { "version", no_argument
, 0, 'V' },
125 { "quiet", no_argument
, 0, 'q' },
126 { "verbose", no_argument
, 0, 'v' },
127 { "verbose-consumer", no_argument
, 0, '\0' },
128 { "no-kernel", no_argument
, 0, '\0' },
129 { "pidfile", required_argument
, 0, 'p' },
130 { "agent-tcp-port", required_argument
, 0, '\0' },
131 { "config", required_argument
, 0, 'f' },
132 { "load", required_argument
, 0, 'l' },
133 { "kmod-probes", required_argument
, 0, '\0' },
134 { "extra-kmod-probes", required_argument
, 0, '\0' },
138 /* Command line options to ignore from configuration file */
139 static const char *config_ignore_options
[] = { "help", "version", "config" };
142 * This pipe is used to inform the thread managing application communication
143 * that a command is queued and ready to be processed.
145 static int apps_cmd_pipe
[2] = { -1, -1 };
146 static int apps_cmd_notify_pipe
[2] = { -1, -1 };
148 /* Pthread, Mutexes and Semaphores */
149 static pthread_t load_session_thread
;
152 * UST registration command queue. This queue is tied with a futex and uses a N
153 * wakers / 1 waiter implemented and detailed in futex.c/.h
155 * The thread_registration_apps and thread_dispatch_ust_registration uses this
156 * queue along with the wait/wake scheme. The thread_manage_apps receives down
157 * the line new application socket and monitors it for any I/O error or clean
158 * close that triggers an unregistration of the application.
160 static struct ust_cmd_queue ust_cmd_queue
;
162 static const char *module_proc_lttng
= "/proc/lttng";
164 /* Load session thread information to operate. */
165 static struct load_session_thread_data
*load_info
;
168 * Section name to look for in the daemon configuration file.
170 static const char * const config_section_name
= "sessiond";
172 /* Am I root or not. Set to 1 if the daemon is running as root */
176 * Stop all threads by closing the thread quit pipe.
178 static void stop_threads(void)
182 /* Stopping all threads */
183 DBG("Terminating all threads");
184 ret
= sessiond_notify_quit_pipe();
186 ERR("write error on thread quit pipe");
191 * Close every consumer sockets.
193 static void close_consumer_sockets(void)
197 if (kconsumer_data
.err_sock
>= 0) {
198 ret
= close(kconsumer_data
.err_sock
);
200 PERROR("kernel consumer err_sock close");
203 if (ustconsumer32_data
.err_sock
>= 0) {
204 ret
= close(ustconsumer32_data
.err_sock
);
206 PERROR("UST consumerd32 err_sock close");
209 if (ustconsumer64_data
.err_sock
>= 0) {
210 ret
= close(ustconsumer64_data
.err_sock
);
212 PERROR("UST consumerd64 err_sock close");
215 if (kconsumer_data
.cmd_sock
>= 0) {
216 ret
= close(kconsumer_data
.cmd_sock
);
218 PERROR("kernel consumer cmd_sock close");
221 if (ustconsumer32_data
.cmd_sock
>= 0) {
222 ret
= close(ustconsumer32_data
.cmd_sock
);
224 PERROR("UST consumerd32 cmd_sock close");
227 if (ustconsumer64_data
.cmd_sock
>= 0) {
228 ret
= close(ustconsumer64_data
.cmd_sock
);
230 PERROR("UST consumerd64 cmd_sock close");
233 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
234 ret
= close(kconsumer_data
.channel_monitor_pipe
);
236 PERROR("kernel consumer channel monitor pipe close");
239 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
240 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
242 PERROR("UST consumerd32 channel monitor pipe close");
245 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
246 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
248 PERROR("UST consumerd64 channel monitor pipe close");
254 * Wait on consumer process termination.
256 * Need to be called with the consumer data lock held or from a context
257 * ensuring no concurrent access to data (e.g: cleanup).
259 static void wait_consumer(struct consumer_data
*consumer_data
)
264 if (consumer_data
->pid
<= 0) {
268 DBG("Waiting for complete teardown of consumerd (PID: %d)",
270 ret
= waitpid(consumer_data
->pid
, &status
, 0);
272 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
273 } else if (!WIFEXITED(status
)) {
274 ERR("consumerd termination with error: %d",
277 consumer_data
->pid
= 0;
281 * Cleanup the session daemon's data structures.
283 static void sessiond_cleanup(void)
286 struct ltt_session_list
*session_list
= session_get_list();
288 DBG("Cleanup sessiond");
291 * Close the thread quit pipe. It has already done its job,
292 * since we are now called.
294 sessiond_close_quit_pipe();
295 utils_close_pipe(apps_cmd_pipe
);
296 utils_close_pipe(apps_cmd_notify_pipe
);
297 utils_close_pipe(kernel_poll_pipe
);
299 ret
= remove(config
.pid_file_path
.value
);
301 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
304 DBG("Removing sessiond and consumerd content of directory %s",
305 config
.rundir
.value
);
308 DBG("Removing %s", config
.pid_file_path
.value
);
309 (void) unlink(config
.pid_file_path
.value
);
311 DBG("Removing %s", config
.agent_port_file_path
.value
);
312 (void) unlink(config
.agent_port_file_path
.value
);
315 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
316 (void) unlink(kconsumer_data
.err_unix_sock_path
);
318 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
319 (void) rmdir(config
.kconsumerd_path
.value
);
321 /* ust consumerd 32 */
322 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
323 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
325 DBG("Removing directory %s", config
.consumerd32_path
.value
);
326 (void) rmdir(config
.consumerd32_path
.value
);
328 /* ust consumerd 64 */
329 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
330 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
332 DBG("Removing directory %s", config
.consumerd64_path
.value
);
333 (void) rmdir(config
.consumerd64_path
.value
);
335 pthread_mutex_destroy(&session_list
->lock
);
337 wait_consumer(&kconsumer_data
);
338 wait_consumer(&ustconsumer64_data
);
339 wait_consumer(&ustconsumer32_data
);
341 DBG("Cleaning up all agent apps");
342 agent_app_ht_clean();
344 DBG("Closing all UST sockets");
345 ust_app_clean_list();
346 buffer_reg_destroy_registries();
348 if (is_root
&& !config
.no_kernel
) {
349 DBG2("Closing kernel fd");
350 if (kernel_tracer_fd
>= 0) {
351 ret
= close(kernel_tracer_fd
);
356 DBG("Unloading kernel modules");
357 modprobe_remove_lttng_all();
361 close_consumer_sockets();
364 load_session_destroy_data(load_info
);
369 * We do NOT rmdir rundir because there are other processes
370 * using it, for instance lttng-relayd, which can start in
371 * parallel with this teardown.
376 * Cleanup the daemon's option data structures.
378 static void sessiond_cleanup_options(void)
380 DBG("Cleaning up options");
382 sessiond_config_fini(&config
);
384 run_as_destroy_worker();
388 * Signal pthread condition of the consumer data that the thread.
390 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
392 pthread_mutex_lock(&data
->cond_mutex
);
395 * The state is set before signaling. It can be any value, it's the waiter
396 * job to correctly interpret this condition variable associated to the
397 * consumer pthread_cond.
399 * A value of 0 means that the corresponding thread of the consumer data
400 * was not started. 1 indicates that the thread has started and is ready
401 * for action. A negative value means that there was an error during the
404 data
->consumer_thread_is_ready
= state
;
405 (void) pthread_cond_signal(&data
->cond
);
407 pthread_mutex_unlock(&data
->cond_mutex
);
411 * This thread manage the consumer error sent back to the session daemon.
413 void *thread_manage_consumer(void *data
)
415 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
416 uint32_t revents
, nb_fd
;
417 enum lttcomm_return_code code
;
418 struct lttng_poll_event events
;
419 struct consumer_data
*consumer_data
= data
;
420 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
422 DBG("[thread] Manage consumer started");
424 rcu_register_thread();
427 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
429 health_code_update();
432 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
433 * metadata_sock. Nothing more will be added to this poll set.
435 ret
= sessiond_set_thread_pollset(&events
, 3);
441 * The error socket here is already in a listening state which was done
442 * just before spawning this thread to avoid a race between the consumer
443 * daemon exec trying to connect and the listen() call.
445 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
450 health_code_update();
452 /* Infinite blocking call, waiting for transmission */
456 if (testpoint(sessiond_thread_manage_consumer
)) {
460 ret
= lttng_poll_wait(&events
, -1);
464 * Restart interrupted system call.
466 if (errno
== EINTR
) {
474 for (i
= 0; i
< nb_fd
; i
++) {
475 /* Fetch once the poll data */
476 revents
= LTTNG_POLL_GETEV(&events
, i
);
477 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
479 health_code_update();
482 /* No activity for this FD (poll implementation). */
486 /* Thread quit pipe has been closed. Killing thread. */
487 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
493 /* Event on the registration socket */
494 if (pollfd
== consumer_data
->err_sock
) {
495 if (revents
& LPOLLIN
) {
497 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
498 ERR("consumer err socket poll error");
501 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
507 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
513 * Set the CLOEXEC flag. Return code is useless because either way, the
516 (void) utils_set_fd_cloexec(sock
);
518 health_code_update();
520 DBG2("Receiving code from consumer err_sock");
522 /* Getting status code from kconsumerd */
523 ret
= lttcomm_recv_unix_sock(sock
, &code
,
524 sizeof(enum lttcomm_return_code
));
529 health_code_update();
530 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
531 ERR("consumer error when waiting for SOCK_READY : %s",
532 lttcomm_get_readable_code(-code
));
536 /* Connect both command and metadata sockets. */
537 consumer_data
->cmd_sock
=
538 lttcomm_connect_unix_sock(
539 consumer_data
->cmd_unix_sock_path
);
540 consumer_data
->metadata_fd
=
541 lttcomm_connect_unix_sock(
542 consumer_data
->cmd_unix_sock_path
);
543 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
544 PERROR("consumer connect cmd socket");
545 /* On error, signal condition and quit. */
546 signal_consumer_condition(consumer_data
, -1);
550 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
552 /* Create metadata socket lock. */
553 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
554 if (consumer_data
->metadata_sock
.lock
== NULL
) {
555 PERROR("zmalloc pthread mutex");
558 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
560 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
561 DBG("Consumer metadata socket ready (fd: %d)",
562 consumer_data
->metadata_fd
);
565 * Remove the consumerd error sock since we've established a connection.
567 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
572 /* Add new accepted error socket. */
573 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
578 /* Add metadata socket that is successfully connected. */
579 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
580 LPOLLIN
| LPOLLRDHUP
);
585 health_code_update();
588 * Transfer the write-end of the channel monitoring and rotate pipe
589 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
591 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
592 if (!cmd_socket_wrapper
) {
595 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
597 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
598 consumer_data
->channel_monitor_pipe
);
603 /* Discard the socket wrapper as it is no longer needed. */
604 consumer_destroy_socket(cmd_socket_wrapper
);
605 cmd_socket_wrapper
= NULL
;
607 /* The thread is completely initialized, signal that it is ready. */
608 signal_consumer_condition(consumer_data
, 1);
610 /* Infinite blocking call, waiting for transmission */
613 health_code_update();
615 /* Exit the thread because the thread quit pipe has been triggered. */
617 /* Not a health error. */
623 ret
= lttng_poll_wait(&events
, -1);
627 * Restart interrupted system call.
629 if (errno
== EINTR
) {
637 for (i
= 0; i
< nb_fd
; i
++) {
638 /* Fetch once the poll data */
639 revents
= LTTNG_POLL_GETEV(&events
, i
);
640 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
642 health_code_update();
645 /* No activity for this FD (poll implementation). */
650 * Thread quit pipe has been triggered, flag that we should stop
651 * but continue the current loop to handle potential data from
654 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
656 if (pollfd
== sock
) {
657 /* Event on the consumerd socket */
658 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
659 && !(revents
& LPOLLIN
)) {
660 ERR("consumer err socket second poll error");
663 health_code_update();
664 /* Wait for any kconsumerd error */
665 ret
= lttcomm_recv_unix_sock(sock
, &code
,
666 sizeof(enum lttcomm_return_code
));
668 ERR("consumer closed the command socket");
672 ERR("consumer return code : %s",
673 lttcomm_get_readable_code(-code
));
676 } else if (pollfd
== consumer_data
->metadata_fd
) {
677 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
678 && !(revents
& LPOLLIN
)) {
679 ERR("consumer err metadata socket second poll error");
682 /* UST metadata requests */
683 ret
= ust_consumer_metadata_request(
684 &consumer_data
->metadata_sock
);
686 ERR("Handling metadata request");
690 /* No need for an else branch all FDs are tested prior. */
692 health_code_update();
698 * We lock here because we are about to close the sockets and some other
699 * thread might be using them so get exclusive access which will abort all
700 * other consumer command by other threads.
702 pthread_mutex_lock(&consumer_data
->lock
);
704 /* Immediately set the consumerd state to stopped */
705 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
706 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
707 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
708 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
709 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
711 /* Code flow error... */
715 if (consumer_data
->err_sock
>= 0) {
716 ret
= close(consumer_data
->err_sock
);
720 consumer_data
->err_sock
= -1;
722 if (consumer_data
->cmd_sock
>= 0) {
723 ret
= close(consumer_data
->cmd_sock
);
727 consumer_data
->cmd_sock
= -1;
729 if (consumer_data
->metadata_sock
.fd_ptr
&&
730 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
731 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
743 unlink(consumer_data
->err_unix_sock_path
);
744 unlink(consumer_data
->cmd_unix_sock_path
);
745 pthread_mutex_unlock(&consumer_data
->lock
);
747 /* Cleanup metadata socket mutex. */
748 if (consumer_data
->metadata_sock
.lock
) {
749 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
750 free(consumer_data
->metadata_sock
.lock
);
752 lttng_poll_clean(&events
);
754 if (cmd_socket_wrapper
) {
755 consumer_destroy_socket(cmd_socket_wrapper
);
760 ERR("Health error occurred in %s", __func__
);
762 health_unregister(health_sessiond
);
763 DBG("consumer thread cleanup completed");
765 rcu_thread_offline();
766 rcu_unregister_thread();
772 * Setup necessary data for kernel tracer action.
774 static int init_kernel_tracer(void)
778 /* Modprobe lttng kernel modules */
779 ret
= modprobe_lttng_control();
784 /* Open debugfs lttng */
785 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
786 if (kernel_tracer_fd
< 0) {
787 DBG("Failed to open %s", module_proc_lttng
);
791 /* Validate kernel version */
792 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
793 &kernel_tracer_abi_version
);
798 ret
= modprobe_lttng_data();
803 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
810 WARN("Kernel tracer does not support buffer monitoring. "
811 "The monitoring timer of channels in the kernel domain "
812 "will be set to 0 (disabled).");
815 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
819 modprobe_remove_lttng_control();
820 ret
= close(kernel_tracer_fd
);
824 kernel_tracer_fd
= -1;
825 return LTTNG_ERR_KERN_VERSION
;
828 ret
= close(kernel_tracer_fd
);
834 modprobe_remove_lttng_control();
837 WARN("No kernel tracer available");
838 kernel_tracer_fd
= -1;
840 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
842 return LTTNG_ERR_KERN_NA
;
846 static int string_match(const char *str1
, const char *str2
)
848 return (str1
&& str2
) && !strcmp(str1
, str2
);
852 * Take an option from the getopt output and set it in the right variable to be
855 * Return 0 on success else a negative value.
857 static int set_option(int opt
, const char *arg
, const char *optname
)
861 if (string_match(optname
, "client-sock") || opt
== 'c') {
862 if (!arg
|| *arg
== '\0') {
866 if (lttng_is_setuid_setgid()) {
867 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
868 "-c, --client-sock");
870 config_string_set(&config
.client_unix_sock_path
,
872 if (!config
.client_unix_sock_path
.value
) {
877 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
878 if (!arg
|| *arg
== '\0') {
882 if (lttng_is_setuid_setgid()) {
883 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
886 config_string_set(&config
.apps_unix_sock_path
,
888 if (!config
.apps_unix_sock_path
.value
) {
893 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
894 config
.daemonize
= true;
895 } else if (string_match(optname
, "background") || opt
== 'b') {
896 config
.background
= true;
897 } else if (string_match(optname
, "group") || opt
== 'g') {
898 if (!arg
|| *arg
== '\0') {
902 if (lttng_is_setuid_setgid()) {
903 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
906 config_string_set(&config
.tracing_group_name
,
908 if (!config
.tracing_group_name
.value
) {
913 } else if (string_match(optname
, "help") || opt
== 'h') {
914 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
916 ERR("Cannot show --help for `lttng-sessiond`");
919 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
920 } else if (string_match(optname
, "version") || opt
== 'V') {
921 fprintf(stdout
, "%s\n", VERSION
);
923 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
924 config
.sig_parent
= true;
925 } else if (string_match(optname
, "kconsumerd-err-sock")) {
926 if (!arg
|| *arg
== '\0') {
930 if (lttng_is_setuid_setgid()) {
931 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
932 "--kconsumerd-err-sock");
934 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
936 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
941 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
942 if (!arg
|| *arg
== '\0') {
946 if (lttng_is_setuid_setgid()) {
947 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
948 "--kconsumerd-cmd-sock");
950 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
952 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
957 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
958 if (!arg
|| *arg
== '\0') {
962 if (lttng_is_setuid_setgid()) {
963 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
964 "--ustconsumerd64-err-sock");
966 config_string_set(&config
.consumerd64_err_unix_sock_path
,
968 if (!config
.consumerd64_err_unix_sock_path
.value
) {
973 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
974 if (!arg
|| *arg
== '\0') {
978 if (lttng_is_setuid_setgid()) {
979 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
980 "--ustconsumerd64-cmd-sock");
982 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
984 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
989 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
990 if (!arg
|| *arg
== '\0') {
994 if (lttng_is_setuid_setgid()) {
995 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
996 "--ustconsumerd32-err-sock");
998 config_string_set(&config
.consumerd32_err_unix_sock_path
,
1000 if (!config
.consumerd32_err_unix_sock_path
.value
) {
1005 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
1006 if (!arg
|| *arg
== '\0') {
1010 if (lttng_is_setuid_setgid()) {
1011 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1012 "--ustconsumerd32-cmd-sock");
1014 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
1016 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
1021 } else if (string_match(optname
, "no-kernel")) {
1022 config
.no_kernel
= true;
1023 } else if (string_match(optname
, "quiet") || opt
== 'q') {
1024 config
.quiet
= true;
1025 } else if (string_match(optname
, "verbose") || opt
== 'v') {
1026 /* Verbose level can increase using multiple -v */
1028 /* Value obtained from config file */
1029 config
.verbose
= config_parse_value(arg
);
1031 /* -v used on command line */
1034 /* Clamp value to [0, 3] */
1035 config
.verbose
= config
.verbose
< 0 ? 0 :
1036 (config
.verbose
<= 3 ? config
.verbose
: 3);
1037 } else if (string_match(optname
, "verbose-consumer")) {
1039 config
.verbose_consumer
= config_parse_value(arg
);
1041 config
.verbose_consumer
++;
1043 } else if (string_match(optname
, "consumerd32-path")) {
1044 if (!arg
|| *arg
== '\0') {
1048 if (lttng_is_setuid_setgid()) {
1049 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1050 "--consumerd32-path");
1052 config_string_set(&config
.consumerd32_bin_path
,
1054 if (!config
.consumerd32_bin_path
.value
) {
1059 } else if (string_match(optname
, "consumerd32-libdir")) {
1060 if (!arg
|| *arg
== '\0') {
1064 if (lttng_is_setuid_setgid()) {
1065 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1066 "--consumerd32-libdir");
1068 config_string_set(&config
.consumerd32_lib_dir
,
1070 if (!config
.consumerd32_lib_dir
.value
) {
1075 } else if (string_match(optname
, "consumerd64-path")) {
1076 if (!arg
|| *arg
== '\0') {
1080 if (lttng_is_setuid_setgid()) {
1081 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1082 "--consumerd64-path");
1084 config_string_set(&config
.consumerd64_bin_path
,
1086 if (!config
.consumerd64_bin_path
.value
) {
1091 } else if (string_match(optname
, "consumerd64-libdir")) {
1092 if (!arg
|| *arg
== '\0') {
1096 if (lttng_is_setuid_setgid()) {
1097 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1098 "--consumerd64-libdir");
1100 config_string_set(&config
.consumerd64_lib_dir
,
1102 if (!config
.consumerd64_lib_dir
.value
) {
1107 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
1108 if (!arg
|| *arg
== '\0') {
1112 if (lttng_is_setuid_setgid()) {
1113 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1116 config_string_set(&config
.pid_file_path
, strdup(arg
));
1117 if (!config
.pid_file_path
.value
) {
1122 } else if (string_match(optname
, "agent-tcp-port")) {
1123 if (!arg
|| *arg
== '\0') {
1127 if (lttng_is_setuid_setgid()) {
1128 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1129 "--agent-tcp-port");
1134 v
= strtoul(arg
, NULL
, 0);
1135 if (errno
!= 0 || !isdigit(arg
[0])) {
1136 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
1139 if (v
== 0 || v
>= 65535) {
1140 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
1143 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
1144 DBG3("Agent TCP port set to non default: %i", (int) v
);
1146 } else if (string_match(optname
, "load") || opt
== 'l') {
1147 if (!arg
|| *arg
== '\0') {
1151 if (lttng_is_setuid_setgid()) {
1152 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1155 config_string_set(&config
.load_session_path
, strdup(arg
));
1156 if (!config
.load_session_path
.value
) {
1161 } else if (string_match(optname
, "kmod-probes")) {
1162 if (!arg
|| *arg
== '\0') {
1166 if (lttng_is_setuid_setgid()) {
1167 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1170 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
1171 if (!config
.kmod_probes_list
.value
) {
1176 } else if (string_match(optname
, "extra-kmod-probes")) {
1177 if (!arg
|| *arg
== '\0') {
1181 if (lttng_is_setuid_setgid()) {
1182 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1183 "--extra-kmod-probes");
1185 config_string_set(&config
.kmod_extra_probes_list
,
1187 if (!config
.kmod_extra_probes_list
.value
) {
1192 } else if (string_match(optname
, "config") || opt
== 'f') {
1193 /* This is handled in set_options() thus silent skip. */
1196 /* Unknown option or other error.
1197 * Error is printed by getopt, just return */
1202 if (ret
== -EINVAL
) {
1203 const char *opt_name
= "unknown";
1206 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
1208 if (opt
== long_options
[i
].val
) {
1209 opt_name
= long_options
[i
].name
;
1214 WARN("Invalid argument provided for option \"%s\", using default value.",
1222 * config_entry_handler_cb used to handle options read from a config file.
1223 * See config_entry_handler_cb comment in common/config/session-config.h for the
1224 * return value conventions.
1226 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
1230 if (!entry
|| !entry
->name
|| !entry
->value
) {
1235 /* Check if the option is to be ignored */
1236 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
1237 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
1242 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
1245 /* Ignore if not fully matched. */
1246 if (strcmp(entry
->name
, long_options
[i
].name
)) {
1251 * If the option takes no argument on the command line, we have to
1252 * check if the value is "true". We support non-zero numeric values,
1255 if (!long_options
[i
].has_arg
) {
1256 ret
= config_parse_value(entry
->value
);
1259 WARN("Invalid configuration value \"%s\" for option %s",
1260 entry
->value
, entry
->name
);
1262 /* False, skip boolean config option. */
1267 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
1271 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
1278 * daemon configuration loading and argument parsing
1280 static int set_options(int argc
, char **argv
)
1282 int ret
= 0, c
= 0, option_index
= 0;
1283 int orig_optopt
= optopt
, orig_optind
= optind
;
1285 const char *config_path
= NULL
;
1287 optstring
= utils_generate_optstring(long_options
,
1288 sizeof(long_options
) / sizeof(struct option
));
1294 /* Check for the --config option */
1295 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
1296 &option_index
)) != -1) {
1300 } else if (c
!= 'f') {
1301 /* if not equal to --config option. */
1305 if (lttng_is_setuid_setgid()) {
1306 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1309 config_path
= utils_expand_path(optarg
);
1311 ERR("Failed to resolve path: %s", optarg
);
1316 ret
= config_get_section_entries(config_path
, config_section_name
,
1317 config_entry_handler
, NULL
);
1320 ERR("Invalid configuration option at line %i", ret
);
1326 /* Reset getopt's global state */
1327 optopt
= orig_optopt
;
1328 optind
= orig_optind
;
1332 * getopt_long() will not set option_index if it encounters a
1335 c
= getopt_long(argc
, argv
, optstring
, long_options
,
1342 * Pass NULL as the long option name if popt left the index
1345 ret
= set_option(c
, optarg
,
1346 option_index
< 0 ? NULL
:
1347 long_options
[option_index
].name
);
1359 * Create lockfile using the rundir and return its fd.
1361 static int create_lockfile(void)
1363 return utils_create_lock_file(config
.lock_file_path
.value
);
1367 * Check if the global socket is available, and if a daemon is answering at the
1368 * other side. If yes, error is returned.
1370 * Also attempts to create and hold the lock file.
1372 static int check_existing_daemon(void)
1376 /* Is there anybody out there ? */
1377 if (lttng_session_daemon_alive()) {
1382 lockfile_fd
= create_lockfile();
1383 if (lockfile_fd
< 0) {
1391 static void sessiond_cleanup_lock_file(void)
1396 * Cleanup lock file by deleting it and finaly closing it which will
1397 * release the file system lock.
1399 if (lockfile_fd
>= 0) {
1400 ret
= remove(config
.lock_file_path
.value
);
1402 PERROR("remove lock file");
1404 ret
= close(lockfile_fd
);
1406 PERROR("close lock file");
1412 * Set the tracing group gid onto the client socket.
1414 * Race window between mkdir and chown is OK because we are going from more
1415 * permissive (root.root) to less permissive (root.tracing).
1417 static int set_permissions(char *rundir
)
1422 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
1424 /* Set lttng run dir */
1425 ret
= chown(rundir
, 0, gid
);
1427 ERR("Unable to set group on %s", rundir
);
1432 * Ensure all applications and tracing group can search the run
1433 * dir. Allow everyone to read the directory, since it does not
1434 * buy us anything to hide its content.
1436 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
1438 ERR("Unable to set permissions on %s", rundir
);
1442 /* lttng client socket path */
1443 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
1445 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
1449 /* kconsumer error socket path */
1450 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
1452 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
1456 /* 64-bit ustconsumer error socket path */
1457 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
1459 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
1463 /* 32-bit ustconsumer compat32 error socket path */
1464 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
1466 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
1470 DBG("All permissions are set");
1476 * Create the lttng run directory needed for all global sockets and pipe.
1478 static int create_lttng_rundir(void)
1482 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
1484 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
1486 if (errno
!= EEXIST
) {
1487 ERR("Unable to create %s", config
.rundir
.value
);
1499 * Setup sockets and directory needed by the consumerds' communication with the
1502 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
1507 switch (consumer_data
->type
) {
1508 case LTTNG_CONSUMER_KERNEL
:
1509 path
= config
.kconsumerd_path
.value
;
1511 case LTTNG_CONSUMER64_UST
:
1512 path
= config
.consumerd64_path
.value
;
1514 case LTTNG_CONSUMER32_UST
:
1515 path
= config
.consumerd32_path
.value
;
1518 ERR("Consumer type unknown");
1524 DBG2("Creating consumer directory: %s", path
);
1526 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
1527 if (ret
< 0 && errno
!= EEXIST
) {
1529 ERR("Failed to create %s", path
);
1533 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
1535 ERR("Unable to set group on %s", path
);
1541 /* Create the consumerd error unix socket */
1542 consumer_data
->err_sock
=
1543 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
1544 if (consumer_data
->err_sock
< 0) {
1545 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
1551 * Set the CLOEXEC flag. Return code is useless because either way, the
1554 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
1556 PERROR("utils_set_fd_cloexec");
1557 /* continue anyway */
1560 /* File permission MUST be 660 */
1561 ret
= chmod(consumer_data
->err_unix_sock_path
,
1562 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1564 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
1574 * Signal handler for the daemon
1576 * Simply stop all worker threads, leaving main() return gracefully after
1577 * joining all threads and calling cleanup().
1579 static void sighandler(int sig
)
1583 DBG("SIGINT caught");
1587 DBG("SIGTERM caught");
1591 CMM_STORE_SHARED(recv_child_signal
, 1);
1599 * Setup signal handler for :
1600 * SIGINT, SIGTERM, SIGPIPE
1602 static int set_signal_handler(void)
1605 struct sigaction sa
;
1608 if ((ret
= sigemptyset(&sigset
)) < 0) {
1609 PERROR("sigemptyset");
1613 sa
.sa_mask
= sigset
;
1616 sa
.sa_handler
= sighandler
;
1617 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1618 PERROR("sigaction");
1622 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1623 PERROR("sigaction");
1627 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
1628 PERROR("sigaction");
1632 sa
.sa_handler
= SIG_IGN
;
1633 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1634 PERROR("sigaction");
1638 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
1644 * Set open files limit to unlimited. This daemon can open a large number of
1645 * file descriptors in order to consume multiple kernel traces.
1647 static void set_ulimit(void)
1652 /* The kernel does not allow an infinite limit for open files */
1653 lim
.rlim_cur
= 65535;
1654 lim
.rlim_max
= 65535;
1656 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1658 PERROR("failed to set open files limit");
1662 static int write_pidfile(void)
1664 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
1667 static int set_clock_plugin_env(void)
1670 char *env_value
= NULL
;
1672 if (!config
.lttng_ust_clock_plugin
.value
) {
1676 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
1677 config
.lttng_ust_clock_plugin
.value
);
1683 ret
= putenv(env_value
);
1686 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
1690 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
1691 config
.lttng_ust_clock_plugin
.value
);
1696 static void destroy_all_sessions_and_wait(void)
1698 struct ltt_session
*session
, *tmp
;
1699 struct ltt_session_list
*session_list
;
1701 session_list
= session_get_list();
1702 DBG("Initiating destruction of all sessions");
1704 if (!session_list
) {
1708 session_lock_list();
1709 /* Initiate the destruction of all sessions. */
1710 cds_list_for_each_entry_safe(session
, tmp
,
1711 &session_list
->head
, list
) {
1712 if (!session_get(session
)) {
1716 session_lock(session
);
1717 if (session
->destroyed
) {
1718 goto unlock_session
;
1720 (void) cmd_destroy_session(session
,
1721 notification_thread_handle
);
1723 session_unlock(session
);
1724 session_put(session
);
1726 session_unlock_list();
1728 /* Wait for the destruction of all sessions to complete. */
1729 DBG("Waiting for the destruction of all sessions to complete");
1730 session_list_wait_empty();
1731 DBG("Destruction of all sessions completed");
1737 int main(int argc
, char **argv
)
1739 int ret
= 0, retval
= 0;
1741 const char *env_app_timeout
;
1742 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
1743 *ust64_channel_monitor_pipe
= NULL
,
1744 *kernel_channel_monitor_pipe
= NULL
;
1745 struct lttng_thread
*ht_cleanup_thread
= NULL
;
1746 struct timer_thread_parameters timer_thread_parameters
;
1747 /* Rotation thread handle. */
1748 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
1749 /* Queue of rotation jobs populated by the sessiond-timer. */
1750 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
1751 struct lttng_thread
*client_thread
= NULL
;
1753 init_kernel_workarounds();
1755 rcu_register_thread();
1757 if (set_signal_handler()) {
1759 goto exit_set_signal_handler
;
1762 if (timer_signal_init()) {
1764 goto exit_set_signal_handler
;
1767 page_size
= sysconf(_SC_PAGESIZE
);
1768 if (page_size
< 0) {
1769 PERROR("sysconf _SC_PAGESIZE");
1770 page_size
= LONG_MAX
;
1771 WARN("Fallback page size to %ld", page_size
);
1774 ret
= sessiond_config_init(&config
);
1777 goto exit_set_signal_handler
;
1781 * Init config from environment variables.
1782 * Command line option override env configuration per-doc. Do env first.
1784 sessiond_config_apply_env_config(&config
);
1787 * Parse arguments and load the daemon configuration file.
1789 * We have an exit_options exit path to free memory reserved by
1790 * set_options. This is needed because the rest of sessiond_cleanup()
1791 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
1792 * depends on set_options.
1795 if (set_options(argc
, argv
)) {
1801 * Resolve all paths received as arguments, configuration option, or
1802 * through environment variable as absolute paths. This is necessary
1803 * since daemonizing causes the sessiond's current working directory
1806 ret
= sessiond_config_resolve_paths(&config
);
1812 lttng_opt_verbose
= config
.verbose
;
1813 lttng_opt_quiet
= config
.quiet
;
1814 kconsumer_data
.err_unix_sock_path
=
1815 config
.kconsumerd_err_unix_sock_path
.value
;
1816 kconsumer_data
.cmd_unix_sock_path
=
1817 config
.kconsumerd_cmd_unix_sock_path
.value
;
1818 ustconsumer32_data
.err_unix_sock_path
=
1819 config
.consumerd32_err_unix_sock_path
.value
;
1820 ustconsumer32_data
.cmd_unix_sock_path
=
1821 config
.consumerd32_cmd_unix_sock_path
.value
;
1822 ustconsumer64_data
.err_unix_sock_path
=
1823 config
.consumerd64_err_unix_sock_path
.value
;
1824 ustconsumer64_data
.cmd_unix_sock_path
=
1825 config
.consumerd64_cmd_unix_sock_path
.value
;
1826 set_clock_plugin_env();
1828 sessiond_config_log(&config
);
1830 if (create_lttng_rundir()) {
1835 /* Abort launch if a session daemon is already running. */
1836 if (check_existing_daemon()) {
1837 ERR("A session daemon is already running.");
1843 if (config
.daemonize
|| config
.background
) {
1846 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
1847 !config
.background
);
1854 * We are in the child. Make sure all other file descriptors are
1855 * closed, in case we are called with more opened file
1856 * descriptors than the standard ones and the lock file.
1858 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1859 if (i
== lockfile_fd
) {
1866 if (run_as_create_worker(argv
[0]) < 0) {
1867 goto exit_create_run_as_worker_cleanup
;
1871 * Starting from here, we can create threads. This needs to be after
1872 * lttng_daemonize due to RCU.
1876 * Initialize the health check subsystem. This call should set the
1877 * appropriate time values.
1879 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
1880 if (!health_sessiond
) {
1881 PERROR("health_app_create error");
1883 goto exit_health_sessiond_cleanup
;
1886 /* Create thread to clean up RCU hash tables */
1887 ht_cleanup_thread
= launch_ht_cleanup_thread();
1888 if (!ht_cleanup_thread
) {
1890 goto exit_ht_cleanup
;
1893 /* Create thread quit pipe */
1894 if (sessiond_init_thread_quit_pipe()) {
1896 goto exit_init_data
;
1899 /* Check if daemon is UID = 0 */
1900 is_root
= !getuid();
1902 /* Create global run dir with root access */
1904 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
1905 if (!kernel_channel_monitor_pipe
) {
1906 ERR("Failed to create kernel consumer channel monitor pipe");
1908 goto exit_init_data
;
1910 kconsumer_data
.channel_monitor_pipe
=
1911 lttng_pipe_release_writefd(
1912 kernel_channel_monitor_pipe
);
1913 if (kconsumer_data
.channel_monitor_pipe
< 0) {
1915 goto exit_init_data
;
1919 /* Set consumer initial state */
1920 kernel_consumerd_state
= CONSUMER_STOPPED
;
1921 ust_consumerd_state
= CONSUMER_STOPPED
;
1923 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
1924 if (!ust32_channel_monitor_pipe
) {
1925 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
1927 goto exit_init_data
;
1929 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1930 ust32_channel_monitor_pipe
);
1931 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
1933 goto exit_init_data
;
1937 * The rotation_thread_timer_queue structure is shared between the
1938 * sessiond timer thread and the rotation thread. The main thread keeps
1939 * its ownership and destroys it when both threads have been joined.
1941 rotation_timer_queue
= rotation_thread_timer_queue_create();
1942 if (!rotation_timer_queue
) {
1944 goto exit_init_data
;
1946 timer_thread_parameters
.rotation_thread_job_queue
=
1947 rotation_timer_queue
;
1949 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
1950 if (!ust64_channel_monitor_pipe
) {
1951 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
1953 goto exit_init_data
;
1955 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
1956 ust64_channel_monitor_pipe
);
1957 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
1959 goto exit_init_data
;
1963 * Init UST app hash table. Alloc hash table before this point since
1964 * cleanup() can get called after that point.
1966 if (ust_app_ht_alloc()) {
1967 ERR("Failed to allocate UST app hash table");
1969 goto exit_init_data
;
1973 * Initialize agent app hash table. We allocate the hash table here
1974 * since cleanup() can get called after this point.
1976 if (agent_app_ht_alloc()) {
1977 ERR("Failed to allocate Agent app hash table");
1979 goto exit_init_data
;
1983 * These actions must be executed as root. We do that *after* setting up
1984 * the sockets path because we MUST make the check for another daemon using
1985 * those paths *before* trying to set the kernel consumer sockets and init
1989 if (set_consumer_sockets(&kconsumer_data
)) {
1991 goto exit_init_data
;
1994 /* Setup kernel tracer */
1995 if (!config
.no_kernel
) {
1996 init_kernel_tracer();
1997 if (kernel_tracer_fd
>= 0) {
1998 ret
= syscall_init_table();
2000 ERR("Unable to populate syscall table. "
2001 "Syscall tracing won't work "
2002 "for this session daemon.");
2007 /* Set ulimit for open files */
2010 /* init lttng_fd tracking must be done after set_ulimit. */
2013 if (set_consumer_sockets(&ustconsumer64_data
)) {
2015 goto exit_init_data
;
2018 if (set_consumer_sockets(&ustconsumer32_data
)) {
2020 goto exit_init_data
;
2023 /* Set credentials to socket */
2024 if (is_root
&& set_permissions(config
.rundir
.value
)) {
2026 goto exit_init_data
;
2029 /* Get parent pid if -S, --sig-parent is specified. */
2030 if (config
.sig_parent
) {
2034 /* Setup the kernel pipe for waking up the kernel thread */
2035 if (is_root
&& !config
.no_kernel
) {
2036 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
2038 goto exit_init_data
;
2042 /* Setup the thread apps communication pipe. */
2043 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
2045 goto exit_init_data
;
2048 /* Setup the thread apps notify communication pipe. */
2049 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
2051 goto exit_init_data
;
2054 /* Initialize global buffer per UID and PID registry. */
2055 buffer_reg_init_uid_registry();
2056 buffer_reg_init_pid_registry();
2058 /* Init UST command queue. */
2059 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2063 /* Check for the application socket timeout env variable. */
2064 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
2065 if (env_app_timeout
) {
2066 config
.app_socket_timeout
= atoi(env_app_timeout
);
2068 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
2071 ret
= write_pidfile();
2073 ERR("Error in write_pidfile");
2075 goto exit_init_data
;
2078 /* Initialize communication library */
2080 /* Initialize TCP timeout values */
2081 lttcomm_inet_init();
2083 if (load_session_init_data(&load_info
) < 0) {
2085 goto exit_init_data
;
2087 load_info
->path
= config
.load_session_path
.value
;
2089 /* Create health-check thread. */
2090 if (!launch_health_management_thread()) {
2095 /* notification_thread_data acquires the pipes' read side. */
2096 notification_thread_handle
= notification_thread_handle_create(
2097 ust32_channel_monitor_pipe
,
2098 ust64_channel_monitor_pipe
,
2099 kernel_channel_monitor_pipe
);
2100 if (!notification_thread_handle
) {
2102 ERR("Failed to create notification thread shared data");
2103 goto exit_notification
;
2106 /* Create notification thread. */
2107 if (!launch_notification_thread(notification_thread_handle
)) {
2109 goto exit_notification
;
2112 /* Create timer thread. */
2113 if (!launch_timer_thread(&timer_thread_parameters
)) {
2115 goto exit_notification
;
2118 /* rotation_thread_data acquires the pipes' read side. */
2119 rotation_thread_handle
= rotation_thread_handle_create(
2120 rotation_timer_queue
,
2121 notification_thread_handle
);
2122 if (!rotation_thread_handle
) {
2124 ERR("Failed to create rotation thread shared data");
2129 /* Create rotation thread. */
2130 if (!launch_rotation_thread(rotation_thread_handle
)) {
2135 /* Create thread to manage the client socket */
2136 client_thread
= launch_client_thread();
2137 if (!client_thread
) {
2142 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
2143 apps_cmd_notify_pipe
[1])) {
2148 /* Create thread to manage application registration. */
2149 if (!launch_application_registration_thread(&ust_cmd_queue
)) {
2154 /* Create thread to manage application socket */
2155 if (!launch_application_management_thread(apps_cmd_pipe
[0])) {
2160 /* Create thread to manage application notify socket */
2161 if (!launch_application_notification_thread(apps_cmd_notify_pipe
[0])) {
2163 goto exit_apps_notify
;
2166 /* Create agent management thread. */
2167 if (!launch_agent_management_thread()) {
2169 goto exit_agent_reg
;
2172 /* Don't start this thread if kernel tracing is not requested nor root */
2173 if (is_root
&& !config
.no_kernel
) {
2174 /* Create kernel thread to manage kernel event */
2175 if (!launch_kernel_management_thread(kernel_poll_pipe
[0])) {
2181 /* Create session loading thread. */
2182 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
2183 thread_load_session
, load_info
);
2186 PERROR("pthread_create load_session_thread");
2189 goto exit_load_session
;
2193 * This is where we start awaiting program completion (e.g. through
2194 * signal that asks threads to teardown).
2197 ret
= pthread_join(load_session_thread
, &status
);
2200 PERROR("pthread_join load_session_thread");
2204 /* Initiate teardown once activity occurs on the quit pipe. */
2205 sessiond_wait_for_quit_pipe(-1U);
2208 * Ensure that the client thread is no longer accepting new commands,
2209 * which could cause new sessions to be created.
2211 if (!lttng_thread_shutdown(client_thread
)) {
2212 ERR("Failed to shutdown the client thread, continuing teardown");
2213 lttng_thread_put(client_thread
);
2214 client_thread
= NULL
;
2217 destroy_all_sessions_and_wait();
2228 lttng_thread_list_shutdown_orphans();
2231 if (client_thread
) {
2232 lttng_thread_put(client_thread
);
2236 * Wait for all pending call_rcu work to complete before tearing
2237 * down data structures. call_rcu worker may be trying to
2238 * perform lookups in those structures.
2242 * sessiond_cleanup() is called when no other thread is running, except
2243 * the ht_cleanup thread, which is needed to destroy the hash tables.
2245 rcu_thread_online();
2249 * Ensure all prior call_rcu are done. call_rcu callbacks may push
2250 * hash tables to the ht_cleanup thread. Therefore, we ensure that
2251 * the queue is empty before shutting down the clean-up thread.
2255 if (ht_cleanup_thread
) {
2256 lttng_thread_shutdown(ht_cleanup_thread
);
2257 lttng_thread_put(ht_cleanup_thread
);
2260 rcu_thread_offline();
2261 rcu_unregister_thread();
2263 if (rotation_thread_handle
) {
2264 rotation_thread_handle_destroy(rotation_thread_handle
);
2268 * After the rotation and timer thread have quit, we can safely destroy
2269 * the rotation_timer_queue.
2271 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
2273 * The teardown of the notification system is performed after the
2274 * session daemon's teardown in order to allow it to be notified
2275 * of the active session and channels at the moment of the teardown.
2277 if (notification_thread_handle
) {
2278 notification_thread_handle_destroy(notification_thread_handle
);
2280 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
2281 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
2282 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
2285 health_app_destroy(health_sessiond
);
2286 exit_health_sessiond_cleanup
:
2287 exit_create_run_as_worker_cleanup
:
2290 sessiond_cleanup_lock_file();
2291 sessiond_cleanup_options();
2293 exit_set_signal_handler
: