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/userspace-probe-internal.h>
54 #include <lttng/event-internal.h>
56 #include "lttng-sessiond.h"
57 #include "buffer-registry.h"
64 #include "kernel-consumer.h"
68 #include "ust-consumer.h"
71 #include "health-sessiond.h"
72 #include "testpoint.h"
73 #include "ust-thread.h"
74 #include "agent-thread.h"
76 #include "load-session-thread.h"
77 #include "notification-thread.h"
78 #include "notification-thread-commands.h"
79 #include "rotation-thread.h"
80 #include "lttng-syscall.h"
82 #include "ht-cleanup.h"
83 #include "sessiond-config.h"
84 #include "sessiond-timer.h"
86 static const char *help_msg
=
87 #ifdef LTTNG_EMBED_HELP
88 #include <lttng-sessiond.8.h>
95 static pid_t ppid
; /* Parent PID for --sig-parent option */
96 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
97 static int lockfile_fd
= -1;
99 /* Set to 1 when a SIGUSR1 signal is received. */
100 static int recv_child_signal
;
102 static struct lttng_kernel_tracer_version kernel_tracer_version
;
103 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
106 * Consumer daemon specific control data. Every value not initialized here is
107 * set to 0 by the static definition.
109 static struct consumer_data kconsumer_data
= {
110 .type
= LTTNG_CONSUMER_KERNEL
,
113 .channel_monitor_pipe
= -1,
114 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
115 .lock
= PTHREAD_MUTEX_INITIALIZER
,
116 .cond
= PTHREAD_COND_INITIALIZER
,
117 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
119 static struct consumer_data ustconsumer64_data
= {
120 .type
= LTTNG_CONSUMER64_UST
,
123 .channel_monitor_pipe
= -1,
124 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
125 .lock
= PTHREAD_MUTEX_INITIALIZER
,
126 .cond
= PTHREAD_COND_INITIALIZER
,
127 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
129 static struct consumer_data ustconsumer32_data
= {
130 .type
= LTTNG_CONSUMER32_UST
,
133 .channel_monitor_pipe
= -1,
134 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
135 .lock
= PTHREAD_MUTEX_INITIALIZER
,
136 .cond
= PTHREAD_COND_INITIALIZER
,
137 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
140 /* Command line options */
141 static const struct option long_options
[] = {
142 { "client-sock", required_argument
, 0, 'c' },
143 { "apps-sock", required_argument
, 0, 'a' },
144 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
145 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
146 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
147 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
148 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
149 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
150 { "consumerd32-path", required_argument
, 0, '\0' },
151 { "consumerd32-libdir", required_argument
, 0, '\0' },
152 { "consumerd64-path", required_argument
, 0, '\0' },
153 { "consumerd64-libdir", required_argument
, 0, '\0' },
154 { "daemonize", no_argument
, 0, 'd' },
155 { "background", no_argument
, 0, 'b' },
156 { "sig-parent", no_argument
, 0, 'S' },
157 { "help", no_argument
, 0, 'h' },
158 { "group", required_argument
, 0, 'g' },
159 { "version", no_argument
, 0, 'V' },
160 { "quiet", no_argument
, 0, 'q' },
161 { "verbose", no_argument
, 0, 'v' },
162 { "verbose-consumer", no_argument
, 0, '\0' },
163 { "no-kernel", no_argument
, 0, '\0' },
164 { "pidfile", required_argument
, 0, 'p' },
165 { "agent-tcp-port", required_argument
, 0, '\0' },
166 { "config", required_argument
, 0, 'f' },
167 { "load", required_argument
, 0, 'l' },
168 { "kmod-probes", required_argument
, 0, '\0' },
169 { "extra-kmod-probes", required_argument
, 0, '\0' },
173 struct sessiond_config config
;
175 /* Command line options to ignore from configuration file */
176 static const char *config_ignore_options
[] = { "help", "version", "config" };
178 /* Shared between threads */
179 static int dispatch_thread_exit
;
181 /* Sockets and FDs */
182 static int client_sock
= -1;
183 static int apps_sock
= -1;
184 int kernel_tracer_fd
= -1;
185 static int kernel_poll_pipe
[2] = { -1, -1 };
188 * Quit pipe for all threads. This permits a single cancellation point
189 * for all threads when receiving an event on the pipe.
191 static int thread_quit_pipe
[2] = { -1, -1 };
194 * This pipe is used to inform the thread managing application communication
195 * that a command is queued and ready to be processed.
197 static int apps_cmd_pipe
[2] = { -1, -1 };
199 int apps_cmd_notify_pipe
[2] = { -1, -1 };
201 /* Pthread, Mutexes and Semaphores */
202 static pthread_t apps_thread
;
203 static pthread_t apps_notify_thread
;
204 static pthread_t reg_apps_thread
;
205 static pthread_t client_thread
;
206 static pthread_t kernel_thread
;
207 static pthread_t dispatch_thread
;
208 static pthread_t health_thread
;
209 static pthread_t ht_cleanup_thread
;
210 static pthread_t agent_reg_thread
;
211 static pthread_t load_session_thread
;
212 static pthread_t notification_thread
;
213 static pthread_t rotation_thread
;
214 static pthread_t timer_thread
;
217 * UST registration command queue. This queue is tied with a futex and uses a N
218 * wakers / 1 waiter implemented and detailed in futex.c/.h
220 * The thread_registration_apps and thread_dispatch_ust_registration uses this
221 * queue along with the wait/wake scheme. The thread_manage_apps receives down
222 * the line new application socket and monitors it for any I/O error or clean
223 * close that triggers an unregistration of the application.
225 static struct ust_cmd_queue ust_cmd_queue
;
228 * Pointer initialized before thread creation.
230 * This points to the tracing session list containing the session count and a
231 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
232 * MUST NOT be taken if you call a public function in session.c.
234 * The lock is nested inside the structure: session_list_ptr->lock. Please use
235 * session_lock_list and session_unlock_list for lock acquisition.
237 static struct ltt_session_list
*session_list_ptr
;
239 int ust_consumerd64_fd
= -1;
240 int ust_consumerd32_fd
= -1;
242 static const char *module_proc_lttng
= "/proc/lttng";
245 * Consumer daemon state which is changed when spawning it, killing it or in
246 * case of a fatal error.
248 enum consumerd_state
{
249 CONSUMER_STARTED
= 1,
250 CONSUMER_STOPPED
= 2,
255 * This consumer daemon state is used to validate if a client command will be
256 * able to reach the consumer. If not, the client is informed. For instance,
257 * doing a "lttng start" when the consumer state is set to ERROR will return an
258 * error to the client.
260 * The following example shows a possible race condition of this scheme:
262 * consumer thread error happens
264 * client cmd checks state -> still OK
265 * consumer thread exit, sets error
266 * client cmd try to talk to consumer
269 * However, since the consumer is a different daemon, we have no way of making
270 * sure the command will reach it safely even with this state flag. This is why
271 * we consider that up to the state validation during command processing, the
272 * command is safe. After that, we can not guarantee the correctness of the
273 * client request vis-a-vis the consumer.
275 static enum consumerd_state ust_consumerd_state
;
276 static enum consumerd_state kernel_consumerd_state
;
278 /* Set in main() with the current page size. */
281 /* Application health monitoring */
282 struct health_app
*health_sessiond
;
284 /* Am I root or not. */
285 int is_root
; /* Set to 1 if the daemon is running as root */
287 const char * const config_section_name
= "sessiond";
289 /* Load session thread information to operate. */
290 struct load_session_thread_data
*load_info
;
292 /* Notification thread handle. */
293 struct notification_thread_handle
*notification_thread_handle
;
295 /* Rotation thread handle. */
296 struct rotation_thread_handle
*rotation_thread_handle
;
298 /* Global hash tables */
299 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
302 * The initialization of the session daemon is done in multiple phases.
304 * While all threads are launched near-simultaneously, only some of them
305 * are needed to ensure the session daemon can start to respond to client
308 * There are two important guarantees that we wish to offer with respect
309 * to the initialisation of the session daemon:
310 * - When the daemonize/background launcher process exits, the sessiond
311 * is fully able to respond to client requests,
312 * - Auto-loaded sessions are visible to clients.
314 * In order to achieve this, a number of support threads have to be launched
315 * to allow the "client" thread to function properly. Moreover, since the
316 * "load session" thread needs the client thread, we must provide a way
317 * for the "load session" thread to know that the "client" thread is up
320 * Hence, the support threads decrement the lttng_sessiond_ready counter
321 * while the "client" threads waits for it to reach 0. Once the "client" thread
322 * unblocks, it posts the message_thread_ready semaphore which allows the
323 * "load session" thread to progress.
325 * This implies that the "load session" thread is the last to be initialized
326 * and will explicitly call sessiond_signal_parents(), which signals the parents
327 * that the session daemon is fully initialized.
329 * The four (4) support threads are:
331 * - notification_thread
335 #define NR_LTTNG_SESSIOND_SUPPORT_THREADS 4
336 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_SUPPORT_THREADS
;
338 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
340 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
343 /* Notify parents that we are ready for cmd and health check */
345 void sessiond_signal_parents(void)
348 * Notify parent pid that we are ready to accept command
349 * for client side. This ppid is the one from the
350 * external process that spawned us.
352 if (config
.sig_parent
) {
357 * Notify the parent of the fork() process that we are
360 if (config
.daemonize
|| config
.background
) {
361 kill(child_ppid
, SIGUSR1
);
366 void sessiond_notify_ready(void)
369 * This memory barrier is paired with the one performed by
370 * the client thread after it has seen that 'lttng_sessiond_ready' is 0.
372 * The purpose of these memory barriers is to ensure that all
373 * initialization operations of the various threads that call this
374 * function to signal that they are ready are commited/published
375 * before the client thread can see the 'lttng_sessiond_ready' counter
378 * Note that this could be a 'write' memory barrier, but a full barrier
379 * is used in case the code using this utility changes. The performance
380 * implications of this choice are minimal since this is a slow path.
383 uatomic_sub(<tng_sessiond_ready
, 1);
387 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
394 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
400 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
412 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
414 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
416 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
420 * Init thread quit pipe.
422 * Return -1 on error or 0 if all pipes are created.
424 static int __init_thread_quit_pipe(int *a_pipe
)
430 PERROR("thread quit pipe");
434 for (i
= 0; i
< 2; i
++) {
435 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
446 static int init_thread_quit_pipe(void)
448 return __init_thread_quit_pipe(thread_quit_pipe
);
452 * Stop all threads by closing the thread quit pipe.
454 static void stop_threads(void)
458 /* Stopping all threads */
459 DBG("Terminating all threads");
460 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
462 ERR("write error on thread quit pipe");
465 /* Dispatch thread */
466 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
467 futex_nto1_wake(&ust_cmd_queue
.futex
);
471 * Close every consumer sockets.
473 static void close_consumer_sockets(void)
477 if (kconsumer_data
.err_sock
>= 0) {
478 ret
= close(kconsumer_data
.err_sock
);
480 PERROR("kernel consumer err_sock close");
483 if (ustconsumer32_data
.err_sock
>= 0) {
484 ret
= close(ustconsumer32_data
.err_sock
);
486 PERROR("UST consumerd32 err_sock close");
489 if (ustconsumer64_data
.err_sock
>= 0) {
490 ret
= close(ustconsumer64_data
.err_sock
);
492 PERROR("UST consumerd64 err_sock close");
495 if (kconsumer_data
.cmd_sock
>= 0) {
496 ret
= close(kconsumer_data
.cmd_sock
);
498 PERROR("kernel consumer cmd_sock close");
501 if (ustconsumer32_data
.cmd_sock
>= 0) {
502 ret
= close(ustconsumer32_data
.cmd_sock
);
504 PERROR("UST consumerd32 cmd_sock close");
507 if (ustconsumer64_data
.cmd_sock
>= 0) {
508 ret
= close(ustconsumer64_data
.cmd_sock
);
510 PERROR("UST consumerd64 cmd_sock close");
513 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
514 ret
= close(kconsumer_data
.channel_monitor_pipe
);
516 PERROR("kernel consumer channel monitor pipe close");
519 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
520 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
522 PERROR("UST consumerd32 channel monitor pipe close");
525 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
526 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
528 PERROR("UST consumerd64 channel monitor pipe close");
534 * Wait on consumer process termination.
536 * Need to be called with the consumer data lock held or from a context
537 * ensuring no concurrent access to data (e.g: cleanup).
539 static void wait_consumer(struct consumer_data
*consumer_data
)
544 if (consumer_data
->pid
<= 0) {
548 DBG("Waiting for complete teardown of consumerd (PID: %d)",
550 ret
= waitpid(consumer_data
->pid
, &status
, 0);
552 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
553 } else if (!WIFEXITED(status
)) {
554 ERR("consumerd termination with error: %d",
557 consumer_data
->pid
= 0;
561 * Cleanup the session daemon's data structures.
563 static void sessiond_cleanup(void)
566 struct ltt_session
*sess
, *stmp
;
568 DBG("Cleanup sessiond");
571 * Close the thread quit pipe. It has already done its job,
572 * since we are now called.
574 utils_close_pipe(thread_quit_pipe
);
576 ret
= remove(config
.pid_file_path
.value
);
578 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
581 DBG("Removing sessiond and consumerd content of directory %s",
582 config
.rundir
.value
);
585 DBG("Removing %s", config
.pid_file_path
.value
);
586 (void) unlink(config
.pid_file_path
.value
);
588 DBG("Removing %s", config
.agent_port_file_path
.value
);
589 (void) unlink(config
.agent_port_file_path
.value
);
592 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
593 (void) unlink(kconsumer_data
.err_unix_sock_path
);
595 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
596 (void) rmdir(config
.kconsumerd_path
.value
);
598 /* ust consumerd 32 */
599 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
600 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
602 DBG("Removing directory %s", config
.consumerd32_path
.value
);
603 (void) rmdir(config
.consumerd32_path
.value
);
605 /* ust consumerd 64 */
606 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
607 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
609 DBG("Removing directory %s", config
.consumerd64_path
.value
);
610 (void) rmdir(config
.consumerd64_path
.value
);
612 DBG("Cleaning up all sessions");
614 /* Destroy session list mutex */
615 if (session_list_ptr
!= NULL
) {
616 pthread_mutex_destroy(&session_list_ptr
->lock
);
618 /* Cleanup ALL session */
619 cds_list_for_each_entry_safe(sess
, stmp
,
620 &session_list_ptr
->head
, list
) {
621 cmd_destroy_session(sess
, kernel_poll_pipe
[1],
622 notification_thread_handle
);
626 wait_consumer(&kconsumer_data
);
627 wait_consumer(&ustconsumer64_data
);
628 wait_consumer(&ustconsumer32_data
);
630 DBG("Cleaning up all agent apps");
631 agent_app_ht_clean();
633 DBG("Closing all UST sockets");
634 ust_app_clean_list();
635 buffer_reg_destroy_registries();
637 if (is_root
&& !config
.no_kernel
) {
638 DBG2("Closing kernel fd");
639 if (kernel_tracer_fd
>= 0) {
640 ret
= close(kernel_tracer_fd
);
645 DBG("Unloading kernel modules");
646 modprobe_remove_lttng_all();
650 close_consumer_sockets();
653 load_session_destroy_data(load_info
);
658 * We do NOT rmdir rundir because there are other processes
659 * using it, for instance lttng-relayd, which can start in
660 * parallel with this teardown.
665 * Cleanup the daemon's option data structures.
667 static void sessiond_cleanup_options(void)
669 DBG("Cleaning up options");
671 sessiond_config_fini(&config
);
673 run_as_destroy_worker();
677 * Send data on a unix socket using the liblttsessiondcomm API.
679 * Return lttcomm error code.
681 static int send_unix_sock(int sock
, void *buf
, size_t len
)
683 /* Check valid length */
688 return lttcomm_send_unix_sock(sock
, buf
, len
);
692 * Free memory of a command context structure.
694 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
696 DBG("Clean command context structure");
698 if ((*cmd_ctx
)->llm
) {
699 free((*cmd_ctx
)->llm
);
701 if ((*cmd_ctx
)->lsm
) {
702 free((*cmd_ctx
)->lsm
);
710 * Notify UST applications using the shm mmap futex.
712 static int notify_ust_apps(int active
)
716 DBG("Notifying applications of session daemon state: %d", active
);
718 /* See shm.c for this call implying mmap, shm and futex calls */
719 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
720 if (wait_shm_mmap
== NULL
) {
724 /* Wake waiting process */
725 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
727 /* Apps notified successfully */
735 * Setup the outgoing data buffer for the response (llm) by allocating the
736 * right amount of memory and copying the original information from the lsm
739 * Return 0 on success, negative value on error.
741 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
742 const void *payload_buf
, size_t payload_len
,
743 const void *cmd_header_buf
, size_t cmd_header_len
)
746 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
747 const size_t cmd_header_offset
= header_len
;
748 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
749 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
751 cmd_ctx
->llm
= zmalloc(total_msg_size
);
753 if (cmd_ctx
->llm
== NULL
) {
759 /* Copy common data */
760 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
761 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
762 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
763 cmd_ctx
->llm
->data_size
= payload_len
;
764 cmd_ctx
->lttng_msg_size
= total_msg_size
;
766 /* Copy command header */
767 if (cmd_header_len
) {
768 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
774 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
783 * Version of setup_lttng_msg() without command header.
785 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
786 void *payload_buf
, size_t payload_len
)
788 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
791 * Update the kernel poll set of all channel fd available over all tracing
792 * session. Add the wakeup pipe at the end of the set.
794 static int update_kernel_poll(struct lttng_poll_event
*events
)
797 struct ltt_session
*session
;
798 struct ltt_kernel_channel
*channel
;
800 DBG("Updating kernel poll set");
803 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
804 session_lock(session
);
805 if (session
->kernel_session
== NULL
) {
806 session_unlock(session
);
810 cds_list_for_each_entry(channel
,
811 &session
->kernel_session
->channel_list
.head
, list
) {
812 /* Add channel fd to the kernel poll set */
813 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
815 session_unlock(session
);
818 DBG("Channel fd %d added to kernel set", channel
->fd
);
820 session_unlock(session
);
822 session_unlock_list();
827 session_unlock_list();
832 * Find the channel fd from 'fd' over all tracing session. When found, check
833 * for new channel stream and send those stream fds to the kernel consumer.
835 * Useful for CPU hotplug feature.
837 static int update_kernel_stream(int fd
)
840 struct ltt_session
*session
;
841 struct ltt_kernel_session
*ksess
;
842 struct ltt_kernel_channel
*channel
;
844 DBG("Updating kernel streams for channel fd %d", fd
);
847 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
848 session_lock(session
);
849 if (session
->kernel_session
== NULL
) {
850 session_unlock(session
);
853 ksess
= session
->kernel_session
;
855 cds_list_for_each_entry(channel
,
856 &ksess
->channel_list
.head
, list
) {
857 struct lttng_ht_iter iter
;
858 struct consumer_socket
*socket
;
860 if (channel
->fd
!= fd
) {
863 DBG("Channel found, updating kernel streams");
864 ret
= kernel_open_channel_stream(channel
);
868 /* Update the stream global counter */
869 ksess
->stream_count_global
+= ret
;
872 * Have we already sent fds to the consumer? If yes, it
873 * means that tracing is started so it is safe to send
874 * our updated stream fds.
876 if (ksess
->consumer_fds_sent
!= 1
877 || ksess
->consumer
== NULL
) {
883 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
884 &iter
.iter
, socket
, node
.node
) {
885 pthread_mutex_lock(socket
->lock
);
886 ret
= kernel_consumer_send_channel_streams(socket
,
888 session
->output_traces
? 1 : 0);
889 pthread_mutex_unlock(socket
->lock
);
897 session_unlock(session
);
899 session_unlock_list();
903 session_unlock(session
);
904 session_unlock_list();
909 * For each tracing session, update newly registered apps. The session list
910 * lock MUST be acquired before calling this.
912 static void update_ust_app(int app_sock
)
914 struct ltt_session
*sess
, *stmp
;
916 /* Consumer is in an ERROR state. Stop any application update. */
917 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
918 /* Stop the update process since the consumer is dead. */
922 /* For all tracing session(s) */
923 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
927 if (!sess
->ust_session
) {
932 assert(app_sock
>= 0);
933 app
= ust_app_find_by_sock(app_sock
);
936 * Application can be unregistered before so
937 * this is possible hence simply stopping the
940 DBG3("UST app update failed to find app sock %d",
944 ust_app_global_update(sess
->ust_session
, app
);
948 session_unlock(sess
);
953 * This thread manage event coming from the kernel.
955 * Features supported in this thread:
958 static void *thread_manage_kernel(void *data
)
960 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
961 uint32_t revents
, nb_fd
;
963 struct lttng_poll_event events
;
965 DBG("[thread] Thread manage kernel started");
967 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
970 * This first step of the while is to clean this structure which could free
971 * non NULL pointers so initialize it before the loop.
973 lttng_poll_init(&events
);
975 if (testpoint(sessiond_thread_manage_kernel
)) {
976 goto error_testpoint
;
979 health_code_update();
981 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
982 goto error_testpoint
;
986 health_code_update();
988 if (update_poll_flag
== 1) {
989 /* Clean events object. We are about to populate it again. */
990 lttng_poll_clean(&events
);
992 ret
= sessiond_set_thread_pollset(&events
, 2);
994 goto error_poll_create
;
997 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1002 /* This will add the available kernel channel if any. */
1003 ret
= update_kernel_poll(&events
);
1007 update_poll_flag
= 0;
1010 DBG("Thread kernel polling");
1012 /* Poll infinite value of time */
1014 health_poll_entry();
1015 ret
= lttng_poll_wait(&events
, -1);
1016 DBG("Thread kernel return from poll on %d fds",
1017 LTTNG_POLL_GETNB(&events
));
1021 * Restart interrupted system call.
1023 if (errno
== EINTR
) {
1027 } else if (ret
== 0) {
1028 /* Should not happen since timeout is infinite */
1029 ERR("Return value of poll is 0 with an infinite timeout.\n"
1030 "This should not have happened! Continuing...");
1036 for (i
= 0; i
< nb_fd
; i
++) {
1037 /* Fetch once the poll data */
1038 revents
= LTTNG_POLL_GETEV(&events
, i
);
1039 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1041 health_code_update();
1044 /* No activity for this FD (poll implementation). */
1048 /* Thread quit pipe has been closed. Killing thread. */
1049 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1055 /* Check for data on kernel pipe */
1056 if (revents
& LPOLLIN
) {
1057 if (pollfd
== kernel_poll_pipe
[0]) {
1058 (void) lttng_read(kernel_poll_pipe
[0],
1061 * Ret value is useless here, if this pipe gets any actions an
1062 * update is required anyway.
1064 update_poll_flag
= 1;
1068 * New CPU detected by the kernel. Adding kernel stream to
1069 * kernel session and updating the kernel consumer
1071 ret
= update_kernel_stream(pollfd
);
1077 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1078 update_poll_flag
= 1;
1081 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1089 lttng_poll_clean(&events
);
1092 utils_close_pipe(kernel_poll_pipe
);
1093 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1096 ERR("Health error occurred in %s", __func__
);
1097 WARN("Kernel thread died unexpectedly. "
1098 "Kernel tracing can continue but CPU hotplug is disabled.");
1100 health_unregister(health_sessiond
);
1101 DBG("Kernel thread dying");
1106 * Signal pthread condition of the consumer data that the thread.
1108 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1110 pthread_mutex_lock(&data
->cond_mutex
);
1113 * The state is set before signaling. It can be any value, it's the waiter
1114 * job to correctly interpret this condition variable associated to the
1115 * consumer pthread_cond.
1117 * A value of 0 means that the corresponding thread of the consumer data
1118 * was not started. 1 indicates that the thread has started and is ready
1119 * for action. A negative value means that there was an error during the
1122 data
->consumer_thread_is_ready
= state
;
1123 (void) pthread_cond_signal(&data
->cond
);
1125 pthread_mutex_unlock(&data
->cond_mutex
);
1129 * This thread manage the consumer error sent back to the session daemon.
1131 static void *thread_manage_consumer(void *data
)
1133 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1134 uint32_t revents
, nb_fd
;
1135 enum lttcomm_return_code code
;
1136 struct lttng_poll_event events
;
1137 struct consumer_data
*consumer_data
= data
;
1138 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1140 DBG("[thread] Manage consumer started");
1142 rcu_register_thread();
1143 rcu_thread_online();
1145 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1147 health_code_update();
1150 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1151 * metadata_sock. Nothing more will be added to this poll set.
1153 ret
= sessiond_set_thread_pollset(&events
, 3);
1159 * The error socket here is already in a listening state which was done
1160 * just before spawning this thread to avoid a race between the consumer
1161 * daemon exec trying to connect and the listen() call.
1163 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1168 health_code_update();
1170 /* Infinite blocking call, waiting for transmission */
1172 health_poll_entry();
1174 if (testpoint(sessiond_thread_manage_consumer
)) {
1178 ret
= lttng_poll_wait(&events
, -1);
1182 * Restart interrupted system call.
1184 if (errno
== EINTR
) {
1192 for (i
= 0; i
< nb_fd
; i
++) {
1193 /* Fetch once the poll data */
1194 revents
= LTTNG_POLL_GETEV(&events
, i
);
1195 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1197 health_code_update();
1200 /* No activity for this FD (poll implementation). */
1204 /* Thread quit pipe has been closed. Killing thread. */
1205 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1211 /* Event on the registration socket */
1212 if (pollfd
== consumer_data
->err_sock
) {
1213 if (revents
& LPOLLIN
) {
1215 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1216 ERR("consumer err socket poll error");
1219 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1225 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1231 * Set the CLOEXEC flag. Return code is useless because either way, the
1234 (void) utils_set_fd_cloexec(sock
);
1236 health_code_update();
1238 DBG2("Receiving code from consumer err_sock");
1240 /* Getting status code from kconsumerd */
1241 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1242 sizeof(enum lttcomm_return_code
));
1247 health_code_update();
1248 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1249 ERR("consumer error when waiting for SOCK_READY : %s",
1250 lttcomm_get_readable_code(-code
));
1254 /* Connect both command and metadata sockets. */
1255 consumer_data
->cmd_sock
=
1256 lttcomm_connect_unix_sock(
1257 consumer_data
->cmd_unix_sock_path
);
1258 consumer_data
->metadata_fd
=
1259 lttcomm_connect_unix_sock(
1260 consumer_data
->cmd_unix_sock_path
);
1261 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1262 PERROR("consumer connect cmd socket");
1263 /* On error, signal condition and quit. */
1264 signal_consumer_condition(consumer_data
, -1);
1268 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1270 /* Create metadata socket lock. */
1271 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1272 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1273 PERROR("zmalloc pthread mutex");
1276 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1278 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1279 DBG("Consumer metadata socket ready (fd: %d)",
1280 consumer_data
->metadata_fd
);
1283 * Remove the consumerd error sock since we've established a connection.
1285 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1290 /* Add new accepted error socket. */
1291 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1296 /* Add metadata socket that is successfully connected. */
1297 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1298 LPOLLIN
| LPOLLRDHUP
);
1303 health_code_update();
1306 * Transfer the write-end of the channel monitoring and rotate pipe
1307 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1309 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1310 if (!cmd_socket_wrapper
) {
1313 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1315 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1316 consumer_data
->channel_monitor_pipe
);
1321 /* Discard the socket wrapper as it is no longer needed. */
1322 consumer_destroy_socket(cmd_socket_wrapper
);
1323 cmd_socket_wrapper
= NULL
;
1325 /* The thread is completely initialized, signal that it is ready. */
1326 signal_consumer_condition(consumer_data
, 1);
1328 /* Infinite blocking call, waiting for transmission */
1331 health_code_update();
1333 /* Exit the thread because the thread quit pipe has been triggered. */
1335 /* Not a health error. */
1340 health_poll_entry();
1341 ret
= lttng_poll_wait(&events
, -1);
1345 * Restart interrupted system call.
1347 if (errno
== EINTR
) {
1355 for (i
= 0; i
< nb_fd
; i
++) {
1356 /* Fetch once the poll data */
1357 revents
= LTTNG_POLL_GETEV(&events
, i
);
1358 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1360 health_code_update();
1363 /* No activity for this FD (poll implementation). */
1368 * Thread quit pipe has been triggered, flag that we should stop
1369 * but continue the current loop to handle potential data from
1372 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1374 if (pollfd
== sock
) {
1375 /* Event on the consumerd socket */
1376 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1377 && !(revents
& LPOLLIN
)) {
1378 ERR("consumer err socket second poll error");
1381 health_code_update();
1382 /* Wait for any kconsumerd error */
1383 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1384 sizeof(enum lttcomm_return_code
));
1386 ERR("consumer closed the command socket");
1390 ERR("consumer return code : %s",
1391 lttcomm_get_readable_code(-code
));
1394 } else if (pollfd
== consumer_data
->metadata_fd
) {
1395 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1396 && !(revents
& LPOLLIN
)) {
1397 ERR("consumer err metadata socket second poll error");
1400 /* UST metadata requests */
1401 ret
= ust_consumer_metadata_request(
1402 &consumer_data
->metadata_sock
);
1404 ERR("Handling metadata request");
1408 /* No need for an else branch all FDs are tested prior. */
1410 health_code_update();
1416 * We lock here because we are about to close the sockets and some other
1417 * thread might be using them so get exclusive access which will abort all
1418 * other consumer command by other threads.
1420 pthread_mutex_lock(&consumer_data
->lock
);
1422 /* Immediately set the consumerd state to stopped */
1423 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1424 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1425 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1426 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1427 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1429 /* Code flow error... */
1433 if (consumer_data
->err_sock
>= 0) {
1434 ret
= close(consumer_data
->err_sock
);
1438 consumer_data
->err_sock
= -1;
1440 if (consumer_data
->cmd_sock
>= 0) {
1441 ret
= close(consumer_data
->cmd_sock
);
1445 consumer_data
->cmd_sock
= -1;
1447 if (consumer_data
->metadata_sock
.fd_ptr
&&
1448 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1449 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1461 unlink(consumer_data
->err_unix_sock_path
);
1462 unlink(consumer_data
->cmd_unix_sock_path
);
1463 pthread_mutex_unlock(&consumer_data
->lock
);
1465 /* Cleanup metadata socket mutex. */
1466 if (consumer_data
->metadata_sock
.lock
) {
1467 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1468 free(consumer_data
->metadata_sock
.lock
);
1470 lttng_poll_clean(&events
);
1472 if (cmd_socket_wrapper
) {
1473 consumer_destroy_socket(cmd_socket_wrapper
);
1478 ERR("Health error occurred in %s", __func__
);
1480 health_unregister(health_sessiond
);
1481 DBG("consumer thread cleanup completed");
1483 rcu_thread_offline();
1484 rcu_unregister_thread();
1490 * This thread receives application command sockets (FDs) on the
1491 * apps_cmd_pipe and waits (polls) on them until they are closed
1492 * or an error occurs.
1494 * At that point, it flushes the data (tracing and metadata) associated
1495 * with this application and tears down ust app sessions and other
1496 * associated data structures through ust_app_unregister().
1498 * Note that this thread never sends commands to the applications
1499 * through the command sockets; it merely listens for hang-ups
1500 * and errors on those sockets and cleans-up as they occur.
1502 static void *thread_manage_apps(void *data
)
1504 int i
, ret
, pollfd
, err
= -1;
1506 uint32_t revents
, nb_fd
;
1507 struct lttng_poll_event events
;
1509 DBG("[thread] Manage application started");
1511 rcu_register_thread();
1512 rcu_thread_online();
1514 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1516 if (testpoint(sessiond_thread_manage_apps
)) {
1517 goto error_testpoint
;
1520 health_code_update();
1522 ret
= sessiond_set_thread_pollset(&events
, 2);
1524 goto error_poll_create
;
1527 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1532 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1536 health_code_update();
1539 DBG("Apps thread polling");
1541 /* Inifinite blocking call, waiting for transmission */
1543 health_poll_entry();
1544 ret
= lttng_poll_wait(&events
, -1);
1545 DBG("Apps thread return from poll on %d fds",
1546 LTTNG_POLL_GETNB(&events
));
1550 * Restart interrupted system call.
1552 if (errno
== EINTR
) {
1560 for (i
= 0; i
< nb_fd
; i
++) {
1561 /* Fetch once the poll data */
1562 revents
= LTTNG_POLL_GETEV(&events
, i
);
1563 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1565 health_code_update();
1568 /* No activity for this FD (poll implementation). */
1572 /* Thread quit pipe has been closed. Killing thread. */
1573 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1579 /* Inspect the apps cmd pipe */
1580 if (pollfd
== apps_cmd_pipe
[0]) {
1581 if (revents
& LPOLLIN
) {
1585 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1586 if (size_ret
< sizeof(sock
)) {
1587 PERROR("read apps cmd pipe");
1591 health_code_update();
1594 * Since this is a command socket (write then read),
1595 * we only monitor the error events of the socket.
1597 ret
= lttng_poll_add(&events
, sock
,
1598 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1603 DBG("Apps with sock %d added to poll set", sock
);
1604 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1605 ERR("Apps command pipe error");
1608 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1613 * At this point, we know that a registered application made
1614 * the event at poll_wait.
1616 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1617 /* Removing from the poll set */
1618 ret
= lttng_poll_del(&events
, pollfd
);
1623 /* Socket closed on remote end. */
1624 ust_app_unregister(pollfd
);
1626 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1631 health_code_update();
1637 lttng_poll_clean(&events
);
1640 utils_close_pipe(apps_cmd_pipe
);
1641 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1644 * We don't clean the UST app hash table here since already registered
1645 * applications can still be controlled so let them be until the session
1646 * daemon dies or the applications stop.
1651 ERR("Health error occurred in %s", __func__
);
1653 health_unregister(health_sessiond
);
1654 DBG("Application communication apps thread cleanup complete");
1655 rcu_thread_offline();
1656 rcu_unregister_thread();
1661 * Send a socket to a thread This is called from the dispatch UST registration
1662 * thread once all sockets are set for the application.
1664 * The sock value can be invalid, we don't really care, the thread will handle
1665 * it and make the necessary cleanup if so.
1667 * On success, return 0 else a negative value being the errno message of the
1670 static int send_socket_to_thread(int fd
, int sock
)
1675 * It's possible that the FD is set as invalid with -1 concurrently just
1676 * before calling this function being a shutdown state of the thread.
1683 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1684 if (ret
< sizeof(sock
)) {
1685 PERROR("write apps pipe %d", fd
);
1692 /* All good. Don't send back the write positive ret value. */
1699 * Sanitize the wait queue of the dispatch registration thread meaning removing
1700 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1701 * notify socket is never received.
1703 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1705 int ret
, nb_fd
= 0, i
;
1706 unsigned int fd_added
= 0;
1707 struct lttng_poll_event events
;
1708 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1712 lttng_poll_init(&events
);
1714 /* Just skip everything for an empty queue. */
1715 if (!wait_queue
->count
) {
1719 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1724 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1725 &wait_queue
->head
, head
) {
1726 assert(wait_node
->app
);
1727 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1728 LPOLLHUP
| LPOLLERR
);
1741 * Poll but don't block so we can quickly identify the faulty events and
1742 * clean them afterwards from the wait queue.
1744 ret
= lttng_poll_wait(&events
, 0);
1750 for (i
= 0; i
< nb_fd
; i
++) {
1751 /* Get faulty FD. */
1752 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1753 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1756 /* No activity for this FD (poll implementation). */
1760 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1761 &wait_queue
->head
, head
) {
1762 if (pollfd
== wait_node
->app
->sock
&&
1763 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1764 cds_list_del(&wait_node
->head
);
1765 wait_queue
->count
--;
1766 ust_app_destroy(wait_node
->app
);
1769 * Silence warning of use-after-free in
1770 * cds_list_for_each_entry_safe which uses
1771 * __typeof__(*wait_node).
1776 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1783 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1787 lttng_poll_clean(&events
);
1791 lttng_poll_clean(&events
);
1793 ERR("Unable to sanitize wait queue");
1798 * Dispatch request from the registration threads to the application
1799 * communication thread.
1801 static void *thread_dispatch_ust_registration(void *data
)
1804 struct cds_wfcq_node
*node
;
1805 struct ust_command
*ust_cmd
= NULL
;
1806 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1807 struct ust_reg_wait_queue wait_queue
= {
1811 rcu_register_thread();
1813 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1815 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1816 goto error_testpoint
;
1819 health_code_update();
1821 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1823 DBG("[thread] Dispatch UST command started");
1826 health_code_update();
1828 /* Atomically prepare the queue futex */
1829 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1831 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1836 struct ust_app
*app
= NULL
;
1840 * Make sure we don't have node(s) that have hung up before receiving
1841 * the notify socket. This is to clean the list in order to avoid
1842 * memory leaks from notify socket that are never seen.
1844 sanitize_wait_queue(&wait_queue
);
1846 health_code_update();
1847 /* Dequeue command for registration */
1848 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1850 DBG("Woken up but nothing in the UST command queue");
1851 /* Continue thread execution */
1855 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1857 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1858 " gid:%d sock:%d name:%s (version %d.%d)",
1859 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1860 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1861 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1862 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1864 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1865 wait_node
= zmalloc(sizeof(*wait_node
));
1867 PERROR("zmalloc wait_node dispatch");
1868 ret
= close(ust_cmd
->sock
);
1870 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1872 lttng_fd_put(LTTNG_FD_APPS
, 1);
1876 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1878 /* Create application object if socket is CMD. */
1879 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1881 if (!wait_node
->app
) {
1882 ret
= close(ust_cmd
->sock
);
1884 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1886 lttng_fd_put(LTTNG_FD_APPS
, 1);
1892 * Add application to the wait queue so we can set the notify
1893 * socket before putting this object in the global ht.
1895 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1900 * We have to continue here since we don't have the notify
1901 * socket and the application MUST be added to the hash table
1902 * only at that moment.
1907 * Look for the application in the local wait queue and set the
1908 * notify socket if found.
1910 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1911 &wait_queue
.head
, head
) {
1912 health_code_update();
1913 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1914 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1915 cds_list_del(&wait_node
->head
);
1917 app
= wait_node
->app
;
1919 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1925 * With no application at this stage the received socket is
1926 * basically useless so close it before we free the cmd data
1927 * structure for good.
1930 ret
= close(ust_cmd
->sock
);
1932 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1934 lttng_fd_put(LTTNG_FD_APPS
, 1);
1941 * @session_lock_list
1943 * Lock the global session list so from the register up to the
1944 * registration done message, no thread can see the application
1945 * and change its state.
1947 session_lock_list();
1951 * Add application to the global hash table. This needs to be
1952 * done before the update to the UST registry can locate the
1957 /* Set app version. This call will print an error if needed. */
1958 (void) ust_app_version(app
);
1960 /* Send notify socket through the notify pipe. */
1961 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1965 session_unlock_list();
1967 * No notify thread, stop the UST tracing. However, this is
1968 * not an internal error of the this thread thus setting
1969 * the health error code to a normal exit.
1976 * Update newly registered application with the tracing
1977 * registry info already enabled information.
1979 update_ust_app(app
->sock
);
1982 * Don't care about return value. Let the manage apps threads
1983 * handle app unregistration upon socket close.
1985 (void) ust_app_register_done(app
);
1988 * Even if the application socket has been closed, send the app
1989 * to the thread and unregistration will take place at that
1992 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1995 session_unlock_list();
1997 * No apps. thread, stop the UST tracing. However, this is
1998 * not an internal error of the this thread thus setting
1999 * the health error code to a normal exit.
2006 session_unlock_list();
2008 } while (node
!= NULL
);
2010 health_poll_entry();
2011 /* Futex wait on queue. Blocking call on futex() */
2012 futex_nto1_wait(&ust_cmd_queue
.futex
);
2015 /* Normal exit, no error */
2019 /* Clean up wait queue. */
2020 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2021 &wait_queue
.head
, head
) {
2022 cds_list_del(&wait_node
->head
);
2027 /* Empty command queue. */
2029 /* Dequeue command for registration */
2030 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2034 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2035 ret
= close(ust_cmd
->sock
);
2037 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2039 lttng_fd_put(LTTNG_FD_APPS
, 1);
2044 DBG("Dispatch thread dying");
2047 ERR("Health error occurred in %s", __func__
);
2049 health_unregister(health_sessiond
);
2050 rcu_unregister_thread();
2055 * This thread manage application registration.
2057 static void *thread_registration_apps(void *data
)
2059 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2060 uint32_t revents
, nb_fd
;
2061 struct lttng_poll_event events
;
2063 * Get allocated in this thread, enqueued to a global queue, dequeued and
2064 * freed in the manage apps thread.
2066 struct ust_command
*ust_cmd
= NULL
;
2068 DBG("[thread] Manage application registration started");
2070 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2072 if (testpoint(sessiond_thread_registration_apps
)) {
2073 goto error_testpoint
;
2076 ret
= lttcomm_listen_unix_sock(apps_sock
);
2082 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2083 * more will be added to this poll set.
2085 ret
= sessiond_set_thread_pollset(&events
, 2);
2087 goto error_create_poll
;
2090 /* Add the application registration socket */
2091 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2093 goto error_poll_add
;
2096 /* Notify all applications to register */
2097 ret
= notify_ust_apps(1);
2099 ERR("Failed to notify applications or create the wait shared memory.\n"
2100 "Execution continues but there might be problem for already\n"
2101 "running applications that wishes to register.");
2105 DBG("Accepting application registration");
2107 /* Inifinite blocking call, waiting for transmission */
2109 health_poll_entry();
2110 ret
= lttng_poll_wait(&events
, -1);
2114 * Restart interrupted system call.
2116 if (errno
== EINTR
) {
2124 for (i
= 0; i
< nb_fd
; i
++) {
2125 health_code_update();
2127 /* Fetch once the poll data */
2128 revents
= LTTNG_POLL_GETEV(&events
, i
);
2129 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2132 /* No activity for this FD (poll implementation). */
2136 /* Thread quit pipe has been closed. Killing thread. */
2137 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2143 /* Event on the registration socket */
2144 if (pollfd
== apps_sock
) {
2145 if (revents
& LPOLLIN
) {
2146 sock
= lttcomm_accept_unix_sock(apps_sock
);
2152 * Set socket timeout for both receiving and ending.
2153 * app_socket_timeout is in seconds, whereas
2154 * lttcomm_setsockopt_rcv_timeout and
2155 * lttcomm_setsockopt_snd_timeout expect msec as
2158 if (config
.app_socket_timeout
>= 0) {
2159 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2160 config
.app_socket_timeout
* 1000);
2161 (void) lttcomm_setsockopt_snd_timeout(sock
,
2162 config
.app_socket_timeout
* 1000);
2166 * Set the CLOEXEC flag. Return code is useless because
2167 * either way, the show must go on.
2169 (void) utils_set_fd_cloexec(sock
);
2171 /* Create UST registration command for enqueuing */
2172 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2173 if (ust_cmd
== NULL
) {
2174 PERROR("ust command zmalloc");
2183 * Using message-based transmissions to ensure we don't
2184 * have to deal with partially received messages.
2186 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2188 ERR("Exhausted file descriptors allowed for applications.");
2198 health_code_update();
2199 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2202 /* Close socket of the application. */
2207 lttng_fd_put(LTTNG_FD_APPS
, 1);
2211 health_code_update();
2213 ust_cmd
->sock
= sock
;
2216 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2217 " gid:%d sock:%d name:%s (version %d.%d)",
2218 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2219 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2220 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2221 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2224 * Lock free enqueue the registration request. The red pill
2225 * has been taken! This apps will be part of the *system*.
2227 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2230 * Wake the registration queue futex. Implicit memory
2231 * barrier with the exchange in cds_wfcq_enqueue.
2233 futex_nto1_wake(&ust_cmd_queue
.futex
);
2234 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2235 ERR("Register apps socket poll error");
2238 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2247 /* Notify that the registration thread is gone */
2250 if (apps_sock
>= 0) {
2251 ret
= close(apps_sock
);
2261 lttng_fd_put(LTTNG_FD_APPS
, 1);
2263 unlink(config
.apps_unix_sock_path
.value
);
2266 lttng_poll_clean(&events
);
2270 DBG("UST Registration thread cleanup complete");
2273 ERR("Health error occurred in %s", __func__
);
2275 health_unregister(health_sessiond
);
2281 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2282 * exec or it will fails.
2284 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2287 struct timespec timeout
;
2290 * Make sure we set the readiness flag to 0 because we are NOT ready.
2291 * This access to consumer_thread_is_ready does not need to be
2292 * protected by consumer_data.cond_mutex (yet) since the consumer
2293 * management thread has not been started at this point.
2295 consumer_data
->consumer_thread_is_ready
= 0;
2297 /* Setup pthread condition */
2298 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2301 PERROR("pthread_condattr_init consumer data");
2306 * Set the monotonic clock in order to make sure we DO NOT jump in time
2307 * between the clock_gettime() call and the timedwait call. See bug #324
2308 * for a more details and how we noticed it.
2310 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2313 PERROR("pthread_condattr_setclock consumer data");
2317 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2320 PERROR("pthread_cond_init consumer data");
2324 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2325 thread_manage_consumer
, consumer_data
);
2328 PERROR("pthread_create consumer");
2333 /* We are about to wait on a pthread condition */
2334 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2336 /* Get time for sem_timedwait absolute timeout */
2337 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2339 * Set the timeout for the condition timed wait even if the clock gettime
2340 * call fails since we might loop on that call and we want to avoid to
2341 * increment the timeout too many times.
2343 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2346 * The following loop COULD be skipped in some conditions so this is why we
2347 * set ret to 0 in order to make sure at least one round of the loop is
2353 * Loop until the condition is reached or when a timeout is reached. Note
2354 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2355 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2356 * possible. This loop does not take any chances and works with both of
2359 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2360 if (clock_ret
< 0) {
2361 PERROR("clock_gettime spawn consumer");
2362 /* Infinite wait for the consumerd thread to be ready */
2363 ret
= pthread_cond_wait(&consumer_data
->cond
,
2364 &consumer_data
->cond_mutex
);
2366 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2367 &consumer_data
->cond_mutex
, &timeout
);
2371 /* Release the pthread condition */
2372 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2376 if (ret
== ETIMEDOUT
) {
2380 * Call has timed out so we kill the kconsumerd_thread and return
2383 ERR("Condition timed out. The consumer thread was never ready."
2385 pth_ret
= pthread_cancel(consumer_data
->thread
);
2387 PERROR("pthread_cancel consumer thread");
2390 PERROR("pthread_cond_wait failed consumer thread");
2392 /* Caller is expecting a negative value on failure. */
2397 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2398 if (consumer_data
->pid
== 0) {
2399 ERR("Consumerd did not start");
2400 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2403 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2412 * Join consumer thread
2414 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2418 /* Consumer pid must be a real one. */
2419 if (consumer_data
->pid
> 0) {
2421 ret
= kill(consumer_data
->pid
, SIGTERM
);
2423 PERROR("Error killing consumer daemon");
2426 return pthread_join(consumer_data
->thread
, &status
);
2433 * Fork and exec a consumer daemon (consumerd).
2435 * Return pid if successful else -1.
2437 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2441 const char *consumer_to_use
;
2442 const char *verbosity
;
2445 DBG("Spawning consumerd");
2452 if (config
.verbose_consumer
) {
2453 verbosity
= "--verbose";
2454 } else if (lttng_opt_quiet
) {
2455 verbosity
= "--quiet";
2460 switch (consumer_data
->type
) {
2461 case LTTNG_CONSUMER_KERNEL
:
2463 * Find out which consumerd to execute. We will first try the
2464 * 64-bit path, then the sessiond's installation directory, and
2465 * fallback on the 32-bit one,
2467 DBG3("Looking for a kernel consumer at these locations:");
2468 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2469 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2470 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2471 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2472 DBG3("Found location #1");
2473 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2474 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2475 DBG3("Found location #2");
2476 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2477 } else if (config
.consumerd32_bin_path
.value
&&
2478 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2479 DBG3("Found location #3");
2480 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2482 DBG("Could not find any valid consumerd executable");
2486 DBG("Using kernel consumer at: %s", consumer_to_use
);
2487 (void) execl(consumer_to_use
,
2488 "lttng-consumerd", verbosity
, "-k",
2489 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2490 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2491 "--group", config
.tracing_group_name
.value
,
2494 case LTTNG_CONSUMER64_UST
:
2496 if (config
.consumerd64_lib_dir
.value
) {
2501 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2505 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2506 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2511 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2512 if (tmp
[0] != '\0') {
2513 strcat(tmpnew
, ":");
2514 strcat(tmpnew
, tmp
);
2516 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2523 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2524 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2525 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2526 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2527 "--group", config
.tracing_group_name
.value
,
2531 case LTTNG_CONSUMER32_UST
:
2533 if (config
.consumerd32_lib_dir
.value
) {
2538 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2542 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2543 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2548 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2549 if (tmp
[0] != '\0') {
2550 strcat(tmpnew
, ":");
2551 strcat(tmpnew
, tmp
);
2553 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2560 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2561 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2562 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2563 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2564 "--group", config
.tracing_group_name
.value
,
2569 ERR("unknown consumer type");
2573 PERROR("Consumer execl()");
2575 /* Reaching this point, we got a failure on our execl(). */
2577 } else if (pid
> 0) {
2580 PERROR("start consumer fork");
2588 * Spawn the consumerd daemon and session daemon thread.
2590 static int start_consumerd(struct consumer_data
*consumer_data
)
2595 * Set the listen() state on the socket since there is a possible race
2596 * between the exec() of the consumer daemon and this call if place in the
2597 * consumer thread. See bug #366 for more details.
2599 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2604 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2605 if (consumer_data
->pid
!= 0) {
2606 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2610 ret
= spawn_consumerd(consumer_data
);
2612 ERR("Spawning consumerd failed");
2613 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2617 /* Setting up the consumer_data pid */
2618 consumer_data
->pid
= ret
;
2619 DBG2("Consumer pid %d", consumer_data
->pid
);
2620 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2622 DBG2("Spawning consumer control thread");
2623 ret
= spawn_consumer_thread(consumer_data
);
2625 ERR("Fatal error spawning consumer control thread");
2633 /* Cleanup already created sockets on error. */
2634 if (consumer_data
->err_sock
>= 0) {
2637 err
= close(consumer_data
->err_sock
);
2639 PERROR("close consumer data error socket");
2646 * Setup necessary data for kernel tracer action.
2648 static int init_kernel_tracer(void)
2652 /* Modprobe lttng kernel modules */
2653 ret
= modprobe_lttng_control();
2658 /* Open debugfs lttng */
2659 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2660 if (kernel_tracer_fd
< 0) {
2661 DBG("Failed to open %s", module_proc_lttng
);
2665 /* Validate kernel version */
2666 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2667 &kernel_tracer_abi_version
);
2672 ret
= modprobe_lttng_data();
2677 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2684 WARN("Kernel tracer does not support buffer monitoring. "
2685 "The monitoring timer of channels in the kernel domain "
2686 "will be set to 0 (disabled).");
2689 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2693 modprobe_remove_lttng_control();
2694 ret
= close(kernel_tracer_fd
);
2698 kernel_tracer_fd
= -1;
2699 return LTTNG_ERR_KERN_VERSION
;
2702 ret
= close(kernel_tracer_fd
);
2708 modprobe_remove_lttng_control();
2711 WARN("No kernel tracer available");
2712 kernel_tracer_fd
= -1;
2714 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2716 return LTTNG_ERR_KERN_NA
;
2722 * Copy consumer output from the tracing session to the domain session. The
2723 * function also applies the right modification on a per domain basis for the
2724 * trace files destination directory.
2726 * Should *NOT* be called with RCU read-side lock held.
2728 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2731 const char *dir_name
;
2732 struct consumer_output
*consumer
;
2735 assert(session
->consumer
);
2738 case LTTNG_DOMAIN_KERNEL
:
2739 DBG3("Copying tracing session consumer output in kernel session");
2741 * XXX: We should audit the session creation and what this function
2742 * does "extra" in order to avoid a destroy since this function is used
2743 * in the domain session creation (kernel and ust) only. Same for UST
2746 if (session
->kernel_session
->consumer
) {
2747 consumer_output_put(session
->kernel_session
->consumer
);
2749 session
->kernel_session
->consumer
=
2750 consumer_copy_output(session
->consumer
);
2751 /* Ease our life a bit for the next part */
2752 consumer
= session
->kernel_session
->consumer
;
2753 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2755 case LTTNG_DOMAIN_JUL
:
2756 case LTTNG_DOMAIN_LOG4J
:
2757 case LTTNG_DOMAIN_PYTHON
:
2758 case LTTNG_DOMAIN_UST
:
2759 DBG3("Copying tracing session consumer output in UST session");
2760 if (session
->ust_session
->consumer
) {
2761 consumer_output_put(session
->ust_session
->consumer
);
2763 session
->ust_session
->consumer
=
2764 consumer_copy_output(session
->consumer
);
2765 /* Ease our life a bit for the next part */
2766 consumer
= session
->ust_session
->consumer
;
2767 dir_name
= DEFAULT_UST_TRACE_DIR
;
2770 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2774 /* Append correct directory to subdir */
2775 strncat(consumer
->subdir
, dir_name
,
2776 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2777 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2786 * Create an UST session and add it to the session ust list.
2788 * Should *NOT* be called with RCU read-side lock held.
2790 static int create_ust_session(struct ltt_session
*session
,
2791 struct lttng_domain
*domain
)
2794 struct ltt_ust_session
*lus
= NULL
;
2798 assert(session
->consumer
);
2800 switch (domain
->type
) {
2801 case LTTNG_DOMAIN_JUL
:
2802 case LTTNG_DOMAIN_LOG4J
:
2803 case LTTNG_DOMAIN_PYTHON
:
2804 case LTTNG_DOMAIN_UST
:
2807 ERR("Unknown UST domain on create session %d", domain
->type
);
2808 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2812 DBG("Creating UST session");
2814 lus
= trace_ust_create_session(session
->id
);
2816 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2820 lus
->uid
= session
->uid
;
2821 lus
->gid
= session
->gid
;
2822 lus
->output_traces
= session
->output_traces
;
2823 lus
->snapshot_mode
= session
->snapshot_mode
;
2824 lus
->live_timer_interval
= session
->live_timer
;
2825 session
->ust_session
= lus
;
2826 if (session
->shm_path
[0]) {
2827 strncpy(lus
->root_shm_path
, session
->shm_path
,
2828 sizeof(lus
->root_shm_path
));
2829 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2830 strncpy(lus
->shm_path
, session
->shm_path
,
2831 sizeof(lus
->shm_path
));
2832 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2833 strncat(lus
->shm_path
, "/ust",
2834 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2836 /* Copy session output to the newly created UST session */
2837 ret
= copy_session_consumer(domain
->type
, session
);
2838 if (ret
!= LTTNG_OK
) {
2846 session
->ust_session
= NULL
;
2851 * Create a kernel tracer session then create the default channel.
2853 static int create_kernel_session(struct ltt_session
*session
)
2857 DBG("Creating kernel session");
2859 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2861 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2865 /* Code flow safety */
2866 assert(session
->kernel_session
);
2868 /* Copy session output to the newly created Kernel session */
2869 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2870 if (ret
!= LTTNG_OK
) {
2874 session
->kernel_session
->uid
= session
->uid
;
2875 session
->kernel_session
->gid
= session
->gid
;
2876 session
->kernel_session
->output_traces
= session
->output_traces
;
2877 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2882 trace_kernel_destroy_session(session
->kernel_session
);
2883 session
->kernel_session
= NULL
;
2888 * Count number of session permitted by uid/gid.
2890 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2893 struct ltt_session
*session
;
2895 DBG("Counting number of available session for UID %d GID %d",
2897 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2899 * Only list the sessions the user can control.
2901 if (!session_access_ok(session
, uid
, gid
)) {
2909 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2910 int *sock_error
, struct lttng_event
*event
)
2913 struct lttng_userspace_probe_location
*probe_location
;
2914 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2915 struct lttng_dynamic_buffer probe_location_buffer
;
2916 struct lttng_buffer_view buffer_view
;
2919 * Create a buffer to store the serialized version of the probe
2922 lttng_dynamic_buffer_init(&probe_location_buffer
);
2923 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2924 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2926 ret
= LTTNG_ERR_NOMEM
;
2931 * Receive the probe location.
2933 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2934 probe_location_buffer
.size
);
2936 DBG("Nothing recv() from client var len data... continuing");
2938 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2939 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2943 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2944 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2947 * Extract the probe location from the serialized version.
2949 ret
= lttng_userspace_probe_location_create_from_buffer(
2950 &buffer_view
, &probe_location
);
2952 WARN("Failed to create a userspace probe location from the received buffer");
2953 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2954 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2959 * Receive the file descriptor to the target binary from the client.
2961 DBG("Receiving userspace probe target FD from client ...");
2962 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2964 DBG("Nothing recv() from client userspace probe fd... continuing");
2966 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2971 * Set the file descriptor received from the client through the unix
2972 * socket in the probe location.
2974 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
2976 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2981 * From the kernel tracer's perspective, all userspace probe event types
2982 * are all the same: a file and an offset.
2984 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
2985 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
2986 ret
= lttng_userspace_probe_location_function_set_binary_fd(
2987 probe_location
, fd
);
2989 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
2990 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
2991 probe_location
, fd
);
2994 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2999 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
3003 /* Attach the probe location to the event. */
3004 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
3006 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
3010 lttng_dynamic_buffer_reset(&probe_location_buffer
);
3016 * Check if the current kernel tracer supports the session rotation feature.
3017 * Return 1 if it does, 0 otherwise.
3019 static int check_rotate_compatible(void)
3023 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
3024 DBG("Kernel tracer version is not compatible with the rotation feature");
3032 * Process the command requested by the lttng client within the command
3033 * context structure. This function make sure that the return structure (llm)
3034 * is set and ready for transmission before returning.
3036 * Return any error encountered or 0 for success.
3038 * "sock" is only used for special-case var. len data.
3040 * Should *NOT* be called with RCU read-side lock held.
3042 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
3046 int need_tracing_session
= 1;
3049 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
3051 assert(!rcu_read_ongoing());
3055 switch (cmd_ctx
->lsm
->cmd_type
) {
3056 case LTTNG_CREATE_SESSION
:
3057 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3058 case LTTNG_CREATE_SESSION_LIVE
:
3059 case LTTNG_DESTROY_SESSION
:
3060 case LTTNG_LIST_SESSIONS
:
3061 case LTTNG_LIST_DOMAINS
:
3062 case LTTNG_START_TRACE
:
3063 case LTTNG_STOP_TRACE
:
3064 case LTTNG_DATA_PENDING
:
3065 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3066 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3067 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3068 case LTTNG_SNAPSHOT_RECORD
:
3069 case LTTNG_SAVE_SESSION
:
3070 case LTTNG_SET_SESSION_SHM_PATH
:
3071 case LTTNG_REGENERATE_METADATA
:
3072 case LTTNG_REGENERATE_STATEDUMP
:
3073 case LTTNG_REGISTER_TRIGGER
:
3074 case LTTNG_UNREGISTER_TRIGGER
:
3075 case LTTNG_ROTATE_SESSION
:
3076 case LTTNG_ROTATION_GET_INFO
:
3077 case LTTNG_ROTATION_SET_SCHEDULE
:
3078 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
3085 if (config
.no_kernel
&& need_domain
3086 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3088 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3090 ret
= LTTNG_ERR_KERN_NA
;
3095 /* Deny register consumer if we already have a spawned consumer. */
3096 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3097 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3098 if (kconsumer_data
.pid
> 0) {
3099 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3100 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3103 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3107 * Check for command that don't needs to allocate a returned payload. We do
3108 * this here so we don't have to make the call for no payload at each
3111 switch(cmd_ctx
->lsm
->cmd_type
) {
3112 case LTTNG_LIST_SESSIONS
:
3113 case LTTNG_LIST_TRACEPOINTS
:
3114 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3115 case LTTNG_LIST_DOMAINS
:
3116 case LTTNG_LIST_CHANNELS
:
3117 case LTTNG_LIST_EVENTS
:
3118 case LTTNG_LIST_SYSCALLS
:
3119 case LTTNG_LIST_TRACKER_PIDS
:
3120 case LTTNG_DATA_PENDING
:
3121 case LTTNG_ROTATE_SESSION
:
3122 case LTTNG_ROTATION_GET_INFO
:
3123 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
3126 /* Setup lttng message with no payload */
3127 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3129 /* This label does not try to unlock the session */
3130 goto init_setup_error
;
3134 /* Commands that DO NOT need a session. */
3135 switch (cmd_ctx
->lsm
->cmd_type
) {
3136 case LTTNG_CREATE_SESSION
:
3137 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3138 case LTTNG_CREATE_SESSION_LIVE
:
3139 case LTTNG_LIST_SESSIONS
:
3140 case LTTNG_LIST_TRACEPOINTS
:
3141 case LTTNG_LIST_SYSCALLS
:
3142 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3143 case LTTNG_SAVE_SESSION
:
3144 case LTTNG_REGISTER_TRIGGER
:
3145 case LTTNG_UNREGISTER_TRIGGER
:
3146 need_tracing_session
= 0;
3149 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3151 * We keep the session list lock across _all_ commands
3152 * for now, because the per-session lock does not
3153 * handle teardown properly.
3155 session_lock_list();
3156 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3157 if (cmd_ctx
->session
== NULL
) {
3158 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3161 /* Acquire lock for the session */
3162 session_lock(cmd_ctx
->session
);
3168 * Commands that need a valid session but should NOT create one if none
3169 * exists. Instead of creating one and destroying it when the command is
3170 * handled, process that right before so we save some round trip in useless
3173 switch (cmd_ctx
->lsm
->cmd_type
) {
3174 case LTTNG_DISABLE_CHANNEL
:
3175 case LTTNG_DISABLE_EVENT
:
3176 switch (cmd_ctx
->lsm
->domain
.type
) {
3177 case LTTNG_DOMAIN_KERNEL
:
3178 if (!cmd_ctx
->session
->kernel_session
) {
3179 ret
= LTTNG_ERR_NO_CHANNEL
;
3183 case LTTNG_DOMAIN_JUL
:
3184 case LTTNG_DOMAIN_LOG4J
:
3185 case LTTNG_DOMAIN_PYTHON
:
3186 case LTTNG_DOMAIN_UST
:
3187 if (!cmd_ctx
->session
->ust_session
) {
3188 ret
= LTTNG_ERR_NO_CHANNEL
;
3193 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3205 * Check domain type for specific "pre-action".
3207 switch (cmd_ctx
->lsm
->domain
.type
) {
3208 case LTTNG_DOMAIN_KERNEL
:
3210 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3214 /* Kernel tracer check */
3215 if (kernel_tracer_fd
== -1) {
3216 /* Basically, load kernel tracer modules */
3217 ret
= init_kernel_tracer();
3223 /* Consumer is in an ERROR state. Report back to client */
3224 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3225 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3229 /* Need a session for kernel command */
3230 if (need_tracing_session
) {
3231 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3232 ret
= create_kernel_session(cmd_ctx
->session
);
3234 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3239 /* Start the kernel consumer daemon */
3240 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3241 if (kconsumer_data
.pid
== 0 &&
3242 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3243 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3244 ret
= start_consumerd(&kconsumer_data
);
3246 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3249 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3251 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3255 * The consumer was just spawned so we need to add the socket to
3256 * the consumer output of the session if exist.
3258 ret
= consumer_create_socket(&kconsumer_data
,
3259 cmd_ctx
->session
->kernel_session
->consumer
);
3266 case LTTNG_DOMAIN_JUL
:
3267 case LTTNG_DOMAIN_LOG4J
:
3268 case LTTNG_DOMAIN_PYTHON
:
3269 case LTTNG_DOMAIN_UST
:
3271 if (!ust_app_supported()) {
3272 ret
= LTTNG_ERR_NO_UST
;
3275 /* Consumer is in an ERROR state. Report back to client */
3276 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3277 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3281 if (need_tracing_session
) {
3282 /* Create UST session if none exist. */
3283 if (cmd_ctx
->session
->ust_session
== NULL
) {
3284 ret
= create_ust_session(cmd_ctx
->session
,
3285 &cmd_ctx
->lsm
->domain
);
3286 if (ret
!= LTTNG_OK
) {
3291 /* Start the UST consumer daemons */
3293 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3294 if (config
.consumerd64_bin_path
.value
&&
3295 ustconsumer64_data
.pid
== 0 &&
3296 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3297 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3298 ret
= start_consumerd(&ustconsumer64_data
);
3300 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3301 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3305 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3306 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3308 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3312 * Setup socket for consumer 64 bit. No need for atomic access
3313 * since it was set above and can ONLY be set in this thread.
3315 ret
= consumer_create_socket(&ustconsumer64_data
,
3316 cmd_ctx
->session
->ust_session
->consumer
);
3322 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3323 if (config
.consumerd32_bin_path
.value
&&
3324 ustconsumer32_data
.pid
== 0 &&
3325 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3326 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3327 ret
= start_consumerd(&ustconsumer32_data
);
3329 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3330 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3334 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3335 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3337 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3341 * Setup socket for consumer 32 bit. No need for atomic access
3342 * since it was set above and can ONLY be set in this thread.
3344 ret
= consumer_create_socket(&ustconsumer32_data
,
3345 cmd_ctx
->session
->ust_session
->consumer
);
3357 /* Validate consumer daemon state when start/stop trace command */
3358 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3359 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3360 switch (cmd_ctx
->lsm
->domain
.type
) {
3361 case LTTNG_DOMAIN_NONE
:
3363 case LTTNG_DOMAIN_JUL
:
3364 case LTTNG_DOMAIN_LOG4J
:
3365 case LTTNG_DOMAIN_PYTHON
:
3366 case LTTNG_DOMAIN_UST
:
3367 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3368 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3372 case LTTNG_DOMAIN_KERNEL
:
3373 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3374 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3379 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3385 * Check that the UID or GID match that of the tracing session.
3386 * The root user can interact with all sessions.
3388 if (need_tracing_session
) {
3389 if (!session_access_ok(cmd_ctx
->session
,
3390 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3391 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3392 ret
= LTTNG_ERR_EPERM
;
3398 * Send relayd information to consumer as soon as we have a domain and a
3401 if (cmd_ctx
->session
&& need_domain
) {
3403 * Setup relayd if not done yet. If the relayd information was already
3404 * sent to the consumer, this call will gracefully return.
3406 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3407 if (ret
!= LTTNG_OK
) {
3412 /* Process by command type */
3413 switch (cmd_ctx
->lsm
->cmd_type
) {
3414 case LTTNG_ADD_CONTEXT
:
3417 * An LTTNG_ADD_CONTEXT command might have a supplementary
3418 * payload if the context being added is an application context.
3420 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3421 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3422 char *provider_name
= NULL
, *context_name
= NULL
;
3423 size_t provider_name_len
=
3424 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3425 size_t context_name_len
=
3426 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3428 if (provider_name_len
== 0 || context_name_len
== 0) {
3430 * Application provider and context names MUST
3433 ret
= -LTTNG_ERR_INVALID
;
3437 provider_name
= zmalloc(provider_name_len
+ 1);
3438 if (!provider_name
) {
3439 ret
= -LTTNG_ERR_NOMEM
;
3442 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3445 context_name
= zmalloc(context_name_len
+ 1);
3446 if (!context_name
) {
3447 ret
= -LTTNG_ERR_NOMEM
;
3448 goto error_add_context
;
3450 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3453 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3456 goto error_add_context
;
3459 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3462 goto error_add_context
;
3467 * cmd_add_context assumes ownership of the provider and context
3470 ret
= cmd_add_context(cmd_ctx
->session
,
3471 cmd_ctx
->lsm
->domain
.type
,
3472 cmd_ctx
->lsm
->u
.context
.channel_name
,
3473 &cmd_ctx
->lsm
->u
.context
.ctx
,
3474 kernel_poll_pipe
[1]);
3476 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3477 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3479 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3480 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3486 case LTTNG_DISABLE_CHANNEL
:
3488 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3489 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3492 case LTTNG_DISABLE_EVENT
:
3496 * FIXME: handle filter; for now we just receive the filter's
3497 * bytecode along with the filter expression which are sent by
3498 * liblttng-ctl and discard them.
3500 * This fixes an issue where the client may block while sending
3501 * the filter payload and encounter an error because the session
3502 * daemon closes the socket without ever handling this data.
3504 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3505 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3508 char data
[LTTNG_FILTER_MAX_LEN
];
3510 DBG("Discarding disable event command payload of size %zu", count
);
3512 ret
= lttcomm_recv_unix_sock(sock
, data
,
3513 count
> sizeof(data
) ? sizeof(data
) : count
);
3518 count
-= (size_t) ret
;
3521 /* FIXME: passing packed structure to non-packed pointer */
3522 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3523 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3524 &cmd_ctx
->lsm
->u
.disable
.event
);
3527 case LTTNG_ENABLE_CHANNEL
:
3529 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3530 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3531 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3532 &cmd_ctx
->lsm
->u
.channel
.chan
,
3533 kernel_poll_pipe
[1]);
3536 case LTTNG_TRACK_PID
:
3538 ret
= cmd_track_pid(cmd_ctx
->session
,
3539 cmd_ctx
->lsm
->domain
.type
,
3540 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3543 case LTTNG_UNTRACK_PID
:
3545 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3546 cmd_ctx
->lsm
->domain
.type
,
3547 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3550 case LTTNG_ENABLE_EVENT
:
3552 struct lttng_event
*ev
= NULL
;
3553 struct lttng_event_exclusion
*exclusion
= NULL
;
3554 struct lttng_filter_bytecode
*bytecode
= NULL
;
3555 char *filter_expression
= NULL
;
3557 /* Handle exclusion events and receive it from the client. */
3558 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3559 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3561 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3562 (count
* LTTNG_SYMBOL_NAME_LEN
));
3564 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3568 DBG("Receiving var len exclusion event list from client ...");
3569 exclusion
->count
= count
;
3570 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3571 count
* LTTNG_SYMBOL_NAME_LEN
);
3573 DBG("Nothing recv() from client var len data... continuing");
3576 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3581 /* Get filter expression from client. */
3582 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3583 size_t expression_len
=
3584 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3586 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3587 ret
= LTTNG_ERR_FILTER_INVAL
;
3592 filter_expression
= zmalloc(expression_len
);
3593 if (!filter_expression
) {
3595 ret
= LTTNG_ERR_FILTER_NOMEM
;
3599 /* Receive var. len. data */
3600 DBG("Receiving var len filter's expression from client ...");
3601 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3604 DBG("Nothing recv() from client var len data... continuing");
3606 free(filter_expression
);
3608 ret
= LTTNG_ERR_FILTER_INVAL
;
3613 /* Handle filter and get bytecode from client. */
3614 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3615 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3617 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3618 ret
= LTTNG_ERR_FILTER_INVAL
;
3619 free(filter_expression
);
3624 bytecode
= zmalloc(bytecode_len
);
3626 free(filter_expression
);
3628 ret
= LTTNG_ERR_FILTER_NOMEM
;
3632 /* Receive var. len. data */
3633 DBG("Receiving var len filter's bytecode from client ...");
3634 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3636 DBG("Nothing recv() from client var len data... continuing");
3638 free(filter_expression
);
3641 ret
= LTTNG_ERR_FILTER_INVAL
;
3645 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3646 free(filter_expression
);
3649 ret
= LTTNG_ERR_FILTER_INVAL
;
3654 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
3656 DBG("Failed to copy event: %s",
3657 cmd_ctx
->lsm
->u
.enable
.event
.name
);
3658 free(filter_expression
);
3661 ret
= LTTNG_ERR_NOMEM
;
3666 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
3667 /* Expect a userspace probe description. */
3668 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
3670 free(filter_expression
);
3673 lttng_event_destroy(ev
);
3678 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3679 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3681 filter_expression
, bytecode
, exclusion
,
3682 kernel_poll_pipe
[1]);
3683 lttng_event_destroy(ev
);
3686 case LTTNG_LIST_TRACEPOINTS
:
3688 struct lttng_event
*events
;
3691 session_lock_list();
3692 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3693 session_unlock_list();
3694 if (nb_events
< 0) {
3695 /* Return value is a negative lttng_error_code. */
3701 * Setup lttng message with payload size set to the event list size in
3702 * bytes and then copy list into the llm payload.
3704 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3705 sizeof(struct lttng_event
) * nb_events
);
3715 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3717 struct lttng_event_field
*fields
;
3720 session_lock_list();
3721 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3723 session_unlock_list();
3724 if (nb_fields
< 0) {
3725 /* Return value is a negative lttng_error_code. */
3731 * Setup lttng message with payload size set to the event list size in
3732 * bytes and then copy list into the llm payload.
3734 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3735 sizeof(struct lttng_event_field
) * nb_fields
);
3745 case LTTNG_LIST_SYSCALLS
:
3747 struct lttng_event
*events
;
3750 nb_events
= cmd_list_syscalls(&events
);
3751 if (nb_events
< 0) {
3752 /* Return value is a negative lttng_error_code. */
3758 * Setup lttng message with payload size set to the event list size in
3759 * bytes and then copy list into the llm payload.
3761 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3762 sizeof(struct lttng_event
) * nb_events
);
3772 case LTTNG_LIST_TRACKER_PIDS
:
3774 int32_t *pids
= NULL
;
3777 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3778 cmd_ctx
->lsm
->domain
.type
, &pids
);
3780 /* Return value is a negative lttng_error_code. */
3786 * Setup lttng message with payload size set to the event list size in
3787 * bytes and then copy list into the llm payload.
3789 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3790 sizeof(int32_t) * nr_pids
);
3800 case LTTNG_SET_CONSUMER_URI
:
3803 struct lttng_uri
*uris
;
3805 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3806 len
= nb_uri
* sizeof(struct lttng_uri
);
3809 ret
= LTTNG_ERR_INVALID
;
3813 uris
= zmalloc(len
);
3815 ret
= LTTNG_ERR_FATAL
;
3819 /* Receive variable len data */
3820 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3821 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3823 DBG("No URIs received from client... continuing");
3825 ret
= LTTNG_ERR_SESSION_FAIL
;
3830 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3832 if (ret
!= LTTNG_OK
) {
3839 case LTTNG_START_TRACE
:
3842 * On the first start, if we have a kernel session and we have
3843 * enabled time or size-based rotations, we have to make sure
3844 * the kernel tracer supports it.
3846 if (!cmd_ctx
->session
->has_been_started
&& \
3847 cmd_ctx
->session
->kernel_session
&& \
3848 (cmd_ctx
->session
->rotate_timer_period
|| \
3849 cmd_ctx
->session
->rotate_size
) && \
3850 !check_rotate_compatible()) {
3851 DBG("Kernel tracer version is not compatible with the rotation feature");
3852 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3855 ret
= cmd_start_trace(cmd_ctx
->session
);
3858 case LTTNG_STOP_TRACE
:
3860 ret
= cmd_stop_trace(cmd_ctx
->session
);
3863 case LTTNG_CREATE_SESSION
:
3866 struct lttng_uri
*uris
= NULL
;
3868 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3869 len
= nb_uri
* sizeof(struct lttng_uri
);
3872 uris
= zmalloc(len
);
3874 ret
= LTTNG_ERR_FATAL
;
3878 /* Receive variable len data */
3879 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3880 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3882 DBG("No URIs received from client... continuing");
3884 ret
= LTTNG_ERR_SESSION_FAIL
;
3889 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3890 DBG("Creating session with ONE network URI is a bad call");
3891 ret
= LTTNG_ERR_SESSION_FAIL
;
3897 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3898 &cmd_ctx
->creds
, 0);
3904 case LTTNG_DESTROY_SESSION
:
3906 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1],
3907 notification_thread_handle
);
3909 /* Set session to NULL so we do not unlock it after free. */
3910 cmd_ctx
->session
= NULL
;
3913 case LTTNG_LIST_DOMAINS
:
3916 struct lttng_domain
*domains
= NULL
;
3918 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3920 /* Return value is a negative lttng_error_code. */
3925 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3926 nb_dom
* sizeof(struct lttng_domain
));
3936 case LTTNG_LIST_CHANNELS
:
3938 ssize_t payload_size
;
3939 struct lttng_channel
*channels
= NULL
;
3941 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3942 cmd_ctx
->session
, &channels
);
3943 if (payload_size
< 0) {
3944 /* Return value is a negative lttng_error_code. */
3945 ret
= -payload_size
;
3949 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3960 case LTTNG_LIST_EVENTS
:
3963 struct lttng_event
*events
= NULL
;
3964 struct lttcomm_event_command_header cmd_header
;
3967 memset(&cmd_header
, 0, sizeof(cmd_header
));
3968 /* Extended infos are included at the end of events */
3969 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3970 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3971 &events
, &total_size
);
3974 /* Return value is a negative lttng_error_code. */
3979 cmd_header
.nb_events
= nb_event
;
3980 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3981 &cmd_header
, sizeof(cmd_header
));
3991 case LTTNG_LIST_SESSIONS
:
3993 unsigned int nr_sessions
;
3994 void *sessions_payload
;
3997 session_lock_list();
3998 nr_sessions
= lttng_sessions_count(
3999 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4000 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4001 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
4002 sessions_payload
= zmalloc(payload_len
);
4004 if (!sessions_payload
) {
4005 session_unlock_list();
4010 cmd_list_lttng_sessions(sessions_payload
,
4011 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4012 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4013 session_unlock_list();
4015 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
4017 free(sessions_payload
);
4026 case LTTNG_REGISTER_CONSUMER
:
4028 struct consumer_data
*cdata
;
4030 switch (cmd_ctx
->lsm
->domain
.type
) {
4031 case LTTNG_DOMAIN_KERNEL
:
4032 cdata
= &kconsumer_data
;
4035 ret
= LTTNG_ERR_UND
;
4039 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4040 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
4043 case LTTNG_DATA_PENDING
:
4046 uint8_t pending_ret_byte
;
4048 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
4053 * This function may returns 0 or 1 to indicate whether or not
4054 * there is data pending. In case of error, it should return an
4055 * LTTNG_ERR code. However, some code paths may still return
4056 * a nondescript error code, which we handle by returning an
4059 if (pending_ret
== 0 || pending_ret
== 1) {
4061 * ret will be set to LTTNG_OK at the end of
4064 } else if (pending_ret
< 0) {
4065 ret
= LTTNG_ERR_UNK
;
4072 pending_ret_byte
= (uint8_t) pending_ret
;
4074 /* 1 byte to return whether or not data is pending */
4075 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
4076 &pending_ret_byte
, 1);
4085 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
4087 struct lttcomm_lttng_output_id reply
;
4089 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
4090 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
4091 if (ret
!= LTTNG_OK
) {
4095 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
4101 /* Copy output list into message payload */
4105 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
4107 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
4108 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
4111 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
4114 struct lttng_snapshot_output
*outputs
= NULL
;
4116 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
4117 if (nb_output
< 0) {
4122 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4123 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4124 nb_output
* sizeof(struct lttng_snapshot_output
));
4134 case LTTNG_SNAPSHOT_RECORD
:
4136 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4137 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4138 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4141 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4144 struct lttng_uri
*uris
= NULL
;
4146 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4147 len
= nb_uri
* sizeof(struct lttng_uri
);
4150 uris
= zmalloc(len
);
4152 ret
= LTTNG_ERR_FATAL
;
4156 /* Receive variable len data */
4157 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4158 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4160 DBG("No URIs received from client... continuing");
4162 ret
= LTTNG_ERR_SESSION_FAIL
;
4167 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4168 DBG("Creating session with ONE network URI is a bad call");
4169 ret
= LTTNG_ERR_SESSION_FAIL
;
4175 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4176 nb_uri
, &cmd_ctx
->creds
);
4180 case LTTNG_CREATE_SESSION_LIVE
:
4183 struct lttng_uri
*uris
= NULL
;
4185 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4186 len
= nb_uri
* sizeof(struct lttng_uri
);
4189 uris
= zmalloc(len
);
4191 ret
= LTTNG_ERR_FATAL
;
4195 /* Receive variable len data */
4196 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4197 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4199 DBG("No URIs received from client... continuing");
4201 ret
= LTTNG_ERR_SESSION_FAIL
;
4206 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4207 DBG("Creating session with ONE network URI is a bad call");
4208 ret
= LTTNG_ERR_SESSION_FAIL
;
4214 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4215 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4219 case LTTNG_SAVE_SESSION
:
4221 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4225 case LTTNG_SET_SESSION_SHM_PATH
:
4227 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4228 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4231 case LTTNG_REGENERATE_METADATA
:
4233 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4236 case LTTNG_REGENERATE_STATEDUMP
:
4238 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4241 case LTTNG_REGISTER_TRIGGER
:
4243 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4244 notification_thread_handle
);
4247 case LTTNG_UNREGISTER_TRIGGER
:
4249 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4250 notification_thread_handle
);
4253 case LTTNG_ROTATE_SESSION
:
4255 struct lttng_rotate_session_return rotate_return
;
4257 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4259 memset(&rotate_return
, 0, sizeof(rotate_return
));
4260 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4261 DBG("Kernel tracer version is not compatible with the rotation feature");
4262 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4266 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4272 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4273 sizeof(rotate_return
));
4282 case LTTNG_ROTATION_GET_INFO
:
4284 struct lttng_rotation_get_info_return get_info_return
;
4286 memset(&get_info_return
, 0, sizeof(get_info_return
));
4287 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4288 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4294 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4295 sizeof(get_info_return
));
4304 case LTTNG_ROTATION_SET_SCHEDULE
:
4307 enum lttng_rotation_schedule_type schedule_type
;
4310 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4311 DBG("Kernel tracer version does not support session rotations");
4312 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4316 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
4317 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
4318 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
4320 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4324 notification_thread_handle
);
4325 if (ret
!= LTTNG_OK
) {
4331 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
4333 struct lttng_session_list_schedules_return schedules
= {
4334 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
4335 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
4336 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
4337 .size
.value
= cmd_ctx
->session
->rotate_size
,
4340 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
4351 ret
= LTTNG_ERR_UND
;
4356 if (cmd_ctx
->llm
== NULL
) {
4357 DBG("Missing llm structure. Allocating one.");
4358 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4362 /* Set return code */
4363 cmd_ctx
->llm
->ret_code
= ret
;
4365 if (cmd_ctx
->session
) {
4366 session_unlock(cmd_ctx
->session
);
4368 if (need_tracing_session
) {
4369 session_unlock_list();
4372 assert(!rcu_read_ongoing());
4377 * Thread managing health check socket.
4379 static void *thread_manage_health(void *data
)
4381 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4382 uint32_t revents
, nb_fd
;
4383 struct lttng_poll_event events
;
4384 struct health_comm_msg msg
;
4385 struct health_comm_reply reply
;
4387 DBG("[thread] Manage health check started");
4389 rcu_register_thread();
4391 /* We might hit an error path before this is created. */
4392 lttng_poll_init(&events
);
4394 /* Create unix socket */
4395 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4397 ERR("Unable to create health check Unix socket");
4402 /* lttng health client socket path permissions */
4403 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4404 utils_get_group_id(config
.tracing_group_name
.value
));
4406 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4411 ret
= chmod(config
.health_unix_sock_path
.value
,
4412 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4414 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4421 * Set the CLOEXEC flag. Return code is useless because either way, the
4424 (void) utils_set_fd_cloexec(sock
);
4426 ret
= lttcomm_listen_unix_sock(sock
);
4432 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4433 * more will be added to this poll set.
4435 ret
= sessiond_set_thread_pollset(&events
, 2);
4440 /* Add the application registration socket */
4441 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4446 sessiond_notify_ready();
4449 DBG("Health check ready");
4451 /* Inifinite blocking call, waiting for transmission */
4453 ret
= lttng_poll_wait(&events
, -1);
4456 * Restart interrupted system call.
4458 if (errno
== EINTR
) {
4466 for (i
= 0; i
< nb_fd
; i
++) {
4467 /* Fetch once the poll data */
4468 revents
= LTTNG_POLL_GETEV(&events
, i
);
4469 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4472 /* No activity for this FD (poll implementation). */
4476 /* Thread quit pipe has been closed. Killing thread. */
4477 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4483 /* Event on the registration socket */
4484 if (pollfd
== sock
) {
4485 if (revents
& LPOLLIN
) {
4487 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4488 ERR("Health socket poll error");
4491 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4497 new_sock
= lttcomm_accept_unix_sock(sock
);
4503 * Set the CLOEXEC flag. Return code is useless because either way, the
4506 (void) utils_set_fd_cloexec(new_sock
);
4508 DBG("Receiving data from client for health...");
4509 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4511 DBG("Nothing recv() from client... continuing");
4512 ret
= close(new_sock
);
4519 rcu_thread_online();
4521 memset(&reply
, 0, sizeof(reply
));
4522 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4524 * health_check_state returns 0 if health is
4527 if (!health_check_state(health_sessiond
, i
)) {
4528 reply
.ret_code
|= 1ULL << i
;
4532 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4534 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4536 ERR("Failed to send health data back to client");
4539 /* End of transmission */
4540 ret
= close(new_sock
);
4549 ERR("Health error occurred in %s", __func__
);
4551 DBG("Health check thread dying");
4552 unlink(config
.health_unix_sock_path
.value
);
4560 lttng_poll_clean(&events
);
4562 rcu_unregister_thread();
4567 * This thread manage all clients request using the unix client socket for
4570 static void *thread_manage_clients(void *data
)
4572 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4574 uint32_t revents
, nb_fd
;
4575 struct command_ctx
*cmd_ctx
= NULL
;
4576 struct lttng_poll_event events
;
4578 DBG("[thread] Manage client started");
4580 rcu_register_thread();
4582 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4584 health_code_update();
4586 ret
= lttcomm_listen_unix_sock(client_sock
);
4592 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4593 * more will be added to this poll set.
4595 ret
= sessiond_set_thread_pollset(&events
, 2);
4597 goto error_create_poll
;
4600 /* Add the application registration socket */
4601 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4606 ret
= sem_post(&load_info
->message_thread_ready
);
4608 PERROR("sem_post message_thread_ready");
4613 * Wait until all support threads are initialized before accepting
4616 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4618 struct timeval timeout
;
4621 FD_SET(thread_quit_pipe
[0], &read_fds
);
4622 memset(&timeout
, 0, sizeof(timeout
));
4623 timeout
.tv_usec
= 1000;
4626 * If a support thread failed to launch, it may signal that
4627 * we must exit and the sessiond would never be marked as
4630 * The timeout is set to 1ms, which serves as a way to
4631 * pace down this check.
4633 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
4635 if (ret
> 0 || (ret
< 0 && errno
!= EINTR
)) {
4640 * This barrier is paired with the one in sessiond_notify_ready() to
4641 * ensure that loads accessing data initialized by the other threads,
4642 * on which this thread was waiting, are not performed before this point.
4644 * Note that this could be a 'read' memory barrier, but a full barrier
4645 * is used in case the code changes. The performance implications of
4646 * this choice are minimal since this is a slow path.
4650 /* This testpoint is after we signal readiness to the parent. */
4651 if (testpoint(sessiond_thread_manage_clients
)) {
4655 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4659 health_code_update();
4662 const struct cmd_completion_handler
*cmd_completion_handler
;
4664 DBG("Accepting client command ...");
4666 /* Inifinite blocking call, waiting for transmission */
4668 health_poll_entry();
4669 ret
= lttng_poll_wait(&events
, -1);
4673 * Restart interrupted system call.
4675 if (errno
== EINTR
) {
4683 for (i
= 0; i
< nb_fd
; i
++) {
4684 /* Fetch once the poll data */
4685 revents
= LTTNG_POLL_GETEV(&events
, i
);
4686 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4688 health_code_update();
4691 /* No activity for this FD (poll implementation). */
4695 /* Thread quit pipe has been closed. Killing thread. */
4696 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4702 /* Event on the registration socket */
4703 if (pollfd
== client_sock
) {
4704 if (revents
& LPOLLIN
) {
4706 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4707 ERR("Client socket poll error");
4710 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4716 DBG("Wait for client response");
4718 health_code_update();
4720 sock
= lttcomm_accept_unix_sock(client_sock
);
4726 * Set the CLOEXEC flag. Return code is useless because either way, the
4729 (void) utils_set_fd_cloexec(sock
);
4731 /* Set socket option for credentials retrieval */
4732 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4737 /* Allocate context command to process the client request */
4738 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4739 if (cmd_ctx
== NULL
) {
4740 PERROR("zmalloc cmd_ctx");
4744 /* Allocate data buffer for reception */
4745 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4746 if (cmd_ctx
->lsm
== NULL
) {
4747 PERROR("zmalloc cmd_ctx->lsm");
4751 cmd_ctx
->llm
= NULL
;
4752 cmd_ctx
->session
= NULL
;
4754 health_code_update();
4757 * Data is received from the lttng client. The struct
4758 * lttcomm_session_msg (lsm) contains the command and data request of
4761 DBG("Receiving data from client ...");
4762 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4763 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4765 DBG("Nothing recv() from client... continuing");
4771 clean_command_ctx(&cmd_ctx
);
4775 health_code_update();
4777 // TODO: Validate cmd_ctx including sanity check for
4778 // security purpose.
4780 rcu_thread_online();
4782 * This function dispatch the work to the kernel or userspace tracer
4783 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4784 * informations for the client. The command context struct contains
4785 * everything this function may needs.
4787 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4788 rcu_thread_offline();
4796 * TODO: Inform client somehow of the fatal error. At
4797 * this point, ret < 0 means that a zmalloc failed
4798 * (ENOMEM). Error detected but still accept
4799 * command, unless a socket error has been
4802 clean_command_ctx(&cmd_ctx
);
4806 cmd_completion_handler
= cmd_pop_completion_handler();
4807 if (cmd_completion_handler
) {
4808 enum lttng_error_code completion_code
;
4810 completion_code
= cmd_completion_handler
->run(
4811 cmd_completion_handler
->data
);
4812 if (completion_code
!= LTTNG_OK
) {
4813 clean_command_ctx(&cmd_ctx
);
4818 health_code_update();
4820 DBG("Sending response (size: %d, retcode: %s (%d))",
4821 cmd_ctx
->lttng_msg_size
,
4822 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4823 cmd_ctx
->llm
->ret_code
);
4824 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4826 ERR("Failed to send data back to client");
4829 /* End of transmission */
4836 clean_command_ctx(&cmd_ctx
);
4838 health_code_update();
4850 lttng_poll_clean(&events
);
4851 clean_command_ctx(&cmd_ctx
);
4855 unlink(config
.client_unix_sock_path
.value
);
4856 if (client_sock
>= 0) {
4857 ret
= close(client_sock
);
4865 ERR("Health error occurred in %s", __func__
);
4868 health_unregister(health_sessiond
);
4870 DBG("Client thread dying");
4872 rcu_unregister_thread();
4875 * Since we are creating the consumer threads, we own them, so we need
4876 * to join them before our thread exits.
4878 ret
= join_consumer_thread(&kconsumer_data
);
4881 PERROR("join_consumer");
4884 ret
= join_consumer_thread(&ustconsumer32_data
);
4887 PERROR("join_consumer ust32");
4890 ret
= join_consumer_thread(&ustconsumer64_data
);
4893 PERROR("join_consumer ust64");
4898 static int string_match(const char *str1
, const char *str2
)
4900 return (str1
&& str2
) && !strcmp(str1
, str2
);
4904 * Take an option from the getopt output and set it in the right variable to be
4907 * Return 0 on success else a negative value.
4909 static int set_option(int opt
, const char *arg
, const char *optname
)
4913 if (string_match(optname
, "client-sock") || opt
== 'c') {
4914 if (!arg
|| *arg
== '\0') {
4918 if (lttng_is_setuid_setgid()) {
4919 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4920 "-c, --client-sock");
4922 config_string_set(&config
.client_unix_sock_path
,
4924 if (!config
.client_unix_sock_path
.value
) {
4929 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4930 if (!arg
|| *arg
== '\0') {
4934 if (lttng_is_setuid_setgid()) {
4935 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4938 config_string_set(&config
.apps_unix_sock_path
,
4940 if (!config
.apps_unix_sock_path
.value
) {
4945 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4946 config
.daemonize
= true;
4947 } else if (string_match(optname
, "background") || opt
== 'b') {
4948 config
.background
= true;
4949 } else if (string_match(optname
, "group") || opt
== 'g') {
4950 if (!arg
|| *arg
== '\0') {
4954 if (lttng_is_setuid_setgid()) {
4955 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4958 config_string_set(&config
.tracing_group_name
,
4960 if (!config
.tracing_group_name
.value
) {
4965 } else if (string_match(optname
, "help") || opt
== 'h') {
4966 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4968 ERR("Cannot show --help for `lttng-sessiond`");
4971 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4972 } else if (string_match(optname
, "version") || opt
== 'V') {
4973 fprintf(stdout
, "%s\n", VERSION
);
4975 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4976 config
.sig_parent
= true;
4977 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4978 if (!arg
|| *arg
== '\0') {
4982 if (lttng_is_setuid_setgid()) {
4983 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4984 "--kconsumerd-err-sock");
4986 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4988 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4993 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4994 if (!arg
|| *arg
== '\0') {
4998 if (lttng_is_setuid_setgid()) {
4999 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5000 "--kconsumerd-cmd-sock");
5002 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
5004 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
5009 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
5010 if (!arg
|| *arg
== '\0') {
5014 if (lttng_is_setuid_setgid()) {
5015 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5016 "--ustconsumerd64-err-sock");
5018 config_string_set(&config
.consumerd64_err_unix_sock_path
,
5020 if (!config
.consumerd64_err_unix_sock_path
.value
) {
5025 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
5026 if (!arg
|| *arg
== '\0') {
5030 if (lttng_is_setuid_setgid()) {
5031 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5032 "--ustconsumerd64-cmd-sock");
5034 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
5036 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
5041 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
5042 if (!arg
|| *arg
== '\0') {
5046 if (lttng_is_setuid_setgid()) {
5047 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5048 "--ustconsumerd32-err-sock");
5050 config_string_set(&config
.consumerd32_err_unix_sock_path
,
5052 if (!config
.consumerd32_err_unix_sock_path
.value
) {
5057 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
5058 if (!arg
|| *arg
== '\0') {
5062 if (lttng_is_setuid_setgid()) {
5063 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5064 "--ustconsumerd32-cmd-sock");
5066 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
5068 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
5073 } else if (string_match(optname
, "no-kernel")) {
5074 config
.no_kernel
= true;
5075 } else if (string_match(optname
, "quiet") || opt
== 'q') {
5076 config
.quiet
= true;
5077 } else if (string_match(optname
, "verbose") || opt
== 'v') {
5078 /* Verbose level can increase using multiple -v */
5080 /* Value obtained from config file */
5081 config
.verbose
= config_parse_value(arg
);
5083 /* -v used on command line */
5086 /* Clamp value to [0, 3] */
5087 config
.verbose
= config
.verbose
< 0 ? 0 :
5088 (config
.verbose
<= 3 ? config
.verbose
: 3);
5089 } else if (string_match(optname
, "verbose-consumer")) {
5091 config
.verbose_consumer
= config_parse_value(arg
);
5093 config
.verbose_consumer
++;
5095 } else if (string_match(optname
, "consumerd32-path")) {
5096 if (!arg
|| *arg
== '\0') {
5100 if (lttng_is_setuid_setgid()) {
5101 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5102 "--consumerd32-path");
5104 config_string_set(&config
.consumerd32_bin_path
,
5106 if (!config
.consumerd32_bin_path
.value
) {
5111 } else if (string_match(optname
, "consumerd32-libdir")) {
5112 if (!arg
|| *arg
== '\0') {
5116 if (lttng_is_setuid_setgid()) {
5117 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5118 "--consumerd32-libdir");
5120 config_string_set(&config
.consumerd32_lib_dir
,
5122 if (!config
.consumerd32_lib_dir
.value
) {
5127 } else if (string_match(optname
, "consumerd64-path")) {
5128 if (!arg
|| *arg
== '\0') {
5132 if (lttng_is_setuid_setgid()) {
5133 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5134 "--consumerd64-path");
5136 config_string_set(&config
.consumerd64_bin_path
,
5138 if (!config
.consumerd64_bin_path
.value
) {
5143 } else if (string_match(optname
, "consumerd64-libdir")) {
5144 if (!arg
|| *arg
== '\0') {
5148 if (lttng_is_setuid_setgid()) {
5149 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5150 "--consumerd64-libdir");
5152 config_string_set(&config
.consumerd64_lib_dir
,
5154 if (!config
.consumerd64_lib_dir
.value
) {
5159 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
5160 if (!arg
|| *arg
== '\0') {
5164 if (lttng_is_setuid_setgid()) {
5165 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5168 config_string_set(&config
.pid_file_path
, strdup(arg
));
5169 if (!config
.pid_file_path
.value
) {
5174 } else if (string_match(optname
, "agent-tcp-port")) {
5175 if (!arg
|| *arg
== '\0') {
5179 if (lttng_is_setuid_setgid()) {
5180 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5181 "--agent-tcp-port");
5186 v
= strtoul(arg
, NULL
, 0);
5187 if (errno
!= 0 || !isdigit(arg
[0])) {
5188 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5191 if (v
== 0 || v
>= 65535) {
5192 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5195 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
5196 DBG3("Agent TCP port set to non default: %i", (int) v
);
5198 } else if (string_match(optname
, "load") || opt
== 'l') {
5199 if (!arg
|| *arg
== '\0') {
5203 if (lttng_is_setuid_setgid()) {
5204 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5207 config_string_set(&config
.load_session_path
, strdup(arg
));
5208 if (!config
.load_session_path
.value
) {
5213 } else if (string_match(optname
, "kmod-probes")) {
5214 if (!arg
|| *arg
== '\0') {
5218 if (lttng_is_setuid_setgid()) {
5219 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5222 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
5223 if (!config
.kmod_probes_list
.value
) {
5228 } else if (string_match(optname
, "extra-kmod-probes")) {
5229 if (!arg
|| *arg
== '\0') {
5233 if (lttng_is_setuid_setgid()) {
5234 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5235 "--extra-kmod-probes");
5237 config_string_set(&config
.kmod_extra_probes_list
,
5239 if (!config
.kmod_extra_probes_list
.value
) {
5244 } else if (string_match(optname
, "config") || opt
== 'f') {
5245 /* This is handled in set_options() thus silent skip. */
5248 /* Unknown option or other error.
5249 * Error is printed by getopt, just return */
5254 if (ret
== -EINVAL
) {
5255 const char *opt_name
= "unknown";
5258 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5260 if (opt
== long_options
[i
].val
) {
5261 opt_name
= long_options
[i
].name
;
5266 WARN("Invalid argument provided for option \"%s\", using default value.",
5274 * config_entry_handler_cb used to handle options read from a config file.
5275 * See config_entry_handler_cb comment in common/config/session-config.h for the
5276 * return value conventions.
5278 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5282 if (!entry
|| !entry
->name
|| !entry
->value
) {
5287 /* Check if the option is to be ignored */
5288 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5289 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5294 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5297 /* Ignore if not fully matched. */
5298 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5303 * If the option takes no argument on the command line, we have to
5304 * check if the value is "true". We support non-zero numeric values,
5307 if (!long_options
[i
].has_arg
) {
5308 ret
= config_parse_value(entry
->value
);
5311 WARN("Invalid configuration value \"%s\" for option %s",
5312 entry
->value
, entry
->name
);
5314 /* False, skip boolean config option. */
5319 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5323 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5330 * daemon configuration loading and argument parsing
5332 static int set_options(int argc
, char **argv
)
5334 int ret
= 0, c
= 0, option_index
= 0;
5335 int orig_optopt
= optopt
, orig_optind
= optind
;
5337 const char *config_path
= NULL
;
5339 optstring
= utils_generate_optstring(long_options
,
5340 sizeof(long_options
) / sizeof(struct option
));
5346 /* Check for the --config option */
5347 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5348 &option_index
)) != -1) {
5352 } else if (c
!= 'f') {
5353 /* if not equal to --config option. */
5357 if (lttng_is_setuid_setgid()) {
5358 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5361 config_path
= utils_expand_path(optarg
);
5363 ERR("Failed to resolve path: %s", optarg
);
5368 ret
= config_get_section_entries(config_path
, config_section_name
,
5369 config_entry_handler
, NULL
);
5372 ERR("Invalid configuration option at line %i", ret
);
5378 /* Reset getopt's global state */
5379 optopt
= orig_optopt
;
5380 optind
= orig_optind
;
5384 * getopt_long() will not set option_index if it encounters a
5387 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5394 * Pass NULL as the long option name if popt left the index
5397 ret
= set_option(c
, optarg
,
5398 option_index
< 0 ? NULL
:
5399 long_options
[option_index
].name
);
5411 * Creates the two needed socket by the daemon.
5412 * apps_sock - The communication socket for all UST apps.
5413 * client_sock - The communication of the cli tool (lttng).
5415 static int init_daemon_socket(void)
5420 old_umask
= umask(0);
5422 /* Create client tool unix socket */
5423 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5424 if (client_sock
< 0) {
5425 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5430 /* Set the cloexec flag */
5431 ret
= utils_set_fd_cloexec(client_sock
);
5433 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5434 "Continuing but note that the consumer daemon will have a "
5435 "reference to this socket on exec()", client_sock
);
5438 /* File permission MUST be 660 */
5439 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5441 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5446 /* Create the application unix socket */
5447 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5448 if (apps_sock
< 0) {
5449 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5454 /* Set the cloexec flag */
5455 ret
= utils_set_fd_cloexec(apps_sock
);
5457 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5458 "Continuing but note that the consumer daemon will have a "
5459 "reference to this socket on exec()", apps_sock
);
5462 /* File permission MUST be 666 */
5463 ret
= chmod(config
.apps_unix_sock_path
.value
,
5464 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5466 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5471 DBG3("Session daemon client socket %d and application socket %d created",
5472 client_sock
, apps_sock
);
5480 * Create lockfile using the rundir and return its fd.
5482 static int create_lockfile(void)
5484 return utils_create_lock_file(config
.lock_file_path
.value
);
5488 * Check if the global socket is available, and if a daemon is answering at the
5489 * other side. If yes, error is returned.
5491 * Also attempts to create and hold the lock file.
5493 static int check_existing_daemon(void)
5497 /* Is there anybody out there ? */
5498 if (lttng_session_daemon_alive()) {
5503 lockfile_fd
= create_lockfile();
5504 if (lockfile_fd
< 0) {
5512 static void sessiond_cleanup_lock_file(void)
5517 * Cleanup lock file by deleting it and finaly closing it which will
5518 * release the file system lock.
5520 if (lockfile_fd
>= 0) {
5521 ret
= remove(config
.lock_file_path
.value
);
5523 PERROR("remove lock file");
5525 ret
= close(lockfile_fd
);
5527 PERROR("close lock file");
5533 * Set the tracing group gid onto the client socket.
5535 * Race window between mkdir and chown is OK because we are going from more
5536 * permissive (root.root) to less permissive (root.tracing).
5538 static int set_permissions(char *rundir
)
5543 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5545 /* Set lttng run dir */
5546 ret
= chown(rundir
, 0, gid
);
5548 ERR("Unable to set group on %s", rundir
);
5553 * Ensure all applications and tracing group can search the run
5554 * dir. Allow everyone to read the directory, since it does not
5555 * buy us anything to hide its content.
5557 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5559 ERR("Unable to set permissions on %s", rundir
);
5563 /* lttng client socket path */
5564 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5566 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5570 /* kconsumer error socket path */
5571 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5573 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5577 /* 64-bit ustconsumer error socket path */
5578 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5580 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5584 /* 32-bit ustconsumer compat32 error socket path */
5585 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5587 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5591 DBG("All permissions are set");
5597 * Create the lttng run directory needed for all global sockets and pipe.
5599 static int create_lttng_rundir(void)
5603 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5605 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5607 if (errno
!= EEXIST
) {
5608 ERR("Unable to create %s", config
.rundir
.value
);
5620 * Setup sockets and directory needed by the consumerds' communication with the
5623 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5628 switch (consumer_data
->type
) {
5629 case LTTNG_CONSUMER_KERNEL
:
5630 path
= config
.kconsumerd_path
.value
;
5632 case LTTNG_CONSUMER64_UST
:
5633 path
= config
.consumerd64_path
.value
;
5635 case LTTNG_CONSUMER32_UST
:
5636 path
= config
.consumerd32_path
.value
;
5639 ERR("Consumer type unknown");
5645 DBG2("Creating consumer directory: %s", path
);
5647 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5648 if (ret
< 0 && errno
!= EEXIST
) {
5650 ERR("Failed to create %s", path
);
5654 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5656 ERR("Unable to set group on %s", path
);
5662 /* Create the consumerd error unix socket */
5663 consumer_data
->err_sock
=
5664 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5665 if (consumer_data
->err_sock
< 0) {
5666 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5672 * Set the CLOEXEC flag. Return code is useless because either way, the
5675 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5677 PERROR("utils_set_fd_cloexec");
5678 /* continue anyway */
5681 /* File permission MUST be 660 */
5682 ret
= chmod(consumer_data
->err_unix_sock_path
,
5683 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5685 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5695 * Signal handler for the daemon
5697 * Simply stop all worker threads, leaving main() return gracefully after
5698 * joining all threads and calling cleanup().
5700 static void sighandler(int sig
)
5704 DBG("SIGINT caught");
5708 DBG("SIGTERM caught");
5712 CMM_STORE_SHARED(recv_child_signal
, 1);
5720 * Setup signal handler for :
5721 * SIGINT, SIGTERM, SIGPIPE
5723 static int set_signal_handler(void)
5726 struct sigaction sa
;
5729 if ((ret
= sigemptyset(&sigset
)) < 0) {
5730 PERROR("sigemptyset");
5734 sa
.sa_mask
= sigset
;
5737 sa
.sa_handler
= sighandler
;
5738 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5739 PERROR("sigaction");
5743 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5744 PERROR("sigaction");
5748 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5749 PERROR("sigaction");
5753 sa
.sa_handler
= SIG_IGN
;
5754 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5755 PERROR("sigaction");
5759 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5765 * Set open files limit to unlimited. This daemon can open a large number of
5766 * file descriptors in order to consume multiple kernel traces.
5768 static void set_ulimit(void)
5773 /* The kernel does not allow an infinite limit for open files */
5774 lim
.rlim_cur
= 65535;
5775 lim
.rlim_max
= 65535;
5777 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5779 PERROR("failed to set open files limit");
5783 static int write_pidfile(void)
5785 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5788 static int set_clock_plugin_env(void)
5791 char *env_value
= NULL
;
5793 if (!config
.lttng_ust_clock_plugin
.value
) {
5797 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5798 config
.lttng_ust_clock_plugin
.value
);
5804 ret
= putenv(env_value
);
5807 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5811 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5812 config
.lttng_ust_clock_plugin
.value
);
5820 int main(int argc
, char **argv
)
5822 int ret
= 0, retval
= 0;
5824 const char *env_app_timeout
;
5825 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5826 *ust64_channel_monitor_pipe
= NULL
,
5827 *kernel_channel_monitor_pipe
= NULL
;
5828 bool notification_thread_launched
= false;
5829 bool rotation_thread_launched
= false;
5830 bool timer_thread_launched
= false;
5831 struct timer_thread_parameters timer_thread_ctx
;
5832 /* Queue of rotation jobs populated by the sessiond-timer. */
5833 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5834 sem_t notification_thread_ready
;
5836 init_kernel_workarounds();
5838 rcu_register_thread();
5840 if (set_signal_handler()) {
5842 goto exit_set_signal_handler
;
5845 if (timer_signal_init()) {
5847 goto exit_set_signal_handler
;
5850 page_size
= sysconf(_SC_PAGESIZE
);
5851 if (page_size
< 0) {
5852 PERROR("sysconf _SC_PAGESIZE");
5853 page_size
= LONG_MAX
;
5854 WARN("Fallback page size to %ld", page_size
);
5857 ret
= sessiond_config_init(&config
);
5860 goto exit_set_signal_handler
;
5864 * Init config from environment variables.
5865 * Command line option override env configuration per-doc. Do env first.
5867 sessiond_config_apply_env_config(&config
);
5870 * Parse arguments and load the daemon configuration file.
5872 * We have an exit_options exit path to free memory reserved by
5873 * set_options. This is needed because the rest of sessiond_cleanup()
5874 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5875 * depends on set_options.
5878 if (set_options(argc
, argv
)) {
5884 * Resolve all paths received as arguments, configuration option, or
5885 * through environment variable as absolute paths. This is necessary
5886 * since daemonizing causes the sessiond's current working directory
5889 ret
= sessiond_config_resolve_paths(&config
);
5895 lttng_opt_verbose
= config
.verbose
;
5896 lttng_opt_quiet
= config
.quiet
;
5897 kconsumer_data
.err_unix_sock_path
=
5898 config
.kconsumerd_err_unix_sock_path
.value
;
5899 kconsumer_data
.cmd_unix_sock_path
=
5900 config
.kconsumerd_cmd_unix_sock_path
.value
;
5901 ustconsumer32_data
.err_unix_sock_path
=
5902 config
.consumerd32_err_unix_sock_path
.value
;
5903 ustconsumer32_data
.cmd_unix_sock_path
=
5904 config
.consumerd32_cmd_unix_sock_path
.value
;
5905 ustconsumer64_data
.err_unix_sock_path
=
5906 config
.consumerd64_err_unix_sock_path
.value
;
5907 ustconsumer64_data
.cmd_unix_sock_path
=
5908 config
.consumerd64_cmd_unix_sock_path
.value
;
5909 set_clock_plugin_env();
5911 sessiond_config_log(&config
);
5913 if (create_lttng_rundir()) {
5918 /* Abort launch if a session daemon is already running. */
5919 if (check_existing_daemon()) {
5920 ERR("A session daemon is already running.");
5926 if (config
.daemonize
|| config
.background
) {
5929 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5930 !config
.background
);
5937 * We are in the child. Make sure all other file descriptors are
5938 * closed, in case we are called with more opened file
5939 * descriptors than the standard ones and the lock file.
5941 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5942 if (i
== lockfile_fd
) {
5949 if (run_as_create_worker(argv
[0]) < 0) {
5950 goto exit_create_run_as_worker_cleanup
;
5954 * Starting from here, we can create threads. This needs to be after
5955 * lttng_daemonize due to RCU.
5959 * Initialize the health check subsystem. This call should set the
5960 * appropriate time values.
5962 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5963 if (!health_sessiond
) {
5964 PERROR("health_app_create error");
5966 goto exit_health_sessiond_cleanup
;
5969 /* Create thread to clean up RCU hash tables */
5970 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5972 goto exit_ht_cleanup
;
5975 /* Create thread quit pipe */
5976 if (init_thread_quit_pipe()) {
5978 goto exit_init_data
;
5981 /* Check if daemon is UID = 0 */
5982 is_root
= !getuid();
5984 /* Create global run dir with root access */
5986 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5987 if (!kernel_channel_monitor_pipe
) {
5988 ERR("Failed to create kernel consumer channel monitor pipe");
5990 goto exit_init_data
;
5992 kconsumer_data
.channel_monitor_pipe
=
5993 lttng_pipe_release_writefd(
5994 kernel_channel_monitor_pipe
);
5995 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5997 goto exit_init_data
;
6001 /* Set consumer initial state */
6002 kernel_consumerd_state
= CONSUMER_STOPPED
;
6003 ust_consumerd_state
= CONSUMER_STOPPED
;
6005 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
6006 if (!ust32_channel_monitor_pipe
) {
6007 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
6009 goto exit_init_data
;
6011 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6012 ust32_channel_monitor_pipe
);
6013 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
6015 goto exit_init_data
;
6019 * The rotation_thread_timer_queue structure is shared between the
6020 * sessiond timer thread and the rotation thread. The main thread keeps
6021 * its ownership and destroys it when both threads have been joined.
6023 rotation_timer_queue
= rotation_thread_timer_queue_create();
6024 if (!rotation_timer_queue
) {
6026 goto exit_init_data
;
6028 timer_thread_ctx
.rotation_thread_job_queue
= rotation_timer_queue
;
6030 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
6031 if (!ust64_channel_monitor_pipe
) {
6032 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
6034 goto exit_init_data
;
6036 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6037 ust64_channel_monitor_pipe
);
6038 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
6040 goto exit_init_data
;
6044 * Init UST app hash table. Alloc hash table before this point since
6045 * cleanup() can get called after that point.
6047 if (ust_app_ht_alloc()) {
6048 ERR("Failed to allocate UST app hash table");
6050 goto exit_init_data
;
6054 * Initialize agent app hash table. We allocate the hash table here
6055 * since cleanup() can get called after this point.
6057 if (agent_app_ht_alloc()) {
6058 ERR("Failed to allocate Agent app hash table");
6060 goto exit_init_data
;
6064 * These actions must be executed as root. We do that *after* setting up
6065 * the sockets path because we MUST make the check for another daemon using
6066 * those paths *before* trying to set the kernel consumer sockets and init
6070 if (set_consumer_sockets(&kconsumer_data
)) {
6072 goto exit_init_data
;
6075 /* Setup kernel tracer */
6076 if (!config
.no_kernel
) {
6077 init_kernel_tracer();
6078 if (kernel_tracer_fd
>= 0) {
6079 ret
= syscall_init_table();
6081 ERR("Unable to populate syscall table. "
6082 "Syscall tracing won't work "
6083 "for this session daemon.");
6088 /* Set ulimit for open files */
6091 /* init lttng_fd tracking must be done after set_ulimit. */
6094 if (set_consumer_sockets(&ustconsumer64_data
)) {
6096 goto exit_init_data
;
6099 if (set_consumer_sockets(&ustconsumer32_data
)) {
6101 goto exit_init_data
;
6104 /* Setup the needed unix socket */
6105 if (init_daemon_socket()) {
6107 goto exit_init_data
;
6110 /* Set credentials to socket */
6111 if (is_root
&& set_permissions(config
.rundir
.value
)) {
6113 goto exit_init_data
;
6116 /* Get parent pid if -S, --sig-parent is specified. */
6117 if (config
.sig_parent
) {
6121 /* Setup the kernel pipe for waking up the kernel thread */
6122 if (is_root
&& !config
.no_kernel
) {
6123 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6125 goto exit_init_data
;
6129 /* Setup the thread apps communication pipe. */
6130 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6132 goto exit_init_data
;
6135 /* Setup the thread apps notify communication pipe. */
6136 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6138 goto exit_init_data
;
6141 /* Initialize global buffer per UID and PID registry. */
6142 buffer_reg_init_uid_registry();
6143 buffer_reg_init_pid_registry();
6145 /* Init UST command queue. */
6146 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6149 * Get session list pointer. This pointer MUST NOT be free'd. This list
6150 * is statically declared in session.c
6152 session_list_ptr
= session_get_list();
6156 /* Check for the application socket timeout env variable. */
6157 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6158 if (env_app_timeout
) {
6159 config
.app_socket_timeout
= atoi(env_app_timeout
);
6161 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6164 ret
= write_pidfile();
6166 ERR("Error in write_pidfile");
6168 goto exit_init_data
;
6171 /* Initialize communication library */
6173 /* Initialize TCP timeout values */
6174 lttcomm_inet_init();
6176 if (load_session_init_data(&load_info
) < 0) {
6178 goto exit_init_data
;
6180 load_info
->path
= config
.load_session_path
.value
;
6182 /* Create health-check thread. */
6183 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6184 thread_manage_health
, (void *) NULL
);
6187 PERROR("pthread_create health");
6193 * The rotation thread needs the notification thread to be ready before
6194 * creating the rotate_notification_channel, so we use this semaphore as
6195 * a rendez-vous point.
6197 sem_init(¬ification_thread_ready
, 0, 0);
6199 /* notification_thread_data acquires the pipes' read side. */
6200 notification_thread_handle
= notification_thread_handle_create(
6201 ust32_channel_monitor_pipe
,
6202 ust64_channel_monitor_pipe
,
6203 kernel_channel_monitor_pipe
,
6204 ¬ification_thread_ready
);
6205 if (!notification_thread_handle
) {
6207 ERR("Failed to create notification thread shared data");
6209 goto exit_notification
;
6212 /* Create notification thread. */
6213 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6214 thread_notification
, notification_thread_handle
);
6217 PERROR("pthread_create notification");
6220 goto exit_notification
;
6222 notification_thread_launched
= true;
6224 /* Create timer thread. */
6225 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
6226 timer_thread_func
, &timer_thread_ctx
);
6229 PERROR("pthread_create timer");
6232 goto exit_notification
;
6234 timer_thread_launched
= true;
6236 /* rotation_thread_data acquires the pipes' read side. */
6237 rotation_thread_handle
= rotation_thread_handle_create(
6238 thread_quit_pipe
[0],
6239 rotation_timer_queue
,
6240 notification_thread_handle
,
6241 ¬ification_thread_ready
);
6242 if (!rotation_thread_handle
) {
6244 ERR("Failed to create rotation thread shared data");
6249 /* Create rotation thread. */
6250 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6251 thread_rotation
, rotation_thread_handle
);
6254 PERROR("pthread_create rotation");
6259 rotation_thread_launched
= true;
6261 /* Create thread to manage the client socket */
6262 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6263 thread_manage_clients
, (void *) NULL
);
6266 PERROR("pthread_create clients");
6272 /* Create thread to dispatch registration */
6273 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6274 thread_dispatch_ust_registration
, (void *) NULL
);
6277 PERROR("pthread_create dispatch");
6283 /* Create thread to manage application registration. */
6284 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6285 thread_registration_apps
, (void *) NULL
);
6288 PERROR("pthread_create registration");
6294 /* Create thread to manage application socket */
6295 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6296 thread_manage_apps
, (void *) NULL
);
6299 PERROR("pthread_create apps");
6305 /* Create thread to manage application notify socket */
6306 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6307 ust_thread_manage_notify
, (void *) NULL
);
6310 PERROR("pthread_create notify");
6313 goto exit_apps_notify
;
6316 /* Create agent registration thread. */
6317 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6318 agent_thread_manage_registration
, (void *) NULL
);
6321 PERROR("pthread_create agent");
6324 goto exit_agent_reg
;
6327 /* Don't start this thread if kernel tracing is not requested nor root */
6328 if (is_root
&& !config
.no_kernel
) {
6329 /* Create kernel thread to manage kernel event */
6330 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6331 thread_manage_kernel
, (void *) NULL
);
6334 PERROR("pthread_create kernel");
6341 /* Create session loading thread. */
6342 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6343 thread_load_session
, load_info
);
6346 PERROR("pthread_create load_session_thread");
6349 goto exit_load_session
;
6353 * This is where we start awaiting program completion (e.g. through
6354 * signal that asks threads to teardown).
6357 ret
= pthread_join(load_session_thread
, &status
);
6360 PERROR("pthread_join load_session_thread");
6365 if (is_root
&& !config
.no_kernel
) {
6366 ret
= pthread_join(kernel_thread
, &status
);
6369 PERROR("pthread_join");
6375 ret
= pthread_join(agent_reg_thread
, &status
);
6378 PERROR("pthread_join agent");
6383 ret
= pthread_join(apps_notify_thread
, &status
);
6386 PERROR("pthread_join apps notify");
6391 ret
= pthread_join(apps_thread
, &status
);
6394 PERROR("pthread_join apps");
6399 ret
= pthread_join(reg_apps_thread
, &status
);
6402 PERROR("pthread_join");
6408 * Join dispatch thread after joining reg_apps_thread to ensure
6409 * we don't leak applications in the queue.
6411 ret
= pthread_join(dispatch_thread
, &status
);
6414 PERROR("pthread_join");
6419 ret
= pthread_join(client_thread
, &status
);
6422 PERROR("pthread_join");
6429 sem_destroy(¬ification_thread_ready
);
6430 ret
= pthread_join(health_thread
, &status
);
6433 PERROR("pthread_join health thread");
6440 * Wait for all pending call_rcu work to complete before tearing
6441 * down data structures. call_rcu worker may be trying to
6442 * perform lookups in those structures.
6446 * sessiond_cleanup() is called when no other thread is running, except
6447 * the ht_cleanup thread, which is needed to destroy the hash tables.
6449 rcu_thread_online();
6453 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6454 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6455 * the queue is empty before shutting down the clean-up thread.
6460 * The teardown of the notification system is performed after the
6461 * session daemon's teardown in order to allow it to be notified
6462 * of the active session and channels at the moment of the teardown.
6464 if (notification_thread_handle
) {
6465 if (notification_thread_launched
) {
6466 notification_thread_command_quit(
6467 notification_thread_handle
);
6468 ret
= pthread_join(notification_thread
, &status
);
6471 PERROR("pthread_join notification thread");
6475 notification_thread_handle_destroy(notification_thread_handle
);
6478 if (rotation_thread_handle
) {
6479 if (rotation_thread_launched
) {
6480 ret
= pthread_join(rotation_thread
, &status
);
6483 PERROR("pthread_join rotation thread");
6487 rotation_thread_handle_destroy(rotation_thread_handle
);
6490 if (timer_thread_launched
) {
6492 ret
= pthread_join(timer_thread
, &status
);
6495 PERROR("pthread_join timer thread");
6501 * After the rotation and timer thread have quit, we can safely destroy
6502 * the rotation_timer_queue.
6504 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
6506 rcu_thread_offline();
6507 rcu_unregister_thread();
6509 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6513 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6514 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6515 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6518 health_app_destroy(health_sessiond
);
6519 exit_health_sessiond_cleanup
:
6520 exit_create_run_as_worker_cleanup
:
6523 sessiond_cleanup_lock_file();
6524 sessiond_cleanup_options();
6526 exit_set_signal_handler
: