2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
52 #include <common/dynamic-buffer.h>
54 #include "lttng-sessiond.h"
55 #include "buffer-registry.h"
62 #include "kernel-consumer.h"
66 #include "ust-consumer.h"
69 #include "health-sessiond.h"
70 #include "testpoint.h"
71 #include "ust-thread.h"
72 #include "agent-thread.h"
74 #include "load-session-thread.h"
75 #include "notification-thread.h"
76 #include "notification-thread-commands.h"
77 #include "rotation-thread.h"
78 #include "lttng-syscall.h"
80 #include "ht-cleanup.h"
81 #include "sessiond-config.h"
82 #include "sessiond-timer.h"
84 static const char *help_msg
=
85 #ifdef LTTNG_EMBED_HELP
86 #include <lttng-sessiond.8.h>
93 static pid_t ppid
; /* Parent PID for --sig-parent option */
94 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
95 static int lockfile_fd
= -1;
97 /* Set to 1 when a SIGUSR1 signal is received. */
98 static int recv_child_signal
;
100 static struct lttng_kernel_tracer_version kernel_tracer_version
;
101 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
104 * Consumer daemon specific control data. Every value not initialized here is
105 * set to 0 by the static definition.
107 static struct consumer_data kconsumer_data
= {
108 .type
= LTTNG_CONSUMER_KERNEL
,
111 .channel_monitor_pipe
= -1,
112 .channel_rotate_pipe
= -1,
113 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
114 .lock
= PTHREAD_MUTEX_INITIALIZER
,
115 .cond
= PTHREAD_COND_INITIALIZER
,
116 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
118 static struct consumer_data ustconsumer64_data
= {
119 .type
= LTTNG_CONSUMER64_UST
,
122 .channel_monitor_pipe
= -1,
123 .channel_rotate_pipe
= -1,
124 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
125 .lock
= PTHREAD_MUTEX_INITIALIZER
,
126 .cond
= PTHREAD_COND_INITIALIZER
,
127 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
129 static struct consumer_data ustconsumer32_data
= {
130 .type
= LTTNG_CONSUMER32_UST
,
133 .channel_monitor_pipe
= -1,
134 .channel_rotate_pipe
= -1,
135 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
136 .lock
= PTHREAD_MUTEX_INITIALIZER
,
137 .cond
= PTHREAD_COND_INITIALIZER
,
138 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
141 /* Command line options */
142 static const struct option long_options
[] = {
143 { "client-sock", required_argument
, 0, 'c' },
144 { "apps-sock", required_argument
, 0, 'a' },
145 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
146 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
147 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
148 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
149 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
150 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
151 { "consumerd32-path", required_argument
, 0, '\0' },
152 { "consumerd32-libdir", required_argument
, 0, '\0' },
153 { "consumerd64-path", required_argument
, 0, '\0' },
154 { "consumerd64-libdir", required_argument
, 0, '\0' },
155 { "daemonize", no_argument
, 0, 'd' },
156 { "background", no_argument
, 0, 'b' },
157 { "sig-parent", no_argument
, 0, 'S' },
158 { "help", no_argument
, 0, 'h' },
159 { "group", required_argument
, 0, 'g' },
160 { "version", no_argument
, 0, 'V' },
161 { "quiet", no_argument
, 0, 'q' },
162 { "verbose", no_argument
, 0, 'v' },
163 { "verbose-consumer", no_argument
, 0, '\0' },
164 { "no-kernel", no_argument
, 0, '\0' },
165 { "pidfile", required_argument
, 0, 'p' },
166 { "agent-tcp-port", required_argument
, 0, '\0' },
167 { "config", required_argument
, 0, 'f' },
168 { "load", required_argument
, 0, 'l' },
169 { "kmod-probes", required_argument
, 0, '\0' },
170 { "extra-kmod-probes", required_argument
, 0, '\0' },
174 struct sessiond_config config
;
176 /* Command line options to ignore from configuration file */
177 static const char *config_ignore_options
[] = { "help", "version", "config" };
179 /* Shared between threads */
180 static int dispatch_thread_exit
;
182 /* Sockets and FDs */
183 static int client_sock
= -1;
184 static int apps_sock
= -1;
185 int kernel_tracer_fd
= -1;
186 static int kernel_poll_pipe
[2] = { -1, -1 };
189 * Quit pipe for all threads. This permits a single cancellation point
190 * for all threads when receiving an event on the pipe.
192 static int thread_quit_pipe
[2] = { -1, -1 };
195 * This pipe is used to inform the thread managing application communication
196 * that a command is queued and ready to be processed.
198 static int apps_cmd_pipe
[2] = { -1, -1 };
200 int apps_cmd_notify_pipe
[2] = { -1, -1 };
202 /* Pthread, Mutexes and Semaphores */
203 static pthread_t apps_thread
;
204 static pthread_t apps_notify_thread
;
205 static pthread_t reg_apps_thread
;
206 static pthread_t client_thread
;
207 static pthread_t kernel_thread
;
208 static pthread_t dispatch_thread
;
209 static pthread_t health_thread
;
210 static pthread_t ht_cleanup_thread
;
211 static pthread_t agent_reg_thread
;
212 static pthread_t load_session_thread
;
213 static pthread_t notification_thread
;
214 static pthread_t rotation_thread
;
215 static pthread_t timer_thread
;
218 * UST registration command queue. This queue is tied with a futex and uses a N
219 * wakers / 1 waiter implemented and detailed in futex.c/.h
221 * The thread_registration_apps and thread_dispatch_ust_registration uses this
222 * queue along with the wait/wake scheme. The thread_manage_apps receives down
223 * the line new application socket and monitors it for any I/O error or clean
224 * close that triggers an unregistration of the application.
226 static struct ust_cmd_queue ust_cmd_queue
;
229 * Pointer initialized before thread creation.
231 * This points to the tracing session list containing the session count and a
232 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
233 * MUST NOT be taken if you call a public function in session.c.
235 * The lock is nested inside the structure: session_list_ptr->lock. Please use
236 * session_lock_list and session_unlock_list for lock acquisition.
238 static struct ltt_session_list
*session_list_ptr
;
240 int ust_consumerd64_fd
= -1;
241 int ust_consumerd32_fd
= -1;
243 static const char *module_proc_lttng
= "/proc/lttng";
246 * Consumer daemon state which is changed when spawning it, killing it or in
247 * case of a fatal error.
249 enum consumerd_state
{
250 CONSUMER_STARTED
= 1,
251 CONSUMER_STOPPED
= 2,
256 * This consumer daemon state is used to validate if a client command will be
257 * able to reach the consumer. If not, the client is informed. For instance,
258 * doing a "lttng start" when the consumer state is set to ERROR will return an
259 * error to the client.
261 * The following example shows a possible race condition of this scheme:
263 * consumer thread error happens
265 * client cmd checks state -> still OK
266 * consumer thread exit, sets error
267 * client cmd try to talk to consumer
270 * However, since the consumer is a different daemon, we have no way of making
271 * sure the command will reach it safely even with this state flag. This is why
272 * we consider that up to the state validation during command processing, the
273 * command is safe. After that, we can not guarantee the correctness of the
274 * client request vis-a-vis the consumer.
276 static enum consumerd_state ust_consumerd_state
;
277 static enum consumerd_state kernel_consumerd_state
;
279 /* Set in main() with the current page size. */
282 /* Application health monitoring */
283 struct health_app
*health_sessiond
;
285 /* Am I root or not. */
286 int is_root
; /* Set to 1 if the daemon is running as root */
288 const char * const config_section_name
= "sessiond";
290 /* Load session thread information to operate. */
291 struct load_session_thread_data
*load_info
;
293 /* Notification thread handle. */
294 struct notification_thread_handle
*notification_thread_handle
;
296 /* Rotation thread handle. */
297 struct rotation_thread_handle
*rotation_thread_handle
;
299 /* Global hash tables */
300 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
303 * The initialization of the session daemon is done in multiple phases.
305 * While all threads are launched near-simultaneously, only some of them
306 * are needed to ensure the session daemon can start to respond to client
309 * There are two important guarantees that we wish to offer with respect
310 * to the initialisation of the session daemon:
311 * - When the daemonize/background launcher process exits, the sessiond
312 * is fully able to respond to client requests,
313 * - Auto-loaded sessions are visible to clients.
315 * In order to achieve this, a number of support threads have to be launched
316 * to allow the "client" thread to function properly. Moreover, since the
317 * "load session" thread needs the client thread, we must provide a way
318 * for the "load session" thread to know that the "client" thread is up
321 * Hence, the support threads decrement the lttng_sessiond_ready counter
322 * while the "client" threads waits for it to reach 0. Once the "client" thread
323 * unblocks, it posts the message_thread_ready semaphore which allows the
324 * "load session" thread to progress.
326 * This implies that the "load session" thread is the last to be initialized
327 * and will explicitly call sessiond_signal_parents(), which signals the parents
328 * that the session daemon is fully initialized.
330 * The four (4) support threads are:
332 * - notification_thread
336 #define NR_LTTNG_SESSIOND_SUPPORT_THREADS 4
337 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_SUPPORT_THREADS
;
339 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
341 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
344 /* Notify parents that we are ready for cmd and health check */
346 void sessiond_signal_parents(void)
349 * Notify parent pid that we are ready to accept command
350 * for client side. This ppid is the one from the
351 * external process that spawned us.
353 if (config
.sig_parent
) {
358 * Notify the parent of the fork() process that we are
361 if (config
.daemonize
|| config
.background
) {
362 kill(child_ppid
, SIGUSR1
);
367 void sessiond_notify_ready(void)
370 * This memory barrier is paired with the one performed by
371 * the client thread after it has seen that 'lttng_sessiond_ready' is 0.
373 * The purpose of these memory barriers is to ensure that all
374 * initialization operations of the various threads that call this
375 * function to signal that they are ready are commited/published
376 * before the client thread can see the 'lttng_sessiond_ready' counter
379 * Note that this could be a 'write' memory barrier, but a full barrier
380 * is used in case the code using this utility changes. The performance
381 * implications of this choice are minimal since this is a slow path.
384 uatomic_sub(<tng_sessiond_ready
, 1);
388 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
395 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
401 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
413 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
415 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
417 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
421 * Init thread quit pipe.
423 * Return -1 on error or 0 if all pipes are created.
425 static int __init_thread_quit_pipe(int *a_pipe
)
431 PERROR("thread quit pipe");
435 for (i
= 0; i
< 2; i
++) {
436 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
447 static int init_thread_quit_pipe(void)
449 return __init_thread_quit_pipe(thread_quit_pipe
);
453 * Stop all threads by closing the thread quit pipe.
455 static void stop_threads(void)
459 /* Stopping all threads */
460 DBG("Terminating all threads");
461 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
463 ERR("write error on thread quit pipe");
466 /* Dispatch thread */
467 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
468 futex_nto1_wake(&ust_cmd_queue
.futex
);
472 * Close every consumer sockets.
474 static void close_consumer_sockets(void)
478 if (kconsumer_data
.err_sock
>= 0) {
479 ret
= close(kconsumer_data
.err_sock
);
481 PERROR("kernel consumer err_sock close");
484 if (ustconsumer32_data
.err_sock
>= 0) {
485 ret
= close(ustconsumer32_data
.err_sock
);
487 PERROR("UST consumerd32 err_sock close");
490 if (ustconsumer64_data
.err_sock
>= 0) {
491 ret
= close(ustconsumer64_data
.err_sock
);
493 PERROR("UST consumerd64 err_sock close");
496 if (kconsumer_data
.cmd_sock
>= 0) {
497 ret
= close(kconsumer_data
.cmd_sock
);
499 PERROR("kernel consumer cmd_sock close");
502 if (ustconsumer32_data
.cmd_sock
>= 0) {
503 ret
= close(ustconsumer32_data
.cmd_sock
);
505 PERROR("UST consumerd32 cmd_sock close");
508 if (ustconsumer64_data
.cmd_sock
>= 0) {
509 ret
= close(ustconsumer64_data
.cmd_sock
);
511 PERROR("UST consumerd64 cmd_sock close");
514 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
515 ret
= close(kconsumer_data
.channel_monitor_pipe
);
517 PERROR("kernel consumer channel monitor pipe close");
520 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
521 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
523 PERROR("UST consumerd32 channel monitor pipe close");
526 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
527 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
529 PERROR("UST consumerd64 channel monitor pipe close");
532 if (kconsumer_data
.channel_rotate_pipe
>= 0) {
533 ret
= close(kconsumer_data
.channel_rotate_pipe
);
535 PERROR("kernel consumer channel rotate pipe close");
538 if (ustconsumer32_data
.channel_rotate_pipe
>= 0) {
539 ret
= close(ustconsumer32_data
.channel_rotate_pipe
);
541 PERROR("UST consumerd32 channel rotate pipe close");
544 if (ustconsumer64_data
.channel_rotate_pipe
>= 0) {
545 ret
= close(ustconsumer64_data
.channel_rotate_pipe
);
547 PERROR("UST consumerd64 channel rotate pipe close");
553 * Wait on consumer process termination.
555 * Need to be called with the consumer data lock held or from a context
556 * ensuring no concurrent access to data (e.g: cleanup).
558 static void wait_consumer(struct consumer_data
*consumer_data
)
563 if (consumer_data
->pid
<= 0) {
567 DBG("Waiting for complete teardown of consumerd (PID: %d)",
569 ret
= waitpid(consumer_data
->pid
, &status
, 0);
571 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
572 } else if (!WIFEXITED(status
)) {
573 ERR("consumerd termination with error: %d",
576 consumer_data
->pid
= 0;
580 * Cleanup the session daemon's data structures.
582 static void sessiond_cleanup(void)
585 struct ltt_session
*sess
, *stmp
;
587 DBG("Cleanup sessiond");
590 * Close the thread quit pipe. It has already done its job,
591 * since we are now called.
593 utils_close_pipe(thread_quit_pipe
);
596 * If config.pid_file_path.value is undefined, the default file will be
597 * wiped when removing the rundir.
599 if (config
.pid_file_path
.value
) {
600 ret
= remove(config
.pid_file_path
.value
);
602 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
606 DBG("Removing sessiond and consumerd content of directory %s",
607 config
.rundir
.value
);
610 DBG("Removing %s", config
.pid_file_path
.value
);
611 (void) unlink(config
.pid_file_path
.value
);
613 DBG("Removing %s", config
.agent_port_file_path
.value
);
614 (void) unlink(config
.agent_port_file_path
.value
);
617 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
618 (void) unlink(kconsumer_data
.err_unix_sock_path
);
620 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
621 (void) rmdir(config
.kconsumerd_path
.value
);
623 /* ust consumerd 32 */
624 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
625 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
627 DBG("Removing directory %s", config
.consumerd32_path
.value
);
628 (void) rmdir(config
.consumerd32_path
.value
);
630 /* ust consumerd 64 */
631 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
632 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
634 DBG("Removing directory %s", config
.consumerd64_path
.value
);
635 (void) rmdir(config
.consumerd64_path
.value
);
637 DBG("Cleaning up all sessions");
639 /* Destroy session list mutex */
640 if (session_list_ptr
!= NULL
) {
641 pthread_mutex_destroy(&session_list_ptr
->lock
);
643 /* Cleanup ALL session */
644 cds_list_for_each_entry_safe(sess
, stmp
,
645 &session_list_ptr
->head
, list
) {
646 cmd_destroy_session(sess
, kernel_poll_pipe
[1],
647 notification_thread_handle
);
651 wait_consumer(&kconsumer_data
);
652 wait_consumer(&ustconsumer64_data
);
653 wait_consumer(&ustconsumer32_data
);
655 DBG("Cleaning up all agent apps");
656 agent_app_ht_clean();
658 DBG("Closing all UST sockets");
659 ust_app_clean_list();
660 buffer_reg_destroy_registries();
662 if (is_root
&& !config
.no_kernel
) {
663 DBG2("Closing kernel fd");
664 if (kernel_tracer_fd
>= 0) {
665 ret
= close(kernel_tracer_fd
);
670 DBG("Unloading kernel modules");
671 modprobe_remove_lttng_all();
675 close_consumer_sockets();
678 load_session_destroy_data(load_info
);
683 * We do NOT rmdir rundir because there are other processes
684 * using it, for instance lttng-relayd, which can start in
685 * parallel with this teardown.
690 * Cleanup the daemon's option data structures.
692 static void sessiond_cleanup_options(void)
694 DBG("Cleaning up options");
696 sessiond_config_fini(&config
);
698 run_as_destroy_worker();
702 * Send data on a unix socket using the liblttsessiondcomm API.
704 * Return lttcomm error code.
706 static int send_unix_sock(int sock
, void *buf
, size_t len
)
708 /* Check valid length */
713 return lttcomm_send_unix_sock(sock
, buf
, len
);
717 * Free memory of a command context structure.
719 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
721 DBG("Clean command context structure");
723 if ((*cmd_ctx
)->llm
) {
724 free((*cmd_ctx
)->llm
);
726 if ((*cmd_ctx
)->lsm
) {
727 free((*cmd_ctx
)->lsm
);
735 * Notify UST applications using the shm mmap futex.
737 static int notify_ust_apps(int active
)
741 DBG("Notifying applications of session daemon state: %d", active
);
743 /* See shm.c for this call implying mmap, shm and futex calls */
744 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
745 if (wait_shm_mmap
== NULL
) {
749 /* Wake waiting process */
750 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
752 /* Apps notified successfully */
760 * Setup the outgoing data buffer for the response (llm) by allocating the
761 * right amount of memory and copying the original information from the lsm
764 * Return 0 on success, negative value on error.
766 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
767 const void *payload_buf
, size_t payload_len
,
768 const void *cmd_header_buf
, size_t cmd_header_len
)
771 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
772 const size_t cmd_header_offset
= header_len
;
773 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
774 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
776 cmd_ctx
->llm
= zmalloc(total_msg_size
);
778 if (cmd_ctx
->llm
== NULL
) {
784 /* Copy common data */
785 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
786 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
787 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
788 cmd_ctx
->llm
->data_size
= payload_len
;
789 cmd_ctx
->lttng_msg_size
= total_msg_size
;
791 /* Copy command header */
792 if (cmd_header_len
) {
793 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
799 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
808 * Version of setup_lttng_msg() without command header.
810 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
811 void *payload_buf
, size_t payload_len
)
813 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
816 * Update the kernel poll set of all channel fd available over all tracing
817 * session. Add the wakeup pipe at the end of the set.
819 static int update_kernel_poll(struct lttng_poll_event
*events
)
822 struct ltt_session
*session
;
823 struct ltt_kernel_channel
*channel
;
825 DBG("Updating kernel poll set");
828 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
829 session_lock(session
);
830 if (session
->kernel_session
== NULL
) {
831 session_unlock(session
);
835 cds_list_for_each_entry(channel
,
836 &session
->kernel_session
->channel_list
.head
, list
) {
837 /* Add channel fd to the kernel poll set */
838 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
840 session_unlock(session
);
843 DBG("Channel fd %d added to kernel set", channel
->fd
);
845 session_unlock(session
);
847 session_unlock_list();
852 session_unlock_list();
857 * Find the channel fd from 'fd' over all tracing session. When found, check
858 * for new channel stream and send those stream fds to the kernel consumer.
860 * Useful for CPU hotplug feature.
862 static int update_kernel_stream(int fd
)
865 struct ltt_session
*session
;
866 struct ltt_kernel_session
*ksess
;
867 struct ltt_kernel_channel
*channel
;
869 DBG("Updating kernel streams for channel fd %d", fd
);
872 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
873 session_lock(session
);
874 if (session
->kernel_session
== NULL
) {
875 session_unlock(session
);
878 ksess
= session
->kernel_session
;
880 cds_list_for_each_entry(channel
,
881 &ksess
->channel_list
.head
, list
) {
882 struct lttng_ht_iter iter
;
883 struct consumer_socket
*socket
;
885 if (channel
->fd
!= fd
) {
888 DBG("Channel found, updating kernel streams");
889 ret
= kernel_open_channel_stream(channel
);
893 /* Update the stream global counter */
894 ksess
->stream_count_global
+= ret
;
897 * Have we already sent fds to the consumer? If yes, it
898 * means that tracing is started so it is safe to send
899 * our updated stream fds.
901 if (ksess
->consumer_fds_sent
!= 1
902 || ksess
->consumer
== NULL
) {
908 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
909 &iter
.iter
, socket
, node
.node
) {
910 pthread_mutex_lock(socket
->lock
);
911 ret
= kernel_consumer_send_channel_streams(socket
,
913 session
->output_traces
? 1 : 0);
914 pthread_mutex_unlock(socket
->lock
);
922 session_unlock(session
);
924 session_unlock_list();
928 session_unlock(session
);
929 session_unlock_list();
934 * For each tracing session, update newly registered apps. The session list
935 * lock MUST be acquired before calling this.
937 static void update_ust_app(int app_sock
)
939 struct ltt_session
*sess
, *stmp
;
941 /* Consumer is in an ERROR state. Stop any application update. */
942 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
943 /* Stop the update process since the consumer is dead. */
947 /* For all tracing session(s) */
948 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
952 if (!sess
->ust_session
) {
957 assert(app_sock
>= 0);
958 app
= ust_app_find_by_sock(app_sock
);
961 * Application can be unregistered before so
962 * this is possible hence simply stopping the
965 DBG3("UST app update failed to find app sock %d",
969 ust_app_global_update(sess
->ust_session
, app
);
973 session_unlock(sess
);
978 * This thread manage event coming from the kernel.
980 * Features supported in this thread:
983 static void *thread_manage_kernel(void *data
)
985 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
986 uint32_t revents
, nb_fd
;
988 struct lttng_poll_event events
;
990 DBG("[thread] Thread manage kernel started");
992 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
995 * This first step of the while is to clean this structure which could free
996 * non NULL pointers so initialize it before the loop.
998 lttng_poll_init(&events
);
1000 if (testpoint(sessiond_thread_manage_kernel
)) {
1001 goto error_testpoint
;
1004 health_code_update();
1006 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1007 goto error_testpoint
;
1011 health_code_update();
1013 if (update_poll_flag
== 1) {
1014 /* Clean events object. We are about to populate it again. */
1015 lttng_poll_clean(&events
);
1017 ret
= sessiond_set_thread_pollset(&events
, 2);
1019 goto error_poll_create
;
1022 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1027 /* This will add the available kernel channel if any. */
1028 ret
= update_kernel_poll(&events
);
1032 update_poll_flag
= 0;
1035 DBG("Thread kernel polling");
1037 /* Poll infinite value of time */
1039 health_poll_entry();
1040 ret
= lttng_poll_wait(&events
, -1);
1041 DBG("Thread kernel return from poll on %d fds",
1042 LTTNG_POLL_GETNB(&events
));
1046 * Restart interrupted system call.
1048 if (errno
== EINTR
) {
1052 } else if (ret
== 0) {
1053 /* Should not happen since timeout is infinite */
1054 ERR("Return value of poll is 0 with an infinite timeout.\n"
1055 "This should not have happened! Continuing...");
1061 for (i
= 0; i
< nb_fd
; i
++) {
1062 /* Fetch once the poll data */
1063 revents
= LTTNG_POLL_GETEV(&events
, i
);
1064 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1066 health_code_update();
1069 /* No activity for this FD (poll implementation). */
1073 /* Thread quit pipe has been closed. Killing thread. */
1074 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1080 /* Check for data on kernel pipe */
1081 if (revents
& LPOLLIN
) {
1082 if (pollfd
== kernel_poll_pipe
[0]) {
1083 (void) lttng_read(kernel_poll_pipe
[0],
1086 * Ret value is useless here, if this pipe gets any actions an
1087 * update is required anyway.
1089 update_poll_flag
= 1;
1093 * New CPU detected by the kernel. Adding kernel stream to
1094 * kernel session and updating the kernel consumer
1096 ret
= update_kernel_stream(pollfd
);
1102 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1103 update_poll_flag
= 1;
1106 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1114 lttng_poll_clean(&events
);
1117 utils_close_pipe(kernel_poll_pipe
);
1118 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1121 ERR("Health error occurred in %s", __func__
);
1122 WARN("Kernel thread died unexpectedly. "
1123 "Kernel tracing can continue but CPU hotplug is disabled.");
1125 health_unregister(health_sessiond
);
1126 DBG("Kernel thread dying");
1131 * Signal pthread condition of the consumer data that the thread.
1133 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1135 pthread_mutex_lock(&data
->cond_mutex
);
1138 * The state is set before signaling. It can be any value, it's the waiter
1139 * job to correctly interpret this condition variable associated to the
1140 * consumer pthread_cond.
1142 * A value of 0 means that the corresponding thread of the consumer data
1143 * was not started. 1 indicates that the thread has started and is ready
1144 * for action. A negative value means that there was an error during the
1147 data
->consumer_thread_is_ready
= state
;
1148 (void) pthread_cond_signal(&data
->cond
);
1150 pthread_mutex_unlock(&data
->cond_mutex
);
1154 * This thread manage the consumer error sent back to the session daemon.
1156 static void *thread_manage_consumer(void *data
)
1158 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1159 uint32_t revents
, nb_fd
;
1160 enum lttcomm_return_code code
;
1161 struct lttng_poll_event events
;
1162 struct consumer_data
*consumer_data
= data
;
1163 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1165 DBG("[thread] Manage consumer started");
1167 rcu_register_thread();
1168 rcu_thread_online();
1170 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1172 health_code_update();
1175 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1176 * metadata_sock. Nothing more will be added to this poll set.
1178 ret
= sessiond_set_thread_pollset(&events
, 3);
1184 * The error socket here is already in a listening state which was done
1185 * just before spawning this thread to avoid a race between the consumer
1186 * daemon exec trying to connect and the listen() call.
1188 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1193 health_code_update();
1195 /* Infinite blocking call, waiting for transmission */
1197 health_poll_entry();
1199 if (testpoint(sessiond_thread_manage_consumer
)) {
1203 ret
= lttng_poll_wait(&events
, -1);
1207 * Restart interrupted system call.
1209 if (errno
== EINTR
) {
1217 for (i
= 0; i
< nb_fd
; i
++) {
1218 /* Fetch once the poll data */
1219 revents
= LTTNG_POLL_GETEV(&events
, i
);
1220 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1222 health_code_update();
1225 /* No activity for this FD (poll implementation). */
1229 /* Thread quit pipe has been closed. Killing thread. */
1230 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1236 /* Event on the registration socket */
1237 if (pollfd
== consumer_data
->err_sock
) {
1238 if (revents
& LPOLLIN
) {
1240 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1241 ERR("consumer err socket poll error");
1244 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1250 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1256 * Set the CLOEXEC flag. Return code is useless because either way, the
1259 (void) utils_set_fd_cloexec(sock
);
1261 health_code_update();
1263 DBG2("Receiving code from consumer err_sock");
1265 /* Getting status code from kconsumerd */
1266 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1267 sizeof(enum lttcomm_return_code
));
1272 health_code_update();
1273 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1274 ERR("consumer error when waiting for SOCK_READY : %s",
1275 lttcomm_get_readable_code(-code
));
1279 /* Connect both command and metadata sockets. */
1280 consumer_data
->cmd_sock
=
1281 lttcomm_connect_unix_sock(
1282 consumer_data
->cmd_unix_sock_path
);
1283 consumer_data
->metadata_fd
=
1284 lttcomm_connect_unix_sock(
1285 consumer_data
->cmd_unix_sock_path
);
1286 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1287 PERROR("consumer connect cmd socket");
1288 /* On error, signal condition and quit. */
1289 signal_consumer_condition(consumer_data
, -1);
1293 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1295 /* Create metadata socket lock. */
1296 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1297 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1298 PERROR("zmalloc pthread mutex");
1301 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1303 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1304 DBG("Consumer metadata socket ready (fd: %d)",
1305 consumer_data
->metadata_fd
);
1308 * Remove the consumerd error sock since we've established a connection.
1310 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1315 /* Add new accepted error socket. */
1316 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1321 /* Add metadata socket that is successfully connected. */
1322 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1323 LPOLLIN
| LPOLLRDHUP
);
1328 health_code_update();
1331 * Transfer the write-end of the channel monitoring and rotate pipe
1332 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1333 * SET_CHANNEL_ROTATE_PIPE commands.
1335 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1336 if (!cmd_socket_wrapper
) {
1339 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1341 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1342 consumer_data
->channel_monitor_pipe
);
1347 ret
= consumer_send_channel_rotate_pipe(cmd_socket_wrapper
,
1348 consumer_data
->channel_rotate_pipe
);
1353 /* Discard the socket wrapper as it is no longer needed. */
1354 consumer_destroy_socket(cmd_socket_wrapper
);
1355 cmd_socket_wrapper
= NULL
;
1357 /* The thread is completely initialized, signal that it is ready. */
1358 signal_consumer_condition(consumer_data
, 1);
1360 /* Infinite blocking call, waiting for transmission */
1363 health_code_update();
1365 /* Exit the thread because the thread quit pipe has been triggered. */
1367 /* Not a health error. */
1372 health_poll_entry();
1373 ret
= lttng_poll_wait(&events
, -1);
1377 * Restart interrupted system call.
1379 if (errno
== EINTR
) {
1387 for (i
= 0; i
< nb_fd
; i
++) {
1388 /* Fetch once the poll data */
1389 revents
= LTTNG_POLL_GETEV(&events
, i
);
1390 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1392 health_code_update();
1395 /* No activity for this FD (poll implementation). */
1400 * Thread quit pipe has been triggered, flag that we should stop
1401 * but continue the current loop to handle potential data from
1404 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1406 if (pollfd
== sock
) {
1407 /* Event on the consumerd socket */
1408 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1409 && !(revents
& LPOLLIN
)) {
1410 ERR("consumer err socket second poll error");
1413 health_code_update();
1414 /* Wait for any kconsumerd error */
1415 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1416 sizeof(enum lttcomm_return_code
));
1418 ERR("consumer closed the command socket");
1422 ERR("consumer return code : %s",
1423 lttcomm_get_readable_code(-code
));
1426 } else if (pollfd
== consumer_data
->metadata_fd
) {
1427 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1428 && !(revents
& LPOLLIN
)) {
1429 ERR("consumer err metadata socket second poll error");
1432 /* UST metadata requests */
1433 ret
= ust_consumer_metadata_request(
1434 &consumer_data
->metadata_sock
);
1436 ERR("Handling metadata request");
1440 /* No need for an else branch all FDs are tested prior. */
1442 health_code_update();
1448 * We lock here because we are about to close the sockets and some other
1449 * thread might be using them so get exclusive access which will abort all
1450 * other consumer command by other threads.
1452 pthread_mutex_lock(&consumer_data
->lock
);
1454 /* Immediately set the consumerd state to stopped */
1455 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1456 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1457 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1458 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1459 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1461 /* Code flow error... */
1465 if (consumer_data
->err_sock
>= 0) {
1466 ret
= close(consumer_data
->err_sock
);
1470 consumer_data
->err_sock
= -1;
1472 if (consumer_data
->cmd_sock
>= 0) {
1473 ret
= close(consumer_data
->cmd_sock
);
1477 consumer_data
->cmd_sock
= -1;
1479 if (consumer_data
->metadata_sock
.fd_ptr
&&
1480 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1481 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1493 unlink(consumer_data
->err_unix_sock_path
);
1494 unlink(consumer_data
->cmd_unix_sock_path
);
1495 pthread_mutex_unlock(&consumer_data
->lock
);
1497 /* Cleanup metadata socket mutex. */
1498 if (consumer_data
->metadata_sock
.lock
) {
1499 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1500 free(consumer_data
->metadata_sock
.lock
);
1502 lttng_poll_clean(&events
);
1504 if (cmd_socket_wrapper
) {
1505 consumer_destroy_socket(cmd_socket_wrapper
);
1510 ERR("Health error occurred in %s", __func__
);
1512 health_unregister(health_sessiond
);
1513 DBG("consumer thread cleanup completed");
1515 rcu_thread_offline();
1516 rcu_unregister_thread();
1522 * This thread receives application command sockets (FDs) on the
1523 * apps_cmd_pipe and waits (polls) on them until they are closed
1524 * or an error occurs.
1526 * At that point, it flushes the data (tracing and metadata) associated
1527 * with this application and tears down ust app sessions and other
1528 * associated data structures through ust_app_unregister().
1530 * Note that this thread never sends commands to the applications
1531 * through the command sockets; it merely listens for hang-ups
1532 * and errors on those sockets and cleans-up as they occur.
1534 static void *thread_manage_apps(void *data
)
1536 int i
, ret
, pollfd
, err
= -1;
1538 uint32_t revents
, nb_fd
;
1539 struct lttng_poll_event events
;
1541 DBG("[thread] Manage application started");
1543 rcu_register_thread();
1544 rcu_thread_online();
1546 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1548 if (testpoint(sessiond_thread_manage_apps
)) {
1549 goto error_testpoint
;
1552 health_code_update();
1554 ret
= sessiond_set_thread_pollset(&events
, 2);
1556 goto error_poll_create
;
1559 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1564 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1568 health_code_update();
1571 DBG("Apps thread polling");
1573 /* Inifinite blocking call, waiting for transmission */
1575 health_poll_entry();
1576 ret
= lttng_poll_wait(&events
, -1);
1577 DBG("Apps thread return from poll on %d fds",
1578 LTTNG_POLL_GETNB(&events
));
1582 * Restart interrupted system call.
1584 if (errno
== EINTR
) {
1592 for (i
= 0; i
< nb_fd
; i
++) {
1593 /* Fetch once the poll data */
1594 revents
= LTTNG_POLL_GETEV(&events
, i
);
1595 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1597 health_code_update();
1600 /* No activity for this FD (poll implementation). */
1604 /* Thread quit pipe has been closed. Killing thread. */
1605 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1611 /* Inspect the apps cmd pipe */
1612 if (pollfd
== apps_cmd_pipe
[0]) {
1613 if (revents
& LPOLLIN
) {
1617 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1618 if (size_ret
< sizeof(sock
)) {
1619 PERROR("read apps cmd pipe");
1623 health_code_update();
1626 * Since this is a command socket (write then read),
1627 * we only monitor the error events of the socket.
1629 ret
= lttng_poll_add(&events
, sock
,
1630 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1635 DBG("Apps with sock %d added to poll set", sock
);
1636 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1637 ERR("Apps command pipe error");
1640 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1645 * At this point, we know that a registered application made
1646 * the event at poll_wait.
1648 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1649 /* Removing from the poll set */
1650 ret
= lttng_poll_del(&events
, pollfd
);
1655 /* Socket closed on remote end. */
1656 ust_app_unregister(pollfd
);
1658 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1663 health_code_update();
1669 lttng_poll_clean(&events
);
1672 utils_close_pipe(apps_cmd_pipe
);
1673 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1676 * We don't clean the UST app hash table here since already registered
1677 * applications can still be controlled so let them be until the session
1678 * daemon dies or the applications stop.
1683 ERR("Health error occurred in %s", __func__
);
1685 health_unregister(health_sessiond
);
1686 DBG("Application communication apps thread cleanup complete");
1687 rcu_thread_offline();
1688 rcu_unregister_thread();
1693 * Send a socket to a thread This is called from the dispatch UST registration
1694 * thread once all sockets are set for the application.
1696 * The sock value can be invalid, we don't really care, the thread will handle
1697 * it and make the necessary cleanup if so.
1699 * On success, return 0 else a negative value being the errno message of the
1702 static int send_socket_to_thread(int fd
, int sock
)
1707 * It's possible that the FD is set as invalid with -1 concurrently just
1708 * before calling this function being a shutdown state of the thread.
1715 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1716 if (ret
< sizeof(sock
)) {
1717 PERROR("write apps pipe %d", fd
);
1724 /* All good. Don't send back the write positive ret value. */
1731 * Sanitize the wait queue of the dispatch registration thread meaning removing
1732 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1733 * notify socket is never received.
1735 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1737 int ret
, nb_fd
= 0, i
;
1738 unsigned int fd_added
= 0;
1739 struct lttng_poll_event events
;
1740 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1744 lttng_poll_init(&events
);
1746 /* Just skip everything for an empty queue. */
1747 if (!wait_queue
->count
) {
1751 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1756 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1757 &wait_queue
->head
, head
) {
1758 assert(wait_node
->app
);
1759 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1760 LPOLLHUP
| LPOLLERR
);
1773 * Poll but don't block so we can quickly identify the faulty events and
1774 * clean them afterwards from the wait queue.
1776 ret
= lttng_poll_wait(&events
, 0);
1782 for (i
= 0; i
< nb_fd
; i
++) {
1783 /* Get faulty FD. */
1784 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1785 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1788 /* No activity for this FD (poll implementation). */
1792 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1793 &wait_queue
->head
, head
) {
1794 if (pollfd
== wait_node
->app
->sock
&&
1795 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1796 cds_list_del(&wait_node
->head
);
1797 wait_queue
->count
--;
1798 ust_app_destroy(wait_node
->app
);
1801 * Silence warning of use-after-free in
1802 * cds_list_for_each_entry_safe which uses
1803 * __typeof__(*wait_node).
1808 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1815 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1819 lttng_poll_clean(&events
);
1823 lttng_poll_clean(&events
);
1825 ERR("Unable to sanitize wait queue");
1830 * Dispatch request from the registration threads to the application
1831 * communication thread.
1833 static void *thread_dispatch_ust_registration(void *data
)
1836 struct cds_wfcq_node
*node
;
1837 struct ust_command
*ust_cmd
= NULL
;
1838 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1839 struct ust_reg_wait_queue wait_queue
= {
1843 rcu_register_thread();
1845 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1847 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1848 goto error_testpoint
;
1851 health_code_update();
1853 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1855 DBG("[thread] Dispatch UST command started");
1858 health_code_update();
1860 /* Atomically prepare the queue futex */
1861 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1863 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1868 struct ust_app
*app
= NULL
;
1872 * Make sure we don't have node(s) that have hung up before receiving
1873 * the notify socket. This is to clean the list in order to avoid
1874 * memory leaks from notify socket that are never seen.
1876 sanitize_wait_queue(&wait_queue
);
1878 health_code_update();
1879 /* Dequeue command for registration */
1880 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1882 DBG("Woken up but nothing in the UST command queue");
1883 /* Continue thread execution */
1887 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1889 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1890 " gid:%d sock:%d name:%s (version %d.%d)",
1891 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1892 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1893 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1894 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1896 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1897 wait_node
= zmalloc(sizeof(*wait_node
));
1899 PERROR("zmalloc wait_node dispatch");
1900 ret
= close(ust_cmd
->sock
);
1902 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1904 lttng_fd_put(LTTNG_FD_APPS
, 1);
1908 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1910 /* Create application object if socket is CMD. */
1911 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1913 if (!wait_node
->app
) {
1914 ret
= close(ust_cmd
->sock
);
1916 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1918 lttng_fd_put(LTTNG_FD_APPS
, 1);
1924 * Add application to the wait queue so we can set the notify
1925 * socket before putting this object in the global ht.
1927 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1932 * We have to continue here since we don't have the notify
1933 * socket and the application MUST be added to the hash table
1934 * only at that moment.
1939 * Look for the application in the local wait queue and set the
1940 * notify socket if found.
1942 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1943 &wait_queue
.head
, head
) {
1944 health_code_update();
1945 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1946 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1947 cds_list_del(&wait_node
->head
);
1949 app
= wait_node
->app
;
1951 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1957 * With no application at this stage the received socket is
1958 * basically useless so close it before we free the cmd data
1959 * structure for good.
1962 ret
= close(ust_cmd
->sock
);
1964 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1966 lttng_fd_put(LTTNG_FD_APPS
, 1);
1973 * @session_lock_list
1975 * Lock the global session list so from the register up to the
1976 * registration done message, no thread can see the application
1977 * and change its state.
1979 session_lock_list();
1983 * Add application to the global hash table. This needs to be
1984 * done before the update to the UST registry can locate the
1989 /* Set app version. This call will print an error if needed. */
1990 (void) ust_app_version(app
);
1992 /* Send notify socket through the notify pipe. */
1993 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1997 session_unlock_list();
1999 * No notify thread, stop the UST tracing. However, this is
2000 * not an internal error of the this thread thus setting
2001 * the health error code to a normal exit.
2008 * Update newly registered application with the tracing
2009 * registry info already enabled information.
2011 update_ust_app(app
->sock
);
2014 * Don't care about return value. Let the manage apps threads
2015 * handle app unregistration upon socket close.
2017 (void) ust_app_register_done(app
);
2020 * Even if the application socket has been closed, send the app
2021 * to the thread and unregistration will take place at that
2024 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2027 session_unlock_list();
2029 * No apps. thread, stop the UST tracing. However, this is
2030 * not an internal error of the this thread thus setting
2031 * the health error code to a normal exit.
2038 session_unlock_list();
2040 } while (node
!= NULL
);
2042 health_poll_entry();
2043 /* Futex wait on queue. Blocking call on futex() */
2044 futex_nto1_wait(&ust_cmd_queue
.futex
);
2047 /* Normal exit, no error */
2051 /* Clean up wait queue. */
2052 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2053 &wait_queue
.head
, head
) {
2054 cds_list_del(&wait_node
->head
);
2059 /* Empty command queue. */
2061 /* Dequeue command for registration */
2062 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2066 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2067 ret
= close(ust_cmd
->sock
);
2069 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2071 lttng_fd_put(LTTNG_FD_APPS
, 1);
2076 DBG("Dispatch thread dying");
2079 ERR("Health error occurred in %s", __func__
);
2081 health_unregister(health_sessiond
);
2082 rcu_unregister_thread();
2087 * This thread manage application registration.
2089 static void *thread_registration_apps(void *data
)
2091 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2092 uint32_t revents
, nb_fd
;
2093 struct lttng_poll_event events
;
2095 * Get allocated in this thread, enqueued to a global queue, dequeued and
2096 * freed in the manage apps thread.
2098 struct ust_command
*ust_cmd
= NULL
;
2100 DBG("[thread] Manage application registration started");
2102 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2104 if (testpoint(sessiond_thread_registration_apps
)) {
2105 goto error_testpoint
;
2108 ret
= lttcomm_listen_unix_sock(apps_sock
);
2114 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2115 * more will be added to this poll set.
2117 ret
= sessiond_set_thread_pollset(&events
, 2);
2119 goto error_create_poll
;
2122 /* Add the application registration socket */
2123 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2125 goto error_poll_add
;
2128 /* Notify all applications to register */
2129 ret
= notify_ust_apps(1);
2131 ERR("Failed to notify applications or create the wait shared memory.\n"
2132 "Execution continues but there might be problem for already\n"
2133 "running applications that wishes to register.");
2137 DBG("Accepting application registration");
2139 /* Inifinite blocking call, waiting for transmission */
2141 health_poll_entry();
2142 ret
= lttng_poll_wait(&events
, -1);
2146 * Restart interrupted system call.
2148 if (errno
== EINTR
) {
2156 for (i
= 0; i
< nb_fd
; i
++) {
2157 health_code_update();
2159 /* Fetch once the poll data */
2160 revents
= LTTNG_POLL_GETEV(&events
, i
);
2161 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2164 /* No activity for this FD (poll implementation). */
2168 /* Thread quit pipe has been closed. Killing thread. */
2169 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2175 /* Event on the registration socket */
2176 if (pollfd
== apps_sock
) {
2177 if (revents
& LPOLLIN
) {
2178 sock
= lttcomm_accept_unix_sock(apps_sock
);
2184 * Set socket timeout for both receiving and ending.
2185 * app_socket_timeout is in seconds, whereas
2186 * lttcomm_setsockopt_rcv_timeout and
2187 * lttcomm_setsockopt_snd_timeout expect msec as
2190 if (config
.app_socket_timeout
>= 0) {
2191 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2192 config
.app_socket_timeout
* 1000);
2193 (void) lttcomm_setsockopt_snd_timeout(sock
,
2194 config
.app_socket_timeout
* 1000);
2198 * Set the CLOEXEC flag. Return code is useless because
2199 * either way, the show must go on.
2201 (void) utils_set_fd_cloexec(sock
);
2203 /* Create UST registration command for enqueuing */
2204 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2205 if (ust_cmd
== NULL
) {
2206 PERROR("ust command zmalloc");
2215 * Using message-based transmissions to ensure we don't
2216 * have to deal with partially received messages.
2218 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2220 ERR("Exhausted file descriptors allowed for applications.");
2230 health_code_update();
2231 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2234 /* Close socket of the application. */
2239 lttng_fd_put(LTTNG_FD_APPS
, 1);
2243 health_code_update();
2245 ust_cmd
->sock
= sock
;
2248 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2249 " gid:%d sock:%d name:%s (version %d.%d)",
2250 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2251 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2252 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2253 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2256 * Lock free enqueue the registration request. The red pill
2257 * has been taken! This apps will be part of the *system*.
2259 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2262 * Wake the registration queue futex. Implicit memory
2263 * barrier with the exchange in cds_wfcq_enqueue.
2265 futex_nto1_wake(&ust_cmd_queue
.futex
);
2266 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2267 ERR("Register apps socket poll error");
2270 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2279 /* Notify that the registration thread is gone */
2282 if (apps_sock
>= 0) {
2283 ret
= close(apps_sock
);
2293 lttng_fd_put(LTTNG_FD_APPS
, 1);
2295 unlink(config
.apps_unix_sock_path
.value
);
2298 lttng_poll_clean(&events
);
2302 DBG("UST Registration thread cleanup complete");
2305 ERR("Health error occurred in %s", __func__
);
2307 health_unregister(health_sessiond
);
2313 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2314 * exec or it will fails.
2316 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2319 struct timespec timeout
;
2322 * Make sure we set the readiness flag to 0 because we are NOT ready.
2323 * This access to consumer_thread_is_ready does not need to be
2324 * protected by consumer_data.cond_mutex (yet) since the consumer
2325 * management thread has not been started at this point.
2327 consumer_data
->consumer_thread_is_ready
= 0;
2329 /* Setup pthread condition */
2330 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2333 PERROR("pthread_condattr_init consumer data");
2338 * Set the monotonic clock in order to make sure we DO NOT jump in time
2339 * between the clock_gettime() call and the timedwait call. See bug #324
2340 * for a more details and how we noticed it.
2342 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2345 PERROR("pthread_condattr_setclock consumer data");
2349 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2352 PERROR("pthread_cond_init consumer data");
2356 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2357 thread_manage_consumer
, consumer_data
);
2360 PERROR("pthread_create consumer");
2365 /* We are about to wait on a pthread condition */
2366 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2368 /* Get time for sem_timedwait absolute timeout */
2369 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2371 * Set the timeout for the condition timed wait even if the clock gettime
2372 * call fails since we might loop on that call and we want to avoid to
2373 * increment the timeout too many times.
2375 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2378 * The following loop COULD be skipped in some conditions so this is why we
2379 * set ret to 0 in order to make sure at least one round of the loop is
2385 * Loop until the condition is reached or when a timeout is reached. Note
2386 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2387 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2388 * possible. This loop does not take any chances and works with both of
2391 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2392 if (clock_ret
< 0) {
2393 PERROR("clock_gettime spawn consumer");
2394 /* Infinite wait for the consumerd thread to be ready */
2395 ret
= pthread_cond_wait(&consumer_data
->cond
,
2396 &consumer_data
->cond_mutex
);
2398 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2399 &consumer_data
->cond_mutex
, &timeout
);
2403 /* Release the pthread condition */
2404 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2408 if (ret
== ETIMEDOUT
) {
2412 * Call has timed out so we kill the kconsumerd_thread and return
2415 ERR("Condition timed out. The consumer thread was never ready."
2417 pth_ret
= pthread_cancel(consumer_data
->thread
);
2419 PERROR("pthread_cancel consumer thread");
2422 PERROR("pthread_cond_wait failed consumer thread");
2424 /* Caller is expecting a negative value on failure. */
2429 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2430 if (consumer_data
->pid
== 0) {
2431 ERR("Consumerd did not start");
2432 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2435 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2444 * Join consumer thread
2446 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2450 /* Consumer pid must be a real one. */
2451 if (consumer_data
->pid
> 0) {
2453 ret
= kill(consumer_data
->pid
, SIGTERM
);
2455 PERROR("Error killing consumer daemon");
2458 return pthread_join(consumer_data
->thread
, &status
);
2465 * Fork and exec a consumer daemon (consumerd).
2467 * Return pid if successful else -1.
2469 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2473 const char *consumer_to_use
;
2474 const char *verbosity
;
2477 DBG("Spawning consumerd");
2484 if (config
.verbose_consumer
) {
2485 verbosity
= "--verbose";
2486 } else if (lttng_opt_quiet
) {
2487 verbosity
= "--quiet";
2492 switch (consumer_data
->type
) {
2493 case LTTNG_CONSUMER_KERNEL
:
2495 * Find out which consumerd to execute. We will first try the
2496 * 64-bit path, then the sessiond's installation directory, and
2497 * fallback on the 32-bit one,
2499 DBG3("Looking for a kernel consumer at these locations:");
2500 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2501 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2502 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2503 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2504 DBG3("Found location #1");
2505 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2506 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2507 DBG3("Found location #2");
2508 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2509 } else if (stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2510 DBG3("Found location #3");
2511 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2513 DBG("Could not find any valid consumerd executable");
2517 DBG("Using kernel consumer at: %s", consumer_to_use
);
2518 (void) execl(consumer_to_use
,
2519 "lttng-consumerd", verbosity
, "-k",
2520 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2521 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2522 "--group", config
.tracing_group_name
.value
,
2525 case LTTNG_CONSUMER64_UST
:
2527 if (config
.consumerd64_lib_dir
.value
) {
2532 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2536 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2537 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2542 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2543 if (tmp
[0] != '\0') {
2544 strcat(tmpnew
, ":");
2545 strcat(tmpnew
, tmp
);
2547 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2554 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2555 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2556 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2557 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2558 "--group", config
.tracing_group_name
.value
,
2562 case LTTNG_CONSUMER32_UST
:
2564 if (config
.consumerd32_lib_dir
.value
) {
2569 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2573 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2574 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2579 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2580 if (tmp
[0] != '\0') {
2581 strcat(tmpnew
, ":");
2582 strcat(tmpnew
, tmp
);
2584 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2591 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2592 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2593 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2594 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2595 "--group", config
.tracing_group_name
.value
,
2600 ERR("unknown consumer type");
2604 PERROR("Consumer execl()");
2606 /* Reaching this point, we got a failure on our execl(). */
2608 } else if (pid
> 0) {
2611 PERROR("start consumer fork");
2619 * Spawn the consumerd daemon and session daemon thread.
2621 static int start_consumerd(struct consumer_data
*consumer_data
)
2626 * Set the listen() state on the socket since there is a possible race
2627 * between the exec() of the consumer daemon and this call if place in the
2628 * consumer thread. See bug #366 for more details.
2630 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2635 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2636 if (consumer_data
->pid
!= 0) {
2637 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2641 ret
= spawn_consumerd(consumer_data
);
2643 ERR("Spawning consumerd failed");
2644 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2648 /* Setting up the consumer_data pid */
2649 consumer_data
->pid
= ret
;
2650 DBG2("Consumer pid %d", consumer_data
->pid
);
2651 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2653 DBG2("Spawning consumer control thread");
2654 ret
= spawn_consumer_thread(consumer_data
);
2656 ERR("Fatal error spawning consumer control thread");
2664 /* Cleanup already created sockets on error. */
2665 if (consumer_data
->err_sock
>= 0) {
2668 err
= close(consumer_data
->err_sock
);
2670 PERROR("close consumer data error socket");
2677 * Setup necessary data for kernel tracer action.
2679 static int init_kernel_tracer(void)
2683 /* Modprobe lttng kernel modules */
2684 ret
= modprobe_lttng_control();
2689 /* Open debugfs lttng */
2690 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2691 if (kernel_tracer_fd
< 0) {
2692 DBG("Failed to open %s", module_proc_lttng
);
2696 /* Validate kernel version */
2697 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2698 &kernel_tracer_abi_version
);
2703 ret
= modprobe_lttng_data();
2708 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2715 WARN("Kernel tracer does not support buffer monitoring. "
2716 "The monitoring timer of channels in the kernel domain "
2717 "will be set to 0 (disabled).");
2720 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2724 modprobe_remove_lttng_control();
2725 ret
= close(kernel_tracer_fd
);
2729 kernel_tracer_fd
= -1;
2730 return LTTNG_ERR_KERN_VERSION
;
2733 ret
= close(kernel_tracer_fd
);
2739 modprobe_remove_lttng_control();
2742 WARN("No kernel tracer available");
2743 kernel_tracer_fd
= -1;
2745 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2747 return LTTNG_ERR_KERN_NA
;
2753 * Copy consumer output from the tracing session to the domain session. The
2754 * function also applies the right modification on a per domain basis for the
2755 * trace files destination directory.
2757 * Should *NOT* be called with RCU read-side lock held.
2759 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2762 const char *dir_name
;
2763 struct consumer_output
*consumer
;
2766 assert(session
->consumer
);
2769 case LTTNG_DOMAIN_KERNEL
:
2770 DBG3("Copying tracing session consumer output in kernel session");
2772 * XXX: We should audit the session creation and what this function
2773 * does "extra" in order to avoid a destroy since this function is used
2774 * in the domain session creation (kernel and ust) only. Same for UST
2777 if (session
->kernel_session
->consumer
) {
2778 consumer_output_put(session
->kernel_session
->consumer
);
2780 session
->kernel_session
->consumer
=
2781 consumer_copy_output(session
->consumer
);
2782 /* Ease our life a bit for the next part */
2783 consumer
= session
->kernel_session
->consumer
;
2784 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2786 case LTTNG_DOMAIN_JUL
:
2787 case LTTNG_DOMAIN_LOG4J
:
2788 case LTTNG_DOMAIN_PYTHON
:
2789 case LTTNG_DOMAIN_UST
:
2790 DBG3("Copying tracing session consumer output in UST session");
2791 if (session
->ust_session
->consumer
) {
2792 consumer_output_put(session
->ust_session
->consumer
);
2794 session
->ust_session
->consumer
=
2795 consumer_copy_output(session
->consumer
);
2796 /* Ease our life a bit for the next part */
2797 consumer
= session
->ust_session
->consumer
;
2798 dir_name
= DEFAULT_UST_TRACE_DIR
;
2801 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2805 /* Append correct directory to subdir */
2806 strncat(consumer
->subdir
, dir_name
,
2807 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2808 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2817 * Create an UST session and add it to the session ust list.
2819 * Should *NOT* be called with RCU read-side lock held.
2821 static int create_ust_session(struct ltt_session
*session
,
2822 struct lttng_domain
*domain
)
2825 struct ltt_ust_session
*lus
= NULL
;
2829 assert(session
->consumer
);
2831 switch (domain
->type
) {
2832 case LTTNG_DOMAIN_JUL
:
2833 case LTTNG_DOMAIN_LOG4J
:
2834 case LTTNG_DOMAIN_PYTHON
:
2835 case LTTNG_DOMAIN_UST
:
2838 ERR("Unknown UST domain on create session %d", domain
->type
);
2839 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2843 DBG("Creating UST session");
2845 lus
= trace_ust_create_session(session
->id
);
2847 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2851 lus
->uid
= session
->uid
;
2852 lus
->gid
= session
->gid
;
2853 lus
->output_traces
= session
->output_traces
;
2854 lus
->snapshot_mode
= session
->snapshot_mode
;
2855 lus
->live_timer_interval
= session
->live_timer
;
2856 session
->ust_session
= lus
;
2857 if (session
->shm_path
[0]) {
2858 strncpy(lus
->root_shm_path
, session
->shm_path
,
2859 sizeof(lus
->root_shm_path
));
2860 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2861 strncpy(lus
->shm_path
, session
->shm_path
,
2862 sizeof(lus
->shm_path
));
2863 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2864 strncat(lus
->shm_path
, "/ust",
2865 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2867 /* Copy session output to the newly created UST session */
2868 ret
= copy_session_consumer(domain
->type
, session
);
2869 if (ret
!= LTTNG_OK
) {
2877 session
->ust_session
= NULL
;
2882 * Create a kernel tracer session then create the default channel.
2884 static int create_kernel_session(struct ltt_session
*session
)
2888 DBG("Creating kernel session");
2890 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2892 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2896 /* Code flow safety */
2897 assert(session
->kernel_session
);
2899 /* Copy session output to the newly created Kernel session */
2900 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2901 if (ret
!= LTTNG_OK
) {
2905 session
->kernel_session
->uid
= session
->uid
;
2906 session
->kernel_session
->gid
= session
->gid
;
2907 session
->kernel_session
->output_traces
= session
->output_traces
;
2908 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2913 trace_kernel_destroy_session(session
->kernel_session
);
2914 session
->kernel_session
= NULL
;
2919 * Count number of session permitted by uid/gid.
2921 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2924 struct ltt_session
*session
;
2926 DBG("Counting number of available session for UID %d GID %d",
2928 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2930 * Only list the sessions the user can control.
2932 if (!session_access_ok(session
, uid
, gid
)) {
2941 * Check if the current kernel tracer supports the session rotation feature.
2942 * Return 1 if it does, 0 otherwise.
2944 static int check_rotate_compatible(void)
2948 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2949 DBG("Kernel tracer version is not compatible with the rotation feature");
2957 * Process the command requested by the lttng client within the command
2958 * context structure. This function make sure that the return structure (llm)
2959 * is set and ready for transmission before returning.
2961 * Return any error encountered or 0 for success.
2963 * "sock" is only used for special-case var. len data.
2965 * Should *NOT* be called with RCU read-side lock held.
2967 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2971 int need_tracing_session
= 1;
2974 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2976 assert(!rcu_read_ongoing());
2980 switch (cmd_ctx
->lsm
->cmd_type
) {
2981 case LTTNG_CREATE_SESSION
:
2982 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2983 case LTTNG_CREATE_SESSION_LIVE
:
2984 case LTTNG_DESTROY_SESSION
:
2985 case LTTNG_LIST_SESSIONS
:
2986 case LTTNG_LIST_DOMAINS
:
2987 case LTTNG_START_TRACE
:
2988 case LTTNG_STOP_TRACE
:
2989 case LTTNG_DATA_PENDING
:
2990 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2991 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2992 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2993 case LTTNG_SNAPSHOT_RECORD
:
2994 case LTTNG_SAVE_SESSION
:
2995 case LTTNG_SET_SESSION_SHM_PATH
:
2996 case LTTNG_REGENERATE_METADATA
:
2997 case LTTNG_REGENERATE_STATEDUMP
:
2998 case LTTNG_REGISTER_TRIGGER
:
2999 case LTTNG_UNREGISTER_TRIGGER
:
3000 case LTTNG_ROTATE_SESSION
:
3001 case LTTNG_ROTATION_GET_INFO
:
3002 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
3003 case LTTNG_ROTATION_SET_SCHEDULE
:
3004 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
3005 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
3012 if (config
.no_kernel
&& need_domain
3013 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3015 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3017 ret
= LTTNG_ERR_KERN_NA
;
3022 /* Deny register consumer if we already have a spawned consumer. */
3023 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3024 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3025 if (kconsumer_data
.pid
> 0) {
3026 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3027 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3030 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3034 * Check for command that don't needs to allocate a returned payload. We do
3035 * this here so we don't have to make the call for no payload at each
3038 switch(cmd_ctx
->lsm
->cmd_type
) {
3039 case LTTNG_LIST_SESSIONS
:
3040 case LTTNG_LIST_TRACEPOINTS
:
3041 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3042 case LTTNG_LIST_DOMAINS
:
3043 case LTTNG_LIST_CHANNELS
:
3044 case LTTNG_LIST_EVENTS
:
3045 case LTTNG_LIST_SYSCALLS
:
3046 case LTTNG_LIST_TRACKER_IDS
:
3047 case LTTNG_DATA_PENDING
:
3048 case LTTNG_ROTATE_SESSION
:
3049 case LTTNG_ROTATION_GET_INFO
:
3050 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
3051 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
3054 /* Setup lttng message with no payload */
3055 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3057 /* This label does not try to unlock the session */
3058 goto init_setup_error
;
3062 /* Commands that DO NOT need a session. */
3063 switch (cmd_ctx
->lsm
->cmd_type
) {
3064 case LTTNG_CREATE_SESSION
:
3065 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3066 case LTTNG_CREATE_SESSION_LIVE
:
3067 case LTTNG_LIST_SESSIONS
:
3068 case LTTNG_LIST_TRACEPOINTS
:
3069 case LTTNG_LIST_SYSCALLS
:
3070 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3071 case LTTNG_SAVE_SESSION
:
3072 case LTTNG_REGISTER_TRIGGER
:
3073 case LTTNG_UNREGISTER_TRIGGER
:
3074 need_tracing_session
= 0;
3077 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3079 * We keep the session list lock across _all_ commands
3080 * for now, because the per-session lock does not
3081 * handle teardown properly.
3083 session_lock_list();
3084 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3085 if (cmd_ctx
->session
== NULL
) {
3086 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3089 /* Acquire lock for the session */
3090 session_lock(cmd_ctx
->session
);
3096 * Commands that need a valid session but should NOT create one if none
3097 * exists. Instead of creating one and destroying it when the command is
3098 * handled, process that right before so we save some round trip in useless
3101 switch (cmd_ctx
->lsm
->cmd_type
) {
3102 case LTTNG_DISABLE_CHANNEL
:
3103 case LTTNG_DISABLE_EVENT
:
3104 switch (cmd_ctx
->lsm
->domain
.type
) {
3105 case LTTNG_DOMAIN_KERNEL
:
3106 if (!cmd_ctx
->session
->kernel_session
) {
3107 ret
= LTTNG_ERR_NO_CHANNEL
;
3111 case LTTNG_DOMAIN_JUL
:
3112 case LTTNG_DOMAIN_LOG4J
:
3113 case LTTNG_DOMAIN_PYTHON
:
3114 case LTTNG_DOMAIN_UST
:
3115 if (!cmd_ctx
->session
->ust_session
) {
3116 ret
= LTTNG_ERR_NO_CHANNEL
;
3121 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3133 * Check domain type for specific "pre-action".
3135 switch (cmd_ctx
->lsm
->domain
.type
) {
3136 case LTTNG_DOMAIN_KERNEL
:
3138 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3142 /* Kernel tracer check */
3143 if (kernel_tracer_fd
== -1) {
3144 /* Basically, load kernel tracer modules */
3145 ret
= init_kernel_tracer();
3151 /* Consumer is in an ERROR state. Report back to client */
3152 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3153 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3157 /* Need a session for kernel command */
3158 if (need_tracing_session
) {
3159 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3160 ret
= create_kernel_session(cmd_ctx
->session
);
3162 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3167 /* Start the kernel consumer daemon */
3168 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3169 if (kconsumer_data
.pid
== 0 &&
3170 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3171 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3172 ret
= start_consumerd(&kconsumer_data
);
3174 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3177 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3179 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3183 * The consumer was just spawned so we need to add the socket to
3184 * the consumer output of the session if exist.
3186 ret
= consumer_create_socket(&kconsumer_data
,
3187 cmd_ctx
->session
->kernel_session
->consumer
);
3194 case LTTNG_DOMAIN_JUL
:
3195 case LTTNG_DOMAIN_LOG4J
:
3196 case LTTNG_DOMAIN_PYTHON
:
3197 case LTTNG_DOMAIN_UST
:
3199 if (!ust_app_supported()) {
3200 ret
= LTTNG_ERR_NO_UST
;
3203 /* Consumer is in an ERROR state. Report back to client */
3204 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3205 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3209 if (need_tracing_session
) {
3210 /* Create UST session if none exist. */
3211 if (cmd_ctx
->session
->ust_session
== NULL
) {
3212 ret
= create_ust_session(cmd_ctx
->session
,
3213 &cmd_ctx
->lsm
->domain
);
3214 if (ret
!= LTTNG_OK
) {
3219 /* Start the UST consumer daemons */
3221 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3222 if (config
.consumerd64_bin_path
.value
&&
3223 ustconsumer64_data
.pid
== 0 &&
3224 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3225 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3226 ret
= start_consumerd(&ustconsumer64_data
);
3228 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3229 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3233 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3234 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3236 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3240 * Setup socket for consumer 64 bit. No need for atomic access
3241 * since it was set above and can ONLY be set in this thread.
3243 ret
= consumer_create_socket(&ustconsumer64_data
,
3244 cmd_ctx
->session
->ust_session
->consumer
);
3250 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3251 if (config
.consumerd32_bin_path
.value
&&
3252 ustconsumer32_data
.pid
== 0 &&
3253 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3254 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3255 ret
= start_consumerd(&ustconsumer32_data
);
3257 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3258 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3262 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3263 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3265 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3269 * Setup socket for consumer 64 bit. No need for atomic access
3270 * since it was set above and can ONLY be set in this thread.
3272 ret
= consumer_create_socket(&ustconsumer32_data
,
3273 cmd_ctx
->session
->ust_session
->consumer
);
3285 /* Validate consumer daemon state when start/stop trace command */
3286 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3287 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3288 switch (cmd_ctx
->lsm
->domain
.type
) {
3289 case LTTNG_DOMAIN_NONE
:
3291 case LTTNG_DOMAIN_JUL
:
3292 case LTTNG_DOMAIN_LOG4J
:
3293 case LTTNG_DOMAIN_PYTHON
:
3294 case LTTNG_DOMAIN_UST
:
3295 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3296 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3300 case LTTNG_DOMAIN_KERNEL
:
3301 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3302 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3307 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3313 * Check that the UID or GID match that of the tracing session.
3314 * The root user can interact with all sessions.
3316 if (need_tracing_session
) {
3317 if (!session_access_ok(cmd_ctx
->session
,
3318 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3319 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3320 ret
= LTTNG_ERR_EPERM
;
3326 * Send relayd information to consumer as soon as we have a domain and a
3329 if (cmd_ctx
->session
&& need_domain
) {
3331 * Setup relayd if not done yet. If the relayd information was already
3332 * sent to the consumer, this call will gracefully return.
3334 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3335 if (ret
!= LTTNG_OK
) {
3340 /* Process by command type */
3341 switch (cmd_ctx
->lsm
->cmd_type
) {
3342 case LTTNG_ADD_CONTEXT
:
3345 * An LTTNG_ADD_CONTEXT command might have a supplementary
3346 * payload if the context being added is an application context.
3348 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3349 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3350 char *provider_name
= NULL
, *context_name
= NULL
;
3351 size_t provider_name_len
=
3352 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3353 size_t context_name_len
=
3354 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3356 if (provider_name_len
== 0 || context_name_len
== 0) {
3358 * Application provider and context names MUST
3361 ret
= -LTTNG_ERR_INVALID
;
3365 provider_name
= zmalloc(provider_name_len
+ 1);
3366 if (!provider_name
) {
3367 ret
= -LTTNG_ERR_NOMEM
;
3370 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3373 context_name
= zmalloc(context_name_len
+ 1);
3374 if (!context_name
) {
3375 ret
= -LTTNG_ERR_NOMEM
;
3376 goto error_add_context
;
3378 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3381 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3384 goto error_add_context
;
3387 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3390 goto error_add_context
;
3395 * cmd_add_context assumes ownership of the provider and context
3398 ret
= cmd_add_context(cmd_ctx
->session
,
3399 cmd_ctx
->lsm
->domain
.type
,
3400 cmd_ctx
->lsm
->u
.context
.channel_name
,
3401 &cmd_ctx
->lsm
->u
.context
.ctx
,
3402 kernel_poll_pipe
[1]);
3404 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3405 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3407 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3408 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3414 case LTTNG_DISABLE_CHANNEL
:
3416 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3417 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3420 case LTTNG_DISABLE_EVENT
:
3424 * FIXME: handle filter; for now we just receive the filter's
3425 * bytecode along with the filter expression which are sent by
3426 * liblttng-ctl and discard them.
3428 * This fixes an issue where the client may block while sending
3429 * the filter payload and encounter an error because the session
3430 * daemon closes the socket without ever handling this data.
3432 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3433 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3436 char data
[LTTNG_FILTER_MAX_LEN
];
3438 DBG("Discarding disable event command payload of size %zu", count
);
3440 ret
= lttcomm_recv_unix_sock(sock
, data
,
3441 count
> sizeof(data
) ? sizeof(data
) : count
);
3446 count
-= (size_t) ret
;
3449 /* FIXME: passing packed structure to non-packed pointer */
3450 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3451 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3452 &cmd_ctx
->lsm
->u
.disable
.event
);
3455 case LTTNG_ENABLE_CHANNEL
:
3457 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3458 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3459 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3460 &cmd_ctx
->lsm
->u
.channel
.chan
,
3461 kernel_poll_pipe
[1]);
3464 case LTTNG_TRACK_ID
:
3466 struct lttng_tracker_id id
;
3468 memset(&id
, 0, sizeof(id
));
3469 id
.type
= cmd_ctx
->lsm
->u
.id_tracker
.id_type
;
3473 case LTTNG_ID_VALUE
:
3474 id
.value
= cmd_ctx
->lsm
->u
.id_tracker
.u
.value
;
3476 case LTTNG_ID_STRING
:
3478 size_t var_len
= cmd_ctx
->lsm
->u
.id_tracker
.u
.var_len
;
3480 id
.string
= zmalloc(var_len
);
3482 ret
= LTTNG_ERR_NOMEM
;
3485 DBG("Receiving var len tracker id string from client.");
3486 ret
= lttcomm_recv_unix_sock(sock
, id
.string
, var_len
);
3488 DBG("Nothing received.");
3491 ret
= LTTNG_ERR_INVALID
;
3494 if (strnlen(id
.string
, var_len
) != var_len
- 1) {
3495 DBG("Corrupted string.");
3497 ret
= LTTNG_ERR_INVALID
;
3503 ret
= LTTNG_ERR_INVALID
;
3506 ret
= cmd_track_id(cmd_ctx
->session
,
3507 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
3508 cmd_ctx
->lsm
->domain
.type
,
3512 case LTTNG_UNTRACK_ID
:
3514 struct lttng_tracker_id id
;
3516 memset(&id
, 0, sizeof(id
));
3517 id
.type
= cmd_ctx
->lsm
->u
.id_tracker
.id_type
;
3521 case LTTNG_ID_VALUE
:
3522 id
.value
= cmd_ctx
->lsm
->u
.id_tracker
.u
.value
;
3524 case LTTNG_ID_STRING
:
3526 size_t var_len
= cmd_ctx
->lsm
->u
.id_tracker
.u
.var_len
;
3528 id
.string
= zmalloc(var_len
);
3530 ret
= LTTNG_ERR_NOMEM
;
3533 DBG("Receiving var len tracker id string from client.");
3534 ret
= lttcomm_recv_unix_sock(sock
, id
.string
, var_len
);
3536 DBG("Nothing received.");
3539 ret
= LTTNG_ERR_INVALID
;
3542 if (strnlen(id
.string
, var_len
) != var_len
- 1) {
3543 DBG("Corrupted string.");
3545 ret
= LTTNG_ERR_INVALID
;
3551 ret
= LTTNG_ERR_INVALID
;
3554 ret
= cmd_untrack_id(cmd_ctx
->session
,
3555 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
3556 cmd_ctx
->lsm
->domain
.type
,
3560 case LTTNG_ENABLE_EVENT
:
3562 struct lttng_event_exclusion
*exclusion
= NULL
;
3563 struct lttng_filter_bytecode
*bytecode
= NULL
;
3564 char *filter_expression
= NULL
;
3566 /* Handle exclusion events and receive it from the client. */
3567 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3568 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3570 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3571 (count
* LTTNG_SYMBOL_NAME_LEN
));
3573 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3577 DBG("Receiving var len exclusion event list from client ...");
3578 exclusion
->count
= count
;
3579 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3580 count
* LTTNG_SYMBOL_NAME_LEN
);
3582 DBG("Nothing recv() from client var len data... continuing");
3585 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3590 /* Get filter expression from client. */
3591 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3592 size_t expression_len
=
3593 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3595 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3596 ret
= LTTNG_ERR_FILTER_INVAL
;
3601 filter_expression
= zmalloc(expression_len
);
3602 if (!filter_expression
) {
3604 ret
= LTTNG_ERR_FILTER_NOMEM
;
3608 /* Receive var. len. data */
3609 DBG("Receiving var len filter's expression from client ...");
3610 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3613 DBG("Nothing recv() from client car len data... continuing");
3615 free(filter_expression
);
3617 ret
= LTTNG_ERR_FILTER_INVAL
;
3622 /* Handle filter and get bytecode from client. */
3623 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3624 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3626 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3627 ret
= LTTNG_ERR_FILTER_INVAL
;
3628 free(filter_expression
);
3633 bytecode
= zmalloc(bytecode_len
);
3635 free(filter_expression
);
3637 ret
= LTTNG_ERR_FILTER_NOMEM
;
3641 /* Receive var. len. data */
3642 DBG("Receiving var len filter's bytecode from client ...");
3643 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3645 DBG("Nothing recv() from client car len data... continuing");
3647 free(filter_expression
);
3650 ret
= LTTNG_ERR_FILTER_INVAL
;
3654 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3655 free(filter_expression
);
3658 ret
= LTTNG_ERR_FILTER_INVAL
;
3663 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3664 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3665 &cmd_ctx
->lsm
->u
.enable
.event
,
3666 filter_expression
, bytecode
, exclusion
,
3667 kernel_poll_pipe
[1]);
3670 case LTTNG_LIST_TRACEPOINTS
:
3672 struct lttng_event
*events
;
3675 session_lock_list();
3676 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3677 session_unlock_list();
3678 if (nb_events
< 0) {
3679 /* Return value is a negative lttng_error_code. */
3685 * Setup lttng message with payload size set to the event list size in
3686 * bytes and then copy list into the llm payload.
3688 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3689 sizeof(struct lttng_event
) * nb_events
);
3699 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3701 struct lttng_event_field
*fields
;
3704 session_lock_list();
3705 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3707 session_unlock_list();
3708 if (nb_fields
< 0) {
3709 /* Return value is a negative lttng_error_code. */
3715 * Setup lttng message with payload size set to the event list size in
3716 * bytes and then copy list into the llm payload.
3718 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3719 sizeof(struct lttng_event_field
) * nb_fields
);
3729 case LTTNG_LIST_SYSCALLS
:
3731 struct lttng_event
*events
;
3734 nb_events
= cmd_list_syscalls(&events
);
3735 if (nb_events
< 0) {
3736 /* Return value is a negative lttng_error_code. */
3742 * Setup lttng message with payload size set to the event list size in
3743 * bytes and then copy list into the llm payload.
3745 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3746 sizeof(struct lttng_event
) * nb_events
);
3756 case LTTNG_LIST_TRACKER_IDS
:
3758 struct lttcomm_tracker_command_header cmd_header
;
3759 struct lttng_tracker_id
*ids
= NULL
;
3761 struct lttng_dynamic_buffer buf
;
3763 nr_ids
= cmd_list_tracker_ids(cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
3765 cmd_ctx
->lsm
->domain
.type
, &ids
);
3767 /* Return value is a negative lttng_error_code. */
3772 lttng_dynamic_buffer_init(&buf
);
3773 for (i
= 0; i
< nr_ids
; i
++) {
3774 struct lttng_tracker_id
*id
= &ids
[i
];
3775 struct lttcomm_tracker_id_header id_hdr
;
3776 size_t var_data_len
= 0;
3778 memset(&id_hdr
, 0, sizeof(id_hdr
));
3779 id_hdr
.type
= id
->type
;
3783 case LTTNG_ID_VALUE
:
3784 id_hdr
.u
.value
= id
->value
;
3786 case LTTNG_ID_STRING
:
3787 id_hdr
.u
.var_data_len
= var_data_len
= strlen(id
->string
) + 1;
3790 ret
= LTTNG_ERR_INVALID
;
3793 ret
= lttng_dynamic_buffer_append(&buf
, &id_hdr
, sizeof(id_hdr
));
3795 ret
= LTTNG_ERR_NOMEM
;
3798 ret
= lttng_dynamic_buffer_append(&buf
, id
->string
, var_data_len
);
3800 ret
= LTTNG_ERR_NOMEM
;
3805 cmd_header
.nb_tracker_id
= nr_ids
;
3806 ret
= setup_lttng_msg(cmd_ctx
, buf
.data
, buf
.size
, &cmd_header
,
3807 sizeof(cmd_header
));
3809 lttng_dynamic_buffer_reset(&buf
);
3817 case LTTNG_SET_CONSUMER_URI
:
3820 struct lttng_uri
*uris
;
3822 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3823 len
= nb_uri
* sizeof(struct lttng_uri
);
3826 ret
= LTTNG_ERR_INVALID
;
3830 uris
= zmalloc(len
);
3832 ret
= LTTNG_ERR_FATAL
;
3836 /* Receive variable len data */
3837 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3838 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3840 DBG("No URIs received from client... continuing");
3842 ret
= LTTNG_ERR_SESSION_FAIL
;
3847 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3849 if (ret
!= LTTNG_OK
) {
3856 case LTTNG_START_TRACE
:
3859 * On the first start, if we have a kernel session and we have
3860 * enabled time or size-based rotations, we have to make sure
3861 * the kernel tracer supports it.
3863 if (!cmd_ctx
->session
->has_been_started
&& \
3864 cmd_ctx
->session
->kernel_session
&& \
3865 (cmd_ctx
->session
->rotate_timer_period
|| \
3866 cmd_ctx
->session
->rotate_size
) && \
3867 !check_rotate_compatible()) {
3868 DBG("Kernel tracer version is not compatible with the rotation feature");
3869 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3872 ret
= cmd_start_trace(cmd_ctx
->session
);
3875 case LTTNG_STOP_TRACE
:
3877 ret
= cmd_stop_trace(cmd_ctx
->session
);
3880 case LTTNG_CREATE_SESSION
:
3883 struct lttng_uri
*uris
= NULL
;
3885 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3886 len
= nb_uri
* sizeof(struct lttng_uri
);
3889 uris
= zmalloc(len
);
3891 ret
= LTTNG_ERR_FATAL
;
3895 /* Receive variable len data */
3896 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3897 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3899 DBG("No URIs received from client... continuing");
3901 ret
= LTTNG_ERR_SESSION_FAIL
;
3906 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3907 DBG("Creating session with ONE network URI is a bad call");
3908 ret
= LTTNG_ERR_SESSION_FAIL
;
3914 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3915 &cmd_ctx
->creds
, 0);
3921 case LTTNG_DESTROY_SESSION
:
3923 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1],
3924 notification_thread_handle
);
3926 /* Set session to NULL so we do not unlock it after free. */
3927 cmd_ctx
->session
= NULL
;
3930 case LTTNG_LIST_DOMAINS
:
3933 struct lttng_domain
*domains
= NULL
;
3935 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3937 /* Return value is a negative lttng_error_code. */
3942 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3943 nb_dom
* sizeof(struct lttng_domain
));
3953 case LTTNG_LIST_CHANNELS
:
3955 ssize_t payload_size
;
3956 struct lttng_channel
*channels
= NULL
;
3958 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3959 cmd_ctx
->session
, &channels
);
3960 if (payload_size
< 0) {
3961 /* Return value is a negative lttng_error_code. */
3962 ret
= -payload_size
;
3966 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3977 case LTTNG_LIST_EVENTS
:
3980 struct lttng_event
*events
= NULL
;
3981 struct lttcomm_event_command_header cmd_header
;
3984 memset(&cmd_header
, 0, sizeof(cmd_header
));
3985 /* Extended infos are included at the end of events */
3986 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3987 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3988 &events
, &total_size
);
3991 /* Return value is a negative lttng_error_code. */
3996 cmd_header
.nb_events
= nb_event
;
3997 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3998 &cmd_header
, sizeof(cmd_header
));
4008 case LTTNG_LIST_SESSIONS
:
4010 unsigned int nr_sessions
;
4011 void *sessions_payload
;
4014 session_lock_list();
4015 nr_sessions
= lttng_sessions_count(
4016 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4017 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4018 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
4019 sessions_payload
= zmalloc(payload_len
);
4021 if (!sessions_payload
) {
4022 session_unlock_list();
4027 cmd_list_lttng_sessions(sessions_payload
,
4028 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4029 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4030 session_unlock_list();
4032 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
4034 free(sessions_payload
);
4043 case LTTNG_REGISTER_CONSUMER
:
4045 struct consumer_data
*cdata
;
4047 switch (cmd_ctx
->lsm
->domain
.type
) {
4048 case LTTNG_DOMAIN_KERNEL
:
4049 cdata
= &kconsumer_data
;
4052 ret
= LTTNG_ERR_UND
;
4056 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4057 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
4060 case LTTNG_DATA_PENDING
:
4063 uint8_t pending_ret_byte
;
4065 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
4070 * This function may returns 0 or 1 to indicate whether or not
4071 * there is data pending. In case of error, it should return an
4072 * LTTNG_ERR code. However, some code paths may still return
4073 * a nondescript error code, which we handle by returning an
4076 if (pending_ret
== 0 || pending_ret
== 1) {
4078 * ret will be set to LTTNG_OK at the end of
4081 } else if (pending_ret
< 0) {
4082 ret
= LTTNG_ERR_UNK
;
4089 pending_ret_byte
= (uint8_t) pending_ret
;
4091 /* 1 byte to return whether or not data is pending */
4092 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
4093 &pending_ret_byte
, 1);
4102 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
4104 struct lttcomm_lttng_output_id reply
;
4106 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
4107 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
4108 if (ret
!= LTTNG_OK
) {
4112 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
4118 /* Copy output list into message payload */
4122 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
4124 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
4125 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
4128 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
4131 struct lttng_snapshot_output
*outputs
= NULL
;
4133 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
4134 if (nb_output
< 0) {
4139 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4140 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4141 nb_output
* sizeof(struct lttng_snapshot_output
));
4151 case LTTNG_SNAPSHOT_RECORD
:
4153 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4154 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4155 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4158 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4161 struct lttng_uri
*uris
= NULL
;
4163 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4164 len
= nb_uri
* sizeof(struct lttng_uri
);
4167 uris
= zmalloc(len
);
4169 ret
= LTTNG_ERR_FATAL
;
4173 /* Receive variable len data */
4174 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4175 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4177 DBG("No URIs received from client... continuing");
4179 ret
= LTTNG_ERR_SESSION_FAIL
;
4184 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4185 DBG("Creating session with ONE network URI is a bad call");
4186 ret
= LTTNG_ERR_SESSION_FAIL
;
4192 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4193 nb_uri
, &cmd_ctx
->creds
);
4197 case LTTNG_CREATE_SESSION_LIVE
:
4200 struct lttng_uri
*uris
= NULL
;
4202 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4203 len
= nb_uri
* sizeof(struct lttng_uri
);
4206 uris
= zmalloc(len
);
4208 ret
= LTTNG_ERR_FATAL
;
4212 /* Receive variable len data */
4213 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4214 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4216 DBG("No URIs received from client... continuing");
4218 ret
= LTTNG_ERR_SESSION_FAIL
;
4223 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4224 DBG("Creating session with ONE network URI is a bad call");
4225 ret
= LTTNG_ERR_SESSION_FAIL
;
4231 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4232 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4236 case LTTNG_SAVE_SESSION
:
4238 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4242 case LTTNG_SET_SESSION_SHM_PATH
:
4244 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4245 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4248 case LTTNG_REGENERATE_METADATA
:
4250 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4253 case LTTNG_REGENERATE_STATEDUMP
:
4255 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4258 case LTTNG_REGISTER_TRIGGER
:
4260 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4261 notification_thread_handle
);
4264 case LTTNG_UNREGISTER_TRIGGER
:
4266 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4267 notification_thread_handle
);
4270 case LTTNG_ROTATE_SESSION
:
4272 struct lttng_rotate_session_return rotate_return
;
4274 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4276 memset(&rotate_return
, 0, sizeof(rotate_return
));
4277 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4278 DBG("Kernel tracer version is not compatible with the rotation feature");
4279 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4283 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4289 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4290 sizeof(rotate_return
));
4299 case LTTNG_ROTATION_GET_INFO
:
4301 struct lttng_rotation_get_info_return get_info_return
;
4303 memset(&get_info_return
, 0, sizeof(get_info_return
));
4304 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4305 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4311 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4312 sizeof(get_info_return
));
4321 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
4323 struct lttng_session_get_current_output_return output_return
;
4325 memset(&output_return
, 0, sizeof(output_return
));
4326 ret
= cmd_session_get_current_output(cmd_ctx
->session
,
4333 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &output_return
,
4334 sizeof(output_return
));
4343 case LTTNG_ROTATION_SET_SCHEDULE
:
4345 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4346 DBG("Kernel tracer version does not support session rotations");
4347 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4351 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4352 cmd_ctx
->lsm
->u
.rotate_setup
.timer_us
,
4353 cmd_ctx
->lsm
->u
.rotate_setup
.size
,
4354 notification_thread_handle
);
4355 if (ret
!= LTTNG_OK
) {
4361 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
4363 struct lttng_rotation_schedule_get_timer_period
*get_timer
;
4365 get_timer
= zmalloc(sizeof(struct lttng_rotation_schedule_get_timer_period
));
4370 get_timer
->rotate_timer
= cmd_ctx
->session
->rotate_timer_period
;
4372 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, get_timer
,
4373 sizeof(struct lttng_rotation_schedule_get_timer_period
));
4383 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
4385 struct lttng_rotation_schedule_get_size
*get_size
;
4387 get_size
= zmalloc(sizeof(struct lttng_rotation_schedule_get_size
));
4392 get_size
->rotate_size
= cmd_ctx
->session
->rotate_size
;
4394 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, get_size
,
4395 sizeof(struct lttng_rotation_schedule_get_size
));
4406 ret
= LTTNG_ERR_UND
;
4411 if (cmd_ctx
->llm
== NULL
) {
4412 DBG("Missing llm structure. Allocating one.");
4413 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4417 /* Set return code */
4418 cmd_ctx
->llm
->ret_code
= ret
;
4420 if (cmd_ctx
->session
) {
4421 session_unlock(cmd_ctx
->session
);
4423 if (need_tracing_session
) {
4424 session_unlock_list();
4427 assert(!rcu_read_ongoing());
4432 * Thread managing health check socket.
4434 static void *thread_manage_health(void *data
)
4436 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4437 uint32_t revents
, nb_fd
;
4438 struct lttng_poll_event events
;
4439 struct health_comm_msg msg
;
4440 struct health_comm_reply reply
;
4442 DBG("[thread] Manage health check started");
4444 rcu_register_thread();
4446 /* We might hit an error path before this is created. */
4447 lttng_poll_init(&events
);
4449 /* Create unix socket */
4450 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4452 ERR("Unable to create health check Unix socket");
4457 /* lttng health client socket path permissions */
4458 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4459 utils_get_group_id(config
.tracing_group_name
.value
));
4461 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4466 ret
= chmod(config
.health_unix_sock_path
.value
,
4467 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4469 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4476 * Set the CLOEXEC flag. Return code is useless because either way, the
4479 (void) utils_set_fd_cloexec(sock
);
4481 ret
= lttcomm_listen_unix_sock(sock
);
4487 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4488 * more will be added to this poll set.
4490 ret
= sessiond_set_thread_pollset(&events
, 2);
4495 /* Add the application registration socket */
4496 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4501 sessiond_notify_ready();
4504 DBG("Health check ready");
4506 /* Inifinite blocking call, waiting for transmission */
4508 ret
= lttng_poll_wait(&events
, -1);
4511 * Restart interrupted system call.
4513 if (errno
== EINTR
) {
4521 for (i
= 0; i
< nb_fd
; i
++) {
4522 /* Fetch once the poll data */
4523 revents
= LTTNG_POLL_GETEV(&events
, i
);
4524 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4527 /* No activity for this FD (poll implementation). */
4531 /* Thread quit pipe has been closed. Killing thread. */
4532 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4538 /* Event on the registration socket */
4539 if (pollfd
== sock
) {
4540 if (revents
& LPOLLIN
) {
4542 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4543 ERR("Health socket poll error");
4546 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4552 new_sock
= lttcomm_accept_unix_sock(sock
);
4558 * Set the CLOEXEC flag. Return code is useless because either way, the
4561 (void) utils_set_fd_cloexec(new_sock
);
4563 DBG("Receiving data from client for health...");
4564 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4566 DBG("Nothing recv() from client... continuing");
4567 ret
= close(new_sock
);
4574 rcu_thread_online();
4576 memset(&reply
, 0, sizeof(reply
));
4577 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4579 * health_check_state returns 0 if health is
4582 if (!health_check_state(health_sessiond
, i
)) {
4583 reply
.ret_code
|= 1ULL << i
;
4587 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4589 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4591 ERR("Failed to send health data back to client");
4594 /* End of transmission */
4595 ret
= close(new_sock
);
4604 ERR("Health error occurred in %s", __func__
);
4606 DBG("Health check thread dying");
4607 unlink(config
.health_unix_sock_path
.value
);
4615 lttng_poll_clean(&events
);
4617 rcu_unregister_thread();
4622 * This thread manage all clients request using the unix client socket for
4625 static void *thread_manage_clients(void *data
)
4627 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4629 uint32_t revents
, nb_fd
;
4630 struct command_ctx
*cmd_ctx
= NULL
;
4631 struct lttng_poll_event events
;
4633 DBG("[thread] Manage client started");
4635 rcu_register_thread();
4637 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4639 health_code_update();
4641 ret
= lttcomm_listen_unix_sock(client_sock
);
4647 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4648 * more will be added to this poll set.
4650 ret
= sessiond_set_thread_pollset(&events
, 2);
4652 goto error_create_poll
;
4655 /* Add the application registration socket */
4656 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4661 ret
= sem_post(&load_info
->message_thread_ready
);
4663 PERROR("sem_post message_thread_ready");
4668 * Wait until all support threads are initialized before accepting
4671 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4673 struct timeval timeout
;
4676 FD_SET(thread_quit_pipe
[0], &read_fds
);
4677 memset(&timeout
, 0, sizeof(timeout
));
4678 timeout
.tv_usec
= 1000;
4681 * If a support thread failed to launch, it may signal that
4682 * we must exit and the sessiond would never be marked as
4685 * The timeout is set to 1ms, which serves as a way to
4686 * pace down this check.
4688 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
4690 if (ret
> 0 || (ret
< 0 && errno
!= EINTR
)) {
4695 * This barrier is paired with the one in sessiond_notify_ready() to
4696 * ensure that loads accessing data initialized by the other threads,
4697 * on which this thread was waiting, are not performed before this point.
4699 * Note that this could be a 'read' memory barrier, but a full barrier
4700 * is used in case the code changes. The performance implications of
4701 * this choice are minimal since this is a slow path.
4705 /* This testpoint is after we signal readiness to the parent. */
4706 if (testpoint(sessiond_thread_manage_clients
)) {
4710 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4714 health_code_update();
4717 DBG("Accepting client command ...");
4719 /* Inifinite blocking call, waiting for transmission */
4721 health_poll_entry();
4722 ret
= lttng_poll_wait(&events
, -1);
4726 * Restart interrupted system call.
4728 if (errno
== EINTR
) {
4736 for (i
= 0; i
< nb_fd
; i
++) {
4737 /* Fetch once the poll data */
4738 revents
= LTTNG_POLL_GETEV(&events
, i
);
4739 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4741 health_code_update();
4744 /* No activity for this FD (poll implementation). */
4748 /* Thread quit pipe has been closed. Killing thread. */
4749 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4755 /* Event on the registration socket */
4756 if (pollfd
== client_sock
) {
4757 if (revents
& LPOLLIN
) {
4759 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4760 ERR("Client socket poll error");
4763 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4769 DBG("Wait for client response");
4771 health_code_update();
4773 sock
= lttcomm_accept_unix_sock(client_sock
);
4779 * Set the CLOEXEC flag. Return code is useless because either way, the
4782 (void) utils_set_fd_cloexec(sock
);
4784 /* Set socket option for credentials retrieval */
4785 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4790 /* Allocate context command to process the client request */
4791 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4792 if (cmd_ctx
== NULL
) {
4793 PERROR("zmalloc cmd_ctx");
4797 /* Allocate data buffer for reception */
4798 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4799 if (cmd_ctx
->lsm
== NULL
) {
4800 PERROR("zmalloc cmd_ctx->lsm");
4804 cmd_ctx
->llm
= NULL
;
4805 cmd_ctx
->session
= NULL
;
4807 health_code_update();
4810 * Data is received from the lttng client. The struct
4811 * lttcomm_session_msg (lsm) contains the command and data request of
4814 DBG("Receiving data from client ...");
4815 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4816 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4818 DBG("Nothing recv() from client... continuing");
4824 clean_command_ctx(&cmd_ctx
);
4828 health_code_update();
4830 // TODO: Validate cmd_ctx including sanity check for
4831 // security purpose.
4833 rcu_thread_online();
4835 * This function dispatch the work to the kernel or userspace tracer
4836 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4837 * informations for the client. The command context struct contains
4838 * everything this function may needs.
4840 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4841 rcu_thread_offline();
4849 * TODO: Inform client somehow of the fatal error. At
4850 * this point, ret < 0 means that a zmalloc failed
4851 * (ENOMEM). Error detected but still accept
4852 * command, unless a socket error has been
4855 clean_command_ctx(&cmd_ctx
);
4859 health_code_update();
4861 DBG("Sending response (size: %d, retcode: %s (%d))",
4862 cmd_ctx
->lttng_msg_size
,
4863 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4864 cmd_ctx
->llm
->ret_code
);
4865 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4867 ERR("Failed to send data back to client");
4870 /* End of transmission */
4877 clean_command_ctx(&cmd_ctx
);
4879 health_code_update();
4891 lttng_poll_clean(&events
);
4892 clean_command_ctx(&cmd_ctx
);
4896 unlink(config
.client_unix_sock_path
.value
);
4897 if (client_sock
>= 0) {
4898 ret
= close(client_sock
);
4906 ERR("Health error occurred in %s", __func__
);
4909 health_unregister(health_sessiond
);
4911 DBG("Client thread dying");
4913 rcu_unregister_thread();
4916 * Since we are creating the consumer threads, we own them, so we need
4917 * to join them before our thread exits.
4919 ret
= join_consumer_thread(&kconsumer_data
);
4922 PERROR("join_consumer");
4925 ret
= join_consumer_thread(&ustconsumer32_data
);
4928 PERROR("join_consumer ust32");
4931 ret
= join_consumer_thread(&ustconsumer64_data
);
4934 PERROR("join_consumer ust64");
4939 static int string_match(const char *str1
, const char *str2
)
4941 return (str1
&& str2
) && !strcmp(str1
, str2
);
4945 * Take an option from the getopt output and set it in the right variable to be
4948 * Return 0 on success else a negative value.
4950 static int set_option(int opt
, const char *arg
, const char *optname
)
4954 if (string_match(optname
, "client-sock") || opt
== 'c') {
4955 if (!arg
|| *arg
== '\0') {
4959 if (lttng_is_setuid_setgid()) {
4960 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4961 "-c, --client-sock");
4963 config_string_set(&config
.client_unix_sock_path
,
4965 if (!config
.client_unix_sock_path
.value
) {
4970 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4971 if (!arg
|| *arg
== '\0') {
4975 if (lttng_is_setuid_setgid()) {
4976 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4979 config_string_set(&config
.apps_unix_sock_path
,
4981 if (!config
.apps_unix_sock_path
.value
) {
4986 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4987 config
.daemonize
= true;
4988 } else if (string_match(optname
, "background") || opt
== 'b') {
4989 config
.background
= true;
4990 } else if (string_match(optname
, "group") || opt
== 'g') {
4991 if (!arg
|| *arg
== '\0') {
4995 if (lttng_is_setuid_setgid()) {
4996 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4999 config_string_set(&config
.tracing_group_name
,
5001 if (!config
.tracing_group_name
.value
) {
5006 } else if (string_match(optname
, "help") || opt
== 'h') {
5007 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
5009 ERR("Cannot show --help for `lttng-sessiond`");
5012 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
5013 } else if (string_match(optname
, "version") || opt
== 'V') {
5014 fprintf(stdout
, "%s\n", VERSION
);
5016 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
5017 config
.sig_parent
= true;
5018 } else if (string_match(optname
, "kconsumerd-err-sock")) {
5019 if (!arg
|| *arg
== '\0') {
5023 if (lttng_is_setuid_setgid()) {
5024 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5025 "--kconsumerd-err-sock");
5027 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
5029 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
5034 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
5035 if (!arg
|| *arg
== '\0') {
5039 if (lttng_is_setuid_setgid()) {
5040 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5041 "--kconsumerd-cmd-sock");
5043 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
5045 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
5050 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
5051 if (!arg
|| *arg
== '\0') {
5055 if (lttng_is_setuid_setgid()) {
5056 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5057 "--ustconsumerd64-err-sock");
5059 config_string_set(&config
.consumerd64_err_unix_sock_path
,
5061 if (!config
.consumerd64_err_unix_sock_path
.value
) {
5066 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
5067 if (!arg
|| *arg
== '\0') {
5071 if (lttng_is_setuid_setgid()) {
5072 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5073 "--ustconsumerd64-cmd-sock");
5075 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
5077 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
5082 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
5083 if (!arg
|| *arg
== '\0') {
5087 if (lttng_is_setuid_setgid()) {
5088 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5089 "--ustconsumerd32-err-sock");
5091 config_string_set(&config
.consumerd32_err_unix_sock_path
,
5093 if (!config
.consumerd32_err_unix_sock_path
.value
) {
5098 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
5099 if (!arg
|| *arg
== '\0') {
5103 if (lttng_is_setuid_setgid()) {
5104 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5105 "--ustconsumerd32-cmd-sock");
5107 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
5109 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
5114 } else if (string_match(optname
, "no-kernel")) {
5115 config
.no_kernel
= true;
5116 } else if (string_match(optname
, "quiet") || opt
== 'q') {
5117 config
.quiet
= true;
5118 } else if (string_match(optname
, "verbose") || opt
== 'v') {
5119 /* Verbose level can increase using multiple -v */
5121 /* Value obtained from config file */
5122 config
.verbose
= config_parse_value(arg
);
5124 /* -v used on command line */
5127 /* Clamp value to [0, 3] */
5128 config
.verbose
= config
.verbose
< 0 ? 0 :
5129 (config
.verbose
<= 3 ? config
.verbose
: 3);
5130 } else if (string_match(optname
, "verbose-consumer")) {
5132 config
.verbose_consumer
= config_parse_value(arg
);
5134 config
.verbose_consumer
++;
5136 } else if (string_match(optname
, "consumerd32-path")) {
5137 if (!arg
|| *arg
== '\0') {
5141 if (lttng_is_setuid_setgid()) {
5142 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5143 "--consumerd32-path");
5145 config_string_set(&config
.consumerd32_bin_path
,
5147 if (!config
.consumerd32_bin_path
.value
) {
5152 } else if (string_match(optname
, "consumerd32-libdir")) {
5153 if (!arg
|| *arg
== '\0') {
5157 if (lttng_is_setuid_setgid()) {
5158 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5159 "--consumerd32-libdir");
5161 config_string_set(&config
.consumerd32_lib_dir
,
5163 if (!config
.consumerd32_lib_dir
.value
) {
5168 } else if (string_match(optname
, "consumerd64-path")) {
5169 if (!arg
|| *arg
== '\0') {
5173 if (lttng_is_setuid_setgid()) {
5174 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5175 "--consumerd64-path");
5177 config_string_set(&config
.consumerd64_bin_path
,
5179 if (!config
.consumerd64_bin_path
.value
) {
5184 } else if (string_match(optname
, "consumerd64-libdir")) {
5185 if (!arg
|| *arg
== '\0') {
5189 if (lttng_is_setuid_setgid()) {
5190 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5191 "--consumerd64-libdir");
5193 config_string_set(&config
.consumerd64_lib_dir
,
5195 if (!config
.consumerd64_lib_dir
.value
) {
5200 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
5201 if (!arg
|| *arg
== '\0') {
5205 if (lttng_is_setuid_setgid()) {
5206 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5209 config_string_set(&config
.pid_file_path
, strdup(arg
));
5210 if (!config
.pid_file_path
.value
) {
5215 } else if (string_match(optname
, "agent-tcp-port")) {
5216 if (!arg
|| *arg
== '\0') {
5220 if (lttng_is_setuid_setgid()) {
5221 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5222 "--agent-tcp-port");
5227 v
= strtoul(arg
, NULL
, 0);
5228 if (errno
!= 0 || !isdigit(arg
[0])) {
5229 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5232 if (v
== 0 || v
>= 65535) {
5233 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5236 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
5237 DBG3("Agent TCP port set to non default: %i", (int) v
);
5239 } else if (string_match(optname
, "load") || opt
== 'l') {
5240 if (!arg
|| *arg
== '\0') {
5244 if (lttng_is_setuid_setgid()) {
5245 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5248 config_string_set(&config
.load_session_path
, strdup(arg
));
5249 if (!config
.load_session_path
.value
) {
5254 } else if (string_match(optname
, "kmod-probes")) {
5255 if (!arg
|| *arg
== '\0') {
5259 if (lttng_is_setuid_setgid()) {
5260 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5263 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
5264 if (!config
.kmod_probes_list
.value
) {
5269 } else if (string_match(optname
, "extra-kmod-probes")) {
5270 if (!arg
|| *arg
== '\0') {
5274 if (lttng_is_setuid_setgid()) {
5275 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5276 "--extra-kmod-probes");
5278 config_string_set(&config
.kmod_extra_probes_list
,
5280 if (!config
.kmod_extra_probes_list
.value
) {
5285 } else if (string_match(optname
, "config") || opt
== 'f') {
5286 /* This is handled in set_options() thus silent skip. */
5289 /* Unknown option or other error.
5290 * Error is printed by getopt, just return */
5295 if (ret
== -EINVAL
) {
5296 const char *opt_name
= "unknown";
5299 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5301 if (opt
== long_options
[i
].val
) {
5302 opt_name
= long_options
[i
].name
;
5307 WARN("Invalid argument provided for option \"%s\", using default value.",
5315 * config_entry_handler_cb used to handle options read from a config file.
5316 * See config_entry_handler_cb comment in common/config/session-config.h for the
5317 * return value conventions.
5319 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5323 if (!entry
|| !entry
->name
|| !entry
->value
) {
5328 /* Check if the option is to be ignored */
5329 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5330 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5335 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5338 /* Ignore if not fully matched. */
5339 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5344 * If the option takes no argument on the command line, we have to
5345 * check if the value is "true". We support non-zero numeric values,
5348 if (!long_options
[i
].has_arg
) {
5349 ret
= config_parse_value(entry
->value
);
5352 WARN("Invalid configuration value \"%s\" for option %s",
5353 entry
->value
, entry
->name
);
5355 /* False, skip boolean config option. */
5360 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5364 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5371 * daemon configuration loading and argument parsing
5373 static int set_options(int argc
, char **argv
)
5375 int ret
= 0, c
= 0, option_index
= 0;
5376 int orig_optopt
= optopt
, orig_optind
= optind
;
5378 const char *config_path
= NULL
;
5380 optstring
= utils_generate_optstring(long_options
,
5381 sizeof(long_options
) / sizeof(struct option
));
5387 /* Check for the --config option */
5388 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5389 &option_index
)) != -1) {
5393 } else if (c
!= 'f') {
5394 /* if not equal to --config option. */
5398 if (lttng_is_setuid_setgid()) {
5399 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5402 config_path
= utils_expand_path(optarg
);
5404 ERR("Failed to resolve path: %s", optarg
);
5409 ret
= config_get_section_entries(config_path
, config_section_name
,
5410 config_entry_handler
, NULL
);
5413 ERR("Invalid configuration option at line %i", ret
);
5419 /* Reset getopt's global state */
5420 optopt
= orig_optopt
;
5421 optind
= orig_optind
;
5425 * getopt_long() will not set option_index if it encounters a
5428 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5435 * Pass NULL as the long option name if popt left the index
5438 ret
= set_option(c
, optarg
,
5439 option_index
< 0 ? NULL
:
5440 long_options
[option_index
].name
);
5452 * Creates the two needed socket by the daemon.
5453 * apps_sock - The communication socket for all UST apps.
5454 * client_sock - The communication of the cli tool (lttng).
5456 static int init_daemon_socket(void)
5461 old_umask
= umask(0);
5463 /* Create client tool unix socket */
5464 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5465 if (client_sock
< 0) {
5466 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5471 /* Set the cloexec flag */
5472 ret
= utils_set_fd_cloexec(client_sock
);
5474 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5475 "Continuing but note that the consumer daemon will have a "
5476 "reference to this socket on exec()", client_sock
);
5479 /* File permission MUST be 660 */
5480 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5482 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5487 /* Create the application unix socket */
5488 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5489 if (apps_sock
< 0) {
5490 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5495 /* Set the cloexec flag */
5496 ret
= utils_set_fd_cloexec(apps_sock
);
5498 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5499 "Continuing but note that the consumer daemon will have a "
5500 "reference to this socket on exec()", apps_sock
);
5503 /* File permission MUST be 666 */
5504 ret
= chmod(config
.apps_unix_sock_path
.value
,
5505 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5507 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5512 DBG3("Session daemon client socket %d and application socket %d created",
5513 client_sock
, apps_sock
);
5521 * Create lockfile using the rundir and return its fd.
5523 static int create_lockfile(void)
5525 return utils_create_lock_file(config
.lock_file_path
.value
);
5529 * Check if the global socket is available, and if a daemon is answering at the
5530 * other side. If yes, error is returned.
5532 * Also attempts to create and hold the lock file.
5534 static int check_existing_daemon(void)
5538 /* Is there anybody out there ? */
5539 if (lttng_session_daemon_alive()) {
5544 lockfile_fd
= create_lockfile();
5545 if (lockfile_fd
< 0) {
5553 static void sessiond_cleanup_lock_file(void)
5558 * Cleanup lock file by deleting it and finaly closing it which will
5559 * release the file system lock.
5561 if (lockfile_fd
>= 0) {
5562 ret
= remove(config
.lock_file_path
.value
);
5564 PERROR("remove lock file");
5566 ret
= close(lockfile_fd
);
5568 PERROR("close lock file");
5574 * Set the tracing group gid onto the client socket.
5576 * Race window between mkdir and chown is OK because we are going from more
5577 * permissive (root.root) to less permissive (root.tracing).
5579 static int set_permissions(char *rundir
)
5584 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5586 /* Set lttng run dir */
5587 ret
= chown(rundir
, 0, gid
);
5589 ERR("Unable to set group on %s", rundir
);
5594 * Ensure all applications and tracing group can search the run
5595 * dir. Allow everyone to read the directory, since it does not
5596 * buy us anything to hide its content.
5598 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5600 ERR("Unable to set permissions on %s", rundir
);
5604 /* lttng client socket path */
5605 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5607 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5611 /* kconsumer error socket path */
5612 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5614 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5618 /* 64-bit ustconsumer error socket path */
5619 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5621 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5625 /* 32-bit ustconsumer compat32 error socket path */
5626 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5628 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5632 DBG("All permissions are set");
5638 * Create the lttng run directory needed for all global sockets and pipe.
5640 static int create_lttng_rundir(void)
5644 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5646 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5648 if (errno
!= EEXIST
) {
5649 ERR("Unable to create %s", config
.rundir
.value
);
5661 * Setup sockets and directory needed by the consumerds' communication with the
5664 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5669 switch (consumer_data
->type
) {
5670 case LTTNG_CONSUMER_KERNEL
:
5671 path
= config
.kconsumerd_path
.value
;
5673 case LTTNG_CONSUMER64_UST
:
5674 path
= config
.consumerd64_path
.value
;
5676 case LTTNG_CONSUMER32_UST
:
5677 path
= config
.consumerd32_path
.value
;
5680 ERR("Consumer type unknown");
5686 DBG2("Creating consumer directory: %s", path
);
5688 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5689 if (ret
< 0 && errno
!= EEXIST
) {
5691 ERR("Failed to create %s", path
);
5695 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5697 ERR("Unable to set group on %s", path
);
5703 /* Create the consumerd error unix socket */
5704 consumer_data
->err_sock
=
5705 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5706 if (consumer_data
->err_sock
< 0) {
5707 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5713 * Set the CLOEXEC flag. Return code is useless because either way, the
5716 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5718 PERROR("utils_set_fd_cloexec");
5719 /* continue anyway */
5722 /* File permission MUST be 660 */
5723 ret
= chmod(consumer_data
->err_unix_sock_path
,
5724 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5726 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5736 * Signal handler for the daemon
5738 * Simply stop all worker threads, leaving main() return gracefully after
5739 * joining all threads and calling cleanup().
5741 static void sighandler(int sig
)
5745 DBG("SIGINT caught");
5749 DBG("SIGTERM caught");
5753 CMM_STORE_SHARED(recv_child_signal
, 1);
5761 * Setup signal handler for :
5762 * SIGINT, SIGTERM, SIGPIPE
5764 static int set_signal_handler(void)
5767 struct sigaction sa
;
5770 if ((ret
= sigemptyset(&sigset
)) < 0) {
5771 PERROR("sigemptyset");
5775 sa
.sa_mask
= sigset
;
5778 sa
.sa_handler
= sighandler
;
5779 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5780 PERROR("sigaction");
5784 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5785 PERROR("sigaction");
5789 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5790 PERROR("sigaction");
5794 sa
.sa_handler
= SIG_IGN
;
5795 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5796 PERROR("sigaction");
5800 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5806 * Set open files limit to unlimited. This daemon can open a large number of
5807 * file descriptors in order to consume multiple kernel traces.
5809 static void set_ulimit(void)
5814 /* The kernel does not allow an infinite limit for open files */
5815 lim
.rlim_cur
= 65535;
5816 lim
.rlim_max
= 65535;
5818 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5820 PERROR("failed to set open files limit");
5824 static int write_pidfile(void)
5826 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5829 static int set_clock_plugin_env(void)
5832 char *env_value
= NULL
;
5834 if (!config
.lttng_ust_clock_plugin
.value
) {
5838 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5839 config
.lttng_ust_clock_plugin
.value
);
5845 ret
= putenv(env_value
);
5848 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5852 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5853 config
.lttng_ust_clock_plugin
.value
);
5859 struct rotation_thread_timer_queue
*create_rotate_timer_queue(void)
5861 struct rotation_thread_timer_queue
*queue
= NULL
;
5863 queue
= zmalloc(sizeof(struct rotation_thread_timer_queue
));
5865 PERROR("Failed to allocate timer rotate queue");
5869 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
5870 CDS_INIT_LIST_HEAD(&queue
->list
);
5871 pthread_mutex_init(&queue
->lock
, NULL
);
5878 void destroy_rotate_timer_queue(struct rotation_thread_timer_queue
*queue
)
5880 struct sessiond_rotation_timer
*node
, *tmp_node
;
5886 lttng_pipe_destroy(queue
->event_pipe
);
5888 pthread_mutex_lock(&queue
->lock
);
5889 /* Empty wait queue. */
5890 cds_list_for_each_entry_safe(node
, tmp_node
, &queue
->list
, head
) {
5891 cds_list_del(&node
->head
);
5894 pthread_mutex_unlock(&queue
->lock
);
5896 pthread_mutex_destroy(&queue
->lock
);
5903 int main(int argc
, char **argv
)
5905 int ret
= 0, retval
= 0;
5907 const char *env_app_timeout
;
5908 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5909 *ust64_channel_monitor_pipe
= NULL
,
5910 *kernel_channel_monitor_pipe
= NULL
;
5911 bool notification_thread_launched
= false;
5912 bool rotation_thread_launched
= false;
5913 bool timer_thread_launched
= false;
5914 struct lttng_pipe
*ust32_channel_rotate_pipe
= NULL
,
5915 *ust64_channel_rotate_pipe
= NULL
,
5916 *kernel_channel_rotate_pipe
= NULL
;
5917 struct timer_thread_parameters timer_thread_ctx
;
5918 /* Queue of rotation jobs populated by the sessiond-timer. */
5919 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5920 sem_t notification_thread_ready
;
5922 init_kernel_workarounds();
5924 rcu_register_thread();
5926 if (set_signal_handler()) {
5928 goto exit_set_signal_handler
;
5931 if (sessiond_timer_signal_init()) {
5933 goto exit_set_signal_handler
;
5936 page_size
= sysconf(_SC_PAGESIZE
);
5937 if (page_size
< 0) {
5938 PERROR("sysconf _SC_PAGESIZE");
5939 page_size
= LONG_MAX
;
5940 WARN("Fallback page size to %ld", page_size
);
5943 ret
= sessiond_config_init(&config
);
5946 goto exit_set_signal_handler
;
5950 * Init config from environment variables.
5951 * Command line option override env configuration per-doc. Do env first.
5953 sessiond_config_apply_env_config(&config
);
5956 * Parse arguments and load the daemon configuration file.
5958 * We have an exit_options exit path to free memory reserved by
5959 * set_options. This is needed because the rest of sessiond_cleanup()
5960 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5961 * depends on set_options.
5964 if (set_options(argc
, argv
)) {
5970 * Resolve all paths received as arguments, configuration option, or
5971 * through environment variable as absolute paths. This is necessary
5972 * since daemonizing causes the sessiond's current working directory
5975 ret
= sessiond_config_resolve_paths(&config
);
5981 lttng_opt_verbose
= config
.verbose
;
5982 lttng_opt_quiet
= config
.quiet
;
5983 kconsumer_data
.err_unix_sock_path
=
5984 config
.kconsumerd_err_unix_sock_path
.value
;
5985 kconsumer_data
.cmd_unix_sock_path
=
5986 config
.kconsumerd_cmd_unix_sock_path
.value
;
5987 ustconsumer32_data
.err_unix_sock_path
=
5988 config
.consumerd32_err_unix_sock_path
.value
;
5989 ustconsumer32_data
.cmd_unix_sock_path
=
5990 config
.consumerd32_cmd_unix_sock_path
.value
;
5991 ustconsumer64_data
.err_unix_sock_path
=
5992 config
.consumerd64_err_unix_sock_path
.value
;
5993 ustconsumer64_data
.cmd_unix_sock_path
=
5994 config
.consumerd64_cmd_unix_sock_path
.value
;
5995 set_clock_plugin_env();
5997 sessiond_config_log(&config
);
5999 if (create_lttng_rundir()) {
6004 /* Abort launch if a session daemon is already running. */
6005 if (check_existing_daemon()) {
6006 ERR("A session daemon is already running.");
6012 if (config
.daemonize
|| config
.background
) {
6015 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
6016 !config
.background
);
6023 * We are in the child. Make sure all other file descriptors are
6024 * closed, in case we are called with more opened file
6025 * descriptors than the standard ones and the lock file.
6027 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
6028 if (i
== lockfile_fd
) {
6035 if (run_as_create_worker(argv
[0]) < 0) {
6036 goto exit_create_run_as_worker_cleanup
;
6040 * Starting from here, we can create threads. This needs to be after
6041 * lttng_daemonize due to RCU.
6045 * Initialize the health check subsystem. This call should set the
6046 * appropriate time values.
6048 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
6049 if (!health_sessiond
) {
6050 PERROR("health_app_create error");
6052 goto exit_health_sessiond_cleanup
;
6055 /* Create thread to clean up RCU hash tables */
6056 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
6058 goto exit_ht_cleanup
;
6061 /* Create thread quit pipe */
6062 if (init_thread_quit_pipe()) {
6064 goto exit_init_data
;
6067 /* Check if daemon is UID = 0 */
6068 is_root
= !getuid();
6070 /* Create global run dir with root access */
6072 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
6073 if (!kernel_channel_monitor_pipe
) {
6074 ERR("Failed to create kernel consumer channel monitor pipe");
6076 goto exit_init_data
;
6078 kconsumer_data
.channel_monitor_pipe
=
6079 lttng_pipe_release_writefd(
6080 kernel_channel_monitor_pipe
);
6081 if (kconsumer_data
.channel_monitor_pipe
< 0) {
6083 goto exit_init_data
;
6085 kernel_channel_rotate_pipe
= lttng_pipe_open(0);
6086 if (!kernel_channel_rotate_pipe
) {
6087 ERR("Failed to create kernel consumer channel rotate pipe");
6089 goto exit_init_data
;
6091 kconsumer_data
.channel_rotate_pipe
=
6092 lttng_pipe_release_writefd(
6093 kernel_channel_rotate_pipe
);
6094 if (kconsumer_data
.channel_rotate_pipe
< 0) {
6096 goto exit_init_data
;
6100 /* Set consumer initial state */
6101 kernel_consumerd_state
= CONSUMER_STOPPED
;
6102 ust_consumerd_state
= CONSUMER_STOPPED
;
6104 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
6105 if (!ust32_channel_monitor_pipe
) {
6106 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
6108 goto exit_init_data
;
6110 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6111 ust32_channel_monitor_pipe
);
6112 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
6114 goto exit_init_data
;
6116 ust32_channel_rotate_pipe
= lttng_pipe_open(0);
6117 if (!ust32_channel_rotate_pipe
) {
6118 ERR("Failed to create 32-bit user space consumer channel rotate pipe");
6120 goto exit_init_data
;
6122 ustconsumer32_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6123 ust32_channel_rotate_pipe
);
6124 if (ustconsumer32_data
.channel_rotate_pipe
< 0) {
6126 goto exit_init_data
;
6130 * The rotation_timer_queue structure is shared between the sessiond timer
6131 * thread and the rotation thread. The main() keeps the ownership and
6132 * destroys it when both threads have quit.
6134 rotation_timer_queue
= create_rotate_timer_queue();
6135 if (!rotation_timer_queue
) {
6137 goto exit_init_data
;
6139 timer_thread_ctx
.rotation_timer_queue
= rotation_timer_queue
;
6141 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
6142 if (!ust64_channel_monitor_pipe
) {
6143 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
6145 goto exit_init_data
;
6147 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6148 ust64_channel_monitor_pipe
);
6149 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
6151 goto exit_init_data
;
6153 ust64_channel_rotate_pipe
= lttng_pipe_open(0);
6154 if (!ust64_channel_rotate_pipe
) {
6155 ERR("Failed to create 64-bit user space consumer channel rotate pipe");
6157 goto exit_init_data
;
6159 ustconsumer64_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6160 ust64_channel_rotate_pipe
);
6161 if (ustconsumer64_data
.channel_rotate_pipe
< 0) {
6163 goto exit_init_data
;
6167 * Init UST app hash table. Alloc hash table before this point since
6168 * cleanup() can get called after that point.
6170 if (ust_app_ht_alloc()) {
6171 ERR("Failed to allocate UST app hash table");
6173 goto exit_init_data
;
6177 * Initialize agent app hash table. We allocate the hash table here
6178 * since cleanup() can get called after this point.
6180 if (agent_app_ht_alloc()) {
6181 ERR("Failed to allocate Agent app hash table");
6183 goto exit_init_data
;
6187 * These actions must be executed as root. We do that *after* setting up
6188 * the sockets path because we MUST make the check for another daemon using
6189 * those paths *before* trying to set the kernel consumer sockets and init
6193 if (set_consumer_sockets(&kconsumer_data
)) {
6195 goto exit_init_data
;
6198 /* Setup kernel tracer */
6199 if (!config
.no_kernel
) {
6200 init_kernel_tracer();
6201 if (kernel_tracer_fd
>= 0) {
6202 ret
= syscall_init_table();
6204 ERR("Unable to populate syscall table. "
6205 "Syscall tracing won't work "
6206 "for this session daemon.");
6211 /* Set ulimit for open files */
6214 /* init lttng_fd tracking must be done after set_ulimit. */
6217 if (set_consumer_sockets(&ustconsumer64_data
)) {
6219 goto exit_init_data
;
6222 if (set_consumer_sockets(&ustconsumer32_data
)) {
6224 goto exit_init_data
;
6227 /* Setup the needed unix socket */
6228 if (init_daemon_socket()) {
6230 goto exit_init_data
;
6233 /* Set credentials to socket */
6234 if (is_root
&& set_permissions(config
.rundir
.value
)) {
6236 goto exit_init_data
;
6239 /* Get parent pid if -S, --sig-parent is specified. */
6240 if (config
.sig_parent
) {
6244 /* Setup the kernel pipe for waking up the kernel thread */
6245 if (is_root
&& !config
.no_kernel
) {
6246 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6248 goto exit_init_data
;
6252 /* Setup the thread apps communication pipe. */
6253 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6255 goto exit_init_data
;
6258 /* Setup the thread apps notify communication pipe. */
6259 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6261 goto exit_init_data
;
6264 /* Initialize global buffer per UID and PID registry. */
6265 buffer_reg_init_uid_registry();
6266 buffer_reg_init_pid_registry();
6268 /* Init UST command queue. */
6269 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6272 * Get session list pointer. This pointer MUST NOT be free'd. This list
6273 * is statically declared in session.c
6275 session_list_ptr
= session_get_list();
6279 /* Check for the application socket timeout env variable. */
6280 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6281 if (env_app_timeout
) {
6282 config
.app_socket_timeout
= atoi(env_app_timeout
);
6284 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6287 ret
= write_pidfile();
6289 ERR("Error in write_pidfile");
6291 goto exit_init_data
;
6294 /* Initialize communication library */
6296 /* Initialize TCP timeout values */
6297 lttcomm_inet_init();
6299 if (load_session_init_data(&load_info
) < 0) {
6301 goto exit_init_data
;
6303 load_info
->path
= config
.load_session_path
.value
;
6305 /* Create health-check thread. */
6306 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6307 thread_manage_health
, (void *) NULL
);
6310 PERROR("pthread_create health");
6316 * The rotation thread needs the notification thread to be ready before
6317 * creating the rotate_notification_channel, so we use this semaphore as
6318 * a rendez-vous point.
6320 sem_init(¬ification_thread_ready
, 0, 0);
6322 /* notification_thread_data acquires the pipes' read side. */
6323 notification_thread_handle
= notification_thread_handle_create(
6324 ust32_channel_monitor_pipe
,
6325 ust64_channel_monitor_pipe
,
6326 kernel_channel_monitor_pipe
,
6327 ¬ification_thread_ready
);
6328 if (!notification_thread_handle
) {
6330 ERR("Failed to create notification thread shared data");
6332 goto exit_notification
;
6335 /* Create notification thread. */
6336 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6337 thread_notification
, notification_thread_handle
);
6340 PERROR("pthread_create notification");
6343 goto exit_notification
;
6345 notification_thread_launched
= true;
6347 /* Create timer thread. */
6348 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
6349 sessiond_timer_thread
, &timer_thread_ctx
);
6352 PERROR("pthread_create timer");
6355 goto exit_notification
;
6357 timer_thread_launched
= true;
6359 /* rotation_thread_data acquires the pipes' read side. */
6360 rotation_thread_handle
= rotation_thread_handle_create(
6361 ust32_channel_rotate_pipe
,
6362 ust64_channel_rotate_pipe
,
6363 kernel_channel_rotate_pipe
,
6364 thread_quit_pipe
[0],
6365 rotation_timer_queue
,
6366 notification_thread_handle
,
6367 ¬ification_thread_ready
);
6368 if (!rotation_thread_handle
) {
6370 ERR("Failed to create rotation thread shared data");
6375 /* Create rotation thread. */
6376 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6377 thread_rotation
, rotation_thread_handle
);
6380 PERROR("pthread_create rotation");
6385 rotation_thread_launched
= true;
6387 /* Create thread to manage the client socket */
6388 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6389 thread_manage_clients
, (void *) NULL
);
6392 PERROR("pthread_create clients");
6398 /* Create thread to dispatch registration */
6399 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6400 thread_dispatch_ust_registration
, (void *) NULL
);
6403 PERROR("pthread_create dispatch");
6409 /* Create thread to manage application registration. */
6410 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6411 thread_registration_apps
, (void *) NULL
);
6414 PERROR("pthread_create registration");
6420 /* Create thread to manage application socket */
6421 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6422 thread_manage_apps
, (void *) NULL
);
6425 PERROR("pthread_create apps");
6431 /* Create thread to manage application notify socket */
6432 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6433 ust_thread_manage_notify
, (void *) NULL
);
6436 PERROR("pthread_create notify");
6439 goto exit_apps_notify
;
6442 /* Create agent registration thread. */
6443 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6444 agent_thread_manage_registration
, (void *) NULL
);
6447 PERROR("pthread_create agent");
6450 goto exit_agent_reg
;
6453 /* Don't start this thread if kernel tracing is not requested nor root */
6454 if (is_root
&& !config
.no_kernel
) {
6455 /* Create kernel thread to manage kernel event */
6456 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6457 thread_manage_kernel
, (void *) NULL
);
6460 PERROR("pthread_create kernel");
6467 /* Create session loading thread. */
6468 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6469 thread_load_session
, load_info
);
6472 PERROR("pthread_create load_session_thread");
6475 goto exit_load_session
;
6479 * This is where we start awaiting program completion (e.g. through
6480 * signal that asks threads to teardown).
6483 ret
= pthread_join(load_session_thread
, &status
);
6486 PERROR("pthread_join load_session_thread");
6491 if (is_root
&& !config
.no_kernel
) {
6492 ret
= pthread_join(kernel_thread
, &status
);
6495 PERROR("pthread_join");
6501 ret
= pthread_join(agent_reg_thread
, &status
);
6504 PERROR("pthread_join agent");
6509 ret
= pthread_join(apps_notify_thread
, &status
);
6512 PERROR("pthread_join apps notify");
6517 ret
= pthread_join(apps_thread
, &status
);
6520 PERROR("pthread_join apps");
6525 ret
= pthread_join(reg_apps_thread
, &status
);
6528 PERROR("pthread_join");
6534 * Join dispatch thread after joining reg_apps_thread to ensure
6535 * we don't leak applications in the queue.
6537 ret
= pthread_join(dispatch_thread
, &status
);
6540 PERROR("pthread_join");
6545 ret
= pthread_join(client_thread
, &status
);
6548 PERROR("pthread_join");
6555 sem_destroy(¬ification_thread_ready
);
6556 ret
= pthread_join(health_thread
, &status
);
6559 PERROR("pthread_join health thread");
6566 * Wait for all pending call_rcu work to complete before tearing
6567 * down data structures. call_rcu worker may be trying to
6568 * perform lookups in those structures.
6572 * sessiond_cleanup() is called when no other thread is running, except
6573 * the ht_cleanup thread, which is needed to destroy the hash tables.
6575 rcu_thread_online();
6579 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6580 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6581 * the queue is empty before shutting down the clean-up thread.
6586 * The teardown of the notification system is performed after the
6587 * session daemon's teardown in order to allow it to be notified
6588 * of the active session and channels at the moment of the teardown.
6590 if (notification_thread_handle
) {
6591 if (notification_thread_launched
) {
6592 notification_thread_command_quit(
6593 notification_thread_handle
);
6594 ret
= pthread_join(notification_thread
, &status
);
6597 PERROR("pthread_join notification thread");
6601 notification_thread_handle_destroy(notification_thread_handle
);
6604 if (rotation_thread_handle
) {
6605 if (rotation_thread_launched
) {
6606 ret
= pthread_join(rotation_thread
, &status
);
6609 PERROR("pthread_join rotation thread");
6613 rotation_thread_handle_destroy(rotation_thread_handle
);
6616 if (timer_thread_launched
) {
6617 kill(getpid(), LTTNG_SESSIOND_SIG_EXIT
);
6618 ret
= pthread_join(timer_thread
, &status
);
6621 PERROR("pthread_join timer thread");
6627 * After the rotation and timer thread have quit, we can safely destroy
6628 * the rotation_timer_queue.
6630 destroy_rotate_timer_queue(rotation_timer_queue
);
6632 rcu_thread_offline();
6633 rcu_unregister_thread();
6635 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6639 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6640 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6641 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6642 lttng_pipe_destroy(ust32_channel_rotate_pipe
);
6643 lttng_pipe_destroy(ust64_channel_rotate_pipe
);
6644 lttng_pipe_destroy(kernel_channel_rotate_pipe
);
6647 health_app_destroy(health_sessiond
);
6648 exit_health_sessiond_cleanup
:
6649 exit_create_run_as_worker_cleanup
:
6652 sessiond_cleanup_lock_file();
6653 sessiond_cleanup_options();
6655 exit_set_signal_handler
: