2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
52 #include <common/dynamic-buffer.h>
53 #include <lttng/userspace-probe-internal.h>
54 #include <lttng/event-internal.h>
56 #include "lttng-sessiond.h"
57 #include "buffer-registry.h"
64 #include "kernel-consumer.h"
68 #include "ust-consumer.h"
71 #include "health-sessiond.h"
72 #include "testpoint.h"
73 #include "ust-thread.h"
74 #include "agent-thread.h"
76 #include "load-session-thread.h"
77 #include "notification-thread.h"
78 #include "notification-thread-commands.h"
79 #include "rotation-thread.h"
80 #include "lttng-syscall.h"
82 #include "ht-cleanup.h"
83 #include "sessiond-config.h"
84 #include "sessiond-timer.h"
86 static const char *help_msg
=
87 #ifdef LTTNG_EMBED_HELP
88 #include <lttng-sessiond.8.h>
95 static pid_t ppid
; /* Parent PID for --sig-parent option */
96 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
97 static int lockfile_fd
= -1;
99 /* Set to 1 when a SIGUSR1 signal is received. */
100 static int recv_child_signal
;
102 static struct lttng_kernel_tracer_version kernel_tracer_version
;
103 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
106 * Consumer daemon specific control data. Every value not initialized here is
107 * set to 0 by the static definition.
109 static struct consumer_data kconsumer_data
= {
110 .type
= LTTNG_CONSUMER_KERNEL
,
113 .channel_monitor_pipe
= -1,
114 .channel_rotate_pipe
= -1,
115 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
116 .lock
= PTHREAD_MUTEX_INITIALIZER
,
117 .cond
= PTHREAD_COND_INITIALIZER
,
118 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
120 static struct consumer_data ustconsumer64_data
= {
121 .type
= LTTNG_CONSUMER64_UST
,
124 .channel_monitor_pipe
= -1,
125 .channel_rotate_pipe
= -1,
126 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
127 .lock
= PTHREAD_MUTEX_INITIALIZER
,
128 .cond
= PTHREAD_COND_INITIALIZER
,
129 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
131 static struct consumer_data ustconsumer32_data
= {
132 .type
= LTTNG_CONSUMER32_UST
,
135 .channel_monitor_pipe
= -1,
136 .channel_rotate_pipe
= -1,
137 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
138 .lock
= PTHREAD_MUTEX_INITIALIZER
,
139 .cond
= PTHREAD_COND_INITIALIZER
,
140 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
143 /* Command line options */
144 static const struct option long_options
[] = {
145 { "client-sock", required_argument
, 0, 'c' },
146 { "apps-sock", required_argument
, 0, 'a' },
147 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
148 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
149 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
150 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
151 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
152 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
153 { "consumerd32-path", required_argument
, 0, '\0' },
154 { "consumerd32-libdir", required_argument
, 0, '\0' },
155 { "consumerd64-path", required_argument
, 0, '\0' },
156 { "consumerd64-libdir", required_argument
, 0, '\0' },
157 { "daemonize", no_argument
, 0, 'd' },
158 { "background", no_argument
, 0, 'b' },
159 { "sig-parent", no_argument
, 0, 'S' },
160 { "help", no_argument
, 0, 'h' },
161 { "group", required_argument
, 0, 'g' },
162 { "version", no_argument
, 0, 'V' },
163 { "quiet", no_argument
, 0, 'q' },
164 { "verbose", no_argument
, 0, 'v' },
165 { "verbose-consumer", no_argument
, 0, '\0' },
166 { "no-kernel", no_argument
, 0, '\0' },
167 { "pidfile", required_argument
, 0, 'p' },
168 { "agent-tcp-port", required_argument
, 0, '\0' },
169 { "config", required_argument
, 0, 'f' },
170 { "load", required_argument
, 0, 'l' },
171 { "kmod-probes", required_argument
, 0, '\0' },
172 { "extra-kmod-probes", required_argument
, 0, '\0' },
176 struct sessiond_config config
;
178 /* Command line options to ignore from configuration file */
179 static const char *config_ignore_options
[] = { "help", "version", "config" };
181 /* Shared between threads */
182 static int dispatch_thread_exit
;
184 /* Sockets and FDs */
185 static int client_sock
= -1;
186 static int apps_sock
= -1;
187 int kernel_tracer_fd
= -1;
188 static int kernel_poll_pipe
[2] = { -1, -1 };
191 * Quit pipe for all threads. This permits a single cancellation point
192 * for all threads when receiving an event on the pipe.
194 static int thread_quit_pipe
[2] = { -1, -1 };
197 * This pipe is used to inform the thread managing application communication
198 * that a command is queued and ready to be processed.
200 static int apps_cmd_pipe
[2] = { -1, -1 };
202 int apps_cmd_notify_pipe
[2] = { -1, -1 };
204 /* Pthread, Mutexes and Semaphores */
205 static pthread_t apps_thread
;
206 static pthread_t apps_notify_thread
;
207 static pthread_t reg_apps_thread
;
208 static pthread_t client_thread
;
209 static pthread_t kernel_thread
;
210 static pthread_t dispatch_thread
;
211 static pthread_t health_thread
;
212 static pthread_t ht_cleanup_thread
;
213 static pthread_t agent_reg_thread
;
214 static pthread_t load_session_thread
;
215 static pthread_t notification_thread
;
216 static pthread_t rotation_thread
;
217 static pthread_t timer_thread
;
220 * UST registration command queue. This queue is tied with a futex and uses a N
221 * wakers / 1 waiter implemented and detailed in futex.c/.h
223 * The thread_registration_apps and thread_dispatch_ust_registration uses this
224 * queue along with the wait/wake scheme. The thread_manage_apps receives down
225 * the line new application socket and monitors it for any I/O error or clean
226 * close that triggers an unregistration of the application.
228 static struct ust_cmd_queue ust_cmd_queue
;
231 * Pointer initialized before thread creation.
233 * This points to the tracing session list containing the session count and a
234 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
235 * MUST NOT be taken if you call a public function in session.c.
237 * The lock is nested inside the structure: session_list_ptr->lock. Please use
238 * session_lock_list and session_unlock_list for lock acquisition.
240 static struct ltt_session_list
*session_list_ptr
;
242 int ust_consumerd64_fd
= -1;
243 int ust_consumerd32_fd
= -1;
245 static const char *module_proc_lttng
= "/proc/lttng";
248 * Consumer daemon state which is changed when spawning it, killing it or in
249 * case of a fatal error.
251 enum consumerd_state
{
252 CONSUMER_STARTED
= 1,
253 CONSUMER_STOPPED
= 2,
258 * This consumer daemon state is used to validate if a client command will be
259 * able to reach the consumer. If not, the client is informed. For instance,
260 * doing a "lttng start" when the consumer state is set to ERROR will return an
261 * error to the client.
263 * The following example shows a possible race condition of this scheme:
265 * consumer thread error happens
267 * client cmd checks state -> still OK
268 * consumer thread exit, sets error
269 * client cmd try to talk to consumer
272 * However, since the consumer is a different daemon, we have no way of making
273 * sure the command will reach it safely even with this state flag. This is why
274 * we consider that up to the state validation during command processing, the
275 * command is safe. After that, we can not guarantee the correctness of the
276 * client request vis-a-vis the consumer.
278 static enum consumerd_state ust_consumerd_state
;
279 static enum consumerd_state kernel_consumerd_state
;
281 /* Set in main() with the current page size. */
284 /* Application health monitoring */
285 struct health_app
*health_sessiond
;
287 /* Am I root or not. */
288 int is_root
; /* Set to 1 if the daemon is running as root */
290 const char * const config_section_name
= "sessiond";
292 /* Load session thread information to operate. */
293 struct load_session_thread_data
*load_info
;
295 /* Notification thread handle. */
296 struct notification_thread_handle
*notification_thread_handle
;
298 /* Rotation thread handle. */
299 struct rotation_thread_handle
*rotation_thread_handle
;
301 /* Global hash tables */
302 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
305 * The initialization of the session daemon is done in multiple phases.
307 * While all threads are launched near-simultaneously, only some of them
308 * are needed to ensure the session daemon can start to respond to client
311 * There are two important guarantees that we wish to offer with respect
312 * to the initialisation of the session daemon:
313 * - When the daemonize/background launcher process exits, the sessiond
314 * is fully able to respond to client requests,
315 * - Auto-loaded sessions are visible to clients.
317 * In order to achieve this, a number of support threads have to be launched
318 * to allow the "client" thread to function properly. Moreover, since the
319 * "load session" thread needs the client thread, we must provide a way
320 * for the "load session" thread to know that the "client" thread is up
323 * Hence, the support threads decrement the lttng_sessiond_ready counter
324 * while the "client" threads waits for it to reach 0. Once the "client" thread
325 * unblocks, it posts the message_thread_ready semaphore which allows the
326 * "load session" thread to progress.
328 * This implies that the "load session" thread is the last to be initialized
329 * and will explicitly call sessiond_signal_parents(), which signals the parents
330 * that the session daemon is fully initialized.
332 * The four (4) support threads are:
334 * - notification_thread
338 #define NR_LTTNG_SESSIOND_SUPPORT_THREADS 4
339 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_SUPPORT_THREADS
;
341 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
343 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
346 /* Notify parents that we are ready for cmd and health check */
348 void sessiond_signal_parents(void)
351 * Notify parent pid that we are ready to accept command
352 * for client side. This ppid is the one from the
353 * external process that spawned us.
355 if (config
.sig_parent
) {
360 * Notify the parent of the fork() process that we are
363 if (config
.daemonize
|| config
.background
) {
364 kill(child_ppid
, SIGUSR1
);
369 void sessiond_notify_ready(void)
372 * This memory barrier is paired with the one performed by
373 * the client thread after it has seen that 'lttng_sessiond_ready' is 0.
375 * The purpose of these memory barriers is to ensure that all
376 * initialization operations of the various threads that call this
377 * function to signal that they are ready are commited/published
378 * before the client thread can see the 'lttng_sessiond_ready' counter
381 * Note that this could be a 'write' memory barrier, but a full barrier
382 * is used in case the code using this utility changes. The performance
383 * implications of this choice are minimal since this is a slow path.
386 uatomic_sub(<tng_sessiond_ready
, 1);
390 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
397 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
403 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
415 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
417 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
419 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
423 * Init thread quit pipe.
425 * Return -1 on error or 0 if all pipes are created.
427 static int __init_thread_quit_pipe(int *a_pipe
)
433 PERROR("thread quit pipe");
437 for (i
= 0; i
< 2; i
++) {
438 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
449 static int init_thread_quit_pipe(void)
451 return __init_thread_quit_pipe(thread_quit_pipe
);
455 * Stop all threads by closing the thread quit pipe.
457 static void stop_threads(void)
461 /* Stopping all threads */
462 DBG("Terminating all threads");
463 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
465 ERR("write error on thread quit pipe");
468 /* Dispatch thread */
469 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
470 futex_nto1_wake(&ust_cmd_queue
.futex
);
474 * Close every consumer sockets.
476 static void close_consumer_sockets(void)
480 if (kconsumer_data
.err_sock
>= 0) {
481 ret
= close(kconsumer_data
.err_sock
);
483 PERROR("kernel consumer err_sock close");
486 if (ustconsumer32_data
.err_sock
>= 0) {
487 ret
= close(ustconsumer32_data
.err_sock
);
489 PERROR("UST consumerd32 err_sock close");
492 if (ustconsumer64_data
.err_sock
>= 0) {
493 ret
= close(ustconsumer64_data
.err_sock
);
495 PERROR("UST consumerd64 err_sock close");
498 if (kconsumer_data
.cmd_sock
>= 0) {
499 ret
= close(kconsumer_data
.cmd_sock
);
501 PERROR("kernel consumer cmd_sock close");
504 if (ustconsumer32_data
.cmd_sock
>= 0) {
505 ret
= close(ustconsumer32_data
.cmd_sock
);
507 PERROR("UST consumerd32 cmd_sock close");
510 if (ustconsumer64_data
.cmd_sock
>= 0) {
511 ret
= close(ustconsumer64_data
.cmd_sock
);
513 PERROR("UST consumerd64 cmd_sock close");
516 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
517 ret
= close(kconsumer_data
.channel_monitor_pipe
);
519 PERROR("kernel consumer channel monitor pipe close");
522 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
523 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
525 PERROR("UST consumerd32 channel monitor pipe close");
528 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
529 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
531 PERROR("UST consumerd64 channel monitor pipe close");
534 if (kconsumer_data
.channel_rotate_pipe
>= 0) {
535 ret
= close(kconsumer_data
.channel_rotate_pipe
);
537 PERROR("kernel consumer channel rotate pipe close");
540 if (ustconsumer32_data
.channel_rotate_pipe
>= 0) {
541 ret
= close(ustconsumer32_data
.channel_rotate_pipe
);
543 PERROR("UST consumerd32 channel rotate pipe close");
546 if (ustconsumer64_data
.channel_rotate_pipe
>= 0) {
547 ret
= close(ustconsumer64_data
.channel_rotate_pipe
);
549 PERROR("UST consumerd64 channel rotate pipe close");
555 * Wait on consumer process termination.
557 * Need to be called with the consumer data lock held or from a context
558 * ensuring no concurrent access to data (e.g: cleanup).
560 static void wait_consumer(struct consumer_data
*consumer_data
)
565 if (consumer_data
->pid
<= 0) {
569 DBG("Waiting for complete teardown of consumerd (PID: %d)",
571 ret
= waitpid(consumer_data
->pid
, &status
, 0);
573 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
574 } else if (!WIFEXITED(status
)) {
575 ERR("consumerd termination with error: %d",
578 consumer_data
->pid
= 0;
582 * Cleanup the session daemon's data structures.
584 static void sessiond_cleanup(void)
587 struct ltt_session
*sess
, *stmp
;
589 DBG("Cleanup sessiond");
592 * Close the thread quit pipe. It has already done its job,
593 * since we are now called.
595 utils_close_pipe(thread_quit_pipe
);
597 ret
= remove(config
.pid_file_path
.value
);
599 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
602 DBG("Removing sessiond and consumerd content of directory %s",
603 config
.rundir
.value
);
606 DBG("Removing %s", config
.pid_file_path
.value
);
607 (void) unlink(config
.pid_file_path
.value
);
609 DBG("Removing %s", config
.agent_port_file_path
.value
);
610 (void) unlink(config
.agent_port_file_path
.value
);
613 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
614 (void) unlink(kconsumer_data
.err_unix_sock_path
);
616 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
617 (void) rmdir(config
.kconsumerd_path
.value
);
619 /* ust consumerd 32 */
620 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
621 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
623 DBG("Removing directory %s", config
.consumerd32_path
.value
);
624 (void) rmdir(config
.consumerd32_path
.value
);
626 /* ust consumerd 64 */
627 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
628 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
630 DBG("Removing directory %s", config
.consumerd64_path
.value
);
631 (void) rmdir(config
.consumerd64_path
.value
);
633 DBG("Cleaning up all sessions");
635 /* Destroy session list mutex */
636 if (session_list_ptr
!= NULL
) {
637 pthread_mutex_destroy(&session_list_ptr
->lock
);
639 /* Cleanup ALL session */
640 cds_list_for_each_entry_safe(sess
, stmp
,
641 &session_list_ptr
->head
, list
) {
642 cmd_destroy_session(sess
, kernel_poll_pipe
[1],
643 notification_thread_handle
);
647 wait_consumer(&kconsumer_data
);
648 wait_consumer(&ustconsumer64_data
);
649 wait_consumer(&ustconsumer32_data
);
651 DBG("Cleaning up all agent apps");
652 agent_app_ht_clean();
654 DBG("Closing all UST sockets");
655 ust_app_clean_list();
656 buffer_reg_destroy_registries();
658 if (is_root
&& !config
.no_kernel
) {
659 DBG2("Closing kernel fd");
660 if (kernel_tracer_fd
>= 0) {
661 ret
= close(kernel_tracer_fd
);
666 DBG("Unloading kernel modules");
667 modprobe_remove_lttng_all();
671 close_consumer_sockets();
674 load_session_destroy_data(load_info
);
679 * We do NOT rmdir rundir because there are other processes
680 * using it, for instance lttng-relayd, which can start in
681 * parallel with this teardown.
686 * Cleanup the daemon's option data structures.
688 static void sessiond_cleanup_options(void)
690 DBG("Cleaning up options");
692 sessiond_config_fini(&config
);
694 run_as_destroy_worker();
698 * Send data on a unix socket using the liblttsessiondcomm API.
700 * Return lttcomm error code.
702 static int send_unix_sock(int sock
, void *buf
, size_t len
)
704 /* Check valid length */
709 return lttcomm_send_unix_sock(sock
, buf
, len
);
713 * Free memory of a command context structure.
715 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
717 DBG("Clean command context structure");
719 if ((*cmd_ctx
)->llm
) {
720 free((*cmd_ctx
)->llm
);
722 if ((*cmd_ctx
)->lsm
) {
723 free((*cmd_ctx
)->lsm
);
731 * Notify UST applications using the shm mmap futex.
733 static int notify_ust_apps(int active
)
737 DBG("Notifying applications of session daemon state: %d", active
);
739 /* See shm.c for this call implying mmap, shm and futex calls */
740 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
741 if (wait_shm_mmap
== NULL
) {
745 /* Wake waiting process */
746 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
748 /* Apps notified successfully */
756 * Setup the outgoing data buffer for the response (llm) by allocating the
757 * right amount of memory and copying the original information from the lsm
760 * Return 0 on success, negative value on error.
762 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
763 const void *payload_buf
, size_t payload_len
,
764 const void *cmd_header_buf
, size_t cmd_header_len
)
767 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
768 const size_t cmd_header_offset
= header_len
;
769 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
770 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
772 cmd_ctx
->llm
= zmalloc(total_msg_size
);
774 if (cmd_ctx
->llm
== NULL
) {
780 /* Copy common data */
781 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
782 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
783 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
784 cmd_ctx
->llm
->data_size
= payload_len
;
785 cmd_ctx
->lttng_msg_size
= total_msg_size
;
787 /* Copy command header */
788 if (cmd_header_len
) {
789 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
795 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
804 * Version of setup_lttng_msg() without command header.
806 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
807 void *payload_buf
, size_t payload_len
)
809 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
812 * Update the kernel poll set of all channel fd available over all tracing
813 * session. Add the wakeup pipe at the end of the set.
815 static int update_kernel_poll(struct lttng_poll_event
*events
)
818 struct ltt_session
*session
;
819 struct ltt_kernel_channel
*channel
;
821 DBG("Updating kernel poll set");
824 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
825 session_lock(session
);
826 if (session
->kernel_session
== NULL
) {
827 session_unlock(session
);
831 cds_list_for_each_entry(channel
,
832 &session
->kernel_session
->channel_list
.head
, list
) {
833 /* Add channel fd to the kernel poll set */
834 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
836 session_unlock(session
);
839 DBG("Channel fd %d added to kernel set", channel
->fd
);
841 session_unlock(session
);
843 session_unlock_list();
848 session_unlock_list();
853 * Find the channel fd from 'fd' over all tracing session. When found, check
854 * for new channel stream and send those stream fds to the kernel consumer.
856 * Useful for CPU hotplug feature.
858 static int update_kernel_stream(int fd
)
861 struct ltt_session
*session
;
862 struct ltt_kernel_session
*ksess
;
863 struct ltt_kernel_channel
*channel
;
865 DBG("Updating kernel streams for channel fd %d", fd
);
868 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
869 session_lock(session
);
870 if (session
->kernel_session
== NULL
) {
871 session_unlock(session
);
874 ksess
= session
->kernel_session
;
876 cds_list_for_each_entry(channel
,
877 &ksess
->channel_list
.head
, list
) {
878 struct lttng_ht_iter iter
;
879 struct consumer_socket
*socket
;
881 if (channel
->fd
!= fd
) {
884 DBG("Channel found, updating kernel streams");
885 ret
= kernel_open_channel_stream(channel
);
889 /* Update the stream global counter */
890 ksess
->stream_count_global
+= ret
;
893 * Have we already sent fds to the consumer? If yes, it
894 * means that tracing is started so it is safe to send
895 * our updated stream fds.
897 if (ksess
->consumer_fds_sent
!= 1
898 || ksess
->consumer
== NULL
) {
904 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
905 &iter
.iter
, socket
, node
.node
) {
906 pthread_mutex_lock(socket
->lock
);
907 ret
= kernel_consumer_send_channel_streams(socket
,
909 session
->output_traces
? 1 : 0);
910 pthread_mutex_unlock(socket
->lock
);
918 session_unlock(session
);
920 session_unlock_list();
924 session_unlock(session
);
925 session_unlock_list();
930 * For each tracing session, update newly registered apps. The session list
931 * lock MUST be acquired before calling this.
933 static void update_ust_app(int app_sock
)
935 struct ltt_session
*sess
, *stmp
;
937 /* Consumer is in an ERROR state. Stop any application update. */
938 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
939 /* Stop the update process since the consumer is dead. */
943 /* For all tracing session(s) */
944 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
948 if (!sess
->ust_session
) {
953 assert(app_sock
>= 0);
954 app
= ust_app_find_by_sock(app_sock
);
957 * Application can be unregistered before so
958 * this is possible hence simply stopping the
961 DBG3("UST app update failed to find app sock %d",
965 ust_app_global_update(sess
->ust_session
, app
);
969 session_unlock(sess
);
974 * This thread manage event coming from the kernel.
976 * Features supported in this thread:
979 static void *thread_manage_kernel(void *data
)
981 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
982 uint32_t revents
, nb_fd
;
984 struct lttng_poll_event events
;
986 DBG("[thread] Thread manage kernel started");
988 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
991 * This first step of the while is to clean this structure which could free
992 * non NULL pointers so initialize it before the loop.
994 lttng_poll_init(&events
);
996 if (testpoint(sessiond_thread_manage_kernel
)) {
997 goto error_testpoint
;
1000 health_code_update();
1002 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1003 goto error_testpoint
;
1007 health_code_update();
1009 if (update_poll_flag
== 1) {
1010 /* Clean events object. We are about to populate it again. */
1011 lttng_poll_clean(&events
);
1013 ret
= sessiond_set_thread_pollset(&events
, 2);
1015 goto error_poll_create
;
1018 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1023 /* This will add the available kernel channel if any. */
1024 ret
= update_kernel_poll(&events
);
1028 update_poll_flag
= 0;
1031 DBG("Thread kernel polling");
1033 /* Poll infinite value of time */
1035 health_poll_entry();
1036 ret
= lttng_poll_wait(&events
, -1);
1037 DBG("Thread kernel return from poll on %d fds",
1038 LTTNG_POLL_GETNB(&events
));
1042 * Restart interrupted system call.
1044 if (errno
== EINTR
) {
1048 } else if (ret
== 0) {
1049 /* Should not happen since timeout is infinite */
1050 ERR("Return value of poll is 0 with an infinite timeout.\n"
1051 "This should not have happened! Continuing...");
1057 for (i
= 0; i
< nb_fd
; i
++) {
1058 /* Fetch once the poll data */
1059 revents
= LTTNG_POLL_GETEV(&events
, i
);
1060 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1062 health_code_update();
1065 /* No activity for this FD (poll implementation). */
1069 /* Thread quit pipe has been closed. Killing thread. */
1070 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1076 /* Check for data on kernel pipe */
1077 if (revents
& LPOLLIN
) {
1078 if (pollfd
== kernel_poll_pipe
[0]) {
1079 (void) lttng_read(kernel_poll_pipe
[0],
1082 * Ret value is useless here, if this pipe gets any actions an
1083 * update is required anyway.
1085 update_poll_flag
= 1;
1089 * New CPU detected by the kernel. Adding kernel stream to
1090 * kernel session and updating the kernel consumer
1092 ret
= update_kernel_stream(pollfd
);
1098 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1099 update_poll_flag
= 1;
1102 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1110 lttng_poll_clean(&events
);
1113 utils_close_pipe(kernel_poll_pipe
);
1114 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1117 ERR("Health error occurred in %s", __func__
);
1118 WARN("Kernel thread died unexpectedly. "
1119 "Kernel tracing can continue but CPU hotplug is disabled.");
1121 health_unregister(health_sessiond
);
1122 DBG("Kernel thread dying");
1127 * Signal pthread condition of the consumer data that the thread.
1129 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1131 pthread_mutex_lock(&data
->cond_mutex
);
1134 * The state is set before signaling. It can be any value, it's the waiter
1135 * job to correctly interpret this condition variable associated to the
1136 * consumer pthread_cond.
1138 * A value of 0 means that the corresponding thread of the consumer data
1139 * was not started. 1 indicates that the thread has started and is ready
1140 * for action. A negative value means that there was an error during the
1143 data
->consumer_thread_is_ready
= state
;
1144 (void) pthread_cond_signal(&data
->cond
);
1146 pthread_mutex_unlock(&data
->cond_mutex
);
1150 * This thread manage the consumer error sent back to the session daemon.
1152 static void *thread_manage_consumer(void *data
)
1154 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1155 uint32_t revents
, nb_fd
;
1156 enum lttcomm_return_code code
;
1157 struct lttng_poll_event events
;
1158 struct consumer_data
*consumer_data
= data
;
1159 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1161 DBG("[thread] Manage consumer started");
1163 rcu_register_thread();
1164 rcu_thread_online();
1166 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1168 health_code_update();
1171 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1172 * metadata_sock. Nothing more will be added to this poll set.
1174 ret
= sessiond_set_thread_pollset(&events
, 3);
1180 * The error socket here is already in a listening state which was done
1181 * just before spawning this thread to avoid a race between the consumer
1182 * daemon exec trying to connect and the listen() call.
1184 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1189 health_code_update();
1191 /* Infinite blocking call, waiting for transmission */
1193 health_poll_entry();
1195 if (testpoint(sessiond_thread_manage_consumer
)) {
1199 ret
= lttng_poll_wait(&events
, -1);
1203 * Restart interrupted system call.
1205 if (errno
== EINTR
) {
1213 for (i
= 0; i
< nb_fd
; i
++) {
1214 /* Fetch once the poll data */
1215 revents
= LTTNG_POLL_GETEV(&events
, i
);
1216 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1218 health_code_update();
1221 /* No activity for this FD (poll implementation). */
1225 /* Thread quit pipe has been closed. Killing thread. */
1226 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1232 /* Event on the registration socket */
1233 if (pollfd
== consumer_data
->err_sock
) {
1234 if (revents
& LPOLLIN
) {
1236 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1237 ERR("consumer err socket poll error");
1240 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1246 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1252 * Set the CLOEXEC flag. Return code is useless because either way, the
1255 (void) utils_set_fd_cloexec(sock
);
1257 health_code_update();
1259 DBG2("Receiving code from consumer err_sock");
1261 /* Getting status code from kconsumerd */
1262 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1263 sizeof(enum lttcomm_return_code
));
1268 health_code_update();
1269 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1270 ERR("consumer error when waiting for SOCK_READY : %s",
1271 lttcomm_get_readable_code(-code
));
1275 /* Connect both command and metadata sockets. */
1276 consumer_data
->cmd_sock
=
1277 lttcomm_connect_unix_sock(
1278 consumer_data
->cmd_unix_sock_path
);
1279 consumer_data
->metadata_fd
=
1280 lttcomm_connect_unix_sock(
1281 consumer_data
->cmd_unix_sock_path
);
1282 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1283 PERROR("consumer connect cmd socket");
1284 /* On error, signal condition and quit. */
1285 signal_consumer_condition(consumer_data
, -1);
1289 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1291 /* Create metadata socket lock. */
1292 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1293 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1294 PERROR("zmalloc pthread mutex");
1297 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1299 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1300 DBG("Consumer metadata socket ready (fd: %d)",
1301 consumer_data
->metadata_fd
);
1304 * Remove the consumerd error sock since we've established a connection.
1306 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1311 /* Add new accepted error socket. */
1312 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1317 /* Add metadata socket that is successfully connected. */
1318 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1319 LPOLLIN
| LPOLLRDHUP
);
1324 health_code_update();
1327 * Transfer the write-end of the channel monitoring and rotate pipe
1328 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1329 * SET_CHANNEL_ROTATE_PIPE commands.
1331 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1332 if (!cmd_socket_wrapper
) {
1335 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1337 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1338 consumer_data
->channel_monitor_pipe
);
1343 ret
= consumer_send_channel_rotate_pipe(cmd_socket_wrapper
,
1344 consumer_data
->channel_rotate_pipe
);
1349 /* Discard the socket wrapper as it is no longer needed. */
1350 consumer_destroy_socket(cmd_socket_wrapper
);
1351 cmd_socket_wrapper
= NULL
;
1353 /* The thread is completely initialized, signal that it is ready. */
1354 signal_consumer_condition(consumer_data
, 1);
1356 /* Infinite blocking call, waiting for transmission */
1359 health_code_update();
1361 /* Exit the thread because the thread quit pipe has been triggered. */
1363 /* Not a health error. */
1368 health_poll_entry();
1369 ret
= lttng_poll_wait(&events
, -1);
1373 * Restart interrupted system call.
1375 if (errno
== EINTR
) {
1383 for (i
= 0; i
< nb_fd
; i
++) {
1384 /* Fetch once the poll data */
1385 revents
= LTTNG_POLL_GETEV(&events
, i
);
1386 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1388 health_code_update();
1391 /* No activity for this FD (poll implementation). */
1396 * Thread quit pipe has been triggered, flag that we should stop
1397 * but continue the current loop to handle potential data from
1400 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1402 if (pollfd
== sock
) {
1403 /* Event on the consumerd socket */
1404 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1405 && !(revents
& LPOLLIN
)) {
1406 ERR("consumer err socket second poll error");
1409 health_code_update();
1410 /* Wait for any kconsumerd error */
1411 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1412 sizeof(enum lttcomm_return_code
));
1414 ERR("consumer closed the command socket");
1418 ERR("consumer return code : %s",
1419 lttcomm_get_readable_code(-code
));
1422 } else if (pollfd
== consumer_data
->metadata_fd
) {
1423 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1424 && !(revents
& LPOLLIN
)) {
1425 ERR("consumer err metadata socket second poll error");
1428 /* UST metadata requests */
1429 ret
= ust_consumer_metadata_request(
1430 &consumer_data
->metadata_sock
);
1432 ERR("Handling metadata request");
1436 /* No need for an else branch all FDs are tested prior. */
1438 health_code_update();
1444 * We lock here because we are about to close the sockets and some other
1445 * thread might be using them so get exclusive access which will abort all
1446 * other consumer command by other threads.
1448 pthread_mutex_lock(&consumer_data
->lock
);
1450 /* Immediately set the consumerd state to stopped */
1451 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1452 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1453 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1454 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1455 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1457 /* Code flow error... */
1461 if (consumer_data
->err_sock
>= 0) {
1462 ret
= close(consumer_data
->err_sock
);
1466 consumer_data
->err_sock
= -1;
1468 if (consumer_data
->cmd_sock
>= 0) {
1469 ret
= close(consumer_data
->cmd_sock
);
1473 consumer_data
->cmd_sock
= -1;
1475 if (consumer_data
->metadata_sock
.fd_ptr
&&
1476 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1477 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1489 unlink(consumer_data
->err_unix_sock_path
);
1490 unlink(consumer_data
->cmd_unix_sock_path
);
1491 pthread_mutex_unlock(&consumer_data
->lock
);
1493 /* Cleanup metadata socket mutex. */
1494 if (consumer_data
->metadata_sock
.lock
) {
1495 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1496 free(consumer_data
->metadata_sock
.lock
);
1498 lttng_poll_clean(&events
);
1500 if (cmd_socket_wrapper
) {
1501 consumer_destroy_socket(cmd_socket_wrapper
);
1506 ERR("Health error occurred in %s", __func__
);
1508 health_unregister(health_sessiond
);
1509 DBG("consumer thread cleanup completed");
1511 rcu_thread_offline();
1512 rcu_unregister_thread();
1518 * This thread receives application command sockets (FDs) on the
1519 * apps_cmd_pipe and waits (polls) on them until they are closed
1520 * or an error occurs.
1522 * At that point, it flushes the data (tracing and metadata) associated
1523 * with this application and tears down ust app sessions and other
1524 * associated data structures through ust_app_unregister().
1526 * Note that this thread never sends commands to the applications
1527 * through the command sockets; it merely listens for hang-ups
1528 * and errors on those sockets and cleans-up as they occur.
1530 static void *thread_manage_apps(void *data
)
1532 int i
, ret
, pollfd
, err
= -1;
1534 uint32_t revents
, nb_fd
;
1535 struct lttng_poll_event events
;
1537 DBG("[thread] Manage application started");
1539 rcu_register_thread();
1540 rcu_thread_online();
1542 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1544 if (testpoint(sessiond_thread_manage_apps
)) {
1545 goto error_testpoint
;
1548 health_code_update();
1550 ret
= sessiond_set_thread_pollset(&events
, 2);
1552 goto error_poll_create
;
1555 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1560 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1564 health_code_update();
1567 DBG("Apps thread polling");
1569 /* Inifinite blocking call, waiting for transmission */
1571 health_poll_entry();
1572 ret
= lttng_poll_wait(&events
, -1);
1573 DBG("Apps thread return from poll on %d fds",
1574 LTTNG_POLL_GETNB(&events
));
1578 * Restart interrupted system call.
1580 if (errno
== EINTR
) {
1588 for (i
= 0; i
< nb_fd
; i
++) {
1589 /* Fetch once the poll data */
1590 revents
= LTTNG_POLL_GETEV(&events
, i
);
1591 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1593 health_code_update();
1596 /* No activity for this FD (poll implementation). */
1600 /* Thread quit pipe has been closed. Killing thread. */
1601 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1607 /* Inspect the apps cmd pipe */
1608 if (pollfd
== apps_cmd_pipe
[0]) {
1609 if (revents
& LPOLLIN
) {
1613 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1614 if (size_ret
< sizeof(sock
)) {
1615 PERROR("read apps cmd pipe");
1619 health_code_update();
1622 * Since this is a command socket (write then read),
1623 * we only monitor the error events of the socket.
1625 ret
= lttng_poll_add(&events
, sock
,
1626 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1631 DBG("Apps with sock %d added to poll set", sock
);
1632 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1633 ERR("Apps command pipe error");
1636 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1641 * At this point, we know that a registered application made
1642 * the event at poll_wait.
1644 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1645 /* Removing from the poll set */
1646 ret
= lttng_poll_del(&events
, pollfd
);
1651 /* Socket closed on remote end. */
1652 ust_app_unregister(pollfd
);
1654 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1659 health_code_update();
1665 lttng_poll_clean(&events
);
1668 utils_close_pipe(apps_cmd_pipe
);
1669 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1672 * We don't clean the UST app hash table here since already registered
1673 * applications can still be controlled so let them be until the session
1674 * daemon dies or the applications stop.
1679 ERR("Health error occurred in %s", __func__
);
1681 health_unregister(health_sessiond
);
1682 DBG("Application communication apps thread cleanup complete");
1683 rcu_thread_offline();
1684 rcu_unregister_thread();
1689 * Send a socket to a thread This is called from the dispatch UST registration
1690 * thread once all sockets are set for the application.
1692 * The sock value can be invalid, we don't really care, the thread will handle
1693 * it and make the necessary cleanup if so.
1695 * On success, return 0 else a negative value being the errno message of the
1698 static int send_socket_to_thread(int fd
, int sock
)
1703 * It's possible that the FD is set as invalid with -1 concurrently just
1704 * before calling this function being a shutdown state of the thread.
1711 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1712 if (ret
< sizeof(sock
)) {
1713 PERROR("write apps pipe %d", fd
);
1720 /* All good. Don't send back the write positive ret value. */
1727 * Sanitize the wait queue of the dispatch registration thread meaning removing
1728 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1729 * notify socket is never received.
1731 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1733 int ret
, nb_fd
= 0, i
;
1734 unsigned int fd_added
= 0;
1735 struct lttng_poll_event events
;
1736 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1740 lttng_poll_init(&events
);
1742 /* Just skip everything for an empty queue. */
1743 if (!wait_queue
->count
) {
1747 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1752 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1753 &wait_queue
->head
, head
) {
1754 assert(wait_node
->app
);
1755 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1756 LPOLLHUP
| LPOLLERR
);
1769 * Poll but don't block so we can quickly identify the faulty events and
1770 * clean them afterwards from the wait queue.
1772 ret
= lttng_poll_wait(&events
, 0);
1778 for (i
= 0; i
< nb_fd
; i
++) {
1779 /* Get faulty FD. */
1780 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1781 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1784 /* No activity for this FD (poll implementation). */
1788 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1789 &wait_queue
->head
, head
) {
1790 if (pollfd
== wait_node
->app
->sock
&&
1791 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1792 cds_list_del(&wait_node
->head
);
1793 wait_queue
->count
--;
1794 ust_app_destroy(wait_node
->app
);
1797 * Silence warning of use-after-free in
1798 * cds_list_for_each_entry_safe which uses
1799 * __typeof__(*wait_node).
1804 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1811 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1815 lttng_poll_clean(&events
);
1819 lttng_poll_clean(&events
);
1821 ERR("Unable to sanitize wait queue");
1826 * Dispatch request from the registration threads to the application
1827 * communication thread.
1829 static void *thread_dispatch_ust_registration(void *data
)
1832 struct cds_wfcq_node
*node
;
1833 struct ust_command
*ust_cmd
= NULL
;
1834 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1835 struct ust_reg_wait_queue wait_queue
= {
1839 rcu_register_thread();
1841 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1843 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1844 goto error_testpoint
;
1847 health_code_update();
1849 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1851 DBG("[thread] Dispatch UST command started");
1854 health_code_update();
1856 /* Atomically prepare the queue futex */
1857 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1859 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1864 struct ust_app
*app
= NULL
;
1868 * Make sure we don't have node(s) that have hung up before receiving
1869 * the notify socket. This is to clean the list in order to avoid
1870 * memory leaks from notify socket that are never seen.
1872 sanitize_wait_queue(&wait_queue
);
1874 health_code_update();
1875 /* Dequeue command for registration */
1876 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1878 DBG("Woken up but nothing in the UST command queue");
1879 /* Continue thread execution */
1883 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1885 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1886 " gid:%d sock:%d name:%s (version %d.%d)",
1887 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1888 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1889 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1890 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1892 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1893 wait_node
= zmalloc(sizeof(*wait_node
));
1895 PERROR("zmalloc wait_node dispatch");
1896 ret
= close(ust_cmd
->sock
);
1898 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1900 lttng_fd_put(LTTNG_FD_APPS
, 1);
1904 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1906 /* Create application object if socket is CMD. */
1907 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1909 if (!wait_node
->app
) {
1910 ret
= close(ust_cmd
->sock
);
1912 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1914 lttng_fd_put(LTTNG_FD_APPS
, 1);
1920 * Add application to the wait queue so we can set the notify
1921 * socket before putting this object in the global ht.
1923 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1928 * We have to continue here since we don't have the notify
1929 * socket and the application MUST be added to the hash table
1930 * only at that moment.
1935 * Look for the application in the local wait queue and set the
1936 * notify socket if found.
1938 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1939 &wait_queue
.head
, head
) {
1940 health_code_update();
1941 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1942 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1943 cds_list_del(&wait_node
->head
);
1945 app
= wait_node
->app
;
1947 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1953 * With no application at this stage the received socket is
1954 * basically useless so close it before we free the cmd data
1955 * structure for good.
1958 ret
= close(ust_cmd
->sock
);
1960 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1962 lttng_fd_put(LTTNG_FD_APPS
, 1);
1969 * @session_lock_list
1971 * Lock the global session list so from the register up to the
1972 * registration done message, no thread can see the application
1973 * and change its state.
1975 session_lock_list();
1979 * Add application to the global hash table. This needs to be
1980 * done before the update to the UST registry can locate the
1985 /* Set app version. This call will print an error if needed. */
1986 (void) ust_app_version(app
);
1988 /* Send notify socket through the notify pipe. */
1989 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1993 session_unlock_list();
1995 * No notify thread, stop the UST tracing. However, this is
1996 * not an internal error of the this thread thus setting
1997 * the health error code to a normal exit.
2004 * Update newly registered application with the tracing
2005 * registry info already enabled information.
2007 update_ust_app(app
->sock
);
2010 * Don't care about return value. Let the manage apps threads
2011 * handle app unregistration upon socket close.
2013 (void) ust_app_register_done(app
);
2016 * Even if the application socket has been closed, send the app
2017 * to the thread and unregistration will take place at that
2020 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2023 session_unlock_list();
2025 * No apps. thread, stop the UST tracing. However, this is
2026 * not an internal error of the this thread thus setting
2027 * the health error code to a normal exit.
2034 session_unlock_list();
2036 } while (node
!= NULL
);
2038 health_poll_entry();
2039 /* Futex wait on queue. Blocking call on futex() */
2040 futex_nto1_wait(&ust_cmd_queue
.futex
);
2043 /* Normal exit, no error */
2047 /* Clean up wait queue. */
2048 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2049 &wait_queue
.head
, head
) {
2050 cds_list_del(&wait_node
->head
);
2055 /* Empty command queue. */
2057 /* Dequeue command for registration */
2058 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2062 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2063 ret
= close(ust_cmd
->sock
);
2065 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2067 lttng_fd_put(LTTNG_FD_APPS
, 1);
2072 DBG("Dispatch thread dying");
2075 ERR("Health error occurred in %s", __func__
);
2077 health_unregister(health_sessiond
);
2078 rcu_unregister_thread();
2083 * This thread manage application registration.
2085 static void *thread_registration_apps(void *data
)
2087 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2088 uint32_t revents
, nb_fd
;
2089 struct lttng_poll_event events
;
2091 * Get allocated in this thread, enqueued to a global queue, dequeued and
2092 * freed in the manage apps thread.
2094 struct ust_command
*ust_cmd
= NULL
;
2096 DBG("[thread] Manage application registration started");
2098 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2100 if (testpoint(sessiond_thread_registration_apps
)) {
2101 goto error_testpoint
;
2104 ret
= lttcomm_listen_unix_sock(apps_sock
);
2110 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2111 * more will be added to this poll set.
2113 ret
= sessiond_set_thread_pollset(&events
, 2);
2115 goto error_create_poll
;
2118 /* Add the application registration socket */
2119 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2121 goto error_poll_add
;
2124 /* Notify all applications to register */
2125 ret
= notify_ust_apps(1);
2127 ERR("Failed to notify applications or create the wait shared memory.\n"
2128 "Execution continues but there might be problem for already\n"
2129 "running applications that wishes to register.");
2133 DBG("Accepting application registration");
2135 /* Inifinite blocking call, waiting for transmission */
2137 health_poll_entry();
2138 ret
= lttng_poll_wait(&events
, -1);
2142 * Restart interrupted system call.
2144 if (errno
== EINTR
) {
2152 for (i
= 0; i
< nb_fd
; i
++) {
2153 health_code_update();
2155 /* Fetch once the poll data */
2156 revents
= LTTNG_POLL_GETEV(&events
, i
);
2157 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2160 /* No activity for this FD (poll implementation). */
2164 /* Thread quit pipe has been closed. Killing thread. */
2165 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2171 /* Event on the registration socket */
2172 if (pollfd
== apps_sock
) {
2173 if (revents
& LPOLLIN
) {
2174 sock
= lttcomm_accept_unix_sock(apps_sock
);
2180 * Set socket timeout for both receiving and ending.
2181 * app_socket_timeout is in seconds, whereas
2182 * lttcomm_setsockopt_rcv_timeout and
2183 * lttcomm_setsockopt_snd_timeout expect msec as
2186 if (config
.app_socket_timeout
>= 0) {
2187 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2188 config
.app_socket_timeout
* 1000);
2189 (void) lttcomm_setsockopt_snd_timeout(sock
,
2190 config
.app_socket_timeout
* 1000);
2194 * Set the CLOEXEC flag. Return code is useless because
2195 * either way, the show must go on.
2197 (void) utils_set_fd_cloexec(sock
);
2199 /* Create UST registration command for enqueuing */
2200 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2201 if (ust_cmd
== NULL
) {
2202 PERROR("ust command zmalloc");
2211 * Using message-based transmissions to ensure we don't
2212 * have to deal with partially received messages.
2214 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2216 ERR("Exhausted file descriptors allowed for applications.");
2226 health_code_update();
2227 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2230 /* Close socket of the application. */
2235 lttng_fd_put(LTTNG_FD_APPS
, 1);
2239 health_code_update();
2241 ust_cmd
->sock
= sock
;
2244 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2245 " gid:%d sock:%d name:%s (version %d.%d)",
2246 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2247 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2248 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2249 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2252 * Lock free enqueue the registration request. The red pill
2253 * has been taken! This apps will be part of the *system*.
2255 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2258 * Wake the registration queue futex. Implicit memory
2259 * barrier with the exchange in cds_wfcq_enqueue.
2261 futex_nto1_wake(&ust_cmd_queue
.futex
);
2262 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2263 ERR("Register apps socket poll error");
2266 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2275 /* Notify that the registration thread is gone */
2278 if (apps_sock
>= 0) {
2279 ret
= close(apps_sock
);
2289 lttng_fd_put(LTTNG_FD_APPS
, 1);
2291 unlink(config
.apps_unix_sock_path
.value
);
2294 lttng_poll_clean(&events
);
2298 DBG("UST Registration thread cleanup complete");
2301 ERR("Health error occurred in %s", __func__
);
2303 health_unregister(health_sessiond
);
2309 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2310 * exec or it will fails.
2312 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2315 struct timespec timeout
;
2318 * Make sure we set the readiness flag to 0 because we are NOT ready.
2319 * This access to consumer_thread_is_ready does not need to be
2320 * protected by consumer_data.cond_mutex (yet) since the consumer
2321 * management thread has not been started at this point.
2323 consumer_data
->consumer_thread_is_ready
= 0;
2325 /* Setup pthread condition */
2326 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2329 PERROR("pthread_condattr_init consumer data");
2334 * Set the monotonic clock in order to make sure we DO NOT jump in time
2335 * between the clock_gettime() call and the timedwait call. See bug #324
2336 * for a more details and how we noticed it.
2338 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2341 PERROR("pthread_condattr_setclock consumer data");
2345 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2348 PERROR("pthread_cond_init consumer data");
2352 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2353 thread_manage_consumer
, consumer_data
);
2356 PERROR("pthread_create consumer");
2361 /* We are about to wait on a pthread condition */
2362 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2364 /* Get time for sem_timedwait absolute timeout */
2365 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2367 * Set the timeout for the condition timed wait even if the clock gettime
2368 * call fails since we might loop on that call and we want to avoid to
2369 * increment the timeout too many times.
2371 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2374 * The following loop COULD be skipped in some conditions so this is why we
2375 * set ret to 0 in order to make sure at least one round of the loop is
2381 * Loop until the condition is reached or when a timeout is reached. Note
2382 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2383 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2384 * possible. This loop does not take any chances and works with both of
2387 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2388 if (clock_ret
< 0) {
2389 PERROR("clock_gettime spawn consumer");
2390 /* Infinite wait for the consumerd thread to be ready */
2391 ret
= pthread_cond_wait(&consumer_data
->cond
,
2392 &consumer_data
->cond_mutex
);
2394 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2395 &consumer_data
->cond_mutex
, &timeout
);
2399 /* Release the pthread condition */
2400 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2404 if (ret
== ETIMEDOUT
) {
2408 * Call has timed out so we kill the kconsumerd_thread and return
2411 ERR("Condition timed out. The consumer thread was never ready."
2413 pth_ret
= pthread_cancel(consumer_data
->thread
);
2415 PERROR("pthread_cancel consumer thread");
2418 PERROR("pthread_cond_wait failed consumer thread");
2420 /* Caller is expecting a negative value on failure. */
2425 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2426 if (consumer_data
->pid
== 0) {
2427 ERR("Consumerd did not start");
2428 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2431 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2440 * Join consumer thread
2442 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2446 /* Consumer pid must be a real one. */
2447 if (consumer_data
->pid
> 0) {
2449 ret
= kill(consumer_data
->pid
, SIGTERM
);
2451 PERROR("Error killing consumer daemon");
2454 return pthread_join(consumer_data
->thread
, &status
);
2461 * Fork and exec a consumer daemon (consumerd).
2463 * Return pid if successful else -1.
2465 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2469 const char *consumer_to_use
;
2470 const char *verbosity
;
2473 DBG("Spawning consumerd");
2480 if (config
.verbose_consumer
) {
2481 verbosity
= "--verbose";
2482 } else if (lttng_opt_quiet
) {
2483 verbosity
= "--quiet";
2488 switch (consumer_data
->type
) {
2489 case LTTNG_CONSUMER_KERNEL
:
2491 * Find out which consumerd to execute. We will first try the
2492 * 64-bit path, then the sessiond's installation directory, and
2493 * fallback on the 32-bit one,
2495 DBG3("Looking for a kernel consumer at these locations:");
2496 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2497 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2498 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2499 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2500 DBG3("Found location #1");
2501 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2502 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2503 DBG3("Found location #2");
2504 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2505 } else if (config
.consumerd32_bin_path
.value
&&
2506 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2507 DBG3("Found location #3");
2508 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2510 DBG("Could not find any valid consumerd executable");
2514 DBG("Using kernel consumer at: %s", consumer_to_use
);
2515 (void) execl(consumer_to_use
,
2516 "lttng-consumerd", verbosity
, "-k",
2517 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2518 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2519 "--group", config
.tracing_group_name
.value
,
2522 case LTTNG_CONSUMER64_UST
:
2524 if (config
.consumerd64_lib_dir
.value
) {
2529 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2533 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2534 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2539 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2540 if (tmp
[0] != '\0') {
2541 strcat(tmpnew
, ":");
2542 strcat(tmpnew
, tmp
);
2544 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2551 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2552 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2553 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2554 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2555 "--group", config
.tracing_group_name
.value
,
2559 case LTTNG_CONSUMER32_UST
:
2561 if (config
.consumerd32_lib_dir
.value
) {
2566 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2570 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2571 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2576 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2577 if (tmp
[0] != '\0') {
2578 strcat(tmpnew
, ":");
2579 strcat(tmpnew
, tmp
);
2581 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2588 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2589 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2590 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2591 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2592 "--group", config
.tracing_group_name
.value
,
2597 ERR("unknown consumer type");
2601 PERROR("Consumer execl()");
2603 /* Reaching this point, we got a failure on our execl(). */
2605 } else if (pid
> 0) {
2608 PERROR("start consumer fork");
2616 * Spawn the consumerd daemon and session daemon thread.
2618 static int start_consumerd(struct consumer_data
*consumer_data
)
2623 * Set the listen() state on the socket since there is a possible race
2624 * between the exec() of the consumer daemon and this call if place in the
2625 * consumer thread. See bug #366 for more details.
2627 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2632 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2633 if (consumer_data
->pid
!= 0) {
2634 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2638 ret
= spawn_consumerd(consumer_data
);
2640 ERR("Spawning consumerd failed");
2641 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2645 /* Setting up the consumer_data pid */
2646 consumer_data
->pid
= ret
;
2647 DBG2("Consumer pid %d", consumer_data
->pid
);
2648 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2650 DBG2("Spawning consumer control thread");
2651 ret
= spawn_consumer_thread(consumer_data
);
2653 ERR("Fatal error spawning consumer control thread");
2661 /* Cleanup already created sockets on error. */
2662 if (consumer_data
->err_sock
>= 0) {
2665 err
= close(consumer_data
->err_sock
);
2667 PERROR("close consumer data error socket");
2674 * Setup necessary data for kernel tracer action.
2676 static int init_kernel_tracer(void)
2680 /* Modprobe lttng kernel modules */
2681 ret
= modprobe_lttng_control();
2686 /* Open debugfs lttng */
2687 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2688 if (kernel_tracer_fd
< 0) {
2689 DBG("Failed to open %s", module_proc_lttng
);
2693 /* Validate kernel version */
2694 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2695 &kernel_tracer_abi_version
);
2700 ret
= modprobe_lttng_data();
2705 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2712 WARN("Kernel tracer does not support buffer monitoring. "
2713 "The monitoring timer of channels in the kernel domain "
2714 "will be set to 0 (disabled).");
2717 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2721 modprobe_remove_lttng_control();
2722 ret
= close(kernel_tracer_fd
);
2726 kernel_tracer_fd
= -1;
2727 return LTTNG_ERR_KERN_VERSION
;
2730 ret
= close(kernel_tracer_fd
);
2736 modprobe_remove_lttng_control();
2739 WARN("No kernel tracer available");
2740 kernel_tracer_fd
= -1;
2742 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2744 return LTTNG_ERR_KERN_NA
;
2750 * Copy consumer output from the tracing session to the domain session. The
2751 * function also applies the right modification on a per domain basis for the
2752 * trace files destination directory.
2754 * Should *NOT* be called with RCU read-side lock held.
2756 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2759 const char *dir_name
;
2760 struct consumer_output
*consumer
;
2763 assert(session
->consumer
);
2766 case LTTNG_DOMAIN_KERNEL
:
2767 DBG3("Copying tracing session consumer output in kernel session");
2769 * XXX: We should audit the session creation and what this function
2770 * does "extra" in order to avoid a destroy since this function is used
2771 * in the domain session creation (kernel and ust) only. Same for UST
2774 if (session
->kernel_session
->consumer
) {
2775 consumer_output_put(session
->kernel_session
->consumer
);
2777 session
->kernel_session
->consumer
=
2778 consumer_copy_output(session
->consumer
);
2779 /* Ease our life a bit for the next part */
2780 consumer
= session
->kernel_session
->consumer
;
2781 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2783 case LTTNG_DOMAIN_JUL
:
2784 case LTTNG_DOMAIN_LOG4J
:
2785 case LTTNG_DOMAIN_PYTHON
:
2786 case LTTNG_DOMAIN_UST
:
2787 DBG3("Copying tracing session consumer output in UST session");
2788 if (session
->ust_session
->consumer
) {
2789 consumer_output_put(session
->ust_session
->consumer
);
2791 session
->ust_session
->consumer
=
2792 consumer_copy_output(session
->consumer
);
2793 /* Ease our life a bit for the next part */
2794 consumer
= session
->ust_session
->consumer
;
2795 dir_name
= DEFAULT_UST_TRACE_DIR
;
2798 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2802 /* Append correct directory to subdir */
2803 strncat(consumer
->subdir
, dir_name
,
2804 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2805 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2814 * Create an UST session and add it to the session ust list.
2816 * Should *NOT* be called with RCU read-side lock held.
2818 static int create_ust_session(struct ltt_session
*session
,
2819 struct lttng_domain
*domain
)
2822 struct ltt_ust_session
*lus
= NULL
;
2826 assert(session
->consumer
);
2828 switch (domain
->type
) {
2829 case LTTNG_DOMAIN_JUL
:
2830 case LTTNG_DOMAIN_LOG4J
:
2831 case LTTNG_DOMAIN_PYTHON
:
2832 case LTTNG_DOMAIN_UST
:
2835 ERR("Unknown UST domain on create session %d", domain
->type
);
2836 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2840 DBG("Creating UST session");
2842 lus
= trace_ust_create_session(session
->id
);
2844 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2848 lus
->uid
= session
->uid
;
2849 lus
->gid
= session
->gid
;
2850 lus
->output_traces
= session
->output_traces
;
2851 lus
->snapshot_mode
= session
->snapshot_mode
;
2852 lus
->live_timer_interval
= session
->live_timer
;
2853 session
->ust_session
= lus
;
2854 if (session
->shm_path
[0]) {
2855 strncpy(lus
->root_shm_path
, session
->shm_path
,
2856 sizeof(lus
->root_shm_path
));
2857 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2858 strncpy(lus
->shm_path
, session
->shm_path
,
2859 sizeof(lus
->shm_path
));
2860 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2861 strncat(lus
->shm_path
, "/ust",
2862 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2864 /* Copy session output to the newly created UST session */
2865 ret
= copy_session_consumer(domain
->type
, session
);
2866 if (ret
!= LTTNG_OK
) {
2874 session
->ust_session
= NULL
;
2879 * Create a kernel tracer session then create the default channel.
2881 static int create_kernel_session(struct ltt_session
*session
)
2885 DBG("Creating kernel session");
2887 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2889 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2893 /* Code flow safety */
2894 assert(session
->kernel_session
);
2896 /* Copy session output to the newly created Kernel session */
2897 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2898 if (ret
!= LTTNG_OK
) {
2902 session
->kernel_session
->uid
= session
->uid
;
2903 session
->kernel_session
->gid
= session
->gid
;
2904 session
->kernel_session
->output_traces
= session
->output_traces
;
2905 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2910 trace_kernel_destroy_session(session
->kernel_session
);
2911 session
->kernel_session
= NULL
;
2916 * Count number of session permitted by uid/gid.
2918 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2921 struct ltt_session
*session
;
2923 DBG("Counting number of available session for UID %d GID %d",
2925 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2927 * Only list the sessions the user can control.
2929 if (!session_access_ok(session
, uid
, gid
)) {
2937 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2938 int *sock_error
, struct lttng_event
*event
)
2941 struct lttng_userspace_probe_location
*probe_location
;
2942 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2943 struct lttng_dynamic_buffer probe_location_buffer
;
2944 struct lttng_buffer_view buffer_view
;
2947 * Create a buffer to store the serialized version of the probe
2950 lttng_dynamic_buffer_init(&probe_location_buffer
);
2951 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2952 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2954 ret
= LTTNG_ERR_NOMEM
;
2959 * Receive the probe location.
2961 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2962 probe_location_buffer
.size
);
2964 DBG("Nothing recv() from client var len data... continuing");
2966 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2967 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2971 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2972 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2975 * Extract the probe location from the serialized version.
2977 ret
= lttng_userspace_probe_location_create_from_buffer(
2978 &buffer_view
, &probe_location
);
2980 WARN("Failed to create a userspace probe location from the received buffer");
2981 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2982 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2987 * Receive the file descriptor to the target binary from the client.
2989 DBG("Receiving userspace probe target FD from client ...");
2990 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2992 DBG("Nothing recv() from client userspace probe fd... continuing");
2994 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2999 * Set the file descriptor received from the client through the unix
3000 * socket in the probe location.
3002 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
3004 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
3009 * From the kernel tracer's perspective, all userspace probe event types
3010 * are all the same: a file and an offset.
3012 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
3013 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
3014 ret
= lttng_userspace_probe_location_function_set_binary_fd(
3015 probe_location
, fd
);
3017 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
3018 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
3019 probe_location
, fd
);
3022 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
3027 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
3031 /* Attach the probe location to the event. */
3032 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
3034 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
3038 lttng_dynamic_buffer_reset(&probe_location_buffer
);
3044 * Check if the current kernel tracer supports the session rotation feature.
3045 * Return 1 if it does, 0 otherwise.
3047 static int check_rotate_compatible(void)
3051 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
3052 DBG("Kernel tracer version is not compatible with the rotation feature");
3060 * Process the command requested by the lttng client within the command
3061 * context structure. This function make sure that the return structure (llm)
3062 * is set and ready for transmission before returning.
3064 * Return any error encountered or 0 for success.
3066 * "sock" is only used for special-case var. len data.
3068 * Should *NOT* be called with RCU read-side lock held.
3070 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
3074 int need_tracing_session
= 1;
3077 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
3079 assert(!rcu_read_ongoing());
3083 switch (cmd_ctx
->lsm
->cmd_type
) {
3084 case LTTNG_CREATE_SESSION
:
3085 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3086 case LTTNG_CREATE_SESSION_LIVE
:
3087 case LTTNG_DESTROY_SESSION
:
3088 case LTTNG_LIST_SESSIONS
:
3089 case LTTNG_LIST_DOMAINS
:
3090 case LTTNG_START_TRACE
:
3091 case LTTNG_STOP_TRACE
:
3092 case LTTNG_DATA_PENDING
:
3093 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3094 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3095 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3096 case LTTNG_SNAPSHOT_RECORD
:
3097 case LTTNG_SAVE_SESSION
:
3098 case LTTNG_SET_SESSION_SHM_PATH
:
3099 case LTTNG_REGENERATE_METADATA
:
3100 case LTTNG_REGENERATE_STATEDUMP
:
3101 case LTTNG_REGISTER_TRIGGER
:
3102 case LTTNG_UNREGISTER_TRIGGER
:
3103 case LTTNG_ROTATE_SESSION
:
3104 case LTTNG_ROTATION_GET_INFO
:
3105 case LTTNG_ROTATION_SET_SCHEDULE
:
3106 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
3113 if (config
.no_kernel
&& need_domain
3114 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3116 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3118 ret
= LTTNG_ERR_KERN_NA
;
3123 /* Deny register consumer if we already have a spawned consumer. */
3124 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3125 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3126 if (kconsumer_data
.pid
> 0) {
3127 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3128 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3131 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3135 * Check for command that don't needs to allocate a returned payload. We do
3136 * this here so we don't have to make the call for no payload at each
3139 switch(cmd_ctx
->lsm
->cmd_type
) {
3140 case LTTNG_LIST_SESSIONS
:
3141 case LTTNG_LIST_TRACEPOINTS
:
3142 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3143 case LTTNG_LIST_DOMAINS
:
3144 case LTTNG_LIST_CHANNELS
:
3145 case LTTNG_LIST_EVENTS
:
3146 case LTTNG_LIST_SYSCALLS
:
3147 case LTTNG_LIST_TRACKER_PIDS
:
3148 case LTTNG_DATA_PENDING
:
3149 case LTTNG_ROTATE_SESSION
:
3150 case LTTNG_ROTATION_GET_INFO
:
3151 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
3154 /* Setup lttng message with no payload */
3155 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3157 /* This label does not try to unlock the session */
3158 goto init_setup_error
;
3162 /* Commands that DO NOT need a session. */
3163 switch (cmd_ctx
->lsm
->cmd_type
) {
3164 case LTTNG_CREATE_SESSION
:
3165 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3166 case LTTNG_CREATE_SESSION_LIVE
:
3167 case LTTNG_LIST_SESSIONS
:
3168 case LTTNG_LIST_TRACEPOINTS
:
3169 case LTTNG_LIST_SYSCALLS
:
3170 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3171 case LTTNG_SAVE_SESSION
:
3172 case LTTNG_REGISTER_TRIGGER
:
3173 case LTTNG_UNREGISTER_TRIGGER
:
3174 need_tracing_session
= 0;
3177 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3179 * We keep the session list lock across _all_ commands
3180 * for now, because the per-session lock does not
3181 * handle teardown properly.
3183 session_lock_list();
3184 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3185 if (cmd_ctx
->session
== NULL
) {
3186 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3189 /* Acquire lock for the session */
3190 session_lock(cmd_ctx
->session
);
3196 * Commands that need a valid session but should NOT create one if none
3197 * exists. Instead of creating one and destroying it when the command is
3198 * handled, process that right before so we save some round trip in useless
3201 switch (cmd_ctx
->lsm
->cmd_type
) {
3202 case LTTNG_DISABLE_CHANNEL
:
3203 case LTTNG_DISABLE_EVENT
:
3204 switch (cmd_ctx
->lsm
->domain
.type
) {
3205 case LTTNG_DOMAIN_KERNEL
:
3206 if (!cmd_ctx
->session
->kernel_session
) {
3207 ret
= LTTNG_ERR_NO_CHANNEL
;
3211 case LTTNG_DOMAIN_JUL
:
3212 case LTTNG_DOMAIN_LOG4J
:
3213 case LTTNG_DOMAIN_PYTHON
:
3214 case LTTNG_DOMAIN_UST
:
3215 if (!cmd_ctx
->session
->ust_session
) {
3216 ret
= LTTNG_ERR_NO_CHANNEL
;
3221 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3233 * Check domain type for specific "pre-action".
3235 switch (cmd_ctx
->lsm
->domain
.type
) {
3236 case LTTNG_DOMAIN_KERNEL
:
3238 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3242 /* Kernel tracer check */
3243 if (kernel_tracer_fd
== -1) {
3244 /* Basically, load kernel tracer modules */
3245 ret
= init_kernel_tracer();
3251 /* Consumer is in an ERROR state. Report back to client */
3252 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3253 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3257 /* Need a session for kernel command */
3258 if (need_tracing_session
) {
3259 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3260 ret
= create_kernel_session(cmd_ctx
->session
);
3262 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3267 /* Start the kernel consumer daemon */
3268 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3269 if (kconsumer_data
.pid
== 0 &&
3270 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3271 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3272 ret
= start_consumerd(&kconsumer_data
);
3274 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3277 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3279 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3283 * The consumer was just spawned so we need to add the socket to
3284 * the consumer output of the session if exist.
3286 ret
= consumer_create_socket(&kconsumer_data
,
3287 cmd_ctx
->session
->kernel_session
->consumer
);
3294 case LTTNG_DOMAIN_JUL
:
3295 case LTTNG_DOMAIN_LOG4J
:
3296 case LTTNG_DOMAIN_PYTHON
:
3297 case LTTNG_DOMAIN_UST
:
3299 if (!ust_app_supported()) {
3300 ret
= LTTNG_ERR_NO_UST
;
3303 /* Consumer is in an ERROR state. Report back to client */
3304 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3305 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3309 if (need_tracing_session
) {
3310 /* Create UST session if none exist. */
3311 if (cmd_ctx
->session
->ust_session
== NULL
) {
3312 ret
= create_ust_session(cmd_ctx
->session
,
3313 &cmd_ctx
->lsm
->domain
);
3314 if (ret
!= LTTNG_OK
) {
3319 /* Start the UST consumer daemons */
3321 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3322 if (config
.consumerd64_bin_path
.value
&&
3323 ustconsumer64_data
.pid
== 0 &&
3324 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3325 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3326 ret
= start_consumerd(&ustconsumer64_data
);
3328 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3329 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3333 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3334 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3336 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3340 * Setup socket for consumer 64 bit. No need for atomic access
3341 * since it was set above and can ONLY be set in this thread.
3343 ret
= consumer_create_socket(&ustconsumer64_data
,
3344 cmd_ctx
->session
->ust_session
->consumer
);
3350 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3351 if (config
.consumerd32_bin_path
.value
&&
3352 ustconsumer32_data
.pid
== 0 &&
3353 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3354 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3355 ret
= start_consumerd(&ustconsumer32_data
);
3357 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3358 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3362 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3363 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3365 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3369 * Setup socket for consumer 64 bit. No need for atomic access
3370 * since it was set above and can ONLY be set in this thread.
3372 ret
= consumer_create_socket(&ustconsumer32_data
,
3373 cmd_ctx
->session
->ust_session
->consumer
);
3385 /* Validate consumer daemon state when start/stop trace command */
3386 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3387 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3388 switch (cmd_ctx
->lsm
->domain
.type
) {
3389 case LTTNG_DOMAIN_NONE
:
3391 case LTTNG_DOMAIN_JUL
:
3392 case LTTNG_DOMAIN_LOG4J
:
3393 case LTTNG_DOMAIN_PYTHON
:
3394 case LTTNG_DOMAIN_UST
:
3395 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3396 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3400 case LTTNG_DOMAIN_KERNEL
:
3401 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3402 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3407 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3413 * Check that the UID or GID match that of the tracing session.
3414 * The root user can interact with all sessions.
3416 if (need_tracing_session
) {
3417 if (!session_access_ok(cmd_ctx
->session
,
3418 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3419 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3420 ret
= LTTNG_ERR_EPERM
;
3426 * Send relayd information to consumer as soon as we have a domain and a
3429 if (cmd_ctx
->session
&& need_domain
) {
3431 * Setup relayd if not done yet. If the relayd information was already
3432 * sent to the consumer, this call will gracefully return.
3434 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3435 if (ret
!= LTTNG_OK
) {
3440 /* Process by command type */
3441 switch (cmd_ctx
->lsm
->cmd_type
) {
3442 case LTTNG_ADD_CONTEXT
:
3445 * An LTTNG_ADD_CONTEXT command might have a supplementary
3446 * payload if the context being added is an application context.
3448 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3449 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3450 char *provider_name
= NULL
, *context_name
= NULL
;
3451 size_t provider_name_len
=
3452 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3453 size_t context_name_len
=
3454 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3456 if (provider_name_len
== 0 || context_name_len
== 0) {
3458 * Application provider and context names MUST
3461 ret
= -LTTNG_ERR_INVALID
;
3465 provider_name
= zmalloc(provider_name_len
+ 1);
3466 if (!provider_name
) {
3467 ret
= -LTTNG_ERR_NOMEM
;
3470 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3473 context_name
= zmalloc(context_name_len
+ 1);
3474 if (!context_name
) {
3475 ret
= -LTTNG_ERR_NOMEM
;
3476 goto error_add_context
;
3478 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3481 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3484 goto error_add_context
;
3487 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3490 goto error_add_context
;
3495 * cmd_add_context assumes ownership of the provider and context
3498 ret
= cmd_add_context(cmd_ctx
->session
,
3499 cmd_ctx
->lsm
->domain
.type
,
3500 cmd_ctx
->lsm
->u
.context
.channel_name
,
3501 &cmd_ctx
->lsm
->u
.context
.ctx
,
3502 kernel_poll_pipe
[1]);
3504 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3505 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3507 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3508 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3514 case LTTNG_DISABLE_CHANNEL
:
3516 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3517 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3520 case LTTNG_DISABLE_EVENT
:
3524 * FIXME: handle filter; for now we just receive the filter's
3525 * bytecode along with the filter expression which are sent by
3526 * liblttng-ctl and discard them.
3528 * This fixes an issue where the client may block while sending
3529 * the filter payload and encounter an error because the session
3530 * daemon closes the socket without ever handling this data.
3532 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3533 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3536 char data
[LTTNG_FILTER_MAX_LEN
];
3538 DBG("Discarding disable event command payload of size %zu", count
);
3540 ret
= lttcomm_recv_unix_sock(sock
, data
,
3541 count
> sizeof(data
) ? sizeof(data
) : count
);
3546 count
-= (size_t) ret
;
3549 /* FIXME: passing packed structure to non-packed pointer */
3550 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3551 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3552 &cmd_ctx
->lsm
->u
.disable
.event
);
3555 case LTTNG_ENABLE_CHANNEL
:
3557 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3558 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3559 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3560 &cmd_ctx
->lsm
->u
.channel
.chan
,
3561 kernel_poll_pipe
[1]);
3564 case LTTNG_TRACK_PID
:
3566 ret
= cmd_track_pid(cmd_ctx
->session
,
3567 cmd_ctx
->lsm
->domain
.type
,
3568 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3571 case LTTNG_UNTRACK_PID
:
3573 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3574 cmd_ctx
->lsm
->domain
.type
,
3575 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3578 case LTTNG_ENABLE_EVENT
:
3580 struct lttng_event
*ev
= NULL
;
3581 struct lttng_event_exclusion
*exclusion
= NULL
;
3582 struct lttng_filter_bytecode
*bytecode
= NULL
;
3583 char *filter_expression
= NULL
;
3585 /* Handle exclusion events and receive it from the client. */
3586 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3587 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3589 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3590 (count
* LTTNG_SYMBOL_NAME_LEN
));
3592 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3596 DBG("Receiving var len exclusion event list from client ...");
3597 exclusion
->count
= count
;
3598 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3599 count
* LTTNG_SYMBOL_NAME_LEN
);
3601 DBG("Nothing recv() from client var len data... continuing");
3604 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3609 /* Get filter expression from client. */
3610 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3611 size_t expression_len
=
3612 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3614 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3615 ret
= LTTNG_ERR_FILTER_INVAL
;
3620 filter_expression
= zmalloc(expression_len
);
3621 if (!filter_expression
) {
3623 ret
= LTTNG_ERR_FILTER_NOMEM
;
3627 /* Receive var. len. data */
3628 DBG("Receiving var len filter's expression from client ...");
3629 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3632 DBG("Nothing recv() from client var len data... continuing");
3634 free(filter_expression
);
3636 ret
= LTTNG_ERR_FILTER_INVAL
;
3641 /* Handle filter and get bytecode from client. */
3642 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3643 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3645 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3646 ret
= LTTNG_ERR_FILTER_INVAL
;
3647 free(filter_expression
);
3652 bytecode
= zmalloc(bytecode_len
);
3654 free(filter_expression
);
3656 ret
= LTTNG_ERR_FILTER_NOMEM
;
3660 /* Receive var. len. data */
3661 DBG("Receiving var len filter's bytecode from client ...");
3662 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3664 DBG("Nothing recv() from client var len data... continuing");
3666 free(filter_expression
);
3669 ret
= LTTNG_ERR_FILTER_INVAL
;
3673 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3674 free(filter_expression
);
3677 ret
= LTTNG_ERR_FILTER_INVAL
;
3682 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
3684 DBG("Failed to copy event: %s",
3685 cmd_ctx
->lsm
->u
.enable
.event
.name
);
3686 free(filter_expression
);
3689 ret
= LTTNG_ERR_NOMEM
;
3694 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
3695 /* Expect a userspace probe description. */
3696 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
3698 free(filter_expression
);
3701 lttng_event_destroy(ev
);
3706 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3707 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3709 filter_expression
, bytecode
, exclusion
,
3710 kernel_poll_pipe
[1]);
3711 lttng_event_destroy(ev
);
3714 case LTTNG_LIST_TRACEPOINTS
:
3716 struct lttng_event
*events
;
3719 session_lock_list();
3720 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3721 session_unlock_list();
3722 if (nb_events
< 0) {
3723 /* Return value is a negative lttng_error_code. */
3729 * Setup lttng message with payload size set to the event list size in
3730 * bytes and then copy list into the llm payload.
3732 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3733 sizeof(struct lttng_event
) * nb_events
);
3743 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3745 struct lttng_event_field
*fields
;
3748 session_lock_list();
3749 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3751 session_unlock_list();
3752 if (nb_fields
< 0) {
3753 /* Return value is a negative lttng_error_code. */
3759 * Setup lttng message with payload size set to the event list size in
3760 * bytes and then copy list into the llm payload.
3762 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3763 sizeof(struct lttng_event_field
) * nb_fields
);
3773 case LTTNG_LIST_SYSCALLS
:
3775 struct lttng_event
*events
;
3778 nb_events
= cmd_list_syscalls(&events
);
3779 if (nb_events
< 0) {
3780 /* Return value is a negative lttng_error_code. */
3786 * Setup lttng message with payload size set to the event list size in
3787 * bytes and then copy list into the llm payload.
3789 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3790 sizeof(struct lttng_event
) * nb_events
);
3800 case LTTNG_LIST_TRACKER_PIDS
:
3802 int32_t *pids
= NULL
;
3805 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3806 cmd_ctx
->lsm
->domain
.type
, &pids
);
3808 /* Return value is a negative lttng_error_code. */
3814 * Setup lttng message with payload size set to the event list size in
3815 * bytes and then copy list into the llm payload.
3817 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3818 sizeof(int32_t) * nr_pids
);
3828 case LTTNG_SET_CONSUMER_URI
:
3831 struct lttng_uri
*uris
;
3833 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3834 len
= nb_uri
* sizeof(struct lttng_uri
);
3837 ret
= LTTNG_ERR_INVALID
;
3841 uris
= zmalloc(len
);
3843 ret
= LTTNG_ERR_FATAL
;
3847 /* Receive variable len data */
3848 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3849 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3851 DBG("No URIs received from client... continuing");
3853 ret
= LTTNG_ERR_SESSION_FAIL
;
3858 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3860 if (ret
!= LTTNG_OK
) {
3867 case LTTNG_START_TRACE
:
3870 * On the first start, if we have a kernel session and we have
3871 * enabled time or size-based rotations, we have to make sure
3872 * the kernel tracer supports it.
3874 if (!cmd_ctx
->session
->has_been_started
&& \
3875 cmd_ctx
->session
->kernel_session
&& \
3876 (cmd_ctx
->session
->rotate_timer_period
|| \
3877 cmd_ctx
->session
->rotate_size
) && \
3878 !check_rotate_compatible()) {
3879 DBG("Kernel tracer version is not compatible with the rotation feature");
3880 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3883 ret
= cmd_start_trace(cmd_ctx
->session
);
3886 case LTTNG_STOP_TRACE
:
3888 ret
= cmd_stop_trace(cmd_ctx
->session
);
3891 case LTTNG_CREATE_SESSION
:
3894 struct lttng_uri
*uris
= NULL
;
3896 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3897 len
= nb_uri
* sizeof(struct lttng_uri
);
3900 uris
= zmalloc(len
);
3902 ret
= LTTNG_ERR_FATAL
;
3906 /* Receive variable len data */
3907 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3908 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3910 DBG("No URIs received from client... continuing");
3912 ret
= LTTNG_ERR_SESSION_FAIL
;
3917 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3918 DBG("Creating session with ONE network URI is a bad call");
3919 ret
= LTTNG_ERR_SESSION_FAIL
;
3925 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3926 &cmd_ctx
->creds
, 0);
3932 case LTTNG_DESTROY_SESSION
:
3934 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1],
3935 notification_thread_handle
);
3937 /* Set session to NULL so we do not unlock it after free. */
3938 cmd_ctx
->session
= NULL
;
3941 case LTTNG_LIST_DOMAINS
:
3944 struct lttng_domain
*domains
= NULL
;
3946 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3948 /* Return value is a negative lttng_error_code. */
3953 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3954 nb_dom
* sizeof(struct lttng_domain
));
3964 case LTTNG_LIST_CHANNELS
:
3966 ssize_t payload_size
;
3967 struct lttng_channel
*channels
= NULL
;
3969 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3970 cmd_ctx
->session
, &channels
);
3971 if (payload_size
< 0) {
3972 /* Return value is a negative lttng_error_code. */
3973 ret
= -payload_size
;
3977 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3988 case LTTNG_LIST_EVENTS
:
3991 struct lttng_event
*events
= NULL
;
3992 struct lttcomm_event_command_header cmd_header
;
3995 memset(&cmd_header
, 0, sizeof(cmd_header
));
3996 /* Extended infos are included at the end of events */
3997 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3998 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3999 &events
, &total_size
);
4002 /* Return value is a negative lttng_error_code. */
4007 cmd_header
.nb_events
= nb_event
;
4008 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
4009 &cmd_header
, sizeof(cmd_header
));
4019 case LTTNG_LIST_SESSIONS
:
4021 unsigned int nr_sessions
;
4022 void *sessions_payload
;
4025 session_lock_list();
4026 nr_sessions
= lttng_sessions_count(
4027 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4028 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4029 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
4030 sessions_payload
= zmalloc(payload_len
);
4032 if (!sessions_payload
) {
4033 session_unlock_list();
4038 cmd_list_lttng_sessions(sessions_payload
,
4039 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4040 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4041 session_unlock_list();
4043 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
4045 free(sessions_payload
);
4054 case LTTNG_REGISTER_CONSUMER
:
4056 struct consumer_data
*cdata
;
4058 switch (cmd_ctx
->lsm
->domain
.type
) {
4059 case LTTNG_DOMAIN_KERNEL
:
4060 cdata
= &kconsumer_data
;
4063 ret
= LTTNG_ERR_UND
;
4067 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4068 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
4071 case LTTNG_DATA_PENDING
:
4074 uint8_t pending_ret_byte
;
4076 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
4081 * This function may returns 0 or 1 to indicate whether or not
4082 * there is data pending. In case of error, it should return an
4083 * LTTNG_ERR code. However, some code paths may still return
4084 * a nondescript error code, which we handle by returning an
4087 if (pending_ret
== 0 || pending_ret
== 1) {
4089 * ret will be set to LTTNG_OK at the end of
4092 } else if (pending_ret
< 0) {
4093 ret
= LTTNG_ERR_UNK
;
4100 pending_ret_byte
= (uint8_t) pending_ret
;
4102 /* 1 byte to return whether or not data is pending */
4103 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
4104 &pending_ret_byte
, 1);
4113 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
4115 struct lttcomm_lttng_output_id reply
;
4117 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
4118 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
4119 if (ret
!= LTTNG_OK
) {
4123 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
4129 /* Copy output list into message payload */
4133 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
4135 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
4136 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
4139 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
4142 struct lttng_snapshot_output
*outputs
= NULL
;
4144 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
4145 if (nb_output
< 0) {
4150 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4151 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4152 nb_output
* sizeof(struct lttng_snapshot_output
));
4162 case LTTNG_SNAPSHOT_RECORD
:
4164 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4165 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4166 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4169 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4172 struct lttng_uri
*uris
= NULL
;
4174 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4175 len
= nb_uri
* sizeof(struct lttng_uri
);
4178 uris
= zmalloc(len
);
4180 ret
= LTTNG_ERR_FATAL
;
4184 /* Receive variable len data */
4185 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4186 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4188 DBG("No URIs received from client... continuing");
4190 ret
= LTTNG_ERR_SESSION_FAIL
;
4195 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4196 DBG("Creating session with ONE network URI is a bad call");
4197 ret
= LTTNG_ERR_SESSION_FAIL
;
4203 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4204 nb_uri
, &cmd_ctx
->creds
);
4208 case LTTNG_CREATE_SESSION_LIVE
:
4211 struct lttng_uri
*uris
= NULL
;
4213 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4214 len
= nb_uri
* sizeof(struct lttng_uri
);
4217 uris
= zmalloc(len
);
4219 ret
= LTTNG_ERR_FATAL
;
4223 /* Receive variable len data */
4224 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4225 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4227 DBG("No URIs received from client... continuing");
4229 ret
= LTTNG_ERR_SESSION_FAIL
;
4234 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4235 DBG("Creating session with ONE network URI is a bad call");
4236 ret
= LTTNG_ERR_SESSION_FAIL
;
4242 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4243 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4247 case LTTNG_SAVE_SESSION
:
4249 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4253 case LTTNG_SET_SESSION_SHM_PATH
:
4255 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4256 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4259 case LTTNG_REGENERATE_METADATA
:
4261 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4264 case LTTNG_REGENERATE_STATEDUMP
:
4266 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4269 case LTTNG_REGISTER_TRIGGER
:
4271 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4272 notification_thread_handle
);
4275 case LTTNG_UNREGISTER_TRIGGER
:
4277 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4278 notification_thread_handle
);
4281 case LTTNG_ROTATE_SESSION
:
4283 struct lttng_rotate_session_return rotate_return
;
4285 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4287 memset(&rotate_return
, 0, sizeof(rotate_return
));
4288 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4289 DBG("Kernel tracer version is not compatible with the rotation feature");
4290 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4294 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4300 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4301 sizeof(rotate_return
));
4310 case LTTNG_ROTATION_GET_INFO
:
4312 struct lttng_rotation_get_info_return get_info_return
;
4314 memset(&get_info_return
, 0, sizeof(get_info_return
));
4315 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4316 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4322 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4323 sizeof(get_info_return
));
4332 case LTTNG_ROTATION_SET_SCHEDULE
:
4335 enum lttng_rotation_schedule_type schedule_type
;
4338 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4339 DBG("Kernel tracer version does not support session rotations");
4340 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4344 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
4345 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
4346 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
4348 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4352 notification_thread_handle
);
4353 if (ret
!= LTTNG_OK
) {
4359 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
4361 struct lttng_session_list_schedules_return schedules
= {
4362 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
4363 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
4364 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
4365 .size
.value
= cmd_ctx
->session
->rotate_size
,
4368 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
4379 ret
= LTTNG_ERR_UND
;
4384 if (cmd_ctx
->llm
== NULL
) {
4385 DBG("Missing llm structure. Allocating one.");
4386 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4390 /* Set return code */
4391 cmd_ctx
->llm
->ret_code
= ret
;
4393 if (cmd_ctx
->session
) {
4394 session_unlock(cmd_ctx
->session
);
4396 if (need_tracing_session
) {
4397 session_unlock_list();
4400 assert(!rcu_read_ongoing());
4405 * Thread managing health check socket.
4407 static void *thread_manage_health(void *data
)
4409 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4410 uint32_t revents
, nb_fd
;
4411 struct lttng_poll_event events
;
4412 struct health_comm_msg msg
;
4413 struct health_comm_reply reply
;
4415 DBG("[thread] Manage health check started");
4417 rcu_register_thread();
4419 /* We might hit an error path before this is created. */
4420 lttng_poll_init(&events
);
4422 /* Create unix socket */
4423 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4425 ERR("Unable to create health check Unix socket");
4430 /* lttng health client socket path permissions */
4431 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4432 utils_get_group_id(config
.tracing_group_name
.value
));
4434 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4439 ret
= chmod(config
.health_unix_sock_path
.value
,
4440 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4442 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4449 * Set the CLOEXEC flag. Return code is useless because either way, the
4452 (void) utils_set_fd_cloexec(sock
);
4454 ret
= lttcomm_listen_unix_sock(sock
);
4460 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4461 * more will be added to this poll set.
4463 ret
= sessiond_set_thread_pollset(&events
, 2);
4468 /* Add the application registration socket */
4469 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4474 sessiond_notify_ready();
4477 DBG("Health check ready");
4479 /* Inifinite blocking call, waiting for transmission */
4481 ret
= lttng_poll_wait(&events
, -1);
4484 * Restart interrupted system call.
4486 if (errno
== EINTR
) {
4494 for (i
= 0; i
< nb_fd
; i
++) {
4495 /* Fetch once the poll data */
4496 revents
= LTTNG_POLL_GETEV(&events
, i
);
4497 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4500 /* No activity for this FD (poll implementation). */
4504 /* Thread quit pipe has been closed. Killing thread. */
4505 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4511 /* Event on the registration socket */
4512 if (pollfd
== sock
) {
4513 if (revents
& LPOLLIN
) {
4515 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4516 ERR("Health socket poll error");
4519 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4525 new_sock
= lttcomm_accept_unix_sock(sock
);
4531 * Set the CLOEXEC flag. Return code is useless because either way, the
4534 (void) utils_set_fd_cloexec(new_sock
);
4536 DBG("Receiving data from client for health...");
4537 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4539 DBG("Nothing recv() from client... continuing");
4540 ret
= close(new_sock
);
4547 rcu_thread_online();
4549 memset(&reply
, 0, sizeof(reply
));
4550 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4552 * health_check_state returns 0 if health is
4555 if (!health_check_state(health_sessiond
, i
)) {
4556 reply
.ret_code
|= 1ULL << i
;
4560 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4562 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4564 ERR("Failed to send health data back to client");
4567 /* End of transmission */
4568 ret
= close(new_sock
);
4577 ERR("Health error occurred in %s", __func__
);
4579 DBG("Health check thread dying");
4580 unlink(config
.health_unix_sock_path
.value
);
4588 lttng_poll_clean(&events
);
4590 rcu_unregister_thread();
4595 * This thread manage all clients request using the unix client socket for
4598 static void *thread_manage_clients(void *data
)
4600 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4602 uint32_t revents
, nb_fd
;
4603 struct command_ctx
*cmd_ctx
= NULL
;
4604 struct lttng_poll_event events
;
4606 DBG("[thread] Manage client started");
4608 rcu_register_thread();
4610 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4612 health_code_update();
4614 ret
= lttcomm_listen_unix_sock(client_sock
);
4620 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4621 * more will be added to this poll set.
4623 ret
= sessiond_set_thread_pollset(&events
, 2);
4625 goto error_create_poll
;
4628 /* Add the application registration socket */
4629 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4634 ret
= sem_post(&load_info
->message_thread_ready
);
4636 PERROR("sem_post message_thread_ready");
4641 * Wait until all support threads are initialized before accepting
4644 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4646 struct timeval timeout
;
4649 FD_SET(thread_quit_pipe
[0], &read_fds
);
4650 memset(&timeout
, 0, sizeof(timeout
));
4651 timeout
.tv_usec
= 1000;
4654 * If a support thread failed to launch, it may signal that
4655 * we must exit and the sessiond would never be marked as
4658 * The timeout is set to 1ms, which serves as a way to
4659 * pace down this check.
4661 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
4663 if (ret
> 0 || (ret
< 0 && errno
!= EINTR
)) {
4668 * This barrier is paired with the one in sessiond_notify_ready() to
4669 * ensure that loads accessing data initialized by the other threads,
4670 * on which this thread was waiting, are not performed before this point.
4672 * Note that this could be a 'read' memory barrier, but a full barrier
4673 * is used in case the code changes. The performance implications of
4674 * this choice are minimal since this is a slow path.
4678 /* This testpoint is after we signal readiness to the parent. */
4679 if (testpoint(sessiond_thread_manage_clients
)) {
4683 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4687 health_code_update();
4690 const struct cmd_completion_handler
*cmd_completion_handler
;
4692 DBG("Accepting client command ...");
4694 /* Inifinite blocking call, waiting for transmission */
4696 health_poll_entry();
4697 ret
= lttng_poll_wait(&events
, -1);
4701 * Restart interrupted system call.
4703 if (errno
== EINTR
) {
4711 for (i
= 0; i
< nb_fd
; i
++) {
4712 /* Fetch once the poll data */
4713 revents
= LTTNG_POLL_GETEV(&events
, i
);
4714 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4716 health_code_update();
4719 /* No activity for this FD (poll implementation). */
4723 /* Thread quit pipe has been closed. Killing thread. */
4724 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4730 /* Event on the registration socket */
4731 if (pollfd
== client_sock
) {
4732 if (revents
& LPOLLIN
) {
4734 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4735 ERR("Client socket poll error");
4738 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4744 DBG("Wait for client response");
4746 health_code_update();
4748 sock
= lttcomm_accept_unix_sock(client_sock
);
4754 * Set the CLOEXEC flag. Return code is useless because either way, the
4757 (void) utils_set_fd_cloexec(sock
);
4759 /* Set socket option for credentials retrieval */
4760 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4765 /* Allocate context command to process the client request */
4766 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4767 if (cmd_ctx
== NULL
) {
4768 PERROR("zmalloc cmd_ctx");
4772 /* Allocate data buffer for reception */
4773 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4774 if (cmd_ctx
->lsm
== NULL
) {
4775 PERROR("zmalloc cmd_ctx->lsm");
4779 cmd_ctx
->llm
= NULL
;
4780 cmd_ctx
->session
= NULL
;
4782 health_code_update();
4785 * Data is received from the lttng client. The struct
4786 * lttcomm_session_msg (lsm) contains the command and data request of
4789 DBG("Receiving data from client ...");
4790 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4791 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4793 DBG("Nothing recv() from client... continuing");
4799 clean_command_ctx(&cmd_ctx
);
4803 health_code_update();
4805 // TODO: Validate cmd_ctx including sanity check for
4806 // security purpose.
4808 rcu_thread_online();
4810 * This function dispatch the work to the kernel or userspace tracer
4811 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4812 * informations for the client. The command context struct contains
4813 * everything this function may needs.
4815 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4816 rcu_thread_offline();
4824 * TODO: Inform client somehow of the fatal error. At
4825 * this point, ret < 0 means that a zmalloc failed
4826 * (ENOMEM). Error detected but still accept
4827 * command, unless a socket error has been
4830 clean_command_ctx(&cmd_ctx
);
4834 cmd_completion_handler
= cmd_pop_completion_handler();
4835 if (cmd_completion_handler
) {
4836 enum lttng_error_code completion_code
;
4838 completion_code
= cmd_completion_handler
->run(
4839 cmd_completion_handler
->data
);
4840 if (completion_code
!= LTTNG_OK
) {
4841 clean_command_ctx(&cmd_ctx
);
4846 health_code_update();
4848 DBG("Sending response (size: %d, retcode: %s (%d))",
4849 cmd_ctx
->lttng_msg_size
,
4850 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4851 cmd_ctx
->llm
->ret_code
);
4852 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4854 ERR("Failed to send data back to client");
4857 /* End of transmission */
4864 clean_command_ctx(&cmd_ctx
);
4866 health_code_update();
4878 lttng_poll_clean(&events
);
4879 clean_command_ctx(&cmd_ctx
);
4883 unlink(config
.client_unix_sock_path
.value
);
4884 if (client_sock
>= 0) {
4885 ret
= close(client_sock
);
4893 ERR("Health error occurred in %s", __func__
);
4896 health_unregister(health_sessiond
);
4898 DBG("Client thread dying");
4900 rcu_unregister_thread();
4903 * Since we are creating the consumer threads, we own them, so we need
4904 * to join them before our thread exits.
4906 ret
= join_consumer_thread(&kconsumer_data
);
4909 PERROR("join_consumer");
4912 ret
= join_consumer_thread(&ustconsumer32_data
);
4915 PERROR("join_consumer ust32");
4918 ret
= join_consumer_thread(&ustconsumer64_data
);
4921 PERROR("join_consumer ust64");
4926 static int string_match(const char *str1
, const char *str2
)
4928 return (str1
&& str2
) && !strcmp(str1
, str2
);
4932 * Take an option from the getopt output and set it in the right variable to be
4935 * Return 0 on success else a negative value.
4937 static int set_option(int opt
, const char *arg
, const char *optname
)
4941 if (string_match(optname
, "client-sock") || opt
== 'c') {
4942 if (!arg
|| *arg
== '\0') {
4946 if (lttng_is_setuid_setgid()) {
4947 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4948 "-c, --client-sock");
4950 config_string_set(&config
.client_unix_sock_path
,
4952 if (!config
.client_unix_sock_path
.value
) {
4957 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4958 if (!arg
|| *arg
== '\0') {
4962 if (lttng_is_setuid_setgid()) {
4963 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4966 config_string_set(&config
.apps_unix_sock_path
,
4968 if (!config
.apps_unix_sock_path
.value
) {
4973 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4974 config
.daemonize
= true;
4975 } else if (string_match(optname
, "background") || opt
== 'b') {
4976 config
.background
= true;
4977 } else if (string_match(optname
, "group") || opt
== 'g') {
4978 if (!arg
|| *arg
== '\0') {
4982 if (lttng_is_setuid_setgid()) {
4983 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4986 config_string_set(&config
.tracing_group_name
,
4988 if (!config
.tracing_group_name
.value
) {
4993 } else if (string_match(optname
, "help") || opt
== 'h') {
4994 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4996 ERR("Cannot show --help for `lttng-sessiond`");
4999 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
5000 } else if (string_match(optname
, "version") || opt
== 'V') {
5001 fprintf(stdout
, "%s\n", VERSION
);
5003 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
5004 config
.sig_parent
= true;
5005 } else if (string_match(optname
, "kconsumerd-err-sock")) {
5006 if (!arg
|| *arg
== '\0') {
5010 if (lttng_is_setuid_setgid()) {
5011 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5012 "--kconsumerd-err-sock");
5014 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
5016 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
5021 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
5022 if (!arg
|| *arg
== '\0') {
5026 if (lttng_is_setuid_setgid()) {
5027 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5028 "--kconsumerd-cmd-sock");
5030 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
5032 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
5037 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
5038 if (!arg
|| *arg
== '\0') {
5042 if (lttng_is_setuid_setgid()) {
5043 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5044 "--ustconsumerd64-err-sock");
5046 config_string_set(&config
.consumerd64_err_unix_sock_path
,
5048 if (!config
.consumerd64_err_unix_sock_path
.value
) {
5053 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
5054 if (!arg
|| *arg
== '\0') {
5058 if (lttng_is_setuid_setgid()) {
5059 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5060 "--ustconsumerd64-cmd-sock");
5062 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
5064 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
5069 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
5070 if (!arg
|| *arg
== '\0') {
5074 if (lttng_is_setuid_setgid()) {
5075 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5076 "--ustconsumerd32-err-sock");
5078 config_string_set(&config
.consumerd32_err_unix_sock_path
,
5080 if (!config
.consumerd32_err_unix_sock_path
.value
) {
5085 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
5086 if (!arg
|| *arg
== '\0') {
5090 if (lttng_is_setuid_setgid()) {
5091 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5092 "--ustconsumerd32-cmd-sock");
5094 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
5096 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
5101 } else if (string_match(optname
, "no-kernel")) {
5102 config
.no_kernel
= true;
5103 } else if (string_match(optname
, "quiet") || opt
== 'q') {
5104 config
.quiet
= true;
5105 } else if (string_match(optname
, "verbose") || opt
== 'v') {
5106 /* Verbose level can increase using multiple -v */
5108 /* Value obtained from config file */
5109 config
.verbose
= config_parse_value(arg
);
5111 /* -v used on command line */
5114 /* Clamp value to [0, 3] */
5115 config
.verbose
= config
.verbose
< 0 ? 0 :
5116 (config
.verbose
<= 3 ? config
.verbose
: 3);
5117 } else if (string_match(optname
, "verbose-consumer")) {
5119 config
.verbose_consumer
= config_parse_value(arg
);
5121 config
.verbose_consumer
++;
5123 } else if (string_match(optname
, "consumerd32-path")) {
5124 if (!arg
|| *arg
== '\0') {
5128 if (lttng_is_setuid_setgid()) {
5129 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5130 "--consumerd32-path");
5132 config_string_set(&config
.consumerd32_bin_path
,
5134 if (!config
.consumerd32_bin_path
.value
) {
5139 } else if (string_match(optname
, "consumerd32-libdir")) {
5140 if (!arg
|| *arg
== '\0') {
5144 if (lttng_is_setuid_setgid()) {
5145 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5146 "--consumerd32-libdir");
5148 config_string_set(&config
.consumerd32_lib_dir
,
5150 if (!config
.consumerd32_lib_dir
.value
) {
5155 } else if (string_match(optname
, "consumerd64-path")) {
5156 if (!arg
|| *arg
== '\0') {
5160 if (lttng_is_setuid_setgid()) {
5161 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5162 "--consumerd64-path");
5164 config_string_set(&config
.consumerd64_bin_path
,
5166 if (!config
.consumerd64_bin_path
.value
) {
5171 } else if (string_match(optname
, "consumerd64-libdir")) {
5172 if (!arg
|| *arg
== '\0') {
5176 if (lttng_is_setuid_setgid()) {
5177 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5178 "--consumerd64-libdir");
5180 config_string_set(&config
.consumerd64_lib_dir
,
5182 if (!config
.consumerd64_lib_dir
.value
) {
5187 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
5188 if (!arg
|| *arg
== '\0') {
5192 if (lttng_is_setuid_setgid()) {
5193 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5196 config_string_set(&config
.pid_file_path
, strdup(arg
));
5197 if (!config
.pid_file_path
.value
) {
5202 } else if (string_match(optname
, "agent-tcp-port")) {
5203 if (!arg
|| *arg
== '\0') {
5207 if (lttng_is_setuid_setgid()) {
5208 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5209 "--agent-tcp-port");
5214 v
= strtoul(arg
, NULL
, 0);
5215 if (errno
!= 0 || !isdigit(arg
[0])) {
5216 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5219 if (v
== 0 || v
>= 65535) {
5220 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5223 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
5224 DBG3("Agent TCP port set to non default: %i", (int) v
);
5226 } else if (string_match(optname
, "load") || opt
== 'l') {
5227 if (!arg
|| *arg
== '\0') {
5231 if (lttng_is_setuid_setgid()) {
5232 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5235 config_string_set(&config
.load_session_path
, strdup(arg
));
5236 if (!config
.load_session_path
.value
) {
5241 } else if (string_match(optname
, "kmod-probes")) {
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
.kmod_probes_list
, strdup(arg
));
5251 if (!config
.kmod_probes_list
.value
) {
5256 } else if (string_match(optname
, "extra-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.",
5263 "--extra-kmod-probes");
5265 config_string_set(&config
.kmod_extra_probes_list
,
5267 if (!config
.kmod_extra_probes_list
.value
) {
5272 } else if (string_match(optname
, "config") || opt
== 'f') {
5273 /* This is handled in set_options() thus silent skip. */
5276 /* Unknown option or other error.
5277 * Error is printed by getopt, just return */
5282 if (ret
== -EINVAL
) {
5283 const char *opt_name
= "unknown";
5286 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5288 if (opt
== long_options
[i
].val
) {
5289 opt_name
= long_options
[i
].name
;
5294 WARN("Invalid argument provided for option \"%s\", using default value.",
5302 * config_entry_handler_cb used to handle options read from a config file.
5303 * See config_entry_handler_cb comment in common/config/session-config.h for the
5304 * return value conventions.
5306 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5310 if (!entry
|| !entry
->name
|| !entry
->value
) {
5315 /* Check if the option is to be ignored */
5316 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5317 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5322 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5325 /* Ignore if not fully matched. */
5326 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5331 * If the option takes no argument on the command line, we have to
5332 * check if the value is "true". We support non-zero numeric values,
5335 if (!long_options
[i
].has_arg
) {
5336 ret
= config_parse_value(entry
->value
);
5339 WARN("Invalid configuration value \"%s\" for option %s",
5340 entry
->value
, entry
->name
);
5342 /* False, skip boolean config option. */
5347 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5351 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5358 * daemon configuration loading and argument parsing
5360 static int set_options(int argc
, char **argv
)
5362 int ret
= 0, c
= 0, option_index
= 0;
5363 int orig_optopt
= optopt
, orig_optind
= optind
;
5365 const char *config_path
= NULL
;
5367 optstring
= utils_generate_optstring(long_options
,
5368 sizeof(long_options
) / sizeof(struct option
));
5374 /* Check for the --config option */
5375 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5376 &option_index
)) != -1) {
5380 } else if (c
!= 'f') {
5381 /* if not equal to --config option. */
5385 if (lttng_is_setuid_setgid()) {
5386 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5389 config_path
= utils_expand_path(optarg
);
5391 ERR("Failed to resolve path: %s", optarg
);
5396 ret
= config_get_section_entries(config_path
, config_section_name
,
5397 config_entry_handler
, NULL
);
5400 ERR("Invalid configuration option at line %i", ret
);
5406 /* Reset getopt's global state */
5407 optopt
= orig_optopt
;
5408 optind
= orig_optind
;
5412 * getopt_long() will not set option_index if it encounters a
5415 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5422 * Pass NULL as the long option name if popt left the index
5425 ret
= set_option(c
, optarg
,
5426 option_index
< 0 ? NULL
:
5427 long_options
[option_index
].name
);
5439 * Creates the two needed socket by the daemon.
5440 * apps_sock - The communication socket for all UST apps.
5441 * client_sock - The communication of the cli tool (lttng).
5443 static int init_daemon_socket(void)
5448 old_umask
= umask(0);
5450 /* Create client tool unix socket */
5451 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5452 if (client_sock
< 0) {
5453 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5458 /* Set the cloexec flag */
5459 ret
= utils_set_fd_cloexec(client_sock
);
5461 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5462 "Continuing but note that the consumer daemon will have a "
5463 "reference to this socket on exec()", client_sock
);
5466 /* File permission MUST be 660 */
5467 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5469 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5474 /* Create the application unix socket */
5475 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5476 if (apps_sock
< 0) {
5477 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5482 /* Set the cloexec flag */
5483 ret
= utils_set_fd_cloexec(apps_sock
);
5485 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5486 "Continuing but note that the consumer daemon will have a "
5487 "reference to this socket on exec()", apps_sock
);
5490 /* File permission MUST be 666 */
5491 ret
= chmod(config
.apps_unix_sock_path
.value
,
5492 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5494 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5499 DBG3("Session daemon client socket %d and application socket %d created",
5500 client_sock
, apps_sock
);
5508 * Create lockfile using the rundir and return its fd.
5510 static int create_lockfile(void)
5512 return utils_create_lock_file(config
.lock_file_path
.value
);
5516 * Check if the global socket is available, and if a daemon is answering at the
5517 * other side. If yes, error is returned.
5519 * Also attempts to create and hold the lock file.
5521 static int check_existing_daemon(void)
5525 /* Is there anybody out there ? */
5526 if (lttng_session_daemon_alive()) {
5531 lockfile_fd
= create_lockfile();
5532 if (lockfile_fd
< 0) {
5540 static void sessiond_cleanup_lock_file(void)
5545 * Cleanup lock file by deleting it and finaly closing it which will
5546 * release the file system lock.
5548 if (lockfile_fd
>= 0) {
5549 ret
= remove(config
.lock_file_path
.value
);
5551 PERROR("remove lock file");
5553 ret
= close(lockfile_fd
);
5555 PERROR("close lock file");
5561 * Set the tracing group gid onto the client socket.
5563 * Race window between mkdir and chown is OK because we are going from more
5564 * permissive (root.root) to less permissive (root.tracing).
5566 static int set_permissions(char *rundir
)
5571 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5573 /* Set lttng run dir */
5574 ret
= chown(rundir
, 0, gid
);
5576 ERR("Unable to set group on %s", rundir
);
5581 * Ensure all applications and tracing group can search the run
5582 * dir. Allow everyone to read the directory, since it does not
5583 * buy us anything to hide its content.
5585 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5587 ERR("Unable to set permissions on %s", rundir
);
5591 /* lttng client socket path */
5592 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5594 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5598 /* kconsumer error socket path */
5599 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5601 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5605 /* 64-bit ustconsumer error socket path */
5606 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5608 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5612 /* 32-bit ustconsumer compat32 error socket path */
5613 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5615 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5619 DBG("All permissions are set");
5625 * Create the lttng run directory needed for all global sockets and pipe.
5627 static int create_lttng_rundir(void)
5631 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5633 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5635 if (errno
!= EEXIST
) {
5636 ERR("Unable to create %s", config
.rundir
.value
);
5648 * Setup sockets and directory needed by the consumerds' communication with the
5651 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5656 switch (consumer_data
->type
) {
5657 case LTTNG_CONSUMER_KERNEL
:
5658 path
= config
.kconsumerd_path
.value
;
5660 case LTTNG_CONSUMER64_UST
:
5661 path
= config
.consumerd64_path
.value
;
5663 case LTTNG_CONSUMER32_UST
:
5664 path
= config
.consumerd32_path
.value
;
5667 ERR("Consumer type unknown");
5673 DBG2("Creating consumer directory: %s", path
);
5675 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5676 if (ret
< 0 && errno
!= EEXIST
) {
5678 ERR("Failed to create %s", path
);
5682 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5684 ERR("Unable to set group on %s", path
);
5690 /* Create the consumerd error unix socket */
5691 consumer_data
->err_sock
=
5692 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5693 if (consumer_data
->err_sock
< 0) {
5694 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5700 * Set the CLOEXEC flag. Return code is useless because either way, the
5703 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5705 PERROR("utils_set_fd_cloexec");
5706 /* continue anyway */
5709 /* File permission MUST be 660 */
5710 ret
= chmod(consumer_data
->err_unix_sock_path
,
5711 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5713 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5723 * Signal handler for the daemon
5725 * Simply stop all worker threads, leaving main() return gracefully after
5726 * joining all threads and calling cleanup().
5728 static void sighandler(int sig
)
5732 DBG("SIGINT caught");
5736 DBG("SIGTERM caught");
5740 CMM_STORE_SHARED(recv_child_signal
, 1);
5748 * Setup signal handler for :
5749 * SIGINT, SIGTERM, SIGPIPE
5751 static int set_signal_handler(void)
5754 struct sigaction sa
;
5757 if ((ret
= sigemptyset(&sigset
)) < 0) {
5758 PERROR("sigemptyset");
5762 sa
.sa_mask
= sigset
;
5765 sa
.sa_handler
= sighandler
;
5766 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5767 PERROR("sigaction");
5771 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5772 PERROR("sigaction");
5776 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5777 PERROR("sigaction");
5781 sa
.sa_handler
= SIG_IGN
;
5782 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5783 PERROR("sigaction");
5787 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5793 * Set open files limit to unlimited. This daemon can open a large number of
5794 * file descriptors in order to consume multiple kernel traces.
5796 static void set_ulimit(void)
5801 /* The kernel does not allow an infinite limit for open files */
5802 lim
.rlim_cur
= 65535;
5803 lim
.rlim_max
= 65535;
5805 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5807 PERROR("failed to set open files limit");
5811 static int write_pidfile(void)
5813 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5816 static int set_clock_plugin_env(void)
5819 char *env_value
= NULL
;
5821 if (!config
.lttng_ust_clock_plugin
.value
) {
5825 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5826 config
.lttng_ust_clock_plugin
.value
);
5832 ret
= putenv(env_value
);
5835 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5839 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5840 config
.lttng_ust_clock_plugin
.value
);
5846 struct rotation_thread_timer_queue
*create_rotate_timer_queue(void)
5848 struct rotation_thread_timer_queue
*queue
= NULL
;
5850 queue
= zmalloc(sizeof(struct rotation_thread_timer_queue
));
5852 PERROR("Failed to allocate timer rotate queue");
5856 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
5857 CDS_INIT_LIST_HEAD(&queue
->list
);
5858 pthread_mutex_init(&queue
->lock
, NULL
);
5865 void destroy_rotate_timer_queue(struct rotation_thread_timer_queue
*queue
)
5867 struct sessiond_rotation_timer
*node
, *tmp_node
;
5873 lttng_pipe_destroy(queue
->event_pipe
);
5875 pthread_mutex_lock(&queue
->lock
);
5876 /* Empty wait queue. */
5877 cds_list_for_each_entry_safe(node
, tmp_node
, &queue
->list
, head
) {
5878 cds_list_del(&node
->head
);
5881 pthread_mutex_unlock(&queue
->lock
);
5883 pthread_mutex_destroy(&queue
->lock
);
5890 int main(int argc
, char **argv
)
5892 int ret
= 0, retval
= 0;
5894 const char *env_app_timeout
;
5895 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5896 *ust64_channel_monitor_pipe
= NULL
,
5897 *kernel_channel_monitor_pipe
= NULL
;
5898 bool notification_thread_launched
= false;
5899 bool rotation_thread_launched
= false;
5900 bool timer_thread_launched
= false;
5901 struct lttng_pipe
*ust32_channel_rotate_pipe
= NULL
,
5902 *ust64_channel_rotate_pipe
= NULL
,
5903 *kernel_channel_rotate_pipe
= NULL
;
5904 struct timer_thread_parameters timer_thread_ctx
;
5905 /* Queue of rotation jobs populated by the sessiond-timer. */
5906 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5907 sem_t notification_thread_ready
;
5909 init_kernel_workarounds();
5911 rcu_register_thread();
5913 if (set_signal_handler()) {
5915 goto exit_set_signal_handler
;
5918 if (sessiond_timer_signal_init()) {
5920 goto exit_set_signal_handler
;
5923 page_size
= sysconf(_SC_PAGESIZE
);
5924 if (page_size
< 0) {
5925 PERROR("sysconf _SC_PAGESIZE");
5926 page_size
= LONG_MAX
;
5927 WARN("Fallback page size to %ld", page_size
);
5930 ret
= sessiond_config_init(&config
);
5933 goto exit_set_signal_handler
;
5937 * Init config from environment variables.
5938 * Command line option override env configuration per-doc. Do env first.
5940 sessiond_config_apply_env_config(&config
);
5943 * Parse arguments and load the daemon configuration file.
5945 * We have an exit_options exit path to free memory reserved by
5946 * set_options. This is needed because the rest of sessiond_cleanup()
5947 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5948 * depends on set_options.
5951 if (set_options(argc
, argv
)) {
5957 * Resolve all paths received as arguments, configuration option, or
5958 * through environment variable as absolute paths. This is necessary
5959 * since daemonizing causes the sessiond's current working directory
5962 ret
= sessiond_config_resolve_paths(&config
);
5968 lttng_opt_verbose
= config
.verbose
;
5969 lttng_opt_quiet
= config
.quiet
;
5970 kconsumer_data
.err_unix_sock_path
=
5971 config
.kconsumerd_err_unix_sock_path
.value
;
5972 kconsumer_data
.cmd_unix_sock_path
=
5973 config
.kconsumerd_cmd_unix_sock_path
.value
;
5974 ustconsumer32_data
.err_unix_sock_path
=
5975 config
.consumerd32_err_unix_sock_path
.value
;
5976 ustconsumer32_data
.cmd_unix_sock_path
=
5977 config
.consumerd32_cmd_unix_sock_path
.value
;
5978 ustconsumer64_data
.err_unix_sock_path
=
5979 config
.consumerd64_err_unix_sock_path
.value
;
5980 ustconsumer64_data
.cmd_unix_sock_path
=
5981 config
.consumerd64_cmd_unix_sock_path
.value
;
5982 set_clock_plugin_env();
5984 sessiond_config_log(&config
);
5986 if (create_lttng_rundir()) {
5991 /* Abort launch if a session daemon is already running. */
5992 if (check_existing_daemon()) {
5993 ERR("A session daemon is already running.");
5999 if (config
.daemonize
|| config
.background
) {
6002 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
6003 !config
.background
);
6010 * We are in the child. Make sure all other file descriptors are
6011 * closed, in case we are called with more opened file
6012 * descriptors than the standard ones and the lock file.
6014 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
6015 if (i
== lockfile_fd
) {
6022 if (run_as_create_worker(argv
[0]) < 0) {
6023 goto exit_create_run_as_worker_cleanup
;
6027 * Starting from here, we can create threads. This needs to be after
6028 * lttng_daemonize due to RCU.
6032 * Initialize the health check subsystem. This call should set the
6033 * appropriate time values.
6035 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
6036 if (!health_sessiond
) {
6037 PERROR("health_app_create error");
6039 goto exit_health_sessiond_cleanup
;
6042 /* Create thread to clean up RCU hash tables */
6043 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
6045 goto exit_ht_cleanup
;
6048 /* Create thread quit pipe */
6049 if (init_thread_quit_pipe()) {
6051 goto exit_init_data
;
6054 /* Check if daemon is UID = 0 */
6055 is_root
= !getuid();
6057 /* Create global run dir with root access */
6059 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
6060 if (!kernel_channel_monitor_pipe
) {
6061 ERR("Failed to create kernel consumer channel monitor pipe");
6063 goto exit_init_data
;
6065 kconsumer_data
.channel_monitor_pipe
=
6066 lttng_pipe_release_writefd(
6067 kernel_channel_monitor_pipe
);
6068 if (kconsumer_data
.channel_monitor_pipe
< 0) {
6070 goto exit_init_data
;
6072 kernel_channel_rotate_pipe
= lttng_pipe_open(0);
6073 if (!kernel_channel_rotate_pipe
) {
6074 ERR("Failed to create kernel consumer channel rotate pipe");
6076 goto exit_init_data
;
6078 kconsumer_data
.channel_rotate_pipe
=
6079 lttng_pipe_release_writefd(
6080 kernel_channel_rotate_pipe
);
6081 if (kconsumer_data
.channel_rotate_pipe
< 0) {
6083 goto exit_init_data
;
6087 /* Set consumer initial state */
6088 kernel_consumerd_state
= CONSUMER_STOPPED
;
6089 ust_consumerd_state
= CONSUMER_STOPPED
;
6091 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
6092 if (!ust32_channel_monitor_pipe
) {
6093 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
6095 goto exit_init_data
;
6097 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6098 ust32_channel_monitor_pipe
);
6099 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
6101 goto exit_init_data
;
6103 ust32_channel_rotate_pipe
= lttng_pipe_open(0);
6104 if (!ust32_channel_rotate_pipe
) {
6105 ERR("Failed to create 32-bit user space consumer channel rotate pipe");
6107 goto exit_init_data
;
6109 ustconsumer32_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6110 ust32_channel_rotate_pipe
);
6111 if (ustconsumer32_data
.channel_rotate_pipe
< 0) {
6113 goto exit_init_data
;
6117 * The rotation_timer_queue structure is shared between the sessiond timer
6118 * thread and the rotation thread. The main() keeps the ownership and
6119 * destroys it when both threads have quit.
6121 rotation_timer_queue
= create_rotate_timer_queue();
6122 if (!rotation_timer_queue
) {
6124 goto exit_init_data
;
6126 timer_thread_ctx
.rotation_timer_queue
= rotation_timer_queue
;
6128 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
6129 if (!ust64_channel_monitor_pipe
) {
6130 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
6132 goto exit_init_data
;
6134 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6135 ust64_channel_monitor_pipe
);
6136 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
6138 goto exit_init_data
;
6140 ust64_channel_rotate_pipe
= lttng_pipe_open(0);
6141 if (!ust64_channel_rotate_pipe
) {
6142 ERR("Failed to create 64-bit user space consumer channel rotate pipe");
6144 goto exit_init_data
;
6146 ustconsumer64_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6147 ust64_channel_rotate_pipe
);
6148 if (ustconsumer64_data
.channel_rotate_pipe
< 0) {
6150 goto exit_init_data
;
6154 * Init UST app hash table. Alloc hash table before this point since
6155 * cleanup() can get called after that point.
6157 if (ust_app_ht_alloc()) {
6158 ERR("Failed to allocate UST app hash table");
6160 goto exit_init_data
;
6164 * Initialize agent app hash table. We allocate the hash table here
6165 * since cleanup() can get called after this point.
6167 if (agent_app_ht_alloc()) {
6168 ERR("Failed to allocate Agent app hash table");
6170 goto exit_init_data
;
6174 * These actions must be executed as root. We do that *after* setting up
6175 * the sockets path because we MUST make the check for another daemon using
6176 * those paths *before* trying to set the kernel consumer sockets and init
6180 if (set_consumer_sockets(&kconsumer_data
)) {
6182 goto exit_init_data
;
6185 /* Setup kernel tracer */
6186 if (!config
.no_kernel
) {
6187 init_kernel_tracer();
6188 if (kernel_tracer_fd
>= 0) {
6189 ret
= syscall_init_table();
6191 ERR("Unable to populate syscall table. "
6192 "Syscall tracing won't work "
6193 "for this session daemon.");
6198 /* Set ulimit for open files */
6201 /* init lttng_fd tracking must be done after set_ulimit. */
6204 if (set_consumer_sockets(&ustconsumer64_data
)) {
6206 goto exit_init_data
;
6209 if (set_consumer_sockets(&ustconsumer32_data
)) {
6211 goto exit_init_data
;
6214 /* Setup the needed unix socket */
6215 if (init_daemon_socket()) {
6217 goto exit_init_data
;
6220 /* Set credentials to socket */
6221 if (is_root
&& set_permissions(config
.rundir
.value
)) {
6223 goto exit_init_data
;
6226 /* Get parent pid if -S, --sig-parent is specified. */
6227 if (config
.sig_parent
) {
6231 /* Setup the kernel pipe for waking up the kernel thread */
6232 if (is_root
&& !config
.no_kernel
) {
6233 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6235 goto exit_init_data
;
6239 /* Setup the thread apps communication pipe. */
6240 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6242 goto exit_init_data
;
6245 /* Setup the thread apps notify communication pipe. */
6246 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6248 goto exit_init_data
;
6251 /* Initialize global buffer per UID and PID registry. */
6252 buffer_reg_init_uid_registry();
6253 buffer_reg_init_pid_registry();
6255 /* Init UST command queue. */
6256 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6259 * Get session list pointer. This pointer MUST NOT be free'd. This list
6260 * is statically declared in session.c
6262 session_list_ptr
= session_get_list();
6266 /* Check for the application socket timeout env variable. */
6267 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6268 if (env_app_timeout
) {
6269 config
.app_socket_timeout
= atoi(env_app_timeout
);
6271 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6274 ret
= write_pidfile();
6276 ERR("Error in write_pidfile");
6278 goto exit_init_data
;
6281 /* Initialize communication library */
6283 /* Initialize TCP timeout values */
6284 lttcomm_inet_init();
6286 if (load_session_init_data(&load_info
) < 0) {
6288 goto exit_init_data
;
6290 load_info
->path
= config
.load_session_path
.value
;
6292 /* Create health-check thread. */
6293 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6294 thread_manage_health
, (void *) NULL
);
6297 PERROR("pthread_create health");
6303 * The rotation thread needs the notification thread to be ready before
6304 * creating the rotate_notification_channel, so we use this semaphore as
6305 * a rendez-vous point.
6307 sem_init(¬ification_thread_ready
, 0, 0);
6309 /* notification_thread_data acquires the pipes' read side. */
6310 notification_thread_handle
= notification_thread_handle_create(
6311 ust32_channel_monitor_pipe
,
6312 ust64_channel_monitor_pipe
,
6313 kernel_channel_monitor_pipe
,
6314 ¬ification_thread_ready
);
6315 if (!notification_thread_handle
) {
6317 ERR("Failed to create notification thread shared data");
6319 goto exit_notification
;
6322 /* Create notification thread. */
6323 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6324 thread_notification
, notification_thread_handle
);
6327 PERROR("pthread_create notification");
6330 goto exit_notification
;
6332 notification_thread_launched
= true;
6334 /* Create timer thread. */
6335 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
6336 sessiond_timer_thread
, &timer_thread_ctx
);
6339 PERROR("pthread_create timer");
6342 goto exit_notification
;
6344 timer_thread_launched
= true;
6346 /* rotation_thread_data acquires the pipes' read side. */
6347 rotation_thread_handle
= rotation_thread_handle_create(
6348 ust32_channel_rotate_pipe
,
6349 ust64_channel_rotate_pipe
,
6350 kernel_channel_rotate_pipe
,
6351 thread_quit_pipe
[0],
6352 rotation_timer_queue
,
6353 notification_thread_handle
,
6354 ¬ification_thread_ready
);
6355 if (!rotation_thread_handle
) {
6357 ERR("Failed to create rotation thread shared data");
6362 /* Create rotation thread. */
6363 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6364 thread_rotation
, rotation_thread_handle
);
6367 PERROR("pthread_create rotation");
6372 rotation_thread_launched
= true;
6374 /* Create thread to manage the client socket */
6375 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6376 thread_manage_clients
, (void *) NULL
);
6379 PERROR("pthread_create clients");
6385 /* Create thread to dispatch registration */
6386 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6387 thread_dispatch_ust_registration
, (void *) NULL
);
6390 PERROR("pthread_create dispatch");
6396 /* Create thread to manage application registration. */
6397 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6398 thread_registration_apps
, (void *) NULL
);
6401 PERROR("pthread_create registration");
6407 /* Create thread to manage application socket */
6408 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6409 thread_manage_apps
, (void *) NULL
);
6412 PERROR("pthread_create apps");
6418 /* Create thread to manage application notify socket */
6419 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6420 ust_thread_manage_notify
, (void *) NULL
);
6423 PERROR("pthread_create notify");
6426 goto exit_apps_notify
;
6429 /* Create agent registration thread. */
6430 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6431 agent_thread_manage_registration
, (void *) NULL
);
6434 PERROR("pthread_create agent");
6437 goto exit_agent_reg
;
6440 /* Don't start this thread if kernel tracing is not requested nor root */
6441 if (is_root
&& !config
.no_kernel
) {
6442 /* Create kernel thread to manage kernel event */
6443 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6444 thread_manage_kernel
, (void *) NULL
);
6447 PERROR("pthread_create kernel");
6454 /* Create session loading thread. */
6455 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6456 thread_load_session
, load_info
);
6459 PERROR("pthread_create load_session_thread");
6462 goto exit_load_session
;
6466 * This is where we start awaiting program completion (e.g. through
6467 * signal that asks threads to teardown).
6470 ret
= pthread_join(load_session_thread
, &status
);
6473 PERROR("pthread_join load_session_thread");
6478 if (is_root
&& !config
.no_kernel
) {
6479 ret
= pthread_join(kernel_thread
, &status
);
6482 PERROR("pthread_join");
6488 ret
= pthread_join(agent_reg_thread
, &status
);
6491 PERROR("pthread_join agent");
6496 ret
= pthread_join(apps_notify_thread
, &status
);
6499 PERROR("pthread_join apps notify");
6504 ret
= pthread_join(apps_thread
, &status
);
6507 PERROR("pthread_join apps");
6512 ret
= pthread_join(reg_apps_thread
, &status
);
6515 PERROR("pthread_join");
6521 * Join dispatch thread after joining reg_apps_thread to ensure
6522 * we don't leak applications in the queue.
6524 ret
= pthread_join(dispatch_thread
, &status
);
6527 PERROR("pthread_join");
6532 ret
= pthread_join(client_thread
, &status
);
6535 PERROR("pthread_join");
6542 sem_destroy(¬ification_thread_ready
);
6543 ret
= pthread_join(health_thread
, &status
);
6546 PERROR("pthread_join health thread");
6553 * Wait for all pending call_rcu work to complete before tearing
6554 * down data structures. call_rcu worker may be trying to
6555 * perform lookups in those structures.
6559 * sessiond_cleanup() is called when no other thread is running, except
6560 * the ht_cleanup thread, which is needed to destroy the hash tables.
6562 rcu_thread_online();
6566 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6567 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6568 * the queue is empty before shutting down the clean-up thread.
6573 * The teardown of the notification system is performed after the
6574 * session daemon's teardown in order to allow it to be notified
6575 * of the active session and channels at the moment of the teardown.
6577 if (notification_thread_handle
) {
6578 if (notification_thread_launched
) {
6579 notification_thread_command_quit(
6580 notification_thread_handle
);
6581 ret
= pthread_join(notification_thread
, &status
);
6584 PERROR("pthread_join notification thread");
6588 notification_thread_handle_destroy(notification_thread_handle
);
6591 if (rotation_thread_handle
) {
6592 if (rotation_thread_launched
) {
6593 ret
= pthread_join(rotation_thread
, &status
);
6596 PERROR("pthread_join rotation thread");
6600 rotation_thread_handle_destroy(rotation_thread_handle
);
6603 if (timer_thread_launched
) {
6604 kill(getpid(), LTTNG_SESSIOND_SIG_EXIT
);
6605 ret
= pthread_join(timer_thread
, &status
);
6608 PERROR("pthread_join timer thread");
6614 * After the rotation and timer thread have quit, we can safely destroy
6615 * the rotation_timer_queue.
6617 destroy_rotate_timer_queue(rotation_timer_queue
);
6619 rcu_thread_offline();
6620 rcu_unregister_thread();
6622 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6626 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6627 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6628 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6629 lttng_pipe_destroy(ust32_channel_rotate_pipe
);
6630 lttng_pipe_destroy(ust64_channel_rotate_pipe
);
6631 lttng_pipe_destroy(kernel_channel_rotate_pipe
);
6634 health_app_destroy(health_sessiond
);
6635 exit_health_sessiond_cleanup
:
6636 exit_create_run_as_worker_cleanup
:
6639 sessiond_cleanup_lock_file();
6640 sessiond_cleanup_options();
6642 exit_set_signal_handler
: