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]);
602 wait_consumer(&kconsumer_data
);
603 wait_consumer(&ustconsumer64_data
);
604 wait_consumer(&ustconsumer32_data
);
606 DBG("Cleaning up all agent apps");
607 agent_app_ht_clean();
609 DBG("Closing all UST sockets");
610 ust_app_clean_list();
611 buffer_reg_destroy_registries();
613 if (is_root
&& !config
.no_kernel
) {
614 DBG2("Closing kernel fd");
615 if (kernel_tracer_fd
>= 0) {
616 ret
= close(kernel_tracer_fd
);
621 DBG("Unloading kernel modules");
622 modprobe_remove_lttng_all();
626 close_consumer_sockets();
629 load_session_destroy_data(load_info
);
634 * Cleanup lock file by deleting it and finaly closing it which will
635 * release the file system lock.
637 if (lockfile_fd
>= 0) {
638 ret
= remove(config
.lock_file_path
.value
);
640 PERROR("remove lock file");
642 ret
= close(lockfile_fd
);
644 PERROR("close lock file");
649 * We do NOT rmdir rundir because there are other processes
650 * using it, for instance lttng-relayd, which can start in
651 * parallel with this teardown.
656 * Cleanup the daemon's option data structures.
658 static void sessiond_cleanup_options(void)
660 DBG("Cleaning up options");
662 sessiond_config_fini(&config
);
664 run_as_destroy_worker();
668 * Send data on a unix socket using the liblttsessiondcomm API.
670 * Return lttcomm error code.
672 static int send_unix_sock(int sock
, void *buf
, size_t len
)
674 /* Check valid length */
679 return lttcomm_send_unix_sock(sock
, buf
, len
);
683 * Free memory of a command context structure.
685 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
687 DBG("Clean command context structure");
689 if ((*cmd_ctx
)->llm
) {
690 free((*cmd_ctx
)->llm
);
692 if ((*cmd_ctx
)->lsm
) {
693 free((*cmd_ctx
)->lsm
);
701 * Notify UST applications using the shm mmap futex.
703 static int notify_ust_apps(int active
)
707 DBG("Notifying applications of session daemon state: %d", active
);
709 /* See shm.c for this call implying mmap, shm and futex calls */
710 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
711 if (wait_shm_mmap
== NULL
) {
715 /* Wake waiting process */
716 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
718 /* Apps notified successfully */
726 * Setup the outgoing data buffer for the response (llm) by allocating the
727 * right amount of memory and copying the original information from the lsm
730 * Return 0 on success, negative value on error.
732 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
733 const void *payload_buf
, size_t payload_len
,
734 const void *cmd_header_buf
, size_t cmd_header_len
)
737 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
738 const size_t cmd_header_offset
= header_len
;
739 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
740 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
742 cmd_ctx
->llm
= zmalloc(total_msg_size
);
744 if (cmd_ctx
->llm
== NULL
) {
750 /* Copy common data */
751 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
752 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
753 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
754 cmd_ctx
->llm
->data_size
= payload_len
;
755 cmd_ctx
->lttng_msg_size
= total_msg_size
;
757 /* Copy command header */
758 if (cmd_header_len
) {
759 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
765 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
774 * Version of setup_lttng_msg() without command header.
776 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
777 void *payload_buf
, size_t payload_len
)
779 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
782 * Update the kernel poll set of all channel fd available over all tracing
783 * session. Add the wakeup pipe at the end of the set.
785 static int update_kernel_poll(struct lttng_poll_event
*events
)
788 struct ltt_session
*session
;
789 struct ltt_kernel_channel
*channel
;
791 DBG("Updating kernel poll set");
794 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
795 session_lock(session
);
796 if (session
->kernel_session
== NULL
) {
797 session_unlock(session
);
801 cds_list_for_each_entry(channel
,
802 &session
->kernel_session
->channel_list
.head
, list
) {
803 /* Add channel fd to the kernel poll set */
804 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
806 session_unlock(session
);
809 DBG("Channel fd %d added to kernel set", channel
->fd
);
811 session_unlock(session
);
813 session_unlock_list();
818 session_unlock_list();
823 * Find the channel fd from 'fd' over all tracing session. When found, check
824 * for new channel stream and send those stream fds to the kernel consumer.
826 * Useful for CPU hotplug feature.
828 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
831 struct ltt_session
*session
;
832 struct ltt_kernel_session
*ksess
;
833 struct ltt_kernel_channel
*channel
;
835 DBG("Updating kernel streams for channel fd %d", fd
);
838 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
839 session_lock(session
);
840 if (session
->kernel_session
== NULL
) {
841 session_unlock(session
);
844 ksess
= session
->kernel_session
;
846 cds_list_for_each_entry(channel
,
847 &ksess
->channel_list
.head
, list
) {
848 struct lttng_ht_iter iter
;
849 struct consumer_socket
*socket
;
851 if (channel
->fd
!= fd
) {
854 DBG("Channel found, updating kernel streams");
855 ret
= kernel_open_channel_stream(channel
);
859 /* Update the stream global counter */
860 ksess
->stream_count_global
+= ret
;
863 * Have we already sent fds to the consumer? If yes, it
864 * means that tracing is started so it is safe to send
865 * our updated stream fds.
867 if (ksess
->consumer_fds_sent
!= 1
868 || ksess
->consumer
== NULL
) {
874 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
875 &iter
.iter
, socket
, node
.node
) {
876 pthread_mutex_lock(socket
->lock
);
877 ret
= kernel_consumer_send_channel_stream(socket
,
879 session
->output_traces
? 1 : 0);
880 pthread_mutex_unlock(socket
->lock
);
888 session_unlock(session
);
890 session_unlock_list();
894 session_unlock(session
);
895 session_unlock_list();
900 * For each tracing session, update newly registered apps. The session list
901 * lock MUST be acquired before calling this.
903 static void update_ust_app(int app_sock
)
905 struct ltt_session
*sess
, *stmp
;
907 /* Consumer is in an ERROR state. Stop any application update. */
908 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
909 /* Stop the update process since the consumer is dead. */
913 /* For all tracing session(s) */
914 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
918 if (!sess
->ust_session
) {
923 assert(app_sock
>= 0);
924 app
= ust_app_find_by_sock(app_sock
);
927 * Application can be unregistered before so
928 * this is possible hence simply stopping the
931 DBG3("UST app update failed to find app sock %d",
935 ust_app_global_update(sess
->ust_session
, app
);
939 session_unlock(sess
);
944 * This thread manage event coming from the kernel.
946 * Features supported in this thread:
949 static void *thread_manage_kernel(void *data
)
951 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
952 uint32_t revents
, nb_fd
;
954 struct lttng_poll_event events
;
956 DBG("[thread] Thread manage kernel started");
958 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
961 * This first step of the while is to clean this structure which could free
962 * non NULL pointers so initialize it before the loop.
964 lttng_poll_init(&events
);
966 if (testpoint(sessiond_thread_manage_kernel
)) {
967 goto error_testpoint
;
970 health_code_update();
972 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
973 goto error_testpoint
;
977 health_code_update();
979 if (update_poll_flag
== 1) {
980 /* Clean events object. We are about to populate it again. */
981 lttng_poll_clean(&events
);
983 ret
= sessiond_set_thread_pollset(&events
, 2);
985 goto error_poll_create
;
988 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
993 /* This will add the available kernel channel if any. */
994 ret
= update_kernel_poll(&events
);
998 update_poll_flag
= 0;
1001 DBG("Thread kernel polling");
1003 /* Poll infinite value of time */
1005 health_poll_entry();
1006 ret
= lttng_poll_wait(&events
, -1);
1007 DBG("Thread kernel return from poll on %d fds",
1008 LTTNG_POLL_GETNB(&events
));
1012 * Restart interrupted system call.
1014 if (errno
== EINTR
) {
1018 } else if (ret
== 0) {
1019 /* Should not happen since timeout is infinite */
1020 ERR("Return value of poll is 0 with an infinite timeout.\n"
1021 "This should not have happened! Continuing...");
1027 for (i
= 0; i
< nb_fd
; i
++) {
1028 /* Fetch once the poll data */
1029 revents
= LTTNG_POLL_GETEV(&events
, i
);
1030 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1032 health_code_update();
1035 /* No activity for this FD (poll implementation). */
1039 /* Thread quit pipe has been closed. Killing thread. */
1040 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1046 /* Check for data on kernel pipe */
1047 if (revents
& LPOLLIN
) {
1048 if (pollfd
== kernel_poll_pipe
[0]) {
1049 (void) lttng_read(kernel_poll_pipe
[0],
1052 * Ret value is useless here, if this pipe gets any actions an
1053 * update is required anyway.
1055 update_poll_flag
= 1;
1059 * New CPU detected by the kernel. Adding kernel stream to
1060 * kernel session and updating the kernel consumer
1062 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1068 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1069 update_poll_flag
= 1;
1072 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1080 lttng_poll_clean(&events
);
1083 utils_close_pipe(kernel_poll_pipe
);
1084 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1087 ERR("Health error occurred in %s", __func__
);
1088 WARN("Kernel thread died unexpectedly. "
1089 "Kernel tracing can continue but CPU hotplug is disabled.");
1091 health_unregister(health_sessiond
);
1092 DBG("Kernel thread dying");
1097 * Signal pthread condition of the consumer data that the thread.
1099 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1101 pthread_mutex_lock(&data
->cond_mutex
);
1104 * The state is set before signaling. It can be any value, it's the waiter
1105 * job to correctly interpret this condition variable associated to the
1106 * consumer pthread_cond.
1108 * A value of 0 means that the corresponding thread of the consumer data
1109 * was not started. 1 indicates that the thread has started and is ready
1110 * for action. A negative value means that there was an error during the
1113 data
->consumer_thread_is_ready
= state
;
1114 (void) pthread_cond_signal(&data
->cond
);
1116 pthread_mutex_unlock(&data
->cond_mutex
);
1120 * This thread manage the consumer error sent back to the session daemon.
1122 static void *thread_manage_consumer(void *data
)
1124 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1125 uint32_t revents
, nb_fd
;
1126 enum lttcomm_return_code code
;
1127 struct lttng_poll_event events
;
1128 struct consumer_data
*consumer_data
= data
;
1129 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1131 DBG("[thread] Manage consumer started");
1133 rcu_register_thread();
1134 rcu_thread_online();
1136 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1138 health_code_update();
1141 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1142 * metadata_sock. Nothing more will be added to this poll set.
1144 ret
= sessiond_set_thread_pollset(&events
, 3);
1150 * The error socket here is already in a listening state which was done
1151 * just before spawning this thread to avoid a race between the consumer
1152 * daemon exec trying to connect and the listen() call.
1154 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1159 health_code_update();
1161 /* Infinite blocking call, waiting for transmission */
1163 health_poll_entry();
1165 if (testpoint(sessiond_thread_manage_consumer
)) {
1169 ret
= lttng_poll_wait(&events
, -1);
1173 * Restart interrupted system call.
1175 if (errno
== EINTR
) {
1183 for (i
= 0; i
< nb_fd
; i
++) {
1184 /* Fetch once the poll data */
1185 revents
= LTTNG_POLL_GETEV(&events
, i
);
1186 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1188 health_code_update();
1191 /* No activity for this FD (poll implementation). */
1195 /* Thread quit pipe has been closed. Killing thread. */
1196 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1202 /* Event on the registration socket */
1203 if (pollfd
== consumer_data
->err_sock
) {
1204 if (revents
& LPOLLIN
) {
1206 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1207 ERR("consumer err socket poll error");
1210 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1216 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1222 * Set the CLOEXEC flag. Return code is useless because either way, the
1225 (void) utils_set_fd_cloexec(sock
);
1227 health_code_update();
1229 DBG2("Receiving code from consumer err_sock");
1231 /* Getting status code from kconsumerd */
1232 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1233 sizeof(enum lttcomm_return_code
));
1238 health_code_update();
1239 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1240 ERR("consumer error when waiting for SOCK_READY : %s",
1241 lttcomm_get_readable_code(-code
));
1245 /* Connect both command and metadata sockets. */
1246 consumer_data
->cmd_sock
=
1247 lttcomm_connect_unix_sock(
1248 consumer_data
->cmd_unix_sock_path
);
1249 consumer_data
->metadata_fd
=
1250 lttcomm_connect_unix_sock(
1251 consumer_data
->cmd_unix_sock_path
);
1252 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1253 PERROR("consumer connect cmd socket");
1254 /* On error, signal condition and quit. */
1255 signal_consumer_condition(consumer_data
, -1);
1259 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1261 /* Create metadata socket lock. */
1262 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1263 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1264 PERROR("zmalloc pthread mutex");
1267 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1269 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1270 DBG("Consumer metadata socket ready (fd: %d)",
1271 consumer_data
->metadata_fd
);
1274 * Remove the consumerd error sock since we've established a connection.
1276 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1281 /* Add new accepted error socket. */
1282 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1287 /* Add metadata socket that is successfully connected. */
1288 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1289 LPOLLIN
| LPOLLRDHUP
);
1294 health_code_update();
1297 * Transfer the write-end of the channel monitoring and rotate pipe
1298 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1299 * SET_CHANNEL_ROTATE_PIPE commands.
1301 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1302 if (!cmd_socket_wrapper
) {
1305 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1307 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1308 consumer_data
->channel_monitor_pipe
);
1313 ret
= consumer_send_channel_rotate_pipe(cmd_socket_wrapper
,
1314 consumer_data
->channel_rotate_pipe
);
1319 /* Discard the socket wrapper as it is no longer needed. */
1320 consumer_destroy_socket(cmd_socket_wrapper
);
1321 cmd_socket_wrapper
= NULL
;
1323 /* The thread is completely initialized, signal that it is ready. */
1324 signal_consumer_condition(consumer_data
, 1);
1326 /* Infinite blocking call, waiting for transmission */
1329 health_code_update();
1331 /* Exit the thread because the thread quit pipe has been triggered. */
1333 /* Not a health error. */
1338 health_poll_entry();
1339 ret
= lttng_poll_wait(&events
, -1);
1343 * Restart interrupted system call.
1345 if (errno
== EINTR
) {
1353 for (i
= 0; i
< nb_fd
; i
++) {
1354 /* Fetch once the poll data */
1355 revents
= LTTNG_POLL_GETEV(&events
, i
);
1356 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1358 health_code_update();
1361 /* No activity for this FD (poll implementation). */
1366 * Thread quit pipe has been triggered, flag that we should stop
1367 * but continue the current loop to handle potential data from
1370 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1372 if (pollfd
== sock
) {
1373 /* Event on the consumerd socket */
1374 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1375 && !(revents
& LPOLLIN
)) {
1376 ERR("consumer err socket second poll error");
1379 health_code_update();
1380 /* Wait for any kconsumerd error */
1381 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1382 sizeof(enum lttcomm_return_code
));
1384 ERR("consumer closed the command socket");
1388 ERR("consumer return code : %s",
1389 lttcomm_get_readable_code(-code
));
1392 } else if (pollfd
== consumer_data
->metadata_fd
) {
1393 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1394 && !(revents
& LPOLLIN
)) {
1395 ERR("consumer err metadata socket second poll error");
1398 /* UST metadata requests */
1399 ret
= ust_consumer_metadata_request(
1400 &consumer_data
->metadata_sock
);
1402 ERR("Handling metadata request");
1406 /* No need for an else branch all FDs are tested prior. */
1408 health_code_update();
1414 * We lock here because we are about to close the sockets and some other
1415 * thread might be using them so get exclusive access which will abort all
1416 * other consumer command by other threads.
1418 pthread_mutex_lock(&consumer_data
->lock
);
1420 /* Immediately set the consumerd state to stopped */
1421 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1422 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1423 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1424 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1425 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1427 /* Code flow error... */
1431 if (consumer_data
->err_sock
>= 0) {
1432 ret
= close(consumer_data
->err_sock
);
1436 consumer_data
->err_sock
= -1;
1438 if (consumer_data
->cmd_sock
>= 0) {
1439 ret
= close(consumer_data
->cmd_sock
);
1443 consumer_data
->cmd_sock
= -1;
1445 if (consumer_data
->metadata_sock
.fd_ptr
&&
1446 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1447 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1459 unlink(consumer_data
->err_unix_sock_path
);
1460 unlink(consumer_data
->cmd_unix_sock_path
);
1461 pthread_mutex_unlock(&consumer_data
->lock
);
1463 /* Cleanup metadata socket mutex. */
1464 if (consumer_data
->metadata_sock
.lock
) {
1465 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1466 free(consumer_data
->metadata_sock
.lock
);
1468 lttng_poll_clean(&events
);
1470 if (cmd_socket_wrapper
) {
1471 consumer_destroy_socket(cmd_socket_wrapper
);
1476 ERR("Health error occurred in %s", __func__
);
1478 health_unregister(health_sessiond
);
1479 DBG("consumer thread cleanup completed");
1481 rcu_thread_offline();
1482 rcu_unregister_thread();
1488 * This thread receives application command sockets (FDs) on the
1489 * apps_cmd_pipe and waits (polls) on them until they are closed
1490 * or an error occurs.
1492 * At that point, it flushes the data (tracing and metadata) associated
1493 * with this application and tears down ust app sessions and other
1494 * associated data structures through ust_app_unregister().
1496 * Note that this thread never sends commands to the applications
1497 * through the command sockets; it merely listens for hang-ups
1498 * and errors on those sockets and cleans-up as they occur.
1500 static void *thread_manage_apps(void *data
)
1502 int i
, ret
, pollfd
, err
= -1;
1504 uint32_t revents
, nb_fd
;
1505 struct lttng_poll_event events
;
1507 DBG("[thread] Manage application started");
1509 rcu_register_thread();
1510 rcu_thread_online();
1512 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1514 if (testpoint(sessiond_thread_manage_apps
)) {
1515 goto error_testpoint
;
1518 health_code_update();
1520 ret
= sessiond_set_thread_pollset(&events
, 2);
1522 goto error_poll_create
;
1525 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1530 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1534 health_code_update();
1537 DBG("Apps thread polling");
1539 /* Inifinite blocking call, waiting for transmission */
1541 health_poll_entry();
1542 ret
= lttng_poll_wait(&events
, -1);
1543 DBG("Apps thread return from poll on %d fds",
1544 LTTNG_POLL_GETNB(&events
));
1548 * Restart interrupted system call.
1550 if (errno
== EINTR
) {
1558 for (i
= 0; i
< nb_fd
; i
++) {
1559 /* Fetch once the poll data */
1560 revents
= LTTNG_POLL_GETEV(&events
, i
);
1561 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1563 health_code_update();
1566 /* No activity for this FD (poll implementation). */
1570 /* Thread quit pipe has been closed. Killing thread. */
1571 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1577 /* Inspect the apps cmd pipe */
1578 if (pollfd
== apps_cmd_pipe
[0]) {
1579 if (revents
& LPOLLIN
) {
1583 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1584 if (size_ret
< sizeof(sock
)) {
1585 PERROR("read apps cmd pipe");
1589 health_code_update();
1592 * Since this is a command socket (write then read),
1593 * we only monitor the error events of the socket.
1595 ret
= lttng_poll_add(&events
, sock
,
1596 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1601 DBG("Apps with sock %d added to poll set", sock
);
1602 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1603 ERR("Apps command pipe error");
1606 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1611 * At this point, we know that a registered application made
1612 * the event at poll_wait.
1614 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1615 /* Removing from the poll set */
1616 ret
= lttng_poll_del(&events
, pollfd
);
1621 /* Socket closed on remote end. */
1622 ust_app_unregister(pollfd
);
1624 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1629 health_code_update();
1635 lttng_poll_clean(&events
);
1638 utils_close_pipe(apps_cmd_pipe
);
1639 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1642 * We don't clean the UST app hash table here since already registered
1643 * applications can still be controlled so let them be until the session
1644 * daemon dies or the applications stop.
1649 ERR("Health error occurred in %s", __func__
);
1651 health_unregister(health_sessiond
);
1652 DBG("Application communication apps thread cleanup complete");
1653 rcu_thread_offline();
1654 rcu_unregister_thread();
1659 * Send a socket to a thread This is called from the dispatch UST registration
1660 * thread once all sockets are set for the application.
1662 * The sock value can be invalid, we don't really care, the thread will handle
1663 * it and make the necessary cleanup if so.
1665 * On success, return 0 else a negative value being the errno message of the
1668 static int send_socket_to_thread(int fd
, int sock
)
1673 * It's possible that the FD is set as invalid with -1 concurrently just
1674 * before calling this function being a shutdown state of the thread.
1681 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1682 if (ret
< sizeof(sock
)) {
1683 PERROR("write apps pipe %d", fd
);
1690 /* All good. Don't send back the write positive ret value. */
1697 * Sanitize the wait queue of the dispatch registration thread meaning removing
1698 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1699 * notify socket is never received.
1701 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1703 int ret
, nb_fd
= 0, i
;
1704 unsigned int fd_added
= 0;
1705 struct lttng_poll_event events
;
1706 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1710 lttng_poll_init(&events
);
1712 /* Just skip everything for an empty queue. */
1713 if (!wait_queue
->count
) {
1717 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1722 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1723 &wait_queue
->head
, head
) {
1724 assert(wait_node
->app
);
1725 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1726 LPOLLHUP
| LPOLLERR
);
1739 * Poll but don't block so we can quickly identify the faulty events and
1740 * clean them afterwards from the wait queue.
1742 ret
= lttng_poll_wait(&events
, 0);
1748 for (i
= 0; i
< nb_fd
; i
++) {
1749 /* Get faulty FD. */
1750 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1751 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1754 /* No activity for this FD (poll implementation). */
1758 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1759 &wait_queue
->head
, head
) {
1760 if (pollfd
== wait_node
->app
->sock
&&
1761 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1762 cds_list_del(&wait_node
->head
);
1763 wait_queue
->count
--;
1764 ust_app_destroy(wait_node
->app
);
1767 * Silence warning of use-after-free in
1768 * cds_list_for_each_entry_safe which uses
1769 * __typeof__(*wait_node).
1774 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1781 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1785 lttng_poll_clean(&events
);
1789 lttng_poll_clean(&events
);
1791 ERR("Unable to sanitize wait queue");
1796 * Dispatch request from the registration threads to the application
1797 * communication thread.
1799 static void *thread_dispatch_ust_registration(void *data
)
1802 struct cds_wfcq_node
*node
;
1803 struct ust_command
*ust_cmd
= NULL
;
1804 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1805 struct ust_reg_wait_queue wait_queue
= {
1809 rcu_register_thread();
1811 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1813 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1814 goto error_testpoint
;
1817 health_code_update();
1819 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1821 DBG("[thread] Dispatch UST command started");
1824 health_code_update();
1826 /* Atomically prepare the queue futex */
1827 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1829 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1834 struct ust_app
*app
= NULL
;
1838 * Make sure we don't have node(s) that have hung up before receiving
1839 * the notify socket. This is to clean the list in order to avoid
1840 * memory leaks from notify socket that are never seen.
1842 sanitize_wait_queue(&wait_queue
);
1844 health_code_update();
1845 /* Dequeue command for registration */
1846 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1848 DBG("Woken up but nothing in the UST command queue");
1849 /* Continue thread execution */
1853 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1855 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1856 " gid:%d sock:%d name:%s (version %d.%d)",
1857 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1858 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1859 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1860 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1862 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1863 wait_node
= zmalloc(sizeof(*wait_node
));
1865 PERROR("zmalloc wait_node dispatch");
1866 ret
= close(ust_cmd
->sock
);
1868 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1870 lttng_fd_put(LTTNG_FD_APPS
, 1);
1874 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1876 /* Create application object if socket is CMD. */
1877 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1879 if (!wait_node
->app
) {
1880 ret
= close(ust_cmd
->sock
);
1882 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1884 lttng_fd_put(LTTNG_FD_APPS
, 1);
1890 * Add application to the wait queue so we can set the notify
1891 * socket before putting this object in the global ht.
1893 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1898 * We have to continue here since we don't have the notify
1899 * socket and the application MUST be added to the hash table
1900 * only at that moment.
1905 * Look for the application in the local wait queue and set the
1906 * notify socket if found.
1908 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1909 &wait_queue
.head
, head
) {
1910 health_code_update();
1911 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1912 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1913 cds_list_del(&wait_node
->head
);
1915 app
= wait_node
->app
;
1917 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1923 * With no application at this stage the received socket is
1924 * basically useless so close it before we free the cmd data
1925 * structure for good.
1928 ret
= close(ust_cmd
->sock
);
1930 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1932 lttng_fd_put(LTTNG_FD_APPS
, 1);
1939 * @session_lock_list
1941 * Lock the global session list so from the register up to the
1942 * registration done message, no thread can see the application
1943 * and change its state.
1945 session_lock_list();
1949 * Add application to the global hash table. This needs to be
1950 * done before the update to the UST registry can locate the
1955 /* Set app version. This call will print an error if needed. */
1956 (void) ust_app_version(app
);
1958 /* Send notify socket through the notify pipe. */
1959 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1963 session_unlock_list();
1965 * No notify thread, stop the UST tracing. However, this is
1966 * not an internal error of the this thread thus setting
1967 * the health error code to a normal exit.
1974 * Update newly registered application with the tracing
1975 * registry info already enabled information.
1977 update_ust_app(app
->sock
);
1980 * Don't care about return value. Let the manage apps threads
1981 * handle app unregistration upon socket close.
1983 (void) ust_app_register_done(app
);
1986 * Even if the application socket has been closed, send the app
1987 * to the thread and unregistration will take place at that
1990 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1993 session_unlock_list();
1995 * No apps. thread, stop the UST tracing. However, this is
1996 * not an internal error of the this thread thus setting
1997 * the health error code to a normal exit.
2004 session_unlock_list();
2006 } while (node
!= NULL
);
2008 health_poll_entry();
2009 /* Futex wait on queue. Blocking call on futex() */
2010 futex_nto1_wait(&ust_cmd_queue
.futex
);
2013 /* Normal exit, no error */
2017 /* Clean up wait queue. */
2018 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2019 &wait_queue
.head
, head
) {
2020 cds_list_del(&wait_node
->head
);
2025 /* Empty command queue. */
2027 /* Dequeue command for registration */
2028 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2032 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2033 ret
= close(ust_cmd
->sock
);
2035 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2037 lttng_fd_put(LTTNG_FD_APPS
, 1);
2042 DBG("Dispatch thread dying");
2045 ERR("Health error occurred in %s", __func__
);
2047 health_unregister(health_sessiond
);
2048 rcu_unregister_thread();
2053 * This thread manage application registration.
2055 static void *thread_registration_apps(void *data
)
2057 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2058 uint32_t revents
, nb_fd
;
2059 struct lttng_poll_event events
;
2061 * Get allocated in this thread, enqueued to a global queue, dequeued and
2062 * freed in the manage apps thread.
2064 struct ust_command
*ust_cmd
= NULL
;
2066 DBG("[thread] Manage application registration started");
2068 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2070 if (testpoint(sessiond_thread_registration_apps
)) {
2071 goto error_testpoint
;
2074 ret
= lttcomm_listen_unix_sock(apps_sock
);
2080 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2081 * more will be added to this poll set.
2083 ret
= sessiond_set_thread_pollset(&events
, 2);
2085 goto error_create_poll
;
2088 /* Add the application registration socket */
2089 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2091 goto error_poll_add
;
2094 /* Notify all applications to register */
2095 ret
= notify_ust_apps(1);
2097 ERR("Failed to notify applications or create the wait shared memory.\n"
2098 "Execution continues but there might be problem for already\n"
2099 "running applications that wishes to register.");
2103 DBG("Accepting application registration");
2105 /* Inifinite blocking call, waiting for transmission */
2107 health_poll_entry();
2108 ret
= lttng_poll_wait(&events
, -1);
2112 * Restart interrupted system call.
2114 if (errno
== EINTR
) {
2122 for (i
= 0; i
< nb_fd
; i
++) {
2123 health_code_update();
2125 /* Fetch once the poll data */
2126 revents
= LTTNG_POLL_GETEV(&events
, i
);
2127 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2130 /* No activity for this FD (poll implementation). */
2134 /* Thread quit pipe has been closed. Killing thread. */
2135 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2141 /* Event on the registration socket */
2142 if (pollfd
== apps_sock
) {
2143 if (revents
& LPOLLIN
) {
2144 sock
= lttcomm_accept_unix_sock(apps_sock
);
2150 * Set socket timeout for both receiving and ending.
2151 * app_socket_timeout is in seconds, whereas
2152 * lttcomm_setsockopt_rcv_timeout and
2153 * lttcomm_setsockopt_snd_timeout expect msec as
2156 if (config
.app_socket_timeout
>= 0) {
2157 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2158 config
.app_socket_timeout
* 1000);
2159 (void) lttcomm_setsockopt_snd_timeout(sock
,
2160 config
.app_socket_timeout
* 1000);
2164 * Set the CLOEXEC flag. Return code is useless because
2165 * either way, the show must go on.
2167 (void) utils_set_fd_cloexec(sock
);
2169 /* Create UST registration command for enqueuing */
2170 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2171 if (ust_cmd
== NULL
) {
2172 PERROR("ust command zmalloc");
2181 * Using message-based transmissions to ensure we don't
2182 * have to deal with partially received messages.
2184 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2186 ERR("Exhausted file descriptors allowed for applications.");
2196 health_code_update();
2197 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2200 /* Close socket of the application. */
2205 lttng_fd_put(LTTNG_FD_APPS
, 1);
2209 health_code_update();
2211 ust_cmd
->sock
= sock
;
2214 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2215 " gid:%d sock:%d name:%s (version %d.%d)",
2216 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2217 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2218 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2219 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2222 * Lock free enqueue the registration request. The red pill
2223 * has been taken! This apps will be part of the *system*.
2225 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2228 * Wake the registration queue futex. Implicit memory
2229 * barrier with the exchange in cds_wfcq_enqueue.
2231 futex_nto1_wake(&ust_cmd_queue
.futex
);
2232 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2233 ERR("Register apps socket poll error");
2236 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2245 /* Notify that the registration thread is gone */
2248 if (apps_sock
>= 0) {
2249 ret
= close(apps_sock
);
2259 lttng_fd_put(LTTNG_FD_APPS
, 1);
2261 unlink(config
.apps_unix_sock_path
.value
);
2264 lttng_poll_clean(&events
);
2268 DBG("UST Registration thread cleanup complete");
2271 ERR("Health error occurred in %s", __func__
);
2273 health_unregister(health_sessiond
);
2279 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2280 * exec or it will fails.
2282 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2285 struct timespec timeout
;
2288 * Make sure we set the readiness flag to 0 because we are NOT ready.
2289 * This access to consumer_thread_is_ready does not need to be
2290 * protected by consumer_data.cond_mutex (yet) since the consumer
2291 * management thread has not been started at this point.
2293 consumer_data
->consumer_thread_is_ready
= 0;
2295 /* Setup pthread condition */
2296 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2299 PERROR("pthread_condattr_init consumer data");
2304 * Set the monotonic clock in order to make sure we DO NOT jump in time
2305 * between the clock_gettime() call and the timedwait call. See bug #324
2306 * for a more details and how we noticed it.
2308 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2311 PERROR("pthread_condattr_setclock consumer data");
2315 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2318 PERROR("pthread_cond_init consumer data");
2322 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2323 thread_manage_consumer
, consumer_data
);
2326 PERROR("pthread_create consumer");
2331 /* We are about to wait on a pthread condition */
2332 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2334 /* Get time for sem_timedwait absolute timeout */
2335 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2337 * Set the timeout for the condition timed wait even if the clock gettime
2338 * call fails since we might loop on that call and we want to avoid to
2339 * increment the timeout too many times.
2341 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2344 * The following loop COULD be skipped in some conditions so this is why we
2345 * set ret to 0 in order to make sure at least one round of the loop is
2351 * Loop until the condition is reached or when a timeout is reached. Note
2352 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2353 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2354 * possible. This loop does not take any chances and works with both of
2357 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2358 if (clock_ret
< 0) {
2359 PERROR("clock_gettime spawn consumer");
2360 /* Infinite wait for the consumerd thread to be ready */
2361 ret
= pthread_cond_wait(&consumer_data
->cond
,
2362 &consumer_data
->cond_mutex
);
2364 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2365 &consumer_data
->cond_mutex
, &timeout
);
2369 /* Release the pthread condition */
2370 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2374 if (ret
== ETIMEDOUT
) {
2378 * Call has timed out so we kill the kconsumerd_thread and return
2381 ERR("Condition timed out. The consumer thread was never ready."
2383 pth_ret
= pthread_cancel(consumer_data
->thread
);
2385 PERROR("pthread_cancel consumer thread");
2388 PERROR("pthread_cond_wait failed consumer thread");
2390 /* Caller is expecting a negative value on failure. */
2395 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2396 if (consumer_data
->pid
== 0) {
2397 ERR("Consumerd did not start");
2398 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2401 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2410 * Join consumer thread
2412 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2416 /* Consumer pid must be a real one. */
2417 if (consumer_data
->pid
> 0) {
2419 ret
= kill(consumer_data
->pid
, SIGTERM
);
2421 PERROR("Error killing consumer daemon");
2424 return pthread_join(consumer_data
->thread
, &status
);
2431 * Fork and exec a consumer daemon (consumerd).
2433 * Return pid if successful else -1.
2435 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2439 const char *consumer_to_use
;
2440 const char *verbosity
;
2443 DBG("Spawning consumerd");
2450 if (config
.verbose_consumer
) {
2451 verbosity
= "--verbose";
2452 } else if (lttng_opt_quiet
) {
2453 verbosity
= "--quiet";
2458 switch (consumer_data
->type
) {
2459 case LTTNG_CONSUMER_KERNEL
:
2461 * Find out which consumerd to execute. We will first try the
2462 * 64-bit path, then the sessiond's installation directory, and
2463 * fallback on the 32-bit one,
2465 DBG3("Looking for a kernel consumer at these locations:");
2466 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2467 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2468 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2469 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2470 DBG3("Found location #1");
2471 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2472 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2473 DBG3("Found location #2");
2474 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2475 } else if (stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2476 DBG3("Found location #3");
2477 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2479 DBG("Could not find any valid consumerd executable");
2483 DBG("Using kernel consumer at: %s", consumer_to_use
);
2484 (void) execl(consumer_to_use
,
2485 "lttng-consumerd", verbosity
, "-k",
2486 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2487 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2488 "--group", config
.tracing_group_name
.value
,
2491 case LTTNG_CONSUMER64_UST
:
2493 if (config
.consumerd64_lib_dir
.value
) {
2498 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2502 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2503 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2508 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2509 if (tmp
[0] != '\0') {
2510 strcat(tmpnew
, ":");
2511 strcat(tmpnew
, tmp
);
2513 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2520 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2521 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2522 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2523 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2524 "--group", config
.tracing_group_name
.value
,
2528 case LTTNG_CONSUMER32_UST
:
2530 if (config
.consumerd32_lib_dir
.value
) {
2535 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2539 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2540 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2545 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2546 if (tmp
[0] != '\0') {
2547 strcat(tmpnew
, ":");
2548 strcat(tmpnew
, tmp
);
2550 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2557 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2558 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2559 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2560 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2561 "--group", config
.tracing_group_name
.value
,
2566 ERR("unknown consumer type");
2570 PERROR("Consumer execl()");
2572 /* Reaching this point, we got a failure on our execl(). */
2574 } else if (pid
> 0) {
2577 PERROR("start consumer fork");
2585 * Spawn the consumerd daemon and session daemon thread.
2587 static int start_consumerd(struct consumer_data
*consumer_data
)
2592 * Set the listen() state on the socket since there is a possible race
2593 * between the exec() of the consumer daemon and this call if place in the
2594 * consumer thread. See bug #366 for more details.
2596 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2601 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2602 if (consumer_data
->pid
!= 0) {
2603 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2607 ret
= spawn_consumerd(consumer_data
);
2609 ERR("Spawning consumerd failed");
2610 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2614 /* Setting up the consumer_data pid */
2615 consumer_data
->pid
= ret
;
2616 DBG2("Consumer pid %d", consumer_data
->pid
);
2617 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2619 DBG2("Spawning consumer control thread");
2620 ret
= spawn_consumer_thread(consumer_data
);
2622 ERR("Fatal error spawning consumer control thread");
2630 /* Cleanup already created sockets on error. */
2631 if (consumer_data
->err_sock
>= 0) {
2634 err
= close(consumer_data
->err_sock
);
2636 PERROR("close consumer data error socket");
2643 * Setup necessary data for kernel tracer action.
2645 static int init_kernel_tracer(void)
2649 /* Modprobe lttng kernel modules */
2650 ret
= modprobe_lttng_control();
2655 /* Open debugfs lttng */
2656 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2657 if (kernel_tracer_fd
< 0) {
2658 DBG("Failed to open %s", module_proc_lttng
);
2662 /* Validate kernel version */
2663 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2664 &kernel_tracer_abi_version
);
2669 ret
= modprobe_lttng_data();
2674 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2681 WARN("Kernel tracer does not support buffer monitoring. "
2682 "The monitoring timer of channels in the kernel domain "
2683 "will be set to 0 (disabled).");
2686 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2690 modprobe_remove_lttng_control();
2691 ret
= close(kernel_tracer_fd
);
2695 kernel_tracer_fd
= -1;
2696 return LTTNG_ERR_KERN_VERSION
;
2699 ret
= close(kernel_tracer_fd
);
2705 modprobe_remove_lttng_control();
2708 WARN("No kernel tracer available");
2709 kernel_tracer_fd
= -1;
2711 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2713 return LTTNG_ERR_KERN_NA
;
2719 * Copy consumer output from the tracing session to the domain session. The
2720 * function also applies the right modification on a per domain basis for the
2721 * trace files destination directory.
2723 * Should *NOT* be called with RCU read-side lock held.
2725 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2728 const char *dir_name
;
2729 struct consumer_output
*consumer
;
2732 assert(session
->consumer
);
2735 case LTTNG_DOMAIN_KERNEL
:
2736 DBG3("Copying tracing session consumer output in kernel session");
2738 * XXX: We should audit the session creation and what this function
2739 * does "extra" in order to avoid a destroy since this function is used
2740 * in the domain session creation (kernel and ust) only. Same for UST
2743 if (session
->kernel_session
->consumer
) {
2744 consumer_output_put(session
->kernel_session
->consumer
);
2746 session
->kernel_session
->consumer
=
2747 consumer_copy_output(session
->consumer
);
2748 /* Ease our life a bit for the next part */
2749 consumer
= session
->kernel_session
->consumer
;
2750 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2752 case LTTNG_DOMAIN_JUL
:
2753 case LTTNG_DOMAIN_LOG4J
:
2754 case LTTNG_DOMAIN_PYTHON
:
2755 case LTTNG_DOMAIN_UST
:
2756 DBG3("Copying tracing session consumer output in UST session");
2757 if (session
->ust_session
->consumer
) {
2758 consumer_output_put(session
->ust_session
->consumer
);
2760 session
->ust_session
->consumer
=
2761 consumer_copy_output(session
->consumer
);
2762 /* Ease our life a bit for the next part */
2763 consumer
= session
->ust_session
->consumer
;
2764 dir_name
= DEFAULT_UST_TRACE_DIR
;
2767 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2771 /* Append correct directory to subdir */
2772 strncat(consumer
->subdir
, dir_name
,
2773 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2774 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2783 * Create an UST session and add it to the session ust list.
2785 * Should *NOT* be called with RCU read-side lock held.
2787 static int create_ust_session(struct ltt_session
*session
,
2788 struct lttng_domain
*domain
)
2791 struct ltt_ust_session
*lus
= NULL
;
2795 assert(session
->consumer
);
2797 switch (domain
->type
) {
2798 case LTTNG_DOMAIN_JUL
:
2799 case LTTNG_DOMAIN_LOG4J
:
2800 case LTTNG_DOMAIN_PYTHON
:
2801 case LTTNG_DOMAIN_UST
:
2804 ERR("Unknown UST domain on create session %d", domain
->type
);
2805 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2809 DBG("Creating UST session");
2811 lus
= trace_ust_create_session(session
->id
);
2813 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2817 lus
->uid
= session
->uid
;
2818 lus
->gid
= session
->gid
;
2819 lus
->output_traces
= session
->output_traces
;
2820 lus
->snapshot_mode
= session
->snapshot_mode
;
2821 lus
->live_timer_interval
= session
->live_timer
;
2822 session
->ust_session
= lus
;
2823 if (session
->shm_path
[0]) {
2824 strncpy(lus
->root_shm_path
, session
->shm_path
,
2825 sizeof(lus
->root_shm_path
));
2826 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2827 strncpy(lus
->shm_path
, session
->shm_path
,
2828 sizeof(lus
->shm_path
));
2829 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2830 strncat(lus
->shm_path
, "/ust",
2831 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2833 /* Copy session output to the newly created UST session */
2834 ret
= copy_session_consumer(domain
->type
, session
);
2835 if (ret
!= LTTNG_OK
) {
2843 session
->ust_session
= NULL
;
2848 * Create a kernel tracer session then create the default channel.
2850 static int create_kernel_session(struct ltt_session
*session
)
2854 DBG("Creating kernel session");
2856 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2858 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2862 /* Code flow safety */
2863 assert(session
->kernel_session
);
2865 /* Copy session output to the newly created Kernel session */
2866 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2867 if (ret
!= LTTNG_OK
) {
2871 session
->kernel_session
->uid
= session
->uid
;
2872 session
->kernel_session
->gid
= session
->gid
;
2873 session
->kernel_session
->output_traces
= session
->output_traces
;
2874 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2879 trace_kernel_destroy_session(session
->kernel_session
);
2880 session
->kernel_session
= NULL
;
2885 * Count number of session permitted by uid/gid.
2887 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2890 struct ltt_session
*session
;
2892 DBG("Counting number of available session for UID %d GID %d",
2894 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2896 * Only list the sessions the user can control.
2898 if (!session_access_ok(session
, uid
, gid
)) {
2907 * Check if the current kernel tracer supports the session rotation feature.
2908 * Return 1 if it does, 0 otherwise.
2910 static int check_rotate_compatible(void)
2914 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2915 DBG("Kernel tracer version is not compatible with the rotation feature");
2923 * Process the command requested by the lttng client within the command
2924 * context structure. This function make sure that the return structure (llm)
2925 * is set and ready for transmission before returning.
2927 * Return any error encountered or 0 for success.
2929 * "sock" is only used for special-case var. len data.
2931 * Should *NOT* be called with RCU read-side lock held.
2933 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2937 int need_tracing_session
= 1;
2940 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2942 assert(!rcu_read_ongoing());
2946 switch (cmd_ctx
->lsm
->cmd_type
) {
2947 case LTTNG_CREATE_SESSION
:
2948 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2949 case LTTNG_CREATE_SESSION_LIVE
:
2950 case LTTNG_DESTROY_SESSION
:
2951 case LTTNG_LIST_SESSIONS
:
2952 case LTTNG_LIST_DOMAINS
:
2953 case LTTNG_START_TRACE
:
2954 case LTTNG_STOP_TRACE
:
2955 case LTTNG_DATA_PENDING
:
2956 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2957 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2958 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2959 case LTTNG_SNAPSHOT_RECORD
:
2960 case LTTNG_SAVE_SESSION
:
2961 case LTTNG_SET_SESSION_SHM_PATH
:
2962 case LTTNG_REGENERATE_METADATA
:
2963 case LTTNG_REGENERATE_STATEDUMP
:
2964 case LTTNG_REGISTER_TRIGGER
:
2965 case LTTNG_UNREGISTER_TRIGGER
:
2966 case LTTNG_ROTATE_SESSION
:
2967 case LTTNG_ROTATION_GET_INFO
:
2968 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
2969 case LTTNG_ROTATION_SET_SCHEDULE
:
2970 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
2971 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
2978 if (config
.no_kernel
&& need_domain
2979 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2981 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2983 ret
= LTTNG_ERR_KERN_NA
;
2988 /* Deny register consumer if we already have a spawned consumer. */
2989 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2990 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2991 if (kconsumer_data
.pid
> 0) {
2992 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2993 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2996 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3000 * Check for command that don't needs to allocate a returned payload. We do
3001 * this here so we don't have to make the call for no payload at each
3004 switch(cmd_ctx
->lsm
->cmd_type
) {
3005 case LTTNG_LIST_SESSIONS
:
3006 case LTTNG_LIST_TRACEPOINTS
:
3007 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3008 case LTTNG_LIST_DOMAINS
:
3009 case LTTNG_LIST_CHANNELS
:
3010 case LTTNG_LIST_EVENTS
:
3011 case LTTNG_LIST_SYSCALLS
:
3012 case LTTNG_LIST_TRACKER_PIDS
:
3013 case LTTNG_DATA_PENDING
:
3014 case LTTNG_ROTATE_SESSION
:
3015 case LTTNG_ROTATION_GET_INFO
:
3016 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
3017 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
3020 /* Setup lttng message with no payload */
3021 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3023 /* This label does not try to unlock the session */
3024 goto init_setup_error
;
3028 /* Commands that DO NOT need a session. */
3029 switch (cmd_ctx
->lsm
->cmd_type
) {
3030 case LTTNG_CREATE_SESSION
:
3031 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3032 case LTTNG_CREATE_SESSION_LIVE
:
3033 case LTTNG_LIST_SESSIONS
:
3034 case LTTNG_LIST_TRACEPOINTS
:
3035 case LTTNG_LIST_SYSCALLS
:
3036 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3037 case LTTNG_SAVE_SESSION
:
3038 case LTTNG_REGISTER_TRIGGER
:
3039 case LTTNG_UNREGISTER_TRIGGER
:
3040 need_tracing_session
= 0;
3043 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3045 * We keep the session list lock across _all_ commands
3046 * for now, because the per-session lock does not
3047 * handle teardown properly.
3049 session_lock_list();
3050 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3051 if (cmd_ctx
->session
== NULL
) {
3052 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3055 /* Acquire lock for the session */
3056 session_lock(cmd_ctx
->session
);
3062 * Commands that need a valid session but should NOT create one if none
3063 * exists. Instead of creating one and destroying it when the command is
3064 * handled, process that right before so we save some round trip in useless
3067 switch (cmd_ctx
->lsm
->cmd_type
) {
3068 case LTTNG_DISABLE_CHANNEL
:
3069 case LTTNG_DISABLE_EVENT
:
3070 switch (cmd_ctx
->lsm
->domain
.type
) {
3071 case LTTNG_DOMAIN_KERNEL
:
3072 if (!cmd_ctx
->session
->kernel_session
) {
3073 ret
= LTTNG_ERR_NO_CHANNEL
;
3077 case LTTNG_DOMAIN_JUL
:
3078 case LTTNG_DOMAIN_LOG4J
:
3079 case LTTNG_DOMAIN_PYTHON
:
3080 case LTTNG_DOMAIN_UST
:
3081 if (!cmd_ctx
->session
->ust_session
) {
3082 ret
= LTTNG_ERR_NO_CHANNEL
;
3087 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3099 * Check domain type for specific "pre-action".
3101 switch (cmd_ctx
->lsm
->domain
.type
) {
3102 case LTTNG_DOMAIN_KERNEL
:
3104 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3108 /* Kernel tracer check */
3109 if (kernel_tracer_fd
== -1) {
3110 /* Basically, load kernel tracer modules */
3111 ret
= init_kernel_tracer();
3117 /* Consumer is in an ERROR state. Report back to client */
3118 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3119 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3123 /* Need a session for kernel command */
3124 if (need_tracing_session
) {
3125 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3126 ret
= create_kernel_session(cmd_ctx
->session
);
3128 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3133 /* Start the kernel consumer daemon */
3134 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3135 if (kconsumer_data
.pid
== 0 &&
3136 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3137 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3138 ret
= start_consumerd(&kconsumer_data
);
3140 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3143 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3145 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3149 * The consumer was just spawned so we need to add the socket to
3150 * the consumer output of the session if exist.
3152 ret
= consumer_create_socket(&kconsumer_data
,
3153 cmd_ctx
->session
->kernel_session
->consumer
);
3160 case LTTNG_DOMAIN_JUL
:
3161 case LTTNG_DOMAIN_LOG4J
:
3162 case LTTNG_DOMAIN_PYTHON
:
3163 case LTTNG_DOMAIN_UST
:
3165 if (!ust_app_supported()) {
3166 ret
= LTTNG_ERR_NO_UST
;
3169 /* Consumer is in an ERROR state. Report back to client */
3170 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3171 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3175 if (need_tracing_session
) {
3176 /* Create UST session if none exist. */
3177 if (cmd_ctx
->session
->ust_session
== NULL
) {
3178 ret
= create_ust_session(cmd_ctx
->session
,
3179 &cmd_ctx
->lsm
->domain
);
3180 if (ret
!= LTTNG_OK
) {
3185 /* Start the UST consumer daemons */
3187 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3188 if (config
.consumerd64_bin_path
.value
&&
3189 ustconsumer64_data
.pid
== 0 &&
3190 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3191 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3192 ret
= start_consumerd(&ustconsumer64_data
);
3194 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3195 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3199 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3200 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3202 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3206 * Setup socket for consumer 64 bit. No need for atomic access
3207 * since it was set above and can ONLY be set in this thread.
3209 ret
= consumer_create_socket(&ustconsumer64_data
,
3210 cmd_ctx
->session
->ust_session
->consumer
);
3216 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3217 if (config
.consumerd32_bin_path
.value
&&
3218 ustconsumer32_data
.pid
== 0 &&
3219 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3220 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3221 ret
= start_consumerd(&ustconsumer32_data
);
3223 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3224 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3228 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3229 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3231 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3235 * Setup socket for consumer 64 bit. No need for atomic access
3236 * since it was set above and can ONLY be set in this thread.
3238 ret
= consumer_create_socket(&ustconsumer32_data
,
3239 cmd_ctx
->session
->ust_session
->consumer
);
3251 /* Validate consumer daemon state when start/stop trace command */
3252 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3253 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3254 switch (cmd_ctx
->lsm
->domain
.type
) {
3255 case LTTNG_DOMAIN_NONE
:
3257 case LTTNG_DOMAIN_JUL
:
3258 case LTTNG_DOMAIN_LOG4J
:
3259 case LTTNG_DOMAIN_PYTHON
:
3260 case LTTNG_DOMAIN_UST
:
3261 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3262 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3266 case LTTNG_DOMAIN_KERNEL
:
3267 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3268 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3273 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3279 * Check that the UID or GID match that of the tracing session.
3280 * The root user can interact with all sessions.
3282 if (need_tracing_session
) {
3283 if (!session_access_ok(cmd_ctx
->session
,
3284 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3285 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3286 ret
= LTTNG_ERR_EPERM
;
3292 * Send relayd information to consumer as soon as we have a domain and a
3295 if (cmd_ctx
->session
&& need_domain
) {
3297 * Setup relayd if not done yet. If the relayd information was already
3298 * sent to the consumer, this call will gracefully return.
3300 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3301 if (ret
!= LTTNG_OK
) {
3306 /* Process by command type */
3307 switch (cmd_ctx
->lsm
->cmd_type
) {
3308 case LTTNG_ADD_CONTEXT
:
3311 * An LTTNG_ADD_CONTEXT command might have a supplementary
3312 * payload if the context being added is an application context.
3314 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3315 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3316 char *provider_name
= NULL
, *context_name
= NULL
;
3317 size_t provider_name_len
=
3318 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3319 size_t context_name_len
=
3320 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3322 if (provider_name_len
== 0 || context_name_len
== 0) {
3324 * Application provider and context names MUST
3327 ret
= -LTTNG_ERR_INVALID
;
3331 provider_name
= zmalloc(provider_name_len
+ 1);
3332 if (!provider_name
) {
3333 ret
= -LTTNG_ERR_NOMEM
;
3336 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3339 context_name
= zmalloc(context_name_len
+ 1);
3340 if (!context_name
) {
3341 ret
= -LTTNG_ERR_NOMEM
;
3342 goto error_add_context
;
3344 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3347 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3350 goto error_add_context
;
3353 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3356 goto error_add_context
;
3361 * cmd_add_context assumes ownership of the provider and context
3364 ret
= cmd_add_context(cmd_ctx
->session
,
3365 cmd_ctx
->lsm
->domain
.type
,
3366 cmd_ctx
->lsm
->u
.context
.channel_name
,
3367 &cmd_ctx
->lsm
->u
.context
.ctx
,
3368 kernel_poll_pipe
[1]);
3370 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3371 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3373 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3374 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3380 case LTTNG_DISABLE_CHANNEL
:
3382 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3383 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3386 case LTTNG_DISABLE_EVENT
:
3390 * FIXME: handle filter; for now we just receive the filter's
3391 * bytecode along with the filter expression which are sent by
3392 * liblttng-ctl and discard them.
3394 * This fixes an issue where the client may block while sending
3395 * the filter payload and encounter an error because the session
3396 * daemon closes the socket without ever handling this data.
3398 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3399 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3402 char data
[LTTNG_FILTER_MAX_LEN
];
3404 DBG("Discarding disable event command payload of size %zu", count
);
3406 ret
= lttcomm_recv_unix_sock(sock
, data
,
3407 count
> sizeof(data
) ? sizeof(data
) : count
);
3412 count
-= (size_t) ret
;
3415 /* FIXME: passing packed structure to non-packed pointer */
3416 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3417 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3418 &cmd_ctx
->lsm
->u
.disable
.event
);
3421 case LTTNG_ENABLE_CHANNEL
:
3423 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3424 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3425 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3426 &cmd_ctx
->lsm
->u
.channel
.chan
,
3427 kernel_poll_pipe
[1]);
3430 case LTTNG_TRACK_PID
:
3432 ret
= cmd_track_pid(cmd_ctx
->session
,
3433 cmd_ctx
->lsm
->domain
.type
,
3434 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3437 case LTTNG_UNTRACK_PID
:
3439 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3440 cmd_ctx
->lsm
->domain
.type
,
3441 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3444 case LTTNG_ENABLE_EVENT
:
3446 struct lttng_event_exclusion
*exclusion
= NULL
;
3447 struct lttng_filter_bytecode
*bytecode
= NULL
;
3448 char *filter_expression
= NULL
;
3450 /* Handle exclusion events and receive it from the client. */
3451 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3452 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3454 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3455 (count
* LTTNG_SYMBOL_NAME_LEN
));
3457 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3461 DBG("Receiving var len exclusion event list from client ...");
3462 exclusion
->count
= count
;
3463 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3464 count
* LTTNG_SYMBOL_NAME_LEN
);
3466 DBG("Nothing recv() from client var len data... continuing");
3469 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3474 /* Get filter expression from client. */
3475 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3476 size_t expression_len
=
3477 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3479 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3480 ret
= LTTNG_ERR_FILTER_INVAL
;
3485 filter_expression
= zmalloc(expression_len
);
3486 if (!filter_expression
) {
3488 ret
= LTTNG_ERR_FILTER_NOMEM
;
3492 /* Receive var. len. data */
3493 DBG("Receiving var len filter's expression from client ...");
3494 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3497 DBG("Nothing recv() from client car len data... continuing");
3499 free(filter_expression
);
3501 ret
= LTTNG_ERR_FILTER_INVAL
;
3506 /* Handle filter and get bytecode from client. */
3507 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3508 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3510 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3511 ret
= LTTNG_ERR_FILTER_INVAL
;
3512 free(filter_expression
);
3517 bytecode
= zmalloc(bytecode_len
);
3519 free(filter_expression
);
3521 ret
= LTTNG_ERR_FILTER_NOMEM
;
3525 /* Receive var. len. data */
3526 DBG("Receiving var len filter's bytecode from client ...");
3527 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3529 DBG("Nothing recv() from client car len data... continuing");
3531 free(filter_expression
);
3534 ret
= LTTNG_ERR_FILTER_INVAL
;
3538 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3539 free(filter_expression
);
3542 ret
= LTTNG_ERR_FILTER_INVAL
;
3547 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3548 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3549 &cmd_ctx
->lsm
->u
.enable
.event
,
3550 filter_expression
, bytecode
, exclusion
,
3551 kernel_poll_pipe
[1]);
3554 case LTTNG_LIST_TRACEPOINTS
:
3556 struct lttng_event
*events
;
3559 session_lock_list();
3560 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3561 session_unlock_list();
3562 if (nb_events
< 0) {
3563 /* Return value is a negative lttng_error_code. */
3569 * Setup lttng message with payload size set to the event list size in
3570 * bytes and then copy list into the llm payload.
3572 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3573 sizeof(struct lttng_event
) * nb_events
);
3583 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3585 struct lttng_event_field
*fields
;
3588 session_lock_list();
3589 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3591 session_unlock_list();
3592 if (nb_fields
< 0) {
3593 /* Return value is a negative lttng_error_code. */
3599 * Setup lttng message with payload size set to the event list size in
3600 * bytes and then copy list into the llm payload.
3602 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3603 sizeof(struct lttng_event_field
) * nb_fields
);
3613 case LTTNG_LIST_SYSCALLS
:
3615 struct lttng_event
*events
;
3618 nb_events
= cmd_list_syscalls(&events
);
3619 if (nb_events
< 0) {
3620 /* Return value is a negative lttng_error_code. */
3626 * Setup lttng message with payload size set to the event list size in
3627 * bytes and then copy list into the llm payload.
3629 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3630 sizeof(struct lttng_event
) * nb_events
);
3640 case LTTNG_LIST_TRACKER_PIDS
:
3642 int32_t *pids
= NULL
;
3645 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3646 cmd_ctx
->lsm
->domain
.type
, &pids
);
3648 /* Return value is a negative lttng_error_code. */
3654 * Setup lttng message with payload size set to the event list size in
3655 * bytes and then copy list into the llm payload.
3657 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3658 sizeof(int32_t) * nr_pids
);
3668 case LTTNG_SET_CONSUMER_URI
:
3671 struct lttng_uri
*uris
;
3673 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3674 len
= nb_uri
* sizeof(struct lttng_uri
);
3677 ret
= LTTNG_ERR_INVALID
;
3681 uris
= zmalloc(len
);
3683 ret
= LTTNG_ERR_FATAL
;
3687 /* Receive variable len data */
3688 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3689 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3691 DBG("No URIs received from client... continuing");
3693 ret
= LTTNG_ERR_SESSION_FAIL
;
3698 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3700 if (ret
!= LTTNG_OK
) {
3707 case LTTNG_START_TRACE
:
3710 * On the first start, if we have a kernel session and we have
3711 * enabled time or size-based rotations, we have to make sure
3712 * the kernel tracer supports it.
3714 if (!cmd_ctx
->session
->has_been_started
&& \
3715 cmd_ctx
->session
->kernel_session
&& \
3716 (cmd_ctx
->session
->rotate_timer_period
|| \
3717 cmd_ctx
->session
->rotate_size
) && \
3718 !check_rotate_compatible()) {
3719 DBG("Kernel tracer version is not compatible with the rotation feature");
3720 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3723 ret
= cmd_start_trace(cmd_ctx
->session
);
3726 case LTTNG_STOP_TRACE
:
3728 ret
= cmd_stop_trace(cmd_ctx
->session
);
3731 case LTTNG_CREATE_SESSION
:
3734 struct lttng_uri
*uris
= NULL
;
3736 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3737 len
= nb_uri
* sizeof(struct lttng_uri
);
3740 uris
= zmalloc(len
);
3742 ret
= LTTNG_ERR_FATAL
;
3746 /* Receive variable len data */
3747 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3748 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3750 DBG("No URIs received from client... continuing");
3752 ret
= LTTNG_ERR_SESSION_FAIL
;
3757 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3758 DBG("Creating session with ONE network URI is a bad call");
3759 ret
= LTTNG_ERR_SESSION_FAIL
;
3765 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3766 &cmd_ctx
->creds
, 0);
3772 case LTTNG_DESTROY_SESSION
:
3774 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3776 /* Set session to NULL so we do not unlock it after free. */
3777 cmd_ctx
->session
= NULL
;
3780 case LTTNG_LIST_DOMAINS
:
3783 struct lttng_domain
*domains
= NULL
;
3785 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3787 /* Return value is a negative lttng_error_code. */
3792 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3793 nb_dom
* sizeof(struct lttng_domain
));
3803 case LTTNG_LIST_CHANNELS
:
3805 ssize_t payload_size
;
3806 struct lttng_channel
*channels
= NULL
;
3808 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3809 cmd_ctx
->session
, &channels
);
3810 if (payload_size
< 0) {
3811 /* Return value is a negative lttng_error_code. */
3812 ret
= -payload_size
;
3816 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3827 case LTTNG_LIST_EVENTS
:
3830 struct lttng_event
*events
= NULL
;
3831 struct lttcomm_event_command_header cmd_header
;
3834 memset(&cmd_header
, 0, sizeof(cmd_header
));
3835 /* Extended infos are included at the end of events */
3836 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3837 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3838 &events
, &total_size
);
3841 /* Return value is a negative lttng_error_code. */
3846 cmd_header
.nb_events
= nb_event
;
3847 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3848 &cmd_header
, sizeof(cmd_header
));
3858 case LTTNG_LIST_SESSIONS
:
3860 unsigned int nr_sessions
;
3861 void *sessions_payload
;
3864 session_lock_list();
3865 nr_sessions
= lttng_sessions_count(
3866 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3867 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3868 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3869 sessions_payload
= zmalloc(payload_len
);
3871 if (!sessions_payload
) {
3872 session_unlock_list();
3877 cmd_list_lttng_sessions(sessions_payload
,
3878 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3879 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3880 session_unlock_list();
3882 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3884 free(sessions_payload
);
3893 case LTTNG_REGISTER_CONSUMER
:
3895 struct consumer_data
*cdata
;
3897 switch (cmd_ctx
->lsm
->domain
.type
) {
3898 case LTTNG_DOMAIN_KERNEL
:
3899 cdata
= &kconsumer_data
;
3902 ret
= LTTNG_ERR_UND
;
3906 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3907 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3910 case LTTNG_DATA_PENDING
:
3913 uint8_t pending_ret_byte
;
3915 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3920 * This function may returns 0 or 1 to indicate whether or not
3921 * there is data pending. In case of error, it should return an
3922 * LTTNG_ERR code. However, some code paths may still return
3923 * a nondescript error code, which we handle by returning an
3926 if (pending_ret
== 0 || pending_ret
== 1) {
3928 * ret will be set to LTTNG_OK at the end of
3931 } else if (pending_ret
< 0) {
3932 ret
= LTTNG_ERR_UNK
;
3939 pending_ret_byte
= (uint8_t) pending_ret
;
3941 /* 1 byte to return whether or not data is pending */
3942 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3943 &pending_ret_byte
, 1);
3952 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3954 struct lttcomm_lttng_output_id reply
;
3956 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3957 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3958 if (ret
!= LTTNG_OK
) {
3962 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3968 /* Copy output list into message payload */
3972 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3974 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3975 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3978 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3981 struct lttng_snapshot_output
*outputs
= NULL
;
3983 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3984 if (nb_output
< 0) {
3989 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3990 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3991 nb_output
* sizeof(struct lttng_snapshot_output
));
4001 case LTTNG_SNAPSHOT_RECORD
:
4003 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4004 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4005 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4008 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4011 struct lttng_uri
*uris
= NULL
;
4013 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4014 len
= nb_uri
* sizeof(struct lttng_uri
);
4017 uris
= zmalloc(len
);
4019 ret
= LTTNG_ERR_FATAL
;
4023 /* Receive variable len data */
4024 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4025 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4027 DBG("No URIs received from client... continuing");
4029 ret
= LTTNG_ERR_SESSION_FAIL
;
4034 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4035 DBG("Creating session with ONE network URI is a bad call");
4036 ret
= LTTNG_ERR_SESSION_FAIL
;
4042 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4043 nb_uri
, &cmd_ctx
->creds
);
4047 case LTTNG_CREATE_SESSION_LIVE
:
4050 struct lttng_uri
*uris
= NULL
;
4052 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4053 len
= nb_uri
* sizeof(struct lttng_uri
);
4056 uris
= zmalloc(len
);
4058 ret
= LTTNG_ERR_FATAL
;
4062 /* Receive variable len data */
4063 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4064 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4066 DBG("No URIs received from client... continuing");
4068 ret
= LTTNG_ERR_SESSION_FAIL
;
4073 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4074 DBG("Creating session with ONE network URI is a bad call");
4075 ret
= LTTNG_ERR_SESSION_FAIL
;
4081 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4082 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4086 case LTTNG_SAVE_SESSION
:
4088 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4092 case LTTNG_SET_SESSION_SHM_PATH
:
4094 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4095 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4098 case LTTNG_REGENERATE_METADATA
:
4100 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4103 case LTTNG_REGENERATE_STATEDUMP
:
4105 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4108 case LTTNG_REGISTER_TRIGGER
:
4110 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4111 notification_thread_handle
);
4114 case LTTNG_UNREGISTER_TRIGGER
:
4116 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4117 notification_thread_handle
);
4120 case LTTNG_ROTATE_SESSION
:
4122 struct lttng_rotate_session_return rotate_return
;
4124 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4126 memset(&rotate_return
, 0, sizeof(rotate_return
));
4127 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4128 DBG("Kernel tracer version is not compatible with the rotation feature");
4129 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4133 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4139 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4140 sizeof(rotate_return
));
4149 case LTTNG_ROTATION_GET_INFO
:
4151 struct lttng_rotation_get_info_return get_info_return
;
4153 memset(&get_info_return
, 0, sizeof(get_info_return
));
4154 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4155 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4161 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4162 sizeof(get_info_return
));
4171 case LTTNG_SESSION_GET_CURRENT_OUTPUT
:
4173 struct lttng_session_get_current_output_return output_return
;
4175 memset(&output_return
, 0, sizeof(output_return
));
4176 ret
= cmd_session_get_current_output(cmd_ctx
->session
,
4183 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &output_return
,
4184 sizeof(output_return
));
4193 case LTTNG_ROTATION_SET_SCHEDULE
:
4195 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4196 DBG("Kernel tracer version does not support session rotations");
4197 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4201 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4202 cmd_ctx
->lsm
->u
.rotate_setup
.timer_us
,
4203 cmd_ctx
->lsm
->u
.rotate_setup
.size
);
4212 case LTTNG_ROTATION_SCHEDULE_GET_TIMER_PERIOD
:
4214 struct lttng_rotation_schedule_get_timer_period
*get_timer
;
4216 get_timer
= zmalloc(sizeof(struct lttng_rotation_schedule_get_timer_period
));
4221 get_timer
->rotate_timer
= cmd_ctx
->session
->rotate_timer_period
;
4223 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, get_timer
,
4224 sizeof(struct lttng_rotation_schedule_get_timer_period
));
4234 case LTTNG_ROTATION_SCHEDULE_GET_SIZE
:
4236 struct lttng_rotation_schedule_get_size
*get_size
;
4238 get_size
= zmalloc(sizeof(struct lttng_rotation_schedule_get_size
));
4243 get_size
->rotate_size
= cmd_ctx
->session
->rotate_size
;
4245 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, get_size
,
4246 sizeof(struct lttng_rotation_schedule_get_size
));
4257 ret
= LTTNG_ERR_UND
;
4262 if (cmd_ctx
->llm
== NULL
) {
4263 DBG("Missing llm structure. Allocating one.");
4264 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4268 /* Set return code */
4269 cmd_ctx
->llm
->ret_code
= ret
;
4271 if (cmd_ctx
->session
) {
4272 session_unlock(cmd_ctx
->session
);
4274 if (need_tracing_session
) {
4275 session_unlock_list();
4278 assert(!rcu_read_ongoing());
4283 * Thread managing health check socket.
4285 static void *thread_manage_health(void *data
)
4287 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4288 uint32_t revents
, nb_fd
;
4289 struct lttng_poll_event events
;
4290 struct health_comm_msg msg
;
4291 struct health_comm_reply reply
;
4293 DBG("[thread] Manage health check started");
4295 rcu_register_thread();
4297 /* We might hit an error path before this is created. */
4298 lttng_poll_init(&events
);
4300 /* Create unix socket */
4301 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4303 ERR("Unable to create health check Unix socket");
4308 /* lttng health client socket path permissions */
4309 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4310 utils_get_group_id(config
.tracing_group_name
.value
));
4312 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4317 ret
= chmod(config
.health_unix_sock_path
.value
,
4318 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4320 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4327 * Set the CLOEXEC flag. Return code is useless because either way, the
4330 (void) utils_set_fd_cloexec(sock
);
4332 ret
= lttcomm_listen_unix_sock(sock
);
4338 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4339 * more will be added to this poll set.
4341 ret
= sessiond_set_thread_pollset(&events
, 2);
4346 /* Add the application registration socket */
4347 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4352 sessiond_notify_ready();
4355 DBG("Health check ready");
4357 /* Inifinite blocking call, waiting for transmission */
4359 ret
= lttng_poll_wait(&events
, -1);
4362 * Restart interrupted system call.
4364 if (errno
== EINTR
) {
4372 for (i
= 0; i
< nb_fd
; i
++) {
4373 /* Fetch once the poll data */
4374 revents
= LTTNG_POLL_GETEV(&events
, i
);
4375 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4378 /* No activity for this FD (poll implementation). */
4382 /* Thread quit pipe has been closed. Killing thread. */
4383 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4389 /* Event on the registration socket */
4390 if (pollfd
== sock
) {
4391 if (revents
& LPOLLIN
) {
4393 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4394 ERR("Health socket poll error");
4397 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4403 new_sock
= lttcomm_accept_unix_sock(sock
);
4409 * Set the CLOEXEC flag. Return code is useless because either way, the
4412 (void) utils_set_fd_cloexec(new_sock
);
4414 DBG("Receiving data from client for health...");
4415 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4417 DBG("Nothing recv() from client... continuing");
4418 ret
= close(new_sock
);
4425 rcu_thread_online();
4427 memset(&reply
, 0, sizeof(reply
));
4428 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4430 * health_check_state returns 0 if health is
4433 if (!health_check_state(health_sessiond
, i
)) {
4434 reply
.ret_code
|= 1ULL << i
;
4438 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4440 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4442 ERR("Failed to send health data back to client");
4445 /* End of transmission */
4446 ret
= close(new_sock
);
4455 ERR("Health error occurred in %s", __func__
);
4457 DBG("Health check thread dying");
4458 unlink(config
.health_unix_sock_path
.value
);
4466 lttng_poll_clean(&events
);
4468 rcu_unregister_thread();
4473 * This thread manage all clients request using the unix client socket for
4476 static void *thread_manage_clients(void *data
)
4478 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4480 uint32_t revents
, nb_fd
;
4481 struct command_ctx
*cmd_ctx
= NULL
;
4482 struct lttng_poll_event events
;
4484 DBG("[thread] Manage client started");
4486 rcu_register_thread();
4488 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4490 health_code_update();
4492 ret
= lttcomm_listen_unix_sock(client_sock
);
4498 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4499 * more will be added to this poll set.
4501 ret
= sessiond_set_thread_pollset(&events
, 2);
4503 goto error_create_poll
;
4506 /* Add the application registration socket */
4507 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4512 sessiond_notify_ready();
4513 ret
= sem_post(&load_info
->message_thread_ready
);
4515 PERROR("sem_post message_thread_ready");
4519 /* This testpoint is after we signal readiness to the parent. */
4520 if (testpoint(sessiond_thread_manage_clients
)) {
4524 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4528 health_code_update();
4531 DBG("Accepting client command ...");
4533 /* Inifinite blocking call, waiting for transmission */
4535 health_poll_entry();
4536 ret
= lttng_poll_wait(&events
, -1);
4540 * Restart interrupted system call.
4542 if (errno
== EINTR
) {
4550 for (i
= 0; i
< nb_fd
; i
++) {
4551 /* Fetch once the poll data */
4552 revents
= LTTNG_POLL_GETEV(&events
, i
);
4553 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4555 health_code_update();
4558 /* No activity for this FD (poll implementation). */
4562 /* Thread quit pipe has been closed. Killing thread. */
4563 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4569 /* Event on the registration socket */
4570 if (pollfd
== client_sock
) {
4571 if (revents
& LPOLLIN
) {
4573 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4574 ERR("Client socket poll error");
4577 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4583 DBG("Wait for client response");
4585 health_code_update();
4587 sock
= lttcomm_accept_unix_sock(client_sock
);
4593 * Set the CLOEXEC flag. Return code is useless because either way, the
4596 (void) utils_set_fd_cloexec(sock
);
4598 /* Set socket option for credentials retrieval */
4599 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4604 /* Allocate context command to process the client request */
4605 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4606 if (cmd_ctx
== NULL
) {
4607 PERROR("zmalloc cmd_ctx");
4611 /* Allocate data buffer for reception */
4612 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4613 if (cmd_ctx
->lsm
== NULL
) {
4614 PERROR("zmalloc cmd_ctx->lsm");
4618 cmd_ctx
->llm
= NULL
;
4619 cmd_ctx
->session
= NULL
;
4621 health_code_update();
4624 * Data is received from the lttng client. The struct
4625 * lttcomm_session_msg (lsm) contains the command and data request of
4628 DBG("Receiving data from client ...");
4629 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4630 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4632 DBG("Nothing recv() from client... continuing");
4638 clean_command_ctx(&cmd_ctx
);
4642 health_code_update();
4644 // TODO: Validate cmd_ctx including sanity check for
4645 // security purpose.
4647 rcu_thread_online();
4649 * This function dispatch the work to the kernel or userspace tracer
4650 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4651 * informations for the client. The command context struct contains
4652 * everything this function may needs.
4654 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4655 rcu_thread_offline();
4663 * TODO: Inform client somehow of the fatal error. At
4664 * this point, ret < 0 means that a zmalloc failed
4665 * (ENOMEM). Error detected but still accept
4666 * command, unless a socket error has been
4669 clean_command_ctx(&cmd_ctx
);
4673 health_code_update();
4675 DBG("Sending response (size: %d, retcode: %s (%d))",
4676 cmd_ctx
->lttng_msg_size
,
4677 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4678 cmd_ctx
->llm
->ret_code
);
4679 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4681 ERR("Failed to send data back to client");
4684 /* End of transmission */
4691 clean_command_ctx(&cmd_ctx
);
4693 health_code_update();
4705 lttng_poll_clean(&events
);
4706 clean_command_ctx(&cmd_ctx
);
4710 unlink(config
.client_unix_sock_path
.value
);
4711 if (client_sock
>= 0) {
4712 ret
= close(client_sock
);
4720 ERR("Health error occurred in %s", __func__
);
4723 health_unregister(health_sessiond
);
4725 DBG("Client thread dying");
4727 rcu_unregister_thread();
4730 * Since we are creating the consumer threads, we own them, so we need
4731 * to join them before our thread exits.
4733 ret
= join_consumer_thread(&kconsumer_data
);
4736 PERROR("join_consumer");
4739 ret
= join_consumer_thread(&ustconsumer32_data
);
4742 PERROR("join_consumer ust32");
4745 ret
= join_consumer_thread(&ustconsumer64_data
);
4748 PERROR("join_consumer ust64");
4753 static int string_match(const char *str1
, const char *str2
)
4755 return (str1
&& str2
) && !strcmp(str1
, str2
);
4759 * Take an option from the getopt output and set it in the right variable to be
4762 * Return 0 on success else a negative value.
4764 static int set_option(int opt
, const char *arg
, const char *optname
)
4768 if (string_match(optname
, "client-sock") || opt
== 'c') {
4769 if (!arg
|| *arg
== '\0') {
4773 if (lttng_is_setuid_setgid()) {
4774 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4775 "-c, --client-sock");
4777 config_string_set(&config
.client_unix_sock_path
,
4779 if (!config
.client_unix_sock_path
.value
) {
4784 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4785 if (!arg
|| *arg
== '\0') {
4789 if (lttng_is_setuid_setgid()) {
4790 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4793 config_string_set(&config
.apps_unix_sock_path
,
4795 if (!config
.apps_unix_sock_path
.value
) {
4800 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4801 config
.daemonize
= true;
4802 } else if (string_match(optname
, "background") || opt
== 'b') {
4803 config
.background
= true;
4804 } else if (string_match(optname
, "group") || opt
== 'g') {
4805 if (!arg
|| *arg
== '\0') {
4809 if (lttng_is_setuid_setgid()) {
4810 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4813 config_string_set(&config
.tracing_group_name
,
4815 if (!config
.tracing_group_name
.value
) {
4820 } else if (string_match(optname
, "help") || opt
== 'h') {
4821 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4823 ERR("Cannot show --help for `lttng-sessiond`");
4826 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4827 } else if (string_match(optname
, "version") || opt
== 'V') {
4828 fprintf(stdout
, "%s\n", VERSION
);
4830 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4831 config
.sig_parent
= true;
4832 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4833 if (!arg
|| *arg
== '\0') {
4837 if (lttng_is_setuid_setgid()) {
4838 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4839 "--kconsumerd-err-sock");
4841 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4843 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4848 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4849 if (!arg
|| *arg
== '\0') {
4853 if (lttng_is_setuid_setgid()) {
4854 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4855 "--kconsumerd-cmd-sock");
4857 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4859 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4864 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4865 if (!arg
|| *arg
== '\0') {
4869 if (lttng_is_setuid_setgid()) {
4870 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4871 "--ustconsumerd64-err-sock");
4873 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4875 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4880 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4881 if (!arg
|| *arg
== '\0') {
4885 if (lttng_is_setuid_setgid()) {
4886 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4887 "--ustconsumerd64-cmd-sock");
4889 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4891 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4896 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4897 if (!arg
|| *arg
== '\0') {
4901 if (lttng_is_setuid_setgid()) {
4902 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4903 "--ustconsumerd32-err-sock");
4905 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4907 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4912 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4913 if (!arg
|| *arg
== '\0') {
4917 if (lttng_is_setuid_setgid()) {
4918 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4919 "--ustconsumerd32-cmd-sock");
4921 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4923 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4928 } else if (string_match(optname
, "no-kernel")) {
4929 config
.no_kernel
= true;
4930 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4931 lttng_opt_quiet
= true;
4932 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4933 /* Verbose level can increase using multiple -v */
4935 /* Value obtained from config file */
4936 config
.verbose
= config_parse_value(arg
);
4938 /* -v used on command line */
4941 /* Clamp value to [0, 3] */
4942 config
.verbose
= config
.verbose
< 0 ? 0 :
4943 (config
.verbose
<= 3 ? config
.verbose
: 3);
4944 } else if (string_match(optname
, "verbose-consumer")) {
4946 config
.verbose_consumer
= config_parse_value(arg
);
4948 config
.verbose_consumer
++;
4950 } else if (string_match(optname
, "consumerd32-path")) {
4951 if (!arg
|| *arg
== '\0') {
4955 if (lttng_is_setuid_setgid()) {
4956 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4957 "--consumerd32-path");
4959 config_string_set(&config
.consumerd32_bin_path
,
4961 if (!config
.consumerd32_bin_path
.value
) {
4966 } else if (string_match(optname
, "consumerd32-libdir")) {
4967 if (!arg
|| *arg
== '\0') {
4971 if (lttng_is_setuid_setgid()) {
4972 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4973 "--consumerd32-libdir");
4975 config_string_set(&config
.consumerd32_lib_dir
,
4977 if (!config
.consumerd32_lib_dir
.value
) {
4982 } else if (string_match(optname
, "consumerd64-path")) {
4983 if (!arg
|| *arg
== '\0') {
4987 if (lttng_is_setuid_setgid()) {
4988 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4989 "--consumerd64-path");
4991 config_string_set(&config
.consumerd64_bin_path
,
4993 if (!config
.consumerd64_bin_path
.value
) {
4998 } else if (string_match(optname
, "consumerd64-libdir")) {
4999 if (!arg
|| *arg
== '\0') {
5003 if (lttng_is_setuid_setgid()) {
5004 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5005 "--consumerd64-libdir");
5007 config_string_set(&config
.consumerd64_lib_dir
,
5009 if (!config
.consumerd64_lib_dir
.value
) {
5014 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
5015 if (!arg
|| *arg
== '\0') {
5019 if (lttng_is_setuid_setgid()) {
5020 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5023 config_string_set(&config
.pid_file_path
, strdup(arg
));
5024 if (!config
.pid_file_path
.value
) {
5029 } else if (string_match(optname
, "agent-tcp-port")) {
5030 if (!arg
|| *arg
== '\0') {
5034 if (lttng_is_setuid_setgid()) {
5035 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5036 "--agent-tcp-port");
5041 v
= strtoul(arg
, NULL
, 0);
5042 if (errno
!= 0 || !isdigit(arg
[0])) {
5043 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5046 if (v
== 0 || v
>= 65535) {
5047 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5050 config
.agent_tcp_port
= (uint32_t) v
;
5051 DBG3("Agent TCP port set to non default: %u", config
.agent_tcp_port
);
5053 } else if (string_match(optname
, "load") || opt
== 'l') {
5054 if (!arg
|| *arg
== '\0') {
5058 if (lttng_is_setuid_setgid()) {
5059 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5062 config_string_set(&config
.load_session_path
, strdup(arg
));
5063 if (!config
.load_session_path
.value
) {
5068 } else if (string_match(optname
, "kmod-probes")) {
5069 if (!arg
|| *arg
== '\0') {
5073 if (lttng_is_setuid_setgid()) {
5074 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5077 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
5078 if (!config
.kmod_probes_list
.value
) {
5083 } else if (string_match(optname
, "extra-kmod-probes")) {
5084 if (!arg
|| *arg
== '\0') {
5088 if (lttng_is_setuid_setgid()) {
5089 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5090 "--extra-kmod-probes");
5092 config_string_set(&config
.kmod_extra_probes_list
,
5094 if (!config
.kmod_extra_probes_list
.value
) {
5099 } else if (string_match(optname
, "config") || opt
== 'f') {
5100 /* This is handled in set_options() thus silent skip. */
5103 /* Unknown option or other error.
5104 * Error is printed by getopt, just return */
5109 if (ret
== -EINVAL
) {
5110 const char *opt_name
= "unknown";
5113 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5115 if (opt
== long_options
[i
].val
) {
5116 opt_name
= long_options
[i
].name
;
5121 WARN("Invalid argument provided for option \"%s\", using default value.",
5129 * config_entry_handler_cb used to handle options read from a config file.
5130 * See config_entry_handler_cb comment in common/config/session-config.h for the
5131 * return value conventions.
5133 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5137 if (!entry
|| !entry
->name
|| !entry
->value
) {
5142 /* Check if the option is to be ignored */
5143 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5144 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5149 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5152 /* Ignore if not fully matched. */
5153 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5158 * If the option takes no argument on the command line, we have to
5159 * check if the value is "true". We support non-zero numeric values,
5162 if (!long_options
[i
].has_arg
) {
5163 ret
= config_parse_value(entry
->value
);
5166 WARN("Invalid configuration value \"%s\" for option %s",
5167 entry
->value
, entry
->name
);
5169 /* False, skip boolean config option. */
5174 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5178 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5185 * daemon configuration loading and argument parsing
5187 static int set_options(int argc
, char **argv
)
5189 int ret
= 0, c
= 0, option_index
= 0;
5190 int orig_optopt
= optopt
, orig_optind
= optind
;
5192 const char *config_path
= NULL
;
5194 optstring
= utils_generate_optstring(long_options
,
5195 sizeof(long_options
) / sizeof(struct option
));
5201 /* Check for the --config option */
5202 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5203 &option_index
)) != -1) {
5207 } else if (c
!= 'f') {
5208 /* if not equal to --config option. */
5212 if (lttng_is_setuid_setgid()) {
5213 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5216 config_path
= utils_expand_path(optarg
);
5218 ERR("Failed to resolve path: %s", optarg
);
5223 ret
= config_get_section_entries(config_path
, config_section_name
,
5224 config_entry_handler
, NULL
);
5227 ERR("Invalid configuration option at line %i", ret
);
5233 /* Reset getopt's global state */
5234 optopt
= orig_optopt
;
5235 optind
= orig_optind
;
5239 * getopt_long() will not set option_index if it encounters a
5242 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5249 * Pass NULL as the long option name if popt left the index
5252 ret
= set_option(c
, optarg
,
5253 option_index
< 0 ? NULL
:
5254 long_options
[option_index
].name
);
5266 * Creates the two needed socket by the daemon.
5267 * apps_sock - The communication socket for all UST apps.
5268 * client_sock - The communication of the cli tool (lttng).
5270 static int init_daemon_socket(void)
5275 old_umask
= umask(0);
5277 /* Create client tool unix socket */
5278 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5279 if (client_sock
< 0) {
5280 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5285 /* Set the cloexec flag */
5286 ret
= utils_set_fd_cloexec(client_sock
);
5288 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5289 "Continuing but note that the consumer daemon will have a "
5290 "reference to this socket on exec()", client_sock
);
5293 /* File permission MUST be 660 */
5294 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5296 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5301 /* Create the application unix socket */
5302 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5303 if (apps_sock
< 0) {
5304 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5309 /* Set the cloexec flag */
5310 ret
= utils_set_fd_cloexec(apps_sock
);
5312 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5313 "Continuing but note that the consumer daemon will have a "
5314 "reference to this socket on exec()", apps_sock
);
5317 /* File permission MUST be 666 */
5318 ret
= chmod(config
.apps_unix_sock_path
.value
,
5319 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5321 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5326 DBG3("Session daemon client socket %d and application socket %d created",
5327 client_sock
, apps_sock
);
5335 * Check if the global socket is available, and if a daemon is answering at the
5336 * other side. If yes, error is returned.
5338 static int check_existing_daemon(void)
5340 /* Is there anybody out there ? */
5341 if (lttng_session_daemon_alive()) {
5349 * Set the tracing group gid onto the client socket.
5351 * Race window between mkdir and chown is OK because we are going from more
5352 * permissive (root.root) to less permissive (root.tracing).
5354 static int set_permissions(char *rundir
)
5359 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5361 /* Set lttng run dir */
5362 ret
= chown(rundir
, 0, gid
);
5364 ERR("Unable to set group on %s", rundir
);
5369 * Ensure all applications and tracing group can search the run
5370 * dir. Allow everyone to read the directory, since it does not
5371 * buy us anything to hide its content.
5373 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5375 ERR("Unable to set permissions on %s", rundir
);
5379 /* lttng client socket path */
5380 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5382 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5386 /* kconsumer error socket path */
5387 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5389 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5393 /* 64-bit ustconsumer error socket path */
5394 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5396 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5400 /* 32-bit ustconsumer compat32 error socket path */
5401 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5403 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5407 DBG("All permissions are set");
5413 * Create the lttng run directory needed for all global sockets and pipe.
5415 static int create_lttng_rundir(void)
5419 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5421 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5423 if (errno
!= EEXIST
) {
5424 ERR("Unable to create %s", config
.rundir
.value
);
5436 * Setup sockets and directory needed by the consumerds' communication with the
5439 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5444 switch (consumer_data
->type
) {
5445 case LTTNG_CONSUMER_KERNEL
:
5446 path
= config
.kconsumerd_path
.value
;
5448 case LTTNG_CONSUMER64_UST
:
5449 path
= config
.consumerd64_path
.value
;
5451 case LTTNG_CONSUMER32_UST
:
5452 path
= config
.consumerd32_path
.value
;
5455 ERR("Consumer type unknown");
5461 DBG2("Creating consumer directory: %s", path
);
5463 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5464 if (ret
< 0 && errno
!= EEXIST
) {
5466 ERR("Failed to create %s", path
);
5470 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5472 ERR("Unable to set group on %s", path
);
5478 /* Create the consumerd error unix socket */
5479 consumer_data
->err_sock
=
5480 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5481 if (consumer_data
->err_sock
< 0) {
5482 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5488 * Set the CLOEXEC flag. Return code is useless because either way, the
5491 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5493 PERROR("utils_set_fd_cloexec");
5494 /* continue anyway */
5497 /* File permission MUST be 660 */
5498 ret
= chmod(consumer_data
->err_unix_sock_path
,
5499 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5501 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5511 * Signal handler for the daemon
5513 * Simply stop all worker threads, leaving main() return gracefully after
5514 * joining all threads and calling cleanup().
5516 static void sighandler(int sig
)
5520 DBG("SIGINT caught");
5524 DBG("SIGTERM caught");
5528 CMM_STORE_SHARED(recv_child_signal
, 1);
5536 * Setup signal handler for :
5537 * SIGINT, SIGTERM, SIGPIPE
5539 static int set_signal_handler(void)
5542 struct sigaction sa
;
5545 if ((ret
= sigemptyset(&sigset
)) < 0) {
5546 PERROR("sigemptyset");
5550 sa
.sa_mask
= sigset
;
5553 sa
.sa_handler
= sighandler
;
5554 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5555 PERROR("sigaction");
5559 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5560 PERROR("sigaction");
5564 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5565 PERROR("sigaction");
5569 sa
.sa_handler
= SIG_IGN
;
5570 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5571 PERROR("sigaction");
5575 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5581 * Set open files limit to unlimited. This daemon can open a large number of
5582 * file descriptors in order to consume multiple kernel traces.
5584 static void set_ulimit(void)
5589 /* The kernel does not allow an infinite limit for open files */
5590 lim
.rlim_cur
= 65535;
5591 lim
.rlim_max
= 65535;
5593 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5595 PERROR("failed to set open files limit");
5599 static int write_pidfile(void)
5601 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5605 * Create lockfile using the rundir and return its fd.
5607 static int create_lockfile(void)
5609 return utils_create_lock_file(config
.lock_file_path
.value
);
5613 * Write agent TCP port using the rundir.
5615 static int write_agent_port(void)
5617 return utils_create_pid_file(config
.agent_tcp_port
,
5618 config
.agent_port_file_path
.value
);
5621 static int set_clock_plugin_env(void)
5624 char *env_value
= NULL
;
5626 if (!config
.lttng_ust_clock_plugin
.value
) {
5630 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5631 config
.lttng_ust_clock_plugin
.value
);
5637 ret
= putenv(env_value
);
5640 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5644 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5645 config
.lttng_ust_clock_plugin
.value
);
5651 struct rotation_thread_timer_queue
*create_rotate_timer_queue(void)
5653 struct rotation_thread_timer_queue
*queue
= NULL
;
5655 queue
= zmalloc(sizeof(struct rotation_thread_timer_queue
));
5657 PERROR("Failed to allocate timer rotate queue");
5661 queue
->event_pipe
= lttng_pipe_open(FD_CLOEXEC
| O_NONBLOCK
);
5662 CDS_INIT_LIST_HEAD(&queue
->list
);
5663 pthread_mutex_init(&queue
->lock
, NULL
);
5670 void destroy_rotate_timer_queue(struct rotation_thread_timer_queue
*queue
)
5672 struct sessiond_rotation_timer
*node
, *tmp_node
;
5678 lttng_pipe_destroy(queue
->event_pipe
);
5680 pthread_mutex_lock(&queue
->lock
);
5681 /* Empty wait queue. */
5682 cds_list_for_each_entry_safe(node
, tmp_node
, &queue
->list
, head
) {
5683 cds_list_del(&node
->head
);
5686 pthread_mutex_unlock(&queue
->lock
);
5688 pthread_mutex_destroy(&queue
->lock
);
5695 int main(int argc
, char **argv
)
5697 int ret
= 0, retval
= 0;
5699 const char *env_app_timeout
;
5700 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5701 *ust64_channel_monitor_pipe
= NULL
,
5702 *kernel_channel_monitor_pipe
= NULL
;
5703 bool notification_thread_running
= false;
5704 bool rotation_thread_running
= false;
5705 bool timer_thread_running
= false;
5706 struct lttng_pipe
*ust32_channel_rotate_pipe
= NULL
,
5707 *ust64_channel_rotate_pipe
= NULL
,
5708 *kernel_channel_rotate_pipe
= NULL
;
5709 struct timer_thread_parameters timer_thread_ctx
;
5710 /* Queue of rotation jobs populated by the sessiond-timer. */
5711 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5713 init_kernel_workarounds();
5715 rcu_register_thread();
5717 if (set_signal_handler()) {
5719 goto exit_set_signal_handler
;
5722 if (sessiond_timer_signal_init()) {
5724 goto exit_set_signal_handler
;
5727 page_size
= sysconf(_SC_PAGESIZE
);
5728 if (page_size
< 0) {
5729 PERROR("sysconf _SC_PAGESIZE");
5730 page_size
= LONG_MAX
;
5731 WARN("Fallback page size to %ld", page_size
);
5734 ret
= sessiond_config_init(&config
);
5737 goto exit_set_signal_handler
;
5741 * Parse arguments and load the daemon configuration file.
5743 * We have an exit_options exit path to free memory reserved by
5744 * set_options. This is needed because the rest of sessiond_cleanup()
5745 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5746 * depends on set_options.
5749 if (set_options(argc
, argv
)) {
5754 /* Init config from environment variables. */
5755 sessiond_config_apply_env_config(&config
);
5758 * Resolve all paths received as arguments, configuration option, or
5759 * through environment variable as absolute paths. This is necessary
5760 * since daemonizing causes the sessiond's current working directory
5763 ret
= sessiond_config_resolve_paths(&config
);
5769 lttng_opt_verbose
= config
.verbose
;
5770 lttng_opt_quiet
= config
.quiet
;
5771 kconsumer_data
.err_unix_sock_path
=
5772 config
.kconsumerd_err_unix_sock_path
.value
;
5773 kconsumer_data
.cmd_unix_sock_path
=
5774 config
.kconsumerd_cmd_unix_sock_path
.value
;
5775 ustconsumer32_data
.err_unix_sock_path
=
5776 config
.consumerd32_err_unix_sock_path
.value
;
5777 ustconsumer32_data
.cmd_unix_sock_path
=
5778 config
.consumerd32_cmd_unix_sock_path
.value
;
5779 ustconsumer64_data
.err_unix_sock_path
=
5780 config
.consumerd64_err_unix_sock_path
.value
;
5781 ustconsumer64_data
.cmd_unix_sock_path
=
5782 config
.consumerd64_cmd_unix_sock_path
.value
;
5783 set_clock_plugin_env();
5785 sessiond_config_log(&config
);
5788 if (config
.daemonize
|| config
.background
) {
5791 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5792 !config
.background
);
5799 * We are in the child. Make sure all other file descriptors are
5800 * closed, in case we are called with more opened file
5801 * descriptors than the standard ones.
5803 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5808 if (run_as_create_worker(argv
[0]) < 0) {
5809 goto exit_create_run_as_worker_cleanup
;
5813 * Starting from here, we can create threads. This needs to be after
5814 * lttng_daemonize due to RCU.
5818 * Initialize the health check subsystem. This call should set the
5819 * appropriate time values.
5821 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5822 if (!health_sessiond
) {
5823 PERROR("health_app_create error");
5825 goto exit_health_sessiond_cleanup
;
5828 /* Create thread to clean up RCU hash tables */
5829 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5831 goto exit_ht_cleanup
;
5834 /* Create thread quit pipe */
5835 if (init_thread_quit_pipe()) {
5837 goto exit_init_data
;
5840 /* Check if daemon is UID = 0 */
5841 is_root
= !getuid();
5843 if (create_lttng_rundir()) {
5845 goto exit_init_data
;
5849 /* Create global run dir with root access */
5851 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5852 if (!kernel_channel_monitor_pipe
) {
5853 ERR("Failed to create kernel consumer channel monitor pipe");
5855 goto exit_init_data
;
5857 kconsumer_data
.channel_monitor_pipe
=
5858 lttng_pipe_release_writefd(
5859 kernel_channel_monitor_pipe
);
5860 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5862 goto exit_init_data
;
5864 kernel_channel_rotate_pipe
= lttng_pipe_open(0);
5865 if (!kernel_channel_rotate_pipe
) {
5866 ERR("Failed to create kernel consumer channel rotate pipe");
5868 goto exit_init_data
;
5870 kconsumer_data
.channel_rotate_pipe
=
5871 lttng_pipe_release_writefd(
5872 kernel_channel_rotate_pipe
);
5873 if (kconsumer_data
.channel_rotate_pipe
< 0) {
5875 goto exit_init_data
;
5879 lockfile_fd
= create_lockfile();
5880 if (lockfile_fd
< 0) {
5882 goto exit_init_data
;
5885 /* Set consumer initial state */
5886 kernel_consumerd_state
= CONSUMER_STOPPED
;
5887 ust_consumerd_state
= CONSUMER_STOPPED
;
5889 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5890 if (!ust32_channel_monitor_pipe
) {
5891 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5893 goto exit_init_data
;
5895 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5896 ust32_channel_monitor_pipe
);
5897 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5899 goto exit_init_data
;
5901 ust32_channel_rotate_pipe
= lttng_pipe_open(0);
5902 if (!ust32_channel_rotate_pipe
) {
5903 ERR("Failed to create 32-bit user space consumer channel rotate pipe");
5905 goto exit_init_data
;
5907 ustconsumer32_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
5908 ust32_channel_rotate_pipe
);
5909 if (ustconsumer32_data
.channel_rotate_pipe
< 0) {
5911 goto exit_init_data
;
5915 * The rotation_timer_queue structure is shared between the sessiond timer
5916 * thread and the rotation thread. The main() keeps the ownership and
5917 * destroys it when both threads have quit.
5919 rotation_timer_queue
= create_rotate_timer_queue();
5920 if (!rotation_timer_queue
) {
5922 goto exit_init_data
;
5924 timer_thread_ctx
.rotation_timer_queue
= rotation_timer_queue
;
5926 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5927 if (!ust64_channel_monitor_pipe
) {
5928 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5930 goto exit_init_data
;
5932 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5933 ust64_channel_monitor_pipe
);
5934 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5936 goto exit_init_data
;
5938 ust64_channel_rotate_pipe
= lttng_pipe_open(0);
5939 if (!ust64_channel_rotate_pipe
) {
5940 ERR("Failed to create 64-bit user space consumer channel rotate pipe");
5942 goto exit_init_data
;
5944 ustconsumer64_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
5945 ust64_channel_rotate_pipe
);
5946 if (ustconsumer64_data
.channel_rotate_pipe
< 0) {
5948 goto exit_init_data
;
5952 * See if daemon already exist.
5954 if (check_existing_daemon()) {
5955 ERR("Already running daemon.\n");
5957 * We do not goto exit because we must not cleanup()
5958 * because a daemon is already running.
5961 goto exit_init_data
;
5965 * Init UST app hash table. Alloc hash table before this point since
5966 * cleanup() can get called after that point.
5968 if (ust_app_ht_alloc()) {
5969 ERR("Failed to allocate UST app hash table");
5971 goto exit_init_data
;
5975 * Initialize agent app hash table. We allocate the hash table here
5976 * since cleanup() can get called after this point.
5978 if (agent_app_ht_alloc()) {
5979 ERR("Failed to allocate Agent app hash table");
5981 goto exit_init_data
;
5985 * These actions must be executed as root. We do that *after* setting up
5986 * the sockets path because we MUST make the check for another daemon using
5987 * those paths *before* trying to set the kernel consumer sockets and init
5991 if (set_consumer_sockets(&kconsumer_data
)) {
5993 goto exit_init_data
;
5996 /* Setup kernel tracer */
5997 if (!config
.no_kernel
) {
5998 init_kernel_tracer();
5999 if (kernel_tracer_fd
>= 0) {
6000 ret
= syscall_init_table();
6002 ERR("Unable to populate syscall table. "
6003 "Syscall tracing won't work "
6004 "for this session daemon.");
6009 /* Set ulimit for open files */
6012 /* init lttng_fd tracking must be done after set_ulimit. */
6015 if (set_consumer_sockets(&ustconsumer64_data
)) {
6017 goto exit_init_data
;
6020 if (set_consumer_sockets(&ustconsumer32_data
)) {
6022 goto exit_init_data
;
6025 /* Setup the needed unix socket */
6026 if (init_daemon_socket()) {
6028 goto exit_init_data
;
6031 /* Set credentials to socket */
6032 if (is_root
&& set_permissions(config
.rundir
.value
)) {
6034 goto exit_init_data
;
6037 /* Get parent pid if -S, --sig-parent is specified. */
6038 if (config
.sig_parent
) {
6042 /* Setup the kernel pipe for waking up the kernel thread */
6043 if (is_root
&& !config
.no_kernel
) {
6044 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6046 goto exit_init_data
;
6050 /* Setup the thread apps communication pipe. */
6051 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6053 goto exit_init_data
;
6056 /* Setup the thread apps notify communication pipe. */
6057 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6059 goto exit_init_data
;
6062 /* Initialize global buffer per UID and PID registry. */
6063 buffer_reg_init_uid_registry();
6064 buffer_reg_init_pid_registry();
6066 /* Init UST command queue. */
6067 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6070 * Get session list pointer. This pointer MUST NOT be free'd. This list
6071 * is statically declared in session.c
6073 session_list_ptr
= session_get_list();
6077 /* Check for the application socket timeout env variable. */
6078 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6079 if (env_app_timeout
) {
6080 config
.app_socket_timeout
= atoi(env_app_timeout
);
6082 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6085 ret
= write_pidfile();
6087 ERR("Error in write_pidfile");
6089 goto exit_init_data
;
6091 ret
= write_agent_port();
6093 ERR("Error in write_agent_port");
6095 goto exit_init_data
;
6098 /* Initialize communication library */
6100 /* Initialize TCP timeout values */
6101 lttcomm_inet_init();
6103 if (load_session_init_data(&load_info
) < 0) {
6105 goto exit_init_data
;
6107 load_info
->path
= config
.load_session_path
.value
;
6109 /* Create health-check thread. */
6110 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6111 thread_manage_health
, (void *) NULL
);
6114 PERROR("pthread_create health");
6119 /* notification_thread_data acquires the pipes' read side. */
6120 notification_thread_handle
= notification_thread_handle_create(
6121 ust32_channel_monitor_pipe
,
6122 ust64_channel_monitor_pipe
,
6123 kernel_channel_monitor_pipe
);
6124 if (!notification_thread_handle
) {
6126 ERR("Failed to create notification thread shared data");
6128 goto exit_notification
;
6131 /* Create notification thread. */
6132 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6133 thread_notification
, notification_thread_handle
);
6136 PERROR("pthread_create notification");
6139 goto exit_notification
;
6141 notification_thread_running
= true;
6143 /* Create timer thread. */
6144 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
6145 sessiond_timer_thread
, &timer_thread_ctx
);
6148 PERROR("pthread_create timer");
6151 goto exit_notification
;
6153 timer_thread_running
= true;
6155 /* rotation_thread_data acquires the pipes' read side. */
6156 rotation_thread_handle
= rotation_thread_handle_create(
6157 ust32_channel_rotate_pipe
,
6158 ust64_channel_rotate_pipe
,
6159 kernel_channel_rotate_pipe
,
6160 thread_quit_pipe
[0],
6161 rotation_timer_queue
);
6162 if (!rotation_thread_handle
) {
6164 ERR("Failed to create rotation thread shared data");
6169 /* Create rotation thread. */
6170 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6171 thread_rotation
, rotation_thread_handle
);
6174 PERROR("pthread_create rotation");
6179 rotation_thread_running
= true;
6181 /* Create thread to manage the client socket */
6182 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6183 thread_manage_clients
, (void *) NULL
);
6186 PERROR("pthread_create clients");
6192 /* Create thread to dispatch registration */
6193 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6194 thread_dispatch_ust_registration
, (void *) NULL
);
6197 PERROR("pthread_create dispatch");
6203 /* Create thread to manage application registration. */
6204 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6205 thread_registration_apps
, (void *) NULL
);
6208 PERROR("pthread_create registration");
6214 /* Create thread to manage application socket */
6215 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6216 thread_manage_apps
, (void *) NULL
);
6219 PERROR("pthread_create apps");
6225 /* Create thread to manage application notify socket */
6226 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6227 ust_thread_manage_notify
, (void *) NULL
);
6230 PERROR("pthread_create notify");
6233 goto exit_apps_notify
;
6236 /* Create agent registration thread. */
6237 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6238 agent_thread_manage_registration
, (void *) NULL
);
6241 PERROR("pthread_create agent");
6244 goto exit_agent_reg
;
6247 /* Don't start this thread if kernel tracing is not requested nor root */
6248 if (is_root
&& !config
.no_kernel
) {
6249 /* Create kernel thread to manage kernel event */
6250 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6251 thread_manage_kernel
, (void *) NULL
);
6254 PERROR("pthread_create kernel");
6261 /* Create session loading thread. */
6262 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6263 thread_load_session
, load_info
);
6266 PERROR("pthread_create load_session_thread");
6269 goto exit_load_session
;
6273 * This is where we start awaiting program completion (e.g. through
6274 * signal that asks threads to teardown).
6277 ret
= pthread_join(load_session_thread
, &status
);
6280 PERROR("pthread_join load_session_thread");
6285 if (is_root
&& !config
.no_kernel
) {
6286 ret
= pthread_join(kernel_thread
, &status
);
6289 PERROR("pthread_join");
6295 ret
= pthread_join(agent_reg_thread
, &status
);
6298 PERROR("pthread_join agent");
6303 ret
= pthread_join(apps_notify_thread
, &status
);
6306 PERROR("pthread_join apps notify");
6311 ret
= pthread_join(apps_thread
, &status
);
6314 PERROR("pthread_join apps");
6319 ret
= pthread_join(reg_apps_thread
, &status
);
6322 PERROR("pthread_join");
6328 * Join dispatch thread after joining reg_apps_thread to ensure
6329 * we don't leak applications in the queue.
6331 ret
= pthread_join(dispatch_thread
, &status
);
6334 PERROR("pthread_join");
6339 ret
= pthread_join(client_thread
, &status
);
6342 PERROR("pthread_join");
6349 ret
= pthread_join(health_thread
, &status
);
6352 PERROR("pthread_join health thread");
6359 * Wait for all pending call_rcu work to complete before tearing
6360 * down data structures. call_rcu worker may be trying to
6361 * perform lookups in those structures.
6365 * sessiond_cleanup() is called when no other thread is running, except
6366 * the ht_cleanup thread, which is needed to destroy the hash tables.
6368 rcu_thread_online();
6372 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6373 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6374 * the queue is empty before shutting down the clean-up thread.
6379 * The teardown of the notification system is performed after the
6380 * session daemon's teardown in order to allow it to be notified
6381 * of the active session and channels at the moment of the teardown.
6383 if (notification_thread_handle
) {
6384 if (notification_thread_running
) {
6385 notification_thread_command_quit(
6386 notification_thread_handle
);
6387 ret
= pthread_join(notification_thread
, &status
);
6390 PERROR("pthread_join notification thread");
6394 notification_thread_handle_destroy(notification_thread_handle
);
6397 if (rotation_thread_handle
) {
6398 if (rotation_thread_running
) {
6399 ret
= pthread_join(rotation_thread
, &status
);
6402 PERROR("pthread_join rotation thread");
6406 rotation_thread_handle_destroy(rotation_thread_handle
);
6409 if (timer_thread_running
) {
6410 kill(getpid(), LTTNG_SESSIOND_SIG_EXIT
);
6411 ret
= pthread_join(timer_thread
, &status
);
6414 PERROR("pthread_join timer thread");
6420 * After the rotation and timer thread have quit, we can safely destroy
6421 * the rotation_timer_queue.
6423 destroy_rotate_timer_queue(rotation_timer_queue
);
6425 rcu_thread_offline();
6426 rcu_unregister_thread();
6428 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6432 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6433 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6434 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6435 lttng_pipe_destroy(ust32_channel_rotate_pipe
);
6436 lttng_pipe_destroy(ust64_channel_rotate_pipe
);
6437 lttng_pipe_destroy(kernel_channel_rotate_pipe
);
6440 health_app_destroy(health_sessiond
);
6441 exit_health_sessiond_cleanup
:
6442 exit_create_run_as_worker_cleanup
:
6445 sessiond_cleanup_options();
6447 exit_set_signal_handler
: