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"
78 #include "ht-cleanup.h"
79 #include "sessiond-config.h"
81 static const char *help_msg
=
82 #ifdef LTTNG_EMBED_HELP
83 #include <lttng-sessiond.8.h>
90 static pid_t ppid
; /* Parent PID for --sig-parent option */
91 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
92 static int lockfile_fd
= -1;
94 /* Set to 1 when a SIGUSR1 signal is received. */
95 static int recv_child_signal
;
98 * Consumer daemon specific control data. Every value not initialized here is
99 * set to 0 by the static definition.
101 static struct consumer_data kconsumer_data
= {
102 .type
= LTTNG_CONSUMER_KERNEL
,
105 .channel_monitor_pipe
= -1,
106 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
107 .lock
= PTHREAD_MUTEX_INITIALIZER
,
108 .cond
= PTHREAD_COND_INITIALIZER
,
109 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
111 static struct consumer_data ustconsumer64_data
= {
112 .type
= LTTNG_CONSUMER64_UST
,
115 .channel_monitor_pipe
= -1,
116 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
117 .lock
= PTHREAD_MUTEX_INITIALIZER
,
118 .cond
= PTHREAD_COND_INITIALIZER
,
119 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
121 static struct consumer_data ustconsumer32_data
= {
122 .type
= LTTNG_CONSUMER32_UST
,
125 .channel_monitor_pipe
= -1,
126 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
127 .lock
= PTHREAD_MUTEX_INITIALIZER
,
128 .cond
= PTHREAD_COND_INITIALIZER
,
129 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
132 /* Command line options */
133 static const struct option long_options
[] = {
134 { "client-sock", required_argument
, 0, 'c' },
135 { "apps-sock", required_argument
, 0, 'a' },
136 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
137 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
138 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
139 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
140 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
141 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
142 { "consumerd32-path", required_argument
, 0, '\0' },
143 { "consumerd32-libdir", required_argument
, 0, '\0' },
144 { "consumerd64-path", required_argument
, 0, '\0' },
145 { "consumerd64-libdir", required_argument
, 0, '\0' },
146 { "daemonize", no_argument
, 0, 'd' },
147 { "background", no_argument
, 0, 'b' },
148 { "sig-parent", no_argument
, 0, 'S' },
149 { "help", no_argument
, 0, 'h' },
150 { "group", required_argument
, 0, 'g' },
151 { "version", no_argument
, 0, 'V' },
152 { "quiet", no_argument
, 0, 'q' },
153 { "verbose", no_argument
, 0, 'v' },
154 { "verbose-consumer", no_argument
, 0, '\0' },
155 { "no-kernel", no_argument
, 0, '\0' },
156 { "pidfile", required_argument
, 0, 'p' },
157 { "agent-tcp-port", required_argument
, 0, '\0' },
158 { "config", required_argument
, 0, 'f' },
159 { "load", required_argument
, 0, 'l' },
160 { "kmod-probes", required_argument
, 0, '\0' },
161 { "extra-kmod-probes", required_argument
, 0, '\0' },
165 struct sessiond_config config
;
167 /* Command line options to ignore from configuration file */
168 static const char *config_ignore_options
[] = { "help", "version", "config" };
170 /* Shared between threads */
171 static int dispatch_thread_exit
;
173 /* Sockets and FDs */
174 static int client_sock
= -1;
175 static int apps_sock
= -1;
176 int kernel_tracer_fd
= -1;
177 static int kernel_poll_pipe
[2] = { -1, -1 };
180 * Quit pipe for all threads. This permits a single cancellation point
181 * for all threads when receiving an event on the pipe.
183 static int thread_quit_pipe
[2] = { -1, -1 };
186 * This pipe is used to inform the thread managing application communication
187 * that a command is queued and ready to be processed.
189 static int apps_cmd_pipe
[2] = { -1, -1 };
191 int apps_cmd_notify_pipe
[2] = { -1, -1 };
193 /* Pthread, Mutexes and Semaphores */
194 static pthread_t apps_thread
;
195 static pthread_t apps_notify_thread
;
196 static pthread_t reg_apps_thread
;
197 static pthread_t client_thread
;
198 static pthread_t kernel_thread
;
199 static pthread_t dispatch_thread
;
200 static pthread_t health_thread
;
201 static pthread_t ht_cleanup_thread
;
202 static pthread_t agent_reg_thread
;
203 static pthread_t load_session_thread
;
204 static pthread_t notification_thread
;
207 * UST registration command queue. This queue is tied with a futex and uses a N
208 * wakers / 1 waiter implemented and detailed in futex.c/.h
210 * The thread_registration_apps and thread_dispatch_ust_registration uses this
211 * queue along with the wait/wake scheme. The thread_manage_apps receives down
212 * the line new application socket and monitors it for any I/O error or clean
213 * close that triggers an unregistration of the application.
215 static struct ust_cmd_queue ust_cmd_queue
;
218 * Pointer initialized before thread creation.
220 * This points to the tracing session list containing the session count and a
221 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
222 * MUST NOT be taken if you call a public function in session.c.
224 * The lock is nested inside the structure: session_list_ptr->lock. Please use
225 * session_lock_list and session_unlock_list for lock acquisition.
227 static struct ltt_session_list
*session_list_ptr
;
229 int ust_consumerd64_fd
= -1;
230 int ust_consumerd32_fd
= -1;
232 static const char *module_proc_lttng
= "/proc/lttng";
235 * Consumer daemon state which is changed when spawning it, killing it or in
236 * case of a fatal error.
238 enum consumerd_state
{
239 CONSUMER_STARTED
= 1,
240 CONSUMER_STOPPED
= 2,
245 * This consumer daemon state is used to validate if a client command will be
246 * able to reach the consumer. If not, the client is informed. For instance,
247 * doing a "lttng start" when the consumer state is set to ERROR will return an
248 * error to the client.
250 * The following example shows a possible race condition of this scheme:
252 * consumer thread error happens
254 * client cmd checks state -> still OK
255 * consumer thread exit, sets error
256 * client cmd try to talk to consumer
259 * However, since the consumer is a different daemon, we have no way of making
260 * sure the command will reach it safely even with this state flag. This is why
261 * we consider that up to the state validation during command processing, the
262 * command is safe. After that, we can not guarantee the correctness of the
263 * client request vis-a-vis the consumer.
265 static enum consumerd_state ust_consumerd_state
;
266 static enum consumerd_state kernel_consumerd_state
;
268 /* Set in main() with the current page size. */
271 /* Application health monitoring */
272 struct health_app
*health_sessiond
;
274 /* Am I root or not. */
275 int is_root
; /* Set to 1 if the daemon is running as root */
277 const char * const config_section_name
= "sessiond";
279 /* Load session thread information to operate. */
280 struct load_session_thread_data
*load_info
;
282 /* Notification thread handle. */
283 struct notification_thread_handle
*notification_thread_handle
;
285 /* Global hash tables */
286 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
289 * The initialization of the session daemon is done in multiple phases.
291 * While all threads are launched near-simultaneously, only some of them
292 * are needed to ensure the session daemon can start to respond to client
295 * There are two important guarantees that we wish to offer with respect
296 * to the initialisation of the session daemon:
297 * - When the daemonize/background launcher process exits, the sessiond
298 * is fully able to respond to client requests,
299 * - Auto-loaded sessions are visible to clients.
301 * In order to achieve this, a number of support threads have to be launched
302 * to allow the "client" thread to function properly. Moreover, since the
303 * "load session" thread needs the client thread, we must provide a way
304 * for the "load session" thread to know that the "client" thread is up
307 * Hence, the support threads decrement the lttng_sessiond_ready counter
308 * while the "client" threads waits for it to reach 0. Once the "client" thread
309 * unblocks, it posts the message_thread_ready semaphore which allows the
310 * "load session" thread to progress.
312 * This implies that the "load session" thread is the last to be initialized
313 * and will explicitly call sessiond_signal_parents(), which signals the parents
314 * that the session daemon is fully initialized.
316 * The three (3) support threads are:
318 * - notification_thread
321 int lttng_sessiond_ready
= 3;
323 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
325 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
328 /* Notify parents that we are ready for cmd and health check */
330 void sessiond_signal_parents(void)
333 * Notify parent pid that we are ready to accept command
334 * for client side. This ppid is the one from the
335 * external process that spawned us.
337 if (config
.sig_parent
) {
342 * Notify the parent of the fork() process that we are
345 if (config
.daemonize
|| config
.background
) {
346 kill(child_ppid
, SIGUSR1
);
351 void sessiond_notify_ready(void)
354 * The _return variant is used since the implied memory barriers are
357 (void) uatomic_sub_return(<tng_sessiond_ready
, 1);
361 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
368 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
374 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
386 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
388 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
390 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
394 * Init thread quit pipe.
396 * Return -1 on error or 0 if all pipes are created.
398 static int __init_thread_quit_pipe(int *a_pipe
)
404 PERROR("thread quit pipe");
408 for (i
= 0; i
< 2; i
++) {
409 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
420 static int init_thread_quit_pipe(void)
422 return __init_thread_quit_pipe(thread_quit_pipe
);
426 * Stop all threads by closing the thread quit pipe.
428 static void stop_threads(void)
432 /* Stopping all threads */
433 DBG("Terminating all threads");
434 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
436 ERR("write error on thread quit pipe");
439 /* Dispatch thread */
440 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
441 futex_nto1_wake(&ust_cmd_queue
.futex
);
445 * Close every consumer sockets.
447 static void close_consumer_sockets(void)
451 if (kconsumer_data
.err_sock
>= 0) {
452 ret
= close(kconsumer_data
.err_sock
);
454 PERROR("kernel consumer err_sock close");
457 if (ustconsumer32_data
.err_sock
>= 0) {
458 ret
= close(ustconsumer32_data
.err_sock
);
460 PERROR("UST consumerd32 err_sock close");
463 if (ustconsumer64_data
.err_sock
>= 0) {
464 ret
= close(ustconsumer64_data
.err_sock
);
466 PERROR("UST consumerd64 err_sock close");
469 if (kconsumer_data
.cmd_sock
>= 0) {
470 ret
= close(kconsumer_data
.cmd_sock
);
472 PERROR("kernel consumer cmd_sock close");
475 if (ustconsumer32_data
.cmd_sock
>= 0) {
476 ret
= close(ustconsumer32_data
.cmd_sock
);
478 PERROR("UST consumerd32 cmd_sock close");
481 if (ustconsumer64_data
.cmd_sock
>= 0) {
482 ret
= close(ustconsumer64_data
.cmd_sock
);
484 PERROR("UST consumerd64 cmd_sock close");
487 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
488 ret
= close(kconsumer_data
.channel_monitor_pipe
);
490 PERROR("kernel consumer channel monitor pipe close");
493 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
494 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
496 PERROR("UST consumerd32 channel monitor pipe close");
499 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
500 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
502 PERROR("UST consumerd64 channel monitor pipe close");
508 * Wait on consumer process termination.
510 * Need to be called with the consumer data lock held or from a context
511 * ensuring no concurrent access to data (e.g: cleanup).
513 static void wait_consumer(struct consumer_data
*consumer_data
)
518 if (consumer_data
->pid
<= 0) {
522 DBG("Waiting for complete teardown of consumerd (PID: %d)",
524 ret
= waitpid(consumer_data
->pid
, &status
, 0);
526 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
527 } else if (!WIFEXITED(status
)) {
528 ERR("consumerd termination with error: %d",
531 consumer_data
->pid
= 0;
535 * Cleanup the session daemon's data structures.
537 static void sessiond_cleanup(void)
540 struct ltt_session
*sess
, *stmp
;
542 DBG("Cleanup sessiond");
545 * Close the thread quit pipe. It has already done its job,
546 * since we are now called.
548 utils_close_pipe(thread_quit_pipe
);
550 ret
= remove(config
.pid_file_path
.value
);
552 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
555 DBG("Removing sessiond and consumerd content of directory %s",
556 config
.rundir
.value
);
559 DBG("Removing %s", config
.pid_file_path
.value
);
560 (void) unlink(config
.pid_file_path
.value
);
562 DBG("Removing %s", config
.agent_port_file_path
.value
);
563 (void) unlink(config
.agent_port_file_path
.value
);
566 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
567 (void) unlink(kconsumer_data
.err_unix_sock_path
);
569 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
570 (void) rmdir(config
.kconsumerd_path
.value
);
572 /* ust consumerd 32 */
573 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
574 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
576 DBG("Removing directory %s", config
.consumerd32_path
.value
);
577 (void) rmdir(config
.consumerd32_path
.value
);
579 /* ust consumerd 64 */
580 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
581 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
583 DBG("Removing directory %s", config
.consumerd64_path
.value
);
584 (void) rmdir(config
.consumerd64_path
.value
);
586 DBG("Cleaning up all sessions");
588 /* Destroy session list mutex */
589 if (session_list_ptr
!= NULL
) {
590 pthread_mutex_destroy(&session_list_ptr
->lock
);
592 /* Cleanup ALL session */
593 cds_list_for_each_entry_safe(sess
, stmp
,
594 &session_list_ptr
->head
, list
) {
595 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
599 wait_consumer(&kconsumer_data
);
600 wait_consumer(&ustconsumer64_data
);
601 wait_consumer(&ustconsumer32_data
);
603 DBG("Cleaning up all agent apps");
604 agent_app_ht_clean();
606 DBG("Closing all UST sockets");
607 ust_app_clean_list();
608 buffer_reg_destroy_registries();
610 if (is_root
&& !config
.no_kernel
) {
611 DBG2("Closing kernel fd");
612 if (kernel_tracer_fd
>= 0) {
613 ret
= close(kernel_tracer_fd
);
618 DBG("Unloading kernel modules");
619 modprobe_remove_lttng_all();
623 close_consumer_sockets();
626 load_session_destroy_data(load_info
);
631 * We do NOT rmdir rundir because there are other processes
632 * using it, for instance lttng-relayd, which can start in
633 * parallel with this teardown.
638 * Cleanup the daemon's option data structures.
640 static void sessiond_cleanup_options(void)
642 DBG("Cleaning up options");
644 sessiond_config_fini(&config
);
646 run_as_destroy_worker();
650 * Send data on a unix socket using the liblttsessiondcomm API.
652 * Return lttcomm error code.
654 static int send_unix_sock(int sock
, void *buf
, size_t len
)
656 /* Check valid length */
661 return lttcomm_send_unix_sock(sock
, buf
, len
);
665 * Free memory of a command context structure.
667 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
669 DBG("Clean command context structure");
671 if ((*cmd_ctx
)->llm
) {
672 free((*cmd_ctx
)->llm
);
674 if ((*cmd_ctx
)->lsm
) {
675 free((*cmd_ctx
)->lsm
);
683 * Notify UST applications using the shm mmap futex.
685 static int notify_ust_apps(int active
)
689 DBG("Notifying applications of session daemon state: %d", active
);
691 /* See shm.c for this call implying mmap, shm and futex calls */
692 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
693 if (wait_shm_mmap
== NULL
) {
697 /* Wake waiting process */
698 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
700 /* Apps notified successfully */
708 * Setup the outgoing data buffer for the response (llm) by allocating the
709 * right amount of memory and copying the original information from the lsm
712 * Return 0 on success, negative value on error.
714 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
715 const void *payload_buf
, size_t payload_len
,
716 const void *cmd_header_buf
, size_t cmd_header_len
)
719 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
720 const size_t cmd_header_offset
= header_len
;
721 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
722 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
724 cmd_ctx
->llm
= zmalloc(total_msg_size
);
726 if (cmd_ctx
->llm
== NULL
) {
732 /* Copy common data */
733 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
734 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
735 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
736 cmd_ctx
->llm
->data_size
= payload_len
;
737 cmd_ctx
->lttng_msg_size
= total_msg_size
;
739 /* Copy command header */
740 if (cmd_header_len
) {
741 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
747 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
756 * Version of setup_lttng_msg() without command header.
758 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
759 void *payload_buf
, size_t payload_len
)
761 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
764 * Update the kernel poll set of all channel fd available over all tracing
765 * session. Add the wakeup pipe at the end of the set.
767 static int update_kernel_poll(struct lttng_poll_event
*events
)
770 struct ltt_session
*session
;
771 struct ltt_kernel_channel
*channel
;
773 DBG("Updating kernel poll set");
776 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
777 session_lock(session
);
778 if (session
->kernel_session
== NULL
) {
779 session_unlock(session
);
783 cds_list_for_each_entry(channel
,
784 &session
->kernel_session
->channel_list
.head
, list
) {
785 /* Add channel fd to the kernel poll set */
786 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
788 session_unlock(session
);
791 DBG("Channel fd %d added to kernel set", channel
->fd
);
793 session_unlock(session
);
795 session_unlock_list();
800 session_unlock_list();
805 * Find the channel fd from 'fd' over all tracing session. When found, check
806 * for new channel stream and send those stream fds to the kernel consumer.
808 * Useful for CPU hotplug feature.
810 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
813 struct ltt_session
*session
;
814 struct ltt_kernel_session
*ksess
;
815 struct ltt_kernel_channel
*channel
;
817 DBG("Updating kernel streams for channel fd %d", fd
);
820 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
821 session_lock(session
);
822 if (session
->kernel_session
== NULL
) {
823 session_unlock(session
);
826 ksess
= session
->kernel_session
;
828 cds_list_for_each_entry(channel
,
829 &ksess
->channel_list
.head
, list
) {
830 struct lttng_ht_iter iter
;
831 struct consumer_socket
*socket
;
833 if (channel
->fd
!= fd
) {
836 DBG("Channel found, updating kernel streams");
837 ret
= kernel_open_channel_stream(channel
);
841 /* Update the stream global counter */
842 ksess
->stream_count_global
+= ret
;
845 * Have we already sent fds to the consumer? If yes, it
846 * means that tracing is started so it is safe to send
847 * our updated stream fds.
849 if (ksess
->consumer_fds_sent
!= 1
850 || ksess
->consumer
== NULL
) {
856 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
857 &iter
.iter
, socket
, node
.node
) {
858 pthread_mutex_lock(socket
->lock
);
859 ret
= kernel_consumer_send_channel_stream(socket
,
861 session
->output_traces
? 1 : 0);
862 pthread_mutex_unlock(socket
->lock
);
870 session_unlock(session
);
872 session_unlock_list();
876 session_unlock(session
);
877 session_unlock_list();
882 * For each tracing session, update newly registered apps. The session list
883 * lock MUST be acquired before calling this.
885 static void update_ust_app(int app_sock
)
887 struct ltt_session
*sess
, *stmp
;
889 /* Consumer is in an ERROR state. Stop any application update. */
890 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
891 /* Stop the update process since the consumer is dead. */
895 /* For all tracing session(s) */
896 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
900 if (!sess
->ust_session
) {
905 assert(app_sock
>= 0);
906 app
= ust_app_find_by_sock(app_sock
);
909 * Application can be unregistered before so
910 * this is possible hence simply stopping the
913 DBG3("UST app update failed to find app sock %d",
917 ust_app_global_update(sess
->ust_session
, app
);
921 session_unlock(sess
);
926 * This thread manage event coming from the kernel.
928 * Features supported in this thread:
931 static void *thread_manage_kernel(void *data
)
933 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
934 uint32_t revents
, nb_fd
;
936 struct lttng_poll_event events
;
938 DBG("[thread] Thread manage kernel started");
940 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
943 * This first step of the while is to clean this structure which could free
944 * non NULL pointers so initialize it before the loop.
946 lttng_poll_init(&events
);
948 if (testpoint(sessiond_thread_manage_kernel
)) {
949 goto error_testpoint
;
952 health_code_update();
954 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
955 goto error_testpoint
;
959 health_code_update();
961 if (update_poll_flag
== 1) {
962 /* Clean events object. We are about to populate it again. */
963 lttng_poll_clean(&events
);
965 ret
= sessiond_set_thread_pollset(&events
, 2);
967 goto error_poll_create
;
970 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
975 /* This will add the available kernel channel if any. */
976 ret
= update_kernel_poll(&events
);
980 update_poll_flag
= 0;
983 DBG("Thread kernel polling");
985 /* Poll infinite value of time */
988 ret
= lttng_poll_wait(&events
, -1);
989 DBG("Thread kernel return from poll on %d fds",
990 LTTNG_POLL_GETNB(&events
));
994 * Restart interrupted system call.
996 if (errno
== EINTR
) {
1000 } else if (ret
== 0) {
1001 /* Should not happen since timeout is infinite */
1002 ERR("Return value of poll is 0 with an infinite timeout.\n"
1003 "This should not have happened! Continuing...");
1009 for (i
= 0; i
< nb_fd
; i
++) {
1010 /* Fetch once the poll data */
1011 revents
= LTTNG_POLL_GETEV(&events
, i
);
1012 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1014 health_code_update();
1017 /* No activity for this FD (poll implementation). */
1021 /* Thread quit pipe has been closed. Killing thread. */
1022 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1028 /* Check for data on kernel pipe */
1029 if (revents
& LPOLLIN
) {
1030 if (pollfd
== kernel_poll_pipe
[0]) {
1031 (void) lttng_read(kernel_poll_pipe
[0],
1034 * Ret value is useless here, if this pipe gets any actions an
1035 * update is required anyway.
1037 update_poll_flag
= 1;
1041 * New CPU detected by the kernel. Adding kernel stream to
1042 * kernel session and updating the kernel consumer
1044 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1050 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1051 update_poll_flag
= 1;
1054 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1062 lttng_poll_clean(&events
);
1065 utils_close_pipe(kernel_poll_pipe
);
1066 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1069 ERR("Health error occurred in %s", __func__
);
1070 WARN("Kernel thread died unexpectedly. "
1071 "Kernel tracing can continue but CPU hotplug is disabled.");
1073 health_unregister(health_sessiond
);
1074 DBG("Kernel thread dying");
1079 * Signal pthread condition of the consumer data that the thread.
1081 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1083 pthread_mutex_lock(&data
->cond_mutex
);
1086 * The state is set before signaling. It can be any value, it's the waiter
1087 * job to correctly interpret this condition variable associated to the
1088 * consumer pthread_cond.
1090 * A value of 0 means that the corresponding thread of the consumer data
1091 * was not started. 1 indicates that the thread has started and is ready
1092 * for action. A negative value means that there was an error during the
1095 data
->consumer_thread_is_ready
= state
;
1096 (void) pthread_cond_signal(&data
->cond
);
1098 pthread_mutex_unlock(&data
->cond_mutex
);
1102 * This thread manage the consumer error sent back to the session daemon.
1104 static void *thread_manage_consumer(void *data
)
1106 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1107 uint32_t revents
, nb_fd
;
1108 enum lttcomm_return_code code
;
1109 struct lttng_poll_event events
;
1110 struct consumer_data
*consumer_data
= data
;
1111 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1113 DBG("[thread] Manage consumer started");
1115 rcu_register_thread();
1116 rcu_thread_online();
1118 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1120 health_code_update();
1123 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1124 * metadata_sock. Nothing more will be added to this poll set.
1126 ret
= sessiond_set_thread_pollset(&events
, 3);
1132 * The error socket here is already in a listening state which was done
1133 * just before spawning this thread to avoid a race between the consumer
1134 * daemon exec trying to connect and the listen() call.
1136 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1141 health_code_update();
1143 /* Infinite blocking call, waiting for transmission */
1145 health_poll_entry();
1147 if (testpoint(sessiond_thread_manage_consumer
)) {
1151 ret
= lttng_poll_wait(&events
, -1);
1155 * Restart interrupted system call.
1157 if (errno
== EINTR
) {
1165 for (i
= 0; i
< nb_fd
; i
++) {
1166 /* Fetch once the poll data */
1167 revents
= LTTNG_POLL_GETEV(&events
, i
);
1168 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1170 health_code_update();
1173 /* No activity for this FD (poll implementation). */
1177 /* Thread quit pipe has been closed. Killing thread. */
1178 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1184 /* Event on the registration socket */
1185 if (pollfd
== consumer_data
->err_sock
) {
1186 if (revents
& LPOLLIN
) {
1188 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1189 ERR("consumer err socket poll error");
1192 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1198 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1204 * Set the CLOEXEC flag. Return code is useless because either way, the
1207 (void) utils_set_fd_cloexec(sock
);
1209 health_code_update();
1211 DBG2("Receiving code from consumer err_sock");
1213 /* Getting status code from kconsumerd */
1214 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1215 sizeof(enum lttcomm_return_code
));
1220 health_code_update();
1221 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1222 ERR("consumer error when waiting for SOCK_READY : %s",
1223 lttcomm_get_readable_code(-code
));
1227 /* Connect both command and metadata sockets. */
1228 consumer_data
->cmd_sock
=
1229 lttcomm_connect_unix_sock(
1230 consumer_data
->cmd_unix_sock_path
);
1231 consumer_data
->metadata_fd
=
1232 lttcomm_connect_unix_sock(
1233 consumer_data
->cmd_unix_sock_path
);
1234 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1235 PERROR("consumer connect cmd socket");
1236 /* On error, signal condition and quit. */
1237 signal_consumer_condition(consumer_data
, -1);
1241 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1243 /* Create metadata socket lock. */
1244 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1245 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1246 PERROR("zmalloc pthread mutex");
1249 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1251 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1252 DBG("Consumer metadata socket ready (fd: %d)",
1253 consumer_data
->metadata_fd
);
1256 * Remove the consumerd error sock since we've established a connection.
1258 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1263 /* Add new accepted error socket. */
1264 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1269 /* Add metadata socket that is successfully connected. */
1270 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1271 LPOLLIN
| LPOLLRDHUP
);
1276 health_code_update();
1279 * Transfer the write-end of the channel monitoring pipe to the
1280 * by issuing a SET_CHANNEL_MONITOR_PIPE command.
1282 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1283 if (!cmd_socket_wrapper
) {
1286 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1288 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1289 consumer_data
->channel_monitor_pipe
);
1293 /* Discard the socket wrapper as it is no longer needed. */
1294 consumer_destroy_socket(cmd_socket_wrapper
);
1295 cmd_socket_wrapper
= NULL
;
1297 /* The thread is completely initialized, signal that it is ready. */
1298 signal_consumer_condition(consumer_data
, 1);
1300 /* Infinite blocking call, waiting for transmission */
1303 health_code_update();
1305 /* Exit the thread because the thread quit pipe has been triggered. */
1307 /* Not a health error. */
1312 health_poll_entry();
1313 ret
= lttng_poll_wait(&events
, -1);
1317 * Restart interrupted system call.
1319 if (errno
== EINTR
) {
1327 for (i
= 0; i
< nb_fd
; i
++) {
1328 /* Fetch once the poll data */
1329 revents
= LTTNG_POLL_GETEV(&events
, i
);
1330 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1332 health_code_update();
1335 /* No activity for this FD (poll implementation). */
1340 * Thread quit pipe has been triggered, flag that we should stop
1341 * but continue the current loop to handle potential data from
1344 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1346 if (pollfd
== sock
) {
1347 /* Event on the consumerd socket */
1348 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1349 && !(revents
& LPOLLIN
)) {
1350 ERR("consumer err socket second poll error");
1353 health_code_update();
1354 /* Wait for any kconsumerd error */
1355 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1356 sizeof(enum lttcomm_return_code
));
1358 ERR("consumer closed the command socket");
1362 ERR("consumer return code : %s",
1363 lttcomm_get_readable_code(-code
));
1366 } else if (pollfd
== consumer_data
->metadata_fd
) {
1367 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1368 && !(revents
& LPOLLIN
)) {
1369 ERR("consumer err metadata socket second poll error");
1372 /* UST metadata requests */
1373 ret
= ust_consumer_metadata_request(
1374 &consumer_data
->metadata_sock
);
1376 ERR("Handling metadata request");
1380 /* No need for an else branch all FDs are tested prior. */
1382 health_code_update();
1388 * We lock here because we are about to close the sockets and some other
1389 * thread might be using them so get exclusive access which will abort all
1390 * other consumer command by other threads.
1392 pthread_mutex_lock(&consumer_data
->lock
);
1394 /* Immediately set the consumerd state to stopped */
1395 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1396 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1397 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1398 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1399 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1401 /* Code flow error... */
1405 if (consumer_data
->err_sock
>= 0) {
1406 ret
= close(consumer_data
->err_sock
);
1410 consumer_data
->err_sock
= -1;
1412 if (consumer_data
->cmd_sock
>= 0) {
1413 ret
= close(consumer_data
->cmd_sock
);
1417 consumer_data
->cmd_sock
= -1;
1419 if (consumer_data
->metadata_sock
.fd_ptr
&&
1420 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1421 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1433 unlink(consumer_data
->err_unix_sock_path
);
1434 unlink(consumer_data
->cmd_unix_sock_path
);
1435 pthread_mutex_unlock(&consumer_data
->lock
);
1437 /* Cleanup metadata socket mutex. */
1438 if (consumer_data
->metadata_sock
.lock
) {
1439 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1440 free(consumer_data
->metadata_sock
.lock
);
1442 lttng_poll_clean(&events
);
1444 if (cmd_socket_wrapper
) {
1445 consumer_destroy_socket(cmd_socket_wrapper
);
1450 ERR("Health error occurred in %s", __func__
);
1452 health_unregister(health_sessiond
);
1453 DBG("consumer thread cleanup completed");
1455 rcu_thread_offline();
1456 rcu_unregister_thread();
1462 * This thread manage application communication.
1464 static void *thread_manage_apps(void *data
)
1466 int i
, ret
, pollfd
, err
= -1;
1468 uint32_t revents
, nb_fd
;
1469 struct lttng_poll_event events
;
1471 DBG("[thread] Manage application started");
1473 rcu_register_thread();
1474 rcu_thread_online();
1476 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1478 if (testpoint(sessiond_thread_manage_apps
)) {
1479 goto error_testpoint
;
1482 health_code_update();
1484 ret
= sessiond_set_thread_pollset(&events
, 2);
1486 goto error_poll_create
;
1489 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1494 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1498 health_code_update();
1501 DBG("Apps thread polling");
1503 /* Inifinite blocking call, waiting for transmission */
1505 health_poll_entry();
1506 ret
= lttng_poll_wait(&events
, -1);
1507 DBG("Apps thread return from poll on %d fds",
1508 LTTNG_POLL_GETNB(&events
));
1512 * Restart interrupted system call.
1514 if (errno
== EINTR
) {
1522 for (i
= 0; i
< nb_fd
; i
++) {
1523 /* Fetch once the poll data */
1524 revents
= LTTNG_POLL_GETEV(&events
, i
);
1525 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1527 health_code_update();
1530 /* No activity for this FD (poll implementation). */
1534 /* Thread quit pipe has been closed. Killing thread. */
1535 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1541 /* Inspect the apps cmd pipe */
1542 if (pollfd
== apps_cmd_pipe
[0]) {
1543 if (revents
& LPOLLIN
) {
1547 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1548 if (size_ret
< sizeof(sock
)) {
1549 PERROR("read apps cmd pipe");
1553 health_code_update();
1556 * Since this is a command socket (write then read),
1557 * we only monitor the error events of the socket.
1559 ret
= lttng_poll_add(&events
, sock
,
1560 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1565 DBG("Apps with sock %d added to poll set", sock
);
1566 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1567 ERR("Apps command pipe error");
1570 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1575 * At this point, we know that a registered application made
1576 * the event at poll_wait.
1578 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1579 /* Removing from the poll set */
1580 ret
= lttng_poll_del(&events
, pollfd
);
1585 /* Socket closed on remote end. */
1586 ust_app_unregister(pollfd
);
1588 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1593 health_code_update();
1599 lttng_poll_clean(&events
);
1602 utils_close_pipe(apps_cmd_pipe
);
1603 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1606 * We don't clean the UST app hash table here since already registered
1607 * applications can still be controlled so let them be until the session
1608 * daemon dies or the applications stop.
1613 ERR("Health error occurred in %s", __func__
);
1615 health_unregister(health_sessiond
);
1616 DBG("Application communication apps thread cleanup complete");
1617 rcu_thread_offline();
1618 rcu_unregister_thread();
1623 * Send a socket to a thread This is called from the dispatch UST registration
1624 * thread once all sockets are set for the application.
1626 * The sock value can be invalid, we don't really care, the thread will handle
1627 * it and make the necessary cleanup if so.
1629 * On success, return 0 else a negative value being the errno message of the
1632 static int send_socket_to_thread(int fd
, int sock
)
1637 * It's possible that the FD is set as invalid with -1 concurrently just
1638 * before calling this function being a shutdown state of the thread.
1645 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1646 if (ret
< sizeof(sock
)) {
1647 PERROR("write apps pipe %d", fd
);
1654 /* All good. Don't send back the write positive ret value. */
1661 * Sanitize the wait queue of the dispatch registration thread meaning removing
1662 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1663 * notify socket is never received.
1665 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1667 int ret
, nb_fd
= 0, i
;
1668 unsigned int fd_added
= 0;
1669 struct lttng_poll_event events
;
1670 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1674 lttng_poll_init(&events
);
1676 /* Just skip everything for an empty queue. */
1677 if (!wait_queue
->count
) {
1681 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1686 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1687 &wait_queue
->head
, head
) {
1688 assert(wait_node
->app
);
1689 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1690 LPOLLHUP
| LPOLLERR
);
1703 * Poll but don't block so we can quickly identify the faulty events and
1704 * clean them afterwards from the wait queue.
1706 ret
= lttng_poll_wait(&events
, 0);
1712 for (i
= 0; i
< nb_fd
; i
++) {
1713 /* Get faulty FD. */
1714 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1715 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1718 /* No activity for this FD (poll implementation). */
1722 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1723 &wait_queue
->head
, head
) {
1724 if (pollfd
== wait_node
->app
->sock
&&
1725 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1726 cds_list_del(&wait_node
->head
);
1727 wait_queue
->count
--;
1728 ust_app_destroy(wait_node
->app
);
1731 * Silence warning of use-after-free in
1732 * cds_list_for_each_entry_safe which uses
1733 * __typeof__(*wait_node).
1738 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1745 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1749 lttng_poll_clean(&events
);
1753 lttng_poll_clean(&events
);
1755 ERR("Unable to sanitize wait queue");
1760 * Dispatch request from the registration threads to the application
1761 * communication thread.
1763 static void *thread_dispatch_ust_registration(void *data
)
1766 struct cds_wfcq_node
*node
;
1767 struct ust_command
*ust_cmd
= NULL
;
1768 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1769 struct ust_reg_wait_queue wait_queue
= {
1773 rcu_register_thread();
1775 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1777 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1778 goto error_testpoint
;
1781 health_code_update();
1783 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1785 DBG("[thread] Dispatch UST command started");
1788 health_code_update();
1790 /* Atomically prepare the queue futex */
1791 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1793 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1798 struct ust_app
*app
= NULL
;
1802 * Make sure we don't have node(s) that have hung up before receiving
1803 * the notify socket. This is to clean the list in order to avoid
1804 * memory leaks from notify socket that are never seen.
1806 sanitize_wait_queue(&wait_queue
);
1808 health_code_update();
1809 /* Dequeue command for registration */
1810 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1812 DBG("Woken up but nothing in the UST command queue");
1813 /* Continue thread execution */
1817 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1819 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1820 " gid:%d sock:%d name:%s (version %d.%d)",
1821 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1822 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1823 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1824 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1826 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1827 wait_node
= zmalloc(sizeof(*wait_node
));
1829 PERROR("zmalloc wait_node dispatch");
1830 ret
= close(ust_cmd
->sock
);
1832 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1834 lttng_fd_put(LTTNG_FD_APPS
, 1);
1838 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1840 /* Create application object if socket is CMD. */
1841 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1843 if (!wait_node
->app
) {
1844 ret
= close(ust_cmd
->sock
);
1846 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1848 lttng_fd_put(LTTNG_FD_APPS
, 1);
1854 * Add application to the wait queue so we can set the notify
1855 * socket before putting this object in the global ht.
1857 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1862 * We have to continue here since we don't have the notify
1863 * socket and the application MUST be added to the hash table
1864 * only at that moment.
1869 * Look for the application in the local wait queue and set the
1870 * notify socket if found.
1872 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1873 &wait_queue
.head
, head
) {
1874 health_code_update();
1875 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1876 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1877 cds_list_del(&wait_node
->head
);
1879 app
= wait_node
->app
;
1881 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1887 * With no application at this stage the received socket is
1888 * basically useless so close it before we free the cmd data
1889 * structure for good.
1892 ret
= close(ust_cmd
->sock
);
1894 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1896 lttng_fd_put(LTTNG_FD_APPS
, 1);
1903 * @session_lock_list
1905 * Lock the global session list so from the register up to the
1906 * registration done message, no thread can see the application
1907 * and change its state.
1909 session_lock_list();
1913 * Add application to the global hash table. This needs to be
1914 * done before the update to the UST registry can locate the
1919 /* Set app version. This call will print an error if needed. */
1920 (void) ust_app_version(app
);
1922 /* Send notify socket through the notify pipe. */
1923 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1927 session_unlock_list();
1929 * No notify thread, stop the UST tracing. However, this is
1930 * not an internal error of the this thread thus setting
1931 * the health error code to a normal exit.
1938 * Update newly registered application with the tracing
1939 * registry info already enabled information.
1941 update_ust_app(app
->sock
);
1944 * Don't care about return value. Let the manage apps threads
1945 * handle app unregistration upon socket close.
1947 (void) ust_app_register_done(app
);
1950 * Even if the application socket has been closed, send the app
1951 * to the thread and unregistration will take place at that
1954 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1957 session_unlock_list();
1959 * No apps. thread, stop the UST tracing. However, this is
1960 * not an internal error of the this thread thus setting
1961 * the health error code to a normal exit.
1968 session_unlock_list();
1970 } while (node
!= NULL
);
1972 health_poll_entry();
1973 /* Futex wait on queue. Blocking call on futex() */
1974 futex_nto1_wait(&ust_cmd_queue
.futex
);
1977 /* Normal exit, no error */
1981 /* Clean up wait queue. */
1982 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1983 &wait_queue
.head
, head
) {
1984 cds_list_del(&wait_node
->head
);
1989 /* Empty command queue. */
1991 /* Dequeue command for registration */
1992 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1996 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1997 ret
= close(ust_cmd
->sock
);
1999 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2001 lttng_fd_put(LTTNG_FD_APPS
, 1);
2006 DBG("Dispatch thread dying");
2009 ERR("Health error occurred in %s", __func__
);
2011 health_unregister(health_sessiond
);
2012 rcu_unregister_thread();
2017 * This thread manage application registration.
2019 static void *thread_registration_apps(void *data
)
2021 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2022 uint32_t revents
, nb_fd
;
2023 struct lttng_poll_event events
;
2025 * Get allocated in this thread, enqueued to a global queue, dequeued and
2026 * freed in the manage apps thread.
2028 struct ust_command
*ust_cmd
= NULL
;
2030 DBG("[thread] Manage application registration started");
2032 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2034 if (testpoint(sessiond_thread_registration_apps
)) {
2035 goto error_testpoint
;
2038 ret
= lttcomm_listen_unix_sock(apps_sock
);
2044 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2045 * more will be added to this poll set.
2047 ret
= sessiond_set_thread_pollset(&events
, 2);
2049 goto error_create_poll
;
2052 /* Add the application registration socket */
2053 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2055 goto error_poll_add
;
2058 /* Notify all applications to register */
2059 ret
= notify_ust_apps(1);
2061 ERR("Failed to notify applications or create the wait shared memory.\n"
2062 "Execution continues but there might be problem for already\n"
2063 "running applications that wishes to register.");
2067 DBG("Accepting application registration");
2069 /* Inifinite blocking call, waiting for transmission */
2071 health_poll_entry();
2072 ret
= lttng_poll_wait(&events
, -1);
2076 * Restart interrupted system call.
2078 if (errno
== EINTR
) {
2086 for (i
= 0; i
< nb_fd
; i
++) {
2087 health_code_update();
2089 /* Fetch once the poll data */
2090 revents
= LTTNG_POLL_GETEV(&events
, i
);
2091 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2094 /* No activity for this FD (poll implementation). */
2098 /* Thread quit pipe has been closed. Killing thread. */
2099 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2105 /* Event on the registration socket */
2106 if (pollfd
== apps_sock
) {
2107 if (revents
& LPOLLIN
) {
2108 sock
= lttcomm_accept_unix_sock(apps_sock
);
2114 * Set socket timeout for both receiving and ending.
2115 * app_socket_timeout is in seconds, whereas
2116 * lttcomm_setsockopt_rcv_timeout and
2117 * lttcomm_setsockopt_snd_timeout expect msec as
2120 if (config
.app_socket_timeout
>= 0) {
2121 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2122 config
.app_socket_timeout
* 1000);
2123 (void) lttcomm_setsockopt_snd_timeout(sock
,
2124 config
.app_socket_timeout
* 1000);
2128 * Set the CLOEXEC flag. Return code is useless because
2129 * either way, the show must go on.
2131 (void) utils_set_fd_cloexec(sock
);
2133 /* Create UST registration command for enqueuing */
2134 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2135 if (ust_cmd
== NULL
) {
2136 PERROR("ust command zmalloc");
2145 * Using message-based transmissions to ensure we don't
2146 * have to deal with partially received messages.
2148 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2150 ERR("Exhausted file descriptors allowed for applications.");
2160 health_code_update();
2161 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2164 /* Close socket of the application. */
2169 lttng_fd_put(LTTNG_FD_APPS
, 1);
2173 health_code_update();
2175 ust_cmd
->sock
= sock
;
2178 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2179 " gid:%d sock:%d name:%s (version %d.%d)",
2180 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2181 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2182 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2183 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2186 * Lock free enqueue the registration request. The red pill
2187 * has been taken! This apps will be part of the *system*.
2189 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2192 * Wake the registration queue futex. Implicit memory
2193 * barrier with the exchange in cds_wfcq_enqueue.
2195 futex_nto1_wake(&ust_cmd_queue
.futex
);
2196 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2197 ERR("Register apps socket poll error");
2200 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2209 /* Notify that the registration thread is gone */
2212 if (apps_sock
>= 0) {
2213 ret
= close(apps_sock
);
2223 lttng_fd_put(LTTNG_FD_APPS
, 1);
2225 unlink(config
.apps_unix_sock_path
.value
);
2228 lttng_poll_clean(&events
);
2232 DBG("UST Registration thread cleanup complete");
2235 ERR("Health error occurred in %s", __func__
);
2237 health_unregister(health_sessiond
);
2243 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2244 * exec or it will fails.
2246 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2249 struct timespec timeout
;
2252 * Make sure we set the readiness flag to 0 because we are NOT ready.
2253 * This access to consumer_thread_is_ready does not need to be
2254 * protected by consumer_data.cond_mutex (yet) since the consumer
2255 * management thread has not been started at this point.
2257 consumer_data
->consumer_thread_is_ready
= 0;
2259 /* Setup pthread condition */
2260 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2263 PERROR("pthread_condattr_init consumer data");
2268 * Set the monotonic clock in order to make sure we DO NOT jump in time
2269 * between the clock_gettime() call and the timedwait call. See bug #324
2270 * for a more details and how we noticed it.
2272 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2275 PERROR("pthread_condattr_setclock consumer data");
2279 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2282 PERROR("pthread_cond_init consumer data");
2286 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2287 thread_manage_consumer
, consumer_data
);
2290 PERROR("pthread_create consumer");
2295 /* We are about to wait on a pthread condition */
2296 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2298 /* Get time for sem_timedwait absolute timeout */
2299 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2301 * Set the timeout for the condition timed wait even if the clock gettime
2302 * call fails since we might loop on that call and we want to avoid to
2303 * increment the timeout too many times.
2305 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2308 * The following loop COULD be skipped in some conditions so this is why we
2309 * set ret to 0 in order to make sure at least one round of the loop is
2315 * Loop until the condition is reached or when a timeout is reached. Note
2316 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2317 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2318 * possible. This loop does not take any chances and works with both of
2321 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2322 if (clock_ret
< 0) {
2323 PERROR("clock_gettime spawn consumer");
2324 /* Infinite wait for the consumerd thread to be ready */
2325 ret
= pthread_cond_wait(&consumer_data
->cond
,
2326 &consumer_data
->cond_mutex
);
2328 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2329 &consumer_data
->cond_mutex
, &timeout
);
2333 /* Release the pthread condition */
2334 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2338 if (ret
== ETIMEDOUT
) {
2342 * Call has timed out so we kill the kconsumerd_thread and return
2345 ERR("Condition timed out. The consumer thread was never ready."
2347 pth_ret
= pthread_cancel(consumer_data
->thread
);
2349 PERROR("pthread_cancel consumer thread");
2352 PERROR("pthread_cond_wait failed consumer thread");
2354 /* Caller is expecting a negative value on failure. */
2359 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2360 if (consumer_data
->pid
== 0) {
2361 ERR("Consumerd did not start");
2362 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2365 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2374 * Join consumer thread
2376 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2380 /* Consumer pid must be a real one. */
2381 if (consumer_data
->pid
> 0) {
2383 ret
= kill(consumer_data
->pid
, SIGTERM
);
2385 PERROR("Error killing consumer daemon");
2388 return pthread_join(consumer_data
->thread
, &status
);
2395 * Fork and exec a consumer daemon (consumerd).
2397 * Return pid if successful else -1.
2399 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2403 const char *consumer_to_use
;
2404 const char *verbosity
;
2407 DBG("Spawning consumerd");
2414 if (config
.verbose_consumer
) {
2415 verbosity
= "--verbose";
2416 } else if (lttng_opt_quiet
) {
2417 verbosity
= "--quiet";
2422 switch (consumer_data
->type
) {
2423 case LTTNG_CONSUMER_KERNEL
:
2425 * Find out which consumerd to execute. We will first try the
2426 * 64-bit path, then the sessiond's installation directory, and
2427 * fallback on the 32-bit one,
2429 DBG3("Looking for a kernel consumer at these locations:");
2430 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2431 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2432 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2433 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2434 DBG3("Found location #1");
2435 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2436 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2437 DBG3("Found location #2");
2438 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2439 } else if (config
.consumerd32_bin_path
.value
&&
2440 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2441 DBG3("Found location #3");
2442 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2444 DBG("Could not find any valid consumerd executable");
2448 DBG("Using kernel consumer at: %s", consumer_to_use
);
2449 (void) execl(consumer_to_use
,
2450 "lttng-consumerd", verbosity
, "-k",
2451 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2452 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2453 "--group", config
.tracing_group_name
.value
,
2456 case LTTNG_CONSUMER64_UST
:
2458 char *tmpnew
= NULL
;
2460 if (config
.consumerd64_lib_dir
.value
) {
2464 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2468 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2469 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2474 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2475 if (tmp
[0] != '\0') {
2476 strcat(tmpnew
, ":");
2477 strcat(tmpnew
, tmp
);
2479 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2486 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2487 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2488 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2489 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2490 "--group", config
.tracing_group_name
.value
,
2495 case LTTNG_CONSUMER32_UST
:
2497 char *tmpnew
= NULL
;
2499 if (config
.consumerd32_lib_dir
.value
) {
2503 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2507 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2508 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2513 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2514 if (tmp
[0] != '\0') {
2515 strcat(tmpnew
, ":");
2516 strcat(tmpnew
, tmp
);
2518 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2525 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2526 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2527 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2528 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2529 "--group", config
.tracing_group_name
.value
,
2535 ERR("unknown consumer type");
2539 PERROR("Consumer execl()");
2541 /* Reaching this point, we got a failure on our execl(). */
2543 } else if (pid
> 0) {
2546 PERROR("start consumer fork");
2554 * Spawn the consumerd daemon and session daemon thread.
2556 static int start_consumerd(struct consumer_data
*consumer_data
)
2561 * Set the listen() state on the socket since there is a possible race
2562 * between the exec() of the consumer daemon and this call if place in the
2563 * consumer thread. See bug #366 for more details.
2565 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2570 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2571 if (consumer_data
->pid
!= 0) {
2572 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2576 ret
= spawn_consumerd(consumer_data
);
2578 ERR("Spawning consumerd failed");
2579 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2583 /* Setting up the consumer_data pid */
2584 consumer_data
->pid
= ret
;
2585 DBG2("Consumer pid %d", consumer_data
->pid
);
2586 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2588 DBG2("Spawning consumer control thread");
2589 ret
= spawn_consumer_thread(consumer_data
);
2591 ERR("Fatal error spawning consumer control thread");
2599 /* Cleanup already created sockets on error. */
2600 if (consumer_data
->err_sock
>= 0) {
2603 err
= close(consumer_data
->err_sock
);
2605 PERROR("close consumer data error socket");
2612 * Setup necessary data for kernel tracer action.
2614 static int init_kernel_tracer(void)
2618 /* Modprobe lttng kernel modules */
2619 ret
= modprobe_lttng_control();
2624 /* Open debugfs lttng */
2625 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2626 if (kernel_tracer_fd
< 0) {
2627 DBG("Failed to open %s", module_proc_lttng
);
2631 /* Validate kernel version */
2632 ret
= kernel_validate_version(kernel_tracer_fd
);
2637 ret
= modprobe_lttng_data();
2642 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2649 WARN("Kernel tracer does not support buffer monitoring. "
2650 "The monitoring timer of channels in the kernel domain "
2651 "will be set to 0 (disabled).");
2654 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2656 ret
= syscall_init_table();
2658 ERR("Unable to populate syscall table. Syscall tracing won't "
2659 "work for this session daemon.");
2664 modprobe_remove_lttng_control();
2665 ret
= close(kernel_tracer_fd
);
2669 kernel_tracer_fd
= -1;
2670 return LTTNG_ERR_KERN_VERSION
;
2673 ret
= close(kernel_tracer_fd
);
2679 modprobe_remove_lttng_control();
2682 WARN("No kernel tracer available");
2683 kernel_tracer_fd
= -1;
2685 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2687 return LTTNG_ERR_KERN_NA
;
2693 * Copy consumer output from the tracing session to the domain session. The
2694 * function also applies the right modification on a per domain basis for the
2695 * trace files destination directory.
2697 * Should *NOT* be called with RCU read-side lock held.
2699 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2702 const char *dir_name
;
2703 struct consumer_output
*consumer
;
2706 assert(session
->consumer
);
2709 case LTTNG_DOMAIN_KERNEL
:
2710 DBG3("Copying tracing session consumer output in kernel session");
2712 * XXX: We should audit the session creation and what this function
2713 * does "extra" in order to avoid a destroy since this function is used
2714 * in the domain session creation (kernel and ust) only. Same for UST
2717 if (session
->kernel_session
->consumer
) {
2718 consumer_output_put(session
->kernel_session
->consumer
);
2720 session
->kernel_session
->consumer
=
2721 consumer_copy_output(session
->consumer
);
2722 /* Ease our life a bit for the next part */
2723 consumer
= session
->kernel_session
->consumer
;
2724 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2726 case LTTNG_DOMAIN_JUL
:
2727 case LTTNG_DOMAIN_LOG4J
:
2728 case LTTNG_DOMAIN_PYTHON
:
2729 case LTTNG_DOMAIN_UST
:
2730 DBG3("Copying tracing session consumer output in UST session");
2731 if (session
->ust_session
->consumer
) {
2732 consumer_output_put(session
->ust_session
->consumer
);
2734 session
->ust_session
->consumer
=
2735 consumer_copy_output(session
->consumer
);
2736 /* Ease our life a bit for the next part */
2737 consumer
= session
->ust_session
->consumer
;
2738 dir_name
= DEFAULT_UST_TRACE_DIR
;
2741 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2745 /* Append correct directory to subdir */
2746 strncat(consumer
->subdir
, dir_name
,
2747 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2748 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2757 * Create an UST session and add it to the session ust list.
2759 * Should *NOT* be called with RCU read-side lock held.
2761 static int create_ust_session(struct ltt_session
*session
,
2762 struct lttng_domain
*domain
)
2765 struct ltt_ust_session
*lus
= NULL
;
2769 assert(session
->consumer
);
2771 switch (domain
->type
) {
2772 case LTTNG_DOMAIN_JUL
:
2773 case LTTNG_DOMAIN_LOG4J
:
2774 case LTTNG_DOMAIN_PYTHON
:
2775 case LTTNG_DOMAIN_UST
:
2778 ERR("Unknown UST domain on create session %d", domain
->type
);
2779 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2783 DBG("Creating UST session");
2785 lus
= trace_ust_create_session(session
->id
);
2787 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2791 lus
->uid
= session
->uid
;
2792 lus
->gid
= session
->gid
;
2793 lus
->output_traces
= session
->output_traces
;
2794 lus
->snapshot_mode
= session
->snapshot_mode
;
2795 lus
->live_timer_interval
= session
->live_timer
;
2796 session
->ust_session
= lus
;
2797 if (session
->shm_path
[0]) {
2798 strncpy(lus
->root_shm_path
, session
->shm_path
,
2799 sizeof(lus
->root_shm_path
));
2800 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2801 strncpy(lus
->shm_path
, session
->shm_path
,
2802 sizeof(lus
->shm_path
));
2803 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2804 strncat(lus
->shm_path
, "/ust",
2805 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2807 /* Copy session output to the newly created UST session */
2808 ret
= copy_session_consumer(domain
->type
, session
);
2809 if (ret
!= LTTNG_OK
) {
2817 session
->ust_session
= NULL
;
2822 * Create a kernel tracer session then create the default channel.
2824 static int create_kernel_session(struct ltt_session
*session
)
2828 DBG("Creating kernel session");
2830 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2832 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2836 /* Code flow safety */
2837 assert(session
->kernel_session
);
2839 /* Copy session output to the newly created Kernel session */
2840 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2841 if (ret
!= LTTNG_OK
) {
2845 /* Create directory(ies) on local filesystem. */
2846 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2847 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2848 ret
= run_as_mkdir_recursive(
2849 session
->kernel_session
->consumer
->dst
.trace_path
,
2850 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2852 if (errno
!= EEXIST
) {
2853 ERR("Trace directory creation error");
2859 session
->kernel_session
->uid
= session
->uid
;
2860 session
->kernel_session
->gid
= session
->gid
;
2861 session
->kernel_session
->output_traces
= session
->output_traces
;
2862 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2867 trace_kernel_destroy_session(session
->kernel_session
);
2868 session
->kernel_session
= NULL
;
2873 * Count number of session permitted by uid/gid.
2875 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2878 struct ltt_session
*session
;
2880 DBG("Counting number of available session for UID %d GID %d",
2882 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2884 * Only list the sessions the user can control.
2886 if (!session_access_ok(session
, uid
, gid
)) {
2895 * Process the command requested by the lttng client within the command
2896 * context structure. This function make sure that the return structure (llm)
2897 * is set and ready for transmission before returning.
2899 * Return any error encountered or 0 for success.
2901 * "sock" is only used for special-case var. len data.
2903 * Should *NOT* be called with RCU read-side lock held.
2905 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2909 int need_tracing_session
= 1;
2912 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2914 assert(!rcu_read_ongoing());
2918 switch (cmd_ctx
->lsm
->cmd_type
) {
2919 case LTTNG_CREATE_SESSION
:
2920 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2921 case LTTNG_CREATE_SESSION_LIVE
:
2922 case LTTNG_DESTROY_SESSION
:
2923 case LTTNG_LIST_SESSIONS
:
2924 case LTTNG_LIST_DOMAINS
:
2925 case LTTNG_START_TRACE
:
2926 case LTTNG_STOP_TRACE
:
2927 case LTTNG_DATA_PENDING
:
2928 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2929 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2930 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2931 case LTTNG_SNAPSHOT_RECORD
:
2932 case LTTNG_SAVE_SESSION
:
2933 case LTTNG_SET_SESSION_SHM_PATH
:
2934 case LTTNG_REGENERATE_METADATA
:
2935 case LTTNG_REGENERATE_STATEDUMP
:
2936 case LTTNG_REGISTER_TRIGGER
:
2937 case LTTNG_UNREGISTER_TRIGGER
:
2944 if (config
.no_kernel
&& need_domain
2945 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2947 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2949 ret
= LTTNG_ERR_KERN_NA
;
2954 /* Deny register consumer if we already have a spawned consumer. */
2955 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2956 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2957 if (kconsumer_data
.pid
> 0) {
2958 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2959 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2962 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2966 * Check for command that don't needs to allocate a returned payload. We do
2967 * this here so we don't have to make the call for no payload at each
2970 switch(cmd_ctx
->lsm
->cmd_type
) {
2971 case LTTNG_LIST_SESSIONS
:
2972 case LTTNG_LIST_TRACEPOINTS
:
2973 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2974 case LTTNG_LIST_DOMAINS
:
2975 case LTTNG_LIST_CHANNELS
:
2976 case LTTNG_LIST_EVENTS
:
2977 case LTTNG_LIST_SYSCALLS
:
2978 case LTTNG_LIST_TRACKER_PIDS
:
2979 case LTTNG_DATA_PENDING
:
2982 /* Setup lttng message with no payload */
2983 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2985 /* This label does not try to unlock the session */
2986 goto init_setup_error
;
2990 /* Commands that DO NOT need a session. */
2991 switch (cmd_ctx
->lsm
->cmd_type
) {
2992 case LTTNG_CREATE_SESSION
:
2993 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2994 case LTTNG_CREATE_SESSION_LIVE
:
2995 case LTTNG_LIST_SESSIONS
:
2996 case LTTNG_LIST_TRACEPOINTS
:
2997 case LTTNG_LIST_SYSCALLS
:
2998 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2999 case LTTNG_SAVE_SESSION
:
3000 case LTTNG_REGISTER_TRIGGER
:
3001 case LTTNG_UNREGISTER_TRIGGER
:
3002 need_tracing_session
= 0;
3005 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3007 * We keep the session list lock across _all_ commands
3008 * for now, because the per-session lock does not
3009 * handle teardown properly.
3011 session_lock_list();
3012 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3013 if (cmd_ctx
->session
== NULL
) {
3014 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3017 /* Acquire lock for the session */
3018 session_lock(cmd_ctx
->session
);
3024 * Commands that need a valid session but should NOT create one if none
3025 * exists. Instead of creating one and destroying it when the command is
3026 * handled, process that right before so we save some round trip in useless
3029 switch (cmd_ctx
->lsm
->cmd_type
) {
3030 case LTTNG_DISABLE_CHANNEL
:
3031 case LTTNG_DISABLE_EVENT
:
3032 switch (cmd_ctx
->lsm
->domain
.type
) {
3033 case LTTNG_DOMAIN_KERNEL
:
3034 if (!cmd_ctx
->session
->kernel_session
) {
3035 ret
= LTTNG_ERR_NO_CHANNEL
;
3039 case LTTNG_DOMAIN_JUL
:
3040 case LTTNG_DOMAIN_LOG4J
:
3041 case LTTNG_DOMAIN_PYTHON
:
3042 case LTTNG_DOMAIN_UST
:
3043 if (!cmd_ctx
->session
->ust_session
) {
3044 ret
= LTTNG_ERR_NO_CHANNEL
;
3049 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3061 * Check domain type for specific "pre-action".
3063 switch (cmd_ctx
->lsm
->domain
.type
) {
3064 case LTTNG_DOMAIN_KERNEL
:
3066 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3070 /* Kernel tracer check */
3071 if (kernel_tracer_fd
== -1) {
3072 /* Basically, load kernel tracer modules */
3073 ret
= init_kernel_tracer();
3079 /* Consumer is in an ERROR state. Report back to client */
3080 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3081 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3085 /* Need a session for kernel command */
3086 if (need_tracing_session
) {
3087 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3088 ret
= create_kernel_session(cmd_ctx
->session
);
3090 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3095 /* Start the kernel consumer daemon */
3096 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3097 if (kconsumer_data
.pid
== 0 &&
3098 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3099 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3100 ret
= start_consumerd(&kconsumer_data
);
3102 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3105 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3107 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3111 * The consumer was just spawned so we need to add the socket to
3112 * the consumer output of the session if exist.
3114 ret
= consumer_create_socket(&kconsumer_data
,
3115 cmd_ctx
->session
->kernel_session
->consumer
);
3122 case LTTNG_DOMAIN_JUL
:
3123 case LTTNG_DOMAIN_LOG4J
:
3124 case LTTNG_DOMAIN_PYTHON
:
3125 case LTTNG_DOMAIN_UST
:
3127 if (!ust_app_supported()) {
3128 ret
= LTTNG_ERR_NO_UST
;
3131 /* Consumer is in an ERROR state. Report back to client */
3132 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3133 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3137 if (need_tracing_session
) {
3138 /* Create UST session if none exist. */
3139 if (cmd_ctx
->session
->ust_session
== NULL
) {
3140 ret
= create_ust_session(cmd_ctx
->session
,
3141 &cmd_ctx
->lsm
->domain
);
3142 if (ret
!= LTTNG_OK
) {
3147 /* Start the UST consumer daemons */
3149 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3150 if (config
.consumerd64_bin_path
.value
&&
3151 ustconsumer64_data
.pid
== 0 &&
3152 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3153 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3154 ret
= start_consumerd(&ustconsumer64_data
);
3156 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3157 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3161 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3162 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3164 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3168 * Setup socket for consumer 64 bit. No need for atomic access
3169 * since it was set above and can ONLY be set in this thread.
3171 ret
= consumer_create_socket(&ustconsumer64_data
,
3172 cmd_ctx
->session
->ust_session
->consumer
);
3178 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3179 if (config
.consumerd32_bin_path
.value
&&
3180 ustconsumer32_data
.pid
== 0 &&
3181 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3182 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3183 ret
= start_consumerd(&ustconsumer32_data
);
3185 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3186 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3190 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3191 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3193 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3197 * Setup socket for consumer 64 bit. No need for atomic access
3198 * since it was set above and can ONLY be set in this thread.
3200 ret
= consumer_create_socket(&ustconsumer32_data
,
3201 cmd_ctx
->session
->ust_session
->consumer
);
3213 /* Validate consumer daemon state when start/stop trace command */
3214 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3215 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3216 switch (cmd_ctx
->lsm
->domain
.type
) {
3217 case LTTNG_DOMAIN_NONE
:
3219 case LTTNG_DOMAIN_JUL
:
3220 case LTTNG_DOMAIN_LOG4J
:
3221 case LTTNG_DOMAIN_PYTHON
:
3222 case LTTNG_DOMAIN_UST
:
3223 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3224 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3228 case LTTNG_DOMAIN_KERNEL
:
3229 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3230 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3235 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3241 * Check that the UID or GID match that of the tracing session.
3242 * The root user can interact with all sessions.
3244 if (need_tracing_session
) {
3245 if (!session_access_ok(cmd_ctx
->session
,
3246 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3247 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3248 ret
= LTTNG_ERR_EPERM
;
3254 * Send relayd information to consumer as soon as we have a domain and a
3257 if (cmd_ctx
->session
&& need_domain
) {
3259 * Setup relayd if not done yet. If the relayd information was already
3260 * sent to the consumer, this call will gracefully return.
3262 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3263 if (ret
!= LTTNG_OK
) {
3268 /* Process by command type */
3269 switch (cmd_ctx
->lsm
->cmd_type
) {
3270 case LTTNG_ADD_CONTEXT
:
3273 * An LTTNG_ADD_CONTEXT command might have a supplementary
3274 * payload if the context being added is an application context.
3276 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3277 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3278 char *provider_name
= NULL
, *context_name
= NULL
;
3279 size_t provider_name_len
=
3280 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3281 size_t context_name_len
=
3282 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3284 if (provider_name_len
== 0 || context_name_len
== 0) {
3286 * Application provider and context names MUST
3289 ret
= -LTTNG_ERR_INVALID
;
3293 provider_name
= zmalloc(provider_name_len
+ 1);
3294 if (!provider_name
) {
3295 ret
= -LTTNG_ERR_NOMEM
;
3298 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3301 context_name
= zmalloc(context_name_len
+ 1);
3302 if (!context_name
) {
3303 ret
= -LTTNG_ERR_NOMEM
;
3304 goto error_add_context
;
3306 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3309 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3312 goto error_add_context
;
3315 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3318 goto error_add_context
;
3323 * cmd_add_context assumes ownership of the provider and context
3326 ret
= cmd_add_context(cmd_ctx
->session
,
3327 cmd_ctx
->lsm
->domain
.type
,
3328 cmd_ctx
->lsm
->u
.context
.channel_name
,
3329 &cmd_ctx
->lsm
->u
.context
.ctx
,
3330 kernel_poll_pipe
[1]);
3332 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3333 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3335 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3336 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3342 case LTTNG_DISABLE_CHANNEL
:
3344 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3345 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3348 case LTTNG_DISABLE_EVENT
:
3352 * FIXME: handle filter; for now we just receive the filter's
3353 * bytecode along with the filter expression which are sent by
3354 * liblttng-ctl and discard them.
3356 * This fixes an issue where the client may block while sending
3357 * the filter payload and encounter an error because the session
3358 * daemon closes the socket without ever handling this data.
3360 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3361 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3364 char data
[LTTNG_FILTER_MAX_LEN
];
3366 DBG("Discarding disable event command payload of size %zu", count
);
3368 ret
= lttcomm_recv_unix_sock(sock
, data
,
3369 count
> sizeof(data
) ? sizeof(data
) : count
);
3374 count
-= (size_t) ret
;
3377 /* FIXME: passing packed structure to non-packed pointer */
3378 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3379 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3380 &cmd_ctx
->lsm
->u
.disable
.event
);
3383 case LTTNG_ENABLE_CHANNEL
:
3385 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3386 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3387 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3388 &cmd_ctx
->lsm
->u
.channel
.chan
,
3389 kernel_poll_pipe
[1]);
3392 case LTTNG_TRACK_PID
:
3394 ret
= cmd_track_pid(cmd_ctx
->session
,
3395 cmd_ctx
->lsm
->domain
.type
,
3396 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3399 case LTTNG_UNTRACK_PID
:
3401 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3402 cmd_ctx
->lsm
->domain
.type
,
3403 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3406 case LTTNG_ENABLE_EVENT
:
3408 struct lttng_event_exclusion
*exclusion
= NULL
;
3409 struct lttng_filter_bytecode
*bytecode
= NULL
;
3410 char *filter_expression
= NULL
;
3412 /* Handle exclusion events and receive it from the client. */
3413 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3414 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3416 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3417 (count
* LTTNG_SYMBOL_NAME_LEN
));
3419 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3423 DBG("Receiving var len exclusion event list from client ...");
3424 exclusion
->count
= count
;
3425 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3426 count
* LTTNG_SYMBOL_NAME_LEN
);
3428 DBG("Nothing recv() from client var len data... continuing");
3431 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3436 /* Get filter expression from client. */
3437 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3438 size_t expression_len
=
3439 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3441 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3442 ret
= LTTNG_ERR_FILTER_INVAL
;
3447 filter_expression
= zmalloc(expression_len
);
3448 if (!filter_expression
) {
3450 ret
= LTTNG_ERR_FILTER_NOMEM
;
3454 /* Receive var. len. data */
3455 DBG("Receiving var len filter's expression from client ...");
3456 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3459 DBG("Nothing recv() from client car len data... continuing");
3461 free(filter_expression
);
3463 ret
= LTTNG_ERR_FILTER_INVAL
;
3468 /* Handle filter and get bytecode from client. */
3469 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3470 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3472 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3473 ret
= LTTNG_ERR_FILTER_INVAL
;
3474 free(filter_expression
);
3479 bytecode
= zmalloc(bytecode_len
);
3481 free(filter_expression
);
3483 ret
= LTTNG_ERR_FILTER_NOMEM
;
3487 /* Receive var. len. data */
3488 DBG("Receiving var len filter's bytecode from client ...");
3489 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3491 DBG("Nothing recv() from client car len data... continuing");
3493 free(filter_expression
);
3496 ret
= LTTNG_ERR_FILTER_INVAL
;
3500 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3501 free(filter_expression
);
3504 ret
= LTTNG_ERR_FILTER_INVAL
;
3509 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3510 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3511 &cmd_ctx
->lsm
->u
.enable
.event
,
3512 filter_expression
, bytecode
, exclusion
,
3513 kernel_poll_pipe
[1]);
3516 case LTTNG_LIST_TRACEPOINTS
:
3518 struct lttng_event
*events
;
3521 session_lock_list();
3522 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3523 session_unlock_list();
3524 if (nb_events
< 0) {
3525 /* Return value is a negative lttng_error_code. */
3531 * Setup lttng message with payload size set to the event list size in
3532 * bytes and then copy list into the llm payload.
3534 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3535 sizeof(struct lttng_event
) * nb_events
);
3545 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3547 struct lttng_event_field
*fields
;
3550 session_lock_list();
3551 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3553 session_unlock_list();
3554 if (nb_fields
< 0) {
3555 /* Return value is a negative lttng_error_code. */
3561 * Setup lttng message with payload size set to the event list size in
3562 * bytes and then copy list into the llm payload.
3564 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3565 sizeof(struct lttng_event_field
) * nb_fields
);
3575 case LTTNG_LIST_SYSCALLS
:
3577 struct lttng_event
*events
;
3580 nb_events
= cmd_list_syscalls(&events
);
3581 if (nb_events
< 0) {
3582 /* Return value is a negative lttng_error_code. */
3588 * Setup lttng message with payload size set to the event list size in
3589 * bytes and then copy list into the llm payload.
3591 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3592 sizeof(struct lttng_event
) * nb_events
);
3602 case LTTNG_LIST_TRACKER_PIDS
:
3604 int32_t *pids
= NULL
;
3607 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3608 cmd_ctx
->lsm
->domain
.type
, &pids
);
3610 /* Return value is a negative lttng_error_code. */
3616 * Setup lttng message with payload size set to the event list size in
3617 * bytes and then copy list into the llm payload.
3619 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3620 sizeof(int32_t) * nr_pids
);
3630 case LTTNG_SET_CONSUMER_URI
:
3633 struct lttng_uri
*uris
;
3635 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3636 len
= nb_uri
* sizeof(struct lttng_uri
);
3639 ret
= LTTNG_ERR_INVALID
;
3643 uris
= zmalloc(len
);
3645 ret
= LTTNG_ERR_FATAL
;
3649 /* Receive variable len data */
3650 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3651 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3653 DBG("No URIs received from client... continuing");
3655 ret
= LTTNG_ERR_SESSION_FAIL
;
3660 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3662 if (ret
!= LTTNG_OK
) {
3669 case LTTNG_START_TRACE
:
3671 ret
= cmd_start_trace(cmd_ctx
->session
);
3674 case LTTNG_STOP_TRACE
:
3676 ret
= cmd_stop_trace(cmd_ctx
->session
);
3679 case LTTNG_CREATE_SESSION
:
3682 struct lttng_uri
*uris
= NULL
;
3684 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3685 len
= nb_uri
* sizeof(struct lttng_uri
);
3688 uris
= zmalloc(len
);
3690 ret
= LTTNG_ERR_FATAL
;
3694 /* Receive variable len data */
3695 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3696 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3698 DBG("No URIs received from client... continuing");
3700 ret
= LTTNG_ERR_SESSION_FAIL
;
3705 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3706 DBG("Creating session with ONE network URI is a bad call");
3707 ret
= LTTNG_ERR_SESSION_FAIL
;
3713 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3714 &cmd_ctx
->creds
, 0);
3720 case LTTNG_DESTROY_SESSION
:
3722 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3724 /* Set session to NULL so we do not unlock it after free. */
3725 cmd_ctx
->session
= NULL
;
3728 case LTTNG_LIST_DOMAINS
:
3731 struct lttng_domain
*domains
= NULL
;
3733 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3735 /* Return value is a negative lttng_error_code. */
3740 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3741 nb_dom
* sizeof(struct lttng_domain
));
3751 case LTTNG_LIST_CHANNELS
:
3753 ssize_t payload_size
;
3754 struct lttng_channel
*channels
= NULL
;
3756 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3757 cmd_ctx
->session
, &channels
);
3758 if (payload_size
< 0) {
3759 /* Return value is a negative lttng_error_code. */
3760 ret
= -payload_size
;
3764 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3775 case LTTNG_LIST_EVENTS
:
3778 struct lttng_event
*events
= NULL
;
3779 struct lttcomm_event_command_header cmd_header
;
3782 memset(&cmd_header
, 0, sizeof(cmd_header
));
3783 /* Extended infos are included at the end of events */
3784 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3785 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3786 &events
, &total_size
);
3789 /* Return value is a negative lttng_error_code. */
3794 cmd_header
.nb_events
= nb_event
;
3795 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3796 &cmd_header
, sizeof(cmd_header
));
3806 case LTTNG_LIST_SESSIONS
:
3808 unsigned int nr_sessions
;
3809 void *sessions_payload
;
3812 session_lock_list();
3813 nr_sessions
= lttng_sessions_count(
3814 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3815 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3816 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3817 sessions_payload
= zmalloc(payload_len
);
3819 if (!sessions_payload
) {
3820 session_unlock_list();
3825 cmd_list_lttng_sessions(sessions_payload
,
3826 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3827 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3828 session_unlock_list();
3830 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3832 free(sessions_payload
);
3841 case LTTNG_REGISTER_CONSUMER
:
3843 struct consumer_data
*cdata
;
3845 switch (cmd_ctx
->lsm
->domain
.type
) {
3846 case LTTNG_DOMAIN_KERNEL
:
3847 cdata
= &kconsumer_data
;
3850 ret
= LTTNG_ERR_UND
;
3854 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3855 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3858 case LTTNG_DATA_PENDING
:
3861 uint8_t pending_ret_byte
;
3863 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3868 * This function may returns 0 or 1 to indicate whether or not
3869 * there is data pending. In case of error, it should return an
3870 * LTTNG_ERR code. However, some code paths may still return
3871 * a nondescript error code, which we handle by returning an
3874 if (pending_ret
== 0 || pending_ret
== 1) {
3876 * ret will be set to LTTNG_OK at the end of
3879 } else if (pending_ret
< 0) {
3880 ret
= LTTNG_ERR_UNK
;
3887 pending_ret_byte
= (uint8_t) pending_ret
;
3889 /* 1 byte to return whether or not data is pending */
3890 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3891 &pending_ret_byte
, 1);
3900 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3902 struct lttcomm_lttng_output_id reply
;
3904 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3905 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3906 if (ret
!= LTTNG_OK
) {
3910 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3916 /* Copy output list into message payload */
3920 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3922 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3923 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3926 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3929 struct lttng_snapshot_output
*outputs
= NULL
;
3931 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3932 if (nb_output
< 0) {
3937 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3938 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3939 nb_output
* sizeof(struct lttng_snapshot_output
));
3949 case LTTNG_SNAPSHOT_RECORD
:
3951 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3952 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3953 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3956 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3959 struct lttng_uri
*uris
= NULL
;
3961 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3962 len
= nb_uri
* sizeof(struct lttng_uri
);
3965 uris
= zmalloc(len
);
3967 ret
= LTTNG_ERR_FATAL
;
3971 /* Receive variable len data */
3972 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3973 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3975 DBG("No URIs received from client... continuing");
3977 ret
= LTTNG_ERR_SESSION_FAIL
;
3982 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3983 DBG("Creating session with ONE network URI is a bad call");
3984 ret
= LTTNG_ERR_SESSION_FAIL
;
3990 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3991 nb_uri
, &cmd_ctx
->creds
);
3995 case LTTNG_CREATE_SESSION_LIVE
:
3998 struct lttng_uri
*uris
= NULL
;
4000 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4001 len
= nb_uri
* sizeof(struct lttng_uri
);
4004 uris
= zmalloc(len
);
4006 ret
= LTTNG_ERR_FATAL
;
4010 /* Receive variable len data */
4011 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4012 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4014 DBG("No URIs received from client... continuing");
4016 ret
= LTTNG_ERR_SESSION_FAIL
;
4021 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4022 DBG("Creating session with ONE network URI is a bad call");
4023 ret
= LTTNG_ERR_SESSION_FAIL
;
4029 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4030 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4034 case LTTNG_SAVE_SESSION
:
4036 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4040 case LTTNG_SET_SESSION_SHM_PATH
:
4042 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4043 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4046 case LTTNG_REGENERATE_METADATA
:
4048 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4051 case LTTNG_REGENERATE_STATEDUMP
:
4053 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4056 case LTTNG_REGISTER_TRIGGER
:
4058 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4059 notification_thread_handle
);
4062 case LTTNG_UNREGISTER_TRIGGER
:
4064 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4065 notification_thread_handle
);
4069 ret
= LTTNG_ERR_UND
;
4074 if (cmd_ctx
->llm
== NULL
) {
4075 DBG("Missing llm structure. Allocating one.");
4076 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4080 /* Set return code */
4081 cmd_ctx
->llm
->ret_code
= ret
;
4083 if (cmd_ctx
->session
) {
4084 session_unlock(cmd_ctx
->session
);
4086 if (need_tracing_session
) {
4087 session_unlock_list();
4090 assert(!rcu_read_ongoing());
4095 * Thread managing health check socket.
4097 static void *thread_manage_health(void *data
)
4099 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4100 uint32_t revents
, nb_fd
;
4101 struct lttng_poll_event events
;
4102 struct health_comm_msg msg
;
4103 struct health_comm_reply reply
;
4105 DBG("[thread] Manage health check started");
4107 rcu_register_thread();
4109 /* We might hit an error path before this is created. */
4110 lttng_poll_init(&events
);
4112 /* Create unix socket */
4113 sock
= lttcomm_create_unix_sock(config
.health_unix_sock_path
.value
);
4115 ERR("Unable to create health check Unix socket");
4122 /* lttng health client socket path permissions */
4123 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
4126 /* Default to root group. */
4129 ret
= chown(config
.health_unix_sock_path
.value
, 0,
4132 ERR("Unable to set group on %s", config
.health_unix_sock_path
.value
);
4137 ret
= chmod(config
.health_unix_sock_path
.value
,
4138 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4140 ERR("Unable to set permissions on %s", config
.health_unix_sock_path
.value
);
4147 * Set the CLOEXEC flag. Return code is useless because either way, the
4150 (void) utils_set_fd_cloexec(sock
);
4152 ret
= lttcomm_listen_unix_sock(sock
);
4158 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4159 * more will be added to this poll set.
4161 ret
= sessiond_set_thread_pollset(&events
, 2);
4166 /* Add the application registration socket */
4167 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4172 sessiond_notify_ready();
4175 DBG("Health check ready");
4177 /* Inifinite blocking call, waiting for transmission */
4179 ret
= lttng_poll_wait(&events
, -1);
4182 * Restart interrupted system call.
4184 if (errno
== EINTR
) {
4192 for (i
= 0; i
< nb_fd
; i
++) {
4193 /* Fetch once the poll data */
4194 revents
= LTTNG_POLL_GETEV(&events
, i
);
4195 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4198 /* No activity for this FD (poll implementation). */
4202 /* Thread quit pipe has been closed. Killing thread. */
4203 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4209 /* Event on the registration socket */
4210 if (pollfd
== sock
) {
4211 if (revents
& LPOLLIN
) {
4213 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4214 ERR("Health socket poll error");
4217 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4223 new_sock
= lttcomm_accept_unix_sock(sock
);
4229 * Set the CLOEXEC flag. Return code is useless because either way, the
4232 (void) utils_set_fd_cloexec(new_sock
);
4234 DBG("Receiving data from client for health...");
4235 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4237 DBG("Nothing recv() from client... continuing");
4238 ret
= close(new_sock
);
4245 rcu_thread_online();
4247 memset(&reply
, 0, sizeof(reply
));
4248 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4250 * health_check_state returns 0 if health is
4253 if (!health_check_state(health_sessiond
, i
)) {
4254 reply
.ret_code
|= 1ULL << i
;
4258 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4260 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4262 ERR("Failed to send health data back to client");
4265 /* End of transmission */
4266 ret
= close(new_sock
);
4275 ERR("Health error occurred in %s", __func__
);
4277 DBG("Health check thread dying");
4278 unlink(config
.health_unix_sock_path
.value
);
4286 lttng_poll_clean(&events
);
4288 rcu_unregister_thread();
4293 * This thread manage all clients request using the unix client socket for
4296 static void *thread_manage_clients(void *data
)
4298 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4300 uint32_t revents
, nb_fd
;
4301 struct command_ctx
*cmd_ctx
= NULL
;
4302 struct lttng_poll_event events
;
4304 DBG("[thread] Manage client started");
4306 rcu_register_thread();
4308 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4310 health_code_update();
4312 ret
= lttcomm_listen_unix_sock(client_sock
);
4318 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4319 * more will be added to this poll set.
4321 ret
= sessiond_set_thread_pollset(&events
, 2);
4323 goto error_create_poll
;
4326 /* Add the application registration socket */
4327 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4332 ret
= sem_post(&load_info
->message_thread_ready
);
4334 PERROR("sem_post message_thread_ready");
4339 * Wait until all support threads are initialized before accepting
4342 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4344 struct timeval timeout
;
4347 FD_SET(thread_quit_pipe
[0], &read_fds
);
4348 memset(&timeout
, 0, sizeof(timeout
));
4349 timeout
.tv_usec
= 1000;
4352 * If a support thread failed to launch, it may signal that
4353 * we must exit and the sessiond would never be marked as
4356 * The timeout is set to 1ms, which serves as a way to
4357 * pace down this check.
4359 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
4361 if (ret
> 0 || (ret
< 0 && errno
!= EINTR
)) {
4366 * This barrier is paired with the one in sessiond_notify_ready() to
4367 * ensure that loads accessing data initialized by the other threads,
4368 * on which this thread was waiting, are not performed before this point.
4370 * Note that this could be a 'read' memory barrier, but a full barrier
4371 * is used in case the code changes. The performance implications of
4372 * this choice are minimal since this is a slow path.
4376 /* This testpoint is after we signal readiness to the parent. */
4377 if (testpoint(sessiond_thread_manage_clients
)) {
4381 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4385 health_code_update();
4388 DBG("Accepting client command ...");
4390 /* Inifinite blocking call, waiting for transmission */
4392 health_poll_entry();
4393 ret
= lttng_poll_wait(&events
, -1);
4397 * Restart interrupted system call.
4399 if (errno
== EINTR
) {
4407 for (i
= 0; i
< nb_fd
; i
++) {
4408 /* Fetch once the poll data */
4409 revents
= LTTNG_POLL_GETEV(&events
, i
);
4410 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4412 health_code_update();
4415 /* No activity for this FD (poll implementation). */
4419 /* Thread quit pipe has been closed. Killing thread. */
4420 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4426 /* Event on the registration socket */
4427 if (pollfd
== client_sock
) {
4428 if (revents
& LPOLLIN
) {
4430 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4431 ERR("Client socket poll error");
4434 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4440 DBG("Wait for client response");
4442 health_code_update();
4444 sock
= lttcomm_accept_unix_sock(client_sock
);
4450 * Set the CLOEXEC flag. Return code is useless because either way, the
4453 (void) utils_set_fd_cloexec(sock
);
4455 /* Set socket option for credentials retrieval */
4456 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4461 /* Allocate context command to process the client request */
4462 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4463 if (cmd_ctx
== NULL
) {
4464 PERROR("zmalloc cmd_ctx");
4468 /* Allocate data buffer for reception */
4469 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4470 if (cmd_ctx
->lsm
== NULL
) {
4471 PERROR("zmalloc cmd_ctx->lsm");
4475 cmd_ctx
->llm
= NULL
;
4476 cmd_ctx
->session
= NULL
;
4478 health_code_update();
4481 * Data is received from the lttng client. The struct
4482 * lttcomm_session_msg (lsm) contains the command and data request of
4485 DBG("Receiving data from client ...");
4486 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4487 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4489 DBG("Nothing recv() from client... continuing");
4495 clean_command_ctx(&cmd_ctx
);
4499 health_code_update();
4501 // TODO: Validate cmd_ctx including sanity check for
4502 // security purpose.
4504 rcu_thread_online();
4506 * This function dispatch the work to the kernel or userspace tracer
4507 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4508 * informations for the client. The command context struct contains
4509 * everything this function may needs.
4511 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4512 rcu_thread_offline();
4520 * TODO: Inform client somehow of the fatal error. At
4521 * this point, ret < 0 means that a zmalloc failed
4522 * (ENOMEM). Error detected but still accept
4523 * command, unless a socket error has been
4526 clean_command_ctx(&cmd_ctx
);
4530 health_code_update();
4532 DBG("Sending response (size: %d, retcode: %s (%d))",
4533 cmd_ctx
->lttng_msg_size
,
4534 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4535 cmd_ctx
->llm
->ret_code
);
4536 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4538 ERR("Failed to send data back to client");
4541 /* End of transmission */
4548 clean_command_ctx(&cmd_ctx
);
4550 health_code_update();
4562 lttng_poll_clean(&events
);
4563 clean_command_ctx(&cmd_ctx
);
4567 unlink(config
.client_unix_sock_path
.value
);
4568 if (client_sock
>= 0) {
4569 ret
= close(client_sock
);
4577 ERR("Health error occurred in %s", __func__
);
4580 health_unregister(health_sessiond
);
4582 DBG("Client thread dying");
4584 rcu_unregister_thread();
4587 * Since we are creating the consumer threads, we own them, so we need
4588 * to join them before our thread exits.
4590 ret
= join_consumer_thread(&kconsumer_data
);
4593 PERROR("join_consumer");
4596 ret
= join_consumer_thread(&ustconsumer32_data
);
4599 PERROR("join_consumer ust32");
4602 ret
= join_consumer_thread(&ustconsumer64_data
);
4605 PERROR("join_consumer ust64");
4610 static int string_match(const char *str1
, const char *str2
)
4612 return (str1
&& str2
) && !strcmp(str1
, str2
);
4616 * Take an option from the getopt output and set it in the right variable to be
4619 * Return 0 on success else a negative value.
4621 static int set_option(int opt
, const char *arg
, const char *optname
)
4625 if (string_match(optname
, "client-sock") || opt
== 'c') {
4626 if (!arg
|| *arg
== '\0') {
4630 if (lttng_is_setuid_setgid()) {
4631 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4632 "-c, --client-sock");
4634 config_string_set(&config
.client_unix_sock_path
,
4636 if (!config
.client_unix_sock_path
.value
) {
4641 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4642 if (!arg
|| *arg
== '\0') {
4646 if (lttng_is_setuid_setgid()) {
4647 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4650 config_string_set(&config
.apps_unix_sock_path
,
4652 if (!config
.apps_unix_sock_path
.value
) {
4657 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4658 config
.daemonize
= true;
4659 } else if (string_match(optname
, "background") || opt
== 'b') {
4660 config
.background
= true;
4661 } else if (string_match(optname
, "group") || opt
== 'g') {
4662 if (!arg
|| *arg
== '\0') {
4666 if (lttng_is_setuid_setgid()) {
4667 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4670 config_string_set(&config
.tracing_group_name
,
4672 if (!config
.tracing_group_name
.value
) {
4677 } else if (string_match(optname
, "help") || opt
== 'h') {
4678 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4680 ERR("Cannot show --help for `lttng-sessiond`");
4683 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4684 } else if (string_match(optname
, "version") || opt
== 'V') {
4685 fprintf(stdout
, "%s\n", VERSION
);
4687 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4688 config
.sig_parent
= true;
4689 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4690 if (!arg
|| *arg
== '\0') {
4694 if (lttng_is_setuid_setgid()) {
4695 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4696 "--kconsumerd-err-sock");
4698 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4700 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4705 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4706 if (!arg
|| *arg
== '\0') {
4710 if (lttng_is_setuid_setgid()) {
4711 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4712 "--kconsumerd-cmd-sock");
4714 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4716 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4721 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4722 if (!arg
|| *arg
== '\0') {
4726 if (lttng_is_setuid_setgid()) {
4727 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4728 "--ustconsumerd64-err-sock");
4730 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4732 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4737 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4738 if (!arg
|| *arg
== '\0') {
4742 if (lttng_is_setuid_setgid()) {
4743 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4744 "--ustconsumerd64-cmd-sock");
4746 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4748 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4753 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4754 if (!arg
|| *arg
== '\0') {
4758 if (lttng_is_setuid_setgid()) {
4759 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4760 "--ustconsumerd32-err-sock");
4762 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4764 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4769 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4770 if (!arg
|| *arg
== '\0') {
4774 if (lttng_is_setuid_setgid()) {
4775 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4776 "--ustconsumerd32-cmd-sock");
4778 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4780 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4785 } else if (string_match(optname
, "no-kernel")) {
4786 config
.no_kernel
= true;
4787 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4788 config
.quiet
= true;
4789 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4790 /* Verbose level can increase using multiple -v */
4792 /* Value obtained from config file */
4793 config
.verbose
= config_parse_value(arg
);
4795 /* -v used on command line */
4798 /* Clamp value to [0, 3] */
4799 config
.verbose
= config
.verbose
< 0 ? 0 :
4800 (config
.verbose
<= 3 ? config
.verbose
: 3);
4801 } else if (string_match(optname
, "verbose-consumer")) {
4803 config
.verbose_consumer
= config_parse_value(arg
);
4805 config
.verbose_consumer
++;
4807 } else if (string_match(optname
, "consumerd32-path")) {
4808 if (!arg
|| *arg
== '\0') {
4812 if (lttng_is_setuid_setgid()) {
4813 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4814 "--consumerd32-path");
4816 config_string_set(&config
.consumerd32_bin_path
,
4818 if (!config
.consumerd32_bin_path
.value
) {
4823 } else if (string_match(optname
, "consumerd32-libdir")) {
4824 if (!arg
|| *arg
== '\0') {
4828 if (lttng_is_setuid_setgid()) {
4829 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4830 "--consumerd32-libdir");
4832 config_string_set(&config
.consumerd32_lib_dir
,
4834 if (!config
.consumerd32_lib_dir
.value
) {
4839 } else if (string_match(optname
, "consumerd64-path")) {
4840 if (!arg
|| *arg
== '\0') {
4844 if (lttng_is_setuid_setgid()) {
4845 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4846 "--consumerd64-path");
4848 config_string_set(&config
.consumerd64_bin_path
,
4850 if (!config
.consumerd64_bin_path
.value
) {
4855 } else if (string_match(optname
, "consumerd64-libdir")) {
4856 if (!arg
|| *arg
== '\0') {
4860 if (lttng_is_setuid_setgid()) {
4861 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4862 "--consumerd64-libdir");
4864 config_string_set(&config
.consumerd64_lib_dir
,
4866 if (!config
.consumerd64_lib_dir
.value
) {
4871 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4872 if (!arg
|| *arg
== '\0') {
4876 if (lttng_is_setuid_setgid()) {
4877 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4880 config_string_set(&config
.pid_file_path
, strdup(arg
));
4881 if (!config
.pid_file_path
.value
) {
4886 } else if (string_match(optname
, "agent-tcp-port")) {
4887 if (!arg
|| *arg
== '\0') {
4891 if (lttng_is_setuid_setgid()) {
4892 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4893 "--agent-tcp-port");
4898 v
= strtoul(arg
, NULL
, 0);
4899 if (errno
!= 0 || !isdigit(arg
[0])) {
4900 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4903 if (v
== 0 || v
>= 65535) {
4904 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4907 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
4908 DBG3("Agent TCP port set to non default: %i", (int) v
);
4910 } else if (string_match(optname
, "load") || opt
== 'l') {
4911 if (!arg
|| *arg
== '\0') {
4915 if (lttng_is_setuid_setgid()) {
4916 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4919 config_string_set(&config
.load_session_path
, strdup(arg
));
4920 if (!config
.load_session_path
.value
) {
4925 } else if (string_match(optname
, "kmod-probes")) {
4926 if (!arg
|| *arg
== '\0') {
4930 if (lttng_is_setuid_setgid()) {
4931 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4934 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
4935 if (!config
.kmod_probes_list
.value
) {
4940 } else if (string_match(optname
, "extra-kmod-probes")) {
4941 if (!arg
|| *arg
== '\0') {
4945 if (lttng_is_setuid_setgid()) {
4946 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4947 "--extra-kmod-probes");
4949 config_string_set(&config
.kmod_extra_probes_list
,
4951 if (!config
.kmod_extra_probes_list
.value
) {
4956 } else if (string_match(optname
, "config") || opt
== 'f') {
4957 /* This is handled in set_options() thus silent skip. */
4960 /* Unknown option or other error.
4961 * Error is printed by getopt, just return */
4966 if (ret
== -EINVAL
) {
4967 const char *opt_name
= "unknown";
4970 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4972 if (opt
== long_options
[i
].val
) {
4973 opt_name
= long_options
[i
].name
;
4978 WARN("Invalid argument provided for option \"%s\", using default value.",
4986 * config_entry_handler_cb used to handle options read from a config file.
4987 * See config_entry_handler_cb comment in common/config/session-config.h for the
4988 * return value conventions.
4990 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4994 if (!entry
|| !entry
->name
|| !entry
->value
) {
4999 /* Check if the option is to be ignored */
5000 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5001 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5006 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5009 /* Ignore if not fully matched. */
5010 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5015 * If the option takes no argument on the command line, we have to
5016 * check if the value is "true". We support non-zero numeric values,
5019 if (!long_options
[i
].has_arg
) {
5020 ret
= config_parse_value(entry
->value
);
5023 WARN("Invalid configuration value \"%s\" for option %s",
5024 entry
->value
, entry
->name
);
5026 /* False, skip boolean config option. */
5031 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5035 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5042 * daemon configuration loading and argument parsing
5044 static int set_options(int argc
, char **argv
)
5046 int ret
= 0, c
= 0, option_index
= 0;
5047 int orig_optopt
= optopt
, orig_optind
= optind
;
5049 const char *config_path
= NULL
;
5051 optstring
= utils_generate_optstring(long_options
,
5052 sizeof(long_options
) / sizeof(struct option
));
5058 /* Check for the --config option */
5059 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5060 &option_index
)) != -1) {
5064 } else if (c
!= 'f') {
5065 /* if not equal to --config option. */
5069 if (lttng_is_setuid_setgid()) {
5070 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5073 config_path
= utils_expand_path(optarg
);
5075 ERR("Failed to resolve path: %s", optarg
);
5080 ret
= config_get_section_entries(config_path
, config_section_name
,
5081 config_entry_handler
, NULL
);
5084 ERR("Invalid configuration option at line %i", ret
);
5090 /* Reset getopt's global state */
5091 optopt
= orig_optopt
;
5092 optind
= orig_optind
;
5096 * getopt_long() will not set option_index if it encounters a
5099 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5106 * Pass NULL as the long option name if popt left the index
5109 ret
= set_option(c
, optarg
,
5110 option_index
< 0 ? NULL
:
5111 long_options
[option_index
].name
);
5123 * Creates the two needed socket by the daemon.
5124 * apps_sock - The communication socket for all UST apps.
5125 * client_sock - The communication of the cli tool (lttng).
5127 static int init_daemon_socket(void)
5132 old_umask
= umask(0);
5134 /* Create client tool unix socket */
5135 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5136 if (client_sock
< 0) {
5137 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5142 /* Set the cloexec flag */
5143 ret
= utils_set_fd_cloexec(client_sock
);
5145 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5146 "Continuing but note that the consumer daemon will have a "
5147 "reference to this socket on exec()", client_sock
);
5150 /* File permission MUST be 660 */
5151 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5153 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5158 /* Create the application unix socket */
5159 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5160 if (apps_sock
< 0) {
5161 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5166 /* Set the cloexec flag */
5167 ret
= utils_set_fd_cloexec(apps_sock
);
5169 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5170 "Continuing but note that the consumer daemon will have a "
5171 "reference to this socket on exec()", apps_sock
);
5174 /* File permission MUST be 666 */
5175 ret
= chmod(config
.apps_unix_sock_path
.value
,
5176 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5178 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5183 DBG3("Session daemon client socket %d and application socket %d created",
5184 client_sock
, apps_sock
);
5192 * Create lockfile using the rundir and return its fd.
5194 static int create_lockfile(void)
5196 return utils_create_lock_file(config
.lock_file_path
.value
);
5200 * Check if the global socket is available, and if a daemon is answering at the
5201 * other side. If yes, error is returned.
5203 * Also attempts to create and hold the lock file.
5205 static int check_existing_daemon(void)
5209 /* Is there anybody out there ? */
5210 if (lttng_session_daemon_alive()) {
5215 lockfile_fd
= create_lockfile();
5216 if (lockfile_fd
< 0) {
5224 static void sessiond_cleanup_lock_file(void)
5229 * Cleanup lock file by deleting it and finaly closing it which will
5230 * release the file system lock.
5232 if (lockfile_fd
>= 0) {
5233 ret
= remove(config
.lock_file_path
.value
);
5235 PERROR("remove lock file");
5237 ret
= close(lockfile_fd
);
5239 PERROR("close lock file");
5245 * Set the tracing group gid onto the client socket.
5247 * Race window between mkdir and chown is OK because we are going from more
5248 * permissive (root.root) to less permissive (root.tracing).
5250 static int set_permissions(char *rundir
)
5255 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true, &gid
);
5257 /* Default to root group. */
5261 /* Set lttng run dir */
5262 ret
= chown(rundir
, 0, gid
);
5264 ERR("Unable to set group on %s", rundir
);
5269 * Ensure all applications and tracing group can search the run
5270 * dir. Allow everyone to read the directory, since it does not
5271 * buy us anything to hide its content.
5273 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5275 ERR("Unable to set permissions on %s", rundir
);
5279 /* lttng client socket path */
5280 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5282 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5286 /* kconsumer error socket path */
5287 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5289 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5293 /* 64-bit ustconsumer error socket path */
5294 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5296 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5300 /* 32-bit ustconsumer compat32 error socket path */
5301 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5303 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5307 DBG("All permissions are set");
5313 * Create the lttng run directory needed for all global sockets and pipe.
5315 static int create_lttng_rundir(void)
5319 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5321 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5323 if (errno
!= EEXIST
) {
5324 ERR("Unable to create %s", config
.rundir
.value
);
5336 * Setup sockets and directory needed by the consumerds' communication with the
5339 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5344 switch (consumer_data
->type
) {
5345 case LTTNG_CONSUMER_KERNEL
:
5346 path
= config
.kconsumerd_path
.value
;
5348 case LTTNG_CONSUMER64_UST
:
5349 path
= config
.consumerd64_path
.value
;
5351 case LTTNG_CONSUMER32_UST
:
5352 path
= config
.consumerd32_path
.value
;
5355 ERR("Consumer type unknown");
5361 DBG2("Creating consumer directory: %s", path
);
5363 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5364 if (ret
< 0 && errno
!= EEXIST
) {
5366 ERR("Failed to create %s", path
);
5372 ret
= utils_get_group_id(config
.tracing_group_name
.value
, true,
5375 /* Default to root group. */
5379 ret
= chown(path
, 0, gid
);
5381 ERR("Unable to set group on %s", path
);
5387 /* Create the consumerd error unix socket */
5388 consumer_data
->err_sock
=
5389 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5390 if (consumer_data
->err_sock
< 0) {
5391 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5397 * Set the CLOEXEC flag. Return code is useless because either way, the
5400 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5402 PERROR("utils_set_fd_cloexec");
5403 /* continue anyway */
5406 /* File permission MUST be 660 */
5407 ret
= chmod(consumer_data
->err_unix_sock_path
,
5408 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5410 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5420 * Signal handler for the daemon
5422 * Simply stop all worker threads, leaving main() return gracefully after
5423 * joining all threads and calling cleanup().
5425 static void sighandler(int sig
)
5429 DBG("SIGINT caught");
5433 DBG("SIGTERM caught");
5437 CMM_STORE_SHARED(recv_child_signal
, 1);
5445 * Setup signal handler for :
5446 * SIGINT, SIGTERM, SIGPIPE
5448 static int set_signal_handler(void)
5451 struct sigaction sa
;
5454 if ((ret
= sigemptyset(&sigset
)) < 0) {
5455 PERROR("sigemptyset");
5459 sa
.sa_mask
= sigset
;
5462 sa
.sa_handler
= sighandler
;
5463 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5464 PERROR("sigaction");
5468 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5469 PERROR("sigaction");
5473 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5474 PERROR("sigaction");
5478 sa
.sa_handler
= SIG_IGN
;
5479 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5480 PERROR("sigaction");
5484 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5490 * Set open files limit to unlimited. This daemon can open a large number of
5491 * file descriptors in order to consume multiple kernel traces.
5493 static void set_ulimit(void)
5498 /* The kernel does not allow an infinite limit for open files */
5499 lim
.rlim_cur
= 65535;
5500 lim
.rlim_max
= 65535;
5502 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5504 PERROR("failed to set open files limit");
5508 static int write_pidfile(void)
5510 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5513 static int set_clock_plugin_env(void)
5516 char *env_value
= NULL
;
5518 if (!config
.lttng_ust_clock_plugin
.value
) {
5522 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5523 config
.lttng_ust_clock_plugin
.value
);
5529 ret
= putenv(env_value
);
5532 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5536 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5537 config
.lttng_ust_clock_plugin
.value
);
5545 int main(int argc
, char **argv
)
5547 int ret
= 0, retval
= 0;
5549 const char *env_app_timeout
;
5550 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5551 *ust64_channel_monitor_pipe
= NULL
,
5552 *kernel_channel_monitor_pipe
= NULL
;
5553 bool notification_thread_running
= false;
5555 init_kernel_workarounds();
5557 rcu_register_thread();
5559 if (set_signal_handler()) {
5561 goto exit_set_signal_handler
;
5564 page_size
= sysconf(_SC_PAGESIZE
);
5565 if (page_size
< 0) {
5566 PERROR("sysconf _SC_PAGESIZE");
5567 page_size
= LONG_MAX
;
5568 WARN("Fallback page size to %ld", page_size
);
5571 ret
= sessiond_config_init(&config
);
5574 goto exit_set_signal_handler
;
5578 * Init config from environment variables.
5579 * Command line option override env configuration per-doc. Do env first.
5581 sessiond_config_apply_env_config(&config
);
5584 * Parse arguments and load the daemon configuration file.
5586 * We have an exit_options exit path to free memory reserved by
5587 * set_options. This is needed because the rest of sessiond_cleanup()
5588 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5589 * depends on set_options.
5592 if (set_options(argc
, argv
)) {
5598 * Resolve all paths received as arguments, configuration option, or
5599 * through environment variable as absolute paths. This is necessary
5600 * since daemonizing causes the sessiond's current working directory
5603 ret
= sessiond_config_resolve_paths(&config
);
5609 lttng_opt_verbose
= config
.verbose
;
5610 lttng_opt_quiet
= config
.quiet
;
5611 kconsumer_data
.err_unix_sock_path
=
5612 config
.kconsumerd_err_unix_sock_path
.value
;
5613 kconsumer_data
.cmd_unix_sock_path
=
5614 config
.kconsumerd_cmd_unix_sock_path
.value
;
5615 ustconsumer32_data
.err_unix_sock_path
=
5616 config
.consumerd32_err_unix_sock_path
.value
;
5617 ustconsumer32_data
.cmd_unix_sock_path
=
5618 config
.consumerd32_cmd_unix_sock_path
.value
;
5619 ustconsumer64_data
.err_unix_sock_path
=
5620 config
.consumerd64_err_unix_sock_path
.value
;
5621 ustconsumer64_data
.cmd_unix_sock_path
=
5622 config
.consumerd64_cmd_unix_sock_path
.value
;
5623 set_clock_plugin_env();
5625 sessiond_config_log(&config
);
5627 if (create_lttng_rundir()) {
5632 /* Abort launch if a session daemon is already running. */
5633 if (check_existing_daemon()) {
5634 ERR("A session daemon is already running.");
5640 if (config
.daemonize
|| config
.background
) {
5643 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5644 !config
.background
);
5651 * We are in the child. Make sure all other file descriptors are
5652 * closed, in case we are called with more opened file
5653 * descriptors than the standard ones and the lock file.
5655 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5656 if (i
== lockfile_fd
) {
5663 if (run_as_create_worker(argv
[0]) < 0) {
5664 goto exit_create_run_as_worker_cleanup
;
5668 * Starting from here, we can create threads. This needs to be after
5669 * lttng_daemonize due to RCU.
5673 * Initialize the health check subsystem. This call should set the
5674 * appropriate time values.
5676 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5677 if (!health_sessiond
) {
5678 PERROR("health_app_create error");
5680 goto exit_health_sessiond_cleanup
;
5683 /* Create thread to clean up RCU hash tables */
5684 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5686 goto exit_ht_cleanup
;
5689 /* Create thread quit pipe */
5690 if (init_thread_quit_pipe()) {
5692 goto exit_init_data
;
5695 /* Check if daemon is UID = 0 */
5696 is_root
= !getuid();
5698 /* Create global run dir with root access */
5700 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5701 if (!kernel_channel_monitor_pipe
) {
5702 ERR("Failed to create kernel consumer channel monitor pipe");
5704 goto exit_init_data
;
5706 kconsumer_data
.channel_monitor_pipe
=
5707 lttng_pipe_release_writefd(
5708 kernel_channel_monitor_pipe
);
5709 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5711 goto exit_init_data
;
5715 /* Set consumer initial state */
5716 kernel_consumerd_state
= CONSUMER_STOPPED
;
5717 ust_consumerd_state
= CONSUMER_STOPPED
;
5719 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5720 if (!ust32_channel_monitor_pipe
) {
5721 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5723 goto exit_init_data
;
5725 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5726 ust32_channel_monitor_pipe
);
5727 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5729 goto exit_init_data
;
5732 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5733 if (!ust64_channel_monitor_pipe
) {
5734 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5736 goto exit_init_data
;
5738 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5739 ust64_channel_monitor_pipe
);
5740 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5742 goto exit_init_data
;
5746 * Init UST app hash table. Alloc hash table before this point since
5747 * cleanup() can get called after that point.
5749 if (ust_app_ht_alloc()) {
5750 ERR("Failed to allocate UST app hash table");
5752 goto exit_init_data
;
5756 * Initialize agent app hash table. We allocate the hash table here
5757 * since cleanup() can get called after this point.
5759 if (agent_app_ht_alloc()) {
5760 ERR("Failed to allocate Agent app hash table");
5762 goto exit_init_data
;
5766 * These actions must be executed as root. We do that *after* setting up
5767 * the sockets path because we MUST make the check for another daemon using
5768 * those paths *before* trying to set the kernel consumer sockets and init
5772 if (set_consumer_sockets(&kconsumer_data
)) {
5774 goto exit_init_data
;
5777 /* Setup kernel tracer */
5778 if (!config
.no_kernel
) {
5779 init_kernel_tracer();
5782 /* Set ulimit for open files */
5785 /* init lttng_fd tracking must be done after set_ulimit. */
5788 if (set_consumer_sockets(&ustconsumer64_data
)) {
5790 goto exit_init_data
;
5793 if (set_consumer_sockets(&ustconsumer32_data
)) {
5795 goto exit_init_data
;
5798 /* Setup the needed unix socket */
5799 if (init_daemon_socket()) {
5801 goto exit_init_data
;
5804 /* Set credentials to socket */
5805 if (is_root
&& set_permissions(config
.rundir
.value
)) {
5807 goto exit_init_data
;
5810 /* Get parent pid if -S, --sig-parent is specified. */
5811 if (config
.sig_parent
) {
5815 /* Setup the kernel pipe for waking up the kernel thread */
5816 if (is_root
&& !config
.no_kernel
) {
5817 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5819 goto exit_init_data
;
5823 /* Setup the thread apps communication pipe. */
5824 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5826 goto exit_init_data
;
5829 /* Setup the thread apps notify communication pipe. */
5830 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5832 goto exit_init_data
;
5835 /* Initialize global buffer per UID and PID registry. */
5836 buffer_reg_init_uid_registry();
5837 buffer_reg_init_pid_registry();
5839 /* Init UST command queue. */
5840 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5843 * Get session list pointer. This pointer MUST NOT be free'd. This list
5844 * is statically declared in session.c
5846 session_list_ptr
= session_get_list();
5850 /* Check for the application socket timeout env variable. */
5851 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5852 if (env_app_timeout
) {
5853 config
.app_socket_timeout
= atoi(env_app_timeout
);
5855 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5858 ret
= write_pidfile();
5860 ERR("Error in write_pidfile");
5862 goto exit_init_data
;
5865 /* Initialize communication library */
5867 /* Initialize TCP timeout values */
5868 lttcomm_inet_init();
5870 if (load_session_init_data(&load_info
) < 0) {
5872 goto exit_init_data
;
5874 load_info
->path
= config
.load_session_path
.value
;
5876 /* Create health-check thread. */
5877 ret
= pthread_create(&health_thread
, default_pthread_attr(),
5878 thread_manage_health
, (void *) NULL
);
5881 PERROR("pthread_create health");
5886 /* notification_thread_data acquires the pipes' read side. */
5887 notification_thread_handle
= notification_thread_handle_create(
5888 ust32_channel_monitor_pipe
,
5889 ust64_channel_monitor_pipe
,
5890 kernel_channel_monitor_pipe
);
5891 if (!notification_thread_handle
) {
5893 ERR("Failed to create notification thread shared data");
5895 goto exit_notification
;
5898 /* Create notification thread. */
5899 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
5900 thread_notification
, notification_thread_handle
);
5903 PERROR("pthread_create notification");
5906 goto exit_notification
;
5908 notification_thread_running
= true;
5910 /* Create thread to manage the client socket */
5911 ret
= pthread_create(&client_thread
, default_pthread_attr(),
5912 thread_manage_clients
, (void *) NULL
);
5915 PERROR("pthread_create clients");
5921 /* Create thread to dispatch registration */
5922 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
5923 thread_dispatch_ust_registration
, (void *) NULL
);
5926 PERROR("pthread_create dispatch");
5932 /* Create thread to manage application registration. */
5933 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
5934 thread_registration_apps
, (void *) NULL
);
5937 PERROR("pthread_create registration");
5943 /* Create thread to manage application socket */
5944 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
5945 thread_manage_apps
, (void *) NULL
);
5948 PERROR("pthread_create apps");
5954 /* Create thread to manage application notify socket */
5955 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
5956 ust_thread_manage_notify
, (void *) NULL
);
5959 PERROR("pthread_create notify");
5962 goto exit_apps_notify
;
5965 /* Create agent registration thread. */
5966 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
5967 agent_thread_manage_registration
, (void *) NULL
);
5970 PERROR("pthread_create agent");
5973 goto exit_agent_reg
;
5976 /* Don't start this thread if kernel tracing is not requested nor root */
5977 if (is_root
&& !config
.no_kernel
) {
5978 /* Create kernel thread to manage kernel event */
5979 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
5980 thread_manage_kernel
, (void *) NULL
);
5983 PERROR("pthread_create kernel");
5990 /* Create session loading thread. */
5991 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
5992 thread_load_session
, load_info
);
5995 PERROR("pthread_create load_session_thread");
5998 goto exit_load_session
;
6002 * This is where we start awaiting program completion (e.g. through
6003 * signal that asks threads to teardown).
6006 ret
= pthread_join(load_session_thread
, &status
);
6009 PERROR("pthread_join load_session_thread");
6014 if (is_root
&& !config
.no_kernel
) {
6015 ret
= pthread_join(kernel_thread
, &status
);
6018 PERROR("pthread_join");
6024 ret
= pthread_join(agent_reg_thread
, &status
);
6027 PERROR("pthread_join agent");
6032 ret
= pthread_join(apps_notify_thread
, &status
);
6035 PERROR("pthread_join apps notify");
6040 ret
= pthread_join(apps_thread
, &status
);
6043 PERROR("pthread_join apps");
6048 ret
= pthread_join(reg_apps_thread
, &status
);
6051 PERROR("pthread_join");
6057 * Join dispatch thread after joining reg_apps_thread to ensure
6058 * we don't leak applications in the queue.
6060 ret
= pthread_join(dispatch_thread
, &status
);
6063 PERROR("pthread_join");
6068 ret
= pthread_join(client_thread
, &status
);
6071 PERROR("pthread_join");
6077 ret
= pthread_join(health_thread
, &status
);
6080 PERROR("pthread_join health thread");
6087 * Wait for all pending call_rcu work to complete before tearing
6088 * down data structures. call_rcu worker may be trying to
6089 * perform lookups in those structures.
6093 * sessiond_cleanup() is called when no other thread is running, except
6094 * the ht_cleanup thread, which is needed to destroy the hash tables.
6096 rcu_thread_online();
6100 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6101 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6102 * the queue is empty before shutting down the clean-up thread.
6107 * The teardown of the notification system is performed after the
6108 * session daemon's teardown in order to allow it to be notified
6109 * of the active session and channels at the moment of the teardown.
6111 if (notification_thread_handle
) {
6112 if (notification_thread_running
) {
6113 notification_thread_command_quit(
6114 notification_thread_handle
);
6115 ret
= pthread_join(notification_thread
, &status
);
6118 PERROR("pthread_join notification thread");
6122 notification_thread_handle_destroy(notification_thread_handle
);
6125 rcu_thread_offline();
6126 rcu_unregister_thread();
6128 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6132 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6133 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6134 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6137 health_app_destroy(health_sessiond
);
6138 exit_health_sessiond_cleanup
:
6139 exit_create_run_as_worker_cleanup
:
6142 sessiond_cleanup_lock_file();
6143 sessiond_cleanup_options();
6145 exit_set_signal_handler
: