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>
53 #include "lttng-sessiond.h"
54 #include "buffer-registry.h"
61 #include "kernel-consumer.h"
65 #include "ust-consumer.h"
68 #include "health-sessiond.h"
69 #include "testpoint.h"
70 #include "ust-thread.h"
71 #include "agent-thread.h"
73 #include "load-session-thread.h"
74 #include "notification-thread.h"
75 #include "notification-thread-commands.h"
76 #include "rotation-thread.h"
77 #include "lttng-syscall.h"
79 #include "ht-cleanup.h"
80 #include "sessiond-config.h"
81 #include "sessiond-timer.h"
83 static const char *help_msg
=
84 #ifdef LTTNG_EMBED_HELP
85 #include <lttng-sessiond.8.h>
92 static pid_t ppid
; /* Parent PID for --sig-parent option */
93 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
94 static int lockfile_fd
= -1;
96 /* Set to 1 when a SIGUSR1 signal is received. */
97 static int recv_child_signal
;
99 static struct lttng_kernel_tracer_version kernel_tracer_version
;
100 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
103 * Consumer daemon specific control data. Every value not initialized here is
104 * set to 0 by the static definition.
106 static struct consumer_data kconsumer_data
= {
107 .type
= LTTNG_CONSUMER_KERNEL
,
110 .channel_monitor_pipe
= -1,
111 .channel_rotate_pipe
= -1,
112 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
113 .lock
= PTHREAD_MUTEX_INITIALIZER
,
114 .cond
= PTHREAD_COND_INITIALIZER
,
115 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
117 static struct consumer_data ustconsumer64_data
= {
118 .type
= LTTNG_CONSUMER64_UST
,
121 .channel_monitor_pipe
= -1,
122 .channel_rotate_pipe
= -1,
123 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
124 .lock
= PTHREAD_MUTEX_INITIALIZER
,
125 .cond
= PTHREAD_COND_INITIALIZER
,
126 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
128 static struct consumer_data ustconsumer32_data
= {
129 .type
= LTTNG_CONSUMER32_UST
,
132 .channel_monitor_pipe
= -1,
133 .channel_rotate_pipe
= -1,
134 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
135 .lock
= PTHREAD_MUTEX_INITIALIZER
,
136 .cond
= PTHREAD_COND_INITIALIZER
,
137 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
140 /* Command line options */
141 static const struct option long_options
[] = {
142 { "client-sock", required_argument
, 0, 'c' },
143 { "apps-sock", required_argument
, 0, 'a' },
144 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
145 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
146 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
147 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
148 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
149 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
150 { "consumerd32-path", required_argument
, 0, '\0' },
151 { "consumerd32-libdir", required_argument
, 0, '\0' },
152 { "consumerd64-path", required_argument
, 0, '\0' },
153 { "consumerd64-libdir", required_argument
, 0, '\0' },
154 { "daemonize", no_argument
, 0, 'd' },
155 { "background", no_argument
, 0, 'b' },
156 { "sig-parent", no_argument
, 0, 'S' },
157 { "help", no_argument
, 0, 'h' },
158 { "group", required_argument
, 0, 'g' },
159 { "version", no_argument
, 0, 'V' },
160 { "quiet", no_argument
, 0, 'q' },
161 { "verbose", no_argument
, 0, 'v' },
162 { "verbose-consumer", no_argument
, 0, '\0' },
163 { "no-kernel", no_argument
, 0, '\0' },
164 { "pidfile", required_argument
, 0, 'p' },
165 { "agent-tcp-port", required_argument
, 0, '\0' },
166 { "config", required_argument
, 0, 'f' },
167 { "load", required_argument
, 0, 'l' },
168 { "kmod-probes", required_argument
, 0, '\0' },
169 { "extra-kmod-probes", required_argument
, 0, '\0' },
173 struct sessiond_config config
;
175 /* Command line options to ignore from configuration file */
176 static const char *config_ignore_options
[] = { "help", "version", "config" };
178 /* Shared between threads */
179 static int dispatch_thread_exit
;
181 /* Sockets and FDs */
182 static int client_sock
= -1;
183 static int apps_sock
= -1;
184 int kernel_tracer_fd
= -1;
185 static int kernel_poll_pipe
[2] = { -1, -1 };
188 * Quit pipe for all threads. This permits a single cancellation point
189 * for all threads when receiving an event on the pipe.
191 static int thread_quit_pipe
[2] = { -1, -1 };
194 * This pipe is used to inform the thread managing application communication
195 * that a command is queued and ready to be processed.
197 static int apps_cmd_pipe
[2] = { -1, -1 };
199 int apps_cmd_notify_pipe
[2] = { -1, -1 };
201 /* Pthread, Mutexes and Semaphores */
202 static pthread_t apps_thread
;
203 static pthread_t apps_notify_thread
;
204 static pthread_t reg_apps_thread
;
205 static pthread_t client_thread
;
206 static pthread_t kernel_thread
;
207 static pthread_t dispatch_thread
;
208 static pthread_t health_thread
;
209 static pthread_t ht_cleanup_thread
;
210 static pthread_t agent_reg_thread
;
211 static pthread_t load_session_thread
;
212 static pthread_t notification_thread
;
213 static pthread_t rotation_thread
;
214 static pthread_t timer_thread
;
217 * UST registration command queue. This queue is tied with a futex and uses a N
218 * wakers / 1 waiter implemented and detailed in futex.c/.h
220 * The thread_registration_apps and thread_dispatch_ust_registration uses this
221 * queue along with the wait/wake scheme. The thread_manage_apps receives down
222 * the line new application socket and monitors it for any I/O error or clean
223 * close that triggers an unregistration of the application.
225 static struct ust_cmd_queue ust_cmd_queue
;
228 * Pointer initialized before thread creation.
230 * This points to the tracing session list containing the session count and a
231 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
232 * MUST NOT be taken if you call a public function in session.c.
234 * The lock is nested inside the structure: session_list_ptr->lock. Please use
235 * session_lock_list and session_unlock_list for lock acquisition.
237 static struct ltt_session_list
*session_list_ptr
;
239 int ust_consumerd64_fd
= -1;
240 int ust_consumerd32_fd
= -1;
242 static const char *module_proc_lttng
= "/proc/lttng";
245 * Consumer daemon state which is changed when spawning it, killing it or in
246 * case of a fatal error.
248 enum consumerd_state
{
249 CONSUMER_STARTED
= 1,
250 CONSUMER_STOPPED
= 2,
255 * This consumer daemon state is used to validate if a client command will be
256 * able to reach the consumer. If not, the client is informed. For instance,
257 * doing a "lttng start" when the consumer state is set to ERROR will return an
258 * error to the client.
260 * The following example shows a possible race condition of this scheme:
262 * consumer thread error happens
264 * client cmd checks state -> still OK
265 * consumer thread exit, sets error
266 * client cmd try to talk to consumer
269 * However, since the consumer is a different daemon, we have no way of making
270 * sure the command will reach it safely even with this state flag. This is why
271 * we consider that up to the state validation during command processing, the
272 * command is safe. After that, we can not guarantee the correctness of the
273 * client request vis-a-vis the consumer.
275 static enum consumerd_state ust_consumerd_state
;
276 static enum consumerd_state kernel_consumerd_state
;
278 /* Set in main() with the current page size. */
281 /* Application health monitoring */
282 struct health_app
*health_sessiond
;
284 /* Am I root or not. */
285 int is_root
; /* Set to 1 if the daemon is running as root */
287 const char * const config_section_name
= "sessiond";
289 /* Load session thread information to operate. */
290 struct load_session_thread_data
*load_info
;
292 /* Notification thread handle. */
293 struct notification_thread_handle
*notification_thread_handle
;
295 /* Rotation thread handle. */
296 struct rotation_thread_handle
*rotation_thread_handle
;
298 /* Global hash tables */
299 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
302 * The initialization of the session daemon is done in multiple phases.
304 * While all threads are launched near-simultaneously, only some of them
305 * are needed to ensure the session daemon can start to respond to client
308 * There are two important guarantees that we wish to offer with respect
309 * to the initialisation of the session daemon:
310 * - When the daemonize/background launcher process exits, the sessiond
311 * is fully able to respond to client requests,
312 * - Auto-loaded sessions are visible to clients.
314 * In order to achieve this, a number of support threads have to be launched
315 * to allow the "client" thread to function properly. Moreover, since the
316 * "load session" thread needs the client thread, we must provide a way
317 * for the "load session" thread to know that the "client" thread is up
320 * Hence, the support threads decrement the lttng_sessiond_ready counter
321 * while the "client" threads waits for it to reach 0. Once the "client" thread
322 * unblocks, it posts the message_thread_ready semaphore which allows the
323 * "load session" thread to progress.
325 * This implies that the "load session" thread is the last to be initialized
326 * and will explicitly call sessiond_signal_parents(), which signals the parents
327 * that the session daemon is fully initialized.
329 * The four (4) support threads are:
331 * - notification_thread
335 #define NR_LTTNG_SESSIOND_SUPPORT_THREADS 4
336 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_SUPPORT_THREADS
;
338 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
340 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
343 /* Notify parents that we are ready for cmd and health check */
345 void sessiond_signal_parents(void)
348 * Notify parent pid that we are ready to accept command
349 * for client side. This ppid is the one from the
350 * external process that spawned us.
352 if (config
.sig_parent
) {
357 * Notify the parent of the fork() process that we are
360 if (config
.daemonize
|| config
.background
) {
361 kill(child_ppid
, SIGUSR1
);
366 void sessiond_notify_ready(void)
369 * This memory barrier is paired with the one performed by
370 * the client thread after it has seen that 'lttng_sessiond_ready' is 0.
372 * The purpose of these memory barriers is to ensure that all
373 * initialization operations of the various threads that call this
374 * function to signal that they are ready are commited/published
375 * before the client thread can see the 'lttng_sessiond_ready' counter
378 * Note that this could be a 'write' memory barrier, but a full barrier
379 * is used in case the code using this utility changes. The performance
380 * implications of this choice are minimal since this is a slow path.
383 uatomic_sub(<tng_sessiond_ready
, 1);
387 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
394 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
400 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
412 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
414 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
416 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
420 * Init thread quit pipe.
422 * Return -1 on error or 0 if all pipes are created.
424 static int __init_thread_quit_pipe(int *a_pipe
)
430 PERROR("thread quit pipe");
434 for (i
= 0; i
< 2; i
++) {
435 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
446 static int init_thread_quit_pipe(void)
448 return __init_thread_quit_pipe(thread_quit_pipe
);
452 * Stop all threads by closing the thread quit pipe.
454 static void stop_threads(void)
458 /* Stopping all threads */
459 DBG("Terminating all threads");
460 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
462 ERR("write error on thread quit pipe");
465 /* Dispatch thread */
466 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
467 futex_nto1_wake(&ust_cmd_queue
.futex
);
471 * Close every consumer sockets.
473 static void close_consumer_sockets(void)
477 if (kconsumer_data
.err_sock
>= 0) {
478 ret
= close(kconsumer_data
.err_sock
);
480 PERROR("kernel consumer err_sock close");
483 if (ustconsumer32_data
.err_sock
>= 0) {
484 ret
= close(ustconsumer32_data
.err_sock
);
486 PERROR("UST consumerd32 err_sock close");
489 if (ustconsumer64_data
.err_sock
>= 0) {
490 ret
= close(ustconsumer64_data
.err_sock
);
492 PERROR("UST consumerd64 err_sock close");
495 if (kconsumer_data
.cmd_sock
>= 0) {
496 ret
= close(kconsumer_data
.cmd_sock
);
498 PERROR("kernel consumer cmd_sock close");
501 if (ustconsumer32_data
.cmd_sock
>= 0) {
502 ret
= close(ustconsumer32_data
.cmd_sock
);
504 PERROR("UST consumerd32 cmd_sock close");
507 if (ustconsumer64_data
.cmd_sock
>= 0) {
508 ret
= close(ustconsumer64_data
.cmd_sock
);
510 PERROR("UST consumerd64 cmd_sock close");
513 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
514 ret
= close(kconsumer_data
.channel_monitor_pipe
);
516 PERROR("kernel consumer channel monitor pipe close");
519 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
520 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
522 PERROR("UST consumerd32 channel monitor pipe close");
525 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
526 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
528 PERROR("UST consumerd64 channel monitor pipe close");
531 if (kconsumer_data
.channel_rotate_pipe
>= 0) {
532 ret
= close(kconsumer_data
.channel_rotate_pipe
);
534 PERROR("kernel consumer channel rotate pipe close");
537 if (ustconsumer32_data
.channel_rotate_pipe
>= 0) {
538 ret
= close(ustconsumer32_data
.channel_rotate_pipe
);
540 PERROR("UST consumerd32 channel rotate pipe close");
543 if (ustconsumer64_data
.channel_rotate_pipe
>= 0) {
544 ret
= close(ustconsumer64_data
.channel_rotate_pipe
);
546 PERROR("UST consumerd64 channel rotate pipe close");
552 * Wait on consumer process termination.
554 * Need to be called with the consumer data lock held or from a context
555 * ensuring no concurrent access to data (e.g: cleanup).
557 static void wait_consumer(struct consumer_data
*consumer_data
)
562 if (consumer_data
->pid
<= 0) {
566 DBG("Waiting for complete teardown of consumerd (PID: %d)",
568 ret
= waitpid(consumer_data
->pid
, &status
, 0);
570 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
571 } else if (!WIFEXITED(status
)) {
572 ERR("consumerd termination with error: %d",
575 consumer_data
->pid
= 0;
579 * Cleanup the session daemon's data structures.
581 static void sessiond_cleanup(void)
584 struct ltt_session
*sess
, *stmp
;
586 DBG("Cleanup sessiond");
589 * Close the thread quit pipe. It has already done its job,
590 * since we are now called.
592 utils_close_pipe(thread_quit_pipe
);
595 * If config.pid_file_path.value is undefined, the default file will be
596 * wiped when removing the rundir.
598 if (config
.pid_file_path
.value
) {
599 ret
= remove(config
.pid_file_path
.value
);
601 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
605 DBG("Removing sessiond and consumerd content of directory %s",
606 config
.rundir
.value
);
609 DBG("Removing %s", config
.pid_file_path
.value
);
610 (void) unlink(config
.pid_file_path
.value
);
612 DBG("Removing %s", config
.agent_port_file_path
.value
);
613 (void) unlink(config
.agent_port_file_path
.value
);
616 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
617 (void) unlink(kconsumer_data
.err_unix_sock_path
);
619 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
620 (void) rmdir(config
.kconsumerd_path
.value
);
622 /* ust consumerd 32 */
623 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
624 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
626 DBG("Removing directory %s", config
.consumerd32_path
.value
);
627 (void) rmdir(config
.consumerd32_path
.value
);
629 /* ust consumerd 64 */
630 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
631 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
633 DBG("Removing directory %s", config
.consumerd64_path
.value
);
634 (void) rmdir(config
.consumerd64_path
.value
);
636 DBG("Cleaning up all sessions");
638 /* Destroy session list mutex */
639 if (session_list_ptr
!= NULL
) {
640 pthread_mutex_destroy(&session_list_ptr
->lock
);
642 /* Cleanup ALL session */
643 cds_list_for_each_entry_safe(sess
, stmp
,
644 &session_list_ptr
->head
, list
) {
645 cmd_destroy_session(sess
, kernel_poll_pipe
[1],
646 notification_thread_handle
);
650 wait_consumer(&kconsumer_data
);
651 wait_consumer(&ustconsumer64_data
);
652 wait_consumer(&ustconsumer32_data
);
654 DBG("Cleaning up all agent apps");
655 agent_app_ht_clean();
657 DBG("Closing all UST sockets");
658 ust_app_clean_list();
659 buffer_reg_destroy_registries();
661 if (is_root
&& !config
.no_kernel
) {
662 DBG2("Closing kernel fd");
663 if (kernel_tracer_fd
>= 0) {
664 ret
= close(kernel_tracer_fd
);
669 DBG("Unloading kernel modules");
670 modprobe_remove_lttng_all();
674 close_consumer_sockets();
677 load_session_destroy_data(load_info
);
682 * We do NOT rmdir rundir because there are other processes
683 * using it, for instance lttng-relayd, which can start in
684 * parallel with this teardown.
689 * Cleanup the daemon's option data structures.
691 static void sessiond_cleanup_options(void)
693 DBG("Cleaning up options");
695 sessiond_config_fini(&config
);
697 run_as_destroy_worker();
701 * Send data on a unix socket using the liblttsessiondcomm API.
703 * Return lttcomm error code.
705 static int send_unix_sock(int sock
, void *buf
, size_t len
)
707 /* Check valid length */
712 return lttcomm_send_unix_sock(sock
, buf
, len
);
716 * Free memory of a command context structure.
718 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
720 DBG("Clean command context structure");
722 if ((*cmd_ctx
)->llm
) {
723 free((*cmd_ctx
)->llm
);
725 if ((*cmd_ctx
)->lsm
) {
726 free((*cmd_ctx
)->lsm
);
734 * Notify UST applications using the shm mmap futex.
736 static int notify_ust_apps(int active
)
740 DBG("Notifying applications of session daemon state: %d", active
);
742 /* See shm.c for this call implying mmap, shm and futex calls */
743 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
744 if (wait_shm_mmap
== NULL
) {
748 /* Wake waiting process */
749 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
751 /* Apps notified successfully */
759 * Setup the outgoing data buffer for the response (llm) by allocating the
760 * right amount of memory and copying the original information from the lsm
763 * Return 0 on success, negative value on error.
765 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
766 const void *payload_buf
, size_t payload_len
,
767 const void *cmd_header_buf
, size_t cmd_header_len
)
770 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
771 const size_t cmd_header_offset
= header_len
;
772 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
773 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
775 cmd_ctx
->llm
= zmalloc(total_msg_size
);
777 if (cmd_ctx
->llm
== NULL
) {
783 /* Copy common data */
784 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
785 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
786 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
787 cmd_ctx
->llm
->data_size
= payload_len
;
788 cmd_ctx
->lttng_msg_size
= total_msg_size
;
790 /* Copy command header */
791 if (cmd_header_len
) {
792 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
798 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
807 * Version of setup_lttng_msg() without command header.
809 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
810 void *payload_buf
, size_t payload_len
)
812 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
815 * Update the kernel poll set of all channel fd available over all tracing
816 * session. Add the wakeup pipe at the end of the set.
818 static int update_kernel_poll(struct lttng_poll_event
*events
)
821 struct ltt_session
*session
;
822 struct ltt_kernel_channel
*channel
;
824 DBG("Updating kernel poll set");
827 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
828 session_lock(session
);
829 if (session
->kernel_session
== NULL
) {
830 session_unlock(session
);
834 cds_list_for_each_entry(channel
,
835 &session
->kernel_session
->channel_list
.head
, list
) {
836 /* Add channel fd to the kernel poll set */
837 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
839 session_unlock(session
);
842 DBG("Channel fd %d added to kernel set", channel
->fd
);
844 session_unlock(session
);
846 session_unlock_list();
851 session_unlock_list();
856 * Find the channel fd from 'fd' over all tracing session. When found, check
857 * for new channel stream and send those stream fds to the kernel consumer.
859 * Useful for CPU hotplug feature.
861 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
864 struct ltt_session
*session
;
865 struct ltt_kernel_session
*ksess
;
866 struct ltt_kernel_channel
*channel
;
868 DBG("Updating kernel streams for channel fd %d", fd
);
871 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
872 session_lock(session
);
873 if (session
->kernel_session
== NULL
) {
874 session_unlock(session
);
877 ksess
= session
->kernel_session
;
879 cds_list_for_each_entry(channel
,
880 &ksess
->channel_list
.head
, list
) {
881 struct lttng_ht_iter iter
;
882 struct consumer_socket
*socket
;
884 if (channel
->fd
!= fd
) {
887 DBG("Channel found, updating kernel streams");
888 ret
= kernel_open_channel_stream(channel
);
892 /* Update the stream global counter */
893 ksess
->stream_count_global
+= ret
;
896 * Have we already sent fds to the consumer? If yes, it
897 * means that tracing is started so it is safe to send
898 * our updated stream fds.
900 if (ksess
->consumer_fds_sent
!= 1
901 || ksess
->consumer
== NULL
) {
907 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
908 &iter
.iter
, socket
, node
.node
) {
909 pthread_mutex_lock(socket
->lock
);
910 ret
= kernel_consumer_send_channel_streams(socket
,
912 session
->output_traces
? 1 : 0);
913 pthread_mutex_unlock(socket
->lock
);
921 session_unlock(session
);
923 session_unlock_list();
927 session_unlock(session
);
928 session_unlock_list();
933 * For each tracing session, update newly registered apps. The session list
934 * lock MUST be acquired before calling this.
936 static void update_ust_app(int app_sock
)
938 struct ltt_session
*sess
, *stmp
;
940 /* Consumer is in an ERROR state. Stop any application update. */
941 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
942 /* Stop the update process since the consumer is dead. */
946 /* For all tracing session(s) */
947 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
951 if (!sess
->ust_session
) {
956 assert(app_sock
>= 0);
957 app
= ust_app_find_by_sock(app_sock
);
960 * Application can be unregistered before so
961 * this is possible hence simply stopping the
964 DBG3("UST app update failed to find app sock %d",
968 ust_app_global_update(sess
->ust_session
, app
);
972 session_unlock(sess
);
977 * This thread manage event coming from the kernel.
979 * Features supported in this thread:
982 static void *thread_manage_kernel(void *data
)
984 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
985 uint32_t revents
, nb_fd
;
987 struct lttng_poll_event events
;
989 DBG("[thread] Thread manage kernel started");
991 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
994 * This first step of the while is to clean this structure which could free
995 * non NULL pointers so initialize it before the loop.
997 lttng_poll_init(&events
);
999 if (testpoint(sessiond_thread_manage_kernel
)) {
1000 goto error_testpoint
;
1003 health_code_update();
1005 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1006 goto error_testpoint
;
1010 health_code_update();
1012 if (update_poll_flag
== 1) {
1013 /* Clean events object. We are about to populate it again. */
1014 lttng_poll_clean(&events
);
1016 ret
= sessiond_set_thread_pollset(&events
, 2);
1018 goto error_poll_create
;
1021 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1026 /* This will add the available kernel channel if any. */
1027 ret
= update_kernel_poll(&events
);
1031 update_poll_flag
= 0;
1034 DBG("Thread kernel polling");
1036 /* Poll infinite value of time */
1038 health_poll_entry();
1039 ret
= lttng_poll_wait(&events
, -1);
1040 DBG("Thread kernel return from poll on %d fds",
1041 LTTNG_POLL_GETNB(&events
));
1045 * Restart interrupted system call.
1047 if (errno
== EINTR
) {
1051 } else if (ret
== 0) {
1052 /* Should not happen since timeout is infinite */
1053 ERR("Return value of poll is 0 with an infinite timeout.\n"
1054 "This should not have happened! Continuing...");
1060 for (i
= 0; i
< nb_fd
; i
++) {
1061 /* Fetch once the poll data */
1062 revents
= LTTNG_POLL_GETEV(&events
, i
);
1063 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1065 health_code_update();
1068 /* No activity for this FD (poll implementation). */
1072 /* Thread quit pipe has been closed. Killing thread. */
1073 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1079 /* Check for data on kernel pipe */
1080 if (revents
& LPOLLIN
) {
1081 if (pollfd
== kernel_poll_pipe
[0]) {
1082 (void) lttng_read(kernel_poll_pipe
[0],
1085 * Ret value is useless here, if this pipe gets any actions an
1086 * update is required anyway.
1088 update_poll_flag
= 1;
1092 * New CPU detected by the kernel. Adding kernel stream to
1093 * kernel session and updating the kernel consumer
1095 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1101 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1102 update_poll_flag
= 1;
1105 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1113 lttng_poll_clean(&events
);
1116 utils_close_pipe(kernel_poll_pipe
);
1117 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1120 ERR("Health error occurred in %s", __func__
);
1121 WARN("Kernel thread died unexpectedly. "
1122 "Kernel tracing can continue but CPU hotplug is disabled.");
1124 health_unregister(health_sessiond
);
1125 DBG("Kernel thread dying");
1130 * Signal pthread condition of the consumer data that the thread.
1132 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1134 pthread_mutex_lock(&data
->cond_mutex
);
1137 * The state is set before signaling. It can be any value, it's the waiter
1138 * job to correctly interpret this condition variable associated to the
1139 * consumer pthread_cond.
1141 * A value of 0 means that the corresponding thread of the consumer data
1142 * was not started. 1 indicates that the thread has started and is ready
1143 * for action. A negative value means that there was an error during the
1146 data
->consumer_thread_is_ready
= state
;
1147 (void) pthread_cond_signal(&data
->cond
);
1149 pthread_mutex_unlock(&data
->cond_mutex
);
1153 * This thread manage the consumer error sent back to the session daemon.
1155 static void *thread_manage_consumer(void *data
)
1157 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1158 uint32_t revents
, nb_fd
;
1159 enum lttcomm_return_code code
;
1160 struct lttng_poll_event events
;
1161 struct consumer_data
*consumer_data
= data
;
1162 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1164 DBG("[thread] Manage consumer started");
1166 rcu_register_thread();
1167 rcu_thread_online();
1169 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1171 health_code_update();
1174 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1175 * metadata_sock. Nothing more will be added to this poll set.
1177 ret
= sessiond_set_thread_pollset(&events
, 3);
1183 * The error socket here is already in a listening state which was done
1184 * just before spawning this thread to avoid a race between the consumer
1185 * daemon exec trying to connect and the listen() call.
1187 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1192 health_code_update();
1194 /* Infinite blocking call, waiting for transmission */
1196 health_poll_entry();
1198 if (testpoint(sessiond_thread_manage_consumer
)) {
1202 ret
= lttng_poll_wait(&events
, -1);
1206 * Restart interrupted system call.
1208 if (errno
== EINTR
) {
1216 for (i
= 0; i
< nb_fd
; i
++) {
1217 /* Fetch once the poll data */
1218 revents
= LTTNG_POLL_GETEV(&events
, i
);
1219 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1221 health_code_update();
1224 /* No activity for this FD (poll implementation). */
1228 /* Thread quit pipe has been closed. Killing thread. */
1229 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1235 /* Event on the registration socket */
1236 if (pollfd
== consumer_data
->err_sock
) {
1237 if (revents
& LPOLLIN
) {
1239 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1240 ERR("consumer err socket poll error");
1243 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1249 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1255 * Set the CLOEXEC flag. Return code is useless because either way, the
1258 (void) utils_set_fd_cloexec(sock
);
1260 health_code_update();
1262 DBG2("Receiving code from consumer err_sock");
1264 /* Getting status code from kconsumerd */
1265 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1266 sizeof(enum lttcomm_return_code
));
1271 health_code_update();
1272 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1273 ERR("consumer error when waiting for SOCK_READY : %s",
1274 lttcomm_get_readable_code(-code
));
1278 /* Connect both command and metadata sockets. */
1279 consumer_data
->cmd_sock
=
1280 lttcomm_connect_unix_sock(
1281 consumer_data
->cmd_unix_sock_path
);
1282 consumer_data
->metadata_fd
=
1283 lttcomm_connect_unix_sock(
1284 consumer_data
->cmd_unix_sock_path
);
1285 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1286 PERROR("consumer connect cmd socket");
1287 /* On error, signal condition and quit. */
1288 signal_consumer_condition(consumer_data
, -1);
1292 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1294 /* Create metadata socket lock. */
1295 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1296 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1297 PERROR("zmalloc pthread mutex");
1300 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1302 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1303 DBG("Consumer metadata socket ready (fd: %d)",
1304 consumer_data
->metadata_fd
);
1307 * Remove the consumerd error sock since we've established a connection.
1309 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1314 /* Add new accepted error socket. */
1315 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1320 /* Add metadata socket that is successfully connected. */
1321 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1322 LPOLLIN
| LPOLLRDHUP
);
1327 health_code_update();
1330 * Transfer the write-end of the channel monitoring and rotate pipe
1331 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1332 * SET_CHANNEL_ROTATE_PIPE commands.
1334 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1335 if (!cmd_socket_wrapper
) {
1338 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1340 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1341 consumer_data
->channel_monitor_pipe
);
1346 ret
= consumer_send_channel_rotate_pipe(cmd_socket_wrapper
,
1347 consumer_data
->channel_rotate_pipe
);
1352 /* Discard the socket wrapper as it is no longer needed. */
1353 consumer_destroy_socket(cmd_socket_wrapper
);
1354 cmd_socket_wrapper
= NULL
;
1356 /* The thread is completely initialized, signal that it is ready. */
1357 signal_consumer_condition(consumer_data
, 1);
1359 /* Infinite blocking call, waiting for transmission */
1362 health_code_update();
1364 /* Exit the thread because the thread quit pipe has been triggered. */
1366 /* Not a health error. */
1371 health_poll_entry();
1372 ret
= lttng_poll_wait(&events
, -1);
1376 * Restart interrupted system call.
1378 if (errno
== EINTR
) {
1386 for (i
= 0; i
< nb_fd
; i
++) {
1387 /* Fetch once the poll data */
1388 revents
= LTTNG_POLL_GETEV(&events
, i
);
1389 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1391 health_code_update();
1394 /* No activity for this FD (poll implementation). */
1399 * Thread quit pipe has been triggered, flag that we should stop
1400 * but continue the current loop to handle potential data from
1403 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1405 if (pollfd
== sock
) {
1406 /* Event on the consumerd socket */
1407 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1408 && !(revents
& LPOLLIN
)) {
1409 ERR("consumer err socket second poll error");
1412 health_code_update();
1413 /* Wait for any kconsumerd error */
1414 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1415 sizeof(enum lttcomm_return_code
));
1417 ERR("consumer closed the command socket");
1421 ERR("consumer return code : %s",
1422 lttcomm_get_readable_code(-code
));
1425 } else if (pollfd
== consumer_data
->metadata_fd
) {
1426 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1427 && !(revents
& LPOLLIN
)) {
1428 ERR("consumer err metadata socket second poll error");
1431 /* UST metadata requests */
1432 ret
= ust_consumer_metadata_request(
1433 &consumer_data
->metadata_sock
);
1435 ERR("Handling metadata request");
1439 /* No need for an else branch all FDs are tested prior. */
1441 health_code_update();
1447 * We lock here because we are about to close the sockets and some other
1448 * thread might be using them so get exclusive access which will abort all
1449 * other consumer command by other threads.
1451 pthread_mutex_lock(&consumer_data
->lock
);
1453 /* Immediately set the consumerd state to stopped */
1454 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1455 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1456 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1457 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1458 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1460 /* Code flow error... */
1464 if (consumer_data
->err_sock
>= 0) {
1465 ret
= close(consumer_data
->err_sock
);
1469 consumer_data
->err_sock
= -1;
1471 if (consumer_data
->cmd_sock
>= 0) {
1472 ret
= close(consumer_data
->cmd_sock
);
1476 consumer_data
->cmd_sock
= -1;
1478 if (consumer_data
->metadata_sock
.fd_ptr
&&
1479 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1480 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1492 unlink(consumer_data
->err_unix_sock_path
);
1493 unlink(consumer_data
->cmd_unix_sock_path
);
1494 pthread_mutex_unlock(&consumer_data
->lock
);
1496 /* Cleanup metadata socket mutex. */
1497 if (consumer_data
->metadata_sock
.lock
) {
1498 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1499 free(consumer_data
->metadata_sock
.lock
);
1501 lttng_poll_clean(&events
);
1503 if (cmd_socket_wrapper
) {
1504 consumer_destroy_socket(cmd_socket_wrapper
);
1509 ERR("Health error occurred in %s", __func__
);
1511 health_unregister(health_sessiond
);
1512 DBG("consumer thread cleanup completed");
1514 rcu_thread_offline();
1515 rcu_unregister_thread();
1521 * This thread receives application command sockets (FDs) on the
1522 * apps_cmd_pipe and waits (polls) on them until they are closed
1523 * or an error occurs.
1525 * At that point, it flushes the data (tracing and metadata) associated
1526 * with this application and tears down ust app sessions and other
1527 * associated data structures through ust_app_unregister().
1529 * Note that this thread never sends commands to the applications
1530 * through the command sockets; it merely listens for hang-ups
1531 * and errors on those sockets and cleans-up as they occur.
1533 static void *thread_manage_apps(void *data
)
1535 int i
, ret
, pollfd
, err
= -1;
1537 uint32_t revents
, nb_fd
;
1538 struct lttng_poll_event events
;
1540 DBG("[thread] Manage application started");
1542 rcu_register_thread();
1543 rcu_thread_online();
1545 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1547 if (testpoint(sessiond_thread_manage_apps
)) {
1548 goto error_testpoint
;
1551 health_code_update();
1553 ret
= sessiond_set_thread_pollset(&events
, 2);
1555 goto error_poll_create
;
1558 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1563 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1567 health_code_update();
1570 DBG("Apps thread polling");
1572 /* Inifinite blocking call, waiting for transmission */
1574 health_poll_entry();
1575 ret
= lttng_poll_wait(&events
, -1);
1576 DBG("Apps thread return from poll on %d fds",
1577 LTTNG_POLL_GETNB(&events
));
1581 * Restart interrupted system call.
1583 if (errno
== EINTR
) {
1591 for (i
= 0; i
< nb_fd
; i
++) {
1592 /* Fetch once the poll data */
1593 revents
= LTTNG_POLL_GETEV(&events
, i
);
1594 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1596 health_code_update();
1599 /* No activity for this FD (poll implementation). */
1603 /* Thread quit pipe has been closed. Killing thread. */
1604 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1610 /* Inspect the apps cmd pipe */
1611 if (pollfd
== apps_cmd_pipe
[0]) {
1612 if (revents
& LPOLLIN
) {
1616 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1617 if (size_ret
< sizeof(sock
)) {
1618 PERROR("read apps cmd pipe");
1622 health_code_update();
1625 * Since this is a command socket (write then read),
1626 * we only monitor the error events of the socket.
1628 ret
= lttng_poll_add(&events
, sock
,
1629 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1634 DBG("Apps with sock %d added to poll set", sock
);
1635 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1636 ERR("Apps command pipe error");
1639 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1644 * At this point, we know that a registered application made
1645 * the event at poll_wait.
1647 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1648 /* Removing from the poll set */
1649 ret
= lttng_poll_del(&events
, pollfd
);
1654 /* Socket closed on remote end. */
1655 ust_app_unregister(pollfd
);
1657 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1662 health_code_update();
1668 lttng_poll_clean(&events
);
1671 utils_close_pipe(apps_cmd_pipe
);
1672 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1675 * We don't clean the UST app hash table here since already registered
1676 * applications can still be controlled so let them be until the session
1677 * daemon dies or the applications stop.
1682 ERR("Health error occurred in %s", __func__
);
1684 health_unregister(health_sessiond
);
1685 DBG("Application communication apps thread cleanup complete");
1686 rcu_thread_offline();
1687 rcu_unregister_thread();
1692 * Send a socket to a thread This is called from the dispatch UST registration
1693 * thread once all sockets are set for the application.
1695 * The sock value can be invalid, we don't really care, the thread will handle
1696 * it and make the necessary cleanup if so.
1698 * On success, return 0 else a negative value being the errno message of the
1701 static int send_socket_to_thread(int fd
, int sock
)
1706 * It's possible that the FD is set as invalid with -1 concurrently just
1707 * before calling this function being a shutdown state of the thread.
1714 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1715 if (ret
< sizeof(sock
)) {
1716 PERROR("write apps pipe %d", fd
);
1723 /* All good. Don't send back the write positive ret value. */
1730 * Sanitize the wait queue of the dispatch registration thread meaning removing
1731 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1732 * notify socket is never received.
1734 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1736 int ret
, nb_fd
= 0, i
;
1737 unsigned int fd_added
= 0;
1738 struct lttng_poll_event events
;
1739 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1743 lttng_poll_init(&events
);
1745 /* Just skip everything for an empty queue. */
1746 if (!wait_queue
->count
) {
1750 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1755 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1756 &wait_queue
->head
, head
) {
1757 assert(wait_node
->app
);
1758 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1759 LPOLLHUP
| LPOLLERR
);
1772 * Poll but don't block so we can quickly identify the faulty events and
1773 * clean them afterwards from the wait queue.
1775 ret
= lttng_poll_wait(&events
, 0);
1781 for (i
= 0; i
< nb_fd
; i
++) {
1782 /* Get faulty FD. */
1783 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1784 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1787 /* No activity for this FD (poll implementation). */
1791 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1792 &wait_queue
->head
, head
) {
1793 if (pollfd
== wait_node
->app
->sock
&&
1794 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1795 cds_list_del(&wait_node
->head
);
1796 wait_queue
->count
--;
1797 ust_app_destroy(wait_node
->app
);
1800 * Silence warning of use-after-free in
1801 * cds_list_for_each_entry_safe which uses
1802 * __typeof__(*wait_node).
1807 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1814 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1818 lttng_poll_clean(&events
);
1822 lttng_poll_clean(&events
);
1824 ERR("Unable to sanitize wait queue");
1829 * Dispatch request from the registration threads to the application
1830 * communication thread.
1832 static void *thread_dispatch_ust_registration(void *data
)
1835 struct cds_wfcq_node
*node
;
1836 struct ust_command
*ust_cmd
= NULL
;
1837 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1838 struct ust_reg_wait_queue wait_queue
= {
1842 rcu_register_thread();
1844 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1846 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1847 goto error_testpoint
;
1850 health_code_update();
1852 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1854 DBG("[thread] Dispatch UST command started");
1857 health_code_update();
1859 /* Atomically prepare the queue futex */
1860 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1862 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1867 struct ust_app
*app
= NULL
;
1871 * Make sure we don't have node(s) that have hung up before receiving
1872 * the notify socket. This is to clean the list in order to avoid
1873 * memory leaks from notify socket that are never seen.
1875 sanitize_wait_queue(&wait_queue
);
1877 health_code_update();
1878 /* Dequeue command for registration */
1879 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1881 DBG("Woken up but nothing in the UST command queue");
1882 /* Continue thread execution */
1886 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1888 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1889 " gid:%d sock:%d name:%s (version %d.%d)",
1890 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1891 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1892 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1893 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1895 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1896 wait_node
= zmalloc(sizeof(*wait_node
));
1898 PERROR("zmalloc wait_node dispatch");
1899 ret
= close(ust_cmd
->sock
);
1901 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1903 lttng_fd_put(LTTNG_FD_APPS
, 1);
1907 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1909 /* Create application object if socket is CMD. */
1910 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1912 if (!wait_node
->app
) {
1913 ret
= close(ust_cmd
->sock
);
1915 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1917 lttng_fd_put(LTTNG_FD_APPS
, 1);
1923 * Add application to the wait queue so we can set the notify
1924 * socket before putting this object in the global ht.
1926 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1931 * We have to continue here since we don't have the notify
1932 * socket and the application MUST be added to the hash table
1933 * only at that moment.
1938 * Look for the application in the local wait queue and set the
1939 * notify socket if found.
1941 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1942 &wait_queue
.head
, head
) {
1943 health_code_update();
1944 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1945 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1946 cds_list_del(&wait_node
->head
);
1948 app
= wait_node
->app
;
1950 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1956 * With no application at this stage the received socket is
1957 * basically useless so close it before we free the cmd data
1958 * structure for good.
1961 ret
= close(ust_cmd
->sock
);
1963 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1965 lttng_fd_put(LTTNG_FD_APPS
, 1);
1972 * @session_lock_list
1974 * Lock the global session list so from the register up to the
1975 * registration done message, no thread can see the application
1976 * and change its state.
1978 session_lock_list();
1982 * Add application to the global hash table. This needs to be
1983 * done before the update to the UST registry can locate the
1988 /* Set app version. This call will print an error if needed. */
1989 (void) ust_app_version(app
);
1991 /* Send notify socket through the notify pipe. */
1992 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1996 session_unlock_list();
1998 * No notify thread, stop the UST tracing. However, this is
1999 * not an internal error of the this thread thus setting
2000 * the health error code to a normal exit.
2007 * Update newly registered application with the tracing
2008 * registry info already enabled information.
2010 update_ust_app(app
->sock
);
2013 * Don't care about return value. Let the manage apps threads
2014 * handle app unregistration upon socket close.
2016 (void) ust_app_register_done(app
);
2019 * Even if the application socket has been closed, send the app
2020 * to the thread and unregistration will take place at that
2023 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2026 session_unlock_list();
2028 * No apps. thread, stop the UST tracing. However, this is
2029 * not an internal error of the this thread thus setting
2030 * the health error code to a normal exit.
2037 session_unlock_list();
2039 } while (node
!= NULL
);
2041 health_poll_entry();
2042 /* Futex wait on queue. Blocking call on futex() */
2043 futex_nto1_wait(&ust_cmd_queue
.futex
);
2046 /* Normal exit, no error */
2050 /* Clean up wait queue. */
2051 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2052 &wait_queue
.head
, head
) {
2053 cds_list_del(&wait_node
->head
);
2058 /* Empty command queue. */
2060 /* Dequeue command for registration */
2061 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2065 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2066 ret
= close(ust_cmd
->sock
);
2068 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2070 lttng_fd_put(LTTNG_FD_APPS
, 1);
2075 DBG("Dispatch thread dying");
2078 ERR("Health error occurred in %s", __func__
);
2080 health_unregister(health_sessiond
);
2081 rcu_unregister_thread();
2086 * This thread manage application registration.
2088 static void *thread_registration_apps(void *data
)
2090 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2091 uint32_t revents
, nb_fd
;
2092 struct lttng_poll_event events
;
2094 * Get allocated in this thread, enqueued to a global queue, dequeued and
2095 * freed in the manage apps thread.
2097 struct ust_command
*ust_cmd
= NULL
;
2099 DBG("[thread] Manage application registration started");
2101 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2103 if (testpoint(sessiond_thread_registration_apps
)) {
2104 goto error_testpoint
;
2107 ret
= lttcomm_listen_unix_sock(apps_sock
);
2113 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2114 * more will be added to this poll set.
2116 ret
= sessiond_set_thread_pollset(&events
, 2);
2118 goto error_create_poll
;
2121 /* Add the application registration socket */
2122 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2124 goto error_poll_add
;
2127 /* Notify all applications to register */
2128 ret
= notify_ust_apps(1);
2130 ERR("Failed to notify applications or create the wait shared memory.\n"
2131 "Execution continues but there might be problem for already\n"
2132 "running applications that wishes to register.");
2136 DBG("Accepting application registration");
2138 /* Inifinite blocking call, waiting for transmission */
2140 health_poll_entry();
2141 ret
= lttng_poll_wait(&events
, -1);
2145 * Restart interrupted system call.
2147 if (errno
== EINTR
) {
2155 for (i
= 0; i
< nb_fd
; i
++) {
2156 health_code_update();
2158 /* Fetch once the poll data */
2159 revents
= LTTNG_POLL_GETEV(&events
, i
);
2160 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2163 /* No activity for this FD (poll implementation). */
2167 /* Thread quit pipe has been closed. Killing thread. */
2168 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2174 /* Event on the registration socket */
2175 if (pollfd
== apps_sock
) {
2176 if (revents
& LPOLLIN
) {
2177 sock
= lttcomm_accept_unix_sock(apps_sock
);
2183 * Set socket timeout for both receiving and ending.
2184 * app_socket_timeout is in seconds, whereas
2185 * lttcomm_setsockopt_rcv_timeout and
2186 * lttcomm_setsockopt_snd_timeout expect msec as
2189 if (config
.app_socket_timeout
>= 0) {
2190 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2191 config
.app_socket_timeout
* 1000);
2192 (void) lttcomm_setsockopt_snd_timeout(sock
,
2193 config
.app_socket_timeout
* 1000);
2197 * Set the CLOEXEC flag. Return code is useless because
2198 * either way, the show must go on.
2200 (void) utils_set_fd_cloexec(sock
);
2202 /* Create UST registration command for enqueuing */
2203 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2204 if (ust_cmd
== NULL
) {
2205 PERROR("ust command zmalloc");
2214 * Using message-based transmissions to ensure we don't
2215 * have to deal with partially received messages.
2217 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2219 ERR("Exhausted file descriptors allowed for applications.");
2229 health_code_update();
2230 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2233 /* Close socket of the application. */
2238 lttng_fd_put(LTTNG_FD_APPS
, 1);
2242 health_code_update();
2244 ust_cmd
->sock
= sock
;
2247 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2248 " gid:%d sock:%d name:%s (version %d.%d)",
2249 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2250 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2251 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2252 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2255 * Lock free enqueue the registration request. The red pill
2256 * has been taken! This apps will be part of the *system*.
2258 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2261 * Wake the registration queue futex. Implicit memory
2262 * barrier with the exchange in cds_wfcq_enqueue.
2264 futex_nto1_wake(&ust_cmd_queue
.futex
);
2265 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2266 ERR("Register apps socket poll error");
2269 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2278 /* Notify that the registration thread is gone */
2281 if (apps_sock
>= 0) {
2282 ret
= close(apps_sock
);
2292 lttng_fd_put(LTTNG_FD_APPS
, 1);
2294 unlink(config
.apps_unix_sock_path
.value
);
2297 lttng_poll_clean(&events
);
2301 DBG("UST Registration thread cleanup complete");
2304 ERR("Health error occurred in %s", __func__
);
2306 health_unregister(health_sessiond
);
2312 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2313 * exec or it will fails.
2315 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2318 struct timespec timeout
;
2321 * Make sure we set the readiness flag to 0 because we are NOT ready.
2322 * This access to consumer_thread_is_ready does not need to be
2323 * protected by consumer_data.cond_mutex (yet) since the consumer
2324 * management thread has not been started at this point.
2326 consumer_data
->consumer_thread_is_ready
= 0;
2328 /* Setup pthread condition */
2329 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2332 PERROR("pthread_condattr_init consumer data");
2337 * Set the monotonic clock in order to make sure we DO NOT jump in time
2338 * between the clock_gettime() call and the timedwait call. See bug #324
2339 * for a more details and how we noticed it.
2341 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2344 PERROR("pthread_condattr_setclock consumer data");
2348 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2351 PERROR("pthread_cond_init consumer data");
2355 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2356 thread_manage_consumer
, consumer_data
);
2359 PERROR("pthread_create consumer");
2364 /* We are about to wait on a pthread condition */
2365 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2367 /* Get time for sem_timedwait absolute timeout */
2368 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2370 * Set the timeout for the condition timed wait even if the clock gettime
2371 * call fails since we might loop on that call and we want to avoid to
2372 * increment the timeout too many times.
2374 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2377 * The following loop COULD be skipped in some conditions so this is why we
2378 * set ret to 0 in order to make sure at least one round of the loop is
2384 * Loop until the condition is reached or when a timeout is reached. Note
2385 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2386 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2387 * possible. This loop does not take any chances and works with both of
2390 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2391 if (clock_ret
< 0) {
2392 PERROR("clock_gettime spawn consumer");
2393 /* Infinite wait for the consumerd thread to be ready */
2394 ret
= pthread_cond_wait(&consumer_data
->cond
,
2395 &consumer_data
->cond_mutex
);
2397 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2398 &consumer_data
->cond_mutex
, &timeout
);
2402 /* Release the pthread condition */
2403 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2407 if (ret
== ETIMEDOUT
) {
2411 * Call has timed out so we kill the kconsumerd_thread and return
2414 ERR("Condition timed out. The consumer thread was never ready."
2416 pth_ret
= pthread_cancel(consumer_data
->thread
);
2418 PERROR("pthread_cancel consumer thread");
2421 PERROR("pthread_cond_wait failed consumer thread");
2423 /* Caller is expecting a negative value on failure. */
2428 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2429 if (consumer_data
->pid
== 0) {
2430 ERR("Consumerd did not start");
2431 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2434 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2443 * Join consumer thread
2445 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2449 /* Consumer pid must be a real one. */
2450 if (consumer_data
->pid
> 0) {
2452 ret
= kill(consumer_data
->pid
, SIGTERM
);
2454 PERROR("Error killing consumer daemon");
2457 return pthread_join(consumer_data
->thread
, &status
);
2464 * Fork and exec a consumer daemon (consumerd).
2466 * Return pid if successful else -1.
2468 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2472 const char *consumer_to_use
;
2473 const char *verbosity
;
2476 DBG("Spawning consumerd");
2483 if (config
.verbose_consumer
) {
2484 verbosity
= "--verbose";
2485 } else if (lttng_opt_quiet
) {
2486 verbosity
= "--quiet";
2491 switch (consumer_data
->type
) {
2492 case LTTNG_CONSUMER_KERNEL
:
2494 * Find out which consumerd to execute. We will first try the
2495 * 64-bit path, then the sessiond's installation directory, and
2496 * fallback on the 32-bit one,
2498 DBG3("Looking for a kernel consumer at these locations:");
2499 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2500 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2501 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2502 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2503 DBG3("Found location #1");
2504 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2505 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2506 DBG3("Found location #2");
2507 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2508 } else if (stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2509 DBG3("Found location #3");
2510 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2512 DBG("Could not find any valid consumerd executable");
2516 DBG("Using kernel consumer at: %s", consumer_to_use
);
2517 (void) execl(consumer_to_use
,
2518 "lttng-consumerd", verbosity
, "-k",
2519 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2520 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2521 "--group", config
.tracing_group_name
.value
,
2524 case LTTNG_CONSUMER64_UST
:
2526 if (config
.consumerd64_lib_dir
.value
) {
2531 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2535 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2536 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2541 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2542 if (tmp
[0] != '\0') {
2543 strcat(tmpnew
, ":");
2544 strcat(tmpnew
, tmp
);
2546 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2553 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2554 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2555 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2556 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2557 "--group", config
.tracing_group_name
.value
,
2561 case LTTNG_CONSUMER32_UST
:
2563 if (config
.consumerd32_lib_dir
.value
) {
2568 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2572 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2573 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2578 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2579 if (tmp
[0] != '\0') {
2580 strcat(tmpnew
, ":");
2581 strcat(tmpnew
, tmp
);
2583 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2590 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2591 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2592 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2593 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2594 "--group", config
.tracing_group_name
.value
,
2599 ERR("unknown consumer type");
2603 PERROR("Consumer execl()");
2605 /* Reaching this point, we got a failure on our execl(). */
2607 } else if (pid
> 0) {
2610 PERROR("start consumer fork");
2618 * Spawn the consumerd daemon and session daemon thread.
2620 static int start_consumerd(struct consumer_data
*consumer_data
)
2625 * Set the listen() state on the socket since there is a possible race
2626 * between the exec() of the consumer daemon and this call if place in the
2627 * consumer thread. See bug #366 for more details.
2629 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2634 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2635 if (consumer_data
->pid
!= 0) {
2636 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2640 ret
= spawn_consumerd(consumer_data
);
2642 ERR("Spawning consumerd failed");
2643 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2647 /* Setting up the consumer_data pid */
2648 consumer_data
->pid
= ret
;
2649 DBG2("Consumer pid %d", consumer_data
->pid
);
2650 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2652 DBG2("Spawning consumer control thread");
2653 ret
= spawn_consumer_thread(consumer_data
);
2655 ERR("Fatal error spawning consumer control thread");
2663 /* Cleanup already created sockets on error. */
2664 if (consumer_data
->err_sock
>= 0) {
2667 err
= close(consumer_data
->err_sock
);
2669 PERROR("close consumer data error socket");
2676 * Setup necessary data for kernel tracer action.
2678 static int init_kernel_tracer(void)
2682 /* Modprobe lttng kernel modules */
2683 ret
= modprobe_lttng_control();
2688 /* Open debugfs lttng */
2689 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2690 if (kernel_tracer_fd
< 0) {
2691 DBG("Failed to open %s", module_proc_lttng
);
2695 /* Validate kernel version */
2696 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2697 &kernel_tracer_abi_version
);
2702 ret
= modprobe_lttng_data();
2707 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2714 WARN("Kernel tracer does not support buffer monitoring. "
2715 "The monitoring timer of channels in the kernel domain "
2716 "will be set to 0 (disabled).");
2719 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2723 modprobe_remove_lttng_control();
2724 ret
= close(kernel_tracer_fd
);
2728 kernel_tracer_fd
= -1;
2729 return LTTNG_ERR_KERN_VERSION
;
2732 ret
= close(kernel_tracer_fd
);
2738 modprobe_remove_lttng_control();
2741 WARN("No kernel tracer available");
2742 kernel_tracer_fd
= -1;
2744 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2746 return LTTNG_ERR_KERN_NA
;
2752 * Copy consumer output from the tracing session to the domain session. The
2753 * function also applies the right modification on a per domain basis for the
2754 * trace files destination directory.
2756 * Should *NOT* be called with RCU read-side lock held.
2758 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2761 const char *dir_name
;
2762 struct consumer_output
*consumer
;
2765 assert(session
->consumer
);
2768 case LTTNG_DOMAIN_KERNEL
:
2769 DBG3("Copying tracing session consumer output in kernel session");
2771 * XXX: We should audit the session creation and what this function
2772 * does "extra" in order to avoid a destroy since this function is used
2773 * in the domain session creation (kernel and ust) only. Same for UST
2776 if (session
->kernel_session
->consumer
) {
2777 consumer_output_put(session
->kernel_session
->consumer
);
2779 session
->kernel_session
->consumer
=
2780 consumer_copy_output(session
->consumer
);
2781 /* Ease our life a bit for the next part */
2782 consumer
= session
->kernel_session
->consumer
;
2783 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2785 case LTTNG_DOMAIN_JUL
:
2786 case LTTNG_DOMAIN_LOG4J
:
2787 case LTTNG_DOMAIN_PYTHON
:
2788 case LTTNG_DOMAIN_UST
:
2789 DBG3("Copying tracing session consumer output in UST session");
2790 if (session
->ust_session
->consumer
) {
2791 consumer_output_put(session
->ust_session
->consumer
);
2793 session
->ust_session
->consumer
=
2794 consumer_copy_output(session
->consumer
);
2795 /* Ease our life a bit for the next part */
2796 consumer
= session
->ust_session
->consumer
;
2797 dir_name
= DEFAULT_UST_TRACE_DIR
;
2800 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2804 /* Append correct directory to subdir */
2805 strncat(consumer
->subdir
, dir_name
,
2806 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2807 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2816 * Create an UST session and add it to the session ust list.
2818 * Should *NOT* be called with RCU read-side lock held.
2820 static int create_ust_session(struct ltt_session
*session
,
2821 struct lttng_domain
*domain
)
2824 struct ltt_ust_session
*lus
= NULL
;
2828 assert(session
->consumer
);
2830 switch (domain
->type
) {
2831 case LTTNG_DOMAIN_JUL
:
2832 case LTTNG_DOMAIN_LOG4J
:
2833 case LTTNG_DOMAIN_PYTHON
:
2834 case LTTNG_DOMAIN_UST
:
2837 ERR("Unknown UST domain on create session %d", domain
->type
);
2838 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2842 DBG("Creating UST session");
2844 lus
= trace_ust_create_session(session
->id
);
2846 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2850 lus
->uid
= session
->uid
;
2851 lus
->gid
= session
->gid
;
2852 lus
->output_traces
= session
->output_traces
;
2853 lus
->snapshot_mode
= session
->snapshot_mode
;
2854 lus
->live_timer_interval
= session
->live_timer
;
2855 session
->ust_session
= lus
;
2856 if (session
->shm_path
[0]) {
2857 strncpy(lus
->root_shm_path
, session
->shm_path
,
2858 sizeof(lus
->root_shm_path
));
2859 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2860 strncpy(lus
->shm_path
, session
->shm_path
,
2861 sizeof(lus
->shm_path
));
2862 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2863 strncat(lus
->shm_path
, "/ust",
2864 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2866 /* Copy session output to the newly created UST session */
2867 ret
= copy_session_consumer(domain
->type
, session
);
2868 if (ret
!= LTTNG_OK
) {
2876 session
->ust_session
= NULL
;
2881 * Create a kernel tracer session then create the default channel.
2883 static int create_kernel_session(struct ltt_session
*session
)
2887 DBG("Creating kernel session");
2889 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2891 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2895 /* Code flow safety */
2896 assert(session
->kernel_session
);
2898 /* Copy session output to the newly created Kernel session */
2899 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2900 if (ret
!= LTTNG_OK
) {
2904 session
->kernel_session
->uid
= session
->uid
;
2905 session
->kernel_session
->gid
= session
->gid
;
2906 session
->kernel_session
->output_traces
= session
->output_traces
;
2907 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2912 trace_kernel_destroy_session(session
->kernel_session
);
2913 session
->kernel_session
= NULL
;
2918 * Count number of session permitted by uid/gid.
2920 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2923 struct ltt_session
*session
;
2925 DBG("Counting number of available session for UID %d GID %d",
2927 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2929 * Only list the sessions the user can control.
2931 if (!session_access_ok(session
, uid
, gid
)) {
2940 * Check if the current kernel tracer supports the session rotation feature.
2941 * Return 1 if it does, 0 otherwise.
2943 static int check_rotate_compatible(void)
2947 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2948 DBG("Kernel tracer version is not compatible with the rotation feature");
2956 * Process the command requested by the lttng client within the command
2957 * context structure. This function make sure that the return structure (llm)
2958 * is set and ready for transmission before returning.
2960 * Return any error encountered or 0 for success.
2962 * "sock" is only used for special-case var. len data.
2964 * Should *NOT* be called with RCU read-side lock held.
2966 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2970 int need_tracing_session
= 1;
2973 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2975 assert(!rcu_read_ongoing());
2979 switch (cmd_ctx
->lsm
->cmd_type
) {
2980 case LTTNG_CREATE_SESSION
:
2981 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2982 case LTTNG_CREATE_SESSION_LIVE
:
2983 case LTTNG_DESTROY_SESSION
:
2984 case LTTNG_LIST_SESSIONS
:
2985 case LTTNG_LIST_DOMAINS
:
2986 case LTTNG_START_TRACE
:
2987 case LTTNG_STOP_TRACE
:
2988 case LTTNG_DATA_PENDING
:
2989 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2990 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2991 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2992 case LTTNG_SNAPSHOT_RECORD
:
2993 case LTTNG_SAVE_SESSION
:
2994 case LTTNG_SET_SESSION_SHM_PATH
:
2995 case LTTNG_REGENERATE_METADATA
:
2996 case LTTNG_REGENERATE_STATEDUMP
:
2997 case LTTNG_REGISTER_TRIGGER
:
2998 case LTTNG_UNREGISTER_TRIGGER
:
2999 case LTTNG_ROTATE_SESSION
:
3000 case LTTNG_ROTATION_GET_INFO
:
3001 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
3002 case LTTNG_ROTATION_SET_SCHEDULE
:
3003 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
3004 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
3011 if (config
.no_kernel
&& need_domain
3012 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3014 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3016 ret
= LTTNG_ERR_KERN_NA
;
3021 /* Deny register consumer if we already have a spawned consumer. */
3022 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3023 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3024 if (kconsumer_data
.pid
> 0) {
3025 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3026 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3029 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3033 * Check for command that don't needs to allocate a returned payload. We do
3034 * this here so we don't have to make the call for no payload at each
3037 switch(cmd_ctx
->lsm
->cmd_type
) {
3038 case LTTNG_LIST_SESSIONS
:
3039 case LTTNG_LIST_TRACEPOINTS
:
3040 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3041 case LTTNG_LIST_DOMAINS
:
3042 case LTTNG_LIST_CHANNELS
:
3043 case LTTNG_LIST_EVENTS
:
3044 case LTTNG_LIST_SYSCALLS
:
3045 case LTTNG_LIST_TRACKER_PIDS
:
3046 case LTTNG_DATA_PENDING
:
3047 case LTTNG_ROTATE_SESSION
:
3048 case LTTNG_ROTATION_GET_INFO
:
3049 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
3050 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
3053 /* Setup lttng message with no payload */
3054 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3056 /* This label does not try to unlock the session */
3057 goto init_setup_error
;
3061 /* Commands that DO NOT need a session. */
3062 switch (cmd_ctx
->lsm
->cmd_type
) {
3063 case LTTNG_CREATE_SESSION
:
3064 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3065 case LTTNG_CREATE_SESSION_LIVE
:
3066 case LTTNG_LIST_SESSIONS
:
3067 case LTTNG_LIST_TRACEPOINTS
:
3068 case LTTNG_LIST_SYSCALLS
:
3069 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3070 case LTTNG_SAVE_SESSION
:
3071 case LTTNG_REGISTER_TRIGGER
:
3072 case LTTNG_UNREGISTER_TRIGGER
:
3073 need_tracing_session
= 0;
3076 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3078 * We keep the session list lock across _all_ commands
3079 * for now, because the per-session lock does not
3080 * handle teardown properly.
3082 session_lock_list();
3083 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3084 if (cmd_ctx
->session
== NULL
) {
3085 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3088 /* Acquire lock for the session */
3089 session_lock(cmd_ctx
->session
);
3095 * Commands that need a valid session but should NOT create one if none
3096 * exists. Instead of creating one and destroying it when the command is
3097 * handled, process that right before so we save some round trip in useless
3100 switch (cmd_ctx
->lsm
->cmd_type
) {
3101 case LTTNG_DISABLE_CHANNEL
:
3102 case LTTNG_DISABLE_EVENT
:
3103 switch (cmd_ctx
->lsm
->domain
.type
) {
3104 case LTTNG_DOMAIN_KERNEL
:
3105 if (!cmd_ctx
->session
->kernel_session
) {
3106 ret
= LTTNG_ERR_NO_CHANNEL
;
3110 case LTTNG_DOMAIN_JUL
:
3111 case LTTNG_DOMAIN_LOG4J
:
3112 case LTTNG_DOMAIN_PYTHON
:
3113 case LTTNG_DOMAIN_UST
:
3114 if (!cmd_ctx
->session
->ust_session
) {
3115 ret
= LTTNG_ERR_NO_CHANNEL
;
3120 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3132 * Check domain type for specific "pre-action".
3134 switch (cmd_ctx
->lsm
->domain
.type
) {
3135 case LTTNG_DOMAIN_KERNEL
:
3137 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3141 /* Kernel tracer check */
3142 if (kernel_tracer_fd
== -1) {
3143 /* Basically, load kernel tracer modules */
3144 ret
= init_kernel_tracer();
3150 /* Consumer is in an ERROR state. Report back to client */
3151 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3152 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3156 /* Need a session for kernel command */
3157 if (need_tracing_session
) {
3158 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3159 ret
= create_kernel_session(cmd_ctx
->session
);
3161 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3166 /* Start the kernel consumer daemon */
3167 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3168 if (kconsumer_data
.pid
== 0 &&
3169 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3170 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3171 ret
= start_consumerd(&kconsumer_data
);
3173 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3176 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3178 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3182 * The consumer was just spawned so we need to add the socket to
3183 * the consumer output of the session if exist.
3185 ret
= consumer_create_socket(&kconsumer_data
,
3186 cmd_ctx
->session
->kernel_session
->consumer
);
3193 case LTTNG_DOMAIN_JUL
:
3194 case LTTNG_DOMAIN_LOG4J
:
3195 case LTTNG_DOMAIN_PYTHON
:
3196 case LTTNG_DOMAIN_UST
:
3198 if (!ust_app_supported()) {
3199 ret
= LTTNG_ERR_NO_UST
;
3202 /* Consumer is in an ERROR state. Report back to client */
3203 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3204 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3208 if (need_tracing_session
) {
3209 /* Create UST session if none exist. */
3210 if (cmd_ctx
->session
->ust_session
== NULL
) {
3211 ret
= create_ust_session(cmd_ctx
->session
,
3212 &cmd_ctx
->lsm
->domain
);
3213 if (ret
!= LTTNG_OK
) {
3218 /* Start the UST consumer daemons */
3220 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3221 if (config
.consumerd64_bin_path
.value
&&
3222 ustconsumer64_data
.pid
== 0 &&
3223 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3224 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3225 ret
= start_consumerd(&ustconsumer64_data
);
3227 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3228 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3232 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3233 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3235 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3239 * Setup socket for consumer 64 bit. No need for atomic access
3240 * since it was set above and can ONLY be set in this thread.
3242 ret
= consumer_create_socket(&ustconsumer64_data
,
3243 cmd_ctx
->session
->ust_session
->consumer
);
3249 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3250 if (config
.consumerd32_bin_path
.value
&&
3251 ustconsumer32_data
.pid
== 0 &&
3252 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3253 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3254 ret
= start_consumerd(&ustconsumer32_data
);
3256 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3257 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3261 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3262 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3264 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3268 * Setup socket for consumer 64 bit. No need for atomic access
3269 * since it was set above and can ONLY be set in this thread.
3271 ret
= consumer_create_socket(&ustconsumer32_data
,
3272 cmd_ctx
->session
->ust_session
->consumer
);
3284 /* Validate consumer daemon state when start/stop trace command */
3285 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3286 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3287 switch (cmd_ctx
->lsm
->domain
.type
) {
3288 case LTTNG_DOMAIN_NONE
:
3290 case LTTNG_DOMAIN_JUL
:
3291 case LTTNG_DOMAIN_LOG4J
:
3292 case LTTNG_DOMAIN_PYTHON
:
3293 case LTTNG_DOMAIN_UST
:
3294 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3295 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3299 case LTTNG_DOMAIN_KERNEL
:
3300 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3301 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3306 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3312 * Check that the UID or GID match that of the tracing session.
3313 * The root user can interact with all sessions.
3315 if (need_tracing_session
) {
3316 if (!session_access_ok(cmd_ctx
->session
,
3317 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3318 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3319 ret
= LTTNG_ERR_EPERM
;
3325 * Send relayd information to consumer as soon as we have a domain and a
3328 if (cmd_ctx
->session
&& need_domain
) {
3330 * Setup relayd if not done yet. If the relayd information was already
3331 * sent to the consumer, this call will gracefully return.
3333 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3334 if (ret
!= LTTNG_OK
) {
3339 /* Process by command type */
3340 switch (cmd_ctx
->lsm
->cmd_type
) {
3341 case LTTNG_ADD_CONTEXT
:
3344 * An LTTNG_ADD_CONTEXT command might have a supplementary
3345 * payload if the context being added is an application context.
3347 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3348 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3349 char *provider_name
= NULL
, *context_name
= NULL
;
3350 size_t provider_name_len
=
3351 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3352 size_t context_name_len
=
3353 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3355 if (provider_name_len
== 0 || context_name_len
== 0) {
3357 * Application provider and context names MUST
3360 ret
= -LTTNG_ERR_INVALID
;
3364 provider_name
= zmalloc(provider_name_len
+ 1);
3365 if (!provider_name
) {
3366 ret
= -LTTNG_ERR_NOMEM
;
3369 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3372 context_name
= zmalloc(context_name_len
+ 1);
3373 if (!context_name
) {
3374 ret
= -LTTNG_ERR_NOMEM
;
3375 goto error_add_context
;
3377 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3380 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3383 goto error_add_context
;
3386 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3389 goto error_add_context
;
3394 * cmd_add_context assumes ownership of the provider and context
3397 ret
= cmd_add_context(cmd_ctx
->session
,
3398 cmd_ctx
->lsm
->domain
.type
,
3399 cmd_ctx
->lsm
->u
.context
.channel_name
,
3400 &cmd_ctx
->lsm
->u
.context
.ctx
,
3401 kernel_poll_pipe
[1]);
3403 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3404 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3406 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3407 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3413 case LTTNG_DISABLE_CHANNEL
:
3415 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3416 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3419 case LTTNG_DISABLE_EVENT
:
3423 * FIXME: handle filter; for now we just receive the filter's
3424 * bytecode along with the filter expression which are sent by
3425 * liblttng-ctl and discard them.
3427 * This fixes an issue where the client may block while sending
3428 * the filter payload and encounter an error because the session
3429 * daemon closes the socket without ever handling this data.
3431 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3432 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3435 char data
[LTTNG_FILTER_MAX_LEN
];
3437 DBG("Discarding disable event command payload of size %zu", count
);
3439 ret
= lttcomm_recv_unix_sock(sock
, data
,
3440 count
> sizeof(data
) ? sizeof(data
) : count
);
3445 count
-= (size_t) ret
;
3448 /* FIXME: passing packed structure to non-packed pointer */
3449 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3450 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3451 &cmd_ctx
->lsm
->u
.disable
.event
);
3454 case LTTNG_ENABLE_CHANNEL
:
3456 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3457 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3458 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3459 &cmd_ctx
->lsm
->u
.channel
.chan
,
3460 kernel_poll_pipe
[1]);
3463 case LTTNG_TRACK_PID
:
3465 ret
= cmd_track_pid(cmd_ctx
->session
,
3466 cmd_ctx
->lsm
->domain
.type
,
3467 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3470 case LTTNG_UNTRACK_PID
:
3472 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3473 cmd_ctx
->lsm
->domain
.type
,
3474 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3477 case LTTNG_ENABLE_EVENT
:
3479 struct lttng_event_exclusion
*exclusion
= NULL
;
3480 struct lttng_filter_bytecode
*bytecode
= NULL
;
3481 char *filter_expression
= NULL
;
3483 /* Handle exclusion events and receive it from the client. */
3484 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3485 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3487 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3488 (count
* LTTNG_SYMBOL_NAME_LEN
));
3490 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3494 DBG("Receiving var len exclusion event list from client ...");
3495 exclusion
->count
= count
;
3496 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3497 count
* LTTNG_SYMBOL_NAME_LEN
);
3499 DBG("Nothing recv() from client var len data... continuing");
3502 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3507 /* Get filter expression from client. */
3508 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3509 size_t expression_len
=
3510 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3512 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3513 ret
= LTTNG_ERR_FILTER_INVAL
;
3518 filter_expression
= zmalloc(expression_len
);
3519 if (!filter_expression
) {
3521 ret
= LTTNG_ERR_FILTER_NOMEM
;
3525 /* Receive var. len. data */
3526 DBG("Receiving var len filter's expression from client ...");
3527 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3530 DBG("Nothing recv() from client car len data... continuing");
3532 free(filter_expression
);
3534 ret
= LTTNG_ERR_FILTER_INVAL
;
3539 /* Handle filter and get bytecode from client. */
3540 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3541 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3543 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3544 ret
= LTTNG_ERR_FILTER_INVAL
;
3545 free(filter_expression
);
3550 bytecode
= zmalloc(bytecode_len
);
3552 free(filter_expression
);
3554 ret
= LTTNG_ERR_FILTER_NOMEM
;
3558 /* Receive var. len. data */
3559 DBG("Receiving var len filter's bytecode from client ...");
3560 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3562 DBG("Nothing recv() from client car len data... continuing");
3564 free(filter_expression
);
3567 ret
= LTTNG_ERR_FILTER_INVAL
;
3571 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3572 free(filter_expression
);
3575 ret
= LTTNG_ERR_FILTER_INVAL
;
3580 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3581 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3582 &cmd_ctx
->lsm
->u
.enable
.event
,
3583 filter_expression
, bytecode
, exclusion
,
3584 kernel_poll_pipe
[1]);
3587 case LTTNG_LIST_TRACEPOINTS
:
3589 struct lttng_event
*events
;
3592 session_lock_list();
3593 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3594 session_unlock_list();
3595 if (nb_events
< 0) {
3596 /* Return value is a negative lttng_error_code. */
3602 * Setup lttng message with payload size set to the event list size in
3603 * bytes and then copy list into the llm payload.
3605 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3606 sizeof(struct lttng_event
) * nb_events
);
3616 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3618 struct lttng_event_field
*fields
;
3621 session_lock_list();
3622 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3624 session_unlock_list();
3625 if (nb_fields
< 0) {
3626 /* Return value is a negative lttng_error_code. */
3632 * Setup lttng message with payload size set to the event list size in
3633 * bytes and then copy list into the llm payload.
3635 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3636 sizeof(struct lttng_event_field
) * nb_fields
);
3646 case LTTNG_LIST_SYSCALLS
:
3648 struct lttng_event
*events
;
3651 nb_events
= cmd_list_syscalls(&events
);
3652 if (nb_events
< 0) {
3653 /* Return value is a negative lttng_error_code. */
3659 * Setup lttng message with payload size set to the event list size in
3660 * bytes and then copy list into the llm payload.
3662 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3663 sizeof(struct lttng_event
) * nb_events
);
3673 case LTTNG_LIST_TRACKER_PIDS
:
3675 int32_t *pids
= NULL
;
3678 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3679 cmd_ctx
->lsm
->domain
.type
, &pids
);
3681 /* Return value is a negative lttng_error_code. */
3687 * Setup lttng message with payload size set to the event list size in
3688 * bytes and then copy list into the llm payload.
3690 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3691 sizeof(int32_t) * nr_pids
);
3701 case LTTNG_SET_CONSUMER_URI
:
3704 struct lttng_uri
*uris
;
3706 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3707 len
= nb_uri
* sizeof(struct lttng_uri
);
3710 ret
= LTTNG_ERR_INVALID
;
3714 uris
= zmalloc(len
);
3716 ret
= LTTNG_ERR_FATAL
;
3720 /* Receive variable len data */
3721 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3722 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3724 DBG("No URIs received from client... continuing");
3726 ret
= LTTNG_ERR_SESSION_FAIL
;
3731 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3733 if (ret
!= LTTNG_OK
) {
3740 case LTTNG_START_TRACE
:
3743 * On the first start, if we have a kernel session and we have
3744 * enabled time or size-based rotations, we have to make sure
3745 * the kernel tracer supports it.
3747 if (!cmd_ctx
->session
->has_been_started
&& \
3748 cmd_ctx
->session
->kernel_session
&& \
3749 (cmd_ctx
->session
->rotate_timer_period
|| \
3750 cmd_ctx
->session
->rotate_size
) && \
3751 !check_rotate_compatible()) {
3752 DBG("Kernel tracer version is not compatible with the rotation feature");
3753 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3756 ret
= cmd_start_trace(cmd_ctx
->session
);
3759 case LTTNG_STOP_TRACE
:
3761 ret
= cmd_stop_trace(cmd_ctx
->session
);
3764 case LTTNG_CREATE_SESSION
:
3767 struct lttng_uri
*uris
= NULL
;
3769 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3770 len
= nb_uri
* sizeof(struct lttng_uri
);
3773 uris
= zmalloc(len
);
3775 ret
= LTTNG_ERR_FATAL
;
3779 /* Receive variable len data */
3780 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3781 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3783 DBG("No URIs received from client... continuing");
3785 ret
= LTTNG_ERR_SESSION_FAIL
;
3790 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3791 DBG("Creating session with ONE network URI is a bad call");
3792 ret
= LTTNG_ERR_SESSION_FAIL
;
3798 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3799 &cmd_ctx
->creds
, 0);
3805 case LTTNG_DESTROY_SESSION
:
3807 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1],
3808 notification_thread_handle
);
3810 /* Set session to NULL so we do not unlock it after free. */
3811 cmd_ctx
->session
= NULL
;
3814 case LTTNG_LIST_DOMAINS
:
3817 struct lttng_domain
*domains
= NULL
;
3819 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3821 /* Return value is a negative lttng_error_code. */
3826 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3827 nb_dom
* sizeof(struct lttng_domain
));
3837 case LTTNG_LIST_CHANNELS
:
3839 ssize_t payload_size
;
3840 struct lttng_channel
*channels
= NULL
;
3842 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3843 cmd_ctx
->session
, &channels
);
3844 if (payload_size
< 0) {
3845 /* Return value is a negative lttng_error_code. */
3846 ret
= -payload_size
;
3850 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3861 case LTTNG_LIST_EVENTS
:
3864 struct lttng_event
*events
= NULL
;
3865 struct lttcomm_event_command_header cmd_header
;
3868 memset(&cmd_header
, 0, sizeof(cmd_header
));
3869 /* Extended infos are included at the end of events */
3870 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3871 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3872 &events
, &total_size
);
3875 /* Return value is a negative lttng_error_code. */
3880 cmd_header
.nb_events
= nb_event
;
3881 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3882 &cmd_header
, sizeof(cmd_header
));
3892 case LTTNG_LIST_SESSIONS
:
3894 unsigned int nr_sessions
;
3895 void *sessions_payload
;
3898 session_lock_list();
3899 nr_sessions
= lttng_sessions_count(
3900 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3901 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3902 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3903 sessions_payload
= zmalloc(payload_len
);
3905 if (!sessions_payload
) {
3906 session_unlock_list();
3911 cmd_list_lttng_sessions(sessions_payload
,
3912 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3913 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3914 session_unlock_list();
3916 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3918 free(sessions_payload
);
3927 case LTTNG_REGISTER_CONSUMER
:
3929 struct consumer_data
*cdata
;
3931 switch (cmd_ctx
->lsm
->domain
.type
) {
3932 case LTTNG_DOMAIN_KERNEL
:
3933 cdata
= &kconsumer_data
;
3936 ret
= LTTNG_ERR_UND
;
3940 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3941 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3944 case LTTNG_DATA_PENDING
:
3947 uint8_t pending_ret_byte
;
3949 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3954 * This function may returns 0 or 1 to indicate whether or not
3955 * there is data pending. In case of error, it should return an
3956 * LTTNG_ERR code. However, some code paths may still return
3957 * a nondescript error code, which we handle by returning an
3960 if (pending_ret
== 0 || pending_ret
== 1) {
3962 * ret will be set to LTTNG_OK at the end of
3965 } else if (pending_ret
< 0) {
3966 ret
= LTTNG_ERR_UNK
;
3973 pending_ret_byte
= (uint8_t) pending_ret
;
3975 /* 1 byte to return whether or not data is pending */
3976 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3977 &pending_ret_byte
, 1);
3986 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3988 struct lttcomm_lttng_output_id reply
;
3990 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3991 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3992 if (ret
!= LTTNG_OK
) {
3996 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
4002 /* Copy output list into message payload */
4006 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
4008 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
4009 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
4012 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
4015 struct lttng_snapshot_output
*outputs
= NULL
;
4017 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
4018 if (nb_output
< 0) {
4023 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4024 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4025 nb_output
* sizeof(struct lttng_snapshot_output
));
4035 case LTTNG_SNAPSHOT_RECORD
:
4037 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4038 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4039 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4042 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4045 struct lttng_uri
*uris
= NULL
;
4047 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4048 len
= nb_uri
* sizeof(struct lttng_uri
);
4051 uris
= zmalloc(len
);
4053 ret
= LTTNG_ERR_FATAL
;
4057 /* Receive variable len data */
4058 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4059 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4061 DBG("No URIs received from client... continuing");
4063 ret
= LTTNG_ERR_SESSION_FAIL
;
4068 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4069 DBG("Creating session with ONE network URI is a bad call");
4070 ret
= LTTNG_ERR_SESSION_FAIL
;
4076 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4077 nb_uri
, &cmd_ctx
->creds
);
4081 case LTTNG_CREATE_SESSION_LIVE
:
4084 struct lttng_uri
*uris
= NULL
;
4086 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4087 len
= nb_uri
* sizeof(struct lttng_uri
);
4090 uris
= zmalloc(len
);
4092 ret
= LTTNG_ERR_FATAL
;
4096 /* Receive variable len data */
4097 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4098 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4100 DBG("No URIs received from client... continuing");
4102 ret
= LTTNG_ERR_SESSION_FAIL
;
4107 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4108 DBG("Creating session with ONE network URI is a bad call");
4109 ret
= LTTNG_ERR_SESSION_FAIL
;
4115 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4116 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4120 case LTTNG_SAVE_SESSION
:
4122 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4126 case LTTNG_SET_SESSION_SHM_PATH
:
4128 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4129 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4132 case LTTNG_REGENERATE_METADATA
:
4134 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4137 case LTTNG_REGENERATE_STATEDUMP
:
4139 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4142 case LTTNG_REGISTER_TRIGGER
:
4144 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4145 notification_thread_handle
);
4148 case LTTNG_UNREGISTER_TRIGGER
:
4150 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4151 notification_thread_handle
);
4154 case LTTNG_ROTATE_SESSION
:
4156 struct lttng_rotate_session_return rotate_return
;
4158 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4160 memset(&rotate_return
, 0, sizeof(rotate_return
));
4161 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4162 DBG("Kernel tracer version is not compatible with the rotation feature");
4163 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4167 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4173 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4174 sizeof(rotate_return
));
4183 case LTTNG_ROTATION_GET_INFO
:
4185 struct lttng_rotation_get_info_return get_info_return
;
4187 memset(&get_info_return
, 0, sizeof(get_info_return
));
4188 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4189 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4195 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4196 sizeof(get_info_return
));
4205 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
4207 struct lttng_session_get_current_output_return output_return
;
4209 memset(&output_return
, 0, sizeof(output_return
));
4210 ret
= cmd_session_get_current_output(cmd_ctx
->session
,
4217 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &output_return
,
4218 sizeof(output_return
));
4227 case LTTNG_ROTATION_SET_SCHEDULE
:
4229 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4230 DBG("Kernel tracer version does not support session rotations");
4231 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4235 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4236 cmd_ctx
->lsm
->u
.rotate_setup
.timer_us
,
4237 cmd_ctx
->lsm
->u
.rotate_setup
.size
,
4238 notification_thread_handle
);
4239 if (ret
!= LTTNG_OK
) {
4245 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
4247 struct lttng_rotation_schedule_get_timer_period
*get_timer
;
4249 get_timer
= zmalloc(sizeof(struct lttng_rotation_schedule_get_timer_period
));
4254 get_timer
->rotate_timer
= cmd_ctx
->session
->rotate_timer_period
;
4256 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, get_timer
,
4257 sizeof(struct lttng_rotation_schedule_get_timer_period
));
4267 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
4269 struct lttng_rotation_schedule_get_size
*get_size
;
4271 get_size
= zmalloc(sizeof(struct lttng_rotation_schedule_get_size
));
4276 get_size
->rotate_size
= cmd_ctx
->session
->rotate_size
;
4278 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, get_size
,
4279 sizeof(struct lttng_rotation_schedule_get_size
));
4290 ret
= LTTNG_ERR_UND
;
4295 if (cmd_ctx
->llm
== NULL
) {
4296 DBG("Missing llm structure. Allocating one.");
4297 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4301 /* Set return code */
4302 cmd_ctx
->llm
->ret_code
= ret
;
4304 if (cmd_ctx
->session
) {
4305 session_unlock(cmd_ctx
->session
);
4307 if (need_tracing_session
) {
4308 session_unlock_list();
4311 assert(!rcu_read_ongoing());
4316 * Thread managing health check socket.
4318 static void *thread_manage_health(void *data
)
4320 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4321 uint32_t revents
, nb_fd
;
4322 struct lttng_poll_event events
;
4323 struct health_comm_msg msg
;
4324 struct health_comm_reply reply
;
4326 DBG("[thread] Manage health check started");
4328 rcu_register_thread();
4330 /* We might hit an error path before this is created. */
4331 lttng_poll_init(&events
);
4333 /* Create unix socket */
4334 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4336 ERR("Unable to create health check Unix socket");
4341 /* lttng health client socket path permissions */
4342 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4343 utils_get_group_id(config
.tracing_group_name
.value
));
4345 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4350 ret
= chmod(config
.health_unix_sock_path
.value
,
4351 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4353 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4360 * Set the CLOEXEC flag. Return code is useless because either way, the
4363 (void) utils_set_fd_cloexec(sock
);
4365 ret
= lttcomm_listen_unix_sock(sock
);
4371 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4372 * more will be added to this poll set.
4374 ret
= sessiond_set_thread_pollset(&events
, 2);
4379 /* Add the application registration socket */
4380 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4385 sessiond_notify_ready();
4388 DBG("Health check ready");
4390 /* Inifinite blocking call, waiting for transmission */
4392 ret
= lttng_poll_wait(&events
, -1);
4395 * Restart interrupted system call.
4397 if (errno
== EINTR
) {
4405 for (i
= 0; i
< nb_fd
; i
++) {
4406 /* Fetch once the poll data */
4407 revents
= LTTNG_POLL_GETEV(&events
, i
);
4408 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4411 /* No activity for this FD (poll implementation). */
4415 /* Thread quit pipe has been closed. Killing thread. */
4416 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4422 /* Event on the registration socket */
4423 if (pollfd
== sock
) {
4424 if (revents
& LPOLLIN
) {
4426 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4427 ERR("Health socket poll error");
4430 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4436 new_sock
= lttcomm_accept_unix_sock(sock
);
4442 * Set the CLOEXEC flag. Return code is useless because either way, the
4445 (void) utils_set_fd_cloexec(new_sock
);
4447 DBG("Receiving data from client for health...");
4448 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4450 DBG("Nothing recv() from client... continuing");
4451 ret
= close(new_sock
);
4458 rcu_thread_online();
4460 memset(&reply
, 0, sizeof(reply
));
4461 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4463 * health_check_state returns 0 if health is
4466 if (!health_check_state(health_sessiond
, i
)) {
4467 reply
.ret_code
|= 1ULL << i
;
4471 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4473 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4475 ERR("Failed to send health data back to client");
4478 /* End of transmission */
4479 ret
= close(new_sock
);
4488 ERR("Health error occurred in %s", __func__
);
4490 DBG("Health check thread dying");
4491 unlink(config
.health_unix_sock_path
.value
);
4499 lttng_poll_clean(&events
);
4501 rcu_unregister_thread();
4506 * This thread manage all clients request using the unix client socket for
4509 static void *thread_manage_clients(void *data
)
4511 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4513 uint32_t revents
, nb_fd
;
4514 struct command_ctx
*cmd_ctx
= NULL
;
4515 struct lttng_poll_event events
;
4517 DBG("[thread] Manage client started");
4519 rcu_register_thread();
4521 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4523 health_code_update();
4525 ret
= lttcomm_listen_unix_sock(client_sock
);
4531 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4532 * more will be added to this poll set.
4534 ret
= sessiond_set_thread_pollset(&events
, 2);
4536 goto error_create_poll
;
4539 /* Add the application registration socket */
4540 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4545 ret
= sem_post(&load_info
->message_thread_ready
);
4547 PERROR("sem_post message_thread_ready");
4552 * Wait until all support threads are initialized before accepting
4555 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4557 struct timeval timeout
;
4560 FD_SET(thread_quit_pipe
[0], &read_fds
);
4561 memset(&timeout
, 0, sizeof(timeout
));
4562 timeout
.tv_usec
= 1000;
4565 * If a support thread failed to launch, it may signal that
4566 * we must exit and the sessiond would never be marked as
4569 * The timeout is set to 1ms, which serves as a way to
4570 * pace down this check.
4572 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
4574 if (ret
> 0 || (ret
< 0 && errno
!= EINTR
)) {
4579 * This barrier is paired with the one in sessiond_notify_ready() to
4580 * ensure that loads accessing data initialized by the other threads,
4581 * on which this thread was waiting, are not performed before this point.
4583 * Note that this could be a 'read' memory barrier, but a full barrier
4584 * is used in case the code changes. The performance implications of
4585 * this choice are minimal since this is a slow path.
4589 /* This testpoint is after we signal readiness to the parent. */
4590 if (testpoint(sessiond_thread_manage_clients
)) {
4594 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4598 health_code_update();
4601 DBG("Accepting client command ...");
4603 /* Inifinite blocking call, waiting for transmission */
4605 health_poll_entry();
4606 ret
= lttng_poll_wait(&events
, -1);
4610 * Restart interrupted system call.
4612 if (errno
== EINTR
) {
4620 for (i
= 0; i
< nb_fd
; i
++) {
4621 /* Fetch once the poll data */
4622 revents
= LTTNG_POLL_GETEV(&events
, i
);
4623 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4625 health_code_update();
4628 /* No activity for this FD (poll implementation). */
4632 /* Thread quit pipe has been closed. Killing thread. */
4633 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4639 /* Event on the registration socket */
4640 if (pollfd
== client_sock
) {
4641 if (revents
& LPOLLIN
) {
4643 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4644 ERR("Client socket poll error");
4647 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4653 DBG("Wait for client response");
4655 health_code_update();
4657 sock
= lttcomm_accept_unix_sock(client_sock
);
4663 * Set the CLOEXEC flag. Return code is useless because either way, the
4666 (void) utils_set_fd_cloexec(sock
);
4668 /* Set socket option for credentials retrieval */
4669 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4674 /* Allocate context command to process the client request */
4675 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4676 if (cmd_ctx
== NULL
) {
4677 PERROR("zmalloc cmd_ctx");
4681 /* Allocate data buffer for reception */
4682 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4683 if (cmd_ctx
->lsm
== NULL
) {
4684 PERROR("zmalloc cmd_ctx->lsm");
4688 cmd_ctx
->llm
= NULL
;
4689 cmd_ctx
->session
= NULL
;
4691 health_code_update();
4694 * Data is received from the lttng client. The struct
4695 * lttcomm_session_msg (lsm) contains the command and data request of
4698 DBG("Receiving data from client ...");
4699 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4700 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4702 DBG("Nothing recv() from client... continuing");
4708 clean_command_ctx(&cmd_ctx
);
4712 health_code_update();
4714 // TODO: Validate cmd_ctx including sanity check for
4715 // security purpose.
4717 rcu_thread_online();
4719 * This function dispatch the work to the kernel or userspace tracer
4720 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4721 * informations for the client. The command context struct contains
4722 * everything this function may needs.
4724 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4725 rcu_thread_offline();
4733 * TODO: Inform client somehow of the fatal error. At
4734 * this point, ret < 0 means that a zmalloc failed
4735 * (ENOMEM). Error detected but still accept
4736 * command, unless a socket error has been
4739 clean_command_ctx(&cmd_ctx
);
4743 health_code_update();
4745 DBG("Sending response (size: %d, retcode: %s (%d))",
4746 cmd_ctx
->lttng_msg_size
,
4747 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4748 cmd_ctx
->llm
->ret_code
);
4749 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4751 ERR("Failed to send data back to client");
4754 /* End of transmission */
4761 clean_command_ctx(&cmd_ctx
);
4763 health_code_update();
4775 lttng_poll_clean(&events
);
4776 clean_command_ctx(&cmd_ctx
);
4780 unlink(config
.client_unix_sock_path
.value
);
4781 if (client_sock
>= 0) {
4782 ret
= close(client_sock
);
4790 ERR("Health error occurred in %s", __func__
);
4793 health_unregister(health_sessiond
);
4795 DBG("Client thread dying");
4797 rcu_unregister_thread();
4800 * Since we are creating the consumer threads, we own them, so we need
4801 * to join them before our thread exits.
4803 ret
= join_consumer_thread(&kconsumer_data
);
4806 PERROR("join_consumer");
4809 ret
= join_consumer_thread(&ustconsumer32_data
);
4812 PERROR("join_consumer ust32");
4815 ret
= join_consumer_thread(&ustconsumer64_data
);
4818 PERROR("join_consumer ust64");
4823 static int string_match(const char *str1
, const char *str2
)
4825 return (str1
&& str2
) && !strcmp(str1
, str2
);
4829 * Take an option from the getopt output and set it in the right variable to be
4832 * Return 0 on success else a negative value.
4834 static int set_option(int opt
, const char *arg
, const char *optname
)
4838 if (string_match(optname
, "client-sock") || opt
== 'c') {
4839 if (!arg
|| *arg
== '\0') {
4843 if (lttng_is_setuid_setgid()) {
4844 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4845 "-c, --client-sock");
4847 config_string_set(&config
.client_unix_sock_path
,
4849 if (!config
.client_unix_sock_path
.value
) {
4854 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4855 if (!arg
|| *arg
== '\0') {
4859 if (lttng_is_setuid_setgid()) {
4860 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4863 config_string_set(&config
.apps_unix_sock_path
,
4865 if (!config
.apps_unix_sock_path
.value
) {
4870 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4871 config
.daemonize
= true;
4872 } else if (string_match(optname
, "background") || opt
== 'b') {
4873 config
.background
= true;
4874 } else if (string_match(optname
, "group") || opt
== 'g') {
4875 if (!arg
|| *arg
== '\0') {
4879 if (lttng_is_setuid_setgid()) {
4880 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4883 config_string_set(&config
.tracing_group_name
,
4885 if (!config
.tracing_group_name
.value
) {
4890 } else if (string_match(optname
, "help") || opt
== 'h') {
4891 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4893 ERR("Cannot show --help for `lttng-sessiond`");
4896 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4897 } else if (string_match(optname
, "version") || opt
== 'V') {
4898 fprintf(stdout
, "%s\n", VERSION
);
4900 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4901 config
.sig_parent
= true;
4902 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4903 if (!arg
|| *arg
== '\0') {
4907 if (lttng_is_setuid_setgid()) {
4908 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4909 "--kconsumerd-err-sock");
4911 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4913 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4918 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4919 if (!arg
|| *arg
== '\0') {
4923 if (lttng_is_setuid_setgid()) {
4924 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4925 "--kconsumerd-cmd-sock");
4927 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4929 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4934 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4935 if (!arg
|| *arg
== '\0') {
4939 if (lttng_is_setuid_setgid()) {
4940 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4941 "--ustconsumerd64-err-sock");
4943 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4945 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4950 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4951 if (!arg
|| *arg
== '\0') {
4955 if (lttng_is_setuid_setgid()) {
4956 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4957 "--ustconsumerd64-cmd-sock");
4959 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4961 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4966 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4967 if (!arg
|| *arg
== '\0') {
4971 if (lttng_is_setuid_setgid()) {
4972 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4973 "--ustconsumerd32-err-sock");
4975 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4977 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4982 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4983 if (!arg
|| *arg
== '\0') {
4987 if (lttng_is_setuid_setgid()) {
4988 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4989 "--ustconsumerd32-cmd-sock");
4991 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4993 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4998 } else if (string_match(optname
, "no-kernel")) {
4999 config
.no_kernel
= true;
5000 } else if (string_match(optname
, "quiet") || opt
== 'q') {
5001 config
.quiet
= true;
5002 } else if (string_match(optname
, "verbose") || opt
== 'v') {
5003 /* Verbose level can increase using multiple -v */
5005 /* Value obtained from config file */
5006 config
.verbose
= config_parse_value(arg
);
5008 /* -v used on command line */
5011 /* Clamp value to [0, 3] */
5012 config
.verbose
= config
.verbose
< 0 ? 0 :
5013 (config
.verbose
<= 3 ? config
.verbose
: 3);
5014 } else if (string_match(optname
, "verbose-consumer")) {
5016 config
.verbose_consumer
= config_parse_value(arg
);
5018 config
.verbose_consumer
++;
5020 } else if (string_match(optname
, "consumerd32-path")) {
5021 if (!arg
|| *arg
== '\0') {
5025 if (lttng_is_setuid_setgid()) {
5026 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5027 "--consumerd32-path");
5029 config_string_set(&config
.consumerd32_bin_path
,
5031 if (!config
.consumerd32_bin_path
.value
) {
5036 } else if (string_match(optname
, "consumerd32-libdir")) {
5037 if (!arg
|| *arg
== '\0') {
5041 if (lttng_is_setuid_setgid()) {
5042 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5043 "--consumerd32-libdir");
5045 config_string_set(&config
.consumerd32_lib_dir
,
5047 if (!config
.consumerd32_lib_dir
.value
) {
5052 } else if (string_match(optname
, "consumerd64-path")) {
5053 if (!arg
|| *arg
== '\0') {
5057 if (lttng_is_setuid_setgid()) {
5058 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5059 "--consumerd64-path");
5061 config_string_set(&config
.consumerd64_bin_path
,
5063 if (!config
.consumerd64_bin_path
.value
) {
5068 } else if (string_match(optname
, "consumerd64-libdir")) {
5069 if (!arg
|| *arg
== '\0') {
5073 if (lttng_is_setuid_setgid()) {
5074 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5075 "--consumerd64-libdir");
5077 config_string_set(&config
.consumerd64_lib_dir
,
5079 if (!config
.consumerd64_lib_dir
.value
) {
5084 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
5085 if (!arg
|| *arg
== '\0') {
5089 if (lttng_is_setuid_setgid()) {
5090 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5093 config_string_set(&config
.pid_file_path
, strdup(arg
));
5094 if (!config
.pid_file_path
.value
) {
5099 } else if (string_match(optname
, "agent-tcp-port")) {
5100 if (!arg
|| *arg
== '\0') {
5104 if (lttng_is_setuid_setgid()) {
5105 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5106 "--agent-tcp-port");
5111 v
= strtoul(arg
, NULL
, 0);
5112 if (errno
!= 0 || !isdigit(arg
[0])) {
5113 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5116 if (v
== 0 || v
>= 65535) {
5117 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5120 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
5121 DBG3("Agent TCP port set to non default: %i", (int) v
);
5123 } else if (string_match(optname
, "load") || opt
== 'l') {
5124 if (!arg
|| *arg
== '\0') {
5128 if (lttng_is_setuid_setgid()) {
5129 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5132 config_string_set(&config
.load_session_path
, strdup(arg
));
5133 if (!config
.load_session_path
.value
) {
5138 } else if (string_match(optname
, "kmod-probes")) {
5139 if (!arg
|| *arg
== '\0') {
5143 if (lttng_is_setuid_setgid()) {
5144 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5147 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
5148 if (!config
.kmod_probes_list
.value
) {
5153 } else if (string_match(optname
, "extra-kmod-probes")) {
5154 if (!arg
|| *arg
== '\0') {
5158 if (lttng_is_setuid_setgid()) {
5159 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5160 "--extra-kmod-probes");
5162 config_string_set(&config
.kmod_extra_probes_list
,
5164 if (!config
.kmod_extra_probes_list
.value
) {
5169 } else if (string_match(optname
, "config") || opt
== 'f') {
5170 /* This is handled in set_options() thus silent skip. */
5173 /* Unknown option or other error.
5174 * Error is printed by getopt, just return */
5179 if (ret
== -EINVAL
) {
5180 const char *opt_name
= "unknown";
5183 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5185 if (opt
== long_options
[i
].val
) {
5186 opt_name
= long_options
[i
].name
;
5191 WARN("Invalid argument provided for option \"%s\", using default value.",
5199 * config_entry_handler_cb used to handle options read from a config file.
5200 * See config_entry_handler_cb comment in common/config/session-config.h for the
5201 * return value conventions.
5203 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5207 if (!entry
|| !entry
->name
|| !entry
->value
) {
5212 /* Check if the option is to be ignored */
5213 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5214 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5219 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5222 /* Ignore if not fully matched. */
5223 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5228 * If the option takes no argument on the command line, we have to
5229 * check if the value is "true". We support non-zero numeric values,
5232 if (!long_options
[i
].has_arg
) {
5233 ret
= config_parse_value(entry
->value
);
5236 WARN("Invalid configuration value \"%s\" for option %s",
5237 entry
->value
, entry
->name
);
5239 /* False, skip boolean config option. */
5244 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5248 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5255 * daemon configuration loading and argument parsing
5257 static int set_options(int argc
, char **argv
)
5259 int ret
= 0, c
= 0, option_index
= 0;
5260 int orig_optopt
= optopt
, orig_optind
= optind
;
5262 const char *config_path
= NULL
;
5264 optstring
= utils_generate_optstring(long_options
,
5265 sizeof(long_options
) / sizeof(struct option
));
5271 /* Check for the --config option */
5272 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5273 &option_index
)) != -1) {
5277 } else if (c
!= 'f') {
5278 /* if not equal to --config option. */
5282 if (lttng_is_setuid_setgid()) {
5283 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5286 config_path
= utils_expand_path(optarg
);
5288 ERR("Failed to resolve path: %s", optarg
);
5293 ret
= config_get_section_entries(config_path
, config_section_name
,
5294 config_entry_handler
, NULL
);
5297 ERR("Invalid configuration option at line %i", ret
);
5303 /* Reset getopt's global state */
5304 optopt
= orig_optopt
;
5305 optind
= orig_optind
;
5309 * getopt_long() will not set option_index if it encounters a
5312 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5319 * Pass NULL as the long option name if popt left the index
5322 ret
= set_option(c
, optarg
,
5323 option_index
< 0 ? NULL
:
5324 long_options
[option_index
].name
);
5336 * Creates the two needed socket by the daemon.
5337 * apps_sock - The communication socket for all UST apps.
5338 * client_sock - The communication of the cli tool (lttng).
5340 static int init_daemon_socket(void)
5345 old_umask
= umask(0);
5347 /* Create client tool unix socket */
5348 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5349 if (client_sock
< 0) {
5350 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5355 /* Set the cloexec flag */
5356 ret
= utils_set_fd_cloexec(client_sock
);
5358 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5359 "Continuing but note that the consumer daemon will have a "
5360 "reference to this socket on exec()", client_sock
);
5363 /* File permission MUST be 660 */
5364 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5366 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5371 /* Create the application unix socket */
5372 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5373 if (apps_sock
< 0) {
5374 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5379 /* Set the cloexec flag */
5380 ret
= utils_set_fd_cloexec(apps_sock
);
5382 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5383 "Continuing but note that the consumer daemon will have a "
5384 "reference to this socket on exec()", apps_sock
);
5387 /* File permission MUST be 666 */
5388 ret
= chmod(config
.apps_unix_sock_path
.value
,
5389 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5391 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5396 DBG3("Session daemon client socket %d and application socket %d created",
5397 client_sock
, apps_sock
);
5405 * Create lockfile using the rundir and return its fd.
5407 static int create_lockfile(void)
5409 return utils_create_lock_file(config
.lock_file_path
.value
);
5413 * Check if the global socket is available, and if a daemon is answering at the
5414 * other side. If yes, error is returned.
5416 * Also attempts to create and hold the lock file.
5418 static int check_existing_daemon(void)
5422 /* Is there anybody out there ? */
5423 if (lttng_session_daemon_alive()) {
5428 lockfile_fd
= create_lockfile();
5429 if (lockfile_fd
< 0) {
5437 static void sessiond_cleanup_lock_file(void)
5442 * Cleanup lock file by deleting it and finaly closing it which will
5443 * release the file system lock.
5445 if (lockfile_fd
>= 0) {
5446 ret
= remove(config
.lock_file_path
.value
);
5448 PERROR("remove lock file");
5450 ret
= close(lockfile_fd
);
5452 PERROR("close lock file");
5458 * Set the tracing group gid onto the client socket.
5460 * Race window between mkdir and chown is OK because we are going from more
5461 * permissive (root.root) to less permissive (root.tracing).
5463 static int set_permissions(char *rundir
)
5468 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5470 /* Set lttng run dir */
5471 ret
= chown(rundir
, 0, gid
);
5473 ERR("Unable to set group on %s", rundir
);
5478 * Ensure all applications and tracing group can search the run
5479 * dir. Allow everyone to read the directory, since it does not
5480 * buy us anything to hide its content.
5482 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5484 ERR("Unable to set permissions on %s", rundir
);
5488 /* lttng client socket path */
5489 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5491 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5495 /* kconsumer error socket path */
5496 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5498 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5502 /* 64-bit ustconsumer error socket path */
5503 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5505 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5509 /* 32-bit ustconsumer compat32 error socket path */
5510 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5512 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5516 DBG("All permissions are set");
5522 * Create the lttng run directory needed for all global sockets and pipe.
5524 static int create_lttng_rundir(void)
5528 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5530 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5532 if (errno
!= EEXIST
) {
5533 ERR("Unable to create %s", config
.rundir
.value
);
5545 * Setup sockets and directory needed by the consumerds' communication with the
5548 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5553 switch (consumer_data
->type
) {
5554 case LTTNG_CONSUMER_KERNEL
:
5555 path
= config
.kconsumerd_path
.value
;
5557 case LTTNG_CONSUMER64_UST
:
5558 path
= config
.consumerd64_path
.value
;
5560 case LTTNG_CONSUMER32_UST
:
5561 path
= config
.consumerd32_path
.value
;
5564 ERR("Consumer type unknown");
5570 DBG2("Creating consumer directory: %s", path
);
5572 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5573 if (ret
< 0 && errno
!= EEXIST
) {
5575 ERR("Failed to create %s", path
);
5579 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5581 ERR("Unable to set group on %s", path
);
5587 /* Create the consumerd error unix socket */
5588 consumer_data
->err_sock
=
5589 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5590 if (consumer_data
->err_sock
< 0) {
5591 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5597 * Set the CLOEXEC flag. Return code is useless because either way, the
5600 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5602 PERROR("utils_set_fd_cloexec");
5603 /* continue anyway */
5606 /* File permission MUST be 660 */
5607 ret
= chmod(consumer_data
->err_unix_sock_path
,
5608 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5610 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5620 * Signal handler for the daemon
5622 * Simply stop all worker threads, leaving main() return gracefully after
5623 * joining all threads and calling cleanup().
5625 static void sighandler(int sig
)
5629 DBG("SIGINT caught");
5633 DBG("SIGTERM caught");
5637 CMM_STORE_SHARED(recv_child_signal
, 1);
5645 * Setup signal handler for :
5646 * SIGINT, SIGTERM, SIGPIPE
5648 static int set_signal_handler(void)
5651 struct sigaction sa
;
5654 if ((ret
= sigemptyset(&sigset
)) < 0) {
5655 PERROR("sigemptyset");
5659 sa
.sa_mask
= sigset
;
5662 sa
.sa_handler
= sighandler
;
5663 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5664 PERROR("sigaction");
5668 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5669 PERROR("sigaction");
5673 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5674 PERROR("sigaction");
5678 sa
.sa_handler
= SIG_IGN
;
5679 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5680 PERROR("sigaction");
5684 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5690 * Set open files limit to unlimited. This daemon can open a large number of
5691 * file descriptors in order to consume multiple kernel traces.
5693 static void set_ulimit(void)
5698 /* The kernel does not allow an infinite limit for open files */
5699 lim
.rlim_cur
= 65535;
5700 lim
.rlim_max
= 65535;
5702 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5704 PERROR("failed to set open files limit");
5708 static int write_pidfile(void)
5710 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5713 static int set_clock_plugin_env(void)
5716 char *env_value
= NULL
;
5718 if (!config
.lttng_ust_clock_plugin
.value
) {
5722 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5723 config
.lttng_ust_clock_plugin
.value
);
5729 ret
= putenv(env_value
);
5732 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5736 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5737 config
.lttng_ust_clock_plugin
.value
);
5743 struct rotation_thread_timer_queue
*create_rotate_timer_queue(void)
5745 struct rotation_thread_timer_queue
*queue
= NULL
;
5747 queue
= zmalloc(sizeof(struct rotation_thread_timer_queue
));
5749 PERROR("Failed to allocate timer rotate queue");
5753 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
5754 CDS_INIT_LIST_HEAD(&queue
->list
);
5755 pthread_mutex_init(&queue
->lock
, NULL
);
5762 void destroy_rotate_timer_queue(struct rotation_thread_timer_queue
*queue
)
5764 struct sessiond_rotation_timer
*node
, *tmp_node
;
5770 lttng_pipe_destroy(queue
->event_pipe
);
5772 pthread_mutex_lock(&queue
->lock
);
5773 /* Empty wait queue. */
5774 cds_list_for_each_entry_safe(node
, tmp_node
, &queue
->list
, head
) {
5775 cds_list_del(&node
->head
);
5778 pthread_mutex_unlock(&queue
->lock
);
5780 pthread_mutex_destroy(&queue
->lock
);
5787 int main(int argc
, char **argv
)
5789 int ret
= 0, retval
= 0;
5791 const char *env_app_timeout
;
5792 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5793 *ust64_channel_monitor_pipe
= NULL
,
5794 *kernel_channel_monitor_pipe
= NULL
;
5795 bool notification_thread_launched
= false;
5796 bool rotation_thread_launched
= false;
5797 bool timer_thread_launched
= false;
5798 struct lttng_pipe
*ust32_channel_rotate_pipe
= NULL
,
5799 *ust64_channel_rotate_pipe
= NULL
,
5800 *kernel_channel_rotate_pipe
= NULL
;
5801 struct timer_thread_parameters timer_thread_ctx
;
5802 /* Queue of rotation jobs populated by the sessiond-timer. */
5803 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5804 sem_t notification_thread_ready
;
5806 init_kernel_workarounds();
5808 rcu_register_thread();
5810 if (set_signal_handler()) {
5812 goto exit_set_signal_handler
;
5815 if (sessiond_timer_signal_init()) {
5817 goto exit_set_signal_handler
;
5820 page_size
= sysconf(_SC_PAGESIZE
);
5821 if (page_size
< 0) {
5822 PERROR("sysconf _SC_PAGESIZE");
5823 page_size
= LONG_MAX
;
5824 WARN("Fallback page size to %ld", page_size
);
5827 ret
= sessiond_config_init(&config
);
5830 goto exit_set_signal_handler
;
5834 * Init config from environment variables.
5835 * Command line option override env configuration per-doc. Do env first.
5837 sessiond_config_apply_env_config(&config
);
5840 * Parse arguments and load the daemon configuration file.
5842 * We have an exit_options exit path to free memory reserved by
5843 * set_options. This is needed because the rest of sessiond_cleanup()
5844 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5845 * depends on set_options.
5848 if (set_options(argc
, argv
)) {
5854 * Resolve all paths received as arguments, configuration option, or
5855 * through environment variable as absolute paths. This is necessary
5856 * since daemonizing causes the sessiond's current working directory
5859 ret
= sessiond_config_resolve_paths(&config
);
5865 lttng_opt_verbose
= config
.verbose
;
5866 lttng_opt_quiet
= config
.quiet
;
5867 kconsumer_data
.err_unix_sock_path
=
5868 config
.kconsumerd_err_unix_sock_path
.value
;
5869 kconsumer_data
.cmd_unix_sock_path
=
5870 config
.kconsumerd_cmd_unix_sock_path
.value
;
5871 ustconsumer32_data
.err_unix_sock_path
=
5872 config
.consumerd32_err_unix_sock_path
.value
;
5873 ustconsumer32_data
.cmd_unix_sock_path
=
5874 config
.consumerd32_cmd_unix_sock_path
.value
;
5875 ustconsumer64_data
.err_unix_sock_path
=
5876 config
.consumerd64_err_unix_sock_path
.value
;
5877 ustconsumer64_data
.cmd_unix_sock_path
=
5878 config
.consumerd64_cmd_unix_sock_path
.value
;
5879 set_clock_plugin_env();
5881 sessiond_config_log(&config
);
5883 if (create_lttng_rundir()) {
5888 /* Abort launch if a session daemon is already running. */
5889 if (check_existing_daemon()) {
5890 ERR("A session daemon is already running.");
5896 if (config
.daemonize
|| config
.background
) {
5899 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5900 !config
.background
);
5907 * We are in the child. Make sure all other file descriptors are
5908 * closed, in case we are called with more opened file
5909 * descriptors than the standard ones and the lock file.
5911 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5912 if (i
== lockfile_fd
) {
5919 if (run_as_create_worker(argv
[0]) < 0) {
5920 goto exit_create_run_as_worker_cleanup
;
5924 * Starting from here, we can create threads. This needs to be after
5925 * lttng_daemonize due to RCU.
5929 * Initialize the health check subsystem. This call should set the
5930 * appropriate time values.
5932 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5933 if (!health_sessiond
) {
5934 PERROR("health_app_create error");
5936 goto exit_health_sessiond_cleanup
;
5939 /* Create thread to clean up RCU hash tables */
5940 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5942 goto exit_ht_cleanup
;
5945 /* Create thread quit pipe */
5946 if (init_thread_quit_pipe()) {
5948 goto exit_init_data
;
5951 /* Check if daemon is UID = 0 */
5952 is_root
= !getuid();
5954 /* Create global run dir with root access */
5956 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5957 if (!kernel_channel_monitor_pipe
) {
5958 ERR("Failed to create kernel consumer channel monitor pipe");
5960 goto exit_init_data
;
5962 kconsumer_data
.channel_monitor_pipe
=
5963 lttng_pipe_release_writefd(
5964 kernel_channel_monitor_pipe
);
5965 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5967 goto exit_init_data
;
5969 kernel_channel_rotate_pipe
= lttng_pipe_open(0);
5970 if (!kernel_channel_rotate_pipe
) {
5971 ERR("Failed to create kernel consumer channel rotate pipe");
5973 goto exit_init_data
;
5975 kconsumer_data
.channel_rotate_pipe
=
5976 lttng_pipe_release_writefd(
5977 kernel_channel_rotate_pipe
);
5978 if (kconsumer_data
.channel_rotate_pipe
< 0) {
5980 goto exit_init_data
;
5984 /* Set consumer initial state */
5985 kernel_consumerd_state
= CONSUMER_STOPPED
;
5986 ust_consumerd_state
= CONSUMER_STOPPED
;
5988 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5989 if (!ust32_channel_monitor_pipe
) {
5990 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5992 goto exit_init_data
;
5994 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5995 ust32_channel_monitor_pipe
);
5996 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5998 goto exit_init_data
;
6000 ust32_channel_rotate_pipe
= lttng_pipe_open(0);
6001 if (!ust32_channel_rotate_pipe
) {
6002 ERR("Failed to create 32-bit user space consumer channel rotate pipe");
6004 goto exit_init_data
;
6006 ustconsumer32_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6007 ust32_channel_rotate_pipe
);
6008 if (ustconsumer32_data
.channel_rotate_pipe
< 0) {
6010 goto exit_init_data
;
6014 * The rotation_timer_queue structure is shared between the sessiond timer
6015 * thread and the rotation thread. The main() keeps the ownership and
6016 * destroys it when both threads have quit.
6018 rotation_timer_queue
= create_rotate_timer_queue();
6019 if (!rotation_timer_queue
) {
6021 goto exit_init_data
;
6023 timer_thread_ctx
.rotation_timer_queue
= rotation_timer_queue
;
6025 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
6026 if (!ust64_channel_monitor_pipe
) {
6027 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
6029 goto exit_init_data
;
6031 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6032 ust64_channel_monitor_pipe
);
6033 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
6035 goto exit_init_data
;
6037 ust64_channel_rotate_pipe
= lttng_pipe_open(0);
6038 if (!ust64_channel_rotate_pipe
) {
6039 ERR("Failed to create 64-bit user space consumer channel rotate pipe");
6041 goto exit_init_data
;
6043 ustconsumer64_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6044 ust64_channel_rotate_pipe
);
6045 if (ustconsumer64_data
.channel_rotate_pipe
< 0) {
6047 goto exit_init_data
;
6051 * Init UST app hash table. Alloc hash table before this point since
6052 * cleanup() can get called after that point.
6054 if (ust_app_ht_alloc()) {
6055 ERR("Failed to allocate UST app hash table");
6057 goto exit_init_data
;
6061 * Initialize agent app hash table. We allocate the hash table here
6062 * since cleanup() can get called after this point.
6064 if (agent_app_ht_alloc()) {
6065 ERR("Failed to allocate Agent app hash table");
6067 goto exit_init_data
;
6071 * These actions must be executed as root. We do that *after* setting up
6072 * the sockets path because we MUST make the check for another daemon using
6073 * those paths *before* trying to set the kernel consumer sockets and init
6077 if (set_consumer_sockets(&kconsumer_data
)) {
6079 goto exit_init_data
;
6082 /* Setup kernel tracer */
6083 if (!config
.no_kernel
) {
6084 init_kernel_tracer();
6085 if (kernel_tracer_fd
>= 0) {
6086 ret
= syscall_init_table();
6088 ERR("Unable to populate syscall table. "
6089 "Syscall tracing won't work "
6090 "for this session daemon.");
6095 /* Set ulimit for open files */
6098 /* init lttng_fd tracking must be done after set_ulimit. */
6101 if (set_consumer_sockets(&ustconsumer64_data
)) {
6103 goto exit_init_data
;
6106 if (set_consumer_sockets(&ustconsumer32_data
)) {
6108 goto exit_init_data
;
6111 /* Setup the needed unix socket */
6112 if (init_daemon_socket()) {
6114 goto exit_init_data
;
6117 /* Set credentials to socket */
6118 if (is_root
&& set_permissions(config
.rundir
.value
)) {
6120 goto exit_init_data
;
6123 /* Get parent pid if -S, --sig-parent is specified. */
6124 if (config
.sig_parent
) {
6128 /* Setup the kernel pipe for waking up the kernel thread */
6129 if (is_root
&& !config
.no_kernel
) {
6130 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6132 goto exit_init_data
;
6136 /* Setup the thread apps communication pipe. */
6137 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6139 goto exit_init_data
;
6142 /* Setup the thread apps notify communication pipe. */
6143 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6145 goto exit_init_data
;
6148 /* Initialize global buffer per UID and PID registry. */
6149 buffer_reg_init_uid_registry();
6150 buffer_reg_init_pid_registry();
6152 /* Init UST command queue. */
6153 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6156 * Get session list pointer. This pointer MUST NOT be free'd. This list
6157 * is statically declared in session.c
6159 session_list_ptr
= session_get_list();
6163 /* Check for the application socket timeout env variable. */
6164 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6165 if (env_app_timeout
) {
6166 config
.app_socket_timeout
= atoi(env_app_timeout
);
6168 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6171 ret
= write_pidfile();
6173 ERR("Error in write_pidfile");
6175 goto exit_init_data
;
6178 /* Initialize communication library */
6180 /* Initialize TCP timeout values */
6181 lttcomm_inet_init();
6183 if (load_session_init_data(&load_info
) < 0) {
6185 goto exit_init_data
;
6187 load_info
->path
= config
.load_session_path
.value
;
6189 /* Create health-check thread. */
6190 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6191 thread_manage_health
, (void *) NULL
);
6194 PERROR("pthread_create health");
6200 * The rotation thread needs the notification thread to be ready before
6201 * creating the rotate_notification_channel, so we use this semaphore as
6202 * a rendez-vous point.
6204 sem_init(¬ification_thread_ready
, 0, 0);
6206 /* notification_thread_data acquires the pipes' read side. */
6207 notification_thread_handle
= notification_thread_handle_create(
6208 ust32_channel_monitor_pipe
,
6209 ust64_channel_monitor_pipe
,
6210 kernel_channel_monitor_pipe
,
6211 ¬ification_thread_ready
);
6212 if (!notification_thread_handle
) {
6214 ERR("Failed to create notification thread shared data");
6216 goto exit_notification
;
6219 /* Create notification thread. */
6220 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6221 thread_notification
, notification_thread_handle
);
6224 PERROR("pthread_create notification");
6227 goto exit_notification
;
6229 notification_thread_launched
= true;
6231 /* Create timer thread. */
6232 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
6233 sessiond_timer_thread
, &timer_thread_ctx
);
6236 PERROR("pthread_create timer");
6239 goto exit_notification
;
6241 timer_thread_launched
= true;
6243 /* rotation_thread_data acquires the pipes' read side. */
6244 rotation_thread_handle
= rotation_thread_handle_create(
6245 ust32_channel_rotate_pipe
,
6246 ust64_channel_rotate_pipe
,
6247 kernel_channel_rotate_pipe
,
6248 thread_quit_pipe
[0],
6249 rotation_timer_queue
,
6250 notification_thread_handle
,
6251 ¬ification_thread_ready
);
6252 if (!rotation_thread_handle
) {
6254 ERR("Failed to create rotation thread shared data");
6259 /* Create rotation thread. */
6260 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6261 thread_rotation
, rotation_thread_handle
);
6264 PERROR("pthread_create rotation");
6269 rotation_thread_launched
= true;
6271 /* Create thread to manage the client socket */
6272 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6273 thread_manage_clients
, (void *) NULL
);
6276 PERROR("pthread_create clients");
6282 /* Create thread to dispatch registration */
6283 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6284 thread_dispatch_ust_registration
, (void *) NULL
);
6287 PERROR("pthread_create dispatch");
6293 /* Create thread to manage application registration. */
6294 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6295 thread_registration_apps
, (void *) NULL
);
6298 PERROR("pthread_create registration");
6304 /* Create thread to manage application socket */
6305 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6306 thread_manage_apps
, (void *) NULL
);
6309 PERROR("pthread_create apps");
6315 /* Create thread to manage application notify socket */
6316 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6317 ust_thread_manage_notify
, (void *) NULL
);
6320 PERROR("pthread_create notify");
6323 goto exit_apps_notify
;
6326 /* Create agent registration thread. */
6327 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6328 agent_thread_manage_registration
, (void *) NULL
);
6331 PERROR("pthread_create agent");
6334 goto exit_agent_reg
;
6337 /* Don't start this thread if kernel tracing is not requested nor root */
6338 if (is_root
&& !config
.no_kernel
) {
6339 /* Create kernel thread to manage kernel event */
6340 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6341 thread_manage_kernel
, (void *) NULL
);
6344 PERROR("pthread_create kernel");
6351 /* Create session loading thread. */
6352 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6353 thread_load_session
, load_info
);
6356 PERROR("pthread_create load_session_thread");
6359 goto exit_load_session
;
6363 * This is where we start awaiting program completion (e.g. through
6364 * signal that asks threads to teardown).
6367 ret
= pthread_join(load_session_thread
, &status
);
6370 PERROR("pthread_join load_session_thread");
6375 if (is_root
&& !config
.no_kernel
) {
6376 ret
= pthread_join(kernel_thread
, &status
);
6379 PERROR("pthread_join");
6385 ret
= pthread_join(agent_reg_thread
, &status
);
6388 PERROR("pthread_join agent");
6393 ret
= pthread_join(apps_notify_thread
, &status
);
6396 PERROR("pthread_join apps notify");
6401 ret
= pthread_join(apps_thread
, &status
);
6404 PERROR("pthread_join apps");
6409 ret
= pthread_join(reg_apps_thread
, &status
);
6412 PERROR("pthread_join");
6418 * Join dispatch thread after joining reg_apps_thread to ensure
6419 * we don't leak applications in the queue.
6421 ret
= pthread_join(dispatch_thread
, &status
);
6424 PERROR("pthread_join");
6429 ret
= pthread_join(client_thread
, &status
);
6432 PERROR("pthread_join");
6439 sem_destroy(¬ification_thread_ready
);
6440 ret
= pthread_join(health_thread
, &status
);
6443 PERROR("pthread_join health thread");
6450 * Wait for all pending call_rcu work to complete before tearing
6451 * down data structures. call_rcu worker may be trying to
6452 * perform lookups in those structures.
6456 * sessiond_cleanup() is called when no other thread is running, except
6457 * the ht_cleanup thread, which is needed to destroy the hash tables.
6459 rcu_thread_online();
6463 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6464 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6465 * the queue is empty before shutting down the clean-up thread.
6470 * The teardown of the notification system is performed after the
6471 * session daemon's teardown in order to allow it to be notified
6472 * of the active session and channels at the moment of the teardown.
6474 if (notification_thread_handle
) {
6475 if (notification_thread_launched
) {
6476 notification_thread_command_quit(
6477 notification_thread_handle
);
6478 ret
= pthread_join(notification_thread
, &status
);
6481 PERROR("pthread_join notification thread");
6485 notification_thread_handle_destroy(notification_thread_handle
);
6488 if (rotation_thread_handle
) {
6489 if (rotation_thread_launched
) {
6490 ret
= pthread_join(rotation_thread
, &status
);
6493 PERROR("pthread_join rotation thread");
6497 rotation_thread_handle_destroy(rotation_thread_handle
);
6500 if (timer_thread_launched
) {
6501 kill(getpid(), LTTNG_SESSIOND_SIG_EXIT
);
6502 ret
= pthread_join(timer_thread
, &status
);
6505 PERROR("pthread_join timer thread");
6511 * After the rotation and timer thread have quit, we can safely destroy
6512 * the rotation_timer_queue.
6514 destroy_rotate_timer_queue(rotation_timer_queue
);
6516 rcu_thread_offline();
6517 rcu_unregister_thread();
6519 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6523 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6524 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6525 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6526 lttng_pipe_destroy(ust32_channel_rotate_pipe
);
6527 lttng_pipe_destroy(ust64_channel_rotate_pipe
);
6528 lttng_pipe_destroy(kernel_channel_rotate_pipe
);
6531 health_app_destroy(health_sessiond
);
6532 exit_health_sessiond_cleanup
:
6533 exit_create_run_as_worker_cleanup
:
6536 sessiond_cleanup_lock_file();
6537 sessiond_cleanup_options();
6539 exit_set_signal_handler
: