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 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" };
137 static int apps_sock
= -1;
140 * This pipe is used to inform the thread managing application communication
141 * that a command is queued and ready to be processed.
143 static int apps_cmd_pipe
[2] = { -1, -1 };
145 /* Pthread, Mutexes and Semaphores */
146 static pthread_t apps_thread
;
147 static pthread_t apps_notify_thread
;
148 static pthread_t reg_apps_thread
;
149 static pthread_t kernel_thread
;
150 static pthread_t agent_reg_thread
;
151 static pthread_t load_session_thread
;
154 * UST registration command queue. This queue is tied with a futex and uses a N
155 * wakers / 1 waiter implemented and detailed in futex.c/.h
157 * The thread_registration_apps and thread_dispatch_ust_registration uses this
158 * queue along with the wait/wake scheme. The thread_manage_apps receives down
159 * the line new application socket and monitors it for any I/O error or clean
160 * close that triggers an unregistration of the application.
162 static struct ust_cmd_queue ust_cmd_queue
;
164 static const char *module_proc_lttng
= "/proc/lttng";
166 /* Load session thread information to operate. */
167 static struct load_session_thread_data
*load_info
;
170 * Section name to look for in the daemon configuration file.
172 static const char * const config_section_name
= "sessiond";
174 /* Am I root or not. Set to 1 if the daemon is running as root */
178 * Stop all threads by closing the thread quit pipe.
180 static void stop_threads(void)
184 /* Stopping all threads */
185 DBG("Terminating all threads");
186 ret
= sessiond_notify_quit_pipe();
188 ERR("write error on thread quit pipe");
193 * Close every consumer sockets.
195 static void close_consumer_sockets(void)
199 if (kconsumer_data
.err_sock
>= 0) {
200 ret
= close(kconsumer_data
.err_sock
);
202 PERROR("kernel consumer err_sock close");
205 if (ustconsumer32_data
.err_sock
>= 0) {
206 ret
= close(ustconsumer32_data
.err_sock
);
208 PERROR("UST consumerd32 err_sock close");
211 if (ustconsumer64_data
.err_sock
>= 0) {
212 ret
= close(ustconsumer64_data
.err_sock
);
214 PERROR("UST consumerd64 err_sock close");
217 if (kconsumer_data
.cmd_sock
>= 0) {
218 ret
= close(kconsumer_data
.cmd_sock
);
220 PERROR("kernel consumer cmd_sock close");
223 if (ustconsumer32_data
.cmd_sock
>= 0) {
224 ret
= close(ustconsumer32_data
.cmd_sock
);
226 PERROR("UST consumerd32 cmd_sock close");
229 if (ustconsumer64_data
.cmd_sock
>= 0) {
230 ret
= close(ustconsumer64_data
.cmd_sock
);
232 PERROR("UST consumerd64 cmd_sock close");
235 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
236 ret
= close(kconsumer_data
.channel_monitor_pipe
);
238 PERROR("kernel consumer channel monitor pipe close");
241 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
242 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
244 PERROR("UST consumerd32 channel monitor pipe close");
247 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
248 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
250 PERROR("UST consumerd64 channel monitor pipe close");
256 * Wait on consumer process termination.
258 * Need to be called with the consumer data lock held or from a context
259 * ensuring no concurrent access to data (e.g: cleanup).
261 static void wait_consumer(struct consumer_data
*consumer_data
)
266 if (consumer_data
->pid
<= 0) {
270 DBG("Waiting for complete teardown of consumerd (PID: %d)",
272 ret
= waitpid(consumer_data
->pid
, &status
, 0);
274 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
275 } else if (!WIFEXITED(status
)) {
276 ERR("consumerd termination with error: %d",
279 consumer_data
->pid
= 0;
283 * Cleanup the session daemon's data structures.
285 static void sessiond_cleanup(void)
288 struct ltt_session_list
*session_list
= session_get_list();
290 DBG("Cleanup sessiond");
293 * Close the thread quit pipe. It has already done its job,
294 * since we are now called.
296 sessiond_close_quit_pipe();
298 ret
= remove(config
.pid_file_path
.value
);
300 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
303 DBG("Removing sessiond and consumerd content of directory %s",
304 config
.rundir
.value
);
307 DBG("Removing %s", config
.pid_file_path
.value
);
308 (void) unlink(config
.pid_file_path
.value
);
310 DBG("Removing %s", config
.agent_port_file_path
.value
);
311 (void) unlink(config
.agent_port_file_path
.value
);
314 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
315 (void) unlink(kconsumer_data
.err_unix_sock_path
);
317 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
318 (void) rmdir(config
.kconsumerd_path
.value
);
320 /* ust consumerd 32 */
321 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
322 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
324 DBG("Removing directory %s", config
.consumerd32_path
.value
);
325 (void) rmdir(config
.consumerd32_path
.value
);
327 /* ust consumerd 64 */
328 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
329 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
331 DBG("Removing directory %s", config
.consumerd64_path
.value
);
332 (void) rmdir(config
.consumerd64_path
.value
);
334 pthread_mutex_destroy(&session_list
->lock
);
336 wait_consumer(&kconsumer_data
);
337 wait_consumer(&ustconsumer64_data
);
338 wait_consumer(&ustconsumer32_data
);
340 DBG("Cleaning up all agent apps");
341 agent_app_ht_clean();
343 DBG("Closing all UST sockets");
344 ust_app_clean_list();
345 buffer_reg_destroy_registries();
347 if (is_root
&& !config
.no_kernel
) {
348 DBG2("Closing kernel fd");
349 if (kernel_tracer_fd
>= 0) {
350 ret
= close(kernel_tracer_fd
);
355 DBG("Unloading kernel modules");
356 modprobe_remove_lttng_all();
360 close_consumer_sockets();
363 load_session_destroy_data(load_info
);
368 * We do NOT rmdir rundir because there are other processes
369 * using it, for instance lttng-relayd, which can start in
370 * parallel with this teardown.
375 * Cleanup the daemon's option data structures.
377 static void sessiond_cleanup_options(void)
379 DBG("Cleaning up options");
381 sessiond_config_fini(&config
);
383 run_as_destroy_worker();
387 * Notify UST applications using the shm mmap futex.
389 static int notify_ust_apps(int active
)
393 DBG("Notifying applications of session daemon state: %d", active
);
395 /* See shm.c for this call implying mmap, shm and futex calls */
396 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
397 if (wait_shm_mmap
== NULL
) {
401 /* Wake waiting process */
402 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
404 /* Apps notified successfully */
412 * Update the kernel poll set of all channel fd available over all tracing
413 * session. Add the wakeup pipe at the end of the set.
415 static int update_kernel_poll(struct lttng_poll_event
*events
)
418 struct ltt_kernel_channel
*channel
;
419 struct ltt_session
*session
;
420 const struct ltt_session_list
*session_list
= session_get_list();
422 DBG("Updating kernel poll set");
425 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
426 if (!session_get(session
)) {
429 session_lock(session
);
430 if (session
->kernel_session
== NULL
) {
431 session_unlock(session
);
432 session_put(session
);
436 cds_list_for_each_entry(channel
,
437 &session
->kernel_session
->channel_list
.head
, list
) {
438 /* Add channel fd to the kernel poll set */
439 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
441 session_unlock(session
);
442 session_put(session
);
445 DBG("Channel fd %d added to kernel set", channel
->fd
);
447 session_unlock(session
);
449 session_unlock_list();
454 session_unlock_list();
459 * Find the channel fd from 'fd' over all tracing session. When found, check
460 * for new channel stream and send those stream fds to the kernel consumer.
462 * Useful for CPU hotplug feature.
464 static int update_kernel_stream(int fd
)
467 struct ltt_session
*session
;
468 struct ltt_kernel_session
*ksess
;
469 struct ltt_kernel_channel
*channel
;
470 const struct ltt_session_list
*session_list
= session_get_list();
472 DBG("Updating kernel streams for channel fd %d", fd
);
475 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
476 if (!session_get(session
)) {
479 session_lock(session
);
480 if (session
->kernel_session
== NULL
) {
481 session_unlock(session
);
482 session_put(session
);
485 ksess
= session
->kernel_session
;
487 cds_list_for_each_entry(channel
,
488 &ksess
->channel_list
.head
, list
) {
489 struct lttng_ht_iter iter
;
490 struct consumer_socket
*socket
;
492 if (channel
->fd
!= fd
) {
495 DBG("Channel found, updating kernel streams");
496 ret
= kernel_open_channel_stream(channel
);
500 /* Update the stream global counter */
501 ksess
->stream_count_global
+= ret
;
504 * Have we already sent fds to the consumer? If yes, it
505 * means that tracing is started so it is safe to send
506 * our updated stream fds.
508 if (ksess
->consumer_fds_sent
!= 1
509 || ksess
->consumer
== NULL
) {
515 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
516 &iter
.iter
, socket
, node
.node
) {
517 pthread_mutex_lock(socket
->lock
);
518 ret
= kernel_consumer_send_channel_streams(socket
,
520 session
->output_traces
? 1 : 0);
521 pthread_mutex_unlock(socket
->lock
);
529 session_unlock(session
);
530 session_put(session
);
532 session_unlock_list();
536 session_unlock(session
);
537 session_put(session
);
538 session_unlock_list();
543 * This thread manage event coming from the kernel.
545 * Features supported in this thread:
548 static void *thread_manage_kernel(void *data
)
550 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
551 uint32_t revents
, nb_fd
;
553 struct lttng_poll_event events
;
555 DBG("[thread] Thread manage kernel started");
557 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
560 * This first step of the while is to clean this structure which could free
561 * non NULL pointers so initialize it before the loop.
563 lttng_poll_init(&events
);
565 if (testpoint(sessiond_thread_manage_kernel
)) {
566 goto error_testpoint
;
569 health_code_update();
571 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
572 goto error_testpoint
;
576 health_code_update();
578 if (update_poll_flag
== 1) {
579 /* Clean events object. We are about to populate it again. */
580 lttng_poll_clean(&events
);
582 ret
= sessiond_set_thread_pollset(&events
, 2);
584 goto error_poll_create
;
587 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
592 /* This will add the available kernel channel if any. */
593 ret
= update_kernel_poll(&events
);
597 update_poll_flag
= 0;
600 DBG("Thread kernel polling");
602 /* Poll infinite value of time */
605 ret
= lttng_poll_wait(&events
, -1);
606 DBG("Thread kernel return from poll on %d fds",
607 LTTNG_POLL_GETNB(&events
));
611 * Restart interrupted system call.
613 if (errno
== EINTR
) {
617 } else if (ret
== 0) {
618 /* Should not happen since timeout is infinite */
619 ERR("Return value of poll is 0 with an infinite timeout.\n"
620 "This should not have happened! Continuing...");
626 for (i
= 0; i
< nb_fd
; i
++) {
627 /* Fetch once the poll data */
628 revents
= LTTNG_POLL_GETEV(&events
, i
);
629 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
631 health_code_update();
634 /* No activity for this FD (poll implementation). */
638 /* Thread quit pipe has been closed. Killing thread. */
639 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
645 /* Check for data on kernel pipe */
646 if (revents
& LPOLLIN
) {
647 if (pollfd
== kernel_poll_pipe
[0]) {
648 (void) lttng_read(kernel_poll_pipe
[0],
651 * Ret value is useless here, if this pipe gets any actions an
652 * update is required anyway.
654 update_poll_flag
= 1;
658 * New CPU detected by the kernel. Adding kernel stream to
659 * kernel session and updating the kernel consumer
661 ret
= update_kernel_stream(pollfd
);
667 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
668 update_poll_flag
= 1;
671 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
679 lttng_poll_clean(&events
);
682 utils_close_pipe(kernel_poll_pipe
);
683 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
686 ERR("Health error occurred in %s", __func__
);
687 WARN("Kernel thread died unexpectedly. "
688 "Kernel tracing can continue but CPU hotplug is disabled.");
690 health_unregister(health_sessiond
);
691 DBG("Kernel thread dying");
696 * Signal pthread condition of the consumer data that the thread.
698 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
700 pthread_mutex_lock(&data
->cond_mutex
);
703 * The state is set before signaling. It can be any value, it's the waiter
704 * job to correctly interpret this condition variable associated to the
705 * consumer pthread_cond.
707 * A value of 0 means that the corresponding thread of the consumer data
708 * was not started. 1 indicates that the thread has started and is ready
709 * for action. A negative value means that there was an error during the
712 data
->consumer_thread_is_ready
= state
;
713 (void) pthread_cond_signal(&data
->cond
);
715 pthread_mutex_unlock(&data
->cond_mutex
);
719 * This thread manage the consumer error sent back to the session daemon.
721 void *thread_manage_consumer(void *data
)
723 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
724 uint32_t revents
, nb_fd
;
725 enum lttcomm_return_code code
;
726 struct lttng_poll_event events
;
727 struct consumer_data
*consumer_data
= data
;
728 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
730 DBG("[thread] Manage consumer started");
732 rcu_register_thread();
735 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
737 health_code_update();
740 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
741 * metadata_sock. Nothing more will be added to this poll set.
743 ret
= sessiond_set_thread_pollset(&events
, 3);
749 * The error socket here is already in a listening state which was done
750 * just before spawning this thread to avoid a race between the consumer
751 * daemon exec trying to connect and the listen() call.
753 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
758 health_code_update();
760 /* Infinite blocking call, waiting for transmission */
764 if (testpoint(sessiond_thread_manage_consumer
)) {
768 ret
= lttng_poll_wait(&events
, -1);
772 * Restart interrupted system call.
774 if (errno
== EINTR
) {
782 for (i
= 0; i
< nb_fd
; i
++) {
783 /* Fetch once the poll data */
784 revents
= LTTNG_POLL_GETEV(&events
, i
);
785 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
787 health_code_update();
790 /* No activity for this FD (poll implementation). */
794 /* Thread quit pipe has been closed. Killing thread. */
795 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
801 /* Event on the registration socket */
802 if (pollfd
== consumer_data
->err_sock
) {
803 if (revents
& LPOLLIN
) {
805 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
806 ERR("consumer err socket poll error");
809 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
815 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
821 * Set the CLOEXEC flag. Return code is useless because either way, the
824 (void) utils_set_fd_cloexec(sock
);
826 health_code_update();
828 DBG2("Receiving code from consumer err_sock");
830 /* Getting status code from kconsumerd */
831 ret
= lttcomm_recv_unix_sock(sock
, &code
,
832 sizeof(enum lttcomm_return_code
));
837 health_code_update();
838 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
839 ERR("consumer error when waiting for SOCK_READY : %s",
840 lttcomm_get_readable_code(-code
));
844 /* Connect both command and metadata sockets. */
845 consumer_data
->cmd_sock
=
846 lttcomm_connect_unix_sock(
847 consumer_data
->cmd_unix_sock_path
);
848 consumer_data
->metadata_fd
=
849 lttcomm_connect_unix_sock(
850 consumer_data
->cmd_unix_sock_path
);
851 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
852 PERROR("consumer connect cmd socket");
853 /* On error, signal condition and quit. */
854 signal_consumer_condition(consumer_data
, -1);
858 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
860 /* Create metadata socket lock. */
861 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
862 if (consumer_data
->metadata_sock
.lock
== NULL
) {
863 PERROR("zmalloc pthread mutex");
866 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
868 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
869 DBG("Consumer metadata socket ready (fd: %d)",
870 consumer_data
->metadata_fd
);
873 * Remove the consumerd error sock since we've established a connection.
875 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
880 /* Add new accepted error socket. */
881 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
886 /* Add metadata socket that is successfully connected. */
887 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
888 LPOLLIN
| LPOLLRDHUP
);
893 health_code_update();
896 * Transfer the write-end of the channel monitoring and rotate pipe
897 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
899 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
900 if (!cmd_socket_wrapper
) {
903 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
905 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
906 consumer_data
->channel_monitor_pipe
);
911 /* Discard the socket wrapper as it is no longer needed. */
912 consumer_destroy_socket(cmd_socket_wrapper
);
913 cmd_socket_wrapper
= NULL
;
915 /* The thread is completely initialized, signal that it is ready. */
916 signal_consumer_condition(consumer_data
, 1);
918 /* Infinite blocking call, waiting for transmission */
921 health_code_update();
923 /* Exit the thread because the thread quit pipe has been triggered. */
925 /* Not a health error. */
931 ret
= lttng_poll_wait(&events
, -1);
935 * Restart interrupted system call.
937 if (errno
== EINTR
) {
945 for (i
= 0; i
< nb_fd
; i
++) {
946 /* Fetch once the poll data */
947 revents
= LTTNG_POLL_GETEV(&events
, i
);
948 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
950 health_code_update();
953 /* No activity for this FD (poll implementation). */
958 * Thread quit pipe has been triggered, flag that we should stop
959 * but continue the current loop to handle potential data from
962 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
964 if (pollfd
== sock
) {
965 /* Event on the consumerd socket */
966 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
967 && !(revents
& LPOLLIN
)) {
968 ERR("consumer err socket second poll error");
971 health_code_update();
972 /* Wait for any kconsumerd error */
973 ret
= lttcomm_recv_unix_sock(sock
, &code
,
974 sizeof(enum lttcomm_return_code
));
976 ERR("consumer closed the command socket");
980 ERR("consumer return code : %s",
981 lttcomm_get_readable_code(-code
));
984 } else if (pollfd
== consumer_data
->metadata_fd
) {
985 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
986 && !(revents
& LPOLLIN
)) {
987 ERR("consumer err metadata socket second poll error");
990 /* UST metadata requests */
991 ret
= ust_consumer_metadata_request(
992 &consumer_data
->metadata_sock
);
994 ERR("Handling metadata request");
998 /* No need for an else branch all FDs are tested prior. */
1000 health_code_update();
1006 * We lock here because we are about to close the sockets and some other
1007 * thread might be using them so get exclusive access which will abort all
1008 * other consumer command by other threads.
1010 pthread_mutex_lock(&consumer_data
->lock
);
1012 /* Immediately set the consumerd state to stopped */
1013 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1014 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1015 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1016 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1017 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1019 /* Code flow error... */
1023 if (consumer_data
->err_sock
>= 0) {
1024 ret
= close(consumer_data
->err_sock
);
1028 consumer_data
->err_sock
= -1;
1030 if (consumer_data
->cmd_sock
>= 0) {
1031 ret
= close(consumer_data
->cmd_sock
);
1035 consumer_data
->cmd_sock
= -1;
1037 if (consumer_data
->metadata_sock
.fd_ptr
&&
1038 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1039 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1051 unlink(consumer_data
->err_unix_sock_path
);
1052 unlink(consumer_data
->cmd_unix_sock_path
);
1053 pthread_mutex_unlock(&consumer_data
->lock
);
1055 /* Cleanup metadata socket mutex. */
1056 if (consumer_data
->metadata_sock
.lock
) {
1057 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1058 free(consumer_data
->metadata_sock
.lock
);
1060 lttng_poll_clean(&events
);
1062 if (cmd_socket_wrapper
) {
1063 consumer_destroy_socket(cmd_socket_wrapper
);
1068 ERR("Health error occurred in %s", __func__
);
1070 health_unregister(health_sessiond
);
1071 DBG("consumer thread cleanup completed");
1073 rcu_thread_offline();
1074 rcu_unregister_thread();
1080 * This thread receives application command sockets (FDs) on the
1081 * apps_cmd_pipe and waits (polls) on them until they are closed
1082 * or an error occurs.
1084 * At that point, it flushes the data (tracing and metadata) associated
1085 * with this application and tears down ust app sessions and other
1086 * associated data structures through ust_app_unregister().
1088 * Note that this thread never sends commands to the applications
1089 * through the command sockets; it merely listens for hang-ups
1090 * and errors on those sockets and cleans-up as they occur.
1092 static void *thread_manage_apps(void *data
)
1094 int i
, ret
, pollfd
, err
= -1;
1096 uint32_t revents
, nb_fd
;
1097 struct lttng_poll_event events
;
1099 DBG("[thread] Manage application started");
1101 rcu_register_thread();
1102 rcu_thread_online();
1104 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1106 if (testpoint(sessiond_thread_manage_apps
)) {
1107 goto error_testpoint
;
1110 health_code_update();
1112 ret
= sessiond_set_thread_pollset(&events
, 2);
1114 goto error_poll_create
;
1117 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1122 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1126 health_code_update();
1129 DBG("Apps thread polling");
1131 /* Inifinite blocking call, waiting for transmission */
1133 health_poll_entry();
1134 ret
= lttng_poll_wait(&events
, -1);
1135 DBG("Apps thread return from poll on %d fds",
1136 LTTNG_POLL_GETNB(&events
));
1140 * Restart interrupted system call.
1142 if (errno
== EINTR
) {
1150 for (i
= 0; i
< nb_fd
; i
++) {
1151 /* Fetch once the poll data */
1152 revents
= LTTNG_POLL_GETEV(&events
, i
);
1153 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1155 health_code_update();
1158 /* No activity for this FD (poll implementation). */
1162 /* Thread quit pipe has been closed. Killing thread. */
1163 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1169 /* Inspect the apps cmd pipe */
1170 if (pollfd
== apps_cmd_pipe
[0]) {
1171 if (revents
& LPOLLIN
) {
1175 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1176 if (size_ret
< sizeof(sock
)) {
1177 PERROR("read apps cmd pipe");
1181 health_code_update();
1184 * Since this is a command socket (write then read),
1185 * we only monitor the error events of the socket.
1187 ret
= lttng_poll_add(&events
, sock
,
1188 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1193 DBG("Apps with sock %d added to poll set", sock
);
1194 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1195 ERR("Apps command pipe error");
1198 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1203 * At this point, we know that a registered application made
1204 * the event at poll_wait.
1206 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1207 /* Removing from the poll set */
1208 ret
= lttng_poll_del(&events
, pollfd
);
1213 /* Socket closed on remote end. */
1214 ust_app_unregister(pollfd
);
1216 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1221 health_code_update();
1227 lttng_poll_clean(&events
);
1230 utils_close_pipe(apps_cmd_pipe
);
1231 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1234 * We don't clean the UST app hash table here since already registered
1235 * applications can still be controlled so let them be until the session
1236 * daemon dies or the applications stop.
1241 ERR("Health error occurred in %s", __func__
);
1243 health_unregister(health_sessiond
);
1244 DBG("Application communication apps thread cleanup complete");
1245 rcu_thread_offline();
1246 rcu_unregister_thread();
1251 * This thread manage application registration.
1253 static void *thread_registration_apps(void *data
)
1255 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1256 uint32_t revents
, nb_fd
;
1257 struct lttng_poll_event events
;
1259 * Get allocated in this thread, enqueued to a global queue, dequeued and
1260 * freed in the manage apps thread.
1262 struct ust_command
*ust_cmd
= NULL
;
1264 DBG("[thread] Manage application registration started");
1266 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1268 if (testpoint(sessiond_thread_registration_apps
)) {
1269 goto error_testpoint
;
1272 ret
= lttcomm_listen_unix_sock(apps_sock
);
1278 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1279 * more will be added to this poll set.
1281 ret
= sessiond_set_thread_pollset(&events
, 2);
1283 goto error_create_poll
;
1286 /* Add the application registration socket */
1287 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1289 goto error_poll_add
;
1292 /* Notify all applications to register */
1293 ret
= notify_ust_apps(1);
1295 ERR("Failed to notify applications or create the wait shared memory.\n"
1296 "Execution continues but there might be problem for already\n"
1297 "running applications that wishes to register.");
1301 DBG("Accepting application registration");
1303 /* Inifinite blocking call, waiting for transmission */
1305 health_poll_entry();
1306 ret
= lttng_poll_wait(&events
, -1);
1310 * Restart interrupted system call.
1312 if (errno
== EINTR
) {
1320 for (i
= 0; i
< nb_fd
; i
++) {
1321 health_code_update();
1323 /* Fetch once the poll data */
1324 revents
= LTTNG_POLL_GETEV(&events
, i
);
1325 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1328 /* No activity for this FD (poll implementation). */
1332 /* Thread quit pipe has been closed. Killing thread. */
1333 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1339 /* Event on the registration socket */
1340 if (pollfd
== apps_sock
) {
1341 if (revents
& LPOLLIN
) {
1342 sock
= lttcomm_accept_unix_sock(apps_sock
);
1348 * Set socket timeout for both receiving and ending.
1349 * app_socket_timeout is in seconds, whereas
1350 * lttcomm_setsockopt_rcv_timeout and
1351 * lttcomm_setsockopt_snd_timeout expect msec as
1354 if (config
.app_socket_timeout
>= 0) {
1355 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1356 config
.app_socket_timeout
* 1000);
1357 (void) lttcomm_setsockopt_snd_timeout(sock
,
1358 config
.app_socket_timeout
* 1000);
1362 * Set the CLOEXEC flag. Return code is useless because
1363 * either way, the show must go on.
1365 (void) utils_set_fd_cloexec(sock
);
1367 /* Create UST registration command for enqueuing */
1368 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1369 if (ust_cmd
== NULL
) {
1370 PERROR("ust command zmalloc");
1379 * Using message-based transmissions to ensure we don't
1380 * have to deal with partially received messages.
1382 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1384 ERR("Exhausted file descriptors allowed for applications.");
1394 health_code_update();
1395 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
1398 /* Close socket of the application. */
1403 lttng_fd_put(LTTNG_FD_APPS
, 1);
1407 health_code_update();
1409 ust_cmd
->sock
= sock
;
1412 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1413 " gid:%d sock:%d name:%s (version %d.%d)",
1414 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1415 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1416 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1417 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1420 * Lock free enqueue the registration request. The red pill
1421 * has been taken! This apps will be part of the *system*.
1423 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
1426 * Wake the registration queue futex. Implicit memory
1427 * barrier with the exchange in cds_wfcq_enqueue.
1429 futex_nto1_wake(&ust_cmd_queue
.futex
);
1430 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1431 ERR("Register apps socket poll error");
1434 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1443 /* Notify that the registration thread is gone */
1446 if (apps_sock
>= 0) {
1447 ret
= close(apps_sock
);
1457 lttng_fd_put(LTTNG_FD_APPS
, 1);
1459 unlink(config
.apps_unix_sock_path
.value
);
1462 lttng_poll_clean(&events
);
1466 DBG("UST Registration thread cleanup complete");
1469 ERR("Health error occurred in %s", __func__
);
1471 health_unregister(health_sessiond
);
1477 * Setup necessary data for kernel tracer action.
1479 static int init_kernel_tracer(void)
1483 /* Modprobe lttng kernel modules */
1484 ret
= modprobe_lttng_control();
1489 /* Open debugfs lttng */
1490 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1491 if (kernel_tracer_fd
< 0) {
1492 DBG("Failed to open %s", module_proc_lttng
);
1496 /* Validate kernel version */
1497 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
1498 &kernel_tracer_abi_version
);
1503 ret
= modprobe_lttng_data();
1508 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
1515 WARN("Kernel tracer does not support buffer monitoring. "
1516 "The monitoring timer of channels in the kernel domain "
1517 "will be set to 0 (disabled).");
1520 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1524 modprobe_remove_lttng_control();
1525 ret
= close(kernel_tracer_fd
);
1529 kernel_tracer_fd
= -1;
1530 return LTTNG_ERR_KERN_VERSION
;
1533 ret
= close(kernel_tracer_fd
);
1539 modprobe_remove_lttng_control();
1542 WARN("No kernel tracer available");
1543 kernel_tracer_fd
= -1;
1545 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1547 return LTTNG_ERR_KERN_NA
;
1551 static int string_match(const char *str1
, const char *str2
)
1553 return (str1
&& str2
) && !strcmp(str1
, str2
);
1557 * Take an option from the getopt output and set it in the right variable to be
1560 * Return 0 on success else a negative value.
1562 static int set_option(int opt
, const char *arg
, const char *optname
)
1566 if (string_match(optname
, "client-sock") || opt
== 'c') {
1567 if (!arg
|| *arg
== '\0') {
1571 if (lttng_is_setuid_setgid()) {
1572 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1573 "-c, --client-sock");
1575 config_string_set(&config
.client_unix_sock_path
,
1577 if (!config
.client_unix_sock_path
.value
) {
1582 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
1583 if (!arg
|| *arg
== '\0') {
1587 if (lttng_is_setuid_setgid()) {
1588 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1591 config_string_set(&config
.apps_unix_sock_path
,
1593 if (!config
.apps_unix_sock_path
.value
) {
1598 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
1599 config
.daemonize
= true;
1600 } else if (string_match(optname
, "background") || opt
== 'b') {
1601 config
.background
= true;
1602 } else if (string_match(optname
, "group") || opt
== 'g') {
1603 if (!arg
|| *arg
== '\0') {
1607 if (lttng_is_setuid_setgid()) {
1608 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1611 config_string_set(&config
.tracing_group_name
,
1613 if (!config
.tracing_group_name
.value
) {
1618 } else if (string_match(optname
, "help") || opt
== 'h') {
1619 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
1621 ERR("Cannot show --help for `lttng-sessiond`");
1624 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
1625 } else if (string_match(optname
, "version") || opt
== 'V') {
1626 fprintf(stdout
, "%s\n", VERSION
);
1628 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
1629 config
.sig_parent
= true;
1630 } else if (string_match(optname
, "kconsumerd-err-sock")) {
1631 if (!arg
|| *arg
== '\0') {
1635 if (lttng_is_setuid_setgid()) {
1636 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1637 "--kconsumerd-err-sock");
1639 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
1641 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
1646 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
1647 if (!arg
|| *arg
== '\0') {
1651 if (lttng_is_setuid_setgid()) {
1652 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1653 "--kconsumerd-cmd-sock");
1655 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
1657 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
1662 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
1663 if (!arg
|| *arg
== '\0') {
1667 if (lttng_is_setuid_setgid()) {
1668 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1669 "--ustconsumerd64-err-sock");
1671 config_string_set(&config
.consumerd64_err_unix_sock_path
,
1673 if (!config
.consumerd64_err_unix_sock_path
.value
) {
1678 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
1679 if (!arg
|| *arg
== '\0') {
1683 if (lttng_is_setuid_setgid()) {
1684 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1685 "--ustconsumerd64-cmd-sock");
1687 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
1689 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
1694 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
1695 if (!arg
|| *arg
== '\0') {
1699 if (lttng_is_setuid_setgid()) {
1700 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1701 "--ustconsumerd32-err-sock");
1703 config_string_set(&config
.consumerd32_err_unix_sock_path
,
1705 if (!config
.consumerd32_err_unix_sock_path
.value
) {
1710 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
1711 if (!arg
|| *arg
== '\0') {
1715 if (lttng_is_setuid_setgid()) {
1716 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1717 "--ustconsumerd32-cmd-sock");
1719 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
1721 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
1726 } else if (string_match(optname
, "no-kernel")) {
1727 config
.no_kernel
= true;
1728 } else if (string_match(optname
, "quiet") || opt
== 'q') {
1729 config
.quiet
= true;
1730 } else if (string_match(optname
, "verbose") || opt
== 'v') {
1731 /* Verbose level can increase using multiple -v */
1733 /* Value obtained from config file */
1734 config
.verbose
= config_parse_value(arg
);
1736 /* -v used on command line */
1739 /* Clamp value to [0, 3] */
1740 config
.verbose
= config
.verbose
< 0 ? 0 :
1741 (config
.verbose
<= 3 ? config
.verbose
: 3);
1742 } else if (string_match(optname
, "verbose-consumer")) {
1744 config
.verbose_consumer
= config_parse_value(arg
);
1746 config
.verbose_consumer
++;
1748 } else if (string_match(optname
, "consumerd32-path")) {
1749 if (!arg
|| *arg
== '\0') {
1753 if (lttng_is_setuid_setgid()) {
1754 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1755 "--consumerd32-path");
1757 config_string_set(&config
.consumerd32_bin_path
,
1759 if (!config
.consumerd32_bin_path
.value
) {
1764 } else if (string_match(optname
, "consumerd32-libdir")) {
1765 if (!arg
|| *arg
== '\0') {
1769 if (lttng_is_setuid_setgid()) {
1770 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1771 "--consumerd32-libdir");
1773 config_string_set(&config
.consumerd32_lib_dir
,
1775 if (!config
.consumerd32_lib_dir
.value
) {
1780 } else if (string_match(optname
, "consumerd64-path")) {
1781 if (!arg
|| *arg
== '\0') {
1785 if (lttng_is_setuid_setgid()) {
1786 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1787 "--consumerd64-path");
1789 config_string_set(&config
.consumerd64_bin_path
,
1791 if (!config
.consumerd64_bin_path
.value
) {
1796 } else if (string_match(optname
, "consumerd64-libdir")) {
1797 if (!arg
|| *arg
== '\0') {
1801 if (lttng_is_setuid_setgid()) {
1802 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1803 "--consumerd64-libdir");
1805 config_string_set(&config
.consumerd64_lib_dir
,
1807 if (!config
.consumerd64_lib_dir
.value
) {
1812 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
1813 if (!arg
|| *arg
== '\0') {
1817 if (lttng_is_setuid_setgid()) {
1818 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1821 config_string_set(&config
.pid_file_path
, strdup(arg
));
1822 if (!config
.pid_file_path
.value
) {
1827 } else if (string_match(optname
, "agent-tcp-port")) {
1828 if (!arg
|| *arg
== '\0') {
1832 if (lttng_is_setuid_setgid()) {
1833 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1834 "--agent-tcp-port");
1839 v
= strtoul(arg
, NULL
, 0);
1840 if (errno
!= 0 || !isdigit(arg
[0])) {
1841 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
1844 if (v
== 0 || v
>= 65535) {
1845 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
1848 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
1849 DBG3("Agent TCP port set to non default: %i", (int) v
);
1851 } else if (string_match(optname
, "load") || opt
== 'l') {
1852 if (!arg
|| *arg
== '\0') {
1856 if (lttng_is_setuid_setgid()) {
1857 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1860 config_string_set(&config
.load_session_path
, strdup(arg
));
1861 if (!config
.load_session_path
.value
) {
1866 } else if (string_match(optname
, "kmod-probes")) {
1867 if (!arg
|| *arg
== '\0') {
1871 if (lttng_is_setuid_setgid()) {
1872 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1875 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
1876 if (!config
.kmod_probes_list
.value
) {
1881 } else if (string_match(optname
, "extra-kmod-probes")) {
1882 if (!arg
|| *arg
== '\0') {
1886 if (lttng_is_setuid_setgid()) {
1887 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
1888 "--extra-kmod-probes");
1890 config_string_set(&config
.kmod_extra_probes_list
,
1892 if (!config
.kmod_extra_probes_list
.value
) {
1897 } else if (string_match(optname
, "config") || opt
== 'f') {
1898 /* This is handled in set_options() thus silent skip. */
1901 /* Unknown option or other error.
1902 * Error is printed by getopt, just return */
1907 if (ret
== -EINVAL
) {
1908 const char *opt_name
= "unknown";
1911 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
1913 if (opt
== long_options
[i
].val
) {
1914 opt_name
= long_options
[i
].name
;
1919 WARN("Invalid argument provided for option \"%s\", using default value.",
1927 * config_entry_handler_cb used to handle options read from a config file.
1928 * See config_entry_handler_cb comment in common/config/session-config.h for the
1929 * return value conventions.
1931 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
1935 if (!entry
|| !entry
->name
|| !entry
->value
) {
1940 /* Check if the option is to be ignored */
1941 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
1942 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
1947 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
1950 /* Ignore if not fully matched. */
1951 if (strcmp(entry
->name
, long_options
[i
].name
)) {
1956 * If the option takes no argument on the command line, we have to
1957 * check if the value is "true". We support non-zero numeric values,
1960 if (!long_options
[i
].has_arg
) {
1961 ret
= config_parse_value(entry
->value
);
1964 WARN("Invalid configuration value \"%s\" for option %s",
1965 entry
->value
, entry
->name
);
1967 /* False, skip boolean config option. */
1972 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
1976 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
1983 * daemon configuration loading and argument parsing
1985 static int set_options(int argc
, char **argv
)
1987 int ret
= 0, c
= 0, option_index
= 0;
1988 int orig_optopt
= optopt
, orig_optind
= optind
;
1990 const char *config_path
= NULL
;
1992 optstring
= utils_generate_optstring(long_options
,
1993 sizeof(long_options
) / sizeof(struct option
));
1999 /* Check for the --config option */
2000 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
2001 &option_index
)) != -1) {
2005 } else if (c
!= 'f') {
2006 /* if not equal to --config option. */
2010 if (lttng_is_setuid_setgid()) {
2011 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
2014 config_path
= utils_expand_path(optarg
);
2016 ERR("Failed to resolve path: %s", optarg
);
2021 ret
= config_get_section_entries(config_path
, config_section_name
,
2022 config_entry_handler
, NULL
);
2025 ERR("Invalid configuration option at line %i", ret
);
2031 /* Reset getopt's global state */
2032 optopt
= orig_optopt
;
2033 optind
= orig_optind
;
2037 * getopt_long() will not set option_index if it encounters a
2040 c
= getopt_long(argc
, argv
, optstring
, long_options
,
2047 * Pass NULL as the long option name if popt left the index
2050 ret
= set_option(c
, optarg
,
2051 option_index
< 0 ? NULL
:
2052 long_options
[option_index
].name
);
2064 * Creates the application socket.
2066 static int init_daemon_socket(void)
2071 old_umask
= umask(0);
2073 /* Create the application unix socket */
2074 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
2075 if (apps_sock
< 0) {
2076 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
2081 /* Set the cloexec flag */
2082 ret
= utils_set_fd_cloexec(apps_sock
);
2084 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
2085 "Continuing but note that the consumer daemon will have a "
2086 "reference to this socket on exec()", apps_sock
);
2089 /* File permission MUST be 666 */
2090 ret
= chmod(config
.apps_unix_sock_path
.value
,
2091 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2093 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
2098 DBG3("Session daemon application socket %d created",
2107 * Create lockfile using the rundir and return its fd.
2109 static int create_lockfile(void)
2111 return utils_create_lock_file(config
.lock_file_path
.value
);
2115 * Check if the global socket is available, and if a daemon is answering at the
2116 * other side. If yes, error is returned.
2118 * Also attempts to create and hold the lock file.
2120 static int check_existing_daemon(void)
2124 /* Is there anybody out there ? */
2125 if (lttng_session_daemon_alive()) {
2130 lockfile_fd
= create_lockfile();
2131 if (lockfile_fd
< 0) {
2139 static void sessiond_cleanup_lock_file(void)
2144 * Cleanup lock file by deleting it and finaly closing it which will
2145 * release the file system lock.
2147 if (lockfile_fd
>= 0) {
2148 ret
= remove(config
.lock_file_path
.value
);
2150 PERROR("remove lock file");
2152 ret
= close(lockfile_fd
);
2154 PERROR("close lock file");
2160 * Set the tracing group gid onto the client socket.
2162 * Race window between mkdir and chown is OK because we are going from more
2163 * permissive (root.root) to less permissive (root.tracing).
2165 static int set_permissions(char *rundir
)
2170 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
2172 /* Set lttng run dir */
2173 ret
= chown(rundir
, 0, gid
);
2175 ERR("Unable to set group on %s", rundir
);
2180 * Ensure all applications and tracing group can search the run
2181 * dir. Allow everyone to read the directory, since it does not
2182 * buy us anything to hide its content.
2184 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
2186 ERR("Unable to set permissions on %s", rundir
);
2190 /* lttng client socket path */
2191 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
2193 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
2197 /* kconsumer error socket path */
2198 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
2200 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
2204 /* 64-bit ustconsumer error socket path */
2205 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
2207 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
2211 /* 32-bit ustconsumer compat32 error socket path */
2212 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
2214 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
2218 DBG("All permissions are set");
2224 * Create the lttng run directory needed for all global sockets and pipe.
2226 static int create_lttng_rundir(void)
2230 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
2232 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
2234 if (errno
!= EEXIST
) {
2235 ERR("Unable to create %s", config
.rundir
.value
);
2247 * Setup sockets and directory needed by the consumerds' communication with the
2250 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
2255 switch (consumer_data
->type
) {
2256 case LTTNG_CONSUMER_KERNEL
:
2257 path
= config
.kconsumerd_path
.value
;
2259 case LTTNG_CONSUMER64_UST
:
2260 path
= config
.consumerd64_path
.value
;
2262 case LTTNG_CONSUMER32_UST
:
2263 path
= config
.consumerd32_path
.value
;
2266 ERR("Consumer type unknown");
2272 DBG2("Creating consumer directory: %s", path
);
2274 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
2275 if (ret
< 0 && errno
!= EEXIST
) {
2277 ERR("Failed to create %s", path
);
2281 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
2283 ERR("Unable to set group on %s", path
);
2289 /* Create the consumerd error unix socket */
2290 consumer_data
->err_sock
=
2291 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
2292 if (consumer_data
->err_sock
< 0) {
2293 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
2299 * Set the CLOEXEC flag. Return code is useless because either way, the
2302 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
2304 PERROR("utils_set_fd_cloexec");
2305 /* continue anyway */
2308 /* File permission MUST be 660 */
2309 ret
= chmod(consumer_data
->err_unix_sock_path
,
2310 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2312 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
2322 * Signal handler for the daemon
2324 * Simply stop all worker threads, leaving main() return gracefully after
2325 * joining all threads and calling cleanup().
2327 static void sighandler(int sig
)
2331 DBG("SIGINT caught");
2335 DBG("SIGTERM caught");
2339 CMM_STORE_SHARED(recv_child_signal
, 1);
2347 * Setup signal handler for :
2348 * SIGINT, SIGTERM, SIGPIPE
2350 static int set_signal_handler(void)
2353 struct sigaction sa
;
2356 if ((ret
= sigemptyset(&sigset
)) < 0) {
2357 PERROR("sigemptyset");
2361 sa
.sa_mask
= sigset
;
2364 sa
.sa_handler
= sighandler
;
2365 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2366 PERROR("sigaction");
2370 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2371 PERROR("sigaction");
2375 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
2376 PERROR("sigaction");
2380 sa
.sa_handler
= SIG_IGN
;
2381 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2382 PERROR("sigaction");
2386 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
2392 * Set open files limit to unlimited. This daemon can open a large number of
2393 * file descriptors in order to consume multiple kernel traces.
2395 static void set_ulimit(void)
2400 /* The kernel does not allow an infinite limit for open files */
2401 lim
.rlim_cur
= 65535;
2402 lim
.rlim_max
= 65535;
2404 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2406 PERROR("failed to set open files limit");
2410 static int write_pidfile(void)
2412 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
2415 static int set_clock_plugin_env(void)
2418 char *env_value
= NULL
;
2420 if (!config
.lttng_ust_clock_plugin
.value
) {
2424 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
2425 config
.lttng_ust_clock_plugin
.value
);
2431 ret
= putenv(env_value
);
2434 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
2438 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
2439 config
.lttng_ust_clock_plugin
.value
);
2444 static void destroy_all_sessions_and_wait(void)
2446 struct ltt_session
*session
, *tmp
;
2447 struct ltt_session_list
*session_list
;
2449 session_list
= session_get_list();
2450 DBG("Initiating destruction of all sessions");
2452 if (!session_list
) {
2456 session_lock_list();
2457 /* Initiate the destruction of all sessions. */
2458 cds_list_for_each_entry_safe(session
, tmp
,
2459 &session_list
->head
, list
) {
2460 if (!session_get(session
)) {
2464 session_lock(session
);
2465 if (session
->destroyed
) {
2466 goto unlock_session
;
2468 (void) cmd_destroy_session(session
,
2469 notification_thread_handle
);
2471 session_unlock(session
);
2472 session_put(session
);
2474 session_unlock_list();
2476 /* Wait for the destruction of all sessions to complete. */
2477 DBG("Waiting for the destruction of all sessions to complete");
2478 session_list_wait_empty();
2479 DBG("Destruction of all sessions completed");
2485 int main(int argc
, char **argv
)
2487 int ret
= 0, retval
= 0;
2489 const char *env_app_timeout
;
2490 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
2491 *ust64_channel_monitor_pipe
= NULL
,
2492 *kernel_channel_monitor_pipe
= NULL
;
2493 struct lttng_thread
*ht_cleanup_thread
= NULL
;
2494 struct timer_thread_parameters timer_thread_parameters
;
2495 /* Rotation thread handle. */
2496 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
2497 /* Queue of rotation jobs populated by the sessiond-timer. */
2498 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
2499 struct lttng_thread
*client_thread
= NULL
;
2501 init_kernel_workarounds();
2503 rcu_register_thread();
2505 if (set_signal_handler()) {
2507 goto exit_set_signal_handler
;
2510 if (timer_signal_init()) {
2512 goto exit_set_signal_handler
;
2515 page_size
= sysconf(_SC_PAGESIZE
);
2516 if (page_size
< 0) {
2517 PERROR("sysconf _SC_PAGESIZE");
2518 page_size
= LONG_MAX
;
2519 WARN("Fallback page size to %ld", page_size
);
2522 ret
= sessiond_config_init(&config
);
2525 goto exit_set_signal_handler
;
2529 * Init config from environment variables.
2530 * Command line option override env configuration per-doc. Do env first.
2532 sessiond_config_apply_env_config(&config
);
2535 * Parse arguments and load the daemon configuration file.
2537 * We have an exit_options exit path to free memory reserved by
2538 * set_options. This is needed because the rest of sessiond_cleanup()
2539 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
2540 * depends on set_options.
2543 if (set_options(argc
, argv
)) {
2549 * Resolve all paths received as arguments, configuration option, or
2550 * through environment variable as absolute paths. This is necessary
2551 * since daemonizing causes the sessiond's current working directory
2554 ret
= sessiond_config_resolve_paths(&config
);
2560 lttng_opt_verbose
= config
.verbose
;
2561 lttng_opt_quiet
= config
.quiet
;
2562 kconsumer_data
.err_unix_sock_path
=
2563 config
.kconsumerd_err_unix_sock_path
.value
;
2564 kconsumer_data
.cmd_unix_sock_path
=
2565 config
.kconsumerd_cmd_unix_sock_path
.value
;
2566 ustconsumer32_data
.err_unix_sock_path
=
2567 config
.consumerd32_err_unix_sock_path
.value
;
2568 ustconsumer32_data
.cmd_unix_sock_path
=
2569 config
.consumerd32_cmd_unix_sock_path
.value
;
2570 ustconsumer64_data
.err_unix_sock_path
=
2571 config
.consumerd64_err_unix_sock_path
.value
;
2572 ustconsumer64_data
.cmd_unix_sock_path
=
2573 config
.consumerd64_cmd_unix_sock_path
.value
;
2574 set_clock_plugin_env();
2576 sessiond_config_log(&config
);
2578 if (create_lttng_rundir()) {
2583 /* Abort launch if a session daemon is already running. */
2584 if (check_existing_daemon()) {
2585 ERR("A session daemon is already running.");
2591 if (config
.daemonize
|| config
.background
) {
2594 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
2595 !config
.background
);
2602 * We are in the child. Make sure all other file descriptors are
2603 * closed, in case we are called with more opened file
2604 * descriptors than the standard ones and the lock file.
2606 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
2607 if (i
== lockfile_fd
) {
2614 if (run_as_create_worker(argv
[0]) < 0) {
2615 goto exit_create_run_as_worker_cleanup
;
2619 * Starting from here, we can create threads. This needs to be after
2620 * lttng_daemonize due to RCU.
2624 * Initialize the health check subsystem. This call should set the
2625 * appropriate time values.
2627 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
2628 if (!health_sessiond
) {
2629 PERROR("health_app_create error");
2631 goto exit_health_sessiond_cleanup
;
2634 /* Create thread to clean up RCU hash tables */
2635 ht_cleanup_thread
= launch_ht_cleanup_thread();
2636 if (!ht_cleanup_thread
) {
2638 goto exit_ht_cleanup
;
2641 /* Create thread quit pipe */
2642 if (sessiond_init_thread_quit_pipe()) {
2644 goto exit_init_data
;
2647 /* Check if daemon is UID = 0 */
2648 is_root
= !getuid();
2650 /* Create global run dir with root access */
2652 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
2653 if (!kernel_channel_monitor_pipe
) {
2654 ERR("Failed to create kernel consumer channel monitor pipe");
2656 goto exit_init_data
;
2658 kconsumer_data
.channel_monitor_pipe
=
2659 lttng_pipe_release_writefd(
2660 kernel_channel_monitor_pipe
);
2661 if (kconsumer_data
.channel_monitor_pipe
< 0) {
2663 goto exit_init_data
;
2667 /* Set consumer initial state */
2668 kernel_consumerd_state
= CONSUMER_STOPPED
;
2669 ust_consumerd_state
= CONSUMER_STOPPED
;
2671 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
2672 if (!ust32_channel_monitor_pipe
) {
2673 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
2675 goto exit_init_data
;
2677 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
2678 ust32_channel_monitor_pipe
);
2679 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
2681 goto exit_init_data
;
2685 * The rotation_thread_timer_queue structure is shared between the
2686 * sessiond timer thread and the rotation thread. The main thread keeps
2687 * its ownership and destroys it when both threads have been joined.
2689 rotation_timer_queue
= rotation_thread_timer_queue_create();
2690 if (!rotation_timer_queue
) {
2692 goto exit_init_data
;
2694 timer_thread_parameters
.rotation_thread_job_queue
=
2695 rotation_timer_queue
;
2697 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
2698 if (!ust64_channel_monitor_pipe
) {
2699 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
2701 goto exit_init_data
;
2703 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
2704 ust64_channel_monitor_pipe
);
2705 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
2707 goto exit_init_data
;
2711 * Init UST app hash table. Alloc hash table before this point since
2712 * cleanup() can get called after that point.
2714 if (ust_app_ht_alloc()) {
2715 ERR("Failed to allocate UST app hash table");
2717 goto exit_init_data
;
2721 * Initialize agent app hash table. We allocate the hash table here
2722 * since cleanup() can get called after this point.
2724 if (agent_app_ht_alloc()) {
2725 ERR("Failed to allocate Agent app hash table");
2727 goto exit_init_data
;
2731 * These actions must be executed as root. We do that *after* setting up
2732 * the sockets path because we MUST make the check for another daemon using
2733 * those paths *before* trying to set the kernel consumer sockets and init
2737 if (set_consumer_sockets(&kconsumer_data
)) {
2739 goto exit_init_data
;
2742 /* Setup kernel tracer */
2743 if (!config
.no_kernel
) {
2744 init_kernel_tracer();
2745 if (kernel_tracer_fd
>= 0) {
2746 ret
= syscall_init_table();
2748 ERR("Unable to populate syscall table. "
2749 "Syscall tracing won't work "
2750 "for this session daemon.");
2755 /* Set ulimit for open files */
2758 /* init lttng_fd tracking must be done after set_ulimit. */
2761 if (set_consumer_sockets(&ustconsumer64_data
)) {
2763 goto exit_init_data
;
2766 if (set_consumer_sockets(&ustconsumer32_data
)) {
2768 goto exit_init_data
;
2771 /* Setup the needed unix socket */
2772 if (init_daemon_socket()) {
2774 goto exit_init_data
;
2777 /* Set credentials to socket */
2778 if (is_root
&& set_permissions(config
.rundir
.value
)) {
2780 goto exit_init_data
;
2783 /* Get parent pid if -S, --sig-parent is specified. */
2784 if (config
.sig_parent
) {
2788 /* Setup the kernel pipe for waking up the kernel thread */
2789 if (is_root
&& !config
.no_kernel
) {
2790 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
2792 goto exit_init_data
;
2796 /* Setup the thread apps communication pipe. */
2797 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
2799 goto exit_init_data
;
2802 /* Setup the thread apps notify communication pipe. */
2803 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
2805 goto exit_init_data
;
2808 /* Initialize global buffer per UID and PID registry. */
2809 buffer_reg_init_uid_registry();
2810 buffer_reg_init_pid_registry();
2812 /* Init UST command queue. */
2813 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2817 /* Check for the application socket timeout env variable. */
2818 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
2819 if (env_app_timeout
) {
2820 config
.app_socket_timeout
= atoi(env_app_timeout
);
2822 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
2825 ret
= write_pidfile();
2827 ERR("Error in write_pidfile");
2829 goto exit_init_data
;
2832 /* Initialize communication library */
2834 /* Initialize TCP timeout values */
2835 lttcomm_inet_init();
2837 if (load_session_init_data(&load_info
) < 0) {
2839 goto exit_init_data
;
2841 load_info
->path
= config
.load_session_path
.value
;
2843 /* Create health-check thread. */
2844 if (!launch_health_management_thread()) {
2849 /* notification_thread_data acquires the pipes' read side. */
2850 notification_thread_handle
= notification_thread_handle_create(
2851 ust32_channel_monitor_pipe
,
2852 ust64_channel_monitor_pipe
,
2853 kernel_channel_monitor_pipe
);
2854 if (!notification_thread_handle
) {
2856 ERR("Failed to create notification thread shared data");
2857 goto exit_notification
;
2860 /* Create notification thread. */
2861 if (!launch_notification_thread(notification_thread_handle
)) {
2863 goto exit_notification
;
2866 /* Create timer thread. */
2867 if (!launch_timer_thread(&timer_thread_parameters
)) {
2869 goto exit_notification
;
2872 /* rotation_thread_data acquires the pipes' read side. */
2873 rotation_thread_handle
= rotation_thread_handle_create(
2874 rotation_timer_queue
,
2875 notification_thread_handle
);
2876 if (!rotation_thread_handle
) {
2878 ERR("Failed to create rotation thread shared data");
2883 /* Create rotation thread. */
2884 if (!launch_rotation_thread(rotation_thread_handle
)) {
2889 /* Create thread to manage the client socket */
2890 client_thread
= launch_client_thread();
2891 if (!client_thread
) {
2896 if (!launch_ust_dispatch_thread(&ust_cmd_queue
, apps_cmd_pipe
[1],
2897 apps_cmd_notify_pipe
[1])) {
2902 /* Create thread to manage application registration. */
2903 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
2904 thread_registration_apps
, (void *) NULL
);
2907 PERROR("pthread_create registration");
2913 /* Create thread to manage application socket */
2914 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
2915 thread_manage_apps
, (void *) NULL
);
2918 PERROR("pthread_create apps");
2924 /* Create thread to manage application notify socket */
2925 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
2926 ust_thread_manage_notify
, (void *) NULL
);
2929 PERROR("pthread_create notify");
2932 goto exit_apps_notify
;
2935 /* Create agent registration thread. */
2936 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
2937 agent_thread_manage_registration
, (void *) NULL
);
2940 PERROR("pthread_create agent");
2943 goto exit_agent_reg
;
2946 /* Don't start this thread if kernel tracing is not requested nor root */
2947 if (is_root
&& !config
.no_kernel
) {
2948 /* Create kernel thread to manage kernel event */
2949 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
2950 thread_manage_kernel
, (void *) NULL
);
2953 PERROR("pthread_create kernel");
2960 /* Create session loading thread. */
2961 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
2962 thread_load_session
, load_info
);
2965 PERROR("pthread_create load_session_thread");
2968 goto exit_load_session
;
2972 * This is where we start awaiting program completion (e.g. through
2973 * signal that asks threads to teardown).
2976 ret
= pthread_join(load_session_thread
, &status
);
2979 PERROR("pthread_join load_session_thread");
2983 /* Initiate teardown once activity occurs on the quit pipe. */
2984 sessiond_wait_for_quit_pipe(-1U);
2987 * Ensure that the client thread is no longer accepting new commands,
2988 * which could cause new sessions to be created.
2990 if (!lttng_thread_shutdown(client_thread
)) {
2991 ERR("Failed to shutdown the client thread, continuing teardown");
2992 lttng_thread_put(client_thread
);
2993 client_thread
= NULL
;
2996 destroy_all_sessions_and_wait();
2999 if (is_root
&& !config
.no_kernel
) {
3000 ret
= pthread_join(kernel_thread
, &status
);
3003 PERROR("pthread_join");
3009 ret
= pthread_join(agent_reg_thread
, &status
);
3012 PERROR("pthread_join agent");
3017 ret
= pthread_join(apps_notify_thread
, &status
);
3020 PERROR("pthread_join apps notify");
3025 ret
= pthread_join(apps_thread
, &status
);
3028 PERROR("pthread_join apps");
3033 ret
= pthread_join(reg_apps_thread
, &status
);
3036 PERROR("pthread_join");
3044 lttng_thread_list_shutdown_orphans();
3047 if (client_thread
) {
3048 lttng_thread_put(client_thread
);
3052 * Wait for all pending call_rcu work to complete before tearing
3053 * down data structures. call_rcu worker may be trying to
3054 * perform lookups in those structures.
3058 * sessiond_cleanup() is called when no other thread is running, except
3059 * the ht_cleanup thread, which is needed to destroy the hash tables.
3061 rcu_thread_online();
3065 * Ensure all prior call_rcu are done. call_rcu callbacks may push
3066 * hash tables to the ht_cleanup thread. Therefore, we ensure that
3067 * the queue is empty before shutting down the clean-up thread.
3071 if (ht_cleanup_thread
) {
3072 lttng_thread_shutdown(ht_cleanup_thread
);
3073 lttng_thread_put(ht_cleanup_thread
);
3076 rcu_thread_offline();
3077 rcu_unregister_thread();
3079 if (rotation_thread_handle
) {
3080 rotation_thread_handle_destroy(rotation_thread_handle
);
3084 * After the rotation and timer thread have quit, we can safely destroy
3085 * the rotation_timer_queue.
3087 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
3089 * The teardown of the notification system is performed after the
3090 * session daemon's teardown in order to allow it to be notified
3091 * of the active session and channels at the moment of the teardown.
3093 if (notification_thread_handle
) {
3094 notification_thread_handle_destroy(notification_thread_handle
);
3096 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
3097 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
3098 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
3101 health_app_destroy(health_sessiond
);
3102 exit_health_sessiond_cleanup
:
3103 exit_create_run_as_worker_cleanup
:
3106 sessiond_cleanup_lock_file();
3107 sessiond_cleanup_options();
3109 exit_set_signal_handler
: