2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
53 #include "lttng-sessiond.h"
54 #include "buffer-registry.h"
61 #include "kernel-consumer.h"
65 #include "ust-consumer.h"
68 #include "health-sessiond.h"
69 #include "testpoint.h"
70 #include "ust-thread.h"
71 #include "agent-thread.h"
73 #include "load-session-thread.h"
74 #include "notification-thread.h"
75 #include "notification-thread-commands.h"
76 #include "rotation-thread.h"
79 #include "ht-cleanup.h"
80 #include "sessiond-config.h"
81 #include "sessiond-timer.h"
83 static const char *help_msg
=
84 #ifdef LTTNG_EMBED_HELP
85 #include <lttng-sessiond.8.h>
92 static pid_t ppid
; /* Parent PID for --sig-parent option */
93 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
94 static int lockfile_fd
= -1;
96 /* Set to 1 when a SIGUSR1 signal is received. */
97 static int recv_child_signal
;
99 static struct lttng_kernel_tracer_version kernel_tracer_version
;
100 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
103 * Consumer daemon specific control data. Every value not initialized here is
104 * set to 0 by the static definition.
106 static struct consumer_data kconsumer_data
= {
107 .type
= LTTNG_CONSUMER_KERNEL
,
110 .channel_monitor_pipe
= -1,
111 .channel_rotate_pipe
= -1,
112 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
113 .lock
= PTHREAD_MUTEX_INITIALIZER
,
114 .cond
= PTHREAD_COND_INITIALIZER
,
115 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
117 static struct consumer_data ustconsumer64_data
= {
118 .type
= LTTNG_CONSUMER64_UST
,
121 .channel_monitor_pipe
= -1,
122 .channel_rotate_pipe
= -1,
123 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
124 .lock
= PTHREAD_MUTEX_INITIALIZER
,
125 .cond
= PTHREAD_COND_INITIALIZER
,
126 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
128 static struct consumer_data ustconsumer32_data
= {
129 .type
= LTTNG_CONSUMER32_UST
,
132 .channel_monitor_pipe
= -1,
133 .channel_rotate_pipe
= -1,
134 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
135 .lock
= PTHREAD_MUTEX_INITIALIZER
,
136 .cond
= PTHREAD_COND_INITIALIZER
,
137 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
140 /* Command line options */
141 static const struct option long_options
[] = {
142 { "client-sock", required_argument
, 0, 'c' },
143 { "apps-sock", required_argument
, 0, 'a' },
144 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
145 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
146 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
147 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
148 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
149 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
150 { "consumerd32-path", required_argument
, 0, '\0' },
151 { "consumerd32-libdir", required_argument
, 0, '\0' },
152 { "consumerd64-path", required_argument
, 0, '\0' },
153 { "consumerd64-libdir", required_argument
, 0, '\0' },
154 { "daemonize", no_argument
, 0, 'd' },
155 { "background", no_argument
, 0, 'b' },
156 { "sig-parent", no_argument
, 0, 'S' },
157 { "help", no_argument
, 0, 'h' },
158 { "group", required_argument
, 0, 'g' },
159 { "version", no_argument
, 0, 'V' },
160 { "quiet", no_argument
, 0, 'q' },
161 { "verbose", no_argument
, 0, 'v' },
162 { "verbose-consumer", no_argument
, 0, '\0' },
163 { "no-kernel", no_argument
, 0, '\0' },
164 { "pidfile", required_argument
, 0, 'p' },
165 { "agent-tcp-port", required_argument
, 0, '\0' },
166 { "config", required_argument
, 0, 'f' },
167 { "load", required_argument
, 0, 'l' },
168 { "kmod-probes", required_argument
, 0, '\0' },
169 { "extra-kmod-probes", required_argument
, 0, '\0' },
173 struct sessiond_config config
;
175 /* Command line options to ignore from configuration file */
176 static const char *config_ignore_options
[] = { "help", "version", "config" };
178 /* Shared between threads */
179 static int dispatch_thread_exit
;
181 /* Sockets and FDs */
182 static int client_sock
= -1;
183 static int apps_sock
= -1;
184 int kernel_tracer_fd
= -1;
185 static int kernel_poll_pipe
[2] = { -1, -1 };
188 * Quit pipe for all threads. This permits a single cancellation point
189 * for all threads when receiving an event on the pipe.
191 static int thread_quit_pipe
[2] = { -1, -1 };
194 * This pipe is used to inform the thread managing application communication
195 * that a command is queued and ready to be processed.
197 static int apps_cmd_pipe
[2] = { -1, -1 };
199 int apps_cmd_notify_pipe
[2] = { -1, -1 };
201 /* Pthread, Mutexes and Semaphores */
202 static pthread_t apps_thread
;
203 static pthread_t apps_notify_thread
;
204 static pthread_t reg_apps_thread
;
205 static pthread_t client_thread
;
206 static pthread_t kernel_thread
;
207 static pthread_t dispatch_thread
;
208 static pthread_t health_thread
;
209 static pthread_t ht_cleanup_thread
;
210 static pthread_t agent_reg_thread
;
211 static pthread_t load_session_thread
;
212 static pthread_t notification_thread
;
213 static pthread_t rotation_thread
;
214 static pthread_t timer_thread
;
217 * UST registration command queue. This queue is tied with a futex and uses a N
218 * wakers / 1 waiter implemented and detailed in futex.c/.h
220 * The thread_registration_apps and thread_dispatch_ust_registration uses this
221 * queue along with the wait/wake scheme. The thread_manage_apps receives down
222 * the line new application socket and monitors it for any I/O error or clean
223 * close that triggers an unregistration of the application.
225 static struct ust_cmd_queue ust_cmd_queue
;
228 * Pointer initialized before thread creation.
230 * This points to the tracing session list containing the session count and a
231 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
232 * MUST NOT be taken if you call a public function in session.c.
234 * The lock is nested inside the structure: session_list_ptr->lock. Please use
235 * session_lock_list and session_unlock_list for lock acquisition.
237 static struct ltt_session_list
*session_list_ptr
;
239 int ust_consumerd64_fd
= -1;
240 int ust_consumerd32_fd
= -1;
242 static const char *module_proc_lttng
= "/proc/lttng";
245 * Consumer daemon state which is changed when spawning it, killing it or in
246 * case of a fatal error.
248 enum consumerd_state
{
249 CONSUMER_STARTED
= 1,
250 CONSUMER_STOPPED
= 2,
255 * This consumer daemon state is used to validate if a client command will be
256 * able to reach the consumer. If not, the client is informed. For instance,
257 * doing a "lttng start" when the consumer state is set to ERROR will return an
258 * error to the client.
260 * The following example shows a possible race condition of this scheme:
262 * consumer thread error happens
264 * client cmd checks state -> still OK
265 * consumer thread exit, sets error
266 * client cmd try to talk to consumer
269 * However, since the consumer is a different daemon, we have no way of making
270 * sure the command will reach it safely even with this state flag. This is why
271 * we consider that up to the state validation during command processing, the
272 * command is safe. After that, we can not guarantee the correctness of the
273 * client request vis-a-vis the consumer.
275 static enum consumerd_state ust_consumerd_state
;
276 static enum consumerd_state kernel_consumerd_state
;
278 /* Set in main() with the current page size. */
281 /* Application health monitoring */
282 struct health_app
*health_sessiond
;
284 /* Am I root or not. */
285 int is_root
; /* Set to 1 if the daemon is running as root */
287 const char * const config_section_name
= "sessiond";
289 /* Load session thread information to operate. */
290 struct load_session_thread_data
*load_info
;
292 /* Notification thread handle. */
293 struct notification_thread_handle
*notification_thread_handle
;
295 /* Rotation thread handle. */
296 struct rotation_thread_handle
*rotation_thread_handle
;
298 /* Global hash tables */
299 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
302 * Whether sessiond is ready for commands/notification channel/health check
304 * NR_LTTNG_SESSIOND_READY must match the number of calls to
305 * sessiond_notify_ready().
307 #define NR_LTTNG_SESSIOND_READY 5
308 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
310 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
312 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
315 /* Notify parents that we are ready for cmd and health check */
317 void sessiond_notify_ready(void)
319 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
321 * Notify parent pid that we are ready to accept command
322 * for client side. This ppid is the one from the
323 * external process that spawned us.
325 if (config
.sig_parent
) {
330 * Notify the parent of the fork() process that we are
333 if (config
.daemonize
|| config
.background
) {
334 kill(child_ppid
, SIGUSR1
);
340 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
347 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
353 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
365 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
367 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
369 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
373 * Init thread quit pipe.
375 * Return -1 on error or 0 if all pipes are created.
377 static int __init_thread_quit_pipe(int *a_pipe
)
383 PERROR("thread quit pipe");
387 for (i
= 0; i
< 2; i
++) {
388 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
399 static int init_thread_quit_pipe(void)
401 return __init_thread_quit_pipe(thread_quit_pipe
);
405 * Stop all threads by closing the thread quit pipe.
407 static void stop_threads(void)
411 /* Stopping all threads */
412 DBG("Terminating all threads");
413 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
415 ERR("write error on thread quit pipe");
418 /* Dispatch thread */
419 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
420 futex_nto1_wake(&ust_cmd_queue
.futex
);
424 * Close every consumer sockets.
426 static void close_consumer_sockets(void)
430 if (kconsumer_data
.err_sock
>= 0) {
431 ret
= close(kconsumer_data
.err_sock
);
433 PERROR("kernel consumer err_sock close");
436 if (ustconsumer32_data
.err_sock
>= 0) {
437 ret
= close(ustconsumer32_data
.err_sock
);
439 PERROR("UST consumerd32 err_sock close");
442 if (ustconsumer64_data
.err_sock
>= 0) {
443 ret
= close(ustconsumer64_data
.err_sock
);
445 PERROR("UST consumerd64 err_sock close");
448 if (kconsumer_data
.cmd_sock
>= 0) {
449 ret
= close(kconsumer_data
.cmd_sock
);
451 PERROR("kernel consumer cmd_sock close");
454 if (ustconsumer32_data
.cmd_sock
>= 0) {
455 ret
= close(ustconsumer32_data
.cmd_sock
);
457 PERROR("UST consumerd32 cmd_sock close");
460 if (ustconsumer64_data
.cmd_sock
>= 0) {
461 ret
= close(ustconsumer64_data
.cmd_sock
);
463 PERROR("UST consumerd64 cmd_sock close");
466 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
467 ret
= close(kconsumer_data
.channel_monitor_pipe
);
469 PERROR("kernel consumer channel monitor pipe close");
472 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
473 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
475 PERROR("UST consumerd32 channel monitor pipe close");
478 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
479 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
481 PERROR("UST consumerd64 channel monitor pipe close");
484 if (kconsumer_data
.channel_rotate_pipe
>= 0) {
485 ret
= close(kconsumer_data
.channel_rotate_pipe
);
487 PERROR("kernel consumer channel rotate pipe close");
490 if (ustconsumer32_data
.channel_rotate_pipe
>= 0) {
491 ret
= close(ustconsumer32_data
.channel_rotate_pipe
);
493 PERROR("UST consumerd32 channel rotate pipe close");
496 if (ustconsumer64_data
.channel_rotate_pipe
>= 0) {
497 ret
= close(ustconsumer64_data
.channel_rotate_pipe
);
499 PERROR("UST consumerd64 channel rotate pipe close");
505 * Wait on consumer process termination.
507 * Need to be called with the consumer data lock held or from a context
508 * ensuring no concurrent access to data (e.g: cleanup).
510 static void wait_consumer(struct consumer_data
*consumer_data
)
515 if (consumer_data
->pid
<= 0) {
519 DBG("Waiting for complete teardown of consumerd (PID: %d)",
521 ret
= waitpid(consumer_data
->pid
, &status
, 0);
523 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
524 } else if (!WIFEXITED(status
)) {
525 ERR("consumerd termination with error: %d",
528 consumer_data
->pid
= 0;
532 * Cleanup the session daemon's data structures.
534 static void sessiond_cleanup(void)
537 struct ltt_session
*sess
, *stmp
;
539 DBG("Cleanup sessiond");
542 * Close the thread quit pipe. It has already done its job,
543 * since we are now called.
545 utils_close_pipe(thread_quit_pipe
);
548 * If config.pid_file_path.value is undefined, the default file will be
549 * wiped when removing the rundir.
551 if (config
.pid_file_path
.value
) {
552 ret
= remove(config
.pid_file_path
.value
);
554 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
558 DBG("Removing sessiond and consumerd content of directory %s",
559 config
.rundir
.value
);
562 DBG("Removing %s", config
.pid_file_path
.value
);
563 (void) unlink(config
.pid_file_path
.value
);
565 DBG("Removing %s", config
.agent_port_file_path
.value
);
566 (void) unlink(config
.agent_port_file_path
.value
);
569 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
570 (void) unlink(kconsumer_data
.err_unix_sock_path
);
572 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
573 (void) rmdir(config
.kconsumerd_path
.value
);
575 /* ust consumerd 32 */
576 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
577 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
579 DBG("Removing directory %s", config
.consumerd32_path
.value
);
580 (void) rmdir(config
.consumerd32_path
.value
);
582 /* ust consumerd 64 */
583 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
584 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
586 DBG("Removing directory %s", config
.consumerd64_path
.value
);
587 (void) rmdir(config
.consumerd64_path
.value
);
589 DBG("Cleaning up all sessions");
591 /* Destroy session list mutex */
592 if (session_list_ptr
!= NULL
) {
593 pthread_mutex_destroy(&session_list_ptr
->lock
);
595 /* Cleanup ALL session */
596 cds_list_for_each_entry_safe(sess
, stmp
,
597 &session_list_ptr
->head
, list
) {
598 cmd_destroy_session(sess
, kernel_poll_pipe
[1],
599 notification_thread_handle
);
603 wait_consumer(&kconsumer_data
);
604 wait_consumer(&ustconsumer64_data
);
605 wait_consumer(&ustconsumer32_data
);
607 DBG("Cleaning up all agent apps");
608 agent_app_ht_clean();
610 DBG("Closing all UST sockets");
611 ust_app_clean_list();
612 buffer_reg_destroy_registries();
614 if (is_root
&& !config
.no_kernel
) {
615 DBG2("Closing kernel fd");
616 if (kernel_tracer_fd
>= 0) {
617 ret
= close(kernel_tracer_fd
);
622 DBG("Unloading kernel modules");
623 modprobe_remove_lttng_all();
627 close_consumer_sockets();
630 load_session_destroy_data(load_info
);
635 * Cleanup lock file by deleting it and finaly closing it which will
636 * release the file system lock.
638 if (lockfile_fd
>= 0) {
639 ret
= remove(config
.lock_file_path
.value
);
641 PERROR("remove lock file");
643 ret
= close(lockfile_fd
);
645 PERROR("close lock file");
650 * We do NOT rmdir rundir because there are other processes
651 * using it, for instance lttng-relayd, which can start in
652 * parallel with this teardown.
657 * Cleanup the daemon's option data structures.
659 static void sessiond_cleanup_options(void)
661 DBG("Cleaning up options");
663 sessiond_config_fini(&config
);
665 run_as_destroy_worker();
669 * Send data on a unix socket using the liblttsessiondcomm API.
671 * Return lttcomm error code.
673 static int send_unix_sock(int sock
, void *buf
, size_t len
)
675 /* Check valid length */
680 return lttcomm_send_unix_sock(sock
, buf
, len
);
684 * Free memory of a command context structure.
686 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
688 DBG("Clean command context structure");
690 if ((*cmd_ctx
)->llm
) {
691 free((*cmd_ctx
)->llm
);
693 if ((*cmd_ctx
)->lsm
) {
694 free((*cmd_ctx
)->lsm
);
702 * Notify UST applications using the shm mmap futex.
704 static int notify_ust_apps(int active
)
708 DBG("Notifying applications of session daemon state: %d", active
);
710 /* See shm.c for this call implying mmap, shm and futex calls */
711 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
712 if (wait_shm_mmap
== NULL
) {
716 /* Wake waiting process */
717 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
719 /* Apps notified successfully */
727 * Setup the outgoing data buffer for the response (llm) by allocating the
728 * right amount of memory and copying the original information from the lsm
731 * Return 0 on success, negative value on error.
733 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
734 const void *payload_buf
, size_t payload_len
,
735 const void *cmd_header_buf
, size_t cmd_header_len
)
738 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
739 const size_t cmd_header_offset
= header_len
;
740 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
741 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
743 cmd_ctx
->llm
= zmalloc(total_msg_size
);
745 if (cmd_ctx
->llm
== NULL
) {
751 /* Copy common data */
752 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
753 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
754 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
755 cmd_ctx
->llm
->data_size
= payload_len
;
756 cmd_ctx
->lttng_msg_size
= total_msg_size
;
758 /* Copy command header */
759 if (cmd_header_len
) {
760 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
766 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
775 * Version of setup_lttng_msg() without command header.
777 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
778 void *payload_buf
, size_t payload_len
)
780 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
783 * Update the kernel poll set of all channel fd available over all tracing
784 * session. Add the wakeup pipe at the end of the set.
786 static int update_kernel_poll(struct lttng_poll_event
*events
)
789 struct ltt_session
*session
;
790 struct ltt_kernel_channel
*channel
;
792 DBG("Updating kernel poll set");
795 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
796 session_lock(session
);
797 if (session
->kernel_session
== NULL
) {
798 session_unlock(session
);
802 cds_list_for_each_entry(channel
,
803 &session
->kernel_session
->channel_list
.head
, list
) {
804 /* Add channel fd to the kernel poll set */
805 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
807 session_unlock(session
);
810 DBG("Channel fd %d added to kernel set", channel
->fd
);
812 session_unlock(session
);
814 session_unlock_list();
819 session_unlock_list();
824 * Find the channel fd from 'fd' over all tracing session. When found, check
825 * for new channel stream and send those stream fds to the kernel consumer.
827 * Useful for CPU hotplug feature.
829 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
832 struct ltt_session
*session
;
833 struct ltt_kernel_session
*ksess
;
834 struct ltt_kernel_channel
*channel
;
836 DBG("Updating kernel streams for channel fd %d", fd
);
839 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
840 session_lock(session
);
841 if (session
->kernel_session
== NULL
) {
842 session_unlock(session
);
845 ksess
= session
->kernel_session
;
847 cds_list_for_each_entry(channel
,
848 &ksess
->channel_list
.head
, list
) {
849 struct lttng_ht_iter iter
;
850 struct consumer_socket
*socket
;
852 if (channel
->fd
!= fd
) {
855 DBG("Channel found, updating kernel streams");
856 ret
= kernel_open_channel_stream(channel
);
860 /* Update the stream global counter */
861 ksess
->stream_count_global
+= ret
;
864 * Have we already sent fds to the consumer? If yes, it
865 * means that tracing is started so it is safe to send
866 * our updated stream fds.
868 if (ksess
->consumer_fds_sent
!= 1
869 || ksess
->consumer
== NULL
) {
875 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
876 &iter
.iter
, socket
, node
.node
) {
877 pthread_mutex_lock(socket
->lock
);
878 ret
= kernel_consumer_send_channel_stream(socket
,
880 session
->output_traces
? 1 : 0);
881 pthread_mutex_unlock(socket
->lock
);
889 session_unlock(session
);
891 session_unlock_list();
895 session_unlock(session
);
896 session_unlock_list();
901 * For each tracing session, update newly registered apps. The session list
902 * lock MUST be acquired before calling this.
904 static void update_ust_app(int app_sock
)
906 struct ltt_session
*sess
, *stmp
;
908 /* Consumer is in an ERROR state. Stop any application update. */
909 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
910 /* Stop the update process since the consumer is dead. */
914 /* For all tracing session(s) */
915 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
919 if (!sess
->ust_session
) {
924 assert(app_sock
>= 0);
925 app
= ust_app_find_by_sock(app_sock
);
928 * Application can be unregistered before so
929 * this is possible hence simply stopping the
932 DBG3("UST app update failed to find app sock %d",
936 ust_app_global_update(sess
->ust_session
, app
);
940 session_unlock(sess
);
945 * This thread manage event coming from the kernel.
947 * Features supported in this thread:
950 static void *thread_manage_kernel(void *data
)
952 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
953 uint32_t revents
, nb_fd
;
955 struct lttng_poll_event events
;
957 DBG("[thread] Thread manage kernel started");
959 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
962 * This first step of the while is to clean this structure which could free
963 * non NULL pointers so initialize it before the loop.
965 lttng_poll_init(&events
);
967 if (testpoint(sessiond_thread_manage_kernel
)) {
968 goto error_testpoint
;
971 health_code_update();
973 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
974 goto error_testpoint
;
978 health_code_update();
980 if (update_poll_flag
== 1) {
981 /* Clean events object. We are about to populate it again. */
982 lttng_poll_clean(&events
);
984 ret
= sessiond_set_thread_pollset(&events
, 2);
986 goto error_poll_create
;
989 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
994 /* This will add the available kernel channel if any. */
995 ret
= update_kernel_poll(&events
);
999 update_poll_flag
= 0;
1002 DBG("Thread kernel polling");
1004 /* Poll infinite value of time */
1006 health_poll_entry();
1007 ret
= lttng_poll_wait(&events
, -1);
1008 DBG("Thread kernel return from poll on %d fds",
1009 LTTNG_POLL_GETNB(&events
));
1013 * Restart interrupted system call.
1015 if (errno
== EINTR
) {
1019 } else if (ret
== 0) {
1020 /* Should not happen since timeout is infinite */
1021 ERR("Return value of poll is 0 with an infinite timeout.\n"
1022 "This should not have happened! Continuing...");
1028 for (i
= 0; i
< nb_fd
; i
++) {
1029 /* Fetch once the poll data */
1030 revents
= LTTNG_POLL_GETEV(&events
, i
);
1031 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1033 health_code_update();
1036 /* No activity for this FD (poll implementation). */
1040 /* Thread quit pipe has been closed. Killing thread. */
1041 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1047 /* Check for data on kernel pipe */
1048 if (revents
& LPOLLIN
) {
1049 if (pollfd
== kernel_poll_pipe
[0]) {
1050 (void) lttng_read(kernel_poll_pipe
[0],
1053 * Ret value is useless here, if this pipe gets any actions an
1054 * update is required anyway.
1056 update_poll_flag
= 1;
1060 * New CPU detected by the kernel. Adding kernel stream to
1061 * kernel session and updating the kernel consumer
1063 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1069 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1070 update_poll_flag
= 1;
1073 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1081 lttng_poll_clean(&events
);
1084 utils_close_pipe(kernel_poll_pipe
);
1085 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1088 ERR("Health error occurred in %s", __func__
);
1089 WARN("Kernel thread died unexpectedly. "
1090 "Kernel tracing can continue but CPU hotplug is disabled.");
1092 health_unregister(health_sessiond
);
1093 DBG("Kernel thread dying");
1098 * Signal pthread condition of the consumer data that the thread.
1100 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1102 pthread_mutex_lock(&data
->cond_mutex
);
1105 * The state is set before signaling. It can be any value, it's the waiter
1106 * job to correctly interpret this condition variable associated to the
1107 * consumer pthread_cond.
1109 * A value of 0 means that the corresponding thread of the consumer data
1110 * was not started. 1 indicates that the thread has started and is ready
1111 * for action. A negative value means that there was an error during the
1114 data
->consumer_thread_is_ready
= state
;
1115 (void) pthread_cond_signal(&data
->cond
);
1117 pthread_mutex_unlock(&data
->cond_mutex
);
1121 * This thread manage the consumer error sent back to the session daemon.
1123 static void *thread_manage_consumer(void *data
)
1125 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1126 uint32_t revents
, nb_fd
;
1127 enum lttcomm_return_code code
;
1128 struct lttng_poll_event events
;
1129 struct consumer_data
*consumer_data
= data
;
1130 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1132 DBG("[thread] Manage consumer started");
1134 rcu_register_thread();
1135 rcu_thread_online();
1137 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1139 health_code_update();
1142 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1143 * metadata_sock. Nothing more will be added to this poll set.
1145 ret
= sessiond_set_thread_pollset(&events
, 3);
1151 * The error socket here is already in a listening state which was done
1152 * just before spawning this thread to avoid a race between the consumer
1153 * daemon exec trying to connect and the listen() call.
1155 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1160 health_code_update();
1162 /* Infinite blocking call, waiting for transmission */
1164 health_poll_entry();
1166 if (testpoint(sessiond_thread_manage_consumer
)) {
1170 ret
= lttng_poll_wait(&events
, -1);
1174 * Restart interrupted system call.
1176 if (errno
== EINTR
) {
1184 for (i
= 0; i
< nb_fd
; i
++) {
1185 /* Fetch once the poll data */
1186 revents
= LTTNG_POLL_GETEV(&events
, i
);
1187 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1189 health_code_update();
1192 /* No activity for this FD (poll implementation). */
1196 /* Thread quit pipe has been closed. Killing thread. */
1197 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1203 /* Event on the registration socket */
1204 if (pollfd
== consumer_data
->err_sock
) {
1205 if (revents
& LPOLLIN
) {
1207 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1208 ERR("consumer err socket poll error");
1211 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1217 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1223 * Set the CLOEXEC flag. Return code is useless because either way, the
1226 (void) utils_set_fd_cloexec(sock
);
1228 health_code_update();
1230 DBG2("Receiving code from consumer err_sock");
1232 /* Getting status code from kconsumerd */
1233 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1234 sizeof(enum lttcomm_return_code
));
1239 health_code_update();
1240 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1241 ERR("consumer error when waiting for SOCK_READY : %s",
1242 lttcomm_get_readable_code(-code
));
1246 /* Connect both command and metadata sockets. */
1247 consumer_data
->cmd_sock
=
1248 lttcomm_connect_unix_sock(
1249 consumer_data
->cmd_unix_sock_path
);
1250 consumer_data
->metadata_fd
=
1251 lttcomm_connect_unix_sock(
1252 consumer_data
->cmd_unix_sock_path
);
1253 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1254 PERROR("consumer connect cmd socket");
1255 /* On error, signal condition and quit. */
1256 signal_consumer_condition(consumer_data
, -1);
1260 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1262 /* Create metadata socket lock. */
1263 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1264 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1265 PERROR("zmalloc pthread mutex");
1268 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1270 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1271 DBG("Consumer metadata socket ready (fd: %d)",
1272 consumer_data
->metadata_fd
);
1275 * Remove the consumerd error sock since we've established a connection.
1277 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1282 /* Add new accepted error socket. */
1283 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1288 /* Add metadata socket that is successfully connected. */
1289 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1290 LPOLLIN
| LPOLLRDHUP
);
1295 health_code_update();
1298 * Transfer the write-end of the channel monitoring and rotate pipe
1299 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1300 * SET_CHANNEL_ROTATE_PIPE commands.
1302 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1303 if (!cmd_socket_wrapper
) {
1306 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1308 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1309 consumer_data
->channel_monitor_pipe
);
1314 ret
= consumer_send_channel_rotate_pipe(cmd_socket_wrapper
,
1315 consumer_data
->channel_rotate_pipe
);
1320 /* Discard the socket wrapper as it is no longer needed. */
1321 consumer_destroy_socket(cmd_socket_wrapper
);
1322 cmd_socket_wrapper
= NULL
;
1324 /* The thread is completely initialized, signal that it is ready. */
1325 signal_consumer_condition(consumer_data
, 1);
1327 /* Infinite blocking call, waiting for transmission */
1330 health_code_update();
1332 /* Exit the thread because the thread quit pipe has been triggered. */
1334 /* Not a health error. */
1339 health_poll_entry();
1340 ret
= lttng_poll_wait(&events
, -1);
1344 * Restart interrupted system call.
1346 if (errno
== EINTR
) {
1354 for (i
= 0; i
< nb_fd
; i
++) {
1355 /* Fetch once the poll data */
1356 revents
= LTTNG_POLL_GETEV(&events
, i
);
1357 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1359 health_code_update();
1362 /* No activity for this FD (poll implementation). */
1367 * Thread quit pipe has been triggered, flag that we should stop
1368 * but continue the current loop to handle potential data from
1371 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1373 if (pollfd
== sock
) {
1374 /* Event on the consumerd socket */
1375 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1376 && !(revents
& LPOLLIN
)) {
1377 ERR("consumer err socket second poll error");
1380 health_code_update();
1381 /* Wait for any kconsumerd error */
1382 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1383 sizeof(enum lttcomm_return_code
));
1385 ERR("consumer closed the command socket");
1389 ERR("consumer return code : %s",
1390 lttcomm_get_readable_code(-code
));
1393 } else if (pollfd
== consumer_data
->metadata_fd
) {
1394 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1395 && !(revents
& LPOLLIN
)) {
1396 ERR("consumer err metadata socket second poll error");
1399 /* UST metadata requests */
1400 ret
= ust_consumer_metadata_request(
1401 &consumer_data
->metadata_sock
);
1403 ERR("Handling metadata request");
1407 /* No need for an else branch all FDs are tested prior. */
1409 health_code_update();
1415 * We lock here because we are about to close the sockets and some other
1416 * thread might be using them so get exclusive access which will abort all
1417 * other consumer command by other threads.
1419 pthread_mutex_lock(&consumer_data
->lock
);
1421 /* Immediately set the consumerd state to stopped */
1422 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1423 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1424 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1425 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1426 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1428 /* Code flow error... */
1432 if (consumer_data
->err_sock
>= 0) {
1433 ret
= close(consumer_data
->err_sock
);
1437 consumer_data
->err_sock
= -1;
1439 if (consumer_data
->cmd_sock
>= 0) {
1440 ret
= close(consumer_data
->cmd_sock
);
1444 consumer_data
->cmd_sock
= -1;
1446 if (consumer_data
->metadata_sock
.fd_ptr
&&
1447 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1448 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1460 unlink(consumer_data
->err_unix_sock_path
);
1461 unlink(consumer_data
->cmd_unix_sock_path
);
1462 pthread_mutex_unlock(&consumer_data
->lock
);
1464 /* Cleanup metadata socket mutex. */
1465 if (consumer_data
->metadata_sock
.lock
) {
1466 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1467 free(consumer_data
->metadata_sock
.lock
);
1469 lttng_poll_clean(&events
);
1471 if (cmd_socket_wrapper
) {
1472 consumer_destroy_socket(cmd_socket_wrapper
);
1477 ERR("Health error occurred in %s", __func__
);
1479 health_unregister(health_sessiond
);
1480 DBG("consumer thread cleanup completed");
1482 rcu_thread_offline();
1483 rcu_unregister_thread();
1489 * This thread receives application command sockets (FDs) on the
1490 * apps_cmd_pipe and waits (polls) on them until they are closed
1491 * or an error occurs.
1493 * At that point, it flushes the data (tracing and metadata) associated
1494 * with this application and tears down ust app sessions and other
1495 * associated data structures through ust_app_unregister().
1497 * Note that this thread never sends commands to the applications
1498 * through the command sockets; it merely listens for hang-ups
1499 * and errors on those sockets and cleans-up as they occur.
1501 static void *thread_manage_apps(void *data
)
1503 int i
, ret
, pollfd
, err
= -1;
1505 uint32_t revents
, nb_fd
;
1506 struct lttng_poll_event events
;
1508 DBG("[thread] Manage application started");
1510 rcu_register_thread();
1511 rcu_thread_online();
1513 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1515 if (testpoint(sessiond_thread_manage_apps
)) {
1516 goto error_testpoint
;
1519 health_code_update();
1521 ret
= sessiond_set_thread_pollset(&events
, 2);
1523 goto error_poll_create
;
1526 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1531 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1535 health_code_update();
1538 DBG("Apps thread polling");
1540 /* Inifinite blocking call, waiting for transmission */
1542 health_poll_entry();
1543 ret
= lttng_poll_wait(&events
, -1);
1544 DBG("Apps thread return from poll on %d fds",
1545 LTTNG_POLL_GETNB(&events
));
1549 * Restart interrupted system call.
1551 if (errno
== EINTR
) {
1559 for (i
= 0; i
< nb_fd
; i
++) {
1560 /* Fetch once the poll data */
1561 revents
= LTTNG_POLL_GETEV(&events
, i
);
1562 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1564 health_code_update();
1567 /* No activity for this FD (poll implementation). */
1571 /* Thread quit pipe has been closed. Killing thread. */
1572 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1578 /* Inspect the apps cmd pipe */
1579 if (pollfd
== apps_cmd_pipe
[0]) {
1580 if (revents
& LPOLLIN
) {
1584 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1585 if (size_ret
< sizeof(sock
)) {
1586 PERROR("read apps cmd pipe");
1590 health_code_update();
1593 * Since this is a command socket (write then read),
1594 * we only monitor the error events of the socket.
1596 ret
= lttng_poll_add(&events
, sock
,
1597 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1602 DBG("Apps with sock %d added to poll set", sock
);
1603 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1604 ERR("Apps command pipe error");
1607 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1612 * At this point, we know that a registered application made
1613 * the event at poll_wait.
1615 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1616 /* Removing from the poll set */
1617 ret
= lttng_poll_del(&events
, pollfd
);
1622 /* Socket closed on remote end. */
1623 ust_app_unregister(pollfd
);
1625 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1630 health_code_update();
1636 lttng_poll_clean(&events
);
1639 utils_close_pipe(apps_cmd_pipe
);
1640 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1643 * We don't clean the UST app hash table here since already registered
1644 * applications can still be controlled so let them be until the session
1645 * daemon dies or the applications stop.
1650 ERR("Health error occurred in %s", __func__
);
1652 health_unregister(health_sessiond
);
1653 DBG("Application communication apps thread cleanup complete");
1654 rcu_thread_offline();
1655 rcu_unregister_thread();
1660 * Send a socket to a thread This is called from the dispatch UST registration
1661 * thread once all sockets are set for the application.
1663 * The sock value can be invalid, we don't really care, the thread will handle
1664 * it and make the necessary cleanup if so.
1666 * On success, return 0 else a negative value being the errno message of the
1669 static int send_socket_to_thread(int fd
, int sock
)
1674 * It's possible that the FD is set as invalid with -1 concurrently just
1675 * before calling this function being a shutdown state of the thread.
1682 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1683 if (ret
< sizeof(sock
)) {
1684 PERROR("write apps pipe %d", fd
);
1691 /* All good. Don't send back the write positive ret value. */
1698 * Sanitize the wait queue of the dispatch registration thread meaning removing
1699 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1700 * notify socket is never received.
1702 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1704 int ret
, nb_fd
= 0, i
;
1705 unsigned int fd_added
= 0;
1706 struct lttng_poll_event events
;
1707 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1711 lttng_poll_init(&events
);
1713 /* Just skip everything for an empty queue. */
1714 if (!wait_queue
->count
) {
1718 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1723 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1724 &wait_queue
->head
, head
) {
1725 assert(wait_node
->app
);
1726 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1727 LPOLLHUP
| LPOLLERR
);
1740 * Poll but don't block so we can quickly identify the faulty events and
1741 * clean them afterwards from the wait queue.
1743 ret
= lttng_poll_wait(&events
, 0);
1749 for (i
= 0; i
< nb_fd
; i
++) {
1750 /* Get faulty FD. */
1751 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1752 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1755 /* No activity for this FD (poll implementation). */
1759 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1760 &wait_queue
->head
, head
) {
1761 if (pollfd
== wait_node
->app
->sock
&&
1762 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1763 cds_list_del(&wait_node
->head
);
1764 wait_queue
->count
--;
1765 ust_app_destroy(wait_node
->app
);
1768 * Silence warning of use-after-free in
1769 * cds_list_for_each_entry_safe which uses
1770 * __typeof__(*wait_node).
1775 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1782 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1786 lttng_poll_clean(&events
);
1790 lttng_poll_clean(&events
);
1792 ERR("Unable to sanitize wait queue");
1797 * Dispatch request from the registration threads to the application
1798 * communication thread.
1800 static void *thread_dispatch_ust_registration(void *data
)
1803 struct cds_wfcq_node
*node
;
1804 struct ust_command
*ust_cmd
= NULL
;
1805 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1806 struct ust_reg_wait_queue wait_queue
= {
1810 rcu_register_thread();
1812 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1814 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1815 goto error_testpoint
;
1818 health_code_update();
1820 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1822 DBG("[thread] Dispatch UST command started");
1825 health_code_update();
1827 /* Atomically prepare the queue futex */
1828 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1830 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1835 struct ust_app
*app
= NULL
;
1839 * Make sure we don't have node(s) that have hung up before receiving
1840 * the notify socket. This is to clean the list in order to avoid
1841 * memory leaks from notify socket that are never seen.
1843 sanitize_wait_queue(&wait_queue
);
1845 health_code_update();
1846 /* Dequeue command for registration */
1847 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1849 DBG("Woken up but nothing in the UST command queue");
1850 /* Continue thread execution */
1854 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1856 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1857 " gid:%d sock:%d name:%s (version %d.%d)",
1858 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1859 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1860 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1861 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1863 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1864 wait_node
= zmalloc(sizeof(*wait_node
));
1866 PERROR("zmalloc wait_node dispatch");
1867 ret
= close(ust_cmd
->sock
);
1869 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1871 lttng_fd_put(LTTNG_FD_APPS
, 1);
1875 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1877 /* Create application object if socket is CMD. */
1878 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1880 if (!wait_node
->app
) {
1881 ret
= close(ust_cmd
->sock
);
1883 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1885 lttng_fd_put(LTTNG_FD_APPS
, 1);
1891 * Add application to the wait queue so we can set the notify
1892 * socket before putting this object in the global ht.
1894 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1899 * We have to continue here since we don't have the notify
1900 * socket and the application MUST be added to the hash table
1901 * only at that moment.
1906 * Look for the application in the local wait queue and set the
1907 * notify socket if found.
1909 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1910 &wait_queue
.head
, head
) {
1911 health_code_update();
1912 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1913 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1914 cds_list_del(&wait_node
->head
);
1916 app
= wait_node
->app
;
1918 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1924 * With no application at this stage the received socket is
1925 * basically useless so close it before we free the cmd data
1926 * structure for good.
1929 ret
= close(ust_cmd
->sock
);
1931 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1933 lttng_fd_put(LTTNG_FD_APPS
, 1);
1940 * @session_lock_list
1942 * Lock the global session list so from the register up to the
1943 * registration done message, no thread can see the application
1944 * and change its state.
1946 session_lock_list();
1950 * Add application to the global hash table. This needs to be
1951 * done before the update to the UST registry can locate the
1956 /* Set app version. This call will print an error if needed. */
1957 (void) ust_app_version(app
);
1959 /* Send notify socket through the notify pipe. */
1960 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1964 session_unlock_list();
1966 * No notify thread, stop the UST tracing. However, this is
1967 * not an internal error of the this thread thus setting
1968 * the health error code to a normal exit.
1975 * Update newly registered application with the tracing
1976 * registry info already enabled information.
1978 update_ust_app(app
->sock
);
1981 * Don't care about return value. Let the manage apps threads
1982 * handle app unregistration upon socket close.
1984 (void) ust_app_register_done(app
);
1987 * Even if the application socket has been closed, send the app
1988 * to the thread and unregistration will take place at that
1991 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1994 session_unlock_list();
1996 * No apps. thread, stop the UST tracing. However, this is
1997 * not an internal error of the this thread thus setting
1998 * the health error code to a normal exit.
2005 session_unlock_list();
2007 } while (node
!= NULL
);
2009 health_poll_entry();
2010 /* Futex wait on queue. Blocking call on futex() */
2011 futex_nto1_wait(&ust_cmd_queue
.futex
);
2014 /* Normal exit, no error */
2018 /* Clean up wait queue. */
2019 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2020 &wait_queue
.head
, head
) {
2021 cds_list_del(&wait_node
->head
);
2026 /* Empty command queue. */
2028 /* Dequeue command for registration */
2029 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2033 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2034 ret
= close(ust_cmd
->sock
);
2036 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2038 lttng_fd_put(LTTNG_FD_APPS
, 1);
2043 DBG("Dispatch thread dying");
2046 ERR("Health error occurred in %s", __func__
);
2048 health_unregister(health_sessiond
);
2049 rcu_unregister_thread();
2054 * This thread manage application registration.
2056 static void *thread_registration_apps(void *data
)
2058 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2059 uint32_t revents
, nb_fd
;
2060 struct lttng_poll_event events
;
2062 * Get allocated in this thread, enqueued to a global queue, dequeued and
2063 * freed in the manage apps thread.
2065 struct ust_command
*ust_cmd
= NULL
;
2067 DBG("[thread] Manage application registration started");
2069 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2071 if (testpoint(sessiond_thread_registration_apps
)) {
2072 goto error_testpoint
;
2075 ret
= lttcomm_listen_unix_sock(apps_sock
);
2081 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2082 * more will be added to this poll set.
2084 ret
= sessiond_set_thread_pollset(&events
, 2);
2086 goto error_create_poll
;
2089 /* Add the application registration socket */
2090 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2092 goto error_poll_add
;
2095 /* Notify all applications to register */
2096 ret
= notify_ust_apps(1);
2098 ERR("Failed to notify applications or create the wait shared memory.\n"
2099 "Execution continues but there might be problem for already\n"
2100 "running applications that wishes to register.");
2104 DBG("Accepting application registration");
2106 /* Inifinite blocking call, waiting for transmission */
2108 health_poll_entry();
2109 ret
= lttng_poll_wait(&events
, -1);
2113 * Restart interrupted system call.
2115 if (errno
== EINTR
) {
2123 for (i
= 0; i
< nb_fd
; i
++) {
2124 health_code_update();
2126 /* Fetch once the poll data */
2127 revents
= LTTNG_POLL_GETEV(&events
, i
);
2128 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2131 /* No activity for this FD (poll implementation). */
2135 /* Thread quit pipe has been closed. Killing thread. */
2136 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2142 /* Event on the registration socket */
2143 if (pollfd
== apps_sock
) {
2144 if (revents
& LPOLLIN
) {
2145 sock
= lttcomm_accept_unix_sock(apps_sock
);
2151 * Set socket timeout for both receiving and ending.
2152 * app_socket_timeout is in seconds, whereas
2153 * lttcomm_setsockopt_rcv_timeout and
2154 * lttcomm_setsockopt_snd_timeout expect msec as
2157 if (config
.app_socket_timeout
>= 0) {
2158 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2159 config
.app_socket_timeout
* 1000);
2160 (void) lttcomm_setsockopt_snd_timeout(sock
,
2161 config
.app_socket_timeout
* 1000);
2165 * Set the CLOEXEC flag. Return code is useless because
2166 * either way, the show must go on.
2168 (void) utils_set_fd_cloexec(sock
);
2170 /* Create UST registration command for enqueuing */
2171 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2172 if (ust_cmd
== NULL
) {
2173 PERROR("ust command zmalloc");
2182 * Using message-based transmissions to ensure we don't
2183 * have to deal with partially received messages.
2185 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2187 ERR("Exhausted file descriptors allowed for applications.");
2197 health_code_update();
2198 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2201 /* Close socket of the application. */
2206 lttng_fd_put(LTTNG_FD_APPS
, 1);
2210 health_code_update();
2212 ust_cmd
->sock
= sock
;
2215 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2216 " gid:%d sock:%d name:%s (version %d.%d)",
2217 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2218 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2219 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2220 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2223 * Lock free enqueue the registration request. The red pill
2224 * has been taken! This apps will be part of the *system*.
2226 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2229 * Wake the registration queue futex. Implicit memory
2230 * barrier with the exchange in cds_wfcq_enqueue.
2232 futex_nto1_wake(&ust_cmd_queue
.futex
);
2233 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2234 ERR("Register apps socket poll error");
2237 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2246 /* Notify that the registration thread is gone */
2249 if (apps_sock
>= 0) {
2250 ret
= close(apps_sock
);
2260 lttng_fd_put(LTTNG_FD_APPS
, 1);
2262 unlink(config
.apps_unix_sock_path
.value
);
2265 lttng_poll_clean(&events
);
2269 DBG("UST Registration thread cleanup complete");
2272 ERR("Health error occurred in %s", __func__
);
2274 health_unregister(health_sessiond
);
2280 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2281 * exec or it will fails.
2283 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2286 struct timespec timeout
;
2289 * Make sure we set the readiness flag to 0 because we are NOT ready.
2290 * This access to consumer_thread_is_ready does not need to be
2291 * protected by consumer_data.cond_mutex (yet) since the consumer
2292 * management thread has not been started at this point.
2294 consumer_data
->consumer_thread_is_ready
= 0;
2296 /* Setup pthread condition */
2297 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2300 PERROR("pthread_condattr_init consumer data");
2305 * Set the monotonic clock in order to make sure we DO NOT jump in time
2306 * between the clock_gettime() call and the timedwait call. See bug #324
2307 * for a more details and how we noticed it.
2309 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2312 PERROR("pthread_condattr_setclock consumer data");
2316 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2319 PERROR("pthread_cond_init consumer data");
2323 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2324 thread_manage_consumer
, consumer_data
);
2327 PERROR("pthread_create consumer");
2332 /* We are about to wait on a pthread condition */
2333 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2335 /* Get time for sem_timedwait absolute timeout */
2336 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2338 * Set the timeout for the condition timed wait even if the clock gettime
2339 * call fails since we might loop on that call and we want to avoid to
2340 * increment the timeout too many times.
2342 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2345 * The following loop COULD be skipped in some conditions so this is why we
2346 * set ret to 0 in order to make sure at least one round of the loop is
2352 * Loop until the condition is reached or when a timeout is reached. Note
2353 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2354 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2355 * possible. This loop does not take any chances and works with both of
2358 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2359 if (clock_ret
< 0) {
2360 PERROR("clock_gettime spawn consumer");
2361 /* Infinite wait for the consumerd thread to be ready */
2362 ret
= pthread_cond_wait(&consumer_data
->cond
,
2363 &consumer_data
->cond_mutex
);
2365 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2366 &consumer_data
->cond_mutex
, &timeout
);
2370 /* Release the pthread condition */
2371 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2375 if (ret
== ETIMEDOUT
) {
2379 * Call has timed out so we kill the kconsumerd_thread and return
2382 ERR("Condition timed out. The consumer thread was never ready."
2384 pth_ret
= pthread_cancel(consumer_data
->thread
);
2386 PERROR("pthread_cancel consumer thread");
2389 PERROR("pthread_cond_wait failed consumer thread");
2391 /* Caller is expecting a negative value on failure. */
2396 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2397 if (consumer_data
->pid
== 0) {
2398 ERR("Consumerd did not start");
2399 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2402 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2411 * Join consumer thread
2413 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2417 /* Consumer pid must be a real one. */
2418 if (consumer_data
->pid
> 0) {
2420 ret
= kill(consumer_data
->pid
, SIGTERM
);
2422 PERROR("Error killing consumer daemon");
2425 return pthread_join(consumer_data
->thread
, &status
);
2432 * Fork and exec a consumer daemon (consumerd).
2434 * Return pid if successful else -1.
2436 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2440 const char *consumer_to_use
;
2441 const char *verbosity
;
2444 DBG("Spawning consumerd");
2451 if (config
.verbose_consumer
) {
2452 verbosity
= "--verbose";
2453 } else if (lttng_opt_quiet
) {
2454 verbosity
= "--quiet";
2459 switch (consumer_data
->type
) {
2460 case LTTNG_CONSUMER_KERNEL
:
2462 * Find out which consumerd to execute. We will first try the
2463 * 64-bit path, then the sessiond's installation directory, and
2464 * fallback on the 32-bit one,
2466 DBG3("Looking for a kernel consumer at these locations:");
2467 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2468 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2469 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2470 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2471 DBG3("Found location #1");
2472 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2473 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2474 DBG3("Found location #2");
2475 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2476 } else if (stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2477 DBG3("Found location #3");
2478 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2480 DBG("Could not find any valid consumerd executable");
2484 DBG("Using kernel consumer at: %s", consumer_to_use
);
2485 (void) execl(consumer_to_use
,
2486 "lttng-consumerd", verbosity
, "-k",
2487 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2488 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2489 "--group", config
.tracing_group_name
.value
,
2492 case LTTNG_CONSUMER64_UST
:
2494 if (config
.consumerd64_lib_dir
.value
) {
2499 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2503 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2504 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2509 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2510 if (tmp
[0] != '\0') {
2511 strcat(tmpnew
, ":");
2512 strcat(tmpnew
, tmp
);
2514 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2521 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2522 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2523 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2524 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2525 "--group", config
.tracing_group_name
.value
,
2529 case LTTNG_CONSUMER32_UST
:
2531 if (config
.consumerd32_lib_dir
.value
) {
2536 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2540 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2541 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2546 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2547 if (tmp
[0] != '\0') {
2548 strcat(tmpnew
, ":");
2549 strcat(tmpnew
, tmp
);
2551 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2558 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2559 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2560 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2561 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2562 "--group", config
.tracing_group_name
.value
,
2567 ERR("unknown consumer type");
2571 PERROR("Consumer execl()");
2573 /* Reaching this point, we got a failure on our execl(). */
2575 } else if (pid
> 0) {
2578 PERROR("start consumer fork");
2586 * Spawn the consumerd daemon and session daemon thread.
2588 static int start_consumerd(struct consumer_data
*consumer_data
)
2593 * Set the listen() state on the socket since there is a possible race
2594 * between the exec() of the consumer daemon and this call if place in the
2595 * consumer thread. See bug #366 for more details.
2597 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2602 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2603 if (consumer_data
->pid
!= 0) {
2604 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2608 ret
= spawn_consumerd(consumer_data
);
2610 ERR("Spawning consumerd failed");
2611 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2615 /* Setting up the consumer_data pid */
2616 consumer_data
->pid
= ret
;
2617 DBG2("Consumer pid %d", consumer_data
->pid
);
2618 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2620 DBG2("Spawning consumer control thread");
2621 ret
= spawn_consumer_thread(consumer_data
);
2623 ERR("Fatal error spawning consumer control thread");
2631 /* Cleanup already created sockets on error. */
2632 if (consumer_data
->err_sock
>= 0) {
2635 err
= close(consumer_data
->err_sock
);
2637 PERROR("close consumer data error socket");
2644 * Setup necessary data for kernel tracer action.
2646 static int init_kernel_tracer(void)
2650 /* Modprobe lttng kernel modules */
2651 ret
= modprobe_lttng_control();
2656 /* Open debugfs lttng */
2657 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2658 if (kernel_tracer_fd
< 0) {
2659 DBG("Failed to open %s", module_proc_lttng
);
2663 /* Validate kernel version */
2664 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2665 &kernel_tracer_abi_version
);
2670 ret
= modprobe_lttng_data();
2675 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2682 WARN("Kernel tracer does not support buffer monitoring. "
2683 "The monitoring timer of channels in the kernel domain "
2684 "will be set to 0 (disabled).");
2687 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2691 modprobe_remove_lttng_control();
2692 ret
= close(kernel_tracer_fd
);
2696 kernel_tracer_fd
= -1;
2697 return LTTNG_ERR_KERN_VERSION
;
2700 ret
= close(kernel_tracer_fd
);
2706 modprobe_remove_lttng_control();
2709 WARN("No kernel tracer available");
2710 kernel_tracer_fd
= -1;
2712 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2714 return LTTNG_ERR_KERN_NA
;
2720 * Copy consumer output from the tracing session to the domain session. The
2721 * function also applies the right modification on a per domain basis for the
2722 * trace files destination directory.
2724 * Should *NOT* be called with RCU read-side lock held.
2726 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2729 const char *dir_name
;
2730 struct consumer_output
*consumer
;
2733 assert(session
->consumer
);
2736 case LTTNG_DOMAIN_KERNEL
:
2737 DBG3("Copying tracing session consumer output in kernel session");
2739 * XXX: We should audit the session creation and what this function
2740 * does "extra" in order to avoid a destroy since this function is used
2741 * in the domain session creation (kernel and ust) only. Same for UST
2744 if (session
->kernel_session
->consumer
) {
2745 consumer_output_put(session
->kernel_session
->consumer
);
2747 session
->kernel_session
->consumer
=
2748 consumer_copy_output(session
->consumer
);
2749 /* Ease our life a bit for the next part */
2750 consumer
= session
->kernel_session
->consumer
;
2751 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2753 case LTTNG_DOMAIN_JUL
:
2754 case LTTNG_DOMAIN_LOG4J
:
2755 case LTTNG_DOMAIN_PYTHON
:
2756 case LTTNG_DOMAIN_UST
:
2757 DBG3("Copying tracing session consumer output in UST session");
2758 if (session
->ust_session
->consumer
) {
2759 consumer_output_put(session
->ust_session
->consumer
);
2761 session
->ust_session
->consumer
=
2762 consumer_copy_output(session
->consumer
);
2763 /* Ease our life a bit for the next part */
2764 consumer
= session
->ust_session
->consumer
;
2765 dir_name
= DEFAULT_UST_TRACE_DIR
;
2768 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2772 /* Append correct directory to subdir */
2773 strncat(consumer
->subdir
, dir_name
,
2774 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2775 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2784 * Create an UST session and add it to the session ust list.
2786 * Should *NOT* be called with RCU read-side lock held.
2788 static int create_ust_session(struct ltt_session
*session
,
2789 struct lttng_domain
*domain
)
2792 struct ltt_ust_session
*lus
= NULL
;
2796 assert(session
->consumer
);
2798 switch (domain
->type
) {
2799 case LTTNG_DOMAIN_JUL
:
2800 case LTTNG_DOMAIN_LOG4J
:
2801 case LTTNG_DOMAIN_PYTHON
:
2802 case LTTNG_DOMAIN_UST
:
2805 ERR("Unknown UST domain on create session %d", domain
->type
);
2806 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2810 DBG("Creating UST session");
2812 lus
= trace_ust_create_session(session
->id
);
2814 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2818 lus
->uid
= session
->uid
;
2819 lus
->gid
= session
->gid
;
2820 lus
->output_traces
= session
->output_traces
;
2821 lus
->snapshot_mode
= session
->snapshot_mode
;
2822 lus
->live_timer_interval
= session
->live_timer
;
2823 session
->ust_session
= lus
;
2824 if (session
->shm_path
[0]) {
2825 strncpy(lus
->root_shm_path
, session
->shm_path
,
2826 sizeof(lus
->root_shm_path
));
2827 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2828 strncpy(lus
->shm_path
, session
->shm_path
,
2829 sizeof(lus
->shm_path
));
2830 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2831 strncat(lus
->shm_path
, "/ust",
2832 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2834 /* Copy session output to the newly created UST session */
2835 ret
= copy_session_consumer(domain
->type
, session
);
2836 if (ret
!= LTTNG_OK
) {
2844 session
->ust_session
= NULL
;
2849 * Create a kernel tracer session then create the default channel.
2851 static int create_kernel_session(struct ltt_session
*session
)
2855 DBG("Creating kernel session");
2857 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2859 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2863 /* Code flow safety */
2864 assert(session
->kernel_session
);
2866 /* Copy session output to the newly created Kernel session */
2867 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2868 if (ret
!= LTTNG_OK
) {
2872 session
->kernel_session
->uid
= session
->uid
;
2873 session
->kernel_session
->gid
= session
->gid
;
2874 session
->kernel_session
->output_traces
= session
->output_traces
;
2875 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2880 trace_kernel_destroy_session(session
->kernel_session
);
2881 session
->kernel_session
= NULL
;
2886 * Count number of session permitted by uid/gid.
2888 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2891 struct ltt_session
*session
;
2893 DBG("Counting number of available session for UID %d GID %d",
2895 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2897 * Only list the sessions the user can control.
2899 if (!session_access_ok(session
, uid
, gid
)) {
2908 * Check if the current kernel tracer supports the session rotation feature.
2909 * Return 1 if it does, 0 otherwise.
2911 static int check_rotate_compatible(void)
2915 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2916 DBG("Kernel tracer version is not compatible with the rotation feature");
2924 * Process the command requested by the lttng client within the command
2925 * context structure. This function make sure that the return structure (llm)
2926 * is set and ready for transmission before returning.
2928 * Return any error encountered or 0 for success.
2930 * "sock" is only used for special-case var. len data.
2932 * Should *NOT* be called with RCU read-side lock held.
2934 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2938 int need_tracing_session
= 1;
2941 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2943 assert(!rcu_read_ongoing());
2947 switch (cmd_ctx
->lsm
->cmd_type
) {
2948 case LTTNG_CREATE_SESSION
:
2949 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2950 case LTTNG_CREATE_SESSION_LIVE
:
2951 case LTTNG_DESTROY_SESSION
:
2952 case LTTNG_LIST_SESSIONS
:
2953 case LTTNG_LIST_DOMAINS
:
2954 case LTTNG_START_TRACE
:
2955 case LTTNG_STOP_TRACE
:
2956 case LTTNG_DATA_PENDING
:
2957 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2958 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2959 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2960 case LTTNG_SNAPSHOT_RECORD
:
2961 case LTTNG_SAVE_SESSION
:
2962 case LTTNG_SET_SESSION_SHM_PATH
:
2963 case LTTNG_REGENERATE_METADATA
:
2964 case LTTNG_REGENERATE_STATEDUMP
:
2965 case LTTNG_REGISTER_TRIGGER
:
2966 case LTTNG_UNREGISTER_TRIGGER
:
2967 case LTTNG_ROTATE_SESSION
:
2968 case LTTNG_ROTATION_GET_INFO
:
2969 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
2970 case LTTNG_ROTATION_SET_SCHEDULE
:
2971 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
2972 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
2979 if (config
.no_kernel
&& need_domain
2980 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2982 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2984 ret
= LTTNG_ERR_KERN_NA
;
2989 /* Deny register consumer if we already have a spawned consumer. */
2990 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2991 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2992 if (kconsumer_data
.pid
> 0) {
2993 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2994 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2997 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3001 * Check for command that don't needs to allocate a returned payload. We do
3002 * this here so we don't have to make the call for no payload at each
3005 switch(cmd_ctx
->lsm
->cmd_type
) {
3006 case LTTNG_LIST_SESSIONS
:
3007 case LTTNG_LIST_TRACEPOINTS
:
3008 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3009 case LTTNG_LIST_DOMAINS
:
3010 case LTTNG_LIST_CHANNELS
:
3011 case LTTNG_LIST_EVENTS
:
3012 case LTTNG_LIST_SYSCALLS
:
3013 case LTTNG_LIST_TRACKER_PIDS
:
3014 case LTTNG_DATA_PENDING
:
3015 case LTTNG_ROTATE_SESSION
:
3016 case LTTNG_ROTATION_GET_INFO
:
3017 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
3018 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
3021 /* Setup lttng message with no payload */
3022 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3024 /* This label does not try to unlock the session */
3025 goto init_setup_error
;
3029 /* Commands that DO NOT need a session. */
3030 switch (cmd_ctx
->lsm
->cmd_type
) {
3031 case LTTNG_CREATE_SESSION
:
3032 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3033 case LTTNG_CREATE_SESSION_LIVE
:
3034 case LTTNG_LIST_SESSIONS
:
3035 case LTTNG_LIST_TRACEPOINTS
:
3036 case LTTNG_LIST_SYSCALLS
:
3037 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3038 case LTTNG_SAVE_SESSION
:
3039 case LTTNG_REGISTER_TRIGGER
:
3040 case LTTNG_UNREGISTER_TRIGGER
:
3041 need_tracing_session
= 0;
3044 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3046 * We keep the session list lock across _all_ commands
3047 * for now, because the per-session lock does not
3048 * handle teardown properly.
3050 session_lock_list();
3051 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3052 if (cmd_ctx
->session
== NULL
) {
3053 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3056 /* Acquire lock for the session */
3057 session_lock(cmd_ctx
->session
);
3063 * Commands that need a valid session but should NOT create one if none
3064 * exists. Instead of creating one and destroying it when the command is
3065 * handled, process that right before so we save some round trip in useless
3068 switch (cmd_ctx
->lsm
->cmd_type
) {
3069 case LTTNG_DISABLE_CHANNEL
:
3070 case LTTNG_DISABLE_EVENT
:
3071 switch (cmd_ctx
->lsm
->domain
.type
) {
3072 case LTTNG_DOMAIN_KERNEL
:
3073 if (!cmd_ctx
->session
->kernel_session
) {
3074 ret
= LTTNG_ERR_NO_CHANNEL
;
3078 case LTTNG_DOMAIN_JUL
:
3079 case LTTNG_DOMAIN_LOG4J
:
3080 case LTTNG_DOMAIN_PYTHON
:
3081 case LTTNG_DOMAIN_UST
:
3082 if (!cmd_ctx
->session
->ust_session
) {
3083 ret
= LTTNG_ERR_NO_CHANNEL
;
3088 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3100 * Check domain type for specific "pre-action".
3102 switch (cmd_ctx
->lsm
->domain
.type
) {
3103 case LTTNG_DOMAIN_KERNEL
:
3105 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3109 /* Kernel tracer check */
3110 if (kernel_tracer_fd
== -1) {
3111 /* Basically, load kernel tracer modules */
3112 ret
= init_kernel_tracer();
3118 /* Consumer is in an ERROR state. Report back to client */
3119 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3120 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3124 /* Need a session for kernel command */
3125 if (need_tracing_session
) {
3126 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3127 ret
= create_kernel_session(cmd_ctx
->session
);
3129 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3134 /* Start the kernel consumer daemon */
3135 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3136 if (kconsumer_data
.pid
== 0 &&
3137 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3138 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3139 ret
= start_consumerd(&kconsumer_data
);
3141 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3144 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3146 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3150 * The consumer was just spawned so we need to add the socket to
3151 * the consumer output of the session if exist.
3153 ret
= consumer_create_socket(&kconsumer_data
,
3154 cmd_ctx
->session
->kernel_session
->consumer
);
3161 case LTTNG_DOMAIN_JUL
:
3162 case LTTNG_DOMAIN_LOG4J
:
3163 case LTTNG_DOMAIN_PYTHON
:
3164 case LTTNG_DOMAIN_UST
:
3166 if (!ust_app_supported()) {
3167 ret
= LTTNG_ERR_NO_UST
;
3170 /* Consumer is in an ERROR state. Report back to client */
3171 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3172 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3176 if (need_tracing_session
) {
3177 /* Create UST session if none exist. */
3178 if (cmd_ctx
->session
->ust_session
== NULL
) {
3179 ret
= create_ust_session(cmd_ctx
->session
,
3180 &cmd_ctx
->lsm
->domain
);
3181 if (ret
!= LTTNG_OK
) {
3186 /* Start the UST consumer daemons */
3188 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3189 if (config
.consumerd64_bin_path
.value
&&
3190 ustconsumer64_data
.pid
== 0 &&
3191 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3192 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3193 ret
= start_consumerd(&ustconsumer64_data
);
3195 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3196 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3200 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3201 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3203 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3207 * Setup socket for consumer 64 bit. No need for atomic access
3208 * since it was set above and can ONLY be set in this thread.
3210 ret
= consumer_create_socket(&ustconsumer64_data
,
3211 cmd_ctx
->session
->ust_session
->consumer
);
3217 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3218 if (config
.consumerd32_bin_path
.value
&&
3219 ustconsumer32_data
.pid
== 0 &&
3220 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3221 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3222 ret
= start_consumerd(&ustconsumer32_data
);
3224 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3225 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3229 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3230 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3232 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3236 * Setup socket for consumer 64 bit. No need for atomic access
3237 * since it was set above and can ONLY be set in this thread.
3239 ret
= consumer_create_socket(&ustconsumer32_data
,
3240 cmd_ctx
->session
->ust_session
->consumer
);
3252 /* Validate consumer daemon state when start/stop trace command */
3253 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3254 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3255 switch (cmd_ctx
->lsm
->domain
.type
) {
3256 case LTTNG_DOMAIN_NONE
:
3258 case LTTNG_DOMAIN_JUL
:
3259 case LTTNG_DOMAIN_LOG4J
:
3260 case LTTNG_DOMAIN_PYTHON
:
3261 case LTTNG_DOMAIN_UST
:
3262 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3263 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3267 case LTTNG_DOMAIN_KERNEL
:
3268 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3269 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3274 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3280 * Check that the UID or GID match that of the tracing session.
3281 * The root user can interact with all sessions.
3283 if (need_tracing_session
) {
3284 if (!session_access_ok(cmd_ctx
->session
,
3285 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3286 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3287 ret
= LTTNG_ERR_EPERM
;
3293 * Send relayd information to consumer as soon as we have a domain and a
3296 if (cmd_ctx
->session
&& need_domain
) {
3298 * Setup relayd if not done yet. If the relayd information was already
3299 * sent to the consumer, this call will gracefully return.
3301 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3302 if (ret
!= LTTNG_OK
) {
3307 /* Process by command type */
3308 switch (cmd_ctx
->lsm
->cmd_type
) {
3309 case LTTNG_ADD_CONTEXT
:
3312 * An LTTNG_ADD_CONTEXT command might have a supplementary
3313 * payload if the context being added is an application context.
3315 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3316 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3317 char *provider_name
= NULL
, *context_name
= NULL
;
3318 size_t provider_name_len
=
3319 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3320 size_t context_name_len
=
3321 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3323 if (provider_name_len
== 0 || context_name_len
== 0) {
3325 * Application provider and context names MUST
3328 ret
= -LTTNG_ERR_INVALID
;
3332 provider_name
= zmalloc(provider_name_len
+ 1);
3333 if (!provider_name
) {
3334 ret
= -LTTNG_ERR_NOMEM
;
3337 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3340 context_name
= zmalloc(context_name_len
+ 1);
3341 if (!context_name
) {
3342 ret
= -LTTNG_ERR_NOMEM
;
3343 goto error_add_context
;
3345 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3348 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3351 goto error_add_context
;
3354 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3357 goto error_add_context
;
3362 * cmd_add_context assumes ownership of the provider and context
3365 ret
= cmd_add_context(cmd_ctx
->session
,
3366 cmd_ctx
->lsm
->domain
.type
,
3367 cmd_ctx
->lsm
->u
.context
.channel_name
,
3368 &cmd_ctx
->lsm
->u
.context
.ctx
,
3369 kernel_poll_pipe
[1]);
3371 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3372 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3374 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3375 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3381 case LTTNG_DISABLE_CHANNEL
:
3383 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3384 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3387 case LTTNG_DISABLE_EVENT
:
3391 * FIXME: handle filter; for now we just receive the filter's
3392 * bytecode along with the filter expression which are sent by
3393 * liblttng-ctl and discard them.
3395 * This fixes an issue where the client may block while sending
3396 * the filter payload and encounter an error because the session
3397 * daemon closes the socket without ever handling this data.
3399 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3400 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3403 char data
[LTTNG_FILTER_MAX_LEN
];
3405 DBG("Discarding disable event command payload of size %zu", count
);
3407 ret
= lttcomm_recv_unix_sock(sock
, data
,
3408 count
> sizeof(data
) ? sizeof(data
) : count
);
3413 count
-= (size_t) ret
;
3416 /* FIXME: passing packed structure to non-packed pointer */
3417 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3418 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3419 &cmd_ctx
->lsm
->u
.disable
.event
);
3422 case LTTNG_ENABLE_CHANNEL
:
3424 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3425 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3426 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3427 &cmd_ctx
->lsm
->u
.channel
.chan
,
3428 kernel_poll_pipe
[1]);
3431 case LTTNG_TRACK_PID
:
3433 ret
= cmd_track_pid(cmd_ctx
->session
,
3434 cmd_ctx
->lsm
->domain
.type
,
3435 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3438 case LTTNG_UNTRACK_PID
:
3440 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3441 cmd_ctx
->lsm
->domain
.type
,
3442 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3445 case LTTNG_ENABLE_EVENT
:
3447 struct lttng_event_exclusion
*exclusion
= NULL
;
3448 struct lttng_filter_bytecode
*bytecode
= NULL
;
3449 char *filter_expression
= NULL
;
3451 /* Handle exclusion events and receive it from the client. */
3452 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3453 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3455 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3456 (count
* LTTNG_SYMBOL_NAME_LEN
));
3458 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3462 DBG("Receiving var len exclusion event list from client ...");
3463 exclusion
->count
= count
;
3464 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3465 count
* LTTNG_SYMBOL_NAME_LEN
);
3467 DBG("Nothing recv() from client var len data... continuing");
3470 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3475 /* Get filter expression from client. */
3476 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3477 size_t expression_len
=
3478 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3480 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3481 ret
= LTTNG_ERR_FILTER_INVAL
;
3486 filter_expression
= zmalloc(expression_len
);
3487 if (!filter_expression
) {
3489 ret
= LTTNG_ERR_FILTER_NOMEM
;
3493 /* Receive var. len. data */
3494 DBG("Receiving var len filter's expression from client ...");
3495 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3498 DBG("Nothing recv() from client car len data... continuing");
3500 free(filter_expression
);
3502 ret
= LTTNG_ERR_FILTER_INVAL
;
3507 /* Handle filter and get bytecode from client. */
3508 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3509 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3511 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3512 ret
= LTTNG_ERR_FILTER_INVAL
;
3513 free(filter_expression
);
3518 bytecode
= zmalloc(bytecode_len
);
3520 free(filter_expression
);
3522 ret
= LTTNG_ERR_FILTER_NOMEM
;
3526 /* Receive var. len. data */
3527 DBG("Receiving var len filter's bytecode from client ...");
3528 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3530 DBG("Nothing recv() from client car len data... continuing");
3532 free(filter_expression
);
3535 ret
= LTTNG_ERR_FILTER_INVAL
;
3539 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3540 free(filter_expression
);
3543 ret
= LTTNG_ERR_FILTER_INVAL
;
3548 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3549 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3550 &cmd_ctx
->lsm
->u
.enable
.event
,
3551 filter_expression
, bytecode
, exclusion
,
3552 kernel_poll_pipe
[1]);
3555 case LTTNG_LIST_TRACEPOINTS
:
3557 struct lttng_event
*events
;
3560 session_lock_list();
3561 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3562 session_unlock_list();
3563 if (nb_events
< 0) {
3564 /* Return value is a negative lttng_error_code. */
3570 * Setup lttng message with payload size set to the event list size in
3571 * bytes and then copy list into the llm payload.
3573 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3574 sizeof(struct lttng_event
) * nb_events
);
3584 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3586 struct lttng_event_field
*fields
;
3589 session_lock_list();
3590 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3592 session_unlock_list();
3593 if (nb_fields
< 0) {
3594 /* Return value is a negative lttng_error_code. */
3600 * Setup lttng message with payload size set to the event list size in
3601 * bytes and then copy list into the llm payload.
3603 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3604 sizeof(struct lttng_event_field
) * nb_fields
);
3614 case LTTNG_LIST_SYSCALLS
:
3616 struct lttng_event
*events
;
3619 nb_events
= cmd_list_syscalls(&events
);
3620 if (nb_events
< 0) {
3621 /* Return value is a negative lttng_error_code. */
3627 * Setup lttng message with payload size set to the event list size in
3628 * bytes and then copy list into the llm payload.
3630 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3631 sizeof(struct lttng_event
) * nb_events
);
3641 case LTTNG_LIST_TRACKER_PIDS
:
3643 int32_t *pids
= NULL
;
3646 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3647 cmd_ctx
->lsm
->domain
.type
, &pids
);
3649 /* Return value is a negative lttng_error_code. */
3655 * Setup lttng message with payload size set to the event list size in
3656 * bytes and then copy list into the llm payload.
3658 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3659 sizeof(int32_t) * nr_pids
);
3669 case LTTNG_SET_CONSUMER_URI
:
3672 struct lttng_uri
*uris
;
3674 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3675 len
= nb_uri
* sizeof(struct lttng_uri
);
3678 ret
= LTTNG_ERR_INVALID
;
3682 uris
= zmalloc(len
);
3684 ret
= LTTNG_ERR_FATAL
;
3688 /* Receive variable len data */
3689 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3690 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3692 DBG("No URIs received from client... continuing");
3694 ret
= LTTNG_ERR_SESSION_FAIL
;
3699 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3701 if (ret
!= LTTNG_OK
) {
3708 case LTTNG_START_TRACE
:
3711 * On the first start, if we have a kernel session and we have
3712 * enabled time or size-based rotations, we have to make sure
3713 * the kernel tracer supports it.
3715 if (!cmd_ctx
->session
->has_been_started
&& \
3716 cmd_ctx
->session
->kernel_session
&& \
3717 (cmd_ctx
->session
->rotate_timer_period
|| \
3718 cmd_ctx
->session
->rotate_size
) && \
3719 !check_rotate_compatible()) {
3720 DBG("Kernel tracer version is not compatible with the rotation feature");
3721 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3724 ret
= cmd_start_trace(cmd_ctx
->session
);
3727 case LTTNG_STOP_TRACE
:
3729 ret
= cmd_stop_trace(cmd_ctx
->session
);
3732 case LTTNG_CREATE_SESSION
:
3735 struct lttng_uri
*uris
= NULL
;
3737 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3738 len
= nb_uri
* sizeof(struct lttng_uri
);
3741 uris
= zmalloc(len
);
3743 ret
= LTTNG_ERR_FATAL
;
3747 /* Receive variable len data */
3748 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3749 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3751 DBG("No URIs received from client... continuing");
3753 ret
= LTTNG_ERR_SESSION_FAIL
;
3758 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3759 DBG("Creating session with ONE network URI is a bad call");
3760 ret
= LTTNG_ERR_SESSION_FAIL
;
3766 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3767 &cmd_ctx
->creds
, 0);
3773 case LTTNG_DESTROY_SESSION
:
3775 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1],
3776 notification_thread_handle
);
3778 /* Set session to NULL so we do not unlock it after free. */
3779 cmd_ctx
->session
= NULL
;
3782 case LTTNG_LIST_DOMAINS
:
3785 struct lttng_domain
*domains
= NULL
;
3787 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3789 /* Return value is a negative lttng_error_code. */
3794 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3795 nb_dom
* sizeof(struct lttng_domain
));
3805 case LTTNG_LIST_CHANNELS
:
3807 ssize_t payload_size
;
3808 struct lttng_channel
*channels
= NULL
;
3810 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3811 cmd_ctx
->session
, &channels
);
3812 if (payload_size
< 0) {
3813 /* Return value is a negative lttng_error_code. */
3814 ret
= -payload_size
;
3818 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3829 case LTTNG_LIST_EVENTS
:
3832 struct lttng_event
*events
= NULL
;
3833 struct lttcomm_event_command_header cmd_header
;
3836 memset(&cmd_header
, 0, sizeof(cmd_header
));
3837 /* Extended infos are included at the end of events */
3838 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3839 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3840 &events
, &total_size
);
3843 /* Return value is a negative lttng_error_code. */
3848 cmd_header
.nb_events
= nb_event
;
3849 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3850 &cmd_header
, sizeof(cmd_header
));
3860 case LTTNG_LIST_SESSIONS
:
3862 unsigned int nr_sessions
;
3863 void *sessions_payload
;
3866 session_lock_list();
3867 nr_sessions
= lttng_sessions_count(
3868 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3869 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3870 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3871 sessions_payload
= zmalloc(payload_len
);
3873 if (!sessions_payload
) {
3874 session_unlock_list();
3879 cmd_list_lttng_sessions(sessions_payload
,
3880 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3881 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3882 session_unlock_list();
3884 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3886 free(sessions_payload
);
3895 case LTTNG_REGISTER_CONSUMER
:
3897 struct consumer_data
*cdata
;
3899 switch (cmd_ctx
->lsm
->domain
.type
) {
3900 case LTTNG_DOMAIN_KERNEL
:
3901 cdata
= &kconsumer_data
;
3904 ret
= LTTNG_ERR_UND
;
3908 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3909 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3912 case LTTNG_DATA_PENDING
:
3915 uint8_t pending_ret_byte
;
3917 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3922 * This function may returns 0 or 1 to indicate whether or not
3923 * there is data pending. In case of error, it should return an
3924 * LTTNG_ERR code. However, some code paths may still return
3925 * a nondescript error code, which we handle by returning an
3928 if (pending_ret
== 0 || pending_ret
== 1) {
3930 * ret will be set to LTTNG_OK at the end of
3933 } else if (pending_ret
< 0) {
3934 ret
= LTTNG_ERR_UNK
;
3941 pending_ret_byte
= (uint8_t) pending_ret
;
3943 /* 1 byte to return whether or not data is pending */
3944 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3945 &pending_ret_byte
, 1);
3954 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3956 struct lttcomm_lttng_output_id reply
;
3958 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3959 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3960 if (ret
!= LTTNG_OK
) {
3964 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3970 /* Copy output list into message payload */
3974 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3976 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3977 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3980 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3983 struct lttng_snapshot_output
*outputs
= NULL
;
3985 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3986 if (nb_output
< 0) {
3991 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3992 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3993 nb_output
* sizeof(struct lttng_snapshot_output
));
4003 case LTTNG_SNAPSHOT_RECORD
:
4005 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4006 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4007 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4010 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4013 struct lttng_uri
*uris
= NULL
;
4015 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4016 len
= nb_uri
* sizeof(struct lttng_uri
);
4019 uris
= zmalloc(len
);
4021 ret
= LTTNG_ERR_FATAL
;
4025 /* Receive variable len data */
4026 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4027 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4029 DBG("No URIs received from client... continuing");
4031 ret
= LTTNG_ERR_SESSION_FAIL
;
4036 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4037 DBG("Creating session with ONE network URI is a bad call");
4038 ret
= LTTNG_ERR_SESSION_FAIL
;
4044 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4045 nb_uri
, &cmd_ctx
->creds
);
4049 case LTTNG_CREATE_SESSION_LIVE
:
4052 struct lttng_uri
*uris
= NULL
;
4054 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4055 len
= nb_uri
* sizeof(struct lttng_uri
);
4058 uris
= zmalloc(len
);
4060 ret
= LTTNG_ERR_FATAL
;
4064 /* Receive variable len data */
4065 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4066 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4068 DBG("No URIs received from client... continuing");
4070 ret
= LTTNG_ERR_SESSION_FAIL
;
4075 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4076 DBG("Creating session with ONE network URI is a bad call");
4077 ret
= LTTNG_ERR_SESSION_FAIL
;
4083 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4084 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4088 case LTTNG_SAVE_SESSION
:
4090 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4094 case LTTNG_SET_SESSION_SHM_PATH
:
4096 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4097 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4100 case LTTNG_REGENERATE_METADATA
:
4102 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4105 case LTTNG_REGENERATE_STATEDUMP
:
4107 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4110 case LTTNG_REGISTER_TRIGGER
:
4112 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4113 notification_thread_handle
);
4116 case LTTNG_UNREGISTER_TRIGGER
:
4118 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4119 notification_thread_handle
);
4122 case LTTNG_ROTATE_SESSION
:
4124 struct lttng_rotate_session_return rotate_return
;
4126 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4128 memset(&rotate_return
, 0, sizeof(rotate_return
));
4129 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4130 DBG("Kernel tracer version is not compatible with the rotation feature");
4131 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4135 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4141 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4142 sizeof(rotate_return
));
4151 case LTTNG_ROTATION_GET_INFO
:
4153 struct lttng_rotation_get_info_return get_info_return
;
4155 memset(&get_info_return
, 0, sizeof(get_info_return
));
4156 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4157 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4163 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4164 sizeof(get_info_return
));
4173 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
4175 struct lttng_session_get_current_output_return output_return
;
4177 memset(&output_return
, 0, sizeof(output_return
));
4178 ret
= cmd_session_get_current_output(cmd_ctx
->session
,
4185 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &output_return
,
4186 sizeof(output_return
));
4195 case LTTNG_ROTATION_SET_SCHEDULE
:
4197 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4198 DBG("Kernel tracer version does not support session rotations");
4199 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4203 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4204 cmd_ctx
->lsm
->u
.rotate_setup
.timer_us
,
4205 cmd_ctx
->lsm
->u
.rotate_setup
.size
,
4206 notification_thread_handle
);
4207 if (ret
!= LTTNG_OK
) {
4213 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
4215 struct lttng_rotation_schedule_get_timer_period
*get_timer
;
4217 get_timer
= zmalloc(sizeof(struct lttng_rotation_schedule_get_timer_period
));
4222 get_timer
->rotate_timer
= cmd_ctx
->session
->rotate_timer_period
;
4224 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, get_timer
,
4225 sizeof(struct lttng_rotation_schedule_get_timer_period
));
4235 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
4237 struct lttng_rotation_schedule_get_size
*get_size
;
4239 get_size
= zmalloc(sizeof(struct lttng_rotation_schedule_get_size
));
4244 get_size
->rotate_size
= cmd_ctx
->session
->rotate_size
;
4246 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, get_size
,
4247 sizeof(struct lttng_rotation_schedule_get_size
));
4258 ret
= LTTNG_ERR_UND
;
4263 if (cmd_ctx
->llm
== NULL
) {
4264 DBG("Missing llm structure. Allocating one.");
4265 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4269 /* Set return code */
4270 cmd_ctx
->llm
->ret_code
= ret
;
4272 if (cmd_ctx
->session
) {
4273 session_unlock(cmd_ctx
->session
);
4275 if (need_tracing_session
) {
4276 session_unlock_list();
4279 assert(!rcu_read_ongoing());
4284 * Thread managing health check socket.
4286 static void *thread_manage_health(void *data
)
4288 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4289 uint32_t revents
, nb_fd
;
4290 struct lttng_poll_event events
;
4291 struct health_comm_msg msg
;
4292 struct health_comm_reply reply
;
4294 DBG("[thread] Manage health check started");
4296 rcu_register_thread();
4298 /* We might hit an error path before this is created. */
4299 lttng_poll_init(&events
);
4301 /* Create unix socket */
4302 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4304 ERR("Unable to create health check Unix socket");
4309 /* lttng health client socket path permissions */
4310 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4311 utils_get_group_id(config
.tracing_group_name
.value
));
4313 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4318 ret
= chmod(config
.health_unix_sock_path
.value
,
4319 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4321 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4328 * Set the CLOEXEC flag. Return code is useless because either way, the
4331 (void) utils_set_fd_cloexec(sock
);
4333 ret
= lttcomm_listen_unix_sock(sock
);
4339 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4340 * more will be added to this poll set.
4342 ret
= sessiond_set_thread_pollset(&events
, 2);
4347 /* Add the application registration socket */
4348 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4353 sessiond_notify_ready();
4356 DBG("Health check ready");
4358 /* Inifinite blocking call, waiting for transmission */
4360 ret
= lttng_poll_wait(&events
, -1);
4363 * Restart interrupted system call.
4365 if (errno
== EINTR
) {
4373 for (i
= 0; i
< nb_fd
; i
++) {
4374 /* Fetch once the poll data */
4375 revents
= LTTNG_POLL_GETEV(&events
, i
);
4376 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4379 /* No activity for this FD (poll implementation). */
4383 /* Thread quit pipe has been closed. Killing thread. */
4384 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4390 /* Event on the registration socket */
4391 if (pollfd
== sock
) {
4392 if (revents
& LPOLLIN
) {
4394 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4395 ERR("Health socket poll error");
4398 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4404 new_sock
= lttcomm_accept_unix_sock(sock
);
4410 * Set the CLOEXEC flag. Return code is useless because either way, the
4413 (void) utils_set_fd_cloexec(new_sock
);
4415 DBG("Receiving data from client for health...");
4416 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4418 DBG("Nothing recv() from client... continuing");
4419 ret
= close(new_sock
);
4426 rcu_thread_online();
4428 memset(&reply
, 0, sizeof(reply
));
4429 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4431 * health_check_state returns 0 if health is
4434 if (!health_check_state(health_sessiond
, i
)) {
4435 reply
.ret_code
|= 1ULL << i
;
4439 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4441 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4443 ERR("Failed to send health data back to client");
4446 /* End of transmission */
4447 ret
= close(new_sock
);
4456 ERR("Health error occurred in %s", __func__
);
4458 DBG("Health check thread dying");
4459 unlink(config
.health_unix_sock_path
.value
);
4467 lttng_poll_clean(&events
);
4469 rcu_unregister_thread();
4474 * This thread manage all clients request using the unix client socket for
4477 static void *thread_manage_clients(void *data
)
4479 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4481 uint32_t revents
, nb_fd
;
4482 struct command_ctx
*cmd_ctx
= NULL
;
4483 struct lttng_poll_event events
;
4485 DBG("[thread] Manage client started");
4487 rcu_register_thread();
4489 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4491 health_code_update();
4493 ret
= lttcomm_listen_unix_sock(client_sock
);
4499 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4500 * more will be added to this poll set.
4502 ret
= sessiond_set_thread_pollset(&events
, 2);
4504 goto error_create_poll
;
4507 /* Add the application registration socket */
4508 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4513 sessiond_notify_ready();
4514 ret
= sem_post(&load_info
->message_thread_ready
);
4516 PERROR("sem_post message_thread_ready");
4520 /* This testpoint is after we signal readiness to the parent. */
4521 if (testpoint(sessiond_thread_manage_clients
)) {
4525 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4529 health_code_update();
4532 DBG("Accepting client command ...");
4534 /* Inifinite blocking call, waiting for transmission */
4536 health_poll_entry();
4537 ret
= lttng_poll_wait(&events
, -1);
4541 * Restart interrupted system call.
4543 if (errno
== EINTR
) {
4551 for (i
= 0; i
< nb_fd
; i
++) {
4552 /* Fetch once the poll data */
4553 revents
= LTTNG_POLL_GETEV(&events
, i
);
4554 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4556 health_code_update();
4559 /* No activity for this FD (poll implementation). */
4563 /* Thread quit pipe has been closed. Killing thread. */
4564 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4570 /* Event on the registration socket */
4571 if (pollfd
== client_sock
) {
4572 if (revents
& LPOLLIN
) {
4574 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4575 ERR("Client socket poll error");
4578 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4584 DBG("Wait for client response");
4586 health_code_update();
4588 sock
= lttcomm_accept_unix_sock(client_sock
);
4594 * Set the CLOEXEC flag. Return code is useless because either way, the
4597 (void) utils_set_fd_cloexec(sock
);
4599 /* Set socket option for credentials retrieval */
4600 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4605 /* Allocate context command to process the client request */
4606 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4607 if (cmd_ctx
== NULL
) {
4608 PERROR("zmalloc cmd_ctx");
4612 /* Allocate data buffer for reception */
4613 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4614 if (cmd_ctx
->lsm
== NULL
) {
4615 PERROR("zmalloc cmd_ctx->lsm");
4619 cmd_ctx
->llm
= NULL
;
4620 cmd_ctx
->session
= NULL
;
4622 health_code_update();
4625 * Data is received from the lttng client. The struct
4626 * lttcomm_session_msg (lsm) contains the command and data request of
4629 DBG("Receiving data from client ...");
4630 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4631 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4633 DBG("Nothing recv() from client... continuing");
4639 clean_command_ctx(&cmd_ctx
);
4643 health_code_update();
4645 // TODO: Validate cmd_ctx including sanity check for
4646 // security purpose.
4648 rcu_thread_online();
4650 * This function dispatch the work to the kernel or userspace tracer
4651 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4652 * informations for the client. The command context struct contains
4653 * everything this function may needs.
4655 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4656 rcu_thread_offline();
4664 * TODO: Inform client somehow of the fatal error. At
4665 * this point, ret < 0 means that a zmalloc failed
4666 * (ENOMEM). Error detected but still accept
4667 * command, unless a socket error has been
4670 clean_command_ctx(&cmd_ctx
);
4674 health_code_update();
4676 DBG("Sending response (size: %d, retcode: %s (%d))",
4677 cmd_ctx
->lttng_msg_size
,
4678 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4679 cmd_ctx
->llm
->ret_code
);
4680 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4682 ERR("Failed to send data back to client");
4685 /* End of transmission */
4692 clean_command_ctx(&cmd_ctx
);
4694 health_code_update();
4706 lttng_poll_clean(&events
);
4707 clean_command_ctx(&cmd_ctx
);
4711 unlink(config
.client_unix_sock_path
.value
);
4712 if (client_sock
>= 0) {
4713 ret
= close(client_sock
);
4721 ERR("Health error occurred in %s", __func__
);
4724 health_unregister(health_sessiond
);
4726 DBG("Client thread dying");
4728 rcu_unregister_thread();
4731 * Since we are creating the consumer threads, we own them, so we need
4732 * to join them before our thread exits.
4734 ret
= join_consumer_thread(&kconsumer_data
);
4737 PERROR("join_consumer");
4740 ret
= join_consumer_thread(&ustconsumer32_data
);
4743 PERROR("join_consumer ust32");
4746 ret
= join_consumer_thread(&ustconsumer64_data
);
4749 PERROR("join_consumer ust64");
4754 static int string_match(const char *str1
, const char *str2
)
4756 return (str1
&& str2
) && !strcmp(str1
, str2
);
4760 * Take an option from the getopt output and set it in the right variable to be
4763 * Return 0 on success else a negative value.
4765 static int set_option(int opt
, const char *arg
, const char *optname
)
4769 if (string_match(optname
, "client-sock") || opt
== 'c') {
4770 if (!arg
|| *arg
== '\0') {
4774 if (lttng_is_setuid_setgid()) {
4775 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4776 "-c, --client-sock");
4778 config_string_set(&config
.client_unix_sock_path
,
4780 if (!config
.client_unix_sock_path
.value
) {
4785 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4786 if (!arg
|| *arg
== '\0') {
4790 if (lttng_is_setuid_setgid()) {
4791 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4794 config_string_set(&config
.apps_unix_sock_path
,
4796 if (!config
.apps_unix_sock_path
.value
) {
4801 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4802 config
.daemonize
= true;
4803 } else if (string_match(optname
, "background") || opt
== 'b') {
4804 config
.background
= true;
4805 } else if (string_match(optname
, "group") || opt
== 'g') {
4806 if (!arg
|| *arg
== '\0') {
4810 if (lttng_is_setuid_setgid()) {
4811 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4814 config_string_set(&config
.tracing_group_name
,
4816 if (!config
.tracing_group_name
.value
) {
4821 } else if (string_match(optname
, "help") || opt
== 'h') {
4822 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4824 ERR("Cannot show --help for `lttng-sessiond`");
4827 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4828 } else if (string_match(optname
, "version") || opt
== 'V') {
4829 fprintf(stdout
, "%s\n", VERSION
);
4831 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4832 config
.sig_parent
= true;
4833 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4834 if (!arg
|| *arg
== '\0') {
4838 if (lttng_is_setuid_setgid()) {
4839 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4840 "--kconsumerd-err-sock");
4842 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4844 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4849 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4850 if (!arg
|| *arg
== '\0') {
4854 if (lttng_is_setuid_setgid()) {
4855 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4856 "--kconsumerd-cmd-sock");
4858 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4860 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4865 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4866 if (!arg
|| *arg
== '\0') {
4870 if (lttng_is_setuid_setgid()) {
4871 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4872 "--ustconsumerd64-err-sock");
4874 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4876 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4881 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4882 if (!arg
|| *arg
== '\0') {
4886 if (lttng_is_setuid_setgid()) {
4887 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4888 "--ustconsumerd64-cmd-sock");
4890 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4892 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4897 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4898 if (!arg
|| *arg
== '\0') {
4902 if (lttng_is_setuid_setgid()) {
4903 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4904 "--ustconsumerd32-err-sock");
4906 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4908 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4913 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4914 if (!arg
|| *arg
== '\0') {
4918 if (lttng_is_setuid_setgid()) {
4919 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4920 "--ustconsumerd32-cmd-sock");
4922 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4924 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4929 } else if (string_match(optname
, "no-kernel")) {
4930 config
.no_kernel
= true;
4931 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4932 lttng_opt_quiet
= true;
4933 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4934 /* Verbose level can increase using multiple -v */
4936 /* Value obtained from config file */
4937 config
.verbose
= config_parse_value(arg
);
4939 /* -v used on command line */
4942 /* Clamp value to [0, 3] */
4943 config
.verbose
= config
.verbose
< 0 ? 0 :
4944 (config
.verbose
<= 3 ? config
.verbose
: 3);
4945 } else if (string_match(optname
, "verbose-consumer")) {
4947 config
.verbose_consumer
= config_parse_value(arg
);
4949 config
.verbose_consumer
++;
4951 } else if (string_match(optname
, "consumerd32-path")) {
4952 if (!arg
|| *arg
== '\0') {
4956 if (lttng_is_setuid_setgid()) {
4957 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4958 "--consumerd32-path");
4960 config_string_set(&config
.consumerd32_bin_path
,
4962 if (!config
.consumerd32_bin_path
.value
) {
4967 } else if (string_match(optname
, "consumerd32-libdir")) {
4968 if (!arg
|| *arg
== '\0') {
4972 if (lttng_is_setuid_setgid()) {
4973 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4974 "--consumerd32-libdir");
4976 config_string_set(&config
.consumerd32_lib_dir
,
4978 if (!config
.consumerd32_lib_dir
.value
) {
4983 } else if (string_match(optname
, "consumerd64-path")) {
4984 if (!arg
|| *arg
== '\0') {
4988 if (lttng_is_setuid_setgid()) {
4989 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4990 "--consumerd64-path");
4992 config_string_set(&config
.consumerd64_bin_path
,
4994 if (!config
.consumerd64_bin_path
.value
) {
4999 } else if (string_match(optname
, "consumerd64-libdir")) {
5000 if (!arg
|| *arg
== '\0') {
5004 if (lttng_is_setuid_setgid()) {
5005 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5006 "--consumerd64-libdir");
5008 config_string_set(&config
.consumerd64_lib_dir
,
5010 if (!config
.consumerd64_lib_dir
.value
) {
5015 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
5016 if (!arg
|| *arg
== '\0') {
5020 if (lttng_is_setuid_setgid()) {
5021 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5024 config_string_set(&config
.pid_file_path
, strdup(arg
));
5025 if (!config
.pid_file_path
.value
) {
5030 } else if (string_match(optname
, "agent-tcp-port")) {
5031 if (!arg
|| *arg
== '\0') {
5035 if (lttng_is_setuid_setgid()) {
5036 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5037 "--agent-tcp-port");
5042 v
= strtoul(arg
, NULL
, 0);
5043 if (errno
!= 0 || !isdigit(arg
[0])) {
5044 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5047 if (v
== 0 || v
>= 65535) {
5048 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5051 config
.agent_tcp_port
= (uint32_t) v
;
5052 DBG3("Agent TCP port set to non default: %u", config
.agent_tcp_port
);
5054 } else if (string_match(optname
, "load") || opt
== 'l') {
5055 if (!arg
|| *arg
== '\0') {
5059 if (lttng_is_setuid_setgid()) {
5060 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5063 config_string_set(&config
.load_session_path
, strdup(arg
));
5064 if (!config
.load_session_path
.value
) {
5069 } else if (string_match(optname
, "kmod-probes")) {
5070 if (!arg
|| *arg
== '\0') {
5074 if (lttng_is_setuid_setgid()) {
5075 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5078 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
5079 if (!config
.kmod_probes_list
.value
) {
5084 } else if (string_match(optname
, "extra-kmod-probes")) {
5085 if (!arg
|| *arg
== '\0') {
5089 if (lttng_is_setuid_setgid()) {
5090 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5091 "--extra-kmod-probes");
5093 config_string_set(&config
.kmod_extra_probes_list
,
5095 if (!config
.kmod_extra_probes_list
.value
) {
5100 } else if (string_match(optname
, "config") || opt
== 'f') {
5101 /* This is handled in set_options() thus silent skip. */
5104 /* Unknown option or other error.
5105 * Error is printed by getopt, just return */
5110 if (ret
== -EINVAL
) {
5111 const char *opt_name
= "unknown";
5114 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5116 if (opt
== long_options
[i
].val
) {
5117 opt_name
= long_options
[i
].name
;
5122 WARN("Invalid argument provided for option \"%s\", using default value.",
5130 * config_entry_handler_cb used to handle options read from a config file.
5131 * See config_entry_handler_cb comment in common/config/session-config.h for the
5132 * return value conventions.
5134 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5138 if (!entry
|| !entry
->name
|| !entry
->value
) {
5143 /* Check if the option is to be ignored */
5144 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5145 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5150 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5153 /* Ignore if not fully matched. */
5154 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5159 * If the option takes no argument on the command line, we have to
5160 * check if the value is "true". We support non-zero numeric values,
5163 if (!long_options
[i
].has_arg
) {
5164 ret
= config_parse_value(entry
->value
);
5167 WARN("Invalid configuration value \"%s\" for option %s",
5168 entry
->value
, entry
->name
);
5170 /* False, skip boolean config option. */
5175 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5179 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5186 * daemon configuration loading and argument parsing
5188 static int set_options(int argc
, char **argv
)
5190 int ret
= 0, c
= 0, option_index
= 0;
5191 int orig_optopt
= optopt
, orig_optind
= optind
;
5193 const char *config_path
= NULL
;
5195 optstring
= utils_generate_optstring(long_options
,
5196 sizeof(long_options
) / sizeof(struct option
));
5202 /* Check for the --config option */
5203 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5204 &option_index
)) != -1) {
5208 } else if (c
!= 'f') {
5209 /* if not equal to --config option. */
5213 if (lttng_is_setuid_setgid()) {
5214 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5217 config_path
= utils_expand_path(optarg
);
5219 ERR("Failed to resolve path: %s", optarg
);
5224 ret
= config_get_section_entries(config_path
, config_section_name
,
5225 config_entry_handler
, NULL
);
5228 ERR("Invalid configuration option at line %i", ret
);
5234 /* Reset getopt's global state */
5235 optopt
= orig_optopt
;
5236 optind
= orig_optind
;
5240 * getopt_long() will not set option_index if it encounters a
5243 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5250 * Pass NULL as the long option name if popt left the index
5253 ret
= set_option(c
, optarg
,
5254 option_index
< 0 ? NULL
:
5255 long_options
[option_index
].name
);
5267 * Creates the two needed socket by the daemon.
5268 * apps_sock - The communication socket for all UST apps.
5269 * client_sock - The communication of the cli tool (lttng).
5271 static int init_daemon_socket(void)
5276 old_umask
= umask(0);
5278 /* Create client tool unix socket */
5279 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5280 if (client_sock
< 0) {
5281 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5286 /* Set the cloexec flag */
5287 ret
= utils_set_fd_cloexec(client_sock
);
5289 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5290 "Continuing but note that the consumer daemon will have a "
5291 "reference to this socket on exec()", client_sock
);
5294 /* File permission MUST be 660 */
5295 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5297 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5302 /* Create the application unix socket */
5303 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5304 if (apps_sock
< 0) {
5305 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5310 /* Set the cloexec flag */
5311 ret
= utils_set_fd_cloexec(apps_sock
);
5313 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5314 "Continuing but note that the consumer daemon will have a "
5315 "reference to this socket on exec()", apps_sock
);
5318 /* File permission MUST be 666 */
5319 ret
= chmod(config
.apps_unix_sock_path
.value
,
5320 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5322 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5327 DBG3("Session daemon client socket %d and application socket %d created",
5328 client_sock
, apps_sock
);
5336 * Check if the global socket is available, and if a daemon is answering at the
5337 * other side. If yes, error is returned.
5339 static int check_existing_daemon(void)
5341 /* Is there anybody out there ? */
5342 if (lttng_session_daemon_alive()) {
5350 * Set the tracing group gid onto the client socket.
5352 * Race window between mkdir and chown is OK because we are going from more
5353 * permissive (root.root) to less permissive (root.tracing).
5355 static int set_permissions(char *rundir
)
5360 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5362 /* Set lttng run dir */
5363 ret
= chown(rundir
, 0, gid
);
5365 ERR("Unable to set group on %s", rundir
);
5370 * Ensure all applications and tracing group can search the run
5371 * dir. Allow everyone to read the directory, since it does not
5372 * buy us anything to hide its content.
5374 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5376 ERR("Unable to set permissions on %s", rundir
);
5380 /* lttng client socket path */
5381 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5383 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5387 /* kconsumer error socket path */
5388 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5390 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5394 /* 64-bit ustconsumer error socket path */
5395 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5397 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5401 /* 32-bit ustconsumer compat32 error socket path */
5402 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5404 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5408 DBG("All permissions are set");
5414 * Create the lttng run directory needed for all global sockets and pipe.
5416 static int create_lttng_rundir(void)
5420 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5422 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5424 if (errno
!= EEXIST
) {
5425 ERR("Unable to create %s", config
.rundir
.value
);
5437 * Setup sockets and directory needed by the consumerds' communication with the
5440 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5445 switch (consumer_data
->type
) {
5446 case LTTNG_CONSUMER_KERNEL
:
5447 path
= config
.kconsumerd_path
.value
;
5449 case LTTNG_CONSUMER64_UST
:
5450 path
= config
.consumerd64_path
.value
;
5452 case LTTNG_CONSUMER32_UST
:
5453 path
= config
.consumerd32_path
.value
;
5456 ERR("Consumer type unknown");
5462 DBG2("Creating consumer directory: %s", path
);
5464 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5465 if (ret
< 0 && errno
!= EEXIST
) {
5467 ERR("Failed to create %s", path
);
5471 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5473 ERR("Unable to set group on %s", path
);
5479 /* Create the consumerd error unix socket */
5480 consumer_data
->err_sock
=
5481 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5482 if (consumer_data
->err_sock
< 0) {
5483 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5489 * Set the CLOEXEC flag. Return code is useless because either way, the
5492 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5494 PERROR("utils_set_fd_cloexec");
5495 /* continue anyway */
5498 /* File permission MUST be 660 */
5499 ret
= chmod(consumer_data
->err_unix_sock_path
,
5500 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5502 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5512 * Signal handler for the daemon
5514 * Simply stop all worker threads, leaving main() return gracefully after
5515 * joining all threads and calling cleanup().
5517 static void sighandler(int sig
)
5521 DBG("SIGINT caught");
5525 DBG("SIGTERM caught");
5529 CMM_STORE_SHARED(recv_child_signal
, 1);
5537 * Setup signal handler for :
5538 * SIGINT, SIGTERM, SIGPIPE
5540 static int set_signal_handler(void)
5543 struct sigaction sa
;
5546 if ((ret
= sigemptyset(&sigset
)) < 0) {
5547 PERROR("sigemptyset");
5551 sa
.sa_mask
= sigset
;
5554 sa
.sa_handler
= sighandler
;
5555 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5556 PERROR("sigaction");
5560 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5561 PERROR("sigaction");
5565 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5566 PERROR("sigaction");
5570 sa
.sa_handler
= SIG_IGN
;
5571 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5572 PERROR("sigaction");
5576 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5582 * Set open files limit to unlimited. This daemon can open a large number of
5583 * file descriptors in order to consume multiple kernel traces.
5585 static void set_ulimit(void)
5590 /* The kernel does not allow an infinite limit for open files */
5591 lim
.rlim_cur
= 65535;
5592 lim
.rlim_max
= 65535;
5594 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5596 PERROR("failed to set open files limit");
5600 static int write_pidfile(void)
5602 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5606 * Create lockfile using the rundir and return its fd.
5608 static int create_lockfile(void)
5610 return utils_create_lock_file(config
.lock_file_path
.value
);
5614 * Write agent TCP port using the rundir.
5616 static int write_agent_port(void)
5618 return utils_create_pid_file(config
.agent_tcp_port
,
5619 config
.agent_port_file_path
.value
);
5622 static int set_clock_plugin_env(void)
5625 char *env_value
= NULL
;
5627 if (!config
.lttng_ust_clock_plugin
.value
) {
5631 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5632 config
.lttng_ust_clock_plugin
.value
);
5638 ret
= putenv(env_value
);
5641 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5645 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5646 config
.lttng_ust_clock_plugin
.value
);
5652 struct rotation_thread_timer_queue
*create_rotate_timer_queue(void)
5654 struct rotation_thread_timer_queue
*queue
= NULL
;
5656 queue
= zmalloc(sizeof(struct rotation_thread_timer_queue
));
5658 PERROR("Failed to allocate timer rotate queue");
5662 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
5663 CDS_INIT_LIST_HEAD(&queue
->list
);
5664 pthread_mutex_init(&queue
->lock
, NULL
);
5671 void destroy_rotate_timer_queue(struct rotation_thread_timer_queue
*queue
)
5673 struct sessiond_rotation_timer
*node
, *tmp_node
;
5679 lttng_pipe_destroy(queue
->event_pipe
);
5681 pthread_mutex_lock(&queue
->lock
);
5682 /* Empty wait queue. */
5683 cds_list_for_each_entry_safe(node
, tmp_node
, &queue
->list
, head
) {
5684 cds_list_del(&node
->head
);
5687 pthread_mutex_unlock(&queue
->lock
);
5689 pthread_mutex_destroy(&queue
->lock
);
5696 int main(int argc
, char **argv
)
5698 int ret
= 0, retval
= 0;
5700 const char *env_app_timeout
;
5701 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5702 *ust64_channel_monitor_pipe
= NULL
,
5703 *kernel_channel_monitor_pipe
= NULL
;
5704 bool notification_thread_running
= false;
5705 bool rotation_thread_running
= false;
5706 bool timer_thread_running
= false;
5707 struct lttng_pipe
*ust32_channel_rotate_pipe
= NULL
,
5708 *ust64_channel_rotate_pipe
= NULL
,
5709 *kernel_channel_rotate_pipe
= NULL
;
5710 struct timer_thread_parameters timer_thread_ctx
;
5711 /* Queue of rotation jobs populated by the sessiond-timer. */
5712 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5713 sem_t notification_thread_ready
;
5715 init_kernel_workarounds();
5717 rcu_register_thread();
5719 if (set_signal_handler()) {
5721 goto exit_set_signal_handler
;
5724 if (sessiond_timer_signal_init()) {
5726 goto exit_set_signal_handler
;
5729 page_size
= sysconf(_SC_PAGESIZE
);
5730 if (page_size
< 0) {
5731 PERROR("sysconf _SC_PAGESIZE");
5732 page_size
= LONG_MAX
;
5733 WARN("Fallback page size to %ld", page_size
);
5736 ret
= sessiond_config_init(&config
);
5739 goto exit_set_signal_handler
;
5743 * Parse arguments and load the daemon configuration file.
5745 * We have an exit_options exit path to free memory reserved by
5746 * set_options. This is needed because the rest of sessiond_cleanup()
5747 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5748 * depends on set_options.
5751 if (set_options(argc
, argv
)) {
5756 /* Init config from environment variables. */
5757 sessiond_config_apply_env_config(&config
);
5760 * Resolve all paths received as arguments, configuration option, or
5761 * through environment variable as absolute paths. This is necessary
5762 * since daemonizing causes the sessiond's current working directory
5765 ret
= sessiond_config_resolve_paths(&config
);
5771 lttng_opt_verbose
= config
.verbose
;
5772 lttng_opt_quiet
= config
.quiet
;
5773 kconsumer_data
.err_unix_sock_path
=
5774 config
.kconsumerd_err_unix_sock_path
.value
;
5775 kconsumer_data
.cmd_unix_sock_path
=
5776 config
.kconsumerd_cmd_unix_sock_path
.value
;
5777 ustconsumer32_data
.err_unix_sock_path
=
5778 config
.consumerd32_err_unix_sock_path
.value
;
5779 ustconsumer32_data
.cmd_unix_sock_path
=
5780 config
.consumerd32_cmd_unix_sock_path
.value
;
5781 ustconsumer64_data
.err_unix_sock_path
=
5782 config
.consumerd64_err_unix_sock_path
.value
;
5783 ustconsumer64_data
.cmd_unix_sock_path
=
5784 config
.consumerd64_cmd_unix_sock_path
.value
;
5785 set_clock_plugin_env();
5787 sessiond_config_log(&config
);
5790 if (config
.daemonize
|| config
.background
) {
5793 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5794 !config
.background
);
5801 * We are in the child. Make sure all other file descriptors are
5802 * closed, in case we are called with more opened file
5803 * descriptors than the standard ones.
5805 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5810 if (run_as_create_worker(argv
[0]) < 0) {
5811 goto exit_create_run_as_worker_cleanup
;
5815 * Starting from here, we can create threads. This needs to be after
5816 * lttng_daemonize due to RCU.
5820 * Initialize the health check subsystem. This call should set the
5821 * appropriate time values.
5823 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5824 if (!health_sessiond
) {
5825 PERROR("health_app_create error");
5827 goto exit_health_sessiond_cleanup
;
5830 /* Create thread to clean up RCU hash tables */
5831 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5833 goto exit_ht_cleanup
;
5836 /* Create thread quit pipe */
5837 if (init_thread_quit_pipe()) {
5839 goto exit_init_data
;
5842 /* Check if daemon is UID = 0 */
5843 is_root
= !getuid();
5845 if (create_lttng_rundir()) {
5847 goto exit_init_data
;
5851 /* Create global run dir with root access */
5853 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5854 if (!kernel_channel_monitor_pipe
) {
5855 ERR("Failed to create kernel consumer channel monitor pipe");
5857 goto exit_init_data
;
5859 kconsumer_data
.channel_monitor_pipe
=
5860 lttng_pipe_release_writefd(
5861 kernel_channel_monitor_pipe
);
5862 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5864 goto exit_init_data
;
5866 kernel_channel_rotate_pipe
= lttng_pipe_open(0);
5867 if (!kernel_channel_rotate_pipe
) {
5868 ERR("Failed to create kernel consumer channel rotate pipe");
5870 goto exit_init_data
;
5872 kconsumer_data
.channel_rotate_pipe
=
5873 lttng_pipe_release_writefd(
5874 kernel_channel_rotate_pipe
);
5875 if (kconsumer_data
.channel_rotate_pipe
< 0) {
5877 goto exit_init_data
;
5881 lockfile_fd
= create_lockfile();
5882 if (lockfile_fd
< 0) {
5884 goto exit_init_data
;
5887 /* Set consumer initial state */
5888 kernel_consumerd_state
= CONSUMER_STOPPED
;
5889 ust_consumerd_state
= CONSUMER_STOPPED
;
5891 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5892 if (!ust32_channel_monitor_pipe
) {
5893 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5895 goto exit_init_data
;
5897 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5898 ust32_channel_monitor_pipe
);
5899 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5901 goto exit_init_data
;
5903 ust32_channel_rotate_pipe
= lttng_pipe_open(0);
5904 if (!ust32_channel_rotate_pipe
) {
5905 ERR("Failed to create 32-bit user space consumer channel rotate pipe");
5907 goto exit_init_data
;
5909 ustconsumer32_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
5910 ust32_channel_rotate_pipe
);
5911 if (ustconsumer32_data
.channel_rotate_pipe
< 0) {
5913 goto exit_init_data
;
5917 * The rotation_timer_queue structure is shared between the sessiond timer
5918 * thread and the rotation thread. The main() keeps the ownership and
5919 * destroys it when both threads have quit.
5921 rotation_timer_queue
= create_rotate_timer_queue();
5922 if (!rotation_timer_queue
) {
5924 goto exit_init_data
;
5926 timer_thread_ctx
.rotation_timer_queue
= rotation_timer_queue
;
5928 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5929 if (!ust64_channel_monitor_pipe
) {
5930 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5932 goto exit_init_data
;
5934 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5935 ust64_channel_monitor_pipe
);
5936 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5938 goto exit_init_data
;
5940 ust64_channel_rotate_pipe
= lttng_pipe_open(0);
5941 if (!ust64_channel_rotate_pipe
) {
5942 ERR("Failed to create 64-bit user space consumer channel rotate pipe");
5944 goto exit_init_data
;
5946 ustconsumer64_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
5947 ust64_channel_rotate_pipe
);
5948 if (ustconsumer64_data
.channel_rotate_pipe
< 0) {
5950 goto exit_init_data
;
5954 * See if daemon already exist.
5956 if (check_existing_daemon()) {
5957 ERR("Already running daemon.\n");
5959 * We do not goto exit because we must not cleanup()
5960 * because a daemon is already running.
5963 goto exit_init_data
;
5967 * Init UST app hash table. Alloc hash table before this point since
5968 * cleanup() can get called after that point.
5970 if (ust_app_ht_alloc()) {
5971 ERR("Failed to allocate UST app hash table");
5973 goto exit_init_data
;
5977 * Initialize agent app hash table. We allocate the hash table here
5978 * since cleanup() can get called after this point.
5980 if (agent_app_ht_alloc()) {
5981 ERR("Failed to allocate Agent app hash table");
5983 goto exit_init_data
;
5987 * These actions must be executed as root. We do that *after* setting up
5988 * the sockets path because we MUST make the check for another daemon using
5989 * those paths *before* trying to set the kernel consumer sockets and init
5993 if (set_consumer_sockets(&kconsumer_data
)) {
5995 goto exit_init_data
;
5998 /* Setup kernel tracer */
5999 if (!config
.no_kernel
) {
6000 init_kernel_tracer();
6001 if (kernel_tracer_fd
>= 0) {
6002 ret
= syscall_init_table();
6004 ERR("Unable to populate syscall table. "
6005 "Syscall tracing won't work "
6006 "for this session daemon.");
6011 /* Set ulimit for open files */
6014 /* init lttng_fd tracking must be done after set_ulimit. */
6017 if (set_consumer_sockets(&ustconsumer64_data
)) {
6019 goto exit_init_data
;
6022 if (set_consumer_sockets(&ustconsumer32_data
)) {
6024 goto exit_init_data
;
6027 /* Setup the needed unix socket */
6028 if (init_daemon_socket()) {
6030 goto exit_init_data
;
6033 /* Set credentials to socket */
6034 if (is_root
&& set_permissions(config
.rundir
.value
)) {
6036 goto exit_init_data
;
6039 /* Get parent pid if -S, --sig-parent is specified. */
6040 if (config
.sig_parent
) {
6044 /* Setup the kernel pipe for waking up the kernel thread */
6045 if (is_root
&& !config
.no_kernel
) {
6046 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6048 goto exit_init_data
;
6052 /* Setup the thread apps communication pipe. */
6053 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6055 goto exit_init_data
;
6058 /* Setup the thread apps notify communication pipe. */
6059 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6061 goto exit_init_data
;
6064 /* Initialize global buffer per UID and PID registry. */
6065 buffer_reg_init_uid_registry();
6066 buffer_reg_init_pid_registry();
6068 /* Init UST command queue. */
6069 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6072 * Get session list pointer. This pointer MUST NOT be free'd. This list
6073 * is statically declared in session.c
6075 session_list_ptr
= session_get_list();
6079 /* Check for the application socket timeout env variable. */
6080 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6081 if (env_app_timeout
) {
6082 config
.app_socket_timeout
= atoi(env_app_timeout
);
6084 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6087 ret
= write_pidfile();
6089 ERR("Error in write_pidfile");
6091 goto exit_init_data
;
6093 ret
= write_agent_port();
6095 ERR("Error in write_agent_port");
6097 goto exit_init_data
;
6100 /* Initialize communication library */
6102 /* Initialize TCP timeout values */
6103 lttcomm_inet_init();
6105 if (load_session_init_data(&load_info
) < 0) {
6107 goto exit_init_data
;
6109 load_info
->path
= config
.load_session_path
.value
;
6111 /* Create health-check thread. */
6112 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6113 thread_manage_health
, (void *) NULL
);
6116 PERROR("pthread_create health");
6122 * The rotation thread needs the notification thread to be ready before
6123 * creating the rotate_notification_channel, so we use this semaphore as
6124 * a rendez-vous point.
6126 sem_init(¬ification_thread_ready
, 0, 0);
6128 /* notification_thread_data acquires the pipes' read side. */
6129 notification_thread_handle
= notification_thread_handle_create(
6130 ust32_channel_monitor_pipe
,
6131 ust64_channel_monitor_pipe
,
6132 kernel_channel_monitor_pipe
,
6133 ¬ification_thread_ready
);
6134 if (!notification_thread_handle
) {
6136 ERR("Failed to create notification thread shared data");
6138 goto exit_notification
;
6141 /* Create notification thread. */
6142 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6143 thread_notification
, notification_thread_handle
);
6146 PERROR("pthread_create notification");
6149 goto exit_notification
;
6151 notification_thread_running
= true;
6153 /* Create timer thread. */
6154 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
6155 sessiond_timer_thread
, &timer_thread_ctx
);
6158 PERROR("pthread_create timer");
6161 goto exit_notification
;
6163 timer_thread_running
= true;
6165 /* rotation_thread_data acquires the pipes' read side. */
6166 rotation_thread_handle
= rotation_thread_handle_create(
6167 ust32_channel_rotate_pipe
,
6168 ust64_channel_rotate_pipe
,
6169 kernel_channel_rotate_pipe
,
6170 thread_quit_pipe
[0],
6171 rotation_timer_queue
,
6172 notification_thread_handle
,
6173 ¬ification_thread_ready
);
6174 if (!rotation_thread_handle
) {
6176 ERR("Failed to create rotation thread shared data");
6181 /* Create rotation thread. */
6182 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6183 thread_rotation
, rotation_thread_handle
);
6186 PERROR("pthread_create rotation");
6191 rotation_thread_running
= true;
6193 /* Create thread to manage the client socket */
6194 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6195 thread_manage_clients
, (void *) NULL
);
6198 PERROR("pthread_create clients");
6204 /* Create thread to dispatch registration */
6205 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6206 thread_dispatch_ust_registration
, (void *) NULL
);
6209 PERROR("pthread_create dispatch");
6215 /* Create thread to manage application registration. */
6216 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6217 thread_registration_apps
, (void *) NULL
);
6220 PERROR("pthread_create registration");
6226 /* Create thread to manage application socket */
6227 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6228 thread_manage_apps
, (void *) NULL
);
6231 PERROR("pthread_create apps");
6237 /* Create thread to manage application notify socket */
6238 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6239 ust_thread_manage_notify
, (void *) NULL
);
6242 PERROR("pthread_create notify");
6245 goto exit_apps_notify
;
6248 /* Create agent registration thread. */
6249 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6250 agent_thread_manage_registration
, (void *) NULL
);
6253 PERROR("pthread_create agent");
6256 goto exit_agent_reg
;
6259 /* Don't start this thread if kernel tracing is not requested nor root */
6260 if (is_root
&& !config
.no_kernel
) {
6261 /* Create kernel thread to manage kernel event */
6262 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6263 thread_manage_kernel
, (void *) NULL
);
6266 PERROR("pthread_create kernel");
6273 /* Create session loading thread. */
6274 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6275 thread_load_session
, load_info
);
6278 PERROR("pthread_create load_session_thread");
6281 goto exit_load_session
;
6285 * This is where we start awaiting program completion (e.g. through
6286 * signal that asks threads to teardown).
6289 ret
= pthread_join(load_session_thread
, &status
);
6292 PERROR("pthread_join load_session_thread");
6297 if (is_root
&& !config
.no_kernel
) {
6298 ret
= pthread_join(kernel_thread
, &status
);
6301 PERROR("pthread_join");
6307 ret
= pthread_join(agent_reg_thread
, &status
);
6310 PERROR("pthread_join agent");
6315 ret
= pthread_join(apps_notify_thread
, &status
);
6318 PERROR("pthread_join apps notify");
6323 ret
= pthread_join(apps_thread
, &status
);
6326 PERROR("pthread_join apps");
6331 ret
= pthread_join(reg_apps_thread
, &status
);
6334 PERROR("pthread_join");
6340 * Join dispatch thread after joining reg_apps_thread to ensure
6341 * we don't leak applications in the queue.
6343 ret
= pthread_join(dispatch_thread
, &status
);
6346 PERROR("pthread_join");
6351 ret
= pthread_join(client_thread
, &status
);
6354 PERROR("pthread_join");
6361 sem_destroy(¬ification_thread_ready
);
6362 ret
= pthread_join(health_thread
, &status
);
6365 PERROR("pthread_join health thread");
6372 * Wait for all pending call_rcu work to complete before tearing
6373 * down data structures. call_rcu worker may be trying to
6374 * perform lookups in those structures.
6378 * sessiond_cleanup() is called when no other thread is running, except
6379 * the ht_cleanup thread, which is needed to destroy the hash tables.
6381 rcu_thread_online();
6385 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6386 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6387 * the queue is empty before shutting down the clean-up thread.
6392 * The teardown of the notification system is performed after the
6393 * session daemon's teardown in order to allow it to be notified
6394 * of the active session and channels at the moment of the teardown.
6396 if (notification_thread_handle
) {
6397 if (notification_thread_running
) {
6398 notification_thread_command_quit(
6399 notification_thread_handle
);
6400 ret
= pthread_join(notification_thread
, &status
);
6403 PERROR("pthread_join notification thread");
6407 notification_thread_handle_destroy(notification_thread_handle
);
6410 if (rotation_thread_handle
) {
6411 if (rotation_thread_running
) {
6412 ret
= pthread_join(rotation_thread
, &status
);
6415 PERROR("pthread_join rotation thread");
6419 rotation_thread_handle_destroy(rotation_thread_handle
);
6422 if (timer_thread_running
) {
6423 kill(getpid(), LTTNG_SESSIOND_SIG_EXIT
);
6424 ret
= pthread_join(timer_thread
, &status
);
6427 PERROR("pthread_join timer thread");
6433 * After the rotation and timer thread have quit, we can safely destroy
6434 * the rotation_timer_queue.
6436 destroy_rotate_timer_queue(rotation_timer_queue
);
6438 rcu_thread_offline();
6439 rcu_unregister_thread();
6441 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6445 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6446 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6447 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6448 lttng_pipe_destroy(ust32_channel_rotate_pipe
);
6449 lttng_pipe_destroy(ust64_channel_rotate_pipe
);
6450 lttng_pipe_destroy(kernel_channel_rotate_pipe
);
6453 health_app_destroy(health_sessiond
);
6454 exit_health_sessiond_cleanup
:
6455 exit_create_run_as_worker_cleanup
:
6458 sessiond_cleanup_options();
6460 exit_set_signal_handler
: