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
);
594 ret
= remove(config
.pid_file_path
.value
);
596 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
599 DBG("Removing sessiond and consumerd content of directory %s",
600 config
.rundir
.value
);
603 DBG("Removing %s", config
.pid_file_path
.value
);
604 (void) unlink(config
.pid_file_path
.value
);
606 DBG("Removing %s", config
.agent_port_file_path
.value
);
607 (void) unlink(config
.agent_port_file_path
.value
);
610 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
611 (void) unlink(kconsumer_data
.err_unix_sock_path
);
613 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
614 (void) rmdir(config
.kconsumerd_path
.value
);
616 /* ust consumerd 32 */
617 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
618 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
620 DBG("Removing directory %s", config
.consumerd32_path
.value
);
621 (void) rmdir(config
.consumerd32_path
.value
);
623 /* ust consumerd 64 */
624 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
625 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
627 DBG("Removing directory %s", config
.consumerd64_path
.value
);
628 (void) rmdir(config
.consumerd64_path
.value
);
630 DBG("Cleaning up all sessions");
632 /* Destroy session list mutex */
633 if (session_list_ptr
!= NULL
) {
634 pthread_mutex_destroy(&session_list_ptr
->lock
);
636 /* Cleanup ALL session */
637 cds_list_for_each_entry_safe(sess
, stmp
,
638 &session_list_ptr
->head
, list
) {
639 cmd_destroy_session(sess
, kernel_poll_pipe
[1],
640 notification_thread_handle
);
644 wait_consumer(&kconsumer_data
);
645 wait_consumer(&ustconsumer64_data
);
646 wait_consumer(&ustconsumer32_data
);
648 DBG("Cleaning up all agent apps");
649 agent_app_ht_clean();
651 DBG("Closing all UST sockets");
652 ust_app_clean_list();
653 buffer_reg_destroy_registries();
655 if (is_root
&& !config
.no_kernel
) {
656 DBG2("Closing kernel fd");
657 if (kernel_tracer_fd
>= 0) {
658 ret
= close(kernel_tracer_fd
);
663 DBG("Unloading kernel modules");
664 modprobe_remove_lttng_all();
668 close_consumer_sockets();
671 load_session_destroy_data(load_info
);
676 * We do NOT rmdir rundir because there are other processes
677 * using it, for instance lttng-relayd, which can start in
678 * parallel with this teardown.
683 * Cleanup the daemon's option data structures.
685 static void sessiond_cleanup_options(void)
687 DBG("Cleaning up options");
689 sessiond_config_fini(&config
);
691 run_as_destroy_worker();
695 * Send data on a unix socket using the liblttsessiondcomm API.
697 * Return lttcomm error code.
699 static int send_unix_sock(int sock
, void *buf
, size_t len
)
701 /* Check valid length */
706 return lttcomm_send_unix_sock(sock
, buf
, len
);
710 * Free memory of a command context structure.
712 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
714 DBG("Clean command context structure");
716 if ((*cmd_ctx
)->llm
) {
717 free((*cmd_ctx
)->llm
);
719 if ((*cmd_ctx
)->lsm
) {
720 free((*cmd_ctx
)->lsm
);
728 * Notify UST applications using the shm mmap futex.
730 static int notify_ust_apps(int active
)
734 DBG("Notifying applications of session daemon state: %d", active
);
736 /* See shm.c for this call implying mmap, shm and futex calls */
737 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
738 if (wait_shm_mmap
== NULL
) {
742 /* Wake waiting process */
743 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
745 /* Apps notified successfully */
753 * Setup the outgoing data buffer for the response (llm) by allocating the
754 * right amount of memory and copying the original information from the lsm
757 * Return 0 on success, negative value on error.
759 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
760 const void *payload_buf
, size_t payload_len
,
761 const void *cmd_header_buf
, size_t cmd_header_len
)
764 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
765 const size_t cmd_header_offset
= header_len
;
766 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
767 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
769 cmd_ctx
->llm
= zmalloc(total_msg_size
);
771 if (cmd_ctx
->llm
== NULL
) {
777 /* Copy common data */
778 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
779 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
780 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
781 cmd_ctx
->llm
->data_size
= payload_len
;
782 cmd_ctx
->lttng_msg_size
= total_msg_size
;
784 /* Copy command header */
785 if (cmd_header_len
) {
786 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
792 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
801 * Version of setup_lttng_msg() without command header.
803 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
804 void *payload_buf
, size_t payload_len
)
806 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
809 * Update the kernel poll set of all channel fd available over all tracing
810 * session. Add the wakeup pipe at the end of the set.
812 static int update_kernel_poll(struct lttng_poll_event
*events
)
815 struct ltt_session
*session
;
816 struct ltt_kernel_channel
*channel
;
818 DBG("Updating kernel poll set");
821 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
822 session_lock(session
);
823 if (session
->kernel_session
== NULL
) {
824 session_unlock(session
);
828 cds_list_for_each_entry(channel
,
829 &session
->kernel_session
->channel_list
.head
, list
) {
830 /* Add channel fd to the kernel poll set */
831 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
833 session_unlock(session
);
836 DBG("Channel fd %d added to kernel set", channel
->fd
);
838 session_unlock(session
);
840 session_unlock_list();
845 session_unlock_list();
850 * Find the channel fd from 'fd' over all tracing session. When found, check
851 * for new channel stream and send those stream fds to the kernel consumer.
853 * Useful for CPU hotplug feature.
855 static int update_kernel_stream(int fd
)
858 struct ltt_session
*session
;
859 struct ltt_kernel_session
*ksess
;
860 struct ltt_kernel_channel
*channel
;
862 DBG("Updating kernel streams for channel fd %d", fd
);
865 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
866 session_lock(session
);
867 if (session
->kernel_session
== NULL
) {
868 session_unlock(session
);
871 ksess
= session
->kernel_session
;
873 cds_list_for_each_entry(channel
,
874 &ksess
->channel_list
.head
, list
) {
875 struct lttng_ht_iter iter
;
876 struct consumer_socket
*socket
;
878 if (channel
->fd
!= fd
) {
881 DBG("Channel found, updating kernel streams");
882 ret
= kernel_open_channel_stream(channel
);
886 /* Update the stream global counter */
887 ksess
->stream_count_global
+= ret
;
890 * Have we already sent fds to the consumer? If yes, it
891 * means that tracing is started so it is safe to send
892 * our updated stream fds.
894 if (ksess
->consumer_fds_sent
!= 1
895 || ksess
->consumer
== NULL
) {
901 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
902 &iter
.iter
, socket
, node
.node
) {
903 pthread_mutex_lock(socket
->lock
);
904 ret
= kernel_consumer_send_channel_streams(socket
,
906 session
->output_traces
? 1 : 0);
907 pthread_mutex_unlock(socket
->lock
);
915 session_unlock(session
);
917 session_unlock_list();
921 session_unlock(session
);
922 session_unlock_list();
927 * For each tracing session, update newly registered apps. The session list
928 * lock MUST be acquired before calling this.
930 static void update_ust_app(int app_sock
)
932 struct ltt_session
*sess
, *stmp
;
934 /* Consumer is in an ERROR state. Stop any application update. */
935 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
936 /* Stop the update process since the consumer is dead. */
940 /* For all tracing session(s) */
941 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
945 if (!sess
->ust_session
) {
950 assert(app_sock
>= 0);
951 app
= ust_app_find_by_sock(app_sock
);
954 * Application can be unregistered before so
955 * this is possible hence simply stopping the
958 DBG3("UST app update failed to find app sock %d",
962 ust_app_global_update(sess
->ust_session
, app
);
966 session_unlock(sess
);
971 * This thread manage event coming from the kernel.
973 * Features supported in this thread:
976 static void *thread_manage_kernel(void *data
)
978 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
979 uint32_t revents
, nb_fd
;
981 struct lttng_poll_event events
;
983 DBG("[thread] Thread manage kernel started");
985 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
988 * This first step of the while is to clean this structure which could free
989 * non NULL pointers so initialize it before the loop.
991 lttng_poll_init(&events
);
993 if (testpoint(sessiond_thread_manage_kernel
)) {
994 goto error_testpoint
;
997 health_code_update();
999 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1000 goto error_testpoint
;
1004 health_code_update();
1006 if (update_poll_flag
== 1) {
1007 /* Clean events object. We are about to populate it again. */
1008 lttng_poll_clean(&events
);
1010 ret
= sessiond_set_thread_pollset(&events
, 2);
1012 goto error_poll_create
;
1015 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1020 /* This will add the available kernel channel if any. */
1021 ret
= update_kernel_poll(&events
);
1025 update_poll_flag
= 0;
1028 DBG("Thread kernel polling");
1030 /* Poll infinite value of time */
1032 health_poll_entry();
1033 ret
= lttng_poll_wait(&events
, -1);
1034 DBG("Thread kernel return from poll on %d fds",
1035 LTTNG_POLL_GETNB(&events
));
1039 * Restart interrupted system call.
1041 if (errno
== EINTR
) {
1045 } else if (ret
== 0) {
1046 /* Should not happen since timeout is infinite */
1047 ERR("Return value of poll is 0 with an infinite timeout.\n"
1048 "This should not have happened! Continuing...");
1054 for (i
= 0; i
< nb_fd
; i
++) {
1055 /* Fetch once the poll data */
1056 revents
= LTTNG_POLL_GETEV(&events
, i
);
1057 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1059 health_code_update();
1062 /* No activity for this FD (poll implementation). */
1066 /* Thread quit pipe has been closed. Killing thread. */
1067 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1073 /* Check for data on kernel pipe */
1074 if (revents
& LPOLLIN
) {
1075 if (pollfd
== kernel_poll_pipe
[0]) {
1076 (void) lttng_read(kernel_poll_pipe
[0],
1079 * Ret value is useless here, if this pipe gets any actions an
1080 * update is required anyway.
1082 update_poll_flag
= 1;
1086 * New CPU detected by the kernel. Adding kernel stream to
1087 * kernel session and updating the kernel consumer
1089 ret
= update_kernel_stream(pollfd
);
1095 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1096 update_poll_flag
= 1;
1099 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1107 lttng_poll_clean(&events
);
1110 utils_close_pipe(kernel_poll_pipe
);
1111 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1114 ERR("Health error occurred in %s", __func__
);
1115 WARN("Kernel thread died unexpectedly. "
1116 "Kernel tracing can continue but CPU hotplug is disabled.");
1118 health_unregister(health_sessiond
);
1119 DBG("Kernel thread dying");
1124 * Signal pthread condition of the consumer data that the thread.
1126 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1128 pthread_mutex_lock(&data
->cond_mutex
);
1131 * The state is set before signaling. It can be any value, it's the waiter
1132 * job to correctly interpret this condition variable associated to the
1133 * consumer pthread_cond.
1135 * A value of 0 means that the corresponding thread of the consumer data
1136 * was not started. 1 indicates that the thread has started and is ready
1137 * for action. A negative value means that there was an error during the
1140 data
->consumer_thread_is_ready
= state
;
1141 (void) pthread_cond_signal(&data
->cond
);
1143 pthread_mutex_unlock(&data
->cond_mutex
);
1147 * This thread manage the consumer error sent back to the session daemon.
1149 static void *thread_manage_consumer(void *data
)
1151 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1152 uint32_t revents
, nb_fd
;
1153 enum lttcomm_return_code code
;
1154 struct lttng_poll_event events
;
1155 struct consumer_data
*consumer_data
= data
;
1156 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1158 DBG("[thread] Manage consumer started");
1160 rcu_register_thread();
1161 rcu_thread_online();
1163 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1165 health_code_update();
1168 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1169 * metadata_sock. Nothing more will be added to this poll set.
1171 ret
= sessiond_set_thread_pollset(&events
, 3);
1177 * The error socket here is already in a listening state which was done
1178 * just before spawning this thread to avoid a race between the consumer
1179 * daemon exec trying to connect and the listen() call.
1181 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1186 health_code_update();
1188 /* Infinite blocking call, waiting for transmission */
1190 health_poll_entry();
1192 if (testpoint(sessiond_thread_manage_consumer
)) {
1196 ret
= lttng_poll_wait(&events
, -1);
1200 * Restart interrupted system call.
1202 if (errno
== EINTR
) {
1210 for (i
= 0; i
< nb_fd
; i
++) {
1211 /* Fetch once the poll data */
1212 revents
= LTTNG_POLL_GETEV(&events
, i
);
1213 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1215 health_code_update();
1218 /* No activity for this FD (poll implementation). */
1222 /* Thread quit pipe has been closed. Killing thread. */
1223 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1229 /* Event on the registration socket */
1230 if (pollfd
== consumer_data
->err_sock
) {
1231 if (revents
& LPOLLIN
) {
1233 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1234 ERR("consumer err socket poll error");
1237 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1243 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1249 * Set the CLOEXEC flag. Return code is useless because either way, the
1252 (void) utils_set_fd_cloexec(sock
);
1254 health_code_update();
1256 DBG2("Receiving code from consumer err_sock");
1258 /* Getting status code from kconsumerd */
1259 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1260 sizeof(enum lttcomm_return_code
));
1265 health_code_update();
1266 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1267 ERR("consumer error when waiting for SOCK_READY : %s",
1268 lttcomm_get_readable_code(-code
));
1272 /* Connect both command and metadata sockets. */
1273 consumer_data
->cmd_sock
=
1274 lttcomm_connect_unix_sock(
1275 consumer_data
->cmd_unix_sock_path
);
1276 consumer_data
->metadata_fd
=
1277 lttcomm_connect_unix_sock(
1278 consumer_data
->cmd_unix_sock_path
);
1279 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1280 PERROR("consumer connect cmd socket");
1281 /* On error, signal condition and quit. */
1282 signal_consumer_condition(consumer_data
, -1);
1286 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1288 /* Create metadata socket lock. */
1289 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1290 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1291 PERROR("zmalloc pthread mutex");
1294 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1296 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1297 DBG("Consumer metadata socket ready (fd: %d)",
1298 consumer_data
->metadata_fd
);
1301 * Remove the consumerd error sock since we've established a connection.
1303 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1308 /* Add new accepted error socket. */
1309 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1314 /* Add metadata socket that is successfully connected. */
1315 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1316 LPOLLIN
| LPOLLRDHUP
);
1321 health_code_update();
1324 * Transfer the write-end of the channel monitoring and rotate pipe
1325 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1326 * SET_CHANNEL_ROTATE_PIPE commands.
1328 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1329 if (!cmd_socket_wrapper
) {
1332 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1334 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1335 consumer_data
->channel_monitor_pipe
);
1340 ret
= consumer_send_channel_rotate_pipe(cmd_socket_wrapper
,
1341 consumer_data
->channel_rotate_pipe
);
1346 /* Discard the socket wrapper as it is no longer needed. */
1347 consumer_destroy_socket(cmd_socket_wrapper
);
1348 cmd_socket_wrapper
= NULL
;
1350 /* The thread is completely initialized, signal that it is ready. */
1351 signal_consumer_condition(consumer_data
, 1);
1353 /* Infinite blocking call, waiting for transmission */
1356 health_code_update();
1358 /* Exit the thread because the thread quit pipe has been triggered. */
1360 /* Not a health error. */
1365 health_poll_entry();
1366 ret
= lttng_poll_wait(&events
, -1);
1370 * Restart interrupted system call.
1372 if (errno
== EINTR
) {
1380 for (i
= 0; i
< nb_fd
; i
++) {
1381 /* Fetch once the poll data */
1382 revents
= LTTNG_POLL_GETEV(&events
, i
);
1383 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1385 health_code_update();
1388 /* No activity for this FD (poll implementation). */
1393 * Thread quit pipe has been triggered, flag that we should stop
1394 * but continue the current loop to handle potential data from
1397 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1399 if (pollfd
== sock
) {
1400 /* Event on the consumerd socket */
1401 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1402 && !(revents
& LPOLLIN
)) {
1403 ERR("consumer err socket second poll error");
1406 health_code_update();
1407 /* Wait for any kconsumerd error */
1408 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1409 sizeof(enum lttcomm_return_code
));
1411 ERR("consumer closed the command socket");
1415 ERR("consumer return code : %s",
1416 lttcomm_get_readable_code(-code
));
1419 } else if (pollfd
== consumer_data
->metadata_fd
) {
1420 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1421 && !(revents
& LPOLLIN
)) {
1422 ERR("consumer err metadata socket second poll error");
1425 /* UST metadata requests */
1426 ret
= ust_consumer_metadata_request(
1427 &consumer_data
->metadata_sock
);
1429 ERR("Handling metadata request");
1433 /* No need for an else branch all FDs are tested prior. */
1435 health_code_update();
1441 * We lock here because we are about to close the sockets and some other
1442 * thread might be using them so get exclusive access which will abort all
1443 * other consumer command by other threads.
1445 pthread_mutex_lock(&consumer_data
->lock
);
1447 /* Immediately set the consumerd state to stopped */
1448 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1449 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1450 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1451 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1452 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1454 /* Code flow error... */
1458 if (consumer_data
->err_sock
>= 0) {
1459 ret
= close(consumer_data
->err_sock
);
1463 consumer_data
->err_sock
= -1;
1465 if (consumer_data
->cmd_sock
>= 0) {
1466 ret
= close(consumer_data
->cmd_sock
);
1470 consumer_data
->cmd_sock
= -1;
1472 if (consumer_data
->metadata_sock
.fd_ptr
&&
1473 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1474 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1486 unlink(consumer_data
->err_unix_sock_path
);
1487 unlink(consumer_data
->cmd_unix_sock_path
);
1488 pthread_mutex_unlock(&consumer_data
->lock
);
1490 /* Cleanup metadata socket mutex. */
1491 if (consumer_data
->metadata_sock
.lock
) {
1492 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1493 free(consumer_data
->metadata_sock
.lock
);
1495 lttng_poll_clean(&events
);
1497 if (cmd_socket_wrapper
) {
1498 consumer_destroy_socket(cmd_socket_wrapper
);
1503 ERR("Health error occurred in %s", __func__
);
1505 health_unregister(health_sessiond
);
1506 DBG("consumer thread cleanup completed");
1508 rcu_thread_offline();
1509 rcu_unregister_thread();
1515 * This thread receives application command sockets (FDs) on the
1516 * apps_cmd_pipe and waits (polls) on them until they are closed
1517 * or an error occurs.
1519 * At that point, it flushes the data (tracing and metadata) associated
1520 * with this application and tears down ust app sessions and other
1521 * associated data structures through ust_app_unregister().
1523 * Note that this thread never sends commands to the applications
1524 * through the command sockets; it merely listens for hang-ups
1525 * and errors on those sockets and cleans-up as they occur.
1527 static void *thread_manage_apps(void *data
)
1529 int i
, ret
, pollfd
, err
= -1;
1531 uint32_t revents
, nb_fd
;
1532 struct lttng_poll_event events
;
1534 DBG("[thread] Manage application started");
1536 rcu_register_thread();
1537 rcu_thread_online();
1539 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1541 if (testpoint(sessiond_thread_manage_apps
)) {
1542 goto error_testpoint
;
1545 health_code_update();
1547 ret
= sessiond_set_thread_pollset(&events
, 2);
1549 goto error_poll_create
;
1552 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1557 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1561 health_code_update();
1564 DBG("Apps thread polling");
1566 /* Inifinite blocking call, waiting for transmission */
1568 health_poll_entry();
1569 ret
= lttng_poll_wait(&events
, -1);
1570 DBG("Apps thread return from poll on %d fds",
1571 LTTNG_POLL_GETNB(&events
));
1575 * Restart interrupted system call.
1577 if (errno
== EINTR
) {
1585 for (i
= 0; i
< nb_fd
; i
++) {
1586 /* Fetch once the poll data */
1587 revents
= LTTNG_POLL_GETEV(&events
, i
);
1588 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1590 health_code_update();
1593 /* No activity for this FD (poll implementation). */
1597 /* Thread quit pipe has been closed. Killing thread. */
1598 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1604 /* Inspect the apps cmd pipe */
1605 if (pollfd
== apps_cmd_pipe
[0]) {
1606 if (revents
& LPOLLIN
) {
1610 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1611 if (size_ret
< sizeof(sock
)) {
1612 PERROR("read apps cmd pipe");
1616 health_code_update();
1619 * Since this is a command socket (write then read),
1620 * we only monitor the error events of the socket.
1622 ret
= lttng_poll_add(&events
, sock
,
1623 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1628 DBG("Apps with sock %d added to poll set", sock
);
1629 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1630 ERR("Apps command pipe error");
1633 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1638 * At this point, we know that a registered application made
1639 * the event at poll_wait.
1641 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1642 /* Removing from the poll set */
1643 ret
= lttng_poll_del(&events
, pollfd
);
1648 /* Socket closed on remote end. */
1649 ust_app_unregister(pollfd
);
1651 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1656 health_code_update();
1662 lttng_poll_clean(&events
);
1665 utils_close_pipe(apps_cmd_pipe
);
1666 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1669 * We don't clean the UST app hash table here since already registered
1670 * applications can still be controlled so let them be until the session
1671 * daemon dies or the applications stop.
1676 ERR("Health error occurred in %s", __func__
);
1678 health_unregister(health_sessiond
);
1679 DBG("Application communication apps thread cleanup complete");
1680 rcu_thread_offline();
1681 rcu_unregister_thread();
1686 * Send a socket to a thread This is called from the dispatch UST registration
1687 * thread once all sockets are set for the application.
1689 * The sock value can be invalid, we don't really care, the thread will handle
1690 * it and make the necessary cleanup if so.
1692 * On success, return 0 else a negative value being the errno message of the
1695 static int send_socket_to_thread(int fd
, int sock
)
1700 * It's possible that the FD is set as invalid with -1 concurrently just
1701 * before calling this function being a shutdown state of the thread.
1708 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1709 if (ret
< sizeof(sock
)) {
1710 PERROR("write apps pipe %d", fd
);
1717 /* All good. Don't send back the write positive ret value. */
1724 * Sanitize the wait queue of the dispatch registration thread meaning removing
1725 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1726 * notify socket is never received.
1728 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1730 int ret
, nb_fd
= 0, i
;
1731 unsigned int fd_added
= 0;
1732 struct lttng_poll_event events
;
1733 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1737 lttng_poll_init(&events
);
1739 /* Just skip everything for an empty queue. */
1740 if (!wait_queue
->count
) {
1744 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1749 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1750 &wait_queue
->head
, head
) {
1751 assert(wait_node
->app
);
1752 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1753 LPOLLHUP
| LPOLLERR
);
1766 * Poll but don't block so we can quickly identify the faulty events and
1767 * clean them afterwards from the wait queue.
1769 ret
= lttng_poll_wait(&events
, 0);
1775 for (i
= 0; i
< nb_fd
; i
++) {
1776 /* Get faulty FD. */
1777 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1778 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1781 /* No activity for this FD (poll implementation). */
1785 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1786 &wait_queue
->head
, head
) {
1787 if (pollfd
== wait_node
->app
->sock
&&
1788 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1789 cds_list_del(&wait_node
->head
);
1790 wait_queue
->count
--;
1791 ust_app_destroy(wait_node
->app
);
1794 * Silence warning of use-after-free in
1795 * cds_list_for_each_entry_safe which uses
1796 * __typeof__(*wait_node).
1801 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1808 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1812 lttng_poll_clean(&events
);
1816 lttng_poll_clean(&events
);
1818 ERR("Unable to sanitize wait queue");
1823 * Dispatch request from the registration threads to the application
1824 * communication thread.
1826 static void *thread_dispatch_ust_registration(void *data
)
1829 struct cds_wfcq_node
*node
;
1830 struct ust_command
*ust_cmd
= NULL
;
1831 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1832 struct ust_reg_wait_queue wait_queue
= {
1836 rcu_register_thread();
1838 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1840 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1841 goto error_testpoint
;
1844 health_code_update();
1846 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1848 DBG("[thread] Dispatch UST command started");
1851 health_code_update();
1853 /* Atomically prepare the queue futex */
1854 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1856 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1861 struct ust_app
*app
= NULL
;
1865 * Make sure we don't have node(s) that have hung up before receiving
1866 * the notify socket. This is to clean the list in order to avoid
1867 * memory leaks from notify socket that are never seen.
1869 sanitize_wait_queue(&wait_queue
);
1871 health_code_update();
1872 /* Dequeue command for registration */
1873 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1875 DBG("Woken up but nothing in the UST command queue");
1876 /* Continue thread execution */
1880 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1882 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1883 " gid:%d sock:%d name:%s (version %d.%d)",
1884 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1885 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1886 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1887 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1889 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1890 wait_node
= zmalloc(sizeof(*wait_node
));
1892 PERROR("zmalloc wait_node dispatch");
1893 ret
= close(ust_cmd
->sock
);
1895 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1897 lttng_fd_put(LTTNG_FD_APPS
, 1);
1901 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1903 /* Create application object if socket is CMD. */
1904 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1906 if (!wait_node
->app
) {
1907 ret
= close(ust_cmd
->sock
);
1909 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1911 lttng_fd_put(LTTNG_FD_APPS
, 1);
1917 * Add application to the wait queue so we can set the notify
1918 * socket before putting this object in the global ht.
1920 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1925 * We have to continue here since we don't have the notify
1926 * socket and the application MUST be added to the hash table
1927 * only at that moment.
1932 * Look for the application in the local wait queue and set the
1933 * notify socket if found.
1935 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1936 &wait_queue
.head
, head
) {
1937 health_code_update();
1938 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1939 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1940 cds_list_del(&wait_node
->head
);
1942 app
= wait_node
->app
;
1944 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1950 * With no application at this stage the received socket is
1951 * basically useless so close it before we free the cmd data
1952 * structure for good.
1955 ret
= close(ust_cmd
->sock
);
1957 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1959 lttng_fd_put(LTTNG_FD_APPS
, 1);
1966 * @session_lock_list
1968 * Lock the global session list so from the register up to the
1969 * registration done message, no thread can see the application
1970 * and change its state.
1972 session_lock_list();
1976 * Add application to the global hash table. This needs to be
1977 * done before the update to the UST registry can locate the
1982 /* Set app version. This call will print an error if needed. */
1983 (void) ust_app_version(app
);
1985 /* Send notify socket through the notify pipe. */
1986 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1990 session_unlock_list();
1992 * No notify thread, stop the UST tracing. However, this is
1993 * not an internal error of the this thread thus setting
1994 * the health error code to a normal exit.
2001 * Update newly registered application with the tracing
2002 * registry info already enabled information.
2004 update_ust_app(app
->sock
);
2007 * Don't care about return value. Let the manage apps threads
2008 * handle app unregistration upon socket close.
2010 (void) ust_app_register_done(app
);
2013 * Even if the application socket has been closed, send the app
2014 * to the thread and unregistration will take place at that
2017 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2020 session_unlock_list();
2022 * No apps. thread, stop the UST tracing. However, this is
2023 * not an internal error of the this thread thus setting
2024 * the health error code to a normal exit.
2031 session_unlock_list();
2033 } while (node
!= NULL
);
2035 health_poll_entry();
2036 /* Futex wait on queue. Blocking call on futex() */
2037 futex_nto1_wait(&ust_cmd_queue
.futex
);
2040 /* Normal exit, no error */
2044 /* Clean up wait queue. */
2045 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2046 &wait_queue
.head
, head
) {
2047 cds_list_del(&wait_node
->head
);
2052 /* Empty command queue. */
2054 /* Dequeue command for registration */
2055 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2059 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2060 ret
= close(ust_cmd
->sock
);
2062 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2064 lttng_fd_put(LTTNG_FD_APPS
, 1);
2069 DBG("Dispatch thread dying");
2072 ERR("Health error occurred in %s", __func__
);
2074 health_unregister(health_sessiond
);
2075 rcu_unregister_thread();
2080 * This thread manage application registration.
2082 static void *thread_registration_apps(void *data
)
2084 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2085 uint32_t revents
, nb_fd
;
2086 struct lttng_poll_event events
;
2088 * Get allocated in this thread, enqueued to a global queue, dequeued and
2089 * freed in the manage apps thread.
2091 struct ust_command
*ust_cmd
= NULL
;
2093 DBG("[thread] Manage application registration started");
2095 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2097 if (testpoint(sessiond_thread_registration_apps
)) {
2098 goto error_testpoint
;
2101 ret
= lttcomm_listen_unix_sock(apps_sock
);
2107 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2108 * more will be added to this poll set.
2110 ret
= sessiond_set_thread_pollset(&events
, 2);
2112 goto error_create_poll
;
2115 /* Add the application registration socket */
2116 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2118 goto error_poll_add
;
2121 /* Notify all applications to register */
2122 ret
= notify_ust_apps(1);
2124 ERR("Failed to notify applications or create the wait shared memory.\n"
2125 "Execution continues but there might be problem for already\n"
2126 "running applications that wishes to register.");
2130 DBG("Accepting application registration");
2132 /* Inifinite blocking call, waiting for transmission */
2134 health_poll_entry();
2135 ret
= lttng_poll_wait(&events
, -1);
2139 * Restart interrupted system call.
2141 if (errno
== EINTR
) {
2149 for (i
= 0; i
< nb_fd
; i
++) {
2150 health_code_update();
2152 /* Fetch once the poll data */
2153 revents
= LTTNG_POLL_GETEV(&events
, i
);
2154 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2157 /* No activity for this FD (poll implementation). */
2161 /* Thread quit pipe has been closed. Killing thread. */
2162 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2168 /* Event on the registration socket */
2169 if (pollfd
== apps_sock
) {
2170 if (revents
& LPOLLIN
) {
2171 sock
= lttcomm_accept_unix_sock(apps_sock
);
2177 * Set socket timeout for both receiving and ending.
2178 * app_socket_timeout is in seconds, whereas
2179 * lttcomm_setsockopt_rcv_timeout and
2180 * lttcomm_setsockopt_snd_timeout expect msec as
2183 if (config
.app_socket_timeout
>= 0) {
2184 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2185 config
.app_socket_timeout
* 1000);
2186 (void) lttcomm_setsockopt_snd_timeout(sock
,
2187 config
.app_socket_timeout
* 1000);
2191 * Set the CLOEXEC flag. Return code is useless because
2192 * either way, the show must go on.
2194 (void) utils_set_fd_cloexec(sock
);
2196 /* Create UST registration command for enqueuing */
2197 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2198 if (ust_cmd
== NULL
) {
2199 PERROR("ust command zmalloc");
2208 * Using message-based transmissions to ensure we don't
2209 * have to deal with partially received messages.
2211 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2213 ERR("Exhausted file descriptors allowed for applications.");
2223 health_code_update();
2224 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2227 /* Close socket of the application. */
2232 lttng_fd_put(LTTNG_FD_APPS
, 1);
2236 health_code_update();
2238 ust_cmd
->sock
= sock
;
2241 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2242 " gid:%d sock:%d name:%s (version %d.%d)",
2243 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2244 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2245 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2246 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2249 * Lock free enqueue the registration request. The red pill
2250 * has been taken! This apps will be part of the *system*.
2252 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2255 * Wake the registration queue futex. Implicit memory
2256 * barrier with the exchange in cds_wfcq_enqueue.
2258 futex_nto1_wake(&ust_cmd_queue
.futex
);
2259 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2260 ERR("Register apps socket poll error");
2263 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2272 /* Notify that the registration thread is gone */
2275 if (apps_sock
>= 0) {
2276 ret
= close(apps_sock
);
2286 lttng_fd_put(LTTNG_FD_APPS
, 1);
2288 unlink(config
.apps_unix_sock_path
.value
);
2291 lttng_poll_clean(&events
);
2295 DBG("UST Registration thread cleanup complete");
2298 ERR("Health error occurred in %s", __func__
);
2300 health_unregister(health_sessiond
);
2306 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2307 * exec or it will fails.
2309 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2312 struct timespec timeout
;
2315 * Make sure we set the readiness flag to 0 because we are NOT ready.
2316 * This access to consumer_thread_is_ready does not need to be
2317 * protected by consumer_data.cond_mutex (yet) since the consumer
2318 * management thread has not been started at this point.
2320 consumer_data
->consumer_thread_is_ready
= 0;
2322 /* Setup pthread condition */
2323 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2326 PERROR("pthread_condattr_init consumer data");
2331 * Set the monotonic clock in order to make sure we DO NOT jump in time
2332 * between the clock_gettime() call and the timedwait call. See bug #324
2333 * for a more details and how we noticed it.
2335 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2338 PERROR("pthread_condattr_setclock consumer data");
2342 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2345 PERROR("pthread_cond_init consumer data");
2349 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2350 thread_manage_consumer
, consumer_data
);
2353 PERROR("pthread_create consumer");
2358 /* We are about to wait on a pthread condition */
2359 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2361 /* Get time for sem_timedwait absolute timeout */
2362 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2364 * Set the timeout for the condition timed wait even if the clock gettime
2365 * call fails since we might loop on that call and we want to avoid to
2366 * increment the timeout too many times.
2368 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2371 * The following loop COULD be skipped in some conditions so this is why we
2372 * set ret to 0 in order to make sure at least one round of the loop is
2378 * Loop until the condition is reached or when a timeout is reached. Note
2379 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2380 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2381 * possible. This loop does not take any chances and works with both of
2384 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2385 if (clock_ret
< 0) {
2386 PERROR("clock_gettime spawn consumer");
2387 /* Infinite wait for the consumerd thread to be ready */
2388 ret
= pthread_cond_wait(&consumer_data
->cond
,
2389 &consumer_data
->cond_mutex
);
2391 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2392 &consumer_data
->cond_mutex
, &timeout
);
2396 /* Release the pthread condition */
2397 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2401 if (ret
== ETIMEDOUT
) {
2405 * Call has timed out so we kill the kconsumerd_thread and return
2408 ERR("Condition timed out. The consumer thread was never ready."
2410 pth_ret
= pthread_cancel(consumer_data
->thread
);
2412 PERROR("pthread_cancel consumer thread");
2415 PERROR("pthread_cond_wait failed consumer thread");
2417 /* Caller is expecting a negative value on failure. */
2422 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2423 if (consumer_data
->pid
== 0) {
2424 ERR("Consumerd did not start");
2425 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2428 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2437 * Join consumer thread
2439 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2443 /* Consumer pid must be a real one. */
2444 if (consumer_data
->pid
> 0) {
2446 ret
= kill(consumer_data
->pid
, SIGTERM
);
2448 PERROR("Error killing consumer daemon");
2451 return pthread_join(consumer_data
->thread
, &status
);
2458 * Fork and exec a consumer daemon (consumerd).
2460 * Return pid if successful else -1.
2462 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2466 const char *consumer_to_use
;
2467 const char *verbosity
;
2470 DBG("Spawning consumerd");
2477 if (config
.verbose_consumer
) {
2478 verbosity
= "--verbose";
2479 } else if (lttng_opt_quiet
) {
2480 verbosity
= "--quiet";
2485 switch (consumer_data
->type
) {
2486 case LTTNG_CONSUMER_KERNEL
:
2488 * Find out which consumerd to execute. We will first try the
2489 * 64-bit path, then the sessiond's installation directory, and
2490 * fallback on the 32-bit one,
2492 DBG3("Looking for a kernel consumer at these locations:");
2493 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2494 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2495 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2496 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2497 DBG3("Found location #1");
2498 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2499 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2500 DBG3("Found location #2");
2501 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2502 } else if (stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2503 DBG3("Found location #3");
2504 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2506 DBG("Could not find any valid consumerd executable");
2510 DBG("Using kernel consumer at: %s", consumer_to_use
);
2511 (void) execl(consumer_to_use
,
2512 "lttng-consumerd", verbosity
, "-k",
2513 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2514 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2515 "--group", config
.tracing_group_name
.value
,
2518 case LTTNG_CONSUMER64_UST
:
2520 if (config
.consumerd64_lib_dir
.value
) {
2525 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2529 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2530 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2535 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2536 if (tmp
[0] != '\0') {
2537 strcat(tmpnew
, ":");
2538 strcat(tmpnew
, tmp
);
2540 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2547 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2548 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2549 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2550 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2551 "--group", config
.tracing_group_name
.value
,
2555 case LTTNG_CONSUMER32_UST
:
2557 if (config
.consumerd32_lib_dir
.value
) {
2562 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2566 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2567 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2572 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2573 if (tmp
[0] != '\0') {
2574 strcat(tmpnew
, ":");
2575 strcat(tmpnew
, tmp
);
2577 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2584 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2585 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2586 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2587 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2588 "--group", config
.tracing_group_name
.value
,
2593 ERR("unknown consumer type");
2597 PERROR("Consumer execl()");
2599 /* Reaching this point, we got a failure on our execl(). */
2601 } else if (pid
> 0) {
2604 PERROR("start consumer fork");
2612 * Spawn the consumerd daemon and session daemon thread.
2614 static int start_consumerd(struct consumer_data
*consumer_data
)
2619 * Set the listen() state on the socket since there is a possible race
2620 * between the exec() of the consumer daemon and this call if place in the
2621 * consumer thread. See bug #366 for more details.
2623 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2628 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2629 if (consumer_data
->pid
!= 0) {
2630 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2634 ret
= spawn_consumerd(consumer_data
);
2636 ERR("Spawning consumerd failed");
2637 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2641 /* Setting up the consumer_data pid */
2642 consumer_data
->pid
= ret
;
2643 DBG2("Consumer pid %d", consumer_data
->pid
);
2644 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2646 DBG2("Spawning consumer control thread");
2647 ret
= spawn_consumer_thread(consumer_data
);
2649 ERR("Fatal error spawning consumer control thread");
2657 /* Cleanup already created sockets on error. */
2658 if (consumer_data
->err_sock
>= 0) {
2661 err
= close(consumer_data
->err_sock
);
2663 PERROR("close consumer data error socket");
2670 * Setup necessary data for kernel tracer action.
2672 static int init_kernel_tracer(void)
2676 /* Modprobe lttng kernel modules */
2677 ret
= modprobe_lttng_control();
2682 /* Open debugfs lttng */
2683 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2684 if (kernel_tracer_fd
< 0) {
2685 DBG("Failed to open %s", module_proc_lttng
);
2689 /* Validate kernel version */
2690 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2691 &kernel_tracer_abi_version
);
2696 ret
= modprobe_lttng_data();
2701 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2708 WARN("Kernel tracer does not support buffer monitoring. "
2709 "The monitoring timer of channels in the kernel domain "
2710 "will be set to 0 (disabled).");
2713 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2717 modprobe_remove_lttng_control();
2718 ret
= close(kernel_tracer_fd
);
2722 kernel_tracer_fd
= -1;
2723 return LTTNG_ERR_KERN_VERSION
;
2726 ret
= close(kernel_tracer_fd
);
2732 modprobe_remove_lttng_control();
2735 WARN("No kernel tracer available");
2736 kernel_tracer_fd
= -1;
2738 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2740 return LTTNG_ERR_KERN_NA
;
2746 * Copy consumer output from the tracing session to the domain session. The
2747 * function also applies the right modification on a per domain basis for the
2748 * trace files destination directory.
2750 * Should *NOT* be called with RCU read-side lock held.
2752 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2755 const char *dir_name
;
2756 struct consumer_output
*consumer
;
2759 assert(session
->consumer
);
2762 case LTTNG_DOMAIN_KERNEL
:
2763 DBG3("Copying tracing session consumer output in kernel session");
2765 * XXX: We should audit the session creation and what this function
2766 * does "extra" in order to avoid a destroy since this function is used
2767 * in the domain session creation (kernel and ust) only. Same for UST
2770 if (session
->kernel_session
->consumer
) {
2771 consumer_output_put(session
->kernel_session
->consumer
);
2773 session
->kernel_session
->consumer
=
2774 consumer_copy_output(session
->consumer
);
2775 /* Ease our life a bit for the next part */
2776 consumer
= session
->kernel_session
->consumer
;
2777 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2779 case LTTNG_DOMAIN_JUL
:
2780 case LTTNG_DOMAIN_LOG4J
:
2781 case LTTNG_DOMAIN_PYTHON
:
2782 case LTTNG_DOMAIN_UST
:
2783 DBG3("Copying tracing session consumer output in UST session");
2784 if (session
->ust_session
->consumer
) {
2785 consumer_output_put(session
->ust_session
->consumer
);
2787 session
->ust_session
->consumer
=
2788 consumer_copy_output(session
->consumer
);
2789 /* Ease our life a bit for the next part */
2790 consumer
= session
->ust_session
->consumer
;
2791 dir_name
= DEFAULT_UST_TRACE_DIR
;
2794 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2798 /* Append correct directory to subdir */
2799 strncat(consumer
->subdir
, dir_name
,
2800 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2801 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2810 * Create an UST session and add it to the session ust list.
2812 * Should *NOT* be called with RCU read-side lock held.
2814 static int create_ust_session(struct ltt_session
*session
,
2815 struct lttng_domain
*domain
)
2818 struct ltt_ust_session
*lus
= NULL
;
2822 assert(session
->consumer
);
2824 switch (domain
->type
) {
2825 case LTTNG_DOMAIN_JUL
:
2826 case LTTNG_DOMAIN_LOG4J
:
2827 case LTTNG_DOMAIN_PYTHON
:
2828 case LTTNG_DOMAIN_UST
:
2831 ERR("Unknown UST domain on create session %d", domain
->type
);
2832 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2836 DBG("Creating UST session");
2838 lus
= trace_ust_create_session(session
->id
);
2840 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2844 lus
->uid
= session
->uid
;
2845 lus
->gid
= session
->gid
;
2846 lus
->output_traces
= session
->output_traces
;
2847 lus
->snapshot_mode
= session
->snapshot_mode
;
2848 lus
->live_timer_interval
= session
->live_timer
;
2849 session
->ust_session
= lus
;
2850 if (session
->shm_path
[0]) {
2851 strncpy(lus
->root_shm_path
, session
->shm_path
,
2852 sizeof(lus
->root_shm_path
));
2853 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2854 strncpy(lus
->shm_path
, session
->shm_path
,
2855 sizeof(lus
->shm_path
));
2856 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2857 strncat(lus
->shm_path
, "/ust",
2858 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2860 /* Copy session output to the newly created UST session */
2861 ret
= copy_session_consumer(domain
->type
, session
);
2862 if (ret
!= LTTNG_OK
) {
2870 session
->ust_session
= NULL
;
2875 * Create a kernel tracer session then create the default channel.
2877 static int create_kernel_session(struct ltt_session
*session
)
2881 DBG("Creating kernel session");
2883 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2885 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2889 /* Code flow safety */
2890 assert(session
->kernel_session
);
2892 /* Copy session output to the newly created Kernel session */
2893 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2894 if (ret
!= LTTNG_OK
) {
2898 session
->kernel_session
->uid
= session
->uid
;
2899 session
->kernel_session
->gid
= session
->gid
;
2900 session
->kernel_session
->output_traces
= session
->output_traces
;
2901 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2906 trace_kernel_destroy_session(session
->kernel_session
);
2907 session
->kernel_session
= NULL
;
2912 * Count number of session permitted by uid/gid.
2914 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2917 struct ltt_session
*session
;
2919 DBG("Counting number of available session for UID %d GID %d",
2921 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2923 * Only list the sessions the user can control.
2925 if (!session_access_ok(session
, uid
, gid
)) {
2934 * Check if the current kernel tracer supports the session rotation feature.
2935 * Return 1 if it does, 0 otherwise.
2937 static int check_rotate_compatible(void)
2941 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2942 DBG("Kernel tracer version is not compatible with the rotation feature");
2950 * Process the command requested by the lttng client within the command
2951 * context structure. This function make sure that the return structure (llm)
2952 * is set and ready for transmission before returning.
2954 * Return any error encountered or 0 for success.
2956 * "sock" is only used for special-case var. len data.
2958 * Should *NOT* be called with RCU read-side lock held.
2960 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2964 int need_tracing_session
= 1;
2967 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2969 assert(!rcu_read_ongoing());
2973 switch (cmd_ctx
->lsm
->cmd_type
) {
2974 case LTTNG_CREATE_SESSION
:
2975 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2976 case LTTNG_CREATE_SESSION_LIVE
:
2977 case LTTNG_DESTROY_SESSION
:
2978 case LTTNG_LIST_SESSIONS
:
2979 case LTTNG_LIST_DOMAINS
:
2980 case LTTNG_START_TRACE
:
2981 case LTTNG_STOP_TRACE
:
2982 case LTTNG_DATA_PENDING
:
2983 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2984 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2985 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2986 case LTTNG_SNAPSHOT_RECORD
:
2987 case LTTNG_SAVE_SESSION
:
2988 case LTTNG_SET_SESSION_SHM_PATH
:
2989 case LTTNG_REGENERATE_METADATA
:
2990 case LTTNG_REGENERATE_STATEDUMP
:
2991 case LTTNG_REGISTER_TRIGGER
:
2992 case LTTNG_UNREGISTER_TRIGGER
:
2993 case LTTNG_ROTATE_SESSION
:
2994 case LTTNG_ROTATION_GET_INFO
:
2995 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
2996 case LTTNG_ROTATION_SET_SCHEDULE
:
2997 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
3004 if (config
.no_kernel
&& need_domain
3005 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3007 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3009 ret
= LTTNG_ERR_KERN_NA
;
3014 /* Deny register consumer if we already have a spawned consumer. */
3015 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3016 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3017 if (kconsumer_data
.pid
> 0) {
3018 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3019 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3022 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3026 * Check for command that don't needs to allocate a returned payload. We do
3027 * this here so we don't have to make the call for no payload at each
3030 switch(cmd_ctx
->lsm
->cmd_type
) {
3031 case LTTNG_LIST_SESSIONS
:
3032 case LTTNG_LIST_TRACEPOINTS
:
3033 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3034 case LTTNG_LIST_DOMAINS
:
3035 case LTTNG_LIST_CHANNELS
:
3036 case LTTNG_LIST_EVENTS
:
3037 case LTTNG_LIST_SYSCALLS
:
3038 case LTTNG_LIST_TRACKER_PIDS
:
3039 case LTTNG_DATA_PENDING
:
3040 case LTTNG_ROTATE_SESSION
:
3041 case LTTNG_ROTATION_GET_INFO
:
3042 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
3045 /* Setup lttng message with no payload */
3046 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3048 /* This label does not try to unlock the session */
3049 goto init_setup_error
;
3053 /* Commands that DO NOT need a session. */
3054 switch (cmd_ctx
->lsm
->cmd_type
) {
3055 case LTTNG_CREATE_SESSION
:
3056 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3057 case LTTNG_CREATE_SESSION_LIVE
:
3058 case LTTNG_LIST_SESSIONS
:
3059 case LTTNG_LIST_TRACEPOINTS
:
3060 case LTTNG_LIST_SYSCALLS
:
3061 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3062 case LTTNG_SAVE_SESSION
:
3063 case LTTNG_REGISTER_TRIGGER
:
3064 case LTTNG_UNREGISTER_TRIGGER
:
3065 need_tracing_session
= 0;
3068 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3070 * We keep the session list lock across _all_ commands
3071 * for now, because the per-session lock does not
3072 * handle teardown properly.
3074 session_lock_list();
3075 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3076 if (cmd_ctx
->session
== NULL
) {
3077 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3080 /* Acquire lock for the session */
3081 session_lock(cmd_ctx
->session
);
3087 * Commands that need a valid session but should NOT create one if none
3088 * exists. Instead of creating one and destroying it when the command is
3089 * handled, process that right before so we save some round trip in useless
3092 switch (cmd_ctx
->lsm
->cmd_type
) {
3093 case LTTNG_DISABLE_CHANNEL
:
3094 case LTTNG_DISABLE_EVENT
:
3095 switch (cmd_ctx
->lsm
->domain
.type
) {
3096 case LTTNG_DOMAIN_KERNEL
:
3097 if (!cmd_ctx
->session
->kernel_session
) {
3098 ret
= LTTNG_ERR_NO_CHANNEL
;
3102 case LTTNG_DOMAIN_JUL
:
3103 case LTTNG_DOMAIN_LOG4J
:
3104 case LTTNG_DOMAIN_PYTHON
:
3105 case LTTNG_DOMAIN_UST
:
3106 if (!cmd_ctx
->session
->ust_session
) {
3107 ret
= LTTNG_ERR_NO_CHANNEL
;
3112 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3124 * Check domain type for specific "pre-action".
3126 switch (cmd_ctx
->lsm
->domain
.type
) {
3127 case LTTNG_DOMAIN_KERNEL
:
3129 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3133 /* Kernel tracer check */
3134 if (kernel_tracer_fd
== -1) {
3135 /* Basically, load kernel tracer modules */
3136 ret
= init_kernel_tracer();
3142 /* Consumer is in an ERROR state. Report back to client */
3143 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3144 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3148 /* Need a session for kernel command */
3149 if (need_tracing_session
) {
3150 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3151 ret
= create_kernel_session(cmd_ctx
->session
);
3153 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3158 /* Start the kernel consumer daemon */
3159 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3160 if (kconsumer_data
.pid
== 0 &&
3161 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3162 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3163 ret
= start_consumerd(&kconsumer_data
);
3165 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3168 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3170 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3174 * The consumer was just spawned so we need to add the socket to
3175 * the consumer output of the session if exist.
3177 ret
= consumer_create_socket(&kconsumer_data
,
3178 cmd_ctx
->session
->kernel_session
->consumer
);
3185 case LTTNG_DOMAIN_JUL
:
3186 case LTTNG_DOMAIN_LOG4J
:
3187 case LTTNG_DOMAIN_PYTHON
:
3188 case LTTNG_DOMAIN_UST
:
3190 if (!ust_app_supported()) {
3191 ret
= LTTNG_ERR_NO_UST
;
3194 /* Consumer is in an ERROR state. Report back to client */
3195 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3196 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3200 if (need_tracing_session
) {
3201 /* Create UST session if none exist. */
3202 if (cmd_ctx
->session
->ust_session
== NULL
) {
3203 ret
= create_ust_session(cmd_ctx
->session
,
3204 &cmd_ctx
->lsm
->domain
);
3205 if (ret
!= LTTNG_OK
) {
3210 /* Start the UST consumer daemons */
3212 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3213 if (config
.consumerd64_bin_path
.value
&&
3214 ustconsumer64_data
.pid
== 0 &&
3215 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3216 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3217 ret
= start_consumerd(&ustconsumer64_data
);
3219 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3220 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3224 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3225 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3227 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3231 * Setup socket for consumer 64 bit. No need for atomic access
3232 * since it was set above and can ONLY be set in this thread.
3234 ret
= consumer_create_socket(&ustconsumer64_data
,
3235 cmd_ctx
->session
->ust_session
->consumer
);
3241 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3242 if (config
.consumerd32_bin_path
.value
&&
3243 ustconsumer32_data
.pid
== 0 &&
3244 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3245 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3246 ret
= start_consumerd(&ustconsumer32_data
);
3248 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3249 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3253 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3254 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3256 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3260 * Setup socket for consumer 64 bit. No need for atomic access
3261 * since it was set above and can ONLY be set in this thread.
3263 ret
= consumer_create_socket(&ustconsumer32_data
,
3264 cmd_ctx
->session
->ust_session
->consumer
);
3276 /* Validate consumer daemon state when start/stop trace command */
3277 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3278 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3279 switch (cmd_ctx
->lsm
->domain
.type
) {
3280 case LTTNG_DOMAIN_NONE
:
3282 case LTTNG_DOMAIN_JUL
:
3283 case LTTNG_DOMAIN_LOG4J
:
3284 case LTTNG_DOMAIN_PYTHON
:
3285 case LTTNG_DOMAIN_UST
:
3286 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3287 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3291 case LTTNG_DOMAIN_KERNEL
:
3292 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3293 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3298 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3304 * Check that the UID or GID match that of the tracing session.
3305 * The root user can interact with all sessions.
3307 if (need_tracing_session
) {
3308 if (!session_access_ok(cmd_ctx
->session
,
3309 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3310 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3311 ret
= LTTNG_ERR_EPERM
;
3317 * Send relayd information to consumer as soon as we have a domain and a
3320 if (cmd_ctx
->session
&& need_domain
) {
3322 * Setup relayd if not done yet. If the relayd information was already
3323 * sent to the consumer, this call will gracefully return.
3325 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3326 if (ret
!= LTTNG_OK
) {
3331 /* Process by command type */
3332 switch (cmd_ctx
->lsm
->cmd_type
) {
3333 case LTTNG_ADD_CONTEXT
:
3336 * An LTTNG_ADD_CONTEXT command might have a supplementary
3337 * payload if the context being added is an application context.
3339 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3340 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3341 char *provider_name
= NULL
, *context_name
= NULL
;
3342 size_t provider_name_len
=
3343 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3344 size_t context_name_len
=
3345 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3347 if (provider_name_len
== 0 || context_name_len
== 0) {
3349 * Application provider and context names MUST
3352 ret
= -LTTNG_ERR_INVALID
;
3356 provider_name
= zmalloc(provider_name_len
+ 1);
3357 if (!provider_name
) {
3358 ret
= -LTTNG_ERR_NOMEM
;
3361 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3364 context_name
= zmalloc(context_name_len
+ 1);
3365 if (!context_name
) {
3366 ret
= -LTTNG_ERR_NOMEM
;
3367 goto error_add_context
;
3369 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3372 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3375 goto error_add_context
;
3378 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3381 goto error_add_context
;
3386 * cmd_add_context assumes ownership of the provider and context
3389 ret
= cmd_add_context(cmd_ctx
->session
,
3390 cmd_ctx
->lsm
->domain
.type
,
3391 cmd_ctx
->lsm
->u
.context
.channel_name
,
3392 &cmd_ctx
->lsm
->u
.context
.ctx
,
3393 kernel_poll_pipe
[1]);
3395 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3396 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3398 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3399 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3405 case LTTNG_DISABLE_CHANNEL
:
3407 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3408 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3411 case LTTNG_DISABLE_EVENT
:
3415 * FIXME: handle filter; for now we just receive the filter's
3416 * bytecode along with the filter expression which are sent by
3417 * liblttng-ctl and discard them.
3419 * This fixes an issue where the client may block while sending
3420 * the filter payload and encounter an error because the session
3421 * daemon closes the socket without ever handling this data.
3423 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3424 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3427 char data
[LTTNG_FILTER_MAX_LEN
];
3429 DBG("Discarding disable event command payload of size %zu", count
);
3431 ret
= lttcomm_recv_unix_sock(sock
, data
,
3432 count
> sizeof(data
) ? sizeof(data
) : count
);
3437 count
-= (size_t) ret
;
3440 /* FIXME: passing packed structure to non-packed pointer */
3441 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3442 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3443 &cmd_ctx
->lsm
->u
.disable
.event
);
3446 case LTTNG_ENABLE_CHANNEL
:
3448 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3449 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3450 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3451 &cmd_ctx
->lsm
->u
.channel
.chan
,
3452 kernel_poll_pipe
[1]);
3455 case LTTNG_TRACK_PID
:
3457 ret
= cmd_track_pid(cmd_ctx
->session
,
3458 cmd_ctx
->lsm
->domain
.type
,
3459 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3462 case LTTNG_UNTRACK_PID
:
3464 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3465 cmd_ctx
->lsm
->domain
.type
,
3466 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3469 case LTTNG_ENABLE_EVENT
:
3471 struct lttng_event_exclusion
*exclusion
= NULL
;
3472 struct lttng_filter_bytecode
*bytecode
= NULL
;
3473 char *filter_expression
= NULL
;
3475 /* Handle exclusion events and receive it from the client. */
3476 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3477 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3479 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3480 (count
* LTTNG_SYMBOL_NAME_LEN
));
3482 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3486 DBG("Receiving var len exclusion event list from client ...");
3487 exclusion
->count
= count
;
3488 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3489 count
* LTTNG_SYMBOL_NAME_LEN
);
3491 DBG("Nothing recv() from client var len data... continuing");
3494 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3499 /* Get filter expression from client. */
3500 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3501 size_t expression_len
=
3502 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3504 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3505 ret
= LTTNG_ERR_FILTER_INVAL
;
3510 filter_expression
= zmalloc(expression_len
);
3511 if (!filter_expression
) {
3513 ret
= LTTNG_ERR_FILTER_NOMEM
;
3517 /* Receive var. len. data */
3518 DBG("Receiving var len filter's expression from client ...");
3519 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3522 DBG("Nothing recv() from client car len data... continuing");
3524 free(filter_expression
);
3526 ret
= LTTNG_ERR_FILTER_INVAL
;
3531 /* Handle filter and get bytecode from client. */
3532 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3533 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3535 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3536 ret
= LTTNG_ERR_FILTER_INVAL
;
3537 free(filter_expression
);
3542 bytecode
= zmalloc(bytecode_len
);
3544 free(filter_expression
);
3546 ret
= LTTNG_ERR_FILTER_NOMEM
;
3550 /* Receive var. len. data */
3551 DBG("Receiving var len filter's bytecode from client ...");
3552 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3554 DBG("Nothing recv() from client car len data... continuing");
3556 free(filter_expression
);
3559 ret
= LTTNG_ERR_FILTER_INVAL
;
3563 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3564 free(filter_expression
);
3567 ret
= LTTNG_ERR_FILTER_INVAL
;
3572 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3573 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3574 &cmd_ctx
->lsm
->u
.enable
.event
,
3575 filter_expression
, bytecode
, exclusion
,
3576 kernel_poll_pipe
[1]);
3579 case LTTNG_LIST_TRACEPOINTS
:
3581 struct lttng_event
*events
;
3584 session_lock_list();
3585 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3586 session_unlock_list();
3587 if (nb_events
< 0) {
3588 /* Return value is a negative lttng_error_code. */
3594 * Setup lttng message with payload size set to the event list size in
3595 * bytes and then copy list into the llm payload.
3597 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3598 sizeof(struct lttng_event
) * nb_events
);
3608 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3610 struct lttng_event_field
*fields
;
3613 session_lock_list();
3614 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3616 session_unlock_list();
3617 if (nb_fields
< 0) {
3618 /* Return value is a negative lttng_error_code. */
3624 * Setup lttng message with payload size set to the event list size in
3625 * bytes and then copy list into the llm payload.
3627 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3628 sizeof(struct lttng_event_field
) * nb_fields
);
3638 case LTTNG_LIST_SYSCALLS
:
3640 struct lttng_event
*events
;
3643 nb_events
= cmd_list_syscalls(&events
);
3644 if (nb_events
< 0) {
3645 /* Return value is a negative lttng_error_code. */
3651 * Setup lttng message with payload size set to the event list size in
3652 * bytes and then copy list into the llm payload.
3654 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3655 sizeof(struct lttng_event
) * nb_events
);
3665 case LTTNG_LIST_TRACKER_PIDS
:
3667 int32_t *pids
= NULL
;
3670 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3671 cmd_ctx
->lsm
->domain
.type
, &pids
);
3673 /* Return value is a negative lttng_error_code. */
3679 * Setup lttng message with payload size set to the event list size in
3680 * bytes and then copy list into the llm payload.
3682 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3683 sizeof(int32_t) * nr_pids
);
3693 case LTTNG_SET_CONSUMER_URI
:
3696 struct lttng_uri
*uris
;
3698 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3699 len
= nb_uri
* sizeof(struct lttng_uri
);
3702 ret
= LTTNG_ERR_INVALID
;
3706 uris
= zmalloc(len
);
3708 ret
= LTTNG_ERR_FATAL
;
3712 /* Receive variable len data */
3713 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3714 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3716 DBG("No URIs received from client... continuing");
3718 ret
= LTTNG_ERR_SESSION_FAIL
;
3723 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3725 if (ret
!= LTTNG_OK
) {
3732 case LTTNG_START_TRACE
:
3735 * On the first start, if we have a kernel session and we have
3736 * enabled time or size-based rotations, we have to make sure
3737 * the kernel tracer supports it.
3739 if (!cmd_ctx
->session
->has_been_started
&& \
3740 cmd_ctx
->session
->kernel_session
&& \
3741 (cmd_ctx
->session
->rotate_timer_period
|| \
3742 cmd_ctx
->session
->rotate_size
) && \
3743 !check_rotate_compatible()) {
3744 DBG("Kernel tracer version is not compatible with the rotation feature");
3745 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3748 ret
= cmd_start_trace(cmd_ctx
->session
);
3751 case LTTNG_STOP_TRACE
:
3753 ret
= cmd_stop_trace(cmd_ctx
->session
);
3756 case LTTNG_CREATE_SESSION
:
3759 struct lttng_uri
*uris
= NULL
;
3761 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3762 len
= nb_uri
* sizeof(struct lttng_uri
);
3765 uris
= zmalloc(len
);
3767 ret
= LTTNG_ERR_FATAL
;
3771 /* Receive variable len data */
3772 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3773 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3775 DBG("No URIs received from client... continuing");
3777 ret
= LTTNG_ERR_SESSION_FAIL
;
3782 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3783 DBG("Creating session with ONE network URI is a bad call");
3784 ret
= LTTNG_ERR_SESSION_FAIL
;
3790 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3791 &cmd_ctx
->creds
, 0);
3797 case LTTNG_DESTROY_SESSION
:
3799 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1],
3800 notification_thread_handle
);
3802 /* Set session to NULL so we do not unlock it after free. */
3803 cmd_ctx
->session
= NULL
;
3806 case LTTNG_LIST_DOMAINS
:
3809 struct lttng_domain
*domains
= NULL
;
3811 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3813 /* Return value is a negative lttng_error_code. */
3818 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3819 nb_dom
* sizeof(struct lttng_domain
));
3829 case LTTNG_LIST_CHANNELS
:
3831 ssize_t payload_size
;
3832 struct lttng_channel
*channels
= NULL
;
3834 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3835 cmd_ctx
->session
, &channels
);
3836 if (payload_size
< 0) {
3837 /* Return value is a negative lttng_error_code. */
3838 ret
= -payload_size
;
3842 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3853 case LTTNG_LIST_EVENTS
:
3856 struct lttng_event
*events
= NULL
;
3857 struct lttcomm_event_command_header cmd_header
;
3860 memset(&cmd_header
, 0, sizeof(cmd_header
));
3861 /* Extended infos are included at the end of events */
3862 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3863 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3864 &events
, &total_size
);
3867 /* Return value is a negative lttng_error_code. */
3872 cmd_header
.nb_events
= nb_event
;
3873 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3874 &cmd_header
, sizeof(cmd_header
));
3884 case LTTNG_LIST_SESSIONS
:
3886 unsigned int nr_sessions
;
3887 void *sessions_payload
;
3890 session_lock_list();
3891 nr_sessions
= lttng_sessions_count(
3892 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3893 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3894 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3895 sessions_payload
= zmalloc(payload_len
);
3897 if (!sessions_payload
) {
3898 session_unlock_list();
3903 cmd_list_lttng_sessions(sessions_payload
,
3904 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3905 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3906 session_unlock_list();
3908 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3910 free(sessions_payload
);
3919 case LTTNG_REGISTER_CONSUMER
:
3921 struct consumer_data
*cdata
;
3923 switch (cmd_ctx
->lsm
->domain
.type
) {
3924 case LTTNG_DOMAIN_KERNEL
:
3925 cdata
= &kconsumer_data
;
3928 ret
= LTTNG_ERR_UND
;
3932 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3933 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3936 case LTTNG_DATA_PENDING
:
3939 uint8_t pending_ret_byte
;
3941 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3946 * This function may returns 0 or 1 to indicate whether or not
3947 * there is data pending. In case of error, it should return an
3948 * LTTNG_ERR code. However, some code paths may still return
3949 * a nondescript error code, which we handle by returning an
3952 if (pending_ret
== 0 || pending_ret
== 1) {
3954 * ret will be set to LTTNG_OK at the end of
3957 } else if (pending_ret
< 0) {
3958 ret
= LTTNG_ERR_UNK
;
3965 pending_ret_byte
= (uint8_t) pending_ret
;
3967 /* 1 byte to return whether or not data is pending */
3968 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3969 &pending_ret_byte
, 1);
3978 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3980 struct lttcomm_lttng_output_id reply
;
3982 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3983 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3984 if (ret
!= LTTNG_OK
) {
3988 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3994 /* Copy output list into message payload */
3998 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
4000 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
4001 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
4004 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
4007 struct lttng_snapshot_output
*outputs
= NULL
;
4009 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
4010 if (nb_output
< 0) {
4015 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4016 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4017 nb_output
* sizeof(struct lttng_snapshot_output
));
4027 case LTTNG_SNAPSHOT_RECORD
:
4029 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4030 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4031 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4034 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4037 struct lttng_uri
*uris
= NULL
;
4039 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4040 len
= nb_uri
* sizeof(struct lttng_uri
);
4043 uris
= zmalloc(len
);
4045 ret
= LTTNG_ERR_FATAL
;
4049 /* Receive variable len data */
4050 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4051 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4053 DBG("No URIs received from client... continuing");
4055 ret
= LTTNG_ERR_SESSION_FAIL
;
4060 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4061 DBG("Creating session with ONE network URI is a bad call");
4062 ret
= LTTNG_ERR_SESSION_FAIL
;
4068 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4069 nb_uri
, &cmd_ctx
->creds
);
4073 case LTTNG_CREATE_SESSION_LIVE
:
4076 struct lttng_uri
*uris
= NULL
;
4078 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4079 len
= nb_uri
* sizeof(struct lttng_uri
);
4082 uris
= zmalloc(len
);
4084 ret
= LTTNG_ERR_FATAL
;
4088 /* Receive variable len data */
4089 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4090 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4092 DBG("No URIs received from client... continuing");
4094 ret
= LTTNG_ERR_SESSION_FAIL
;
4099 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4100 DBG("Creating session with ONE network URI is a bad call");
4101 ret
= LTTNG_ERR_SESSION_FAIL
;
4107 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4108 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4112 case LTTNG_SAVE_SESSION
:
4114 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4118 case LTTNG_SET_SESSION_SHM_PATH
:
4120 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4121 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4124 case LTTNG_REGENERATE_METADATA
:
4126 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4129 case LTTNG_REGENERATE_STATEDUMP
:
4131 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4134 case LTTNG_REGISTER_TRIGGER
:
4136 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4137 notification_thread_handle
);
4140 case LTTNG_UNREGISTER_TRIGGER
:
4142 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4143 notification_thread_handle
);
4146 case LTTNG_ROTATE_SESSION
:
4148 struct lttng_rotate_session_return rotate_return
;
4150 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4152 memset(&rotate_return
, 0, sizeof(rotate_return
));
4153 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4154 DBG("Kernel tracer version is not compatible with the rotation feature");
4155 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4159 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4165 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4166 sizeof(rotate_return
));
4175 case LTTNG_ROTATION_GET_INFO
:
4177 struct lttng_rotation_get_info_return get_info_return
;
4179 memset(&get_info_return
, 0, sizeof(get_info_return
));
4180 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4181 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4187 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4188 sizeof(get_info_return
));
4197 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
4199 struct lttng_session_get_current_output_return output_return
;
4201 memset(&output_return
, 0, sizeof(output_return
));
4202 ret
= cmd_session_get_current_output(cmd_ctx
->session
,
4209 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &output_return
,
4210 sizeof(output_return
));
4219 case LTTNG_ROTATION_SET_SCHEDULE
:
4222 enum lttng_rotation_schedule_type schedule_type
;
4225 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4226 DBG("Kernel tracer version does not support session rotations");
4227 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4231 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
4232 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
4233 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
4235 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4239 notification_thread_handle
);
4240 if (ret
!= LTTNG_OK
) {
4246 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
4248 struct lttng_session_list_schedules_return schedules
= {
4249 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
4250 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
4251 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
4252 .size
.value
= cmd_ctx
->session
->rotate_size
,
4255 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
4266 ret
= LTTNG_ERR_UND
;
4271 if (cmd_ctx
->llm
== NULL
) {
4272 DBG("Missing llm structure. Allocating one.");
4273 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4277 /* Set return code */
4278 cmd_ctx
->llm
->ret_code
= ret
;
4280 if (cmd_ctx
->session
) {
4281 session_unlock(cmd_ctx
->session
);
4283 if (need_tracing_session
) {
4284 session_unlock_list();
4287 assert(!rcu_read_ongoing());
4292 * Thread managing health check socket.
4294 static void *thread_manage_health(void *data
)
4296 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4297 uint32_t revents
, nb_fd
;
4298 struct lttng_poll_event events
;
4299 struct health_comm_msg msg
;
4300 struct health_comm_reply reply
;
4302 DBG("[thread] Manage health check started");
4304 rcu_register_thread();
4306 /* We might hit an error path before this is created. */
4307 lttng_poll_init(&events
);
4309 /* Create unix socket */
4310 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4312 ERR("Unable to create health check Unix socket");
4317 /* lttng health client socket path permissions */
4318 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4319 utils_get_group_id(config
.tracing_group_name
.value
));
4321 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4326 ret
= chmod(config
.health_unix_sock_path
.value
,
4327 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4329 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4336 * Set the CLOEXEC flag. Return code is useless because either way, the
4339 (void) utils_set_fd_cloexec(sock
);
4341 ret
= lttcomm_listen_unix_sock(sock
);
4347 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4348 * more will be added to this poll set.
4350 ret
= sessiond_set_thread_pollset(&events
, 2);
4355 /* Add the application registration socket */
4356 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4361 sessiond_notify_ready();
4364 DBG("Health check ready");
4366 /* Inifinite blocking call, waiting for transmission */
4368 ret
= lttng_poll_wait(&events
, -1);
4371 * Restart interrupted system call.
4373 if (errno
== EINTR
) {
4381 for (i
= 0; i
< nb_fd
; i
++) {
4382 /* Fetch once the poll data */
4383 revents
= LTTNG_POLL_GETEV(&events
, i
);
4384 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4387 /* No activity for this FD (poll implementation). */
4391 /* Thread quit pipe has been closed. Killing thread. */
4392 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4398 /* Event on the registration socket */
4399 if (pollfd
== sock
) {
4400 if (revents
& LPOLLIN
) {
4402 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4403 ERR("Health socket poll error");
4406 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4412 new_sock
= lttcomm_accept_unix_sock(sock
);
4418 * Set the CLOEXEC flag. Return code is useless because either way, the
4421 (void) utils_set_fd_cloexec(new_sock
);
4423 DBG("Receiving data from client for health...");
4424 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4426 DBG("Nothing recv() from client... continuing");
4427 ret
= close(new_sock
);
4434 rcu_thread_online();
4436 memset(&reply
, 0, sizeof(reply
));
4437 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4439 * health_check_state returns 0 if health is
4442 if (!health_check_state(health_sessiond
, i
)) {
4443 reply
.ret_code
|= 1ULL << i
;
4447 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4449 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4451 ERR("Failed to send health data back to client");
4454 /* End of transmission */
4455 ret
= close(new_sock
);
4464 ERR("Health error occurred in %s", __func__
);
4466 DBG("Health check thread dying");
4467 unlink(config
.health_unix_sock_path
.value
);
4475 lttng_poll_clean(&events
);
4477 rcu_unregister_thread();
4482 * This thread manage all clients request using the unix client socket for
4485 static void *thread_manage_clients(void *data
)
4487 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4489 uint32_t revents
, nb_fd
;
4490 struct command_ctx
*cmd_ctx
= NULL
;
4491 struct lttng_poll_event events
;
4493 DBG("[thread] Manage client started");
4495 rcu_register_thread();
4497 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4499 health_code_update();
4501 ret
= lttcomm_listen_unix_sock(client_sock
);
4507 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4508 * more will be added to this poll set.
4510 ret
= sessiond_set_thread_pollset(&events
, 2);
4512 goto error_create_poll
;
4515 /* Add the application registration socket */
4516 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4521 ret
= sem_post(&load_info
->message_thread_ready
);
4523 PERROR("sem_post message_thread_ready");
4528 * Wait until all support threads are initialized before accepting
4531 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4533 struct timeval timeout
;
4536 FD_SET(thread_quit_pipe
[0], &read_fds
);
4537 memset(&timeout
, 0, sizeof(timeout
));
4538 timeout
.tv_usec
= 1000;
4541 * If a support thread failed to launch, it may signal that
4542 * we must exit and the sessiond would never be marked as
4545 * The timeout is set to 1ms, which serves as a way to
4546 * pace down this check.
4548 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
4550 if (ret
> 0 || (ret
< 0 && errno
!= EINTR
)) {
4555 * This barrier is paired with the one in sessiond_notify_ready() to
4556 * ensure that loads accessing data initialized by the other threads,
4557 * on which this thread was waiting, are not performed before this point.
4559 * Note that this could be a 'read' memory barrier, but a full barrier
4560 * is used in case the code changes. The performance implications of
4561 * this choice are minimal since this is a slow path.
4565 /* This testpoint is after we signal readiness to the parent. */
4566 if (testpoint(sessiond_thread_manage_clients
)) {
4570 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4574 health_code_update();
4577 const struct cmd_completion_handler
*cmd_completion_handler
;
4579 DBG("Accepting client command ...");
4581 /* Inifinite blocking call, waiting for transmission */
4583 health_poll_entry();
4584 ret
= lttng_poll_wait(&events
, -1);
4588 * Restart interrupted system call.
4590 if (errno
== EINTR
) {
4598 for (i
= 0; i
< nb_fd
; i
++) {
4599 /* Fetch once the poll data */
4600 revents
= LTTNG_POLL_GETEV(&events
, i
);
4601 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4603 health_code_update();
4606 /* No activity for this FD (poll implementation). */
4610 /* Thread quit pipe has been closed. Killing thread. */
4611 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4617 /* Event on the registration socket */
4618 if (pollfd
== client_sock
) {
4619 if (revents
& LPOLLIN
) {
4621 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4622 ERR("Client socket poll error");
4625 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4631 DBG("Wait for client response");
4633 health_code_update();
4635 sock
= lttcomm_accept_unix_sock(client_sock
);
4641 * Set the CLOEXEC flag. Return code is useless because either way, the
4644 (void) utils_set_fd_cloexec(sock
);
4646 /* Set socket option for credentials retrieval */
4647 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4652 /* Allocate context command to process the client request */
4653 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4654 if (cmd_ctx
== NULL
) {
4655 PERROR("zmalloc cmd_ctx");
4659 /* Allocate data buffer for reception */
4660 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4661 if (cmd_ctx
->lsm
== NULL
) {
4662 PERROR("zmalloc cmd_ctx->lsm");
4666 cmd_ctx
->llm
= NULL
;
4667 cmd_ctx
->session
= NULL
;
4669 health_code_update();
4672 * Data is received from the lttng client. The struct
4673 * lttcomm_session_msg (lsm) contains the command and data request of
4676 DBG("Receiving data from client ...");
4677 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4678 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4680 DBG("Nothing recv() from client... continuing");
4686 clean_command_ctx(&cmd_ctx
);
4690 health_code_update();
4692 // TODO: Validate cmd_ctx including sanity check for
4693 // security purpose.
4695 rcu_thread_online();
4697 * This function dispatch the work to the kernel or userspace tracer
4698 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4699 * informations for the client. The command context struct contains
4700 * everything this function may needs.
4702 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4703 rcu_thread_offline();
4711 * TODO: Inform client somehow of the fatal error. At
4712 * this point, ret < 0 means that a zmalloc failed
4713 * (ENOMEM). Error detected but still accept
4714 * command, unless a socket error has been
4717 clean_command_ctx(&cmd_ctx
);
4721 cmd_completion_handler
= cmd_pop_completion_handler();
4722 if (cmd_completion_handler
) {
4723 enum lttng_error_code completion_code
;
4725 completion_code
= cmd_completion_handler
->run(
4726 cmd_completion_handler
->data
);
4727 if (completion_code
!= LTTNG_OK
) {
4728 clean_command_ctx(&cmd_ctx
);
4733 health_code_update();
4735 DBG("Sending response (size: %d, retcode: %s (%d))",
4736 cmd_ctx
->lttng_msg_size
,
4737 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4738 cmd_ctx
->llm
->ret_code
);
4739 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4741 ERR("Failed to send data back to client");
4744 /* End of transmission */
4751 clean_command_ctx(&cmd_ctx
);
4753 health_code_update();
4765 lttng_poll_clean(&events
);
4766 clean_command_ctx(&cmd_ctx
);
4770 unlink(config
.client_unix_sock_path
.value
);
4771 if (client_sock
>= 0) {
4772 ret
= close(client_sock
);
4780 ERR("Health error occurred in %s", __func__
);
4783 health_unregister(health_sessiond
);
4785 DBG("Client thread dying");
4787 rcu_unregister_thread();
4790 * Since we are creating the consumer threads, we own them, so we need
4791 * to join them before our thread exits.
4793 ret
= join_consumer_thread(&kconsumer_data
);
4796 PERROR("join_consumer");
4799 ret
= join_consumer_thread(&ustconsumer32_data
);
4802 PERROR("join_consumer ust32");
4805 ret
= join_consumer_thread(&ustconsumer64_data
);
4808 PERROR("join_consumer ust64");
4813 static int string_match(const char *str1
, const char *str2
)
4815 return (str1
&& str2
) && !strcmp(str1
, str2
);
4819 * Take an option from the getopt output and set it in the right variable to be
4822 * Return 0 on success else a negative value.
4824 static int set_option(int opt
, const char *arg
, const char *optname
)
4828 if (string_match(optname
, "client-sock") || opt
== 'c') {
4829 if (!arg
|| *arg
== '\0') {
4833 if (lttng_is_setuid_setgid()) {
4834 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4835 "-c, --client-sock");
4837 config_string_set(&config
.client_unix_sock_path
,
4839 if (!config
.client_unix_sock_path
.value
) {
4844 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4845 if (!arg
|| *arg
== '\0') {
4849 if (lttng_is_setuid_setgid()) {
4850 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4853 config_string_set(&config
.apps_unix_sock_path
,
4855 if (!config
.apps_unix_sock_path
.value
) {
4860 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4861 config
.daemonize
= true;
4862 } else if (string_match(optname
, "background") || opt
== 'b') {
4863 config
.background
= true;
4864 } else if (string_match(optname
, "group") || opt
== 'g') {
4865 if (!arg
|| *arg
== '\0') {
4869 if (lttng_is_setuid_setgid()) {
4870 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4873 config_string_set(&config
.tracing_group_name
,
4875 if (!config
.tracing_group_name
.value
) {
4880 } else if (string_match(optname
, "help") || opt
== 'h') {
4881 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4883 ERR("Cannot show --help for `lttng-sessiond`");
4886 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4887 } else if (string_match(optname
, "version") || opt
== 'V') {
4888 fprintf(stdout
, "%s\n", VERSION
);
4890 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4891 config
.sig_parent
= true;
4892 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4893 if (!arg
|| *arg
== '\0') {
4897 if (lttng_is_setuid_setgid()) {
4898 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4899 "--kconsumerd-err-sock");
4901 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4903 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4908 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4909 if (!arg
|| *arg
== '\0') {
4913 if (lttng_is_setuid_setgid()) {
4914 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4915 "--kconsumerd-cmd-sock");
4917 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4919 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4924 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4925 if (!arg
|| *arg
== '\0') {
4929 if (lttng_is_setuid_setgid()) {
4930 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4931 "--ustconsumerd64-err-sock");
4933 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4935 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4940 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4941 if (!arg
|| *arg
== '\0') {
4945 if (lttng_is_setuid_setgid()) {
4946 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4947 "--ustconsumerd64-cmd-sock");
4949 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4951 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4956 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4957 if (!arg
|| *arg
== '\0') {
4961 if (lttng_is_setuid_setgid()) {
4962 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4963 "--ustconsumerd32-err-sock");
4965 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4967 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4972 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4973 if (!arg
|| *arg
== '\0') {
4977 if (lttng_is_setuid_setgid()) {
4978 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4979 "--ustconsumerd32-cmd-sock");
4981 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4983 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4988 } else if (string_match(optname
, "no-kernel")) {
4989 config
.no_kernel
= true;
4990 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4991 config
.quiet
= true;
4992 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4993 /* Verbose level can increase using multiple -v */
4995 /* Value obtained from config file */
4996 config
.verbose
= config_parse_value(arg
);
4998 /* -v used on command line */
5001 /* Clamp value to [0, 3] */
5002 config
.verbose
= config
.verbose
< 0 ? 0 :
5003 (config
.verbose
<= 3 ? config
.verbose
: 3);
5004 } else if (string_match(optname
, "verbose-consumer")) {
5006 config
.verbose_consumer
= config_parse_value(arg
);
5008 config
.verbose_consumer
++;
5010 } else if (string_match(optname
, "consumerd32-path")) {
5011 if (!arg
|| *arg
== '\0') {
5015 if (lttng_is_setuid_setgid()) {
5016 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5017 "--consumerd32-path");
5019 config_string_set(&config
.consumerd32_bin_path
,
5021 if (!config
.consumerd32_bin_path
.value
) {
5026 } else if (string_match(optname
, "consumerd32-libdir")) {
5027 if (!arg
|| *arg
== '\0') {
5031 if (lttng_is_setuid_setgid()) {
5032 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5033 "--consumerd32-libdir");
5035 config_string_set(&config
.consumerd32_lib_dir
,
5037 if (!config
.consumerd32_lib_dir
.value
) {
5042 } else if (string_match(optname
, "consumerd64-path")) {
5043 if (!arg
|| *arg
== '\0') {
5047 if (lttng_is_setuid_setgid()) {
5048 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5049 "--consumerd64-path");
5051 config_string_set(&config
.consumerd64_bin_path
,
5053 if (!config
.consumerd64_bin_path
.value
) {
5058 } else if (string_match(optname
, "consumerd64-libdir")) {
5059 if (!arg
|| *arg
== '\0') {
5063 if (lttng_is_setuid_setgid()) {
5064 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5065 "--consumerd64-libdir");
5067 config_string_set(&config
.consumerd64_lib_dir
,
5069 if (!config
.consumerd64_lib_dir
.value
) {
5074 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
5075 if (!arg
|| *arg
== '\0') {
5079 if (lttng_is_setuid_setgid()) {
5080 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5083 config_string_set(&config
.pid_file_path
, strdup(arg
));
5084 if (!config
.pid_file_path
.value
) {
5089 } else if (string_match(optname
, "agent-tcp-port")) {
5090 if (!arg
|| *arg
== '\0') {
5094 if (lttng_is_setuid_setgid()) {
5095 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5096 "--agent-tcp-port");
5101 v
= strtoul(arg
, NULL
, 0);
5102 if (errno
!= 0 || !isdigit(arg
[0])) {
5103 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5106 if (v
== 0 || v
>= 65535) {
5107 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5110 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
5111 DBG3("Agent TCP port set to non default: %i", (int) v
);
5113 } else if (string_match(optname
, "load") || opt
== 'l') {
5114 if (!arg
|| *arg
== '\0') {
5118 if (lttng_is_setuid_setgid()) {
5119 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5122 config_string_set(&config
.load_session_path
, strdup(arg
));
5123 if (!config
.load_session_path
.value
) {
5128 } else if (string_match(optname
, "kmod-probes")) {
5129 if (!arg
|| *arg
== '\0') {
5133 if (lttng_is_setuid_setgid()) {
5134 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5137 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
5138 if (!config
.kmod_probes_list
.value
) {
5143 } else if (string_match(optname
, "extra-kmod-probes")) {
5144 if (!arg
|| *arg
== '\0') {
5148 if (lttng_is_setuid_setgid()) {
5149 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5150 "--extra-kmod-probes");
5152 config_string_set(&config
.kmod_extra_probes_list
,
5154 if (!config
.kmod_extra_probes_list
.value
) {
5159 } else if (string_match(optname
, "config") || opt
== 'f') {
5160 /* This is handled in set_options() thus silent skip. */
5163 /* Unknown option or other error.
5164 * Error is printed by getopt, just return */
5169 if (ret
== -EINVAL
) {
5170 const char *opt_name
= "unknown";
5173 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5175 if (opt
== long_options
[i
].val
) {
5176 opt_name
= long_options
[i
].name
;
5181 WARN("Invalid argument provided for option \"%s\", using default value.",
5189 * config_entry_handler_cb used to handle options read from a config file.
5190 * See config_entry_handler_cb comment in common/config/session-config.h for the
5191 * return value conventions.
5193 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5197 if (!entry
|| !entry
->name
|| !entry
->value
) {
5202 /* Check if the option is to be ignored */
5203 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5204 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5209 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5212 /* Ignore if not fully matched. */
5213 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5218 * If the option takes no argument on the command line, we have to
5219 * check if the value is "true". We support non-zero numeric values,
5222 if (!long_options
[i
].has_arg
) {
5223 ret
= config_parse_value(entry
->value
);
5226 WARN("Invalid configuration value \"%s\" for option %s",
5227 entry
->value
, entry
->name
);
5229 /* False, skip boolean config option. */
5234 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5238 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5245 * daemon configuration loading and argument parsing
5247 static int set_options(int argc
, char **argv
)
5249 int ret
= 0, c
= 0, option_index
= 0;
5250 int orig_optopt
= optopt
, orig_optind
= optind
;
5252 const char *config_path
= NULL
;
5254 optstring
= utils_generate_optstring(long_options
,
5255 sizeof(long_options
) / sizeof(struct option
));
5261 /* Check for the --config option */
5262 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5263 &option_index
)) != -1) {
5267 } else if (c
!= 'f') {
5268 /* if not equal to --config option. */
5272 if (lttng_is_setuid_setgid()) {
5273 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5276 config_path
= utils_expand_path(optarg
);
5278 ERR("Failed to resolve path: %s", optarg
);
5283 ret
= config_get_section_entries(config_path
, config_section_name
,
5284 config_entry_handler
, NULL
);
5287 ERR("Invalid configuration option at line %i", ret
);
5293 /* Reset getopt's global state */
5294 optopt
= orig_optopt
;
5295 optind
= orig_optind
;
5299 * getopt_long() will not set option_index if it encounters a
5302 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5309 * Pass NULL as the long option name if popt left the index
5312 ret
= set_option(c
, optarg
,
5313 option_index
< 0 ? NULL
:
5314 long_options
[option_index
].name
);
5326 * Creates the two needed socket by the daemon.
5327 * apps_sock - The communication socket for all UST apps.
5328 * client_sock - The communication of the cli tool (lttng).
5330 static int init_daemon_socket(void)
5335 old_umask
= umask(0);
5337 /* Create client tool unix socket */
5338 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5339 if (client_sock
< 0) {
5340 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5345 /* Set the cloexec flag */
5346 ret
= utils_set_fd_cloexec(client_sock
);
5348 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5349 "Continuing but note that the consumer daemon will have a "
5350 "reference to this socket on exec()", client_sock
);
5353 /* File permission MUST be 660 */
5354 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5356 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5361 /* Create the application unix socket */
5362 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5363 if (apps_sock
< 0) {
5364 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5369 /* Set the cloexec flag */
5370 ret
= utils_set_fd_cloexec(apps_sock
);
5372 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5373 "Continuing but note that the consumer daemon will have a "
5374 "reference to this socket on exec()", apps_sock
);
5377 /* File permission MUST be 666 */
5378 ret
= chmod(config
.apps_unix_sock_path
.value
,
5379 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5381 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5386 DBG3("Session daemon client socket %d and application socket %d created",
5387 client_sock
, apps_sock
);
5395 * Create lockfile using the rundir and return its fd.
5397 static int create_lockfile(void)
5399 return utils_create_lock_file(config
.lock_file_path
.value
);
5403 * Check if the global socket is available, and if a daemon is answering at the
5404 * other side. If yes, error is returned.
5406 * Also attempts to create and hold the lock file.
5408 static int check_existing_daemon(void)
5412 /* Is there anybody out there ? */
5413 if (lttng_session_daemon_alive()) {
5418 lockfile_fd
= create_lockfile();
5419 if (lockfile_fd
< 0) {
5427 static void sessiond_cleanup_lock_file(void)
5432 * Cleanup lock file by deleting it and finaly closing it which will
5433 * release the file system lock.
5435 if (lockfile_fd
>= 0) {
5436 ret
= remove(config
.lock_file_path
.value
);
5438 PERROR("remove lock file");
5440 ret
= close(lockfile_fd
);
5442 PERROR("close lock file");
5448 * Set the tracing group gid onto the client socket.
5450 * Race window between mkdir and chown is OK because we are going from more
5451 * permissive (root.root) to less permissive (root.tracing).
5453 static int set_permissions(char *rundir
)
5458 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5460 /* Set lttng run dir */
5461 ret
= chown(rundir
, 0, gid
);
5463 ERR("Unable to set group on %s", rundir
);
5468 * Ensure all applications and tracing group can search the run
5469 * dir. Allow everyone to read the directory, since it does not
5470 * buy us anything to hide its content.
5472 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5474 ERR("Unable to set permissions on %s", rundir
);
5478 /* lttng client socket path */
5479 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5481 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5485 /* kconsumer error socket path */
5486 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5488 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5492 /* 64-bit ustconsumer error socket path */
5493 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5495 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5499 /* 32-bit ustconsumer compat32 error socket path */
5500 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5502 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5506 DBG("All permissions are set");
5512 * Create the lttng run directory needed for all global sockets and pipe.
5514 static int create_lttng_rundir(void)
5518 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5520 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5522 if (errno
!= EEXIST
) {
5523 ERR("Unable to create %s", config
.rundir
.value
);
5535 * Setup sockets and directory needed by the consumerds' communication with the
5538 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5543 switch (consumer_data
->type
) {
5544 case LTTNG_CONSUMER_KERNEL
:
5545 path
= config
.kconsumerd_path
.value
;
5547 case LTTNG_CONSUMER64_UST
:
5548 path
= config
.consumerd64_path
.value
;
5550 case LTTNG_CONSUMER32_UST
:
5551 path
= config
.consumerd32_path
.value
;
5554 ERR("Consumer type unknown");
5560 DBG2("Creating consumer directory: %s", path
);
5562 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5563 if (ret
< 0 && errno
!= EEXIST
) {
5565 ERR("Failed to create %s", path
);
5569 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5571 ERR("Unable to set group on %s", path
);
5577 /* Create the consumerd error unix socket */
5578 consumer_data
->err_sock
=
5579 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5580 if (consumer_data
->err_sock
< 0) {
5581 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5587 * Set the CLOEXEC flag. Return code is useless because either way, the
5590 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5592 PERROR("utils_set_fd_cloexec");
5593 /* continue anyway */
5596 /* File permission MUST be 660 */
5597 ret
= chmod(consumer_data
->err_unix_sock_path
,
5598 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5600 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5610 * Signal handler for the daemon
5612 * Simply stop all worker threads, leaving main() return gracefully after
5613 * joining all threads and calling cleanup().
5615 static void sighandler(int sig
)
5619 DBG("SIGINT caught");
5623 DBG("SIGTERM caught");
5627 CMM_STORE_SHARED(recv_child_signal
, 1);
5635 * Setup signal handler for :
5636 * SIGINT, SIGTERM, SIGPIPE
5638 static int set_signal_handler(void)
5641 struct sigaction sa
;
5644 if ((ret
= sigemptyset(&sigset
)) < 0) {
5645 PERROR("sigemptyset");
5649 sa
.sa_mask
= sigset
;
5652 sa
.sa_handler
= sighandler
;
5653 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5654 PERROR("sigaction");
5658 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5659 PERROR("sigaction");
5663 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5664 PERROR("sigaction");
5668 sa
.sa_handler
= SIG_IGN
;
5669 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5670 PERROR("sigaction");
5674 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5680 * Set open files limit to unlimited. This daemon can open a large number of
5681 * file descriptors in order to consume multiple kernel traces.
5683 static void set_ulimit(void)
5688 /* The kernel does not allow an infinite limit for open files */
5689 lim
.rlim_cur
= 65535;
5690 lim
.rlim_max
= 65535;
5692 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5694 PERROR("failed to set open files limit");
5698 static int write_pidfile(void)
5700 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5703 static int set_clock_plugin_env(void)
5706 char *env_value
= NULL
;
5708 if (!config
.lttng_ust_clock_plugin
.value
) {
5712 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5713 config
.lttng_ust_clock_plugin
.value
);
5719 ret
= putenv(env_value
);
5722 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5726 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5727 config
.lttng_ust_clock_plugin
.value
);
5733 struct rotation_thread_timer_queue
*create_rotate_timer_queue(void)
5735 struct rotation_thread_timer_queue
*queue
= NULL
;
5737 queue
= zmalloc(sizeof(struct rotation_thread_timer_queue
));
5739 PERROR("Failed to allocate timer rotate queue");
5743 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
5744 CDS_INIT_LIST_HEAD(&queue
->list
);
5745 pthread_mutex_init(&queue
->lock
, NULL
);
5752 void destroy_rotate_timer_queue(struct rotation_thread_timer_queue
*queue
)
5754 struct sessiond_rotation_timer
*node
, *tmp_node
;
5760 lttng_pipe_destroy(queue
->event_pipe
);
5762 pthread_mutex_lock(&queue
->lock
);
5763 /* Empty wait queue. */
5764 cds_list_for_each_entry_safe(node
, tmp_node
, &queue
->list
, head
) {
5765 cds_list_del(&node
->head
);
5768 pthread_mutex_unlock(&queue
->lock
);
5770 pthread_mutex_destroy(&queue
->lock
);
5777 int main(int argc
, char **argv
)
5779 int ret
= 0, retval
= 0;
5781 const char *env_app_timeout
;
5782 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5783 *ust64_channel_monitor_pipe
= NULL
,
5784 *kernel_channel_monitor_pipe
= NULL
;
5785 bool notification_thread_launched
= false;
5786 bool rotation_thread_launched
= false;
5787 bool timer_thread_launched
= false;
5788 struct lttng_pipe
*ust32_channel_rotate_pipe
= NULL
,
5789 *ust64_channel_rotate_pipe
= NULL
,
5790 *kernel_channel_rotate_pipe
= NULL
;
5791 struct timer_thread_parameters timer_thread_ctx
;
5792 /* Queue of rotation jobs populated by the sessiond-timer. */
5793 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5794 sem_t notification_thread_ready
;
5796 init_kernel_workarounds();
5798 rcu_register_thread();
5800 if (set_signal_handler()) {
5802 goto exit_set_signal_handler
;
5805 if (sessiond_timer_signal_init()) {
5807 goto exit_set_signal_handler
;
5810 page_size
= sysconf(_SC_PAGESIZE
);
5811 if (page_size
< 0) {
5812 PERROR("sysconf _SC_PAGESIZE");
5813 page_size
= LONG_MAX
;
5814 WARN("Fallback page size to %ld", page_size
);
5817 ret
= sessiond_config_init(&config
);
5820 goto exit_set_signal_handler
;
5824 * Init config from environment variables.
5825 * Command line option override env configuration per-doc. Do env first.
5827 sessiond_config_apply_env_config(&config
);
5830 * Parse arguments and load the daemon configuration file.
5832 * We have an exit_options exit path to free memory reserved by
5833 * set_options. This is needed because the rest of sessiond_cleanup()
5834 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5835 * depends on set_options.
5838 if (set_options(argc
, argv
)) {
5844 * Resolve all paths received as arguments, configuration option, or
5845 * through environment variable as absolute paths. This is necessary
5846 * since daemonizing causes the sessiond's current working directory
5849 ret
= sessiond_config_resolve_paths(&config
);
5855 lttng_opt_verbose
= config
.verbose
;
5856 lttng_opt_quiet
= config
.quiet
;
5857 kconsumer_data
.err_unix_sock_path
=
5858 config
.kconsumerd_err_unix_sock_path
.value
;
5859 kconsumer_data
.cmd_unix_sock_path
=
5860 config
.kconsumerd_cmd_unix_sock_path
.value
;
5861 ustconsumer32_data
.err_unix_sock_path
=
5862 config
.consumerd32_err_unix_sock_path
.value
;
5863 ustconsumer32_data
.cmd_unix_sock_path
=
5864 config
.consumerd32_cmd_unix_sock_path
.value
;
5865 ustconsumer64_data
.err_unix_sock_path
=
5866 config
.consumerd64_err_unix_sock_path
.value
;
5867 ustconsumer64_data
.cmd_unix_sock_path
=
5868 config
.consumerd64_cmd_unix_sock_path
.value
;
5869 set_clock_plugin_env();
5871 sessiond_config_log(&config
);
5873 if (create_lttng_rundir()) {
5878 /* Abort launch if a session daemon is already running. */
5879 if (check_existing_daemon()) {
5880 ERR("A session daemon is already running.");
5886 if (config
.daemonize
|| config
.background
) {
5889 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5890 !config
.background
);
5897 * We are in the child. Make sure all other file descriptors are
5898 * closed, in case we are called with more opened file
5899 * descriptors than the standard ones and the lock file.
5901 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5902 if (i
== lockfile_fd
) {
5909 if (run_as_create_worker(argv
[0]) < 0) {
5910 goto exit_create_run_as_worker_cleanup
;
5914 * Starting from here, we can create threads. This needs to be after
5915 * lttng_daemonize due to RCU.
5919 * Initialize the health check subsystem. This call should set the
5920 * appropriate time values.
5922 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5923 if (!health_sessiond
) {
5924 PERROR("health_app_create error");
5926 goto exit_health_sessiond_cleanup
;
5929 /* Create thread to clean up RCU hash tables */
5930 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5932 goto exit_ht_cleanup
;
5935 /* Create thread quit pipe */
5936 if (init_thread_quit_pipe()) {
5938 goto exit_init_data
;
5941 /* Check if daemon is UID = 0 */
5942 is_root
= !getuid();
5944 /* Create global run dir with root access */
5946 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5947 if (!kernel_channel_monitor_pipe
) {
5948 ERR("Failed to create kernel consumer channel monitor pipe");
5950 goto exit_init_data
;
5952 kconsumer_data
.channel_monitor_pipe
=
5953 lttng_pipe_release_writefd(
5954 kernel_channel_monitor_pipe
);
5955 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5957 goto exit_init_data
;
5959 kernel_channel_rotate_pipe
= lttng_pipe_open(0);
5960 if (!kernel_channel_rotate_pipe
) {
5961 ERR("Failed to create kernel consumer channel rotate pipe");
5963 goto exit_init_data
;
5965 kconsumer_data
.channel_rotate_pipe
=
5966 lttng_pipe_release_writefd(
5967 kernel_channel_rotate_pipe
);
5968 if (kconsumer_data
.channel_rotate_pipe
< 0) {
5970 goto exit_init_data
;
5974 /* Set consumer initial state */
5975 kernel_consumerd_state
= CONSUMER_STOPPED
;
5976 ust_consumerd_state
= CONSUMER_STOPPED
;
5978 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5979 if (!ust32_channel_monitor_pipe
) {
5980 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5982 goto exit_init_data
;
5984 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5985 ust32_channel_monitor_pipe
);
5986 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5988 goto exit_init_data
;
5990 ust32_channel_rotate_pipe
= lttng_pipe_open(0);
5991 if (!ust32_channel_rotate_pipe
) {
5992 ERR("Failed to create 32-bit user space consumer channel rotate pipe");
5994 goto exit_init_data
;
5996 ustconsumer32_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
5997 ust32_channel_rotate_pipe
);
5998 if (ustconsumer32_data
.channel_rotate_pipe
< 0) {
6000 goto exit_init_data
;
6004 * The rotation_timer_queue structure is shared between the sessiond timer
6005 * thread and the rotation thread. The main() keeps the ownership and
6006 * destroys it when both threads have quit.
6008 rotation_timer_queue
= create_rotate_timer_queue();
6009 if (!rotation_timer_queue
) {
6011 goto exit_init_data
;
6013 timer_thread_ctx
.rotation_timer_queue
= rotation_timer_queue
;
6015 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
6016 if (!ust64_channel_monitor_pipe
) {
6017 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
6019 goto exit_init_data
;
6021 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6022 ust64_channel_monitor_pipe
);
6023 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
6025 goto exit_init_data
;
6027 ust64_channel_rotate_pipe
= lttng_pipe_open(0);
6028 if (!ust64_channel_rotate_pipe
) {
6029 ERR("Failed to create 64-bit user space consumer channel rotate pipe");
6031 goto exit_init_data
;
6033 ustconsumer64_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6034 ust64_channel_rotate_pipe
);
6035 if (ustconsumer64_data
.channel_rotate_pipe
< 0) {
6037 goto exit_init_data
;
6041 * Init UST app hash table. Alloc hash table before this point since
6042 * cleanup() can get called after that point.
6044 if (ust_app_ht_alloc()) {
6045 ERR("Failed to allocate UST app hash table");
6047 goto exit_init_data
;
6051 * Initialize agent app hash table. We allocate the hash table here
6052 * since cleanup() can get called after this point.
6054 if (agent_app_ht_alloc()) {
6055 ERR("Failed to allocate Agent app hash table");
6057 goto exit_init_data
;
6061 * These actions must be executed as root. We do that *after* setting up
6062 * the sockets path because we MUST make the check for another daemon using
6063 * those paths *before* trying to set the kernel consumer sockets and init
6067 if (set_consumer_sockets(&kconsumer_data
)) {
6069 goto exit_init_data
;
6072 /* Setup kernel tracer */
6073 if (!config
.no_kernel
) {
6074 init_kernel_tracer();
6075 if (kernel_tracer_fd
>= 0) {
6076 ret
= syscall_init_table();
6078 ERR("Unable to populate syscall table. "
6079 "Syscall tracing won't work "
6080 "for this session daemon.");
6085 /* Set ulimit for open files */
6088 /* init lttng_fd tracking must be done after set_ulimit. */
6091 if (set_consumer_sockets(&ustconsumer64_data
)) {
6093 goto exit_init_data
;
6096 if (set_consumer_sockets(&ustconsumer32_data
)) {
6098 goto exit_init_data
;
6101 /* Setup the needed unix socket */
6102 if (init_daemon_socket()) {
6104 goto exit_init_data
;
6107 /* Set credentials to socket */
6108 if (is_root
&& set_permissions(config
.rundir
.value
)) {
6110 goto exit_init_data
;
6113 /* Get parent pid if -S, --sig-parent is specified. */
6114 if (config
.sig_parent
) {
6118 /* Setup the kernel pipe for waking up the kernel thread */
6119 if (is_root
&& !config
.no_kernel
) {
6120 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6122 goto exit_init_data
;
6126 /* Setup the thread apps communication pipe. */
6127 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6129 goto exit_init_data
;
6132 /* Setup the thread apps notify communication pipe. */
6133 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6135 goto exit_init_data
;
6138 /* Initialize global buffer per UID and PID registry. */
6139 buffer_reg_init_uid_registry();
6140 buffer_reg_init_pid_registry();
6142 /* Init UST command queue. */
6143 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6146 * Get session list pointer. This pointer MUST NOT be free'd. This list
6147 * is statically declared in session.c
6149 session_list_ptr
= session_get_list();
6153 /* Check for the application socket timeout env variable. */
6154 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6155 if (env_app_timeout
) {
6156 config
.app_socket_timeout
= atoi(env_app_timeout
);
6158 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6161 ret
= write_pidfile();
6163 ERR("Error in write_pidfile");
6165 goto exit_init_data
;
6168 /* Initialize communication library */
6170 /* Initialize TCP timeout values */
6171 lttcomm_inet_init();
6173 if (load_session_init_data(&load_info
) < 0) {
6175 goto exit_init_data
;
6177 load_info
->path
= config
.load_session_path
.value
;
6179 /* Create health-check thread. */
6180 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6181 thread_manage_health
, (void *) NULL
);
6184 PERROR("pthread_create health");
6190 * The rotation thread needs the notification thread to be ready before
6191 * creating the rotate_notification_channel, so we use this semaphore as
6192 * a rendez-vous point.
6194 sem_init(¬ification_thread_ready
, 0, 0);
6196 /* notification_thread_data acquires the pipes' read side. */
6197 notification_thread_handle
= notification_thread_handle_create(
6198 ust32_channel_monitor_pipe
,
6199 ust64_channel_monitor_pipe
,
6200 kernel_channel_monitor_pipe
,
6201 ¬ification_thread_ready
);
6202 if (!notification_thread_handle
) {
6204 ERR("Failed to create notification thread shared data");
6206 goto exit_notification
;
6209 /* Create notification thread. */
6210 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6211 thread_notification
, notification_thread_handle
);
6214 PERROR("pthread_create notification");
6217 goto exit_notification
;
6219 notification_thread_launched
= true;
6221 /* Create timer thread. */
6222 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
6223 sessiond_timer_thread
, &timer_thread_ctx
);
6226 PERROR("pthread_create timer");
6229 goto exit_notification
;
6231 timer_thread_launched
= true;
6233 /* rotation_thread_data acquires the pipes' read side. */
6234 rotation_thread_handle
= rotation_thread_handle_create(
6235 ust32_channel_rotate_pipe
,
6236 ust64_channel_rotate_pipe
,
6237 kernel_channel_rotate_pipe
,
6238 thread_quit_pipe
[0],
6239 rotation_timer_queue
,
6240 notification_thread_handle
,
6241 ¬ification_thread_ready
);
6242 if (!rotation_thread_handle
) {
6244 ERR("Failed to create rotation thread shared data");
6249 /* Create rotation thread. */
6250 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6251 thread_rotation
, rotation_thread_handle
);
6254 PERROR("pthread_create rotation");
6259 rotation_thread_launched
= true;
6261 /* Create thread to manage the client socket */
6262 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6263 thread_manage_clients
, (void *) NULL
);
6266 PERROR("pthread_create clients");
6272 /* Create thread to dispatch registration */
6273 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6274 thread_dispatch_ust_registration
, (void *) NULL
);
6277 PERROR("pthread_create dispatch");
6283 /* Create thread to manage application registration. */
6284 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6285 thread_registration_apps
, (void *) NULL
);
6288 PERROR("pthread_create registration");
6294 /* Create thread to manage application socket */
6295 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6296 thread_manage_apps
, (void *) NULL
);
6299 PERROR("pthread_create apps");
6305 /* Create thread to manage application notify socket */
6306 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6307 ust_thread_manage_notify
, (void *) NULL
);
6310 PERROR("pthread_create notify");
6313 goto exit_apps_notify
;
6316 /* Create agent registration thread. */
6317 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6318 agent_thread_manage_registration
, (void *) NULL
);
6321 PERROR("pthread_create agent");
6324 goto exit_agent_reg
;
6327 /* Don't start this thread if kernel tracing is not requested nor root */
6328 if (is_root
&& !config
.no_kernel
) {
6329 /* Create kernel thread to manage kernel event */
6330 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6331 thread_manage_kernel
, (void *) NULL
);
6334 PERROR("pthread_create kernel");
6341 /* Create session loading thread. */
6342 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6343 thread_load_session
, load_info
);
6346 PERROR("pthread_create load_session_thread");
6349 goto exit_load_session
;
6353 * This is where we start awaiting program completion (e.g. through
6354 * signal that asks threads to teardown).
6357 ret
= pthread_join(load_session_thread
, &status
);
6360 PERROR("pthread_join load_session_thread");
6365 if (is_root
&& !config
.no_kernel
) {
6366 ret
= pthread_join(kernel_thread
, &status
);
6369 PERROR("pthread_join");
6375 ret
= pthread_join(agent_reg_thread
, &status
);
6378 PERROR("pthread_join agent");
6383 ret
= pthread_join(apps_notify_thread
, &status
);
6386 PERROR("pthread_join apps notify");
6391 ret
= pthread_join(apps_thread
, &status
);
6394 PERROR("pthread_join apps");
6399 ret
= pthread_join(reg_apps_thread
, &status
);
6402 PERROR("pthread_join");
6408 * Join dispatch thread after joining reg_apps_thread to ensure
6409 * we don't leak applications in the queue.
6411 ret
= pthread_join(dispatch_thread
, &status
);
6414 PERROR("pthread_join");
6419 ret
= pthread_join(client_thread
, &status
);
6422 PERROR("pthread_join");
6429 sem_destroy(¬ification_thread_ready
);
6430 ret
= pthread_join(health_thread
, &status
);
6433 PERROR("pthread_join health thread");
6440 * Wait for all pending call_rcu work to complete before tearing
6441 * down data structures. call_rcu worker may be trying to
6442 * perform lookups in those structures.
6446 * sessiond_cleanup() is called when no other thread is running, except
6447 * the ht_cleanup thread, which is needed to destroy the hash tables.
6449 rcu_thread_online();
6453 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6454 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6455 * the queue is empty before shutting down the clean-up thread.
6460 * The teardown of the notification system is performed after the
6461 * session daemon's teardown in order to allow it to be notified
6462 * of the active session and channels at the moment of the teardown.
6464 if (notification_thread_handle
) {
6465 if (notification_thread_launched
) {
6466 notification_thread_command_quit(
6467 notification_thread_handle
);
6468 ret
= pthread_join(notification_thread
, &status
);
6471 PERROR("pthread_join notification thread");
6475 notification_thread_handle_destroy(notification_thread_handle
);
6478 if (rotation_thread_handle
) {
6479 if (rotation_thread_launched
) {
6480 ret
= pthread_join(rotation_thread
, &status
);
6483 PERROR("pthread_join rotation thread");
6487 rotation_thread_handle_destroy(rotation_thread_handle
);
6490 if (timer_thread_launched
) {
6491 kill(getpid(), LTTNG_SESSIOND_SIG_EXIT
);
6492 ret
= pthread_join(timer_thread
, &status
);
6495 PERROR("pthread_join timer thread");
6501 * After the rotation and timer thread have quit, we can safely destroy
6502 * the rotation_timer_queue.
6504 destroy_rotate_timer_queue(rotation_timer_queue
);
6506 rcu_thread_offline();
6507 rcu_unregister_thread();
6509 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6513 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6514 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6515 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6516 lttng_pipe_destroy(ust32_channel_rotate_pipe
);
6517 lttng_pipe_destroy(ust64_channel_rotate_pipe
);
6518 lttng_pipe_destroy(kernel_channel_rotate_pipe
);
6521 health_app_destroy(health_sessiond
);
6522 exit_health_sessiond_cleanup
:
6523 exit_create_run_as_worker_cleanup
:
6526 sessiond_cleanup_lock_file();
6527 sessiond_cleanup_options();
6529 exit_set_signal_handler
: