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>
55 #include "lttng-sessiond.h"
56 #include "buffer-registry.h"
63 #include "kernel-consumer.h"
67 #include "ust-consumer.h"
70 #include "health-sessiond.h"
71 #include "testpoint.h"
72 #include "ust-thread.h"
73 #include "agent-thread.h"
75 #include "load-session-thread.h"
76 #include "notification-thread.h"
77 #include "notification-thread-commands.h"
78 #include "rotation-thread.h"
79 #include "lttng-syscall.h"
81 #include "ht-cleanup.h"
82 #include "sessiond-config.h"
83 #include "sessiond-timer.h"
85 static const char *help_msg
=
86 #ifdef LTTNG_EMBED_HELP
87 #include <lttng-sessiond.8.h>
94 static pid_t ppid
; /* Parent PID for --sig-parent option */
95 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
96 static int lockfile_fd
= -1;
98 /* Set to 1 when a SIGUSR1 signal is received. */
99 static int recv_child_signal
;
101 static struct lttng_kernel_tracer_version kernel_tracer_version
;
102 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
105 * Consumer daemon specific control data. Every value not initialized here is
106 * set to 0 by the static definition.
108 static struct consumer_data kconsumer_data
= {
109 .type
= LTTNG_CONSUMER_KERNEL
,
112 .channel_monitor_pipe
= -1,
113 .channel_rotate_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 .channel_rotate_pipe
= -1,
125 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
126 .lock
= PTHREAD_MUTEX_INITIALIZER
,
127 .cond
= PTHREAD_COND_INITIALIZER
,
128 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
130 static struct consumer_data ustconsumer32_data
= {
131 .type
= LTTNG_CONSUMER32_UST
,
134 .channel_monitor_pipe
= -1,
135 .channel_rotate_pipe
= -1,
136 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
137 .lock
= PTHREAD_MUTEX_INITIALIZER
,
138 .cond
= PTHREAD_COND_INITIALIZER
,
139 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
142 /* Command line options */
143 static const struct option long_options
[] = {
144 { "client-sock", required_argument
, 0, 'c' },
145 { "apps-sock", required_argument
, 0, 'a' },
146 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
147 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
148 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
149 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
150 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
151 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
152 { "consumerd32-path", required_argument
, 0, '\0' },
153 { "consumerd32-libdir", required_argument
, 0, '\0' },
154 { "consumerd64-path", required_argument
, 0, '\0' },
155 { "consumerd64-libdir", required_argument
, 0, '\0' },
156 { "daemonize", no_argument
, 0, 'd' },
157 { "background", no_argument
, 0, 'b' },
158 { "sig-parent", no_argument
, 0, 'S' },
159 { "help", no_argument
, 0, 'h' },
160 { "group", required_argument
, 0, 'g' },
161 { "version", no_argument
, 0, 'V' },
162 { "quiet", no_argument
, 0, 'q' },
163 { "verbose", no_argument
, 0, 'v' },
164 { "verbose-consumer", no_argument
, 0, '\0' },
165 { "no-kernel", no_argument
, 0, '\0' },
166 { "pidfile", required_argument
, 0, 'p' },
167 { "agent-tcp-port", required_argument
, 0, '\0' },
168 { "config", required_argument
, 0, 'f' },
169 { "load", required_argument
, 0, 'l' },
170 { "kmod-probes", required_argument
, 0, '\0' },
171 { "extra-kmod-probes", required_argument
, 0, '\0' },
175 struct sessiond_config config
;
177 /* Command line options to ignore from configuration file */
178 static const char *config_ignore_options
[] = { "help", "version", "config" };
180 /* Shared between threads */
181 static int dispatch_thread_exit
;
183 /* Sockets and FDs */
184 static int client_sock
= -1;
185 static int apps_sock
= -1;
186 int kernel_tracer_fd
= -1;
187 static int kernel_poll_pipe
[2] = { -1, -1 };
190 * Quit pipe for all threads. This permits a single cancellation point
191 * for all threads when receiving an event on the pipe.
193 static int thread_quit_pipe
[2] = { -1, -1 };
196 * This pipe is used to inform the thread managing application communication
197 * that a command is queued and ready to be processed.
199 static int apps_cmd_pipe
[2] = { -1, -1 };
201 int apps_cmd_notify_pipe
[2] = { -1, -1 };
203 /* Pthread, Mutexes and Semaphores */
204 static pthread_t apps_thread
;
205 static pthread_t apps_notify_thread
;
206 static pthread_t reg_apps_thread
;
207 static pthread_t client_thread
;
208 static pthread_t kernel_thread
;
209 static pthread_t dispatch_thread
;
210 static pthread_t health_thread
;
211 static pthread_t ht_cleanup_thread
;
212 static pthread_t agent_reg_thread
;
213 static pthread_t load_session_thread
;
214 static pthread_t notification_thread
;
215 static pthread_t rotation_thread
;
216 static pthread_t timer_thread
;
219 * UST registration command queue. This queue is tied with a futex and uses a N
220 * wakers / 1 waiter implemented and detailed in futex.c/.h
222 * The thread_registration_apps and thread_dispatch_ust_registration uses this
223 * queue along with the wait/wake scheme. The thread_manage_apps receives down
224 * the line new application socket and monitors it for any I/O error or clean
225 * close that triggers an unregistration of the application.
227 static struct ust_cmd_queue ust_cmd_queue
;
230 * Pointer initialized before thread creation.
232 * This points to the tracing session list containing the session count and a
233 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
234 * MUST NOT be taken if you call a public function in session.c.
236 * The lock is nested inside the structure: session_list_ptr->lock. Please use
237 * session_lock_list and session_unlock_list for lock acquisition.
239 static struct ltt_session_list
*session_list_ptr
;
241 int ust_consumerd64_fd
= -1;
242 int ust_consumerd32_fd
= -1;
244 static const char *module_proc_lttng
= "/proc/lttng";
247 * Consumer daemon state which is changed when spawning it, killing it or in
248 * case of a fatal error.
250 enum consumerd_state
{
251 CONSUMER_STARTED
= 1,
252 CONSUMER_STOPPED
= 2,
257 * This consumer daemon state is used to validate if a client command will be
258 * able to reach the consumer. If not, the client is informed. For instance,
259 * doing a "lttng start" when the consumer state is set to ERROR will return an
260 * error to the client.
262 * The following example shows a possible race condition of this scheme:
264 * consumer thread error happens
266 * client cmd checks state -> still OK
267 * consumer thread exit, sets error
268 * client cmd try to talk to consumer
271 * However, since the consumer is a different daemon, we have no way of making
272 * sure the command will reach it safely even with this state flag. This is why
273 * we consider that up to the state validation during command processing, the
274 * command is safe. After that, we can not guarantee the correctness of the
275 * client request vis-a-vis the consumer.
277 static enum consumerd_state ust_consumerd_state
;
278 static enum consumerd_state kernel_consumerd_state
;
280 /* Set in main() with the current page size. */
283 /* Application health monitoring */
284 struct health_app
*health_sessiond
;
286 /* Am I root or not. */
287 int is_root
; /* Set to 1 if the daemon is running as root */
289 const char * const config_section_name
= "sessiond";
291 /* Load session thread information to operate. */
292 struct load_session_thread_data
*load_info
;
294 /* Notification thread handle. */
295 struct notification_thread_handle
*notification_thread_handle
;
297 /* Rotation thread handle. */
298 struct rotation_thread_handle
*rotation_thread_handle
;
300 /* Global hash tables */
301 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
304 * The initialization of the session daemon is done in multiple phases.
306 * While all threads are launched near-simultaneously, only some of them
307 * are needed to ensure the session daemon can start to respond to client
310 * There are two important guarantees that we wish to offer with respect
311 * to the initialisation of the session daemon:
312 * - When the daemonize/background launcher process exits, the sessiond
313 * is fully able to respond to client requests,
314 * - Auto-loaded sessions are visible to clients.
316 * In order to achieve this, a number of support threads have to be launched
317 * to allow the "client" thread to function properly. Moreover, since the
318 * "load session" thread needs the client thread, we must provide a way
319 * for the "load session" thread to know that the "client" thread is up
322 * Hence, the support threads decrement the lttng_sessiond_ready counter
323 * while the "client" threads waits for it to reach 0. Once the "client" thread
324 * unblocks, it posts the message_thread_ready semaphore which allows the
325 * "load session" thread to progress.
327 * This implies that the "load session" thread is the last to be initialized
328 * and will explicitly call sessiond_signal_parents(), which signals the parents
329 * that the session daemon is fully initialized.
331 * The four (4) support threads are:
333 * - notification_thread
337 #define NR_LTTNG_SESSIOND_SUPPORT_THREADS 4
338 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_SUPPORT_THREADS
;
340 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
342 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
345 /* Notify parents that we are ready for cmd and health check */
347 void sessiond_signal_parents(void)
350 * Notify parent pid that we are ready to accept command
351 * for client side. This ppid is the one from the
352 * external process that spawned us.
354 if (config
.sig_parent
) {
359 * Notify the parent of the fork() process that we are
362 if (config
.daemonize
|| config
.background
) {
363 kill(child_ppid
, SIGUSR1
);
368 void sessiond_notify_ready(void)
371 * This memory barrier is paired with the one performed by
372 * the client thread after it has seen that 'lttng_sessiond_ready' is 0.
374 * The purpose of these memory barriers is to ensure that all
375 * initialization operations of the various threads that call this
376 * function to signal that they are ready are commited/published
377 * before the client thread can see the 'lttng_sessiond_ready' counter
380 * Note that this could be a 'write' memory barrier, but a full barrier
381 * is used in case the code using this utility changes. The performance
382 * implications of this choice are minimal since this is a slow path.
385 uatomic_sub(<tng_sessiond_ready
, 1);
389 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
396 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
402 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
414 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
416 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
418 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
422 * Init thread quit pipe.
424 * Return -1 on error or 0 if all pipes are created.
426 static int __init_thread_quit_pipe(int *a_pipe
)
432 PERROR("thread quit pipe");
436 for (i
= 0; i
< 2; i
++) {
437 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
448 static int init_thread_quit_pipe(void)
450 return __init_thread_quit_pipe(thread_quit_pipe
);
454 * Stop all threads by closing the thread quit pipe.
456 static void stop_threads(void)
460 /* Stopping all threads */
461 DBG("Terminating all threads");
462 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
464 ERR("write error on thread quit pipe");
467 /* Dispatch thread */
468 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
469 futex_nto1_wake(&ust_cmd_queue
.futex
);
473 * Close every consumer sockets.
475 static void close_consumer_sockets(void)
479 if (kconsumer_data
.err_sock
>= 0) {
480 ret
= close(kconsumer_data
.err_sock
);
482 PERROR("kernel consumer err_sock close");
485 if (ustconsumer32_data
.err_sock
>= 0) {
486 ret
= close(ustconsumer32_data
.err_sock
);
488 PERROR("UST consumerd32 err_sock close");
491 if (ustconsumer64_data
.err_sock
>= 0) {
492 ret
= close(ustconsumer64_data
.err_sock
);
494 PERROR("UST consumerd64 err_sock close");
497 if (kconsumer_data
.cmd_sock
>= 0) {
498 ret
= close(kconsumer_data
.cmd_sock
);
500 PERROR("kernel consumer cmd_sock close");
503 if (ustconsumer32_data
.cmd_sock
>= 0) {
504 ret
= close(ustconsumer32_data
.cmd_sock
);
506 PERROR("UST consumerd32 cmd_sock close");
509 if (ustconsumer64_data
.cmd_sock
>= 0) {
510 ret
= close(ustconsumer64_data
.cmd_sock
);
512 PERROR("UST consumerd64 cmd_sock close");
515 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
516 ret
= close(kconsumer_data
.channel_monitor_pipe
);
518 PERROR("kernel consumer channel monitor pipe close");
521 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
522 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
524 PERROR("UST consumerd32 channel monitor pipe close");
527 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
528 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
530 PERROR("UST consumerd64 channel monitor pipe close");
533 if (kconsumer_data
.channel_rotate_pipe
>= 0) {
534 ret
= close(kconsumer_data
.channel_rotate_pipe
);
536 PERROR("kernel consumer channel rotate pipe close");
539 if (ustconsumer32_data
.channel_rotate_pipe
>= 0) {
540 ret
= close(ustconsumer32_data
.channel_rotate_pipe
);
542 PERROR("UST consumerd32 channel rotate pipe close");
545 if (ustconsumer64_data
.channel_rotate_pipe
>= 0) {
546 ret
= close(ustconsumer64_data
.channel_rotate_pipe
);
548 PERROR("UST consumerd64 channel rotate pipe close");
554 * Wait on consumer process termination.
556 * Need to be called with the consumer data lock held or from a context
557 * ensuring no concurrent access to data (e.g: cleanup).
559 static void wait_consumer(struct consumer_data
*consumer_data
)
564 if (consumer_data
->pid
<= 0) {
568 DBG("Waiting for complete teardown of consumerd (PID: %d)",
570 ret
= waitpid(consumer_data
->pid
, &status
, 0);
572 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
573 } else if (!WIFEXITED(status
)) {
574 ERR("consumerd termination with error: %d",
577 consumer_data
->pid
= 0;
581 * Cleanup the session daemon's data structures.
583 static void sessiond_cleanup(void)
586 struct ltt_session
*sess
, *stmp
;
588 DBG("Cleanup sessiond");
591 * Close the thread quit pipe. It has already done its job,
592 * since we are now called.
594 utils_close_pipe(thread_quit_pipe
);
596 ret
= remove(config
.pid_file_path
.value
);
598 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
601 DBG("Removing sessiond and consumerd content of directory %s",
602 config
.rundir
.value
);
605 DBG("Removing %s", config
.pid_file_path
.value
);
606 (void) unlink(config
.pid_file_path
.value
);
608 DBG("Removing %s", config
.agent_port_file_path
.value
);
609 (void) unlink(config
.agent_port_file_path
.value
);
612 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
613 (void) unlink(kconsumer_data
.err_unix_sock_path
);
615 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
616 (void) rmdir(config
.kconsumerd_path
.value
);
618 /* ust consumerd 32 */
619 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
620 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
622 DBG("Removing directory %s", config
.consumerd32_path
.value
);
623 (void) rmdir(config
.consumerd32_path
.value
);
625 /* ust consumerd 64 */
626 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
627 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
629 DBG("Removing directory %s", config
.consumerd64_path
.value
);
630 (void) rmdir(config
.consumerd64_path
.value
);
632 DBG("Cleaning up all sessions");
634 /* Destroy session list mutex */
635 if (session_list_ptr
!= NULL
) {
636 pthread_mutex_destroy(&session_list_ptr
->lock
);
638 /* Cleanup ALL session */
639 cds_list_for_each_entry_safe(sess
, stmp
,
640 &session_list_ptr
->head
, list
) {
641 cmd_destroy_session(sess
, kernel_poll_pipe
[1],
642 notification_thread_handle
);
646 wait_consumer(&kconsumer_data
);
647 wait_consumer(&ustconsumer64_data
);
648 wait_consumer(&ustconsumer32_data
);
650 DBG("Cleaning up all agent apps");
651 agent_app_ht_clean();
653 DBG("Closing all UST sockets");
654 ust_app_clean_list();
655 buffer_reg_destroy_registries();
657 if (is_root
&& !config
.no_kernel
) {
658 DBG2("Closing kernel fd");
659 if (kernel_tracer_fd
>= 0) {
660 ret
= close(kernel_tracer_fd
);
665 DBG("Unloading kernel modules");
666 modprobe_remove_lttng_all();
670 close_consumer_sockets();
673 load_session_destroy_data(load_info
);
678 * We do NOT rmdir rundir because there are other processes
679 * using it, for instance lttng-relayd, which can start in
680 * parallel with this teardown.
685 * Cleanup the daemon's option data structures.
687 static void sessiond_cleanup_options(void)
689 DBG("Cleaning up options");
691 sessiond_config_fini(&config
);
693 run_as_destroy_worker();
697 * Send data on a unix socket using the liblttsessiondcomm API.
699 * Return lttcomm error code.
701 static int send_unix_sock(int sock
, void *buf
, size_t len
)
703 /* Check valid length */
708 return lttcomm_send_unix_sock(sock
, buf
, len
);
712 * Free memory of a command context structure.
714 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
716 DBG("Clean command context structure");
718 if ((*cmd_ctx
)->llm
) {
719 free((*cmd_ctx
)->llm
);
721 if ((*cmd_ctx
)->lsm
) {
722 free((*cmd_ctx
)->lsm
);
730 * Notify UST applications using the shm mmap futex.
732 static int notify_ust_apps(int active
)
736 DBG("Notifying applications of session daemon state: %d", active
);
738 /* See shm.c for this call implying mmap, shm and futex calls */
739 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
740 if (wait_shm_mmap
== NULL
) {
744 /* Wake waiting process */
745 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
747 /* Apps notified successfully */
755 * Setup the outgoing data buffer for the response (llm) by allocating the
756 * right amount of memory and copying the original information from the lsm
759 * Return 0 on success, negative value on error.
761 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
762 const void *payload_buf
, size_t payload_len
,
763 const void *cmd_header_buf
, size_t cmd_header_len
)
766 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
767 const size_t cmd_header_offset
= header_len
;
768 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
769 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
771 cmd_ctx
->llm
= zmalloc(total_msg_size
);
773 if (cmd_ctx
->llm
== NULL
) {
779 /* Copy common data */
780 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
781 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
782 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
783 cmd_ctx
->llm
->data_size
= payload_len
;
784 cmd_ctx
->lttng_msg_size
= total_msg_size
;
786 /* Copy command header */
787 if (cmd_header_len
) {
788 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
794 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
803 * Version of setup_lttng_msg() without command header.
805 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
806 void *payload_buf
, size_t payload_len
)
808 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
811 * Update the kernel poll set of all channel fd available over all tracing
812 * session. Add the wakeup pipe at the end of the set.
814 static int update_kernel_poll(struct lttng_poll_event
*events
)
817 struct ltt_session
*session
;
818 struct ltt_kernel_channel
*channel
;
820 DBG("Updating kernel poll set");
823 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
824 session_lock(session
);
825 if (session
->kernel_session
== NULL
) {
826 session_unlock(session
);
830 cds_list_for_each_entry(channel
,
831 &session
->kernel_session
->channel_list
.head
, list
) {
832 /* Add channel fd to the kernel poll set */
833 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
835 session_unlock(session
);
838 DBG("Channel fd %d added to kernel set", channel
->fd
);
840 session_unlock(session
);
842 session_unlock_list();
847 session_unlock_list();
852 * Find the channel fd from 'fd' over all tracing session. When found, check
853 * for new channel stream and send those stream fds to the kernel consumer.
855 * Useful for CPU hotplug feature.
857 static int update_kernel_stream(int fd
)
860 struct ltt_session
*session
;
861 struct ltt_kernel_session
*ksess
;
862 struct ltt_kernel_channel
*channel
;
864 DBG("Updating kernel streams for channel fd %d", fd
);
867 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
868 session_lock(session
);
869 if (session
->kernel_session
== NULL
) {
870 session_unlock(session
);
873 ksess
= session
->kernel_session
;
875 cds_list_for_each_entry(channel
,
876 &ksess
->channel_list
.head
, list
) {
877 struct lttng_ht_iter iter
;
878 struct consumer_socket
*socket
;
880 if (channel
->fd
!= fd
) {
883 DBG("Channel found, updating kernel streams");
884 ret
= kernel_open_channel_stream(channel
);
888 /* Update the stream global counter */
889 ksess
->stream_count_global
+= ret
;
892 * Have we already sent fds to the consumer? If yes, it
893 * means that tracing is started so it is safe to send
894 * our updated stream fds.
896 if (ksess
->consumer_fds_sent
!= 1
897 || ksess
->consumer
== NULL
) {
903 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
904 &iter
.iter
, socket
, node
.node
) {
905 pthread_mutex_lock(socket
->lock
);
906 ret
= kernel_consumer_send_channel_streams(socket
,
908 session
->output_traces
? 1 : 0);
909 pthread_mutex_unlock(socket
->lock
);
917 session_unlock(session
);
919 session_unlock_list();
923 session_unlock(session
);
924 session_unlock_list();
929 * For each tracing session, update newly registered apps. The session list
930 * lock MUST be acquired before calling this.
932 static void update_ust_app(int app_sock
)
934 struct ltt_session
*sess
, *stmp
;
936 /* Consumer is in an ERROR state. Stop any application update. */
937 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
938 /* Stop the update process since the consumer is dead. */
942 /* For all tracing session(s) */
943 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
947 if (!sess
->ust_session
) {
952 assert(app_sock
>= 0);
953 app
= ust_app_find_by_sock(app_sock
);
956 * Application can be unregistered before so
957 * this is possible hence simply stopping the
960 DBG3("UST app update failed to find app sock %d",
964 ust_app_global_update(sess
->ust_session
, app
);
968 session_unlock(sess
);
973 * This thread manage event coming from the kernel.
975 * Features supported in this thread:
978 static void *thread_manage_kernel(void *data
)
980 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
981 uint32_t revents
, nb_fd
;
983 struct lttng_poll_event events
;
985 DBG("[thread] Thread manage kernel started");
987 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
990 * This first step of the while is to clean this structure which could free
991 * non NULL pointers so initialize it before the loop.
993 lttng_poll_init(&events
);
995 if (testpoint(sessiond_thread_manage_kernel
)) {
996 goto error_testpoint
;
999 health_code_update();
1001 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1002 goto error_testpoint
;
1006 health_code_update();
1008 if (update_poll_flag
== 1) {
1009 /* Clean events object. We are about to populate it again. */
1010 lttng_poll_clean(&events
);
1012 ret
= sessiond_set_thread_pollset(&events
, 2);
1014 goto error_poll_create
;
1017 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1022 /* This will add the available kernel channel if any. */
1023 ret
= update_kernel_poll(&events
);
1027 update_poll_flag
= 0;
1030 DBG("Thread kernel polling");
1032 /* Poll infinite value of time */
1034 health_poll_entry();
1035 ret
= lttng_poll_wait(&events
, -1);
1036 DBG("Thread kernel return from poll on %d fds",
1037 LTTNG_POLL_GETNB(&events
));
1041 * Restart interrupted system call.
1043 if (errno
== EINTR
) {
1047 } else if (ret
== 0) {
1048 /* Should not happen since timeout is infinite */
1049 ERR("Return value of poll is 0 with an infinite timeout.\n"
1050 "This should not have happened! Continuing...");
1056 for (i
= 0; i
< nb_fd
; i
++) {
1057 /* Fetch once the poll data */
1058 revents
= LTTNG_POLL_GETEV(&events
, i
);
1059 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1061 health_code_update();
1064 /* No activity for this FD (poll implementation). */
1068 /* Thread quit pipe has been closed. Killing thread. */
1069 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1075 /* Check for data on kernel pipe */
1076 if (revents
& LPOLLIN
) {
1077 if (pollfd
== kernel_poll_pipe
[0]) {
1078 (void) lttng_read(kernel_poll_pipe
[0],
1081 * Ret value is useless here, if this pipe gets any actions an
1082 * update is required anyway.
1084 update_poll_flag
= 1;
1088 * New CPU detected by the kernel. Adding kernel stream to
1089 * kernel session and updating the kernel consumer
1091 ret
= update_kernel_stream(pollfd
);
1097 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1098 update_poll_flag
= 1;
1101 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1109 lttng_poll_clean(&events
);
1112 utils_close_pipe(kernel_poll_pipe
);
1113 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1116 ERR("Health error occurred in %s", __func__
);
1117 WARN("Kernel thread died unexpectedly. "
1118 "Kernel tracing can continue but CPU hotplug is disabled.");
1120 health_unregister(health_sessiond
);
1121 DBG("Kernel thread dying");
1126 * Signal pthread condition of the consumer data that the thread.
1128 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1130 pthread_mutex_lock(&data
->cond_mutex
);
1133 * The state is set before signaling. It can be any value, it's the waiter
1134 * job to correctly interpret this condition variable associated to the
1135 * consumer pthread_cond.
1137 * A value of 0 means that the corresponding thread of the consumer data
1138 * was not started. 1 indicates that the thread has started and is ready
1139 * for action. A negative value means that there was an error during the
1142 data
->consumer_thread_is_ready
= state
;
1143 (void) pthread_cond_signal(&data
->cond
);
1145 pthread_mutex_unlock(&data
->cond_mutex
);
1149 * This thread manage the consumer error sent back to the session daemon.
1151 static void *thread_manage_consumer(void *data
)
1153 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1154 uint32_t revents
, nb_fd
;
1155 enum lttcomm_return_code code
;
1156 struct lttng_poll_event events
;
1157 struct consumer_data
*consumer_data
= data
;
1158 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1160 DBG("[thread] Manage consumer started");
1162 rcu_register_thread();
1163 rcu_thread_online();
1165 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1167 health_code_update();
1170 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1171 * metadata_sock. Nothing more will be added to this poll set.
1173 ret
= sessiond_set_thread_pollset(&events
, 3);
1179 * The error socket here is already in a listening state which was done
1180 * just before spawning this thread to avoid a race between the consumer
1181 * daemon exec trying to connect and the listen() call.
1183 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1188 health_code_update();
1190 /* Infinite blocking call, waiting for transmission */
1192 health_poll_entry();
1194 if (testpoint(sessiond_thread_manage_consumer
)) {
1198 ret
= lttng_poll_wait(&events
, -1);
1202 * Restart interrupted system call.
1204 if (errno
== EINTR
) {
1212 for (i
= 0; i
< nb_fd
; i
++) {
1213 /* Fetch once the poll data */
1214 revents
= LTTNG_POLL_GETEV(&events
, i
);
1215 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1217 health_code_update();
1220 /* No activity for this FD (poll implementation). */
1224 /* Thread quit pipe has been closed. Killing thread. */
1225 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1231 /* Event on the registration socket */
1232 if (pollfd
== consumer_data
->err_sock
) {
1233 if (revents
& LPOLLIN
) {
1235 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1236 ERR("consumer err socket poll error");
1239 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1245 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1251 * Set the CLOEXEC flag. Return code is useless because either way, the
1254 (void) utils_set_fd_cloexec(sock
);
1256 health_code_update();
1258 DBG2("Receiving code from consumer err_sock");
1260 /* Getting status code from kconsumerd */
1261 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1262 sizeof(enum lttcomm_return_code
));
1267 health_code_update();
1268 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1269 ERR("consumer error when waiting for SOCK_READY : %s",
1270 lttcomm_get_readable_code(-code
));
1274 /* Connect both command and metadata sockets. */
1275 consumer_data
->cmd_sock
=
1276 lttcomm_connect_unix_sock(
1277 consumer_data
->cmd_unix_sock_path
);
1278 consumer_data
->metadata_fd
=
1279 lttcomm_connect_unix_sock(
1280 consumer_data
->cmd_unix_sock_path
);
1281 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1282 PERROR("consumer connect cmd socket");
1283 /* On error, signal condition and quit. */
1284 signal_consumer_condition(consumer_data
, -1);
1288 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1290 /* Create metadata socket lock. */
1291 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1292 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1293 PERROR("zmalloc pthread mutex");
1296 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1298 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1299 DBG("Consumer metadata socket ready (fd: %d)",
1300 consumer_data
->metadata_fd
);
1303 * Remove the consumerd error sock since we've established a connection.
1305 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1310 /* Add new accepted error socket. */
1311 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1316 /* Add metadata socket that is successfully connected. */
1317 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1318 LPOLLIN
| LPOLLRDHUP
);
1323 health_code_update();
1326 * Transfer the write-end of the channel monitoring and rotate pipe
1327 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1328 * SET_CHANNEL_ROTATE_PIPE commands.
1330 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1331 if (!cmd_socket_wrapper
) {
1334 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1336 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1337 consumer_data
->channel_monitor_pipe
);
1342 ret
= consumer_send_channel_rotate_pipe(cmd_socket_wrapper
,
1343 consumer_data
->channel_rotate_pipe
);
1348 /* Discard the socket wrapper as it is no longer needed. */
1349 consumer_destroy_socket(cmd_socket_wrapper
);
1350 cmd_socket_wrapper
= NULL
;
1352 /* The thread is completely initialized, signal that it is ready. */
1353 signal_consumer_condition(consumer_data
, 1);
1355 /* Infinite blocking call, waiting for transmission */
1358 health_code_update();
1360 /* Exit the thread because the thread quit pipe has been triggered. */
1362 /* Not a health error. */
1367 health_poll_entry();
1368 ret
= lttng_poll_wait(&events
, -1);
1372 * Restart interrupted system call.
1374 if (errno
== EINTR
) {
1382 for (i
= 0; i
< nb_fd
; i
++) {
1383 /* Fetch once the poll data */
1384 revents
= LTTNG_POLL_GETEV(&events
, i
);
1385 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1387 health_code_update();
1390 /* No activity for this FD (poll implementation). */
1395 * Thread quit pipe has been triggered, flag that we should stop
1396 * but continue the current loop to handle potential data from
1399 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1401 if (pollfd
== sock
) {
1402 /* Event on the consumerd socket */
1403 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1404 && !(revents
& LPOLLIN
)) {
1405 ERR("consumer err socket second poll error");
1408 health_code_update();
1409 /* Wait for any kconsumerd error */
1410 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1411 sizeof(enum lttcomm_return_code
));
1413 ERR("consumer closed the command socket");
1417 ERR("consumer return code : %s",
1418 lttcomm_get_readable_code(-code
));
1421 } else if (pollfd
== consumer_data
->metadata_fd
) {
1422 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1423 && !(revents
& LPOLLIN
)) {
1424 ERR("consumer err metadata socket second poll error");
1427 /* UST metadata requests */
1428 ret
= ust_consumer_metadata_request(
1429 &consumer_data
->metadata_sock
);
1431 ERR("Handling metadata request");
1435 /* No need for an else branch all FDs are tested prior. */
1437 health_code_update();
1443 * We lock here because we are about to close the sockets and some other
1444 * thread might be using them so get exclusive access which will abort all
1445 * other consumer command by other threads.
1447 pthread_mutex_lock(&consumer_data
->lock
);
1449 /* Immediately set the consumerd state to stopped */
1450 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1451 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1452 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1453 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1454 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1456 /* Code flow error... */
1460 if (consumer_data
->err_sock
>= 0) {
1461 ret
= close(consumer_data
->err_sock
);
1465 consumer_data
->err_sock
= -1;
1467 if (consumer_data
->cmd_sock
>= 0) {
1468 ret
= close(consumer_data
->cmd_sock
);
1472 consumer_data
->cmd_sock
= -1;
1474 if (consumer_data
->metadata_sock
.fd_ptr
&&
1475 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1476 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1488 unlink(consumer_data
->err_unix_sock_path
);
1489 unlink(consumer_data
->cmd_unix_sock_path
);
1490 pthread_mutex_unlock(&consumer_data
->lock
);
1492 /* Cleanup metadata socket mutex. */
1493 if (consumer_data
->metadata_sock
.lock
) {
1494 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1495 free(consumer_data
->metadata_sock
.lock
);
1497 lttng_poll_clean(&events
);
1499 if (cmd_socket_wrapper
) {
1500 consumer_destroy_socket(cmd_socket_wrapper
);
1505 ERR("Health error occurred in %s", __func__
);
1507 health_unregister(health_sessiond
);
1508 DBG("consumer thread cleanup completed");
1510 rcu_thread_offline();
1511 rcu_unregister_thread();
1517 * This thread receives application command sockets (FDs) on the
1518 * apps_cmd_pipe and waits (polls) on them until they are closed
1519 * or an error occurs.
1521 * At that point, it flushes the data (tracing and metadata) associated
1522 * with this application and tears down ust app sessions and other
1523 * associated data structures through ust_app_unregister().
1525 * Note that this thread never sends commands to the applications
1526 * through the command sockets; it merely listens for hang-ups
1527 * and errors on those sockets and cleans-up as they occur.
1529 static void *thread_manage_apps(void *data
)
1531 int i
, ret
, pollfd
, err
= -1;
1533 uint32_t revents
, nb_fd
;
1534 struct lttng_poll_event events
;
1536 DBG("[thread] Manage application started");
1538 rcu_register_thread();
1539 rcu_thread_online();
1541 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1543 if (testpoint(sessiond_thread_manage_apps
)) {
1544 goto error_testpoint
;
1547 health_code_update();
1549 ret
= sessiond_set_thread_pollset(&events
, 2);
1551 goto error_poll_create
;
1554 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1559 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1563 health_code_update();
1566 DBG("Apps thread polling");
1568 /* Inifinite blocking call, waiting for transmission */
1570 health_poll_entry();
1571 ret
= lttng_poll_wait(&events
, -1);
1572 DBG("Apps thread return from poll on %d fds",
1573 LTTNG_POLL_GETNB(&events
));
1577 * Restart interrupted system call.
1579 if (errno
== EINTR
) {
1587 for (i
= 0; i
< nb_fd
; i
++) {
1588 /* Fetch once the poll data */
1589 revents
= LTTNG_POLL_GETEV(&events
, i
);
1590 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1592 health_code_update();
1595 /* No activity for this FD (poll implementation). */
1599 /* Thread quit pipe has been closed. Killing thread. */
1600 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1606 /* Inspect the apps cmd pipe */
1607 if (pollfd
== apps_cmd_pipe
[0]) {
1608 if (revents
& LPOLLIN
) {
1612 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1613 if (size_ret
< sizeof(sock
)) {
1614 PERROR("read apps cmd pipe");
1618 health_code_update();
1621 * Since this is a command socket (write then read),
1622 * we only monitor the error events of the socket.
1624 ret
= lttng_poll_add(&events
, sock
,
1625 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1630 DBG("Apps with sock %d added to poll set", sock
);
1631 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1632 ERR("Apps command pipe error");
1635 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1640 * At this point, we know that a registered application made
1641 * the event at poll_wait.
1643 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1644 /* Removing from the poll set */
1645 ret
= lttng_poll_del(&events
, pollfd
);
1650 /* Socket closed on remote end. */
1651 ust_app_unregister(pollfd
);
1653 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1658 health_code_update();
1664 lttng_poll_clean(&events
);
1667 utils_close_pipe(apps_cmd_pipe
);
1668 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1671 * We don't clean the UST app hash table here since already registered
1672 * applications can still be controlled so let them be until the session
1673 * daemon dies or the applications stop.
1678 ERR("Health error occurred in %s", __func__
);
1680 health_unregister(health_sessiond
);
1681 DBG("Application communication apps thread cleanup complete");
1682 rcu_thread_offline();
1683 rcu_unregister_thread();
1688 * Send a socket to a thread This is called from the dispatch UST registration
1689 * thread once all sockets are set for the application.
1691 * The sock value can be invalid, we don't really care, the thread will handle
1692 * it and make the necessary cleanup if so.
1694 * On success, return 0 else a negative value being the errno message of the
1697 static int send_socket_to_thread(int fd
, int sock
)
1702 * It's possible that the FD is set as invalid with -1 concurrently just
1703 * before calling this function being a shutdown state of the thread.
1710 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1711 if (ret
< sizeof(sock
)) {
1712 PERROR("write apps pipe %d", fd
);
1719 /* All good. Don't send back the write positive ret value. */
1726 * Sanitize the wait queue of the dispatch registration thread meaning removing
1727 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1728 * notify socket is never received.
1730 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1732 int ret
, nb_fd
= 0, i
;
1733 unsigned int fd_added
= 0;
1734 struct lttng_poll_event events
;
1735 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1739 lttng_poll_init(&events
);
1741 /* Just skip everything for an empty queue. */
1742 if (!wait_queue
->count
) {
1746 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1751 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1752 &wait_queue
->head
, head
) {
1753 assert(wait_node
->app
);
1754 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1755 LPOLLHUP
| LPOLLERR
);
1768 * Poll but don't block so we can quickly identify the faulty events and
1769 * clean them afterwards from the wait queue.
1771 ret
= lttng_poll_wait(&events
, 0);
1777 for (i
= 0; i
< nb_fd
; i
++) {
1778 /* Get faulty FD. */
1779 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1780 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1783 /* No activity for this FD (poll implementation). */
1787 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1788 &wait_queue
->head
, head
) {
1789 if (pollfd
== wait_node
->app
->sock
&&
1790 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1791 cds_list_del(&wait_node
->head
);
1792 wait_queue
->count
--;
1793 ust_app_destroy(wait_node
->app
);
1796 * Silence warning of use-after-free in
1797 * cds_list_for_each_entry_safe which uses
1798 * __typeof__(*wait_node).
1803 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1810 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1814 lttng_poll_clean(&events
);
1818 lttng_poll_clean(&events
);
1820 ERR("Unable to sanitize wait queue");
1825 * Dispatch request from the registration threads to the application
1826 * communication thread.
1828 static void *thread_dispatch_ust_registration(void *data
)
1831 struct cds_wfcq_node
*node
;
1832 struct ust_command
*ust_cmd
= NULL
;
1833 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1834 struct ust_reg_wait_queue wait_queue
= {
1838 rcu_register_thread();
1840 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1842 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1843 goto error_testpoint
;
1846 health_code_update();
1848 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1850 DBG("[thread] Dispatch UST command started");
1853 health_code_update();
1855 /* Atomically prepare the queue futex */
1856 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1858 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1863 struct ust_app
*app
= NULL
;
1867 * Make sure we don't have node(s) that have hung up before receiving
1868 * the notify socket. This is to clean the list in order to avoid
1869 * memory leaks from notify socket that are never seen.
1871 sanitize_wait_queue(&wait_queue
);
1873 health_code_update();
1874 /* Dequeue command for registration */
1875 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1877 DBG("Woken up but nothing in the UST command queue");
1878 /* Continue thread execution */
1882 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1884 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1885 " gid:%d sock:%d name:%s (version %d.%d)",
1886 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1887 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1888 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1889 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1891 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1892 wait_node
= zmalloc(sizeof(*wait_node
));
1894 PERROR("zmalloc wait_node dispatch");
1895 ret
= close(ust_cmd
->sock
);
1897 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1899 lttng_fd_put(LTTNG_FD_APPS
, 1);
1903 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1905 /* Create application object if socket is CMD. */
1906 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1908 if (!wait_node
->app
) {
1909 ret
= close(ust_cmd
->sock
);
1911 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1913 lttng_fd_put(LTTNG_FD_APPS
, 1);
1919 * Add application to the wait queue so we can set the notify
1920 * socket before putting this object in the global ht.
1922 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1927 * We have to continue here since we don't have the notify
1928 * socket and the application MUST be added to the hash table
1929 * only at that moment.
1934 * Look for the application in the local wait queue and set the
1935 * notify socket if found.
1937 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1938 &wait_queue
.head
, head
) {
1939 health_code_update();
1940 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1941 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1942 cds_list_del(&wait_node
->head
);
1944 app
= wait_node
->app
;
1946 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1952 * With no application at this stage the received socket is
1953 * basically useless so close it before we free the cmd data
1954 * structure for good.
1957 ret
= close(ust_cmd
->sock
);
1959 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1961 lttng_fd_put(LTTNG_FD_APPS
, 1);
1968 * @session_lock_list
1970 * Lock the global session list so from the register up to the
1971 * registration done message, no thread can see the application
1972 * and change its state.
1974 session_lock_list();
1978 * Add application to the global hash table. This needs to be
1979 * done before the update to the UST registry can locate the
1984 /* Set app version. This call will print an error if needed. */
1985 (void) ust_app_version(app
);
1987 /* Send notify socket through the notify pipe. */
1988 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1992 session_unlock_list();
1994 * No notify thread, stop the UST tracing. However, this is
1995 * not an internal error of the this thread thus setting
1996 * the health error code to a normal exit.
2003 * Update newly registered application with the tracing
2004 * registry info already enabled information.
2006 update_ust_app(app
->sock
);
2009 * Don't care about return value. Let the manage apps threads
2010 * handle app unregistration upon socket close.
2012 (void) ust_app_register_done(app
);
2015 * Even if the application socket has been closed, send the app
2016 * to the thread and unregistration will take place at that
2019 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2022 session_unlock_list();
2024 * No apps. thread, stop the UST tracing. However, this is
2025 * not an internal error of the this thread thus setting
2026 * the health error code to a normal exit.
2033 session_unlock_list();
2035 } while (node
!= NULL
);
2037 health_poll_entry();
2038 /* Futex wait on queue. Blocking call on futex() */
2039 futex_nto1_wait(&ust_cmd_queue
.futex
);
2042 /* Normal exit, no error */
2046 /* Clean up wait queue. */
2047 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2048 &wait_queue
.head
, head
) {
2049 cds_list_del(&wait_node
->head
);
2054 /* Empty command queue. */
2056 /* Dequeue command for registration */
2057 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2061 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2062 ret
= close(ust_cmd
->sock
);
2064 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2066 lttng_fd_put(LTTNG_FD_APPS
, 1);
2071 DBG("Dispatch thread dying");
2074 ERR("Health error occurred in %s", __func__
);
2076 health_unregister(health_sessiond
);
2077 rcu_unregister_thread();
2082 * This thread manage application registration.
2084 static void *thread_registration_apps(void *data
)
2086 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2087 uint32_t revents
, nb_fd
;
2088 struct lttng_poll_event events
;
2090 * Get allocated in this thread, enqueued to a global queue, dequeued and
2091 * freed in the manage apps thread.
2093 struct ust_command
*ust_cmd
= NULL
;
2095 DBG("[thread] Manage application registration started");
2097 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2099 if (testpoint(sessiond_thread_registration_apps
)) {
2100 goto error_testpoint
;
2103 ret
= lttcomm_listen_unix_sock(apps_sock
);
2109 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2110 * more will be added to this poll set.
2112 ret
= sessiond_set_thread_pollset(&events
, 2);
2114 goto error_create_poll
;
2117 /* Add the application registration socket */
2118 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2120 goto error_poll_add
;
2123 /* Notify all applications to register */
2124 ret
= notify_ust_apps(1);
2126 ERR("Failed to notify applications or create the wait shared memory.\n"
2127 "Execution continues but there might be problem for already\n"
2128 "running applications that wishes to register.");
2132 DBG("Accepting application registration");
2134 /* Inifinite blocking call, waiting for transmission */
2136 health_poll_entry();
2137 ret
= lttng_poll_wait(&events
, -1);
2141 * Restart interrupted system call.
2143 if (errno
== EINTR
) {
2151 for (i
= 0; i
< nb_fd
; i
++) {
2152 health_code_update();
2154 /* Fetch once the poll data */
2155 revents
= LTTNG_POLL_GETEV(&events
, i
);
2156 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2159 /* No activity for this FD (poll implementation). */
2163 /* Thread quit pipe has been closed. Killing thread. */
2164 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2170 /* Event on the registration socket */
2171 if (pollfd
== apps_sock
) {
2172 if (revents
& LPOLLIN
) {
2173 sock
= lttcomm_accept_unix_sock(apps_sock
);
2179 * Set socket timeout for both receiving and ending.
2180 * app_socket_timeout is in seconds, whereas
2181 * lttcomm_setsockopt_rcv_timeout and
2182 * lttcomm_setsockopt_snd_timeout expect msec as
2185 if (config
.app_socket_timeout
>= 0) {
2186 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2187 config
.app_socket_timeout
* 1000);
2188 (void) lttcomm_setsockopt_snd_timeout(sock
,
2189 config
.app_socket_timeout
* 1000);
2193 * Set the CLOEXEC flag. Return code is useless because
2194 * either way, the show must go on.
2196 (void) utils_set_fd_cloexec(sock
);
2198 /* Create UST registration command for enqueuing */
2199 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2200 if (ust_cmd
== NULL
) {
2201 PERROR("ust command zmalloc");
2210 * Using message-based transmissions to ensure we don't
2211 * have to deal with partially received messages.
2213 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2215 ERR("Exhausted file descriptors allowed for applications.");
2225 health_code_update();
2226 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2229 /* Close socket of the application. */
2234 lttng_fd_put(LTTNG_FD_APPS
, 1);
2238 health_code_update();
2240 ust_cmd
->sock
= sock
;
2243 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2244 " gid:%d sock:%d name:%s (version %d.%d)",
2245 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2246 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2247 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2248 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2251 * Lock free enqueue the registration request. The red pill
2252 * has been taken! This apps will be part of the *system*.
2254 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2257 * Wake the registration queue futex. Implicit memory
2258 * barrier with the exchange in cds_wfcq_enqueue.
2260 futex_nto1_wake(&ust_cmd_queue
.futex
);
2261 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2262 ERR("Register apps socket poll error");
2265 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2274 /* Notify that the registration thread is gone */
2277 if (apps_sock
>= 0) {
2278 ret
= close(apps_sock
);
2288 lttng_fd_put(LTTNG_FD_APPS
, 1);
2290 unlink(config
.apps_unix_sock_path
.value
);
2293 lttng_poll_clean(&events
);
2297 DBG("UST Registration thread cleanup complete");
2300 ERR("Health error occurred in %s", __func__
);
2302 health_unregister(health_sessiond
);
2308 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2309 * exec or it will fails.
2311 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2314 struct timespec timeout
;
2317 * Make sure we set the readiness flag to 0 because we are NOT ready.
2318 * This access to consumer_thread_is_ready does not need to be
2319 * protected by consumer_data.cond_mutex (yet) since the consumer
2320 * management thread has not been started at this point.
2322 consumer_data
->consumer_thread_is_ready
= 0;
2324 /* Setup pthread condition */
2325 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2328 PERROR("pthread_condattr_init consumer data");
2333 * Set the monotonic clock in order to make sure we DO NOT jump in time
2334 * between the clock_gettime() call and the timedwait call. See bug #324
2335 * for a more details and how we noticed it.
2337 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2340 PERROR("pthread_condattr_setclock consumer data");
2344 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2347 PERROR("pthread_cond_init consumer data");
2351 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2352 thread_manage_consumer
, consumer_data
);
2355 PERROR("pthread_create consumer");
2360 /* We are about to wait on a pthread condition */
2361 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2363 /* Get time for sem_timedwait absolute timeout */
2364 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2366 * Set the timeout for the condition timed wait even if the clock gettime
2367 * call fails since we might loop on that call and we want to avoid to
2368 * increment the timeout too many times.
2370 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2373 * The following loop COULD be skipped in some conditions so this is why we
2374 * set ret to 0 in order to make sure at least one round of the loop is
2380 * Loop until the condition is reached or when a timeout is reached. Note
2381 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2382 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2383 * possible. This loop does not take any chances and works with both of
2386 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2387 if (clock_ret
< 0) {
2388 PERROR("clock_gettime spawn consumer");
2389 /* Infinite wait for the consumerd thread to be ready */
2390 ret
= pthread_cond_wait(&consumer_data
->cond
,
2391 &consumer_data
->cond_mutex
);
2393 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2394 &consumer_data
->cond_mutex
, &timeout
);
2398 /* Release the pthread condition */
2399 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2403 if (ret
== ETIMEDOUT
) {
2407 * Call has timed out so we kill the kconsumerd_thread and return
2410 ERR("Condition timed out. The consumer thread was never ready."
2412 pth_ret
= pthread_cancel(consumer_data
->thread
);
2414 PERROR("pthread_cancel consumer thread");
2417 PERROR("pthread_cond_wait failed consumer thread");
2419 /* Caller is expecting a negative value on failure. */
2424 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2425 if (consumer_data
->pid
== 0) {
2426 ERR("Consumerd did not start");
2427 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2430 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2439 * Join consumer thread
2441 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2445 /* Consumer pid must be a real one. */
2446 if (consumer_data
->pid
> 0) {
2448 ret
= kill(consumer_data
->pid
, SIGTERM
);
2450 PERROR("Error killing consumer daemon");
2453 return pthread_join(consumer_data
->thread
, &status
);
2460 * Fork and exec a consumer daemon (consumerd).
2462 * Return pid if successful else -1.
2464 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2468 const char *consumer_to_use
;
2469 const char *verbosity
;
2472 DBG("Spawning consumerd");
2479 if (config
.verbose_consumer
) {
2480 verbosity
= "--verbose";
2481 } else if (lttng_opt_quiet
) {
2482 verbosity
= "--quiet";
2487 switch (consumer_data
->type
) {
2488 case LTTNG_CONSUMER_KERNEL
:
2490 * Find out which consumerd to execute. We will first try the
2491 * 64-bit path, then the sessiond's installation directory, and
2492 * fallback on the 32-bit one,
2494 DBG3("Looking for a kernel consumer at these locations:");
2495 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2496 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2497 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2498 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2499 DBG3("Found location #1");
2500 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2501 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2502 DBG3("Found location #2");
2503 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2504 } else if (stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2505 DBG3("Found location #3");
2506 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2508 DBG("Could not find any valid consumerd executable");
2512 DBG("Using kernel consumer at: %s", consumer_to_use
);
2513 (void) execl(consumer_to_use
,
2514 "lttng-consumerd", verbosity
, "-k",
2515 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2516 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2517 "--group", config
.tracing_group_name
.value
,
2520 case LTTNG_CONSUMER64_UST
:
2522 if (config
.consumerd64_lib_dir
.value
) {
2527 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2531 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2532 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2537 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2538 if (tmp
[0] != '\0') {
2539 strcat(tmpnew
, ":");
2540 strcat(tmpnew
, tmp
);
2542 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2549 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2550 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2551 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2552 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2553 "--group", config
.tracing_group_name
.value
,
2557 case LTTNG_CONSUMER32_UST
:
2559 if (config
.consumerd32_lib_dir
.value
) {
2564 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2568 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2569 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2574 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2575 if (tmp
[0] != '\0') {
2576 strcat(tmpnew
, ":");
2577 strcat(tmpnew
, tmp
);
2579 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2586 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2587 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2588 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2589 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2590 "--group", config
.tracing_group_name
.value
,
2595 ERR("unknown consumer type");
2599 PERROR("Consumer execl()");
2601 /* Reaching this point, we got a failure on our execl(). */
2603 } else if (pid
> 0) {
2606 PERROR("start consumer fork");
2614 * Spawn the consumerd daemon and session daemon thread.
2616 static int start_consumerd(struct consumer_data
*consumer_data
)
2621 * Set the listen() state on the socket since there is a possible race
2622 * between the exec() of the consumer daemon and this call if place in the
2623 * consumer thread. See bug #366 for more details.
2625 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2630 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2631 if (consumer_data
->pid
!= 0) {
2632 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2636 ret
= spawn_consumerd(consumer_data
);
2638 ERR("Spawning consumerd failed");
2639 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2643 /* Setting up the consumer_data pid */
2644 consumer_data
->pid
= ret
;
2645 DBG2("Consumer pid %d", consumer_data
->pid
);
2646 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2648 DBG2("Spawning consumer control thread");
2649 ret
= spawn_consumer_thread(consumer_data
);
2651 ERR("Fatal error spawning consumer control thread");
2659 /* Cleanup already created sockets on error. */
2660 if (consumer_data
->err_sock
>= 0) {
2663 err
= close(consumer_data
->err_sock
);
2665 PERROR("close consumer data error socket");
2672 * Setup necessary data for kernel tracer action.
2674 static int init_kernel_tracer(void)
2678 /* Modprobe lttng kernel modules */
2679 ret
= modprobe_lttng_control();
2684 /* Open debugfs lttng */
2685 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2686 if (kernel_tracer_fd
< 0) {
2687 DBG("Failed to open %s", module_proc_lttng
);
2691 /* Validate kernel version */
2692 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2693 &kernel_tracer_abi_version
);
2698 ret
= modprobe_lttng_data();
2703 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2710 WARN("Kernel tracer does not support buffer monitoring. "
2711 "The monitoring timer of channels in the kernel domain "
2712 "will be set to 0 (disabled).");
2715 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2719 modprobe_remove_lttng_control();
2720 ret
= close(kernel_tracer_fd
);
2724 kernel_tracer_fd
= -1;
2725 return LTTNG_ERR_KERN_VERSION
;
2728 ret
= close(kernel_tracer_fd
);
2734 modprobe_remove_lttng_control();
2737 WARN("No kernel tracer available");
2738 kernel_tracer_fd
= -1;
2740 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2742 return LTTNG_ERR_KERN_NA
;
2748 * Copy consumer output from the tracing session to the domain session. The
2749 * function also applies the right modification on a per domain basis for the
2750 * trace files destination directory.
2752 * Should *NOT* be called with RCU read-side lock held.
2754 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2757 const char *dir_name
;
2758 struct consumer_output
*consumer
;
2761 assert(session
->consumer
);
2764 case LTTNG_DOMAIN_KERNEL
:
2765 DBG3("Copying tracing session consumer output in kernel session");
2767 * XXX: We should audit the session creation and what this function
2768 * does "extra" in order to avoid a destroy since this function is used
2769 * in the domain session creation (kernel and ust) only. Same for UST
2772 if (session
->kernel_session
->consumer
) {
2773 consumer_output_put(session
->kernel_session
->consumer
);
2775 session
->kernel_session
->consumer
=
2776 consumer_copy_output(session
->consumer
);
2777 /* Ease our life a bit for the next part */
2778 consumer
= session
->kernel_session
->consumer
;
2779 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2781 case LTTNG_DOMAIN_JUL
:
2782 case LTTNG_DOMAIN_LOG4J
:
2783 case LTTNG_DOMAIN_PYTHON
:
2784 case LTTNG_DOMAIN_UST
:
2785 DBG3("Copying tracing session consumer output in UST session");
2786 if (session
->ust_session
->consumer
) {
2787 consumer_output_put(session
->ust_session
->consumer
);
2789 session
->ust_session
->consumer
=
2790 consumer_copy_output(session
->consumer
);
2791 /* Ease our life a bit for the next part */
2792 consumer
= session
->ust_session
->consumer
;
2793 dir_name
= DEFAULT_UST_TRACE_DIR
;
2796 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2800 /* Append correct directory to subdir */
2801 strncat(consumer
->subdir
, dir_name
,
2802 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2803 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2812 * Create an UST session and add it to the session ust list.
2814 * Should *NOT* be called with RCU read-side lock held.
2816 static int create_ust_session(struct ltt_session
*session
,
2817 struct lttng_domain
*domain
)
2820 struct ltt_ust_session
*lus
= NULL
;
2824 assert(session
->consumer
);
2826 switch (domain
->type
) {
2827 case LTTNG_DOMAIN_JUL
:
2828 case LTTNG_DOMAIN_LOG4J
:
2829 case LTTNG_DOMAIN_PYTHON
:
2830 case LTTNG_DOMAIN_UST
:
2833 ERR("Unknown UST domain on create session %d", domain
->type
);
2834 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2838 DBG("Creating UST session");
2840 lus
= trace_ust_create_session(session
->id
);
2842 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2846 lus
->uid
= session
->uid
;
2847 lus
->gid
= session
->gid
;
2848 lus
->output_traces
= session
->output_traces
;
2849 lus
->snapshot_mode
= session
->snapshot_mode
;
2850 lus
->live_timer_interval
= session
->live_timer
;
2851 session
->ust_session
= lus
;
2852 if (session
->shm_path
[0]) {
2853 strncpy(lus
->root_shm_path
, session
->shm_path
,
2854 sizeof(lus
->root_shm_path
));
2855 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2856 strncpy(lus
->shm_path
, session
->shm_path
,
2857 sizeof(lus
->shm_path
));
2858 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2859 strncat(lus
->shm_path
, "/ust",
2860 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2862 /* Copy session output to the newly created UST session */
2863 ret
= copy_session_consumer(domain
->type
, session
);
2864 if (ret
!= LTTNG_OK
) {
2872 session
->ust_session
= NULL
;
2877 * Create a kernel tracer session then create the default channel.
2879 static int create_kernel_session(struct ltt_session
*session
)
2883 DBG("Creating kernel session");
2885 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2887 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2891 /* Code flow safety */
2892 assert(session
->kernel_session
);
2894 /* Copy session output to the newly created Kernel session */
2895 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2896 if (ret
!= LTTNG_OK
) {
2900 session
->kernel_session
->uid
= session
->uid
;
2901 session
->kernel_session
->gid
= session
->gid
;
2902 session
->kernel_session
->output_traces
= session
->output_traces
;
2903 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2908 trace_kernel_destroy_session(session
->kernel_session
);
2909 session
->kernel_session
= NULL
;
2914 * Count number of session permitted by uid/gid.
2916 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2919 struct ltt_session
*session
;
2921 DBG("Counting number of available session for UID %d GID %d",
2923 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2925 * Only list the sessions the user can control.
2927 if (!session_access_ok(session
, uid
, gid
)) {
2935 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2936 int *sock_error
, struct lttng_event
*event
)
2939 struct lttng_userspace_probe_location
*probe_location
;
2940 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2941 struct lttng_dynamic_buffer probe_location_buffer
;
2942 struct lttng_buffer_view buffer_view
;
2945 * Create a buffer to store the serialized version of the probe
2948 lttng_dynamic_buffer_init(&probe_location_buffer
);
2949 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2950 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2952 ret
= LTTNG_ERR_NOMEM
;
2957 * Receive the probe location.
2959 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2960 probe_location_buffer
.size
);
2962 DBG("Nothing recv() from client var len data... continuing");
2964 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2965 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2969 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2970 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2973 * Extract the probe location from the serialized version.
2975 ret
= lttng_userspace_probe_location_create_from_buffer(
2976 &buffer_view
, &probe_location
);
2978 WARN("Failed to create a userspace probe location from the received buffer");
2979 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2980 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2985 * Receive the file descriptor to the target binary from the client.
2987 DBG("Receiving userspace probe target FD from client ...");
2988 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2990 DBG("Nothing recv() from client userspace probe fd... continuing");
2992 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2997 * Set the file descriptor received from the client through the unix
2998 * socket in the probe location.
3000 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
3002 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
3007 * From the kernel tracer's perspective, all userspace probe event types
3008 * are all the same: a file and an offset.
3010 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
3011 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
3012 ret
= lttng_userspace_probe_location_function_set_binary_fd(
3013 probe_location
, fd
);
3015 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
3016 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
3017 probe_location
, fd
);
3020 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
3025 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
3029 /* Attach the probe location to the event. */
3030 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
3032 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
3036 lttng_dynamic_buffer_reset(&probe_location_buffer
);
3042 * Check if the current kernel tracer supports the session rotation feature.
3043 * Return 1 if it does, 0 otherwise.
3045 static int check_rotate_compatible(void)
3049 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
3050 DBG("Kernel tracer version is not compatible with the rotation feature");
3058 * Process the command requested by the lttng client within the command
3059 * context structure. This function make sure that the return structure (llm)
3060 * is set and ready for transmission before returning.
3062 * Return any error encountered or 0 for success.
3064 * "sock" is only used for special-case var. len data.
3066 * Should *NOT* be called with RCU read-side lock held.
3068 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
3072 int need_tracing_session
= 1;
3075 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
3077 assert(!rcu_read_ongoing());
3081 switch (cmd_ctx
->lsm
->cmd_type
) {
3082 case LTTNG_CREATE_SESSION
:
3083 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3084 case LTTNG_CREATE_SESSION_LIVE
:
3085 case LTTNG_DESTROY_SESSION
:
3086 case LTTNG_LIST_SESSIONS
:
3087 case LTTNG_LIST_DOMAINS
:
3088 case LTTNG_START_TRACE
:
3089 case LTTNG_STOP_TRACE
:
3090 case LTTNG_DATA_PENDING
:
3091 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3092 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3093 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3094 case LTTNG_SNAPSHOT_RECORD
:
3095 case LTTNG_SAVE_SESSION
:
3096 case LTTNG_SET_SESSION_SHM_PATH
:
3097 case LTTNG_REGENERATE_METADATA
:
3098 case LTTNG_REGENERATE_STATEDUMP
:
3099 case LTTNG_REGISTER_TRIGGER
:
3100 case LTTNG_UNREGISTER_TRIGGER
:
3101 case LTTNG_ROTATE_SESSION
:
3102 case LTTNG_ROTATION_GET_INFO
:
3103 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
3104 case LTTNG_ROTATION_SET_SCHEDULE
:
3105 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
3112 if (config
.no_kernel
&& need_domain
3113 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3115 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3117 ret
= LTTNG_ERR_KERN_NA
;
3122 /* Deny register consumer if we already have a spawned consumer. */
3123 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3124 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3125 if (kconsumer_data
.pid
> 0) {
3126 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3127 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3130 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3134 * Check for command that don't needs to allocate a returned payload. We do
3135 * this here so we don't have to make the call for no payload at each
3138 switch(cmd_ctx
->lsm
->cmd_type
) {
3139 case LTTNG_LIST_SESSIONS
:
3140 case LTTNG_LIST_TRACEPOINTS
:
3141 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3142 case LTTNG_LIST_DOMAINS
:
3143 case LTTNG_LIST_CHANNELS
:
3144 case LTTNG_LIST_EVENTS
:
3145 case LTTNG_LIST_SYSCALLS
:
3146 case LTTNG_LIST_TRACKER_PIDS
:
3147 case LTTNG_DATA_PENDING
:
3148 case LTTNG_ROTATE_SESSION
:
3149 case LTTNG_ROTATION_GET_INFO
:
3150 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
3153 /* Setup lttng message with no payload */
3154 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3156 /* This label does not try to unlock the session */
3157 goto init_setup_error
;
3161 /* Commands that DO NOT need a session. */
3162 switch (cmd_ctx
->lsm
->cmd_type
) {
3163 case LTTNG_CREATE_SESSION
:
3164 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3165 case LTTNG_CREATE_SESSION_LIVE
:
3166 case LTTNG_LIST_SESSIONS
:
3167 case LTTNG_LIST_TRACEPOINTS
:
3168 case LTTNG_LIST_SYSCALLS
:
3169 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3170 case LTTNG_SAVE_SESSION
:
3171 case LTTNG_REGISTER_TRIGGER
:
3172 case LTTNG_UNREGISTER_TRIGGER
:
3173 need_tracing_session
= 0;
3176 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3178 * We keep the session list lock across _all_ commands
3179 * for now, because the per-session lock does not
3180 * handle teardown properly.
3182 session_lock_list();
3183 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3184 if (cmd_ctx
->session
== NULL
) {
3185 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3188 /* Acquire lock for the session */
3189 session_lock(cmd_ctx
->session
);
3195 * Commands that need a valid session but should NOT create one if none
3196 * exists. Instead of creating one and destroying it when the command is
3197 * handled, process that right before so we save some round trip in useless
3200 switch (cmd_ctx
->lsm
->cmd_type
) {
3201 case LTTNG_DISABLE_CHANNEL
:
3202 case LTTNG_DISABLE_EVENT
:
3203 switch (cmd_ctx
->lsm
->domain
.type
) {
3204 case LTTNG_DOMAIN_KERNEL
:
3205 if (!cmd_ctx
->session
->kernel_session
) {
3206 ret
= LTTNG_ERR_NO_CHANNEL
;
3210 case LTTNG_DOMAIN_JUL
:
3211 case LTTNG_DOMAIN_LOG4J
:
3212 case LTTNG_DOMAIN_PYTHON
:
3213 case LTTNG_DOMAIN_UST
:
3214 if (!cmd_ctx
->session
->ust_session
) {
3215 ret
= LTTNG_ERR_NO_CHANNEL
;
3220 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3232 * Check domain type for specific "pre-action".
3234 switch (cmd_ctx
->lsm
->domain
.type
) {
3235 case LTTNG_DOMAIN_KERNEL
:
3237 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3241 /* Kernel tracer check */
3242 if (kernel_tracer_fd
== -1) {
3243 /* Basically, load kernel tracer modules */
3244 ret
= init_kernel_tracer();
3250 /* Consumer is in an ERROR state. Report back to client */
3251 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3252 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3256 /* Need a session for kernel command */
3257 if (need_tracing_session
) {
3258 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3259 ret
= create_kernel_session(cmd_ctx
->session
);
3261 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3266 /* Start the kernel consumer daemon */
3267 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3268 if (kconsumer_data
.pid
== 0 &&
3269 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3270 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3271 ret
= start_consumerd(&kconsumer_data
);
3273 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3276 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3278 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3282 * The consumer was just spawned so we need to add the socket to
3283 * the consumer output of the session if exist.
3285 ret
= consumer_create_socket(&kconsumer_data
,
3286 cmd_ctx
->session
->kernel_session
->consumer
);
3293 case LTTNG_DOMAIN_JUL
:
3294 case LTTNG_DOMAIN_LOG4J
:
3295 case LTTNG_DOMAIN_PYTHON
:
3296 case LTTNG_DOMAIN_UST
:
3298 if (!ust_app_supported()) {
3299 ret
= LTTNG_ERR_NO_UST
;
3302 /* Consumer is in an ERROR state. Report back to client */
3303 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3304 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3308 if (need_tracing_session
) {
3309 /* Create UST session if none exist. */
3310 if (cmd_ctx
->session
->ust_session
== NULL
) {
3311 ret
= create_ust_session(cmd_ctx
->session
,
3312 &cmd_ctx
->lsm
->domain
);
3313 if (ret
!= LTTNG_OK
) {
3318 /* Start the UST consumer daemons */
3320 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3321 if (config
.consumerd64_bin_path
.value
&&
3322 ustconsumer64_data
.pid
== 0 &&
3323 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3324 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3325 ret
= start_consumerd(&ustconsumer64_data
);
3327 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3328 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3332 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3333 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3335 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3339 * Setup socket for consumer 64 bit. No need for atomic access
3340 * since it was set above and can ONLY be set in this thread.
3342 ret
= consumer_create_socket(&ustconsumer64_data
,
3343 cmd_ctx
->session
->ust_session
->consumer
);
3349 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3350 if (config
.consumerd32_bin_path
.value
&&
3351 ustconsumer32_data
.pid
== 0 &&
3352 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3353 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3354 ret
= start_consumerd(&ustconsumer32_data
);
3356 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3357 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3361 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3362 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3364 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3368 * Setup socket for consumer 64 bit. No need for atomic access
3369 * since it was set above and can ONLY be set in this thread.
3371 ret
= consumer_create_socket(&ustconsumer32_data
,
3372 cmd_ctx
->session
->ust_session
->consumer
);
3384 /* Validate consumer daemon state when start/stop trace command */
3385 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3386 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3387 switch (cmd_ctx
->lsm
->domain
.type
) {
3388 case LTTNG_DOMAIN_NONE
:
3390 case LTTNG_DOMAIN_JUL
:
3391 case LTTNG_DOMAIN_LOG4J
:
3392 case LTTNG_DOMAIN_PYTHON
:
3393 case LTTNG_DOMAIN_UST
:
3394 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3395 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3399 case LTTNG_DOMAIN_KERNEL
:
3400 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3401 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3406 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3412 * Check that the UID or GID match that of the tracing session.
3413 * The root user can interact with all sessions.
3415 if (need_tracing_session
) {
3416 if (!session_access_ok(cmd_ctx
->session
,
3417 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3418 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3419 ret
= LTTNG_ERR_EPERM
;
3425 * Send relayd information to consumer as soon as we have a domain and a
3428 if (cmd_ctx
->session
&& need_domain
) {
3430 * Setup relayd if not done yet. If the relayd information was already
3431 * sent to the consumer, this call will gracefully return.
3433 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3434 if (ret
!= LTTNG_OK
) {
3439 /* Process by command type */
3440 switch (cmd_ctx
->lsm
->cmd_type
) {
3441 case LTTNG_ADD_CONTEXT
:
3444 * An LTTNG_ADD_CONTEXT command might have a supplementary
3445 * payload if the context being added is an application context.
3447 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3448 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3449 char *provider_name
= NULL
, *context_name
= NULL
;
3450 size_t provider_name_len
=
3451 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3452 size_t context_name_len
=
3453 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3455 if (provider_name_len
== 0 || context_name_len
== 0) {
3457 * Application provider and context names MUST
3460 ret
= -LTTNG_ERR_INVALID
;
3464 provider_name
= zmalloc(provider_name_len
+ 1);
3465 if (!provider_name
) {
3466 ret
= -LTTNG_ERR_NOMEM
;
3469 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3472 context_name
= zmalloc(context_name_len
+ 1);
3473 if (!context_name
) {
3474 ret
= -LTTNG_ERR_NOMEM
;
3475 goto error_add_context
;
3477 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3480 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3483 goto error_add_context
;
3486 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3489 goto error_add_context
;
3494 * cmd_add_context assumes ownership of the provider and context
3497 ret
= cmd_add_context(cmd_ctx
->session
,
3498 cmd_ctx
->lsm
->domain
.type
,
3499 cmd_ctx
->lsm
->u
.context
.channel_name
,
3500 &cmd_ctx
->lsm
->u
.context
.ctx
,
3501 kernel_poll_pipe
[1]);
3503 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3504 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3506 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3507 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3513 case LTTNG_DISABLE_CHANNEL
:
3515 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3516 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3519 case LTTNG_DISABLE_EVENT
:
3523 * FIXME: handle filter; for now we just receive the filter's
3524 * bytecode along with the filter expression which are sent by
3525 * liblttng-ctl and discard them.
3527 * This fixes an issue where the client may block while sending
3528 * the filter payload and encounter an error because the session
3529 * daemon closes the socket without ever handling this data.
3531 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3532 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3535 char data
[LTTNG_FILTER_MAX_LEN
];
3537 DBG("Discarding disable event command payload of size %zu", count
);
3539 ret
= lttcomm_recv_unix_sock(sock
, data
,
3540 count
> sizeof(data
) ? sizeof(data
) : count
);
3545 count
-= (size_t) ret
;
3548 /* FIXME: passing packed structure to non-packed pointer */
3549 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3550 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3551 &cmd_ctx
->lsm
->u
.disable
.event
);
3554 case LTTNG_ENABLE_CHANNEL
:
3556 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3557 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3558 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3559 &cmd_ctx
->lsm
->u
.channel
.chan
,
3560 kernel_poll_pipe
[1]);
3563 case LTTNG_TRACK_PID
:
3565 ret
= cmd_track_pid(cmd_ctx
->session
,
3566 cmd_ctx
->lsm
->domain
.type
,
3567 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3570 case LTTNG_UNTRACK_PID
:
3572 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3573 cmd_ctx
->lsm
->domain
.type
,
3574 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3577 case LTTNG_ENABLE_EVENT
:
3579 struct lttng_event
*ev
= NULL
;
3580 struct lttng_event_exclusion
*exclusion
= NULL
;
3581 struct lttng_filter_bytecode
*bytecode
= NULL
;
3582 char *filter_expression
= NULL
;
3584 /* Handle exclusion events and receive it from the client. */
3585 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3586 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3588 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3589 (count
* LTTNG_SYMBOL_NAME_LEN
));
3591 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3595 DBG("Receiving var len exclusion event list from client ...");
3596 exclusion
->count
= count
;
3597 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3598 count
* LTTNG_SYMBOL_NAME_LEN
);
3600 DBG("Nothing recv() from client var len data... continuing");
3603 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3608 /* Get filter expression from client. */
3609 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3610 size_t expression_len
=
3611 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3613 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3614 ret
= LTTNG_ERR_FILTER_INVAL
;
3619 filter_expression
= zmalloc(expression_len
);
3620 if (!filter_expression
) {
3622 ret
= LTTNG_ERR_FILTER_NOMEM
;
3626 /* Receive var. len. data */
3627 DBG("Receiving var len filter's expression from client ...");
3628 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3631 DBG("Nothing recv() from client var len data... continuing");
3633 free(filter_expression
);
3635 ret
= LTTNG_ERR_FILTER_INVAL
;
3640 /* Handle filter and get bytecode from client. */
3641 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3642 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3644 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3645 ret
= LTTNG_ERR_FILTER_INVAL
;
3646 free(filter_expression
);
3651 bytecode
= zmalloc(bytecode_len
);
3653 free(filter_expression
);
3655 ret
= LTTNG_ERR_FILTER_NOMEM
;
3659 /* Receive var. len. data */
3660 DBG("Receiving var len filter's bytecode from client ...");
3661 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3663 DBG("Nothing recv() from client var len data... continuing");
3665 free(filter_expression
);
3668 ret
= LTTNG_ERR_FILTER_INVAL
;
3672 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3673 free(filter_expression
);
3676 ret
= LTTNG_ERR_FILTER_INVAL
;
3681 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
3683 DBG("Failed to copy event: %s",
3684 cmd_ctx
->lsm
->u
.enable
.event
.name
);
3685 ret
= LTTNG_ERR_NOMEM
;
3690 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
3691 /* Expect a userspace probe description. */
3692 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
3694 lttng_event_destroy(ev
);
3699 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3700 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3702 filter_expression
, bytecode
, exclusion
,
3703 kernel_poll_pipe
[1]);
3704 lttng_event_destroy(ev
);
3707 case LTTNG_LIST_TRACEPOINTS
:
3709 struct lttng_event
*events
;
3712 session_lock_list();
3713 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3714 session_unlock_list();
3715 if (nb_events
< 0) {
3716 /* Return value is a negative lttng_error_code. */
3722 * Setup lttng message with payload size set to the event list size in
3723 * bytes and then copy list into the llm payload.
3725 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3726 sizeof(struct lttng_event
) * nb_events
);
3736 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3738 struct lttng_event_field
*fields
;
3741 session_lock_list();
3742 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3744 session_unlock_list();
3745 if (nb_fields
< 0) {
3746 /* Return value is a negative lttng_error_code. */
3752 * Setup lttng message with payload size set to the event list size in
3753 * bytes and then copy list into the llm payload.
3755 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3756 sizeof(struct lttng_event_field
) * nb_fields
);
3766 case LTTNG_LIST_SYSCALLS
:
3768 struct lttng_event
*events
;
3771 nb_events
= cmd_list_syscalls(&events
);
3772 if (nb_events
< 0) {
3773 /* Return value is a negative lttng_error_code. */
3779 * Setup lttng message with payload size set to the event list size in
3780 * bytes and then copy list into the llm payload.
3782 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3783 sizeof(struct lttng_event
) * nb_events
);
3793 case LTTNG_LIST_TRACKER_PIDS
:
3795 int32_t *pids
= NULL
;
3798 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3799 cmd_ctx
->lsm
->domain
.type
, &pids
);
3801 /* Return value is a negative lttng_error_code. */
3807 * Setup lttng message with payload size set to the event list size in
3808 * bytes and then copy list into the llm payload.
3810 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3811 sizeof(int32_t) * nr_pids
);
3821 case LTTNG_SET_CONSUMER_URI
:
3824 struct lttng_uri
*uris
;
3826 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3827 len
= nb_uri
* sizeof(struct lttng_uri
);
3830 ret
= LTTNG_ERR_INVALID
;
3834 uris
= zmalloc(len
);
3836 ret
= LTTNG_ERR_FATAL
;
3840 /* Receive variable len data */
3841 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3842 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3844 DBG("No URIs received from client... continuing");
3846 ret
= LTTNG_ERR_SESSION_FAIL
;
3851 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3853 if (ret
!= LTTNG_OK
) {
3860 case LTTNG_START_TRACE
:
3863 * On the first start, if we have a kernel session and we have
3864 * enabled time or size-based rotations, we have to make sure
3865 * the kernel tracer supports it.
3867 if (!cmd_ctx
->session
->has_been_started
&& \
3868 cmd_ctx
->session
->kernel_session
&& \
3869 (cmd_ctx
->session
->rotate_timer_period
|| \
3870 cmd_ctx
->session
->rotate_size
) && \
3871 !check_rotate_compatible()) {
3872 DBG("Kernel tracer version is not compatible with the rotation feature");
3873 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3876 ret
= cmd_start_trace(cmd_ctx
->session
);
3879 case LTTNG_STOP_TRACE
:
3881 ret
= cmd_stop_trace(cmd_ctx
->session
);
3884 case LTTNG_CREATE_SESSION
:
3887 struct lttng_uri
*uris
= NULL
;
3889 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3890 len
= nb_uri
* sizeof(struct lttng_uri
);
3893 uris
= zmalloc(len
);
3895 ret
= LTTNG_ERR_FATAL
;
3899 /* Receive variable len data */
3900 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3901 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3903 DBG("No URIs received from client... continuing");
3905 ret
= LTTNG_ERR_SESSION_FAIL
;
3910 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3911 DBG("Creating session with ONE network URI is a bad call");
3912 ret
= LTTNG_ERR_SESSION_FAIL
;
3918 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3919 &cmd_ctx
->creds
, 0);
3925 case LTTNG_DESTROY_SESSION
:
3927 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1],
3928 notification_thread_handle
);
3930 /* Set session to NULL so we do not unlock it after free. */
3931 cmd_ctx
->session
= NULL
;
3934 case LTTNG_LIST_DOMAINS
:
3937 struct lttng_domain
*domains
= NULL
;
3939 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3941 /* Return value is a negative lttng_error_code. */
3946 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3947 nb_dom
* sizeof(struct lttng_domain
));
3957 case LTTNG_LIST_CHANNELS
:
3959 ssize_t payload_size
;
3960 struct lttng_channel
*channels
= NULL
;
3962 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3963 cmd_ctx
->session
, &channels
);
3964 if (payload_size
< 0) {
3965 /* Return value is a negative lttng_error_code. */
3966 ret
= -payload_size
;
3970 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3981 case LTTNG_LIST_EVENTS
:
3984 struct lttng_event
*events
= NULL
;
3985 struct lttcomm_event_command_header cmd_header
;
3988 memset(&cmd_header
, 0, sizeof(cmd_header
));
3989 /* Extended infos are included at the end of events */
3990 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3991 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3992 &events
, &total_size
);
3995 /* Return value is a negative lttng_error_code. */
4000 cmd_header
.nb_events
= nb_event
;
4001 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
4002 &cmd_header
, sizeof(cmd_header
));
4012 case LTTNG_LIST_SESSIONS
:
4014 unsigned int nr_sessions
;
4015 void *sessions_payload
;
4018 session_lock_list();
4019 nr_sessions
= lttng_sessions_count(
4020 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4021 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4022 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
4023 sessions_payload
= zmalloc(payload_len
);
4025 if (!sessions_payload
) {
4026 session_unlock_list();
4031 cmd_list_lttng_sessions(sessions_payload
,
4032 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4033 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4034 session_unlock_list();
4036 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
4038 free(sessions_payload
);
4047 case LTTNG_REGISTER_CONSUMER
:
4049 struct consumer_data
*cdata
;
4051 switch (cmd_ctx
->lsm
->domain
.type
) {
4052 case LTTNG_DOMAIN_KERNEL
:
4053 cdata
= &kconsumer_data
;
4056 ret
= LTTNG_ERR_UND
;
4060 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4061 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
4064 case LTTNG_DATA_PENDING
:
4067 uint8_t pending_ret_byte
;
4069 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
4074 * This function may returns 0 or 1 to indicate whether or not
4075 * there is data pending. In case of error, it should return an
4076 * LTTNG_ERR code. However, some code paths may still return
4077 * a nondescript error code, which we handle by returning an
4080 if (pending_ret
== 0 || pending_ret
== 1) {
4082 * ret will be set to LTTNG_OK at the end of
4085 } else if (pending_ret
< 0) {
4086 ret
= LTTNG_ERR_UNK
;
4093 pending_ret_byte
= (uint8_t) pending_ret
;
4095 /* 1 byte to return whether or not data is pending */
4096 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
4097 &pending_ret_byte
, 1);
4106 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
4108 struct lttcomm_lttng_output_id reply
;
4110 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
4111 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
4112 if (ret
!= LTTNG_OK
) {
4116 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
4122 /* Copy output list into message payload */
4126 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
4128 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
4129 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
4132 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
4135 struct lttng_snapshot_output
*outputs
= NULL
;
4137 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
4138 if (nb_output
< 0) {
4143 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4144 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4145 nb_output
* sizeof(struct lttng_snapshot_output
));
4155 case LTTNG_SNAPSHOT_RECORD
:
4157 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4158 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4159 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4162 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4165 struct lttng_uri
*uris
= NULL
;
4167 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4168 len
= nb_uri
* sizeof(struct lttng_uri
);
4171 uris
= zmalloc(len
);
4173 ret
= LTTNG_ERR_FATAL
;
4177 /* Receive variable len data */
4178 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4179 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4181 DBG("No URIs received from client... continuing");
4183 ret
= LTTNG_ERR_SESSION_FAIL
;
4188 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4189 DBG("Creating session with ONE network URI is a bad call");
4190 ret
= LTTNG_ERR_SESSION_FAIL
;
4196 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4197 nb_uri
, &cmd_ctx
->creds
);
4201 case LTTNG_CREATE_SESSION_LIVE
:
4204 struct lttng_uri
*uris
= NULL
;
4206 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4207 len
= nb_uri
* sizeof(struct lttng_uri
);
4210 uris
= zmalloc(len
);
4212 ret
= LTTNG_ERR_FATAL
;
4216 /* Receive variable len data */
4217 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4218 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4220 DBG("No URIs received from client... continuing");
4222 ret
= LTTNG_ERR_SESSION_FAIL
;
4227 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4228 DBG("Creating session with ONE network URI is a bad call");
4229 ret
= LTTNG_ERR_SESSION_FAIL
;
4235 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4236 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4240 case LTTNG_SAVE_SESSION
:
4242 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4246 case LTTNG_SET_SESSION_SHM_PATH
:
4248 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4249 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4252 case LTTNG_REGENERATE_METADATA
:
4254 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4257 case LTTNG_REGENERATE_STATEDUMP
:
4259 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4262 case LTTNG_REGISTER_TRIGGER
:
4264 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4265 notification_thread_handle
);
4268 case LTTNG_UNREGISTER_TRIGGER
:
4270 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4271 notification_thread_handle
);
4274 case LTTNG_ROTATE_SESSION
:
4276 struct lttng_rotate_session_return rotate_return
;
4278 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4280 memset(&rotate_return
, 0, sizeof(rotate_return
));
4281 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4282 DBG("Kernel tracer version is not compatible with the rotation feature");
4283 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4287 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4293 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4294 sizeof(rotate_return
));
4303 case LTTNG_ROTATION_GET_INFO
:
4305 struct lttng_rotation_get_info_return get_info_return
;
4307 memset(&get_info_return
, 0, sizeof(get_info_return
));
4308 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4309 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4315 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4316 sizeof(get_info_return
));
4325 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
4327 struct lttng_session_get_current_output_return output_return
;
4329 memset(&output_return
, 0, sizeof(output_return
));
4330 ret
= cmd_session_get_current_output(cmd_ctx
->session
,
4337 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &output_return
,
4338 sizeof(output_return
));
4347 case LTTNG_ROTATION_SET_SCHEDULE
:
4350 enum lttng_rotation_schedule_type schedule_type
;
4353 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4354 DBG("Kernel tracer version does not support session rotations");
4355 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4359 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
4360 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
4361 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
4363 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4367 notification_thread_handle
);
4368 if (ret
!= LTTNG_OK
) {
4374 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
4376 struct lttng_session_list_schedules_return schedules
= {
4377 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
4378 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
4379 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
4380 .size
.value
= cmd_ctx
->session
->rotate_size
,
4383 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
4394 ret
= LTTNG_ERR_UND
;
4399 if (cmd_ctx
->llm
== NULL
) {
4400 DBG("Missing llm structure. Allocating one.");
4401 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4405 /* Set return code */
4406 cmd_ctx
->llm
->ret_code
= ret
;
4408 if (cmd_ctx
->session
) {
4409 session_unlock(cmd_ctx
->session
);
4411 if (need_tracing_session
) {
4412 session_unlock_list();
4415 assert(!rcu_read_ongoing());
4420 * Thread managing health check socket.
4422 static void *thread_manage_health(void *data
)
4424 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4425 uint32_t revents
, nb_fd
;
4426 struct lttng_poll_event events
;
4427 struct health_comm_msg msg
;
4428 struct health_comm_reply reply
;
4430 DBG("[thread] Manage health check started");
4432 rcu_register_thread();
4434 /* We might hit an error path before this is created. */
4435 lttng_poll_init(&events
);
4437 /* Create unix socket */
4438 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4440 ERR("Unable to create health check Unix socket");
4445 /* lttng health client socket path permissions */
4446 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4447 utils_get_group_id(config
.tracing_group_name
.value
));
4449 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4454 ret
= chmod(config
.health_unix_sock_path
.value
,
4455 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4457 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4464 * Set the CLOEXEC flag. Return code is useless because either way, the
4467 (void) utils_set_fd_cloexec(sock
);
4469 ret
= lttcomm_listen_unix_sock(sock
);
4475 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4476 * more will be added to this poll set.
4478 ret
= sessiond_set_thread_pollset(&events
, 2);
4483 /* Add the application registration socket */
4484 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4489 sessiond_notify_ready();
4492 DBG("Health check ready");
4494 /* Inifinite blocking call, waiting for transmission */
4496 ret
= lttng_poll_wait(&events
, -1);
4499 * Restart interrupted system call.
4501 if (errno
== EINTR
) {
4509 for (i
= 0; i
< nb_fd
; i
++) {
4510 /* Fetch once the poll data */
4511 revents
= LTTNG_POLL_GETEV(&events
, i
);
4512 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4515 /* No activity for this FD (poll implementation). */
4519 /* Thread quit pipe has been closed. Killing thread. */
4520 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4526 /* Event on the registration socket */
4527 if (pollfd
== sock
) {
4528 if (revents
& LPOLLIN
) {
4530 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4531 ERR("Health socket poll error");
4534 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4540 new_sock
= lttcomm_accept_unix_sock(sock
);
4546 * Set the CLOEXEC flag. Return code is useless because either way, the
4549 (void) utils_set_fd_cloexec(new_sock
);
4551 DBG("Receiving data from client for health...");
4552 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4554 DBG("Nothing recv() from client... continuing");
4555 ret
= close(new_sock
);
4562 rcu_thread_online();
4564 memset(&reply
, 0, sizeof(reply
));
4565 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4567 * health_check_state returns 0 if health is
4570 if (!health_check_state(health_sessiond
, i
)) {
4571 reply
.ret_code
|= 1ULL << i
;
4575 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4577 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4579 ERR("Failed to send health data back to client");
4582 /* End of transmission */
4583 ret
= close(new_sock
);
4592 ERR("Health error occurred in %s", __func__
);
4594 DBG("Health check thread dying");
4595 unlink(config
.health_unix_sock_path
.value
);
4603 lttng_poll_clean(&events
);
4605 rcu_unregister_thread();
4610 * This thread manage all clients request using the unix client socket for
4613 static void *thread_manage_clients(void *data
)
4615 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4617 uint32_t revents
, nb_fd
;
4618 struct command_ctx
*cmd_ctx
= NULL
;
4619 struct lttng_poll_event events
;
4621 DBG("[thread] Manage client started");
4623 rcu_register_thread();
4625 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4627 health_code_update();
4629 ret
= lttcomm_listen_unix_sock(client_sock
);
4635 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4636 * more will be added to this poll set.
4638 ret
= sessiond_set_thread_pollset(&events
, 2);
4640 goto error_create_poll
;
4643 /* Add the application registration socket */
4644 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4649 ret
= sem_post(&load_info
->message_thread_ready
);
4651 PERROR("sem_post message_thread_ready");
4656 * Wait until all support threads are initialized before accepting
4659 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4661 struct timeval timeout
;
4664 FD_SET(thread_quit_pipe
[0], &read_fds
);
4665 memset(&timeout
, 0, sizeof(timeout
));
4666 timeout
.tv_usec
= 1000;
4669 * If a support thread failed to launch, it may signal that
4670 * we must exit and the sessiond would never be marked as
4673 * The timeout is set to 1ms, which serves as a way to
4674 * pace down this check.
4676 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
4678 if (ret
> 0 || (ret
< 0 && errno
!= EINTR
)) {
4683 * This barrier is paired with the one in sessiond_notify_ready() to
4684 * ensure that loads accessing data initialized by the other threads,
4685 * on which this thread was waiting, are not performed before this point.
4687 * Note that this could be a 'read' memory barrier, but a full barrier
4688 * is used in case the code changes. The performance implications of
4689 * this choice are minimal since this is a slow path.
4693 /* This testpoint is after we signal readiness to the parent. */
4694 if (testpoint(sessiond_thread_manage_clients
)) {
4698 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4702 health_code_update();
4705 const struct cmd_completion_handler
*cmd_completion_handler
;
4707 DBG("Accepting client command ...");
4709 /* Inifinite blocking call, waiting for transmission */
4711 health_poll_entry();
4712 ret
= lttng_poll_wait(&events
, -1);
4716 * Restart interrupted system call.
4718 if (errno
== EINTR
) {
4726 for (i
= 0; i
< nb_fd
; i
++) {
4727 /* Fetch once the poll data */
4728 revents
= LTTNG_POLL_GETEV(&events
, i
);
4729 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4731 health_code_update();
4734 /* No activity for this FD (poll implementation). */
4738 /* Thread quit pipe has been closed. Killing thread. */
4739 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4745 /* Event on the registration socket */
4746 if (pollfd
== client_sock
) {
4747 if (revents
& LPOLLIN
) {
4749 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4750 ERR("Client socket poll error");
4753 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4759 DBG("Wait for client response");
4761 health_code_update();
4763 sock
= lttcomm_accept_unix_sock(client_sock
);
4769 * Set the CLOEXEC flag. Return code is useless because either way, the
4772 (void) utils_set_fd_cloexec(sock
);
4774 /* Set socket option for credentials retrieval */
4775 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4780 /* Allocate context command to process the client request */
4781 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4782 if (cmd_ctx
== NULL
) {
4783 PERROR("zmalloc cmd_ctx");
4787 /* Allocate data buffer for reception */
4788 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4789 if (cmd_ctx
->lsm
== NULL
) {
4790 PERROR("zmalloc cmd_ctx->lsm");
4794 cmd_ctx
->llm
= NULL
;
4795 cmd_ctx
->session
= NULL
;
4797 health_code_update();
4800 * Data is received from the lttng client. The struct
4801 * lttcomm_session_msg (lsm) contains the command and data request of
4804 DBG("Receiving data from client ...");
4805 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4806 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4808 DBG("Nothing recv() from client... continuing");
4814 clean_command_ctx(&cmd_ctx
);
4818 health_code_update();
4820 // TODO: Validate cmd_ctx including sanity check for
4821 // security purpose.
4823 rcu_thread_online();
4825 * This function dispatch the work to the kernel or userspace tracer
4826 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4827 * informations for the client. The command context struct contains
4828 * everything this function may needs.
4830 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4831 rcu_thread_offline();
4839 * TODO: Inform client somehow of the fatal error. At
4840 * this point, ret < 0 means that a zmalloc failed
4841 * (ENOMEM). Error detected but still accept
4842 * command, unless a socket error has been
4845 clean_command_ctx(&cmd_ctx
);
4849 cmd_completion_handler
= cmd_pop_completion_handler();
4850 if (cmd_completion_handler
) {
4851 enum lttng_error_code completion_code
;
4853 completion_code
= cmd_completion_handler
->run(
4854 cmd_completion_handler
->data
);
4855 if (completion_code
!= LTTNG_OK
) {
4856 clean_command_ctx(&cmd_ctx
);
4861 health_code_update();
4863 DBG("Sending response (size: %d, retcode: %s (%d))",
4864 cmd_ctx
->lttng_msg_size
,
4865 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4866 cmd_ctx
->llm
->ret_code
);
4867 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4869 ERR("Failed to send data back to client");
4872 /* End of transmission */
4879 clean_command_ctx(&cmd_ctx
);
4881 health_code_update();
4893 lttng_poll_clean(&events
);
4894 clean_command_ctx(&cmd_ctx
);
4898 unlink(config
.client_unix_sock_path
.value
);
4899 if (client_sock
>= 0) {
4900 ret
= close(client_sock
);
4908 ERR("Health error occurred in %s", __func__
);
4911 health_unregister(health_sessiond
);
4913 DBG("Client thread dying");
4915 rcu_unregister_thread();
4918 * Since we are creating the consumer threads, we own them, so we need
4919 * to join them before our thread exits.
4921 ret
= join_consumer_thread(&kconsumer_data
);
4924 PERROR("join_consumer");
4927 ret
= join_consumer_thread(&ustconsumer32_data
);
4930 PERROR("join_consumer ust32");
4933 ret
= join_consumer_thread(&ustconsumer64_data
);
4936 PERROR("join_consumer ust64");
4941 static int string_match(const char *str1
, const char *str2
)
4943 return (str1
&& str2
) && !strcmp(str1
, str2
);
4947 * Take an option from the getopt output and set it in the right variable to be
4950 * Return 0 on success else a negative value.
4952 static int set_option(int opt
, const char *arg
, const char *optname
)
4956 if (string_match(optname
, "client-sock") || opt
== 'c') {
4957 if (!arg
|| *arg
== '\0') {
4961 if (lttng_is_setuid_setgid()) {
4962 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4963 "-c, --client-sock");
4965 config_string_set(&config
.client_unix_sock_path
,
4967 if (!config
.client_unix_sock_path
.value
) {
4972 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4973 if (!arg
|| *arg
== '\0') {
4977 if (lttng_is_setuid_setgid()) {
4978 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4981 config_string_set(&config
.apps_unix_sock_path
,
4983 if (!config
.apps_unix_sock_path
.value
) {
4988 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4989 config
.daemonize
= true;
4990 } else if (string_match(optname
, "background") || opt
== 'b') {
4991 config
.background
= true;
4992 } else if (string_match(optname
, "group") || opt
== 'g') {
4993 if (!arg
|| *arg
== '\0') {
4997 if (lttng_is_setuid_setgid()) {
4998 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5001 config_string_set(&config
.tracing_group_name
,
5003 if (!config
.tracing_group_name
.value
) {
5008 } else if (string_match(optname
, "help") || opt
== 'h') {
5009 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
5011 ERR("Cannot show --help for `lttng-sessiond`");
5014 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
5015 } else if (string_match(optname
, "version") || opt
== 'V') {
5016 fprintf(stdout
, "%s\n", VERSION
);
5018 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
5019 config
.sig_parent
= true;
5020 } else if (string_match(optname
, "kconsumerd-err-sock")) {
5021 if (!arg
|| *arg
== '\0') {
5025 if (lttng_is_setuid_setgid()) {
5026 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5027 "--kconsumerd-err-sock");
5029 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
5031 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
5036 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
5037 if (!arg
|| *arg
== '\0') {
5041 if (lttng_is_setuid_setgid()) {
5042 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5043 "--kconsumerd-cmd-sock");
5045 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
5047 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
5052 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
5053 if (!arg
|| *arg
== '\0') {
5057 if (lttng_is_setuid_setgid()) {
5058 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5059 "--ustconsumerd64-err-sock");
5061 config_string_set(&config
.consumerd64_err_unix_sock_path
,
5063 if (!config
.consumerd64_err_unix_sock_path
.value
) {
5068 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
5069 if (!arg
|| *arg
== '\0') {
5073 if (lttng_is_setuid_setgid()) {
5074 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5075 "--ustconsumerd64-cmd-sock");
5077 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
5079 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
5084 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
5085 if (!arg
|| *arg
== '\0') {
5089 if (lttng_is_setuid_setgid()) {
5090 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5091 "--ustconsumerd32-err-sock");
5093 config_string_set(&config
.consumerd32_err_unix_sock_path
,
5095 if (!config
.consumerd32_err_unix_sock_path
.value
) {
5100 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
5101 if (!arg
|| *arg
== '\0') {
5105 if (lttng_is_setuid_setgid()) {
5106 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5107 "--ustconsumerd32-cmd-sock");
5109 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
5111 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
5116 } else if (string_match(optname
, "no-kernel")) {
5117 config
.no_kernel
= true;
5118 } else if (string_match(optname
, "quiet") || opt
== 'q') {
5119 config
.quiet
= true;
5120 } else if (string_match(optname
, "verbose") || opt
== 'v') {
5121 /* Verbose level can increase using multiple -v */
5123 /* Value obtained from config file */
5124 config
.verbose
= config_parse_value(arg
);
5126 /* -v used on command line */
5129 /* Clamp value to [0, 3] */
5130 config
.verbose
= config
.verbose
< 0 ? 0 :
5131 (config
.verbose
<= 3 ? config
.verbose
: 3);
5132 } else if (string_match(optname
, "verbose-consumer")) {
5134 config
.verbose_consumer
= config_parse_value(arg
);
5136 config
.verbose_consumer
++;
5138 } else if (string_match(optname
, "consumerd32-path")) {
5139 if (!arg
|| *arg
== '\0') {
5143 if (lttng_is_setuid_setgid()) {
5144 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5145 "--consumerd32-path");
5147 config_string_set(&config
.consumerd32_bin_path
,
5149 if (!config
.consumerd32_bin_path
.value
) {
5154 } else if (string_match(optname
, "consumerd32-libdir")) {
5155 if (!arg
|| *arg
== '\0') {
5159 if (lttng_is_setuid_setgid()) {
5160 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5161 "--consumerd32-libdir");
5163 config_string_set(&config
.consumerd32_lib_dir
,
5165 if (!config
.consumerd32_lib_dir
.value
) {
5170 } else if (string_match(optname
, "consumerd64-path")) {
5171 if (!arg
|| *arg
== '\0') {
5175 if (lttng_is_setuid_setgid()) {
5176 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5177 "--consumerd64-path");
5179 config_string_set(&config
.consumerd64_bin_path
,
5181 if (!config
.consumerd64_bin_path
.value
) {
5186 } else if (string_match(optname
, "consumerd64-libdir")) {
5187 if (!arg
|| *arg
== '\0') {
5191 if (lttng_is_setuid_setgid()) {
5192 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5193 "--consumerd64-libdir");
5195 config_string_set(&config
.consumerd64_lib_dir
,
5197 if (!config
.consumerd64_lib_dir
.value
) {
5202 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
5203 if (!arg
|| *arg
== '\0') {
5207 if (lttng_is_setuid_setgid()) {
5208 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5211 config_string_set(&config
.pid_file_path
, strdup(arg
));
5212 if (!config
.pid_file_path
.value
) {
5217 } else if (string_match(optname
, "agent-tcp-port")) {
5218 if (!arg
|| *arg
== '\0') {
5222 if (lttng_is_setuid_setgid()) {
5223 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5224 "--agent-tcp-port");
5229 v
= strtoul(arg
, NULL
, 0);
5230 if (errno
!= 0 || !isdigit(arg
[0])) {
5231 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5234 if (v
== 0 || v
>= 65535) {
5235 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5238 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
5239 DBG3("Agent TCP port set to non default: %i", (int) v
);
5241 } else if (string_match(optname
, "load") || opt
== 'l') {
5242 if (!arg
|| *arg
== '\0') {
5246 if (lttng_is_setuid_setgid()) {
5247 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5250 config_string_set(&config
.load_session_path
, strdup(arg
));
5251 if (!config
.load_session_path
.value
) {
5256 } else if (string_match(optname
, "kmod-probes")) {
5257 if (!arg
|| *arg
== '\0') {
5261 if (lttng_is_setuid_setgid()) {
5262 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5265 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
5266 if (!config
.kmod_probes_list
.value
) {
5271 } else if (string_match(optname
, "extra-kmod-probes")) {
5272 if (!arg
|| *arg
== '\0') {
5276 if (lttng_is_setuid_setgid()) {
5277 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5278 "--extra-kmod-probes");
5280 config_string_set(&config
.kmod_extra_probes_list
,
5282 if (!config
.kmod_extra_probes_list
.value
) {
5287 } else if (string_match(optname
, "config") || opt
== 'f') {
5288 /* This is handled in set_options() thus silent skip. */
5291 /* Unknown option or other error.
5292 * Error is printed by getopt, just return */
5297 if (ret
== -EINVAL
) {
5298 const char *opt_name
= "unknown";
5301 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5303 if (opt
== long_options
[i
].val
) {
5304 opt_name
= long_options
[i
].name
;
5309 WARN("Invalid argument provided for option \"%s\", using default value.",
5317 * config_entry_handler_cb used to handle options read from a config file.
5318 * See config_entry_handler_cb comment in common/config/session-config.h for the
5319 * return value conventions.
5321 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5325 if (!entry
|| !entry
->name
|| !entry
->value
) {
5330 /* Check if the option is to be ignored */
5331 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5332 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5337 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5340 /* Ignore if not fully matched. */
5341 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5346 * If the option takes no argument on the command line, we have to
5347 * check if the value is "true". We support non-zero numeric values,
5350 if (!long_options
[i
].has_arg
) {
5351 ret
= config_parse_value(entry
->value
);
5354 WARN("Invalid configuration value \"%s\" for option %s",
5355 entry
->value
, entry
->name
);
5357 /* False, skip boolean config option. */
5362 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5366 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5373 * daemon configuration loading and argument parsing
5375 static int set_options(int argc
, char **argv
)
5377 int ret
= 0, c
= 0, option_index
= 0;
5378 int orig_optopt
= optopt
, orig_optind
= optind
;
5380 const char *config_path
= NULL
;
5382 optstring
= utils_generate_optstring(long_options
,
5383 sizeof(long_options
) / sizeof(struct option
));
5389 /* Check for the --config option */
5390 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5391 &option_index
)) != -1) {
5395 } else if (c
!= 'f') {
5396 /* if not equal to --config option. */
5400 if (lttng_is_setuid_setgid()) {
5401 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5404 config_path
= utils_expand_path(optarg
);
5406 ERR("Failed to resolve path: %s", optarg
);
5411 ret
= config_get_section_entries(config_path
, config_section_name
,
5412 config_entry_handler
, NULL
);
5415 ERR("Invalid configuration option at line %i", ret
);
5421 /* Reset getopt's global state */
5422 optopt
= orig_optopt
;
5423 optind
= orig_optind
;
5427 * getopt_long() will not set option_index if it encounters a
5430 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5437 * Pass NULL as the long option name if popt left the index
5440 ret
= set_option(c
, optarg
,
5441 option_index
< 0 ? NULL
:
5442 long_options
[option_index
].name
);
5454 * Creates the two needed socket by the daemon.
5455 * apps_sock - The communication socket for all UST apps.
5456 * client_sock - The communication of the cli tool (lttng).
5458 static int init_daemon_socket(void)
5463 old_umask
= umask(0);
5465 /* Create client tool unix socket */
5466 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5467 if (client_sock
< 0) {
5468 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5473 /* Set the cloexec flag */
5474 ret
= utils_set_fd_cloexec(client_sock
);
5476 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5477 "Continuing but note that the consumer daemon will have a "
5478 "reference to this socket on exec()", client_sock
);
5481 /* File permission MUST be 660 */
5482 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5484 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5489 /* Create the application unix socket */
5490 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5491 if (apps_sock
< 0) {
5492 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5497 /* Set the cloexec flag */
5498 ret
= utils_set_fd_cloexec(apps_sock
);
5500 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5501 "Continuing but note that the consumer daemon will have a "
5502 "reference to this socket on exec()", apps_sock
);
5505 /* File permission MUST be 666 */
5506 ret
= chmod(config
.apps_unix_sock_path
.value
,
5507 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5509 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5514 DBG3("Session daemon client socket %d and application socket %d created",
5515 client_sock
, apps_sock
);
5523 * Create lockfile using the rundir and return its fd.
5525 static int create_lockfile(void)
5527 return utils_create_lock_file(config
.lock_file_path
.value
);
5531 * Check if the global socket is available, and if a daemon is answering at the
5532 * other side. If yes, error is returned.
5534 * Also attempts to create and hold the lock file.
5536 static int check_existing_daemon(void)
5540 /* Is there anybody out there ? */
5541 if (lttng_session_daemon_alive()) {
5546 lockfile_fd
= create_lockfile();
5547 if (lockfile_fd
< 0) {
5555 static void sessiond_cleanup_lock_file(void)
5560 * Cleanup lock file by deleting it and finaly closing it which will
5561 * release the file system lock.
5563 if (lockfile_fd
>= 0) {
5564 ret
= remove(config
.lock_file_path
.value
);
5566 PERROR("remove lock file");
5568 ret
= close(lockfile_fd
);
5570 PERROR("close lock file");
5576 * Set the tracing group gid onto the client socket.
5578 * Race window between mkdir and chown is OK because we are going from more
5579 * permissive (root.root) to less permissive (root.tracing).
5581 static int set_permissions(char *rundir
)
5586 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5588 /* Set lttng run dir */
5589 ret
= chown(rundir
, 0, gid
);
5591 ERR("Unable to set group on %s", rundir
);
5596 * Ensure all applications and tracing group can search the run
5597 * dir. Allow everyone to read the directory, since it does not
5598 * buy us anything to hide its content.
5600 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5602 ERR("Unable to set permissions on %s", rundir
);
5606 /* lttng client socket path */
5607 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5609 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5613 /* kconsumer error socket path */
5614 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5616 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5620 /* 64-bit ustconsumer error socket path */
5621 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5623 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5627 /* 32-bit ustconsumer compat32 error socket path */
5628 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5630 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5634 DBG("All permissions are set");
5640 * Create the lttng run directory needed for all global sockets and pipe.
5642 static int create_lttng_rundir(void)
5646 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5648 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5650 if (errno
!= EEXIST
) {
5651 ERR("Unable to create %s", config
.rundir
.value
);
5663 * Setup sockets and directory needed by the consumerds' communication with the
5666 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5671 switch (consumer_data
->type
) {
5672 case LTTNG_CONSUMER_KERNEL
:
5673 path
= config
.kconsumerd_path
.value
;
5675 case LTTNG_CONSUMER64_UST
:
5676 path
= config
.consumerd64_path
.value
;
5678 case LTTNG_CONSUMER32_UST
:
5679 path
= config
.consumerd32_path
.value
;
5682 ERR("Consumer type unknown");
5688 DBG2("Creating consumer directory: %s", path
);
5690 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5691 if (ret
< 0 && errno
!= EEXIST
) {
5693 ERR("Failed to create %s", path
);
5697 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5699 ERR("Unable to set group on %s", path
);
5705 /* Create the consumerd error unix socket */
5706 consumer_data
->err_sock
=
5707 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5708 if (consumer_data
->err_sock
< 0) {
5709 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5715 * Set the CLOEXEC flag. Return code is useless because either way, the
5718 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5720 PERROR("utils_set_fd_cloexec");
5721 /* continue anyway */
5724 /* File permission MUST be 660 */
5725 ret
= chmod(consumer_data
->err_unix_sock_path
,
5726 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5728 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5738 * Signal handler for the daemon
5740 * Simply stop all worker threads, leaving main() return gracefully after
5741 * joining all threads and calling cleanup().
5743 static void sighandler(int sig
)
5747 DBG("SIGINT caught");
5751 DBG("SIGTERM caught");
5755 CMM_STORE_SHARED(recv_child_signal
, 1);
5763 * Setup signal handler for :
5764 * SIGINT, SIGTERM, SIGPIPE
5766 static int set_signal_handler(void)
5769 struct sigaction sa
;
5772 if ((ret
= sigemptyset(&sigset
)) < 0) {
5773 PERROR("sigemptyset");
5777 sa
.sa_mask
= sigset
;
5780 sa
.sa_handler
= sighandler
;
5781 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5782 PERROR("sigaction");
5786 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5787 PERROR("sigaction");
5791 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5792 PERROR("sigaction");
5796 sa
.sa_handler
= SIG_IGN
;
5797 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5798 PERROR("sigaction");
5802 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5808 * Set open files limit to unlimited. This daemon can open a large number of
5809 * file descriptors in order to consume multiple kernel traces.
5811 static void set_ulimit(void)
5816 /* The kernel does not allow an infinite limit for open files */
5817 lim
.rlim_cur
= 65535;
5818 lim
.rlim_max
= 65535;
5820 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5822 PERROR("failed to set open files limit");
5826 static int write_pidfile(void)
5828 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5831 static int set_clock_plugin_env(void)
5834 char *env_value
= NULL
;
5836 if (!config
.lttng_ust_clock_plugin
.value
) {
5840 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5841 config
.lttng_ust_clock_plugin
.value
);
5847 ret
= putenv(env_value
);
5850 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5854 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5855 config
.lttng_ust_clock_plugin
.value
);
5861 struct rotation_thread_timer_queue
*create_rotate_timer_queue(void)
5863 struct rotation_thread_timer_queue
*queue
= NULL
;
5865 queue
= zmalloc(sizeof(struct rotation_thread_timer_queue
));
5867 PERROR("Failed to allocate timer rotate queue");
5871 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
5872 CDS_INIT_LIST_HEAD(&queue
->list
);
5873 pthread_mutex_init(&queue
->lock
, NULL
);
5880 void destroy_rotate_timer_queue(struct rotation_thread_timer_queue
*queue
)
5882 struct sessiond_rotation_timer
*node
, *tmp_node
;
5888 lttng_pipe_destroy(queue
->event_pipe
);
5890 pthread_mutex_lock(&queue
->lock
);
5891 /* Empty wait queue. */
5892 cds_list_for_each_entry_safe(node
, tmp_node
, &queue
->list
, head
) {
5893 cds_list_del(&node
->head
);
5896 pthread_mutex_unlock(&queue
->lock
);
5898 pthread_mutex_destroy(&queue
->lock
);
5905 int main(int argc
, char **argv
)
5907 int ret
= 0, retval
= 0;
5909 const char *env_app_timeout
;
5910 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5911 *ust64_channel_monitor_pipe
= NULL
,
5912 *kernel_channel_monitor_pipe
= NULL
;
5913 bool notification_thread_launched
= false;
5914 bool rotation_thread_launched
= false;
5915 bool timer_thread_launched
= false;
5916 struct lttng_pipe
*ust32_channel_rotate_pipe
= NULL
,
5917 *ust64_channel_rotate_pipe
= NULL
,
5918 *kernel_channel_rotate_pipe
= NULL
;
5919 struct timer_thread_parameters timer_thread_ctx
;
5920 /* Queue of rotation jobs populated by the sessiond-timer. */
5921 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5922 sem_t notification_thread_ready
;
5924 init_kernel_workarounds();
5926 rcu_register_thread();
5928 if (set_signal_handler()) {
5930 goto exit_set_signal_handler
;
5933 if (sessiond_timer_signal_init()) {
5935 goto exit_set_signal_handler
;
5938 page_size
= sysconf(_SC_PAGESIZE
);
5939 if (page_size
< 0) {
5940 PERROR("sysconf _SC_PAGESIZE");
5941 page_size
= LONG_MAX
;
5942 WARN("Fallback page size to %ld", page_size
);
5945 ret
= sessiond_config_init(&config
);
5948 goto exit_set_signal_handler
;
5952 * Init config from environment variables.
5953 * Command line option override env configuration per-doc. Do env first.
5955 sessiond_config_apply_env_config(&config
);
5958 * Parse arguments and load the daemon configuration file.
5960 * We have an exit_options exit path to free memory reserved by
5961 * set_options. This is needed because the rest of sessiond_cleanup()
5962 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5963 * depends on set_options.
5966 if (set_options(argc
, argv
)) {
5972 * Resolve all paths received as arguments, configuration option, or
5973 * through environment variable as absolute paths. This is necessary
5974 * since daemonizing causes the sessiond's current working directory
5977 ret
= sessiond_config_resolve_paths(&config
);
5983 lttng_opt_verbose
= config
.verbose
;
5984 lttng_opt_quiet
= config
.quiet
;
5985 kconsumer_data
.err_unix_sock_path
=
5986 config
.kconsumerd_err_unix_sock_path
.value
;
5987 kconsumer_data
.cmd_unix_sock_path
=
5988 config
.kconsumerd_cmd_unix_sock_path
.value
;
5989 ustconsumer32_data
.err_unix_sock_path
=
5990 config
.consumerd32_err_unix_sock_path
.value
;
5991 ustconsumer32_data
.cmd_unix_sock_path
=
5992 config
.consumerd32_cmd_unix_sock_path
.value
;
5993 ustconsumer64_data
.err_unix_sock_path
=
5994 config
.consumerd64_err_unix_sock_path
.value
;
5995 ustconsumer64_data
.cmd_unix_sock_path
=
5996 config
.consumerd64_cmd_unix_sock_path
.value
;
5997 set_clock_plugin_env();
5999 sessiond_config_log(&config
);
6001 if (create_lttng_rundir()) {
6006 /* Abort launch if a session daemon is already running. */
6007 if (check_existing_daemon()) {
6008 ERR("A session daemon is already running.");
6014 if (config
.daemonize
|| config
.background
) {
6017 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
6018 !config
.background
);
6025 * We are in the child. Make sure all other file descriptors are
6026 * closed, in case we are called with more opened file
6027 * descriptors than the standard ones and the lock file.
6029 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
6030 if (i
== lockfile_fd
) {
6037 if (run_as_create_worker(argv
[0]) < 0) {
6038 goto exit_create_run_as_worker_cleanup
;
6042 * Starting from here, we can create threads. This needs to be after
6043 * lttng_daemonize due to RCU.
6047 * Initialize the health check subsystem. This call should set the
6048 * appropriate time values.
6050 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
6051 if (!health_sessiond
) {
6052 PERROR("health_app_create error");
6054 goto exit_health_sessiond_cleanup
;
6057 /* Create thread to clean up RCU hash tables */
6058 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
6060 goto exit_ht_cleanup
;
6063 /* Create thread quit pipe */
6064 if (init_thread_quit_pipe()) {
6066 goto exit_init_data
;
6069 /* Check if daemon is UID = 0 */
6070 is_root
= !getuid();
6072 /* Create global run dir with root access */
6074 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
6075 if (!kernel_channel_monitor_pipe
) {
6076 ERR("Failed to create kernel consumer channel monitor pipe");
6078 goto exit_init_data
;
6080 kconsumer_data
.channel_monitor_pipe
=
6081 lttng_pipe_release_writefd(
6082 kernel_channel_monitor_pipe
);
6083 if (kconsumer_data
.channel_monitor_pipe
< 0) {
6085 goto exit_init_data
;
6087 kernel_channel_rotate_pipe
= lttng_pipe_open(0);
6088 if (!kernel_channel_rotate_pipe
) {
6089 ERR("Failed to create kernel consumer channel rotate pipe");
6091 goto exit_init_data
;
6093 kconsumer_data
.channel_rotate_pipe
=
6094 lttng_pipe_release_writefd(
6095 kernel_channel_rotate_pipe
);
6096 if (kconsumer_data
.channel_rotate_pipe
< 0) {
6098 goto exit_init_data
;
6102 /* Set consumer initial state */
6103 kernel_consumerd_state
= CONSUMER_STOPPED
;
6104 ust_consumerd_state
= CONSUMER_STOPPED
;
6106 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
6107 if (!ust32_channel_monitor_pipe
) {
6108 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
6110 goto exit_init_data
;
6112 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6113 ust32_channel_monitor_pipe
);
6114 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
6116 goto exit_init_data
;
6118 ust32_channel_rotate_pipe
= lttng_pipe_open(0);
6119 if (!ust32_channel_rotate_pipe
) {
6120 ERR("Failed to create 32-bit user space consumer channel rotate pipe");
6122 goto exit_init_data
;
6124 ustconsumer32_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6125 ust32_channel_rotate_pipe
);
6126 if (ustconsumer32_data
.channel_rotate_pipe
< 0) {
6128 goto exit_init_data
;
6132 * The rotation_timer_queue structure is shared between the sessiond timer
6133 * thread and the rotation thread. The main() keeps the ownership and
6134 * destroys it when both threads have quit.
6136 rotation_timer_queue
= create_rotate_timer_queue();
6137 if (!rotation_timer_queue
) {
6139 goto exit_init_data
;
6141 timer_thread_ctx
.rotation_timer_queue
= rotation_timer_queue
;
6143 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
6144 if (!ust64_channel_monitor_pipe
) {
6145 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
6147 goto exit_init_data
;
6149 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6150 ust64_channel_monitor_pipe
);
6151 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
6153 goto exit_init_data
;
6155 ust64_channel_rotate_pipe
= lttng_pipe_open(0);
6156 if (!ust64_channel_rotate_pipe
) {
6157 ERR("Failed to create 64-bit user space consumer channel rotate pipe");
6159 goto exit_init_data
;
6161 ustconsumer64_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6162 ust64_channel_rotate_pipe
);
6163 if (ustconsumer64_data
.channel_rotate_pipe
< 0) {
6165 goto exit_init_data
;
6169 * Init UST app hash table. Alloc hash table before this point since
6170 * cleanup() can get called after that point.
6172 if (ust_app_ht_alloc()) {
6173 ERR("Failed to allocate UST app hash table");
6175 goto exit_init_data
;
6179 * Initialize agent app hash table. We allocate the hash table here
6180 * since cleanup() can get called after this point.
6182 if (agent_app_ht_alloc()) {
6183 ERR("Failed to allocate Agent app hash table");
6185 goto exit_init_data
;
6189 * These actions must be executed as root. We do that *after* setting up
6190 * the sockets path because we MUST make the check for another daemon using
6191 * those paths *before* trying to set the kernel consumer sockets and init
6195 if (set_consumer_sockets(&kconsumer_data
)) {
6197 goto exit_init_data
;
6200 /* Setup kernel tracer */
6201 if (!config
.no_kernel
) {
6202 init_kernel_tracer();
6203 if (kernel_tracer_fd
>= 0) {
6204 ret
= syscall_init_table();
6206 ERR("Unable to populate syscall table. "
6207 "Syscall tracing won't work "
6208 "for this session daemon.");
6213 /* Set ulimit for open files */
6216 /* init lttng_fd tracking must be done after set_ulimit. */
6219 if (set_consumer_sockets(&ustconsumer64_data
)) {
6221 goto exit_init_data
;
6224 if (set_consumer_sockets(&ustconsumer32_data
)) {
6226 goto exit_init_data
;
6229 /* Setup the needed unix socket */
6230 if (init_daemon_socket()) {
6232 goto exit_init_data
;
6235 /* Set credentials to socket */
6236 if (is_root
&& set_permissions(config
.rundir
.value
)) {
6238 goto exit_init_data
;
6241 /* Get parent pid if -S, --sig-parent is specified. */
6242 if (config
.sig_parent
) {
6246 /* Setup the kernel pipe for waking up the kernel thread */
6247 if (is_root
&& !config
.no_kernel
) {
6248 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6250 goto exit_init_data
;
6254 /* Setup the thread apps communication pipe. */
6255 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6257 goto exit_init_data
;
6260 /* Setup the thread apps notify communication pipe. */
6261 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6263 goto exit_init_data
;
6266 /* Initialize global buffer per UID and PID registry. */
6267 buffer_reg_init_uid_registry();
6268 buffer_reg_init_pid_registry();
6270 /* Init UST command queue. */
6271 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6274 * Get session list pointer. This pointer MUST NOT be free'd. This list
6275 * is statically declared in session.c
6277 session_list_ptr
= session_get_list();
6281 /* Check for the application socket timeout env variable. */
6282 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6283 if (env_app_timeout
) {
6284 config
.app_socket_timeout
= atoi(env_app_timeout
);
6286 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6289 ret
= write_pidfile();
6291 ERR("Error in write_pidfile");
6293 goto exit_init_data
;
6296 /* Initialize communication library */
6298 /* Initialize TCP timeout values */
6299 lttcomm_inet_init();
6301 if (load_session_init_data(&load_info
) < 0) {
6303 goto exit_init_data
;
6305 load_info
->path
= config
.load_session_path
.value
;
6307 /* Create health-check thread. */
6308 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6309 thread_manage_health
, (void *) NULL
);
6312 PERROR("pthread_create health");
6318 * The rotation thread needs the notification thread to be ready before
6319 * creating the rotate_notification_channel, so we use this semaphore as
6320 * a rendez-vous point.
6322 sem_init(¬ification_thread_ready
, 0, 0);
6324 /* notification_thread_data acquires the pipes' read side. */
6325 notification_thread_handle
= notification_thread_handle_create(
6326 ust32_channel_monitor_pipe
,
6327 ust64_channel_monitor_pipe
,
6328 kernel_channel_monitor_pipe
,
6329 ¬ification_thread_ready
);
6330 if (!notification_thread_handle
) {
6332 ERR("Failed to create notification thread shared data");
6334 goto exit_notification
;
6337 /* Create notification thread. */
6338 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6339 thread_notification
, notification_thread_handle
);
6342 PERROR("pthread_create notification");
6345 goto exit_notification
;
6347 notification_thread_launched
= true;
6349 /* Create timer thread. */
6350 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
6351 sessiond_timer_thread
, &timer_thread_ctx
);
6354 PERROR("pthread_create timer");
6357 goto exit_notification
;
6359 timer_thread_launched
= true;
6361 /* rotation_thread_data acquires the pipes' read side. */
6362 rotation_thread_handle
= rotation_thread_handle_create(
6363 ust32_channel_rotate_pipe
,
6364 ust64_channel_rotate_pipe
,
6365 kernel_channel_rotate_pipe
,
6366 thread_quit_pipe
[0],
6367 rotation_timer_queue
,
6368 notification_thread_handle
,
6369 ¬ification_thread_ready
);
6370 if (!rotation_thread_handle
) {
6372 ERR("Failed to create rotation thread shared data");
6377 /* Create rotation thread. */
6378 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6379 thread_rotation
, rotation_thread_handle
);
6382 PERROR("pthread_create rotation");
6387 rotation_thread_launched
= true;
6389 /* Create thread to manage the client socket */
6390 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6391 thread_manage_clients
, (void *) NULL
);
6394 PERROR("pthread_create clients");
6400 /* Create thread to dispatch registration */
6401 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6402 thread_dispatch_ust_registration
, (void *) NULL
);
6405 PERROR("pthread_create dispatch");
6411 /* Create thread to manage application registration. */
6412 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6413 thread_registration_apps
, (void *) NULL
);
6416 PERROR("pthread_create registration");
6422 /* Create thread to manage application socket */
6423 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6424 thread_manage_apps
, (void *) NULL
);
6427 PERROR("pthread_create apps");
6433 /* Create thread to manage application notify socket */
6434 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6435 ust_thread_manage_notify
, (void *) NULL
);
6438 PERROR("pthread_create notify");
6441 goto exit_apps_notify
;
6444 /* Create agent registration thread. */
6445 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6446 agent_thread_manage_registration
, (void *) NULL
);
6449 PERROR("pthread_create agent");
6452 goto exit_agent_reg
;
6455 /* Don't start this thread if kernel tracing is not requested nor root */
6456 if (is_root
&& !config
.no_kernel
) {
6457 /* Create kernel thread to manage kernel event */
6458 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6459 thread_manage_kernel
, (void *) NULL
);
6462 PERROR("pthread_create kernel");
6469 /* Create session loading thread. */
6470 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6471 thread_load_session
, load_info
);
6474 PERROR("pthread_create load_session_thread");
6477 goto exit_load_session
;
6481 * This is where we start awaiting program completion (e.g. through
6482 * signal that asks threads to teardown).
6485 ret
= pthread_join(load_session_thread
, &status
);
6488 PERROR("pthread_join load_session_thread");
6493 if (is_root
&& !config
.no_kernel
) {
6494 ret
= pthread_join(kernel_thread
, &status
);
6497 PERROR("pthread_join");
6503 ret
= pthread_join(agent_reg_thread
, &status
);
6506 PERROR("pthread_join agent");
6511 ret
= pthread_join(apps_notify_thread
, &status
);
6514 PERROR("pthread_join apps notify");
6519 ret
= pthread_join(apps_thread
, &status
);
6522 PERROR("pthread_join apps");
6527 ret
= pthread_join(reg_apps_thread
, &status
);
6530 PERROR("pthread_join");
6536 * Join dispatch thread after joining reg_apps_thread to ensure
6537 * we don't leak applications in the queue.
6539 ret
= pthread_join(dispatch_thread
, &status
);
6542 PERROR("pthread_join");
6547 ret
= pthread_join(client_thread
, &status
);
6550 PERROR("pthread_join");
6557 sem_destroy(¬ification_thread_ready
);
6558 ret
= pthread_join(health_thread
, &status
);
6561 PERROR("pthread_join health thread");
6568 * Wait for all pending call_rcu work to complete before tearing
6569 * down data structures. call_rcu worker may be trying to
6570 * perform lookups in those structures.
6574 * sessiond_cleanup() is called when no other thread is running, except
6575 * the ht_cleanup thread, which is needed to destroy the hash tables.
6577 rcu_thread_online();
6581 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6582 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6583 * the queue is empty before shutting down the clean-up thread.
6588 * The teardown of the notification system is performed after the
6589 * session daemon's teardown in order to allow it to be notified
6590 * of the active session and channels at the moment of the teardown.
6592 if (notification_thread_handle
) {
6593 if (notification_thread_launched
) {
6594 notification_thread_command_quit(
6595 notification_thread_handle
);
6596 ret
= pthread_join(notification_thread
, &status
);
6599 PERROR("pthread_join notification thread");
6603 notification_thread_handle_destroy(notification_thread_handle
);
6606 if (rotation_thread_handle
) {
6607 if (rotation_thread_launched
) {
6608 ret
= pthread_join(rotation_thread
, &status
);
6611 PERROR("pthread_join rotation thread");
6615 rotation_thread_handle_destroy(rotation_thread_handle
);
6618 if (timer_thread_launched
) {
6619 kill(getpid(), LTTNG_SESSIOND_SIG_EXIT
);
6620 ret
= pthread_join(timer_thread
, &status
);
6623 PERROR("pthread_join timer thread");
6629 * After the rotation and timer thread have quit, we can safely destroy
6630 * the rotation_timer_queue.
6632 destroy_rotate_timer_queue(rotation_timer_queue
);
6634 rcu_thread_offline();
6635 rcu_unregister_thread();
6637 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6641 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6642 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6643 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6644 lttng_pipe_destroy(ust32_channel_rotate_pipe
);
6645 lttng_pipe_destroy(ust64_channel_rotate_pipe
);
6646 lttng_pipe_destroy(kernel_channel_rotate_pipe
);
6649 health_app_destroy(health_sessiond
);
6650 exit_health_sessiond_cleanup
:
6651 exit_create_run_as_worker_cleanup
:
6654 sessiond_cleanup_lock_file();
6655 sessiond_cleanup_options();
6657 exit_set_signal_handler
: