2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
52 #include <common/dynamic-buffer.h>
53 #include <lttng/userspace-probe-internal.h>
54 #include <lttng/event-internal.h>
56 #include "lttng-sessiond.h"
57 #include "buffer-registry.h"
64 #include "kernel-consumer.h"
68 #include "ust-consumer.h"
71 #include "health-sessiond.h"
72 #include "testpoint.h"
73 #include "ust-thread.h"
74 #include "agent-thread.h"
76 #include "load-session-thread.h"
77 #include "notification-thread.h"
78 #include "notification-thread-commands.h"
79 #include "rotation-thread.h"
80 #include "lttng-syscall.h"
82 #include "ht-cleanup.h"
83 #include "sessiond-config.h"
87 static const char *help_msg
=
88 #ifdef LTTNG_EMBED_HELP
89 #include <lttng-sessiond.8.h>
96 static int lockfile_fd
= -1;
98 /* Set to 1 when a SIGUSR1 signal is received. */
99 static int recv_child_signal
;
101 static struct lttng_kernel_tracer_version kernel_tracer_version
;
102 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
105 * Consumer daemon specific control data. Every value not initialized here is
106 * set to 0 by the static definition.
108 static struct consumer_data kconsumer_data
= {
109 .type
= LTTNG_CONSUMER_KERNEL
,
112 .channel_monitor_pipe
= -1,
113 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
114 .lock
= PTHREAD_MUTEX_INITIALIZER
,
115 .cond
= PTHREAD_COND_INITIALIZER
,
116 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
118 static struct consumer_data ustconsumer64_data
= {
119 .type
= LTTNG_CONSUMER64_UST
,
122 .channel_monitor_pipe
= -1,
123 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
124 .lock
= PTHREAD_MUTEX_INITIALIZER
,
125 .cond
= PTHREAD_COND_INITIALIZER
,
126 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
128 static struct consumer_data ustconsumer32_data
= {
129 .type
= LTTNG_CONSUMER32_UST
,
132 .channel_monitor_pipe
= -1,
133 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
134 .lock
= PTHREAD_MUTEX_INITIALIZER
,
135 .cond
= PTHREAD_COND_INITIALIZER
,
136 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
139 /* Command line options */
140 static const struct option long_options
[] = {
141 { "client-sock", required_argument
, 0, 'c' },
142 { "apps-sock", required_argument
, 0, 'a' },
143 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
144 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
145 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
146 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
147 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
148 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
149 { "consumerd32-path", required_argument
, 0, '\0' },
150 { "consumerd32-libdir", required_argument
, 0, '\0' },
151 { "consumerd64-path", required_argument
, 0, '\0' },
152 { "consumerd64-libdir", required_argument
, 0, '\0' },
153 { "daemonize", no_argument
, 0, 'd' },
154 { "background", no_argument
, 0, 'b' },
155 { "sig-parent", no_argument
, 0, 'S' },
156 { "help", no_argument
, 0, 'h' },
157 { "group", required_argument
, 0, 'g' },
158 { "version", no_argument
, 0, 'V' },
159 { "quiet", no_argument
, 0, 'q' },
160 { "verbose", no_argument
, 0, 'v' },
161 { "verbose-consumer", no_argument
, 0, '\0' },
162 { "no-kernel", no_argument
, 0, '\0' },
163 { "pidfile", required_argument
, 0, 'p' },
164 { "agent-tcp-port", required_argument
, 0, '\0' },
165 { "config", required_argument
, 0, 'f' },
166 { "load", required_argument
, 0, 'l' },
167 { "kmod-probes", required_argument
, 0, '\0' },
168 { "extra-kmod-probes", required_argument
, 0, '\0' },
172 /* Command line options to ignore from configuration file */
173 static const char *config_ignore_options
[] = { "help", "version", "config" };
175 /* Shared between threads */
176 static int dispatch_thread_exit
;
178 /* Sockets and FDs */
179 static int client_sock
= -1;
180 static int apps_sock
= -1;
183 * This pipe is used to inform the thread managing application communication
184 * that a command is queued and ready to be processed.
186 static int apps_cmd_pipe
[2] = { -1, -1 };
188 /* Pthread, Mutexes and Semaphores */
189 static pthread_t apps_thread
;
190 static pthread_t apps_notify_thread
;
191 static pthread_t reg_apps_thread
;
192 static pthread_t client_thread
;
193 static pthread_t kernel_thread
;
194 static pthread_t dispatch_thread
;
195 static pthread_t agent_reg_thread
;
196 static pthread_t load_session_thread
;
197 static pthread_t rotation_thread
;
198 static pthread_t timer_thread
;
201 * UST registration command queue. This queue is tied with a futex and uses a N
202 * wakers / 1 waiter implemented and detailed in futex.c/.h
204 * The thread_registration_apps and thread_dispatch_ust_registration uses this
205 * queue along with the wait/wake scheme. The thread_manage_apps receives down
206 * the line new application socket and monitors it for any I/O error or clean
207 * close that triggers an unregistration of the application.
209 static struct ust_cmd_queue ust_cmd_queue
;
211 static const char *module_proc_lttng
= "/proc/lttng";
214 * Consumer daemon state which is changed when spawning it, killing it or in
215 * case of a fatal error.
217 enum consumerd_state
{
218 CONSUMER_STARTED
= 1,
219 CONSUMER_STOPPED
= 2,
224 * This consumer daemon state is used to validate if a client command will be
225 * able to reach the consumer. If not, the client is informed. For instance,
226 * doing a "lttng start" when the consumer state is set to ERROR will return an
227 * error to the client.
229 * The following example shows a possible race condition of this scheme:
231 * consumer thread error happens
233 * client cmd checks state -> still OK
234 * consumer thread exit, sets error
235 * client cmd try to talk to consumer
238 * However, since the consumer is a different daemon, we have no way of making
239 * sure the command will reach it safely even with this state flag. This is why
240 * we consider that up to the state validation during command processing, the
241 * command is safe. After that, we can not guarantee the correctness of the
242 * client request vis-a-vis the consumer.
244 static enum consumerd_state ust_consumerd_state
;
245 static enum consumerd_state kernel_consumerd_state
;
247 /* Load session thread information to operate. */
248 static struct load_session_thread_data
*load_info
;
251 * Section name to look for in the daemon configuration file.
253 static const char * const config_section_name
= "sessiond";
255 /* Am I root or not. Set to 1 if the daemon is running as root */
258 /* Rotation thread handle. */
259 static struct rotation_thread_handle
*rotation_thread_handle
;
262 * Stop all threads by closing the thread quit pipe.
264 static void stop_threads(void)
268 /* Stopping all threads */
269 DBG("Terminating all threads");
270 ret
= sessiond_notify_quit_pipe();
272 ERR("write error on thread quit pipe");
275 /* Dispatch thread */
276 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
277 futex_nto1_wake(&ust_cmd_queue
.futex
);
281 * Close every consumer sockets.
283 static void close_consumer_sockets(void)
287 if (kconsumer_data
.err_sock
>= 0) {
288 ret
= close(kconsumer_data
.err_sock
);
290 PERROR("kernel consumer err_sock close");
293 if (ustconsumer32_data
.err_sock
>= 0) {
294 ret
= close(ustconsumer32_data
.err_sock
);
296 PERROR("UST consumerd32 err_sock close");
299 if (ustconsumer64_data
.err_sock
>= 0) {
300 ret
= close(ustconsumer64_data
.err_sock
);
302 PERROR("UST consumerd64 err_sock close");
305 if (kconsumer_data
.cmd_sock
>= 0) {
306 ret
= close(kconsumer_data
.cmd_sock
);
308 PERROR("kernel consumer cmd_sock close");
311 if (ustconsumer32_data
.cmd_sock
>= 0) {
312 ret
= close(ustconsumer32_data
.cmd_sock
);
314 PERROR("UST consumerd32 cmd_sock close");
317 if (ustconsumer64_data
.cmd_sock
>= 0) {
318 ret
= close(ustconsumer64_data
.cmd_sock
);
320 PERROR("UST consumerd64 cmd_sock close");
323 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
324 ret
= close(kconsumer_data
.channel_monitor_pipe
);
326 PERROR("kernel consumer channel monitor pipe close");
329 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
330 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
332 PERROR("UST consumerd32 channel monitor pipe close");
335 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
336 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
338 PERROR("UST consumerd64 channel monitor pipe close");
344 * Wait on consumer process termination.
346 * Need to be called with the consumer data lock held or from a context
347 * ensuring no concurrent access to data (e.g: cleanup).
349 static void wait_consumer(struct consumer_data
*consumer_data
)
354 if (consumer_data
->pid
<= 0) {
358 DBG("Waiting for complete teardown of consumerd (PID: %d)",
360 ret
= waitpid(consumer_data
->pid
, &status
, 0);
362 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
363 } else if (!WIFEXITED(status
)) {
364 ERR("consumerd termination with error: %d",
367 consumer_data
->pid
= 0;
371 * Cleanup the session daemon's data structures.
373 static void sessiond_cleanup(void)
376 struct ltt_session_list
*session_list
= session_get_list();
378 DBG("Cleanup sessiond");
381 * Close the thread quit pipe. It has already done its job,
382 * since we are now called.
384 sessiond_close_quit_pipe();
386 ret
= remove(config
.pid_file_path
.value
);
388 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
391 DBG("Removing sessiond and consumerd content of directory %s",
392 config
.rundir
.value
);
395 DBG("Removing %s", config
.pid_file_path
.value
);
396 (void) unlink(config
.pid_file_path
.value
);
398 DBG("Removing %s", config
.agent_port_file_path
.value
);
399 (void) unlink(config
.agent_port_file_path
.value
);
402 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
403 (void) unlink(kconsumer_data
.err_unix_sock_path
);
405 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
406 (void) rmdir(config
.kconsumerd_path
.value
);
408 /* ust consumerd 32 */
409 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
410 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
412 DBG("Removing directory %s", config
.consumerd32_path
.value
);
413 (void) rmdir(config
.consumerd32_path
.value
);
415 /* ust consumerd 64 */
416 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
417 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
419 DBG("Removing directory %s", config
.consumerd64_path
.value
);
420 (void) rmdir(config
.consumerd64_path
.value
);
422 pthread_mutex_destroy(&session_list
->lock
);
424 wait_consumer(&kconsumer_data
);
425 wait_consumer(&ustconsumer64_data
);
426 wait_consumer(&ustconsumer32_data
);
428 DBG("Cleaning up all agent apps");
429 agent_app_ht_clean();
431 DBG("Closing all UST sockets");
432 ust_app_clean_list();
433 buffer_reg_destroy_registries();
435 if (is_root
&& !config
.no_kernel
) {
436 DBG2("Closing kernel fd");
437 if (kernel_tracer_fd
>= 0) {
438 ret
= close(kernel_tracer_fd
);
443 DBG("Unloading kernel modules");
444 modprobe_remove_lttng_all();
448 close_consumer_sockets();
451 load_session_destroy_data(load_info
);
456 * We do NOT rmdir rundir because there are other processes
457 * using it, for instance lttng-relayd, which can start in
458 * parallel with this teardown.
463 * Cleanup the daemon's option data structures.
465 static void sessiond_cleanup_options(void)
467 DBG("Cleaning up options");
469 sessiond_config_fini(&config
);
471 run_as_destroy_worker();
475 * Send data on a unix socket using the liblttsessiondcomm API.
477 * Return lttcomm error code.
479 static int send_unix_sock(int sock
, void *buf
, size_t len
)
481 /* Check valid length */
486 return lttcomm_send_unix_sock(sock
, buf
, len
);
490 * Free memory of a command context structure.
492 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
494 DBG("Clean command context structure");
496 if ((*cmd_ctx
)->llm
) {
497 free((*cmd_ctx
)->llm
);
499 if ((*cmd_ctx
)->lsm
) {
500 free((*cmd_ctx
)->lsm
);
508 * Notify UST applications using the shm mmap futex.
510 static int notify_ust_apps(int active
)
514 DBG("Notifying applications of session daemon state: %d", active
);
516 /* See shm.c for this call implying mmap, shm and futex calls */
517 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
518 if (wait_shm_mmap
== NULL
) {
522 /* Wake waiting process */
523 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
525 /* Apps notified successfully */
533 * Setup the outgoing data buffer for the response (llm) by allocating the
534 * right amount of memory and copying the original information from the lsm
537 * Return 0 on success, negative value on error.
539 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
540 const void *payload_buf
, size_t payload_len
,
541 const void *cmd_header_buf
, size_t cmd_header_len
)
544 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
545 const size_t cmd_header_offset
= header_len
;
546 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
547 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
549 cmd_ctx
->llm
= zmalloc(total_msg_size
);
551 if (cmd_ctx
->llm
== NULL
) {
557 /* Copy common data */
558 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
559 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
560 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
561 cmd_ctx
->llm
->data_size
= payload_len
;
562 cmd_ctx
->lttng_msg_size
= total_msg_size
;
564 /* Copy command header */
565 if (cmd_header_len
) {
566 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
572 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
581 * Version of setup_lttng_msg() without command header.
583 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
584 void *payload_buf
, size_t payload_len
)
586 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
589 * Update the kernel poll set of all channel fd available over all tracing
590 * session. Add the wakeup pipe at the end of the set.
592 static int update_kernel_poll(struct lttng_poll_event
*events
)
595 struct ltt_kernel_channel
*channel
;
596 struct ltt_session
*session
;
597 const struct ltt_session_list
*session_list
= session_get_list();
599 DBG("Updating kernel poll set");
602 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
603 if (!session_get(session
)) {
606 session_lock(session
);
607 if (session
->kernel_session
== NULL
) {
608 session_unlock(session
);
609 session_put(session
);
613 cds_list_for_each_entry(channel
,
614 &session
->kernel_session
->channel_list
.head
, list
) {
615 /* Add channel fd to the kernel poll set */
616 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
618 session_unlock(session
);
619 session_put(session
);
622 DBG("Channel fd %d added to kernel set", channel
->fd
);
624 session_unlock(session
);
626 session_unlock_list();
631 session_unlock_list();
636 * Find the channel fd from 'fd' over all tracing session. When found, check
637 * for new channel stream and send those stream fds to the kernel consumer.
639 * Useful for CPU hotplug feature.
641 static int update_kernel_stream(int fd
)
644 struct ltt_session
*session
;
645 struct ltt_kernel_session
*ksess
;
646 struct ltt_kernel_channel
*channel
;
647 const struct ltt_session_list
*session_list
= session_get_list();
649 DBG("Updating kernel streams for channel fd %d", fd
);
652 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
653 if (!session_get(session
)) {
656 session_lock(session
);
657 if (session
->kernel_session
== NULL
) {
658 session_unlock(session
);
659 session_put(session
);
662 ksess
= session
->kernel_session
;
664 cds_list_for_each_entry(channel
,
665 &ksess
->channel_list
.head
, list
) {
666 struct lttng_ht_iter iter
;
667 struct consumer_socket
*socket
;
669 if (channel
->fd
!= fd
) {
672 DBG("Channel found, updating kernel streams");
673 ret
= kernel_open_channel_stream(channel
);
677 /* Update the stream global counter */
678 ksess
->stream_count_global
+= ret
;
681 * Have we already sent fds to the consumer? If yes, it
682 * means that tracing is started so it is safe to send
683 * our updated stream fds.
685 if (ksess
->consumer_fds_sent
!= 1
686 || ksess
->consumer
== NULL
) {
692 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
693 &iter
.iter
, socket
, node
.node
) {
694 pthread_mutex_lock(socket
->lock
);
695 ret
= kernel_consumer_send_channel_streams(socket
,
697 session
->output_traces
? 1 : 0);
698 pthread_mutex_unlock(socket
->lock
);
706 session_unlock(session
);
707 session_put(session
);
709 session_unlock_list();
713 session_unlock(session
);
714 session_put(session
);
715 session_unlock_list();
720 * For each tracing session, update newly registered apps. The session list
721 * lock MUST be acquired before calling this.
723 static void update_ust_app(int app_sock
)
725 struct ltt_session
*sess
, *stmp
;
726 const struct ltt_session_list
*session_list
= session_get_list();
728 /* Consumer is in an ERROR state. Stop any application update. */
729 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
730 /* Stop the update process since the consumer is dead. */
734 /* For all tracing session(s) */
735 cds_list_for_each_entry_safe(sess
, stmp
, &session_list
->head
, list
) {
738 if (!session_get(sess
)) {
742 if (!sess
->ust_session
) {
747 assert(app_sock
>= 0);
748 app
= ust_app_find_by_sock(app_sock
);
751 * Application can be unregistered before so
752 * this is possible hence simply stopping the
755 DBG3("UST app update failed to find app sock %d",
759 ust_app_global_update(sess
->ust_session
, app
);
763 session_unlock(sess
);
769 * This thread manage event coming from the kernel.
771 * Features supported in this thread:
774 static void *thread_manage_kernel(void *data
)
776 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
777 uint32_t revents
, nb_fd
;
779 struct lttng_poll_event events
;
781 DBG("[thread] Thread manage kernel started");
783 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
786 * This first step of the while is to clean this structure which could free
787 * non NULL pointers so initialize it before the loop.
789 lttng_poll_init(&events
);
791 if (testpoint(sessiond_thread_manage_kernel
)) {
792 goto error_testpoint
;
795 health_code_update();
797 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
798 goto error_testpoint
;
802 health_code_update();
804 if (update_poll_flag
== 1) {
805 /* Clean events object. We are about to populate it again. */
806 lttng_poll_clean(&events
);
808 ret
= sessiond_set_thread_pollset(&events
, 2);
810 goto error_poll_create
;
813 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
818 /* This will add the available kernel channel if any. */
819 ret
= update_kernel_poll(&events
);
823 update_poll_flag
= 0;
826 DBG("Thread kernel polling");
828 /* Poll infinite value of time */
831 ret
= lttng_poll_wait(&events
, -1);
832 DBG("Thread kernel return from poll on %d fds",
833 LTTNG_POLL_GETNB(&events
));
837 * Restart interrupted system call.
839 if (errno
== EINTR
) {
843 } else if (ret
== 0) {
844 /* Should not happen since timeout is infinite */
845 ERR("Return value of poll is 0 with an infinite timeout.\n"
846 "This should not have happened! Continuing...");
852 for (i
= 0; i
< nb_fd
; i
++) {
853 /* Fetch once the poll data */
854 revents
= LTTNG_POLL_GETEV(&events
, i
);
855 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
857 health_code_update();
860 /* No activity for this FD (poll implementation). */
864 /* Thread quit pipe has been closed. Killing thread. */
865 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
871 /* Check for data on kernel pipe */
872 if (revents
& LPOLLIN
) {
873 if (pollfd
== kernel_poll_pipe
[0]) {
874 (void) lttng_read(kernel_poll_pipe
[0],
877 * Ret value is useless here, if this pipe gets any actions an
878 * update is required anyway.
880 update_poll_flag
= 1;
884 * New CPU detected by the kernel. Adding kernel stream to
885 * kernel session and updating the kernel consumer
887 ret
= update_kernel_stream(pollfd
);
893 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
894 update_poll_flag
= 1;
897 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
905 lttng_poll_clean(&events
);
908 utils_close_pipe(kernel_poll_pipe
);
909 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
912 ERR("Health error occurred in %s", __func__
);
913 WARN("Kernel thread died unexpectedly. "
914 "Kernel tracing can continue but CPU hotplug is disabled.");
916 health_unregister(health_sessiond
);
917 DBG("Kernel thread dying");
922 * Signal pthread condition of the consumer data that the thread.
924 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
926 pthread_mutex_lock(&data
->cond_mutex
);
929 * The state is set before signaling. It can be any value, it's the waiter
930 * job to correctly interpret this condition variable associated to the
931 * consumer pthread_cond.
933 * A value of 0 means that the corresponding thread of the consumer data
934 * was not started. 1 indicates that the thread has started and is ready
935 * for action. A negative value means that there was an error during the
938 data
->consumer_thread_is_ready
= state
;
939 (void) pthread_cond_signal(&data
->cond
);
941 pthread_mutex_unlock(&data
->cond_mutex
);
945 * This thread manage the consumer error sent back to the session daemon.
947 static void *thread_manage_consumer(void *data
)
949 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
950 uint32_t revents
, nb_fd
;
951 enum lttcomm_return_code code
;
952 struct lttng_poll_event events
;
953 struct consumer_data
*consumer_data
= data
;
954 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
956 DBG("[thread] Manage consumer started");
958 rcu_register_thread();
961 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
963 health_code_update();
966 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
967 * metadata_sock. Nothing more will be added to this poll set.
969 ret
= sessiond_set_thread_pollset(&events
, 3);
975 * The error socket here is already in a listening state which was done
976 * just before spawning this thread to avoid a race between the consumer
977 * daemon exec trying to connect and the listen() call.
979 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
984 health_code_update();
986 /* Infinite blocking call, waiting for transmission */
990 if (testpoint(sessiond_thread_manage_consumer
)) {
994 ret
= lttng_poll_wait(&events
, -1);
998 * Restart interrupted system call.
1000 if (errno
== EINTR
) {
1008 for (i
= 0; i
< nb_fd
; i
++) {
1009 /* Fetch once the poll data */
1010 revents
= LTTNG_POLL_GETEV(&events
, i
);
1011 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1013 health_code_update();
1016 /* No activity for this FD (poll implementation). */
1020 /* Thread quit pipe has been closed. Killing thread. */
1021 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1027 /* Event on the registration socket */
1028 if (pollfd
== consumer_data
->err_sock
) {
1029 if (revents
& LPOLLIN
) {
1031 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1032 ERR("consumer err socket poll error");
1035 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1041 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1047 * Set the CLOEXEC flag. Return code is useless because either way, the
1050 (void) utils_set_fd_cloexec(sock
);
1052 health_code_update();
1054 DBG2("Receiving code from consumer err_sock");
1056 /* Getting status code from kconsumerd */
1057 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1058 sizeof(enum lttcomm_return_code
));
1063 health_code_update();
1064 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1065 ERR("consumer error when waiting for SOCK_READY : %s",
1066 lttcomm_get_readable_code(-code
));
1070 /* Connect both command and metadata sockets. */
1071 consumer_data
->cmd_sock
=
1072 lttcomm_connect_unix_sock(
1073 consumer_data
->cmd_unix_sock_path
);
1074 consumer_data
->metadata_fd
=
1075 lttcomm_connect_unix_sock(
1076 consumer_data
->cmd_unix_sock_path
);
1077 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1078 PERROR("consumer connect cmd socket");
1079 /* On error, signal condition and quit. */
1080 signal_consumer_condition(consumer_data
, -1);
1084 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1086 /* Create metadata socket lock. */
1087 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1088 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1089 PERROR("zmalloc pthread mutex");
1092 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1094 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1095 DBG("Consumer metadata socket ready (fd: %d)",
1096 consumer_data
->metadata_fd
);
1099 * Remove the consumerd error sock since we've established a connection.
1101 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1106 /* Add new accepted error socket. */
1107 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1112 /* Add metadata socket that is successfully connected. */
1113 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1114 LPOLLIN
| LPOLLRDHUP
);
1119 health_code_update();
1122 * Transfer the write-end of the channel monitoring and rotate pipe
1123 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1125 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1126 if (!cmd_socket_wrapper
) {
1129 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1131 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1132 consumer_data
->channel_monitor_pipe
);
1137 /* Discard the socket wrapper as it is no longer needed. */
1138 consumer_destroy_socket(cmd_socket_wrapper
);
1139 cmd_socket_wrapper
= NULL
;
1141 /* The thread is completely initialized, signal that it is ready. */
1142 signal_consumer_condition(consumer_data
, 1);
1144 /* Infinite blocking call, waiting for transmission */
1147 health_code_update();
1149 /* Exit the thread because the thread quit pipe has been triggered. */
1151 /* Not a health error. */
1156 health_poll_entry();
1157 ret
= lttng_poll_wait(&events
, -1);
1161 * Restart interrupted system call.
1163 if (errno
== EINTR
) {
1171 for (i
= 0; i
< nb_fd
; i
++) {
1172 /* Fetch once the poll data */
1173 revents
= LTTNG_POLL_GETEV(&events
, i
);
1174 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1176 health_code_update();
1179 /* No activity for this FD (poll implementation). */
1184 * Thread quit pipe has been triggered, flag that we should stop
1185 * but continue the current loop to handle potential data from
1188 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1190 if (pollfd
== sock
) {
1191 /* Event on the consumerd socket */
1192 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1193 && !(revents
& LPOLLIN
)) {
1194 ERR("consumer err socket second poll error");
1197 health_code_update();
1198 /* Wait for any kconsumerd error */
1199 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1200 sizeof(enum lttcomm_return_code
));
1202 ERR("consumer closed the command socket");
1206 ERR("consumer return code : %s",
1207 lttcomm_get_readable_code(-code
));
1210 } else if (pollfd
== consumer_data
->metadata_fd
) {
1211 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1212 && !(revents
& LPOLLIN
)) {
1213 ERR("consumer err metadata socket second poll error");
1216 /* UST metadata requests */
1217 ret
= ust_consumer_metadata_request(
1218 &consumer_data
->metadata_sock
);
1220 ERR("Handling metadata request");
1224 /* No need for an else branch all FDs are tested prior. */
1226 health_code_update();
1232 * We lock here because we are about to close the sockets and some other
1233 * thread might be using them so get exclusive access which will abort all
1234 * other consumer command by other threads.
1236 pthread_mutex_lock(&consumer_data
->lock
);
1238 /* Immediately set the consumerd state to stopped */
1239 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1240 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1241 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1242 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1243 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1245 /* Code flow error... */
1249 if (consumer_data
->err_sock
>= 0) {
1250 ret
= close(consumer_data
->err_sock
);
1254 consumer_data
->err_sock
= -1;
1256 if (consumer_data
->cmd_sock
>= 0) {
1257 ret
= close(consumer_data
->cmd_sock
);
1261 consumer_data
->cmd_sock
= -1;
1263 if (consumer_data
->metadata_sock
.fd_ptr
&&
1264 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1265 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1277 unlink(consumer_data
->err_unix_sock_path
);
1278 unlink(consumer_data
->cmd_unix_sock_path
);
1279 pthread_mutex_unlock(&consumer_data
->lock
);
1281 /* Cleanup metadata socket mutex. */
1282 if (consumer_data
->metadata_sock
.lock
) {
1283 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1284 free(consumer_data
->metadata_sock
.lock
);
1286 lttng_poll_clean(&events
);
1288 if (cmd_socket_wrapper
) {
1289 consumer_destroy_socket(cmd_socket_wrapper
);
1294 ERR("Health error occurred in %s", __func__
);
1296 health_unregister(health_sessiond
);
1297 DBG("consumer thread cleanup completed");
1299 rcu_thread_offline();
1300 rcu_unregister_thread();
1306 * This thread receives application command sockets (FDs) on the
1307 * apps_cmd_pipe and waits (polls) on them until they are closed
1308 * or an error occurs.
1310 * At that point, it flushes the data (tracing and metadata) associated
1311 * with this application and tears down ust app sessions and other
1312 * associated data structures through ust_app_unregister().
1314 * Note that this thread never sends commands to the applications
1315 * through the command sockets; it merely listens for hang-ups
1316 * and errors on those sockets and cleans-up as they occur.
1318 static void *thread_manage_apps(void *data
)
1320 int i
, ret
, pollfd
, err
= -1;
1322 uint32_t revents
, nb_fd
;
1323 struct lttng_poll_event events
;
1325 DBG("[thread] Manage application started");
1327 rcu_register_thread();
1328 rcu_thread_online();
1330 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1332 if (testpoint(sessiond_thread_manage_apps
)) {
1333 goto error_testpoint
;
1336 health_code_update();
1338 ret
= sessiond_set_thread_pollset(&events
, 2);
1340 goto error_poll_create
;
1343 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1348 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1352 health_code_update();
1355 DBG("Apps thread polling");
1357 /* Inifinite blocking call, waiting for transmission */
1359 health_poll_entry();
1360 ret
= lttng_poll_wait(&events
, -1);
1361 DBG("Apps thread return from poll on %d fds",
1362 LTTNG_POLL_GETNB(&events
));
1366 * Restart interrupted system call.
1368 if (errno
== EINTR
) {
1376 for (i
= 0; i
< nb_fd
; i
++) {
1377 /* Fetch once the poll data */
1378 revents
= LTTNG_POLL_GETEV(&events
, i
);
1379 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1381 health_code_update();
1384 /* No activity for this FD (poll implementation). */
1388 /* Thread quit pipe has been closed. Killing thread. */
1389 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1395 /* Inspect the apps cmd pipe */
1396 if (pollfd
== apps_cmd_pipe
[0]) {
1397 if (revents
& LPOLLIN
) {
1401 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1402 if (size_ret
< sizeof(sock
)) {
1403 PERROR("read apps cmd pipe");
1407 health_code_update();
1410 * Since this is a command socket (write then read),
1411 * we only monitor the error events of the socket.
1413 ret
= lttng_poll_add(&events
, sock
,
1414 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1419 DBG("Apps with sock %d added to poll set", sock
);
1420 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1421 ERR("Apps command pipe error");
1424 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1429 * At this point, we know that a registered application made
1430 * the event at poll_wait.
1432 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1433 /* Removing from the poll set */
1434 ret
= lttng_poll_del(&events
, pollfd
);
1439 /* Socket closed on remote end. */
1440 ust_app_unregister(pollfd
);
1442 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1447 health_code_update();
1453 lttng_poll_clean(&events
);
1456 utils_close_pipe(apps_cmd_pipe
);
1457 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1460 * We don't clean the UST app hash table here since already registered
1461 * applications can still be controlled so let them be until the session
1462 * daemon dies or the applications stop.
1467 ERR("Health error occurred in %s", __func__
);
1469 health_unregister(health_sessiond
);
1470 DBG("Application communication apps thread cleanup complete");
1471 rcu_thread_offline();
1472 rcu_unregister_thread();
1477 * Send a socket to a thread This is called from the dispatch UST registration
1478 * thread once all sockets are set for the application.
1480 * The sock value can be invalid, we don't really care, the thread will handle
1481 * it and make the necessary cleanup if so.
1483 * On success, return 0 else a negative value being the errno message of the
1486 static int send_socket_to_thread(int fd
, int sock
)
1491 * It's possible that the FD is set as invalid with -1 concurrently just
1492 * before calling this function being a shutdown state of the thread.
1499 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1500 if (ret
< sizeof(sock
)) {
1501 PERROR("write apps pipe %d", fd
);
1508 /* All good. Don't send back the write positive ret value. */
1515 * Sanitize the wait queue of the dispatch registration thread meaning removing
1516 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1517 * notify socket is never received.
1519 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1521 int ret
, nb_fd
= 0, i
;
1522 unsigned int fd_added
= 0;
1523 struct lttng_poll_event events
;
1524 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1528 lttng_poll_init(&events
);
1530 /* Just skip everything for an empty queue. */
1531 if (!wait_queue
->count
) {
1535 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1540 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1541 &wait_queue
->head
, head
) {
1542 assert(wait_node
->app
);
1543 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1544 LPOLLHUP
| LPOLLERR
);
1557 * Poll but don't block so we can quickly identify the faulty events and
1558 * clean them afterwards from the wait queue.
1560 ret
= lttng_poll_wait(&events
, 0);
1566 for (i
= 0; i
< nb_fd
; i
++) {
1567 /* Get faulty FD. */
1568 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1569 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1572 /* No activity for this FD (poll implementation). */
1576 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1577 &wait_queue
->head
, head
) {
1578 if (pollfd
== wait_node
->app
->sock
&&
1579 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1580 cds_list_del(&wait_node
->head
);
1581 wait_queue
->count
--;
1582 ust_app_destroy(wait_node
->app
);
1585 * Silence warning of use-after-free in
1586 * cds_list_for_each_entry_safe which uses
1587 * __typeof__(*wait_node).
1592 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1599 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1603 lttng_poll_clean(&events
);
1607 lttng_poll_clean(&events
);
1609 ERR("Unable to sanitize wait queue");
1614 * Dispatch request from the registration threads to the application
1615 * communication thread.
1617 static void *thread_dispatch_ust_registration(void *data
)
1620 struct cds_wfcq_node
*node
;
1621 struct ust_command
*ust_cmd
= NULL
;
1622 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1623 struct ust_reg_wait_queue wait_queue
= {
1627 rcu_register_thread();
1629 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1631 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1632 goto error_testpoint
;
1635 health_code_update();
1637 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1639 DBG("[thread] Dispatch UST command started");
1642 health_code_update();
1644 /* Atomically prepare the queue futex */
1645 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1647 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1652 struct ust_app
*app
= NULL
;
1656 * Make sure we don't have node(s) that have hung up before receiving
1657 * the notify socket. This is to clean the list in order to avoid
1658 * memory leaks from notify socket that are never seen.
1660 sanitize_wait_queue(&wait_queue
);
1662 health_code_update();
1663 /* Dequeue command for registration */
1664 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1666 DBG("Woken up but nothing in the UST command queue");
1667 /* Continue thread execution */
1671 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1673 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1674 " gid:%d sock:%d name:%s (version %d.%d)",
1675 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1676 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1677 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1678 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1680 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1681 wait_node
= zmalloc(sizeof(*wait_node
));
1683 PERROR("zmalloc wait_node dispatch");
1684 ret
= close(ust_cmd
->sock
);
1686 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1688 lttng_fd_put(LTTNG_FD_APPS
, 1);
1692 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1694 /* Create application object if socket is CMD. */
1695 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1697 if (!wait_node
->app
) {
1698 ret
= close(ust_cmd
->sock
);
1700 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1702 lttng_fd_put(LTTNG_FD_APPS
, 1);
1708 * Add application to the wait queue so we can set the notify
1709 * socket before putting this object in the global ht.
1711 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1716 * We have to continue here since we don't have the notify
1717 * socket and the application MUST be added to the hash table
1718 * only at that moment.
1723 * Look for the application in the local wait queue and set the
1724 * notify socket if found.
1726 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1727 &wait_queue
.head
, head
) {
1728 health_code_update();
1729 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1730 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1731 cds_list_del(&wait_node
->head
);
1733 app
= wait_node
->app
;
1735 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1741 * With no application at this stage the received socket is
1742 * basically useless so close it before we free the cmd data
1743 * structure for good.
1746 ret
= close(ust_cmd
->sock
);
1748 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1750 lttng_fd_put(LTTNG_FD_APPS
, 1);
1757 * @session_lock_list
1759 * Lock the global session list so from the register up to the
1760 * registration done message, no thread can see the application
1761 * and change its state.
1763 session_lock_list();
1767 * Add application to the global hash table. This needs to be
1768 * done before the update to the UST registry can locate the
1773 /* Set app version. This call will print an error if needed. */
1774 (void) ust_app_version(app
);
1776 /* Send notify socket through the notify pipe. */
1777 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1781 session_unlock_list();
1783 * No notify thread, stop the UST tracing. However, this is
1784 * not an internal error of the this thread thus setting
1785 * the health error code to a normal exit.
1792 * Update newly registered application with the tracing
1793 * registry info already enabled information.
1795 update_ust_app(app
->sock
);
1798 * Don't care about return value. Let the manage apps threads
1799 * handle app unregistration upon socket close.
1801 (void) ust_app_register_done(app
);
1804 * Even if the application socket has been closed, send the app
1805 * to the thread and unregistration will take place at that
1808 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1811 session_unlock_list();
1813 * No apps. thread, stop the UST tracing. However, this is
1814 * not an internal error of the this thread thus setting
1815 * the health error code to a normal exit.
1822 session_unlock_list();
1824 } while (node
!= NULL
);
1826 health_poll_entry();
1827 /* Futex wait on queue. Blocking call on futex() */
1828 futex_nto1_wait(&ust_cmd_queue
.futex
);
1831 /* Normal exit, no error */
1835 /* Clean up wait queue. */
1836 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1837 &wait_queue
.head
, head
) {
1838 cds_list_del(&wait_node
->head
);
1843 /* Empty command queue. */
1845 /* Dequeue command for registration */
1846 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1850 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1851 ret
= close(ust_cmd
->sock
);
1853 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
1855 lttng_fd_put(LTTNG_FD_APPS
, 1);
1860 DBG("Dispatch thread dying");
1863 ERR("Health error occurred in %s", __func__
);
1865 health_unregister(health_sessiond
);
1866 rcu_unregister_thread();
1871 * This thread manage application registration.
1873 static void *thread_registration_apps(void *data
)
1875 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1876 uint32_t revents
, nb_fd
;
1877 struct lttng_poll_event events
;
1879 * Get allocated in this thread, enqueued to a global queue, dequeued and
1880 * freed in the manage apps thread.
1882 struct ust_command
*ust_cmd
= NULL
;
1884 DBG("[thread] Manage application registration started");
1886 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1888 if (testpoint(sessiond_thread_registration_apps
)) {
1889 goto error_testpoint
;
1892 ret
= lttcomm_listen_unix_sock(apps_sock
);
1898 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1899 * more will be added to this poll set.
1901 ret
= sessiond_set_thread_pollset(&events
, 2);
1903 goto error_create_poll
;
1906 /* Add the application registration socket */
1907 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1909 goto error_poll_add
;
1912 /* Notify all applications to register */
1913 ret
= notify_ust_apps(1);
1915 ERR("Failed to notify applications or create the wait shared memory.\n"
1916 "Execution continues but there might be problem for already\n"
1917 "running applications that wishes to register.");
1921 DBG("Accepting application registration");
1923 /* Inifinite blocking call, waiting for transmission */
1925 health_poll_entry();
1926 ret
= lttng_poll_wait(&events
, -1);
1930 * Restart interrupted system call.
1932 if (errno
== EINTR
) {
1940 for (i
= 0; i
< nb_fd
; i
++) {
1941 health_code_update();
1943 /* Fetch once the poll data */
1944 revents
= LTTNG_POLL_GETEV(&events
, i
);
1945 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1948 /* No activity for this FD (poll implementation). */
1952 /* Thread quit pipe has been closed. Killing thread. */
1953 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1959 /* Event on the registration socket */
1960 if (pollfd
== apps_sock
) {
1961 if (revents
& LPOLLIN
) {
1962 sock
= lttcomm_accept_unix_sock(apps_sock
);
1968 * Set socket timeout for both receiving and ending.
1969 * app_socket_timeout is in seconds, whereas
1970 * lttcomm_setsockopt_rcv_timeout and
1971 * lttcomm_setsockopt_snd_timeout expect msec as
1974 if (config
.app_socket_timeout
>= 0) {
1975 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1976 config
.app_socket_timeout
* 1000);
1977 (void) lttcomm_setsockopt_snd_timeout(sock
,
1978 config
.app_socket_timeout
* 1000);
1982 * Set the CLOEXEC flag. Return code is useless because
1983 * either way, the show must go on.
1985 (void) utils_set_fd_cloexec(sock
);
1987 /* Create UST registration command for enqueuing */
1988 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1989 if (ust_cmd
== NULL
) {
1990 PERROR("ust command zmalloc");
1999 * Using message-based transmissions to ensure we don't
2000 * have to deal with partially received messages.
2002 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2004 ERR("Exhausted file descriptors allowed for applications.");
2014 health_code_update();
2015 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2018 /* Close socket of the application. */
2023 lttng_fd_put(LTTNG_FD_APPS
, 1);
2027 health_code_update();
2029 ust_cmd
->sock
= sock
;
2032 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2033 " gid:%d sock:%d name:%s (version %d.%d)",
2034 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2035 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2036 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2037 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2040 * Lock free enqueue the registration request. The red pill
2041 * has been taken! This apps will be part of the *system*.
2043 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2046 * Wake the registration queue futex. Implicit memory
2047 * barrier with the exchange in cds_wfcq_enqueue.
2049 futex_nto1_wake(&ust_cmd_queue
.futex
);
2050 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2051 ERR("Register apps socket poll error");
2054 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2063 /* Notify that the registration thread is gone */
2066 if (apps_sock
>= 0) {
2067 ret
= close(apps_sock
);
2077 lttng_fd_put(LTTNG_FD_APPS
, 1);
2079 unlink(config
.apps_unix_sock_path
.value
);
2082 lttng_poll_clean(&events
);
2086 DBG("UST Registration thread cleanup complete");
2089 ERR("Health error occurred in %s", __func__
);
2091 health_unregister(health_sessiond
);
2097 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2098 * exec or it will fails.
2100 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2103 struct timespec timeout
;
2106 * Make sure we set the readiness flag to 0 because we are NOT ready.
2107 * This access to consumer_thread_is_ready does not need to be
2108 * protected by consumer_data.cond_mutex (yet) since the consumer
2109 * management thread has not been started at this point.
2111 consumer_data
->consumer_thread_is_ready
= 0;
2113 /* Setup pthread condition */
2114 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2117 PERROR("pthread_condattr_init consumer data");
2122 * Set the monotonic clock in order to make sure we DO NOT jump in time
2123 * between the clock_gettime() call and the timedwait call. See bug #324
2124 * for a more details and how we noticed it.
2126 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2129 PERROR("pthread_condattr_setclock consumer data");
2133 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2136 PERROR("pthread_cond_init consumer data");
2140 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2141 thread_manage_consumer
, consumer_data
);
2144 PERROR("pthread_create consumer");
2149 /* We are about to wait on a pthread condition */
2150 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2152 /* Get time for sem_timedwait absolute timeout */
2153 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2155 * Set the timeout for the condition timed wait even if the clock gettime
2156 * call fails since we might loop on that call and we want to avoid to
2157 * increment the timeout too many times.
2159 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2162 * The following loop COULD be skipped in some conditions so this is why we
2163 * set ret to 0 in order to make sure at least one round of the loop is
2169 * Loop until the condition is reached or when a timeout is reached. Note
2170 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2171 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2172 * possible. This loop does not take any chances and works with both of
2175 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2176 if (clock_ret
< 0) {
2177 PERROR("clock_gettime spawn consumer");
2178 /* Infinite wait for the consumerd thread to be ready */
2179 ret
= pthread_cond_wait(&consumer_data
->cond
,
2180 &consumer_data
->cond_mutex
);
2182 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2183 &consumer_data
->cond_mutex
, &timeout
);
2187 /* Release the pthread condition */
2188 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2192 if (ret
== ETIMEDOUT
) {
2196 * Call has timed out so we kill the kconsumerd_thread and return
2199 ERR("Condition timed out. The consumer thread was never ready."
2201 pth_ret
= pthread_cancel(consumer_data
->thread
);
2203 PERROR("pthread_cancel consumer thread");
2206 PERROR("pthread_cond_wait failed consumer thread");
2208 /* Caller is expecting a negative value on failure. */
2213 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2214 if (consumer_data
->pid
== 0) {
2215 ERR("Consumerd did not start");
2216 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2219 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2228 * Join consumer thread
2230 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2234 /* Consumer pid must be a real one. */
2235 if (consumer_data
->pid
> 0) {
2237 ret
= kill(consumer_data
->pid
, SIGTERM
);
2239 PERROR("Error killing consumer daemon");
2242 return pthread_join(consumer_data
->thread
, &status
);
2249 * Fork and exec a consumer daemon (consumerd).
2251 * Return pid if successful else -1.
2253 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2257 const char *consumer_to_use
;
2258 const char *verbosity
;
2261 DBG("Spawning consumerd");
2268 if (config
.verbose_consumer
) {
2269 verbosity
= "--verbose";
2270 } else if (lttng_opt_quiet
) {
2271 verbosity
= "--quiet";
2276 switch (consumer_data
->type
) {
2277 case LTTNG_CONSUMER_KERNEL
:
2279 * Find out which consumerd to execute. We will first try the
2280 * 64-bit path, then the sessiond's installation directory, and
2281 * fallback on the 32-bit one,
2283 DBG3("Looking for a kernel consumer at these locations:");
2284 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2285 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2286 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2287 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2288 DBG3("Found location #1");
2289 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2290 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2291 DBG3("Found location #2");
2292 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2293 } else if (config
.consumerd32_bin_path
.value
&&
2294 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2295 DBG3("Found location #3");
2296 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2298 DBG("Could not find any valid consumerd executable");
2302 DBG("Using kernel consumer at: %s", consumer_to_use
);
2303 (void) execl(consumer_to_use
,
2304 "lttng-consumerd", verbosity
, "-k",
2305 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2306 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2307 "--group", config
.tracing_group_name
.value
,
2310 case LTTNG_CONSUMER64_UST
:
2312 if (config
.consumerd64_lib_dir
.value
) {
2317 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2321 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2322 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2327 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2328 if (tmp
[0] != '\0') {
2329 strcat(tmpnew
, ":");
2330 strcat(tmpnew
, tmp
);
2332 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2339 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2340 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2341 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2342 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2343 "--group", config
.tracing_group_name
.value
,
2347 case LTTNG_CONSUMER32_UST
:
2349 if (config
.consumerd32_lib_dir
.value
) {
2354 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2358 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2359 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2364 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2365 if (tmp
[0] != '\0') {
2366 strcat(tmpnew
, ":");
2367 strcat(tmpnew
, tmp
);
2369 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2376 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2377 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2378 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2379 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2380 "--group", config
.tracing_group_name
.value
,
2385 ERR("unknown consumer type");
2389 PERROR("Consumer execl()");
2391 /* Reaching this point, we got a failure on our execl(). */
2393 } else if (pid
> 0) {
2396 PERROR("start consumer fork");
2404 * Spawn the consumerd daemon and session daemon thread.
2406 static int start_consumerd(struct consumer_data
*consumer_data
)
2411 * Set the listen() state on the socket since there is a possible race
2412 * between the exec() of the consumer daemon and this call if place in the
2413 * consumer thread. See bug #366 for more details.
2415 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2420 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2421 if (consumer_data
->pid
!= 0) {
2422 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2426 ret
= spawn_consumerd(consumer_data
);
2428 ERR("Spawning consumerd failed");
2429 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2433 /* Setting up the consumer_data pid */
2434 consumer_data
->pid
= ret
;
2435 DBG2("Consumer pid %d", consumer_data
->pid
);
2436 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2438 DBG2("Spawning consumer control thread");
2439 ret
= spawn_consumer_thread(consumer_data
);
2441 ERR("Fatal error spawning consumer control thread");
2449 /* Cleanup already created sockets on error. */
2450 if (consumer_data
->err_sock
>= 0) {
2453 err
= close(consumer_data
->err_sock
);
2455 PERROR("close consumer data error socket");
2462 * Setup necessary data for kernel tracer action.
2464 static int init_kernel_tracer(void)
2468 /* Modprobe lttng kernel modules */
2469 ret
= modprobe_lttng_control();
2474 /* Open debugfs lttng */
2475 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2476 if (kernel_tracer_fd
< 0) {
2477 DBG("Failed to open %s", module_proc_lttng
);
2481 /* Validate kernel version */
2482 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2483 &kernel_tracer_abi_version
);
2488 ret
= modprobe_lttng_data();
2493 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2500 WARN("Kernel tracer does not support buffer monitoring. "
2501 "The monitoring timer of channels in the kernel domain "
2502 "will be set to 0 (disabled).");
2505 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2509 modprobe_remove_lttng_control();
2510 ret
= close(kernel_tracer_fd
);
2514 kernel_tracer_fd
= -1;
2515 return LTTNG_ERR_KERN_VERSION
;
2518 ret
= close(kernel_tracer_fd
);
2524 modprobe_remove_lttng_control();
2527 WARN("No kernel tracer available");
2528 kernel_tracer_fd
= -1;
2530 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2532 return LTTNG_ERR_KERN_NA
;
2538 * Copy consumer output from the tracing session to the domain session. The
2539 * function also applies the right modification on a per domain basis for the
2540 * trace files destination directory.
2542 * Should *NOT* be called with RCU read-side lock held.
2544 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2547 const char *dir_name
;
2548 struct consumer_output
*consumer
;
2551 assert(session
->consumer
);
2554 case LTTNG_DOMAIN_KERNEL
:
2555 DBG3("Copying tracing session consumer output in kernel session");
2557 * XXX: We should audit the session creation and what this function
2558 * does "extra" in order to avoid a destroy since this function is used
2559 * in the domain session creation (kernel and ust) only. Same for UST
2562 if (session
->kernel_session
->consumer
) {
2563 consumer_output_put(session
->kernel_session
->consumer
);
2565 session
->kernel_session
->consumer
=
2566 consumer_copy_output(session
->consumer
);
2567 /* Ease our life a bit for the next part */
2568 consumer
= session
->kernel_session
->consumer
;
2569 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2571 case LTTNG_DOMAIN_JUL
:
2572 case LTTNG_DOMAIN_LOG4J
:
2573 case LTTNG_DOMAIN_PYTHON
:
2574 case LTTNG_DOMAIN_UST
:
2575 DBG3("Copying tracing session consumer output in UST session");
2576 if (session
->ust_session
->consumer
) {
2577 consumer_output_put(session
->ust_session
->consumer
);
2579 session
->ust_session
->consumer
=
2580 consumer_copy_output(session
->consumer
);
2581 /* Ease our life a bit for the next part */
2582 consumer
= session
->ust_session
->consumer
;
2583 dir_name
= DEFAULT_UST_TRACE_DIR
;
2586 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2590 /* Append correct directory to subdir */
2591 strncat(consumer
->subdir
, dir_name
,
2592 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2593 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2602 * Create an UST session and add it to the session ust list.
2604 * Should *NOT* be called with RCU read-side lock held.
2606 static int create_ust_session(struct ltt_session
*session
,
2607 struct lttng_domain
*domain
)
2610 struct ltt_ust_session
*lus
= NULL
;
2614 assert(session
->consumer
);
2616 switch (domain
->type
) {
2617 case LTTNG_DOMAIN_JUL
:
2618 case LTTNG_DOMAIN_LOG4J
:
2619 case LTTNG_DOMAIN_PYTHON
:
2620 case LTTNG_DOMAIN_UST
:
2623 ERR("Unknown UST domain on create session %d", domain
->type
);
2624 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2628 DBG("Creating UST session");
2630 lus
= trace_ust_create_session(session
->id
);
2632 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2636 lus
->uid
= session
->uid
;
2637 lus
->gid
= session
->gid
;
2638 lus
->output_traces
= session
->output_traces
;
2639 lus
->snapshot_mode
= session
->snapshot_mode
;
2640 lus
->live_timer_interval
= session
->live_timer
;
2641 session
->ust_session
= lus
;
2642 if (session
->shm_path
[0]) {
2643 strncpy(lus
->root_shm_path
, session
->shm_path
,
2644 sizeof(lus
->root_shm_path
));
2645 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2646 strncpy(lus
->shm_path
, session
->shm_path
,
2647 sizeof(lus
->shm_path
));
2648 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2649 strncat(lus
->shm_path
, "/ust",
2650 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2652 /* Copy session output to the newly created UST session */
2653 ret
= copy_session_consumer(domain
->type
, session
);
2654 if (ret
!= LTTNG_OK
) {
2662 session
->ust_session
= NULL
;
2667 * Create a kernel tracer session then create the default channel.
2669 static int create_kernel_session(struct ltt_session
*session
)
2673 DBG("Creating kernel session");
2675 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2677 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2681 /* Code flow safety */
2682 assert(session
->kernel_session
);
2684 /* Copy session output to the newly created Kernel session */
2685 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2686 if (ret
!= LTTNG_OK
) {
2690 session
->kernel_session
->uid
= session
->uid
;
2691 session
->kernel_session
->gid
= session
->gid
;
2692 session
->kernel_session
->output_traces
= session
->output_traces
;
2693 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2698 trace_kernel_destroy_session(session
->kernel_session
);
2699 session
->kernel_session
= NULL
;
2704 * Count number of session permitted by uid/gid.
2706 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2709 struct ltt_session
*session
;
2710 const struct ltt_session_list
*session_list
= session_get_list();
2712 DBG("Counting number of available session for UID %d GID %d",
2714 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
2715 if (!session_get(session
)) {
2718 session_lock(session
);
2719 /* Only count the sessions the user can control. */
2720 if (session_access_ok(session
, uid
, gid
) &&
2721 !session
->destroyed
) {
2724 session_unlock(session
);
2725 session_put(session
);
2730 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2731 int *sock_error
, struct lttng_event
*event
)
2734 struct lttng_userspace_probe_location
*probe_location
;
2735 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2736 struct lttng_dynamic_buffer probe_location_buffer
;
2737 struct lttng_buffer_view buffer_view
;
2740 * Create a buffer to store the serialized version of the probe
2743 lttng_dynamic_buffer_init(&probe_location_buffer
);
2744 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2745 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2747 ret
= LTTNG_ERR_NOMEM
;
2752 * Receive the probe location.
2754 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2755 probe_location_buffer
.size
);
2757 DBG("Nothing recv() from client var len data... continuing");
2759 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2760 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2764 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2765 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2768 * Extract the probe location from the serialized version.
2770 ret
= lttng_userspace_probe_location_create_from_buffer(
2771 &buffer_view
, &probe_location
);
2773 WARN("Failed to create a userspace probe location from the received buffer");
2774 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2775 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2780 * Receive the file descriptor to the target binary from the client.
2782 DBG("Receiving userspace probe target FD from client ...");
2783 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2785 DBG("Nothing recv() from client userspace probe fd... continuing");
2787 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2792 * Set the file descriptor received from the client through the unix
2793 * socket in the probe location.
2795 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
2797 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2802 * From the kernel tracer's perspective, all userspace probe event types
2803 * are all the same: a file and an offset.
2805 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
2806 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
2807 ret
= lttng_userspace_probe_location_function_set_binary_fd(
2808 probe_location
, fd
);
2810 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
2811 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
2812 probe_location
, fd
);
2815 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2820 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2824 /* Attach the probe location to the event. */
2825 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
2827 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2831 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2837 * Check if the current kernel tracer supports the session rotation feature.
2838 * Return 1 if it does, 0 otherwise.
2840 static int check_rotate_compatible(void)
2844 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2845 DBG("Kernel tracer version is not compatible with the rotation feature");
2853 * Process the command requested by the lttng client within the command
2854 * context structure. This function make sure that the return structure (llm)
2855 * is set and ready for transmission before returning.
2857 * Return any error encountered or 0 for success.
2859 * "sock" is only used for special-case var. len data.
2861 * Should *NOT* be called with RCU read-side lock held.
2863 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2867 int need_tracing_session
= 1;
2870 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2872 assert(!rcu_read_ongoing());
2876 switch (cmd_ctx
->lsm
->cmd_type
) {
2877 case LTTNG_CREATE_SESSION
:
2878 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2879 case LTTNG_CREATE_SESSION_LIVE
:
2880 case LTTNG_DESTROY_SESSION
:
2881 case LTTNG_LIST_SESSIONS
:
2882 case LTTNG_LIST_DOMAINS
:
2883 case LTTNG_START_TRACE
:
2884 case LTTNG_STOP_TRACE
:
2885 case LTTNG_DATA_PENDING
:
2886 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2887 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2888 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2889 case LTTNG_SNAPSHOT_RECORD
:
2890 case LTTNG_SAVE_SESSION
:
2891 case LTTNG_SET_SESSION_SHM_PATH
:
2892 case LTTNG_REGENERATE_METADATA
:
2893 case LTTNG_REGENERATE_STATEDUMP
:
2894 case LTTNG_REGISTER_TRIGGER
:
2895 case LTTNG_UNREGISTER_TRIGGER
:
2896 case LTTNG_ROTATE_SESSION
:
2897 case LTTNG_ROTATION_GET_INFO
:
2898 case LTTNG_ROTATION_SET_SCHEDULE
:
2899 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2906 if (config
.no_kernel
&& need_domain
2907 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2909 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2911 ret
= LTTNG_ERR_KERN_NA
;
2916 /* Deny register consumer if we already have a spawned consumer. */
2917 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2918 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2919 if (kconsumer_data
.pid
> 0) {
2920 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2921 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2924 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2928 * Check for command that don't needs to allocate a returned payload. We do
2929 * this here so we don't have to make the call for no payload at each
2932 switch(cmd_ctx
->lsm
->cmd_type
) {
2933 case LTTNG_LIST_SESSIONS
:
2934 case LTTNG_LIST_TRACEPOINTS
:
2935 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2936 case LTTNG_LIST_DOMAINS
:
2937 case LTTNG_LIST_CHANNELS
:
2938 case LTTNG_LIST_EVENTS
:
2939 case LTTNG_LIST_SYSCALLS
:
2940 case LTTNG_LIST_TRACKER_PIDS
:
2941 case LTTNG_DATA_PENDING
:
2942 case LTTNG_ROTATE_SESSION
:
2943 case LTTNG_ROTATION_GET_INFO
:
2944 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2947 /* Setup lttng message with no payload */
2948 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2950 /* This label does not try to unlock the session */
2951 goto init_setup_error
;
2955 /* Commands that DO NOT need a session. */
2956 switch (cmd_ctx
->lsm
->cmd_type
) {
2957 case LTTNG_CREATE_SESSION
:
2958 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2959 case LTTNG_CREATE_SESSION_LIVE
:
2960 case LTTNG_LIST_SESSIONS
:
2961 case LTTNG_LIST_TRACEPOINTS
:
2962 case LTTNG_LIST_SYSCALLS
:
2963 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2964 case LTTNG_SAVE_SESSION
:
2965 case LTTNG_REGISTER_TRIGGER
:
2966 case LTTNG_UNREGISTER_TRIGGER
:
2967 need_tracing_session
= 0;
2970 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2972 * We keep the session list lock across _all_ commands
2973 * for now, because the per-session lock does not
2974 * handle teardown properly.
2976 session_lock_list();
2977 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2978 if (cmd_ctx
->session
== NULL
) {
2979 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2982 /* Acquire lock for the session */
2983 session_lock(cmd_ctx
->session
);
2989 * Commands that need a valid session but should NOT create one if none
2990 * exists. Instead of creating one and destroying it when the command is
2991 * handled, process that right before so we save some round trip in useless
2994 switch (cmd_ctx
->lsm
->cmd_type
) {
2995 case LTTNG_DISABLE_CHANNEL
:
2996 case LTTNG_DISABLE_EVENT
:
2997 switch (cmd_ctx
->lsm
->domain
.type
) {
2998 case LTTNG_DOMAIN_KERNEL
:
2999 if (!cmd_ctx
->session
->kernel_session
) {
3000 ret
= LTTNG_ERR_NO_CHANNEL
;
3004 case LTTNG_DOMAIN_JUL
:
3005 case LTTNG_DOMAIN_LOG4J
:
3006 case LTTNG_DOMAIN_PYTHON
:
3007 case LTTNG_DOMAIN_UST
:
3008 if (!cmd_ctx
->session
->ust_session
) {
3009 ret
= LTTNG_ERR_NO_CHANNEL
;
3014 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3026 * Check domain type for specific "pre-action".
3028 switch (cmd_ctx
->lsm
->domain
.type
) {
3029 case LTTNG_DOMAIN_KERNEL
:
3031 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3035 /* Kernel tracer check */
3036 if (kernel_tracer_fd
== -1) {
3037 /* Basically, load kernel tracer modules */
3038 ret
= init_kernel_tracer();
3044 /* Consumer is in an ERROR state. Report back to client */
3045 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3046 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3050 /* Need a session for kernel command */
3051 if (need_tracing_session
) {
3052 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3053 ret
= create_kernel_session(cmd_ctx
->session
);
3055 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3060 /* Start the kernel consumer daemon */
3061 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3062 if (kconsumer_data
.pid
== 0 &&
3063 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3064 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3065 ret
= start_consumerd(&kconsumer_data
);
3067 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3070 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3072 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3076 * The consumer was just spawned so we need to add the socket to
3077 * the consumer output of the session if exist.
3079 ret
= consumer_create_socket(&kconsumer_data
,
3080 cmd_ctx
->session
->kernel_session
->consumer
);
3087 case LTTNG_DOMAIN_JUL
:
3088 case LTTNG_DOMAIN_LOG4J
:
3089 case LTTNG_DOMAIN_PYTHON
:
3090 case LTTNG_DOMAIN_UST
:
3092 if (!ust_app_supported()) {
3093 ret
= LTTNG_ERR_NO_UST
;
3096 /* Consumer is in an ERROR state. Report back to client */
3097 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3098 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3102 if (need_tracing_session
) {
3103 /* Create UST session if none exist. */
3104 if (cmd_ctx
->session
->ust_session
== NULL
) {
3105 ret
= create_ust_session(cmd_ctx
->session
,
3106 &cmd_ctx
->lsm
->domain
);
3107 if (ret
!= LTTNG_OK
) {
3112 /* Start the UST consumer daemons */
3114 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3115 if (config
.consumerd64_bin_path
.value
&&
3116 ustconsumer64_data
.pid
== 0 &&
3117 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3118 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3119 ret
= start_consumerd(&ustconsumer64_data
);
3121 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3122 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3126 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3127 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3129 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3133 * Setup socket for consumer 64 bit. No need for atomic access
3134 * since it was set above and can ONLY be set in this thread.
3136 ret
= consumer_create_socket(&ustconsumer64_data
,
3137 cmd_ctx
->session
->ust_session
->consumer
);
3143 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3144 if (config
.consumerd32_bin_path
.value
&&
3145 ustconsumer32_data
.pid
== 0 &&
3146 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3147 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3148 ret
= start_consumerd(&ustconsumer32_data
);
3150 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3151 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3155 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3156 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3158 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3162 * Setup socket for consumer 32 bit. No need for atomic access
3163 * since it was set above and can ONLY be set in this thread.
3165 ret
= consumer_create_socket(&ustconsumer32_data
,
3166 cmd_ctx
->session
->ust_session
->consumer
);
3178 /* Validate consumer daemon state when start/stop trace command */
3179 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3180 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3181 switch (cmd_ctx
->lsm
->domain
.type
) {
3182 case LTTNG_DOMAIN_NONE
:
3184 case LTTNG_DOMAIN_JUL
:
3185 case LTTNG_DOMAIN_LOG4J
:
3186 case LTTNG_DOMAIN_PYTHON
:
3187 case LTTNG_DOMAIN_UST
:
3188 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3189 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3193 case LTTNG_DOMAIN_KERNEL
:
3194 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3195 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3200 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3206 * Check that the UID or GID match that of the tracing session.
3207 * The root user can interact with all sessions.
3209 if (need_tracing_session
) {
3210 if (!session_access_ok(cmd_ctx
->session
,
3211 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3212 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
3213 cmd_ctx
->session
->destroyed
) {
3214 ret
= LTTNG_ERR_EPERM
;
3220 * Send relayd information to consumer as soon as we have a domain and a
3223 if (cmd_ctx
->session
&& need_domain
) {
3225 * Setup relayd if not done yet. If the relayd information was already
3226 * sent to the consumer, this call will gracefully return.
3228 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3229 if (ret
!= LTTNG_OK
) {
3234 /* Process by command type */
3235 switch (cmd_ctx
->lsm
->cmd_type
) {
3236 case LTTNG_ADD_CONTEXT
:
3239 * An LTTNG_ADD_CONTEXT command might have a supplementary
3240 * payload if the context being added is an application context.
3242 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3243 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3244 char *provider_name
= NULL
, *context_name
= NULL
;
3245 size_t provider_name_len
=
3246 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3247 size_t context_name_len
=
3248 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3250 if (provider_name_len
== 0 || context_name_len
== 0) {
3252 * Application provider and context names MUST
3255 ret
= -LTTNG_ERR_INVALID
;
3259 provider_name
= zmalloc(provider_name_len
+ 1);
3260 if (!provider_name
) {
3261 ret
= -LTTNG_ERR_NOMEM
;
3264 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3267 context_name
= zmalloc(context_name_len
+ 1);
3268 if (!context_name
) {
3269 ret
= -LTTNG_ERR_NOMEM
;
3270 goto error_add_context
;
3272 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3275 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3278 goto error_add_context
;
3281 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3284 goto error_add_context
;
3289 * cmd_add_context assumes ownership of the provider and context
3292 ret
= cmd_add_context(cmd_ctx
->session
,
3293 cmd_ctx
->lsm
->domain
.type
,
3294 cmd_ctx
->lsm
->u
.context
.channel_name
,
3295 &cmd_ctx
->lsm
->u
.context
.ctx
,
3296 kernel_poll_pipe
[1]);
3298 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3299 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3301 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3302 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3308 case LTTNG_DISABLE_CHANNEL
:
3310 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3311 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3314 case LTTNG_DISABLE_EVENT
:
3318 * FIXME: handle filter; for now we just receive the filter's
3319 * bytecode along with the filter expression which are sent by
3320 * liblttng-ctl and discard them.
3322 * This fixes an issue where the client may block while sending
3323 * the filter payload and encounter an error because the session
3324 * daemon closes the socket without ever handling this data.
3326 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3327 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3330 char data
[LTTNG_FILTER_MAX_LEN
];
3332 DBG("Discarding disable event command payload of size %zu", count
);
3334 ret
= lttcomm_recv_unix_sock(sock
, data
,
3335 count
> sizeof(data
) ? sizeof(data
) : count
);
3340 count
-= (size_t) ret
;
3343 /* FIXME: passing packed structure to non-packed pointer */
3344 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3345 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3346 &cmd_ctx
->lsm
->u
.disable
.event
);
3349 case LTTNG_ENABLE_CHANNEL
:
3351 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3352 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3353 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3354 &cmd_ctx
->lsm
->u
.channel
.chan
,
3355 kernel_poll_pipe
[1]);
3358 case LTTNG_TRACK_PID
:
3360 ret
= cmd_track_pid(cmd_ctx
->session
,
3361 cmd_ctx
->lsm
->domain
.type
,
3362 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3365 case LTTNG_UNTRACK_PID
:
3367 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3368 cmd_ctx
->lsm
->domain
.type
,
3369 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3372 case LTTNG_ENABLE_EVENT
:
3374 struct lttng_event
*ev
= NULL
;
3375 struct lttng_event_exclusion
*exclusion
= NULL
;
3376 struct lttng_filter_bytecode
*bytecode
= NULL
;
3377 char *filter_expression
= NULL
;
3379 /* Handle exclusion events and receive it from the client. */
3380 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3381 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3383 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3384 (count
* LTTNG_SYMBOL_NAME_LEN
));
3386 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3390 DBG("Receiving var len exclusion event list from client ...");
3391 exclusion
->count
= count
;
3392 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3393 count
* LTTNG_SYMBOL_NAME_LEN
);
3395 DBG("Nothing recv() from client var len data... continuing");
3398 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3403 /* Get filter expression from client. */
3404 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3405 size_t expression_len
=
3406 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3408 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3409 ret
= LTTNG_ERR_FILTER_INVAL
;
3414 filter_expression
= zmalloc(expression_len
);
3415 if (!filter_expression
) {
3417 ret
= LTTNG_ERR_FILTER_NOMEM
;
3421 /* Receive var. len. data */
3422 DBG("Receiving var len filter's expression from client ...");
3423 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3426 DBG("Nothing recv() from client var len data... continuing");
3428 free(filter_expression
);
3430 ret
= LTTNG_ERR_FILTER_INVAL
;
3435 /* Handle filter and get bytecode from client. */
3436 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3437 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3439 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3440 ret
= LTTNG_ERR_FILTER_INVAL
;
3441 free(filter_expression
);
3446 bytecode
= zmalloc(bytecode_len
);
3448 free(filter_expression
);
3450 ret
= LTTNG_ERR_FILTER_NOMEM
;
3454 /* Receive var. len. data */
3455 DBG("Receiving var len filter's bytecode from client ...");
3456 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3458 DBG("Nothing recv() from client var len data... continuing");
3460 free(filter_expression
);
3463 ret
= LTTNG_ERR_FILTER_INVAL
;
3467 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3468 free(filter_expression
);
3471 ret
= LTTNG_ERR_FILTER_INVAL
;
3476 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
3478 DBG("Failed to copy event: %s",
3479 cmd_ctx
->lsm
->u
.enable
.event
.name
);
3480 free(filter_expression
);
3483 ret
= LTTNG_ERR_NOMEM
;
3488 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
3489 /* Expect a userspace probe description. */
3490 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
3492 free(filter_expression
);
3495 lttng_event_destroy(ev
);
3500 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3501 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3503 filter_expression
, bytecode
, exclusion
,
3504 kernel_poll_pipe
[1]);
3505 lttng_event_destroy(ev
);
3508 case LTTNG_LIST_TRACEPOINTS
:
3510 struct lttng_event
*events
;
3513 session_lock_list();
3514 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3515 session_unlock_list();
3516 if (nb_events
< 0) {
3517 /* Return value is a negative lttng_error_code. */
3523 * Setup lttng message with payload size set to the event list size in
3524 * bytes and then copy list into the llm payload.
3526 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3527 sizeof(struct lttng_event
) * nb_events
);
3537 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3539 struct lttng_event_field
*fields
;
3542 session_lock_list();
3543 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3545 session_unlock_list();
3546 if (nb_fields
< 0) {
3547 /* Return value is a negative lttng_error_code. */
3553 * Setup lttng message with payload size set to the event list size in
3554 * bytes and then copy list into the llm payload.
3556 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3557 sizeof(struct lttng_event_field
) * nb_fields
);
3567 case LTTNG_LIST_SYSCALLS
:
3569 struct lttng_event
*events
;
3572 nb_events
= cmd_list_syscalls(&events
);
3573 if (nb_events
< 0) {
3574 /* Return value is a negative lttng_error_code. */
3580 * Setup lttng message with payload size set to the event list size in
3581 * bytes and then copy list into the llm payload.
3583 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3584 sizeof(struct lttng_event
) * nb_events
);
3594 case LTTNG_LIST_TRACKER_PIDS
:
3596 int32_t *pids
= NULL
;
3599 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3600 cmd_ctx
->lsm
->domain
.type
, &pids
);
3602 /* Return value is a negative lttng_error_code. */
3608 * Setup lttng message with payload size set to the event list size in
3609 * bytes and then copy list into the llm payload.
3611 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3612 sizeof(int32_t) * nr_pids
);
3622 case LTTNG_SET_CONSUMER_URI
:
3625 struct lttng_uri
*uris
;
3627 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3628 len
= nb_uri
* sizeof(struct lttng_uri
);
3631 ret
= LTTNG_ERR_INVALID
;
3635 uris
= zmalloc(len
);
3637 ret
= LTTNG_ERR_FATAL
;
3641 /* Receive variable len data */
3642 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3643 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3645 DBG("No URIs received from client... continuing");
3647 ret
= LTTNG_ERR_SESSION_FAIL
;
3652 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3654 if (ret
!= LTTNG_OK
) {
3661 case LTTNG_START_TRACE
:
3664 * On the first start, if we have a kernel session and we have
3665 * enabled time or size-based rotations, we have to make sure
3666 * the kernel tracer supports it.
3668 if (!cmd_ctx
->session
->has_been_started
&& \
3669 cmd_ctx
->session
->kernel_session
&& \
3670 (cmd_ctx
->session
->rotate_timer_period
|| \
3671 cmd_ctx
->session
->rotate_size
) && \
3672 !check_rotate_compatible()) {
3673 DBG("Kernel tracer version is not compatible with the rotation feature");
3674 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3677 ret
= cmd_start_trace(cmd_ctx
->session
);
3680 case LTTNG_STOP_TRACE
:
3682 ret
= cmd_stop_trace(cmd_ctx
->session
);
3685 case LTTNG_CREATE_SESSION
:
3688 struct lttng_uri
*uris
= NULL
;
3690 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3691 len
= nb_uri
* sizeof(struct lttng_uri
);
3694 uris
= zmalloc(len
);
3696 ret
= LTTNG_ERR_FATAL
;
3700 /* Receive variable len data */
3701 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3702 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3704 DBG("No URIs received from client... continuing");
3706 ret
= LTTNG_ERR_SESSION_FAIL
;
3711 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3712 DBG("Creating session with ONE network URI is a bad call");
3713 ret
= LTTNG_ERR_SESSION_FAIL
;
3719 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3720 &cmd_ctx
->creds
, 0);
3726 case LTTNG_DESTROY_SESSION
:
3728 ret
= cmd_destroy_session(cmd_ctx
->session
,
3729 notification_thread_handle
);
3732 case LTTNG_LIST_DOMAINS
:
3735 struct lttng_domain
*domains
= NULL
;
3737 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3739 /* Return value is a negative lttng_error_code. */
3744 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3745 nb_dom
* sizeof(struct lttng_domain
));
3755 case LTTNG_LIST_CHANNELS
:
3757 ssize_t payload_size
;
3758 struct lttng_channel
*channels
= NULL
;
3760 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3761 cmd_ctx
->session
, &channels
);
3762 if (payload_size
< 0) {
3763 /* Return value is a negative lttng_error_code. */
3764 ret
= -payload_size
;
3768 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3779 case LTTNG_LIST_EVENTS
:
3782 struct lttng_event
*events
= NULL
;
3783 struct lttcomm_event_command_header cmd_header
;
3786 memset(&cmd_header
, 0, sizeof(cmd_header
));
3787 /* Extended infos are included at the end of events */
3788 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3789 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3790 &events
, &total_size
);
3793 /* Return value is a negative lttng_error_code. */
3798 cmd_header
.nb_events
= nb_event
;
3799 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3800 &cmd_header
, sizeof(cmd_header
));
3810 case LTTNG_LIST_SESSIONS
:
3812 unsigned int nr_sessions
;
3813 void *sessions_payload
;
3816 session_lock_list();
3817 nr_sessions
= lttng_sessions_count(
3818 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3819 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3820 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3821 sessions_payload
= zmalloc(payload_len
);
3823 if (!sessions_payload
) {
3824 session_unlock_list();
3829 cmd_list_lttng_sessions(sessions_payload
,
3830 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3831 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3832 session_unlock_list();
3834 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3836 free(sessions_payload
);
3845 case LTTNG_REGISTER_CONSUMER
:
3847 struct consumer_data
*cdata
;
3849 switch (cmd_ctx
->lsm
->domain
.type
) {
3850 case LTTNG_DOMAIN_KERNEL
:
3851 cdata
= &kconsumer_data
;
3854 ret
= LTTNG_ERR_UND
;
3858 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3859 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3862 case LTTNG_DATA_PENDING
:
3865 uint8_t pending_ret_byte
;
3867 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3872 * This function may returns 0 or 1 to indicate whether or not
3873 * there is data pending. In case of error, it should return an
3874 * LTTNG_ERR code. However, some code paths may still return
3875 * a nondescript error code, which we handle by returning an
3878 if (pending_ret
== 0 || pending_ret
== 1) {
3880 * ret will be set to LTTNG_OK at the end of
3883 } else if (pending_ret
< 0) {
3884 ret
= LTTNG_ERR_UNK
;
3891 pending_ret_byte
= (uint8_t) pending_ret
;
3893 /* 1 byte to return whether or not data is pending */
3894 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3895 &pending_ret_byte
, 1);
3904 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3906 struct lttcomm_lttng_output_id reply
;
3908 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3909 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3910 if (ret
!= LTTNG_OK
) {
3914 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3920 /* Copy output list into message payload */
3924 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3926 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3927 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3930 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3933 struct lttng_snapshot_output
*outputs
= NULL
;
3935 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3936 if (nb_output
< 0) {
3941 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3942 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3943 nb_output
* sizeof(struct lttng_snapshot_output
));
3953 case LTTNG_SNAPSHOT_RECORD
:
3955 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3956 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3957 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3960 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3963 struct lttng_uri
*uris
= NULL
;
3965 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3966 len
= nb_uri
* sizeof(struct lttng_uri
);
3969 uris
= zmalloc(len
);
3971 ret
= LTTNG_ERR_FATAL
;
3975 /* Receive variable len data */
3976 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3977 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3979 DBG("No URIs received from client... continuing");
3981 ret
= LTTNG_ERR_SESSION_FAIL
;
3986 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3987 DBG("Creating session with ONE network URI is a bad call");
3988 ret
= LTTNG_ERR_SESSION_FAIL
;
3994 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3995 nb_uri
, &cmd_ctx
->creds
);
3999 case LTTNG_CREATE_SESSION_LIVE
:
4002 struct lttng_uri
*uris
= NULL
;
4004 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4005 len
= nb_uri
* sizeof(struct lttng_uri
);
4008 uris
= zmalloc(len
);
4010 ret
= LTTNG_ERR_FATAL
;
4014 /* Receive variable len data */
4015 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4016 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4018 DBG("No URIs received from client... continuing");
4020 ret
= LTTNG_ERR_SESSION_FAIL
;
4025 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4026 DBG("Creating session with ONE network URI is a bad call");
4027 ret
= LTTNG_ERR_SESSION_FAIL
;
4033 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4034 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4038 case LTTNG_SAVE_SESSION
:
4040 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4044 case LTTNG_SET_SESSION_SHM_PATH
:
4046 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4047 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4050 case LTTNG_REGENERATE_METADATA
:
4052 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4055 case LTTNG_REGENERATE_STATEDUMP
:
4057 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4060 case LTTNG_REGISTER_TRIGGER
:
4062 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4063 notification_thread_handle
);
4066 case LTTNG_UNREGISTER_TRIGGER
:
4068 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4069 notification_thread_handle
);
4072 case LTTNG_ROTATE_SESSION
:
4074 struct lttng_rotate_session_return rotate_return
;
4076 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4078 memset(&rotate_return
, 0, sizeof(rotate_return
));
4079 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4080 DBG("Kernel tracer version is not compatible with the rotation feature");
4081 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4085 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4091 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4092 sizeof(rotate_return
));
4101 case LTTNG_ROTATION_GET_INFO
:
4103 struct lttng_rotation_get_info_return get_info_return
;
4105 memset(&get_info_return
, 0, sizeof(get_info_return
));
4106 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4107 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4113 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4114 sizeof(get_info_return
));
4123 case LTTNG_ROTATION_SET_SCHEDULE
:
4126 enum lttng_rotation_schedule_type schedule_type
;
4129 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4130 DBG("Kernel tracer version does not support session rotations");
4131 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4135 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
4136 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
4137 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
4139 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4143 notification_thread_handle
);
4144 if (ret
!= LTTNG_OK
) {
4150 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
4152 struct lttng_session_list_schedules_return schedules
= {
4153 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
4154 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
4155 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
4156 .size
.value
= cmd_ctx
->session
->rotate_size
,
4159 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
4170 ret
= LTTNG_ERR_UND
;
4175 if (cmd_ctx
->llm
== NULL
) {
4176 DBG("Missing llm structure. Allocating one.");
4177 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4181 /* Set return code */
4182 cmd_ctx
->llm
->ret_code
= ret
;
4184 if (cmd_ctx
->session
) {
4185 session_unlock(cmd_ctx
->session
);
4186 session_put(cmd_ctx
->session
);
4188 if (need_tracing_session
) {
4189 session_unlock_list();
4192 assert(!rcu_read_ongoing());
4197 * This thread manage all clients request using the unix client socket for
4200 static void *thread_manage_clients(void *data
)
4202 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4204 uint32_t revents
, nb_fd
;
4205 struct command_ctx
*cmd_ctx
= NULL
;
4206 struct lttng_poll_event events
;
4208 DBG("[thread] Manage client started");
4210 rcu_register_thread();
4212 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4214 health_code_update();
4216 ret
= lttcomm_listen_unix_sock(client_sock
);
4222 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4223 * more will be added to this poll set.
4225 ret
= sessiond_set_thread_pollset(&events
, 2);
4227 goto error_create_poll
;
4230 /* Add the application registration socket */
4231 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4236 ret
= sem_post(&load_info
->message_thread_ready
);
4238 PERROR("sem_post message_thread_ready");
4243 * Wait until all support threads are initialized before accepting
4246 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4248 * If a support thread failed to launch, it may signal that
4249 * we must exit and the sessiond would never be marked as
4252 * The timeout is set to 1ms, which serves as a way to
4253 * pace down this check.
4255 ret
= sessiond_wait_for_quit_pipe(1000);
4261 * This barrier is paired with the one in sessiond_notify_ready() to
4262 * ensure that loads accessing data initialized by the other threads,
4263 * on which this thread was waiting, are not performed before this point.
4265 * Note that this could be a 'read' memory barrier, but a full barrier
4266 * is used in case the code changes. The performance implications of
4267 * this choice are minimal since this is a slow path.
4271 /* This testpoint is after we signal readiness to the parent. */
4272 if (testpoint(sessiond_thread_manage_clients
)) {
4276 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4280 health_code_update();
4282 /* Set state as running. */
4283 sessiond_set_client_thread_state(true);
4286 const struct cmd_completion_handler
*cmd_completion_handler
;
4288 DBG("Accepting client command ...");
4290 /* Inifinite blocking call, waiting for transmission */
4292 health_poll_entry();
4293 ret
= lttng_poll_wait(&events
, -1);
4297 * Restart interrupted system call.
4299 if (errno
== EINTR
) {
4307 for (i
= 0; i
< nb_fd
; i
++) {
4308 /* Fetch once the poll data */
4309 revents
= LTTNG_POLL_GETEV(&events
, i
);
4310 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4312 health_code_update();
4315 /* No activity for this FD (poll implementation). */
4319 /* Thread quit pipe has been closed. Killing thread. */
4320 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4326 /* Event on the registration socket */
4327 if (pollfd
== client_sock
) {
4328 if (revents
& LPOLLIN
) {
4330 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4331 ERR("Client socket poll error");
4334 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4340 DBG("Wait for client response");
4342 health_code_update();
4344 sock
= lttcomm_accept_unix_sock(client_sock
);
4350 * Set the CLOEXEC flag. Return code is useless because either way, the
4353 (void) utils_set_fd_cloexec(sock
);
4355 /* Set socket option for credentials retrieval */
4356 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4361 /* Allocate context command to process the client request */
4362 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4363 if (cmd_ctx
== NULL
) {
4364 PERROR("zmalloc cmd_ctx");
4368 /* Allocate data buffer for reception */
4369 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4370 if (cmd_ctx
->lsm
== NULL
) {
4371 PERROR("zmalloc cmd_ctx->lsm");
4375 cmd_ctx
->llm
= NULL
;
4376 cmd_ctx
->session
= NULL
;
4378 health_code_update();
4381 * Data is received from the lttng client. The struct
4382 * lttcomm_session_msg (lsm) contains the command and data request of
4385 DBG("Receiving data from client ...");
4386 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4387 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4389 DBG("Nothing recv() from client... continuing");
4395 clean_command_ctx(&cmd_ctx
);
4399 health_code_update();
4401 // TODO: Validate cmd_ctx including sanity check for
4402 // security purpose.
4404 rcu_thread_online();
4406 * This function dispatch the work to the kernel or userspace tracer
4407 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4408 * informations for the client. The command context struct contains
4409 * everything this function may needs.
4411 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4412 rcu_thread_offline();
4420 * TODO: Inform client somehow of the fatal error. At
4421 * this point, ret < 0 means that a zmalloc failed
4422 * (ENOMEM). Error detected but still accept
4423 * command, unless a socket error has been
4426 clean_command_ctx(&cmd_ctx
);
4430 cmd_completion_handler
= cmd_pop_completion_handler();
4431 if (cmd_completion_handler
) {
4432 enum lttng_error_code completion_code
;
4434 completion_code
= cmd_completion_handler
->run(
4435 cmd_completion_handler
->data
);
4436 if (completion_code
!= LTTNG_OK
) {
4437 clean_command_ctx(&cmd_ctx
);
4442 health_code_update();
4444 DBG("Sending response (size: %d, retcode: %s (%d))",
4445 cmd_ctx
->lttng_msg_size
,
4446 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4447 cmd_ctx
->llm
->ret_code
);
4448 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4450 ERR("Failed to send data back to client");
4453 /* End of transmission */
4460 clean_command_ctx(&cmd_ctx
);
4462 health_code_update();
4474 lttng_poll_clean(&events
);
4475 clean_command_ctx(&cmd_ctx
);
4479 unlink(config
.client_unix_sock_path
.value
);
4480 if (client_sock
>= 0) {
4481 ret
= close(client_sock
);
4489 ERR("Health error occurred in %s", __func__
);
4492 health_unregister(health_sessiond
);
4494 DBG("Client thread dying");
4496 rcu_unregister_thread();
4499 * Since we are creating the consumer threads, we own them, so we need
4500 * to join them before our thread exits.
4502 ret
= join_consumer_thread(&kconsumer_data
);
4505 PERROR("join_consumer");
4508 ret
= join_consumer_thread(&ustconsumer32_data
);
4511 PERROR("join_consumer ust32");
4514 ret
= join_consumer_thread(&ustconsumer64_data
);
4517 PERROR("join_consumer ust64");
4520 /* Set state as non-running. */
4521 sessiond_set_client_thread_state(false);
4525 static int string_match(const char *str1
, const char *str2
)
4527 return (str1
&& str2
) && !strcmp(str1
, str2
);
4531 * Take an option from the getopt output and set it in the right variable to be
4534 * Return 0 on success else a negative value.
4536 static int set_option(int opt
, const char *arg
, const char *optname
)
4540 if (string_match(optname
, "client-sock") || opt
== 'c') {
4541 if (!arg
|| *arg
== '\0') {
4545 if (lttng_is_setuid_setgid()) {
4546 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4547 "-c, --client-sock");
4549 config_string_set(&config
.client_unix_sock_path
,
4551 if (!config
.client_unix_sock_path
.value
) {
4556 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4557 if (!arg
|| *arg
== '\0') {
4561 if (lttng_is_setuid_setgid()) {
4562 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4565 config_string_set(&config
.apps_unix_sock_path
,
4567 if (!config
.apps_unix_sock_path
.value
) {
4572 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4573 config
.daemonize
= true;
4574 } else if (string_match(optname
, "background") || opt
== 'b') {
4575 config
.background
= true;
4576 } else if (string_match(optname
, "group") || opt
== 'g') {
4577 if (!arg
|| *arg
== '\0') {
4581 if (lttng_is_setuid_setgid()) {
4582 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4585 config_string_set(&config
.tracing_group_name
,
4587 if (!config
.tracing_group_name
.value
) {
4592 } else if (string_match(optname
, "help") || opt
== 'h') {
4593 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4595 ERR("Cannot show --help for `lttng-sessiond`");
4598 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4599 } else if (string_match(optname
, "version") || opt
== 'V') {
4600 fprintf(stdout
, "%s\n", VERSION
);
4602 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4603 config
.sig_parent
= true;
4604 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4605 if (!arg
|| *arg
== '\0') {
4609 if (lttng_is_setuid_setgid()) {
4610 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4611 "--kconsumerd-err-sock");
4613 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4615 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4620 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4621 if (!arg
|| *arg
== '\0') {
4625 if (lttng_is_setuid_setgid()) {
4626 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4627 "--kconsumerd-cmd-sock");
4629 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4631 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4636 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4637 if (!arg
|| *arg
== '\0') {
4641 if (lttng_is_setuid_setgid()) {
4642 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4643 "--ustconsumerd64-err-sock");
4645 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4647 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4652 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4653 if (!arg
|| *arg
== '\0') {
4657 if (lttng_is_setuid_setgid()) {
4658 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4659 "--ustconsumerd64-cmd-sock");
4661 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4663 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4668 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4669 if (!arg
|| *arg
== '\0') {
4673 if (lttng_is_setuid_setgid()) {
4674 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4675 "--ustconsumerd32-err-sock");
4677 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4679 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4684 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4685 if (!arg
|| *arg
== '\0') {
4689 if (lttng_is_setuid_setgid()) {
4690 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4691 "--ustconsumerd32-cmd-sock");
4693 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4695 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4700 } else if (string_match(optname
, "no-kernel")) {
4701 config
.no_kernel
= true;
4702 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4703 config
.quiet
= true;
4704 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4705 /* Verbose level can increase using multiple -v */
4707 /* Value obtained from config file */
4708 config
.verbose
= config_parse_value(arg
);
4710 /* -v used on command line */
4713 /* Clamp value to [0, 3] */
4714 config
.verbose
= config
.verbose
< 0 ? 0 :
4715 (config
.verbose
<= 3 ? config
.verbose
: 3);
4716 } else if (string_match(optname
, "verbose-consumer")) {
4718 config
.verbose_consumer
= config_parse_value(arg
);
4720 config
.verbose_consumer
++;
4722 } else if (string_match(optname
, "consumerd32-path")) {
4723 if (!arg
|| *arg
== '\0') {
4727 if (lttng_is_setuid_setgid()) {
4728 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4729 "--consumerd32-path");
4731 config_string_set(&config
.consumerd32_bin_path
,
4733 if (!config
.consumerd32_bin_path
.value
) {
4738 } else if (string_match(optname
, "consumerd32-libdir")) {
4739 if (!arg
|| *arg
== '\0') {
4743 if (lttng_is_setuid_setgid()) {
4744 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4745 "--consumerd32-libdir");
4747 config_string_set(&config
.consumerd32_lib_dir
,
4749 if (!config
.consumerd32_lib_dir
.value
) {
4754 } else if (string_match(optname
, "consumerd64-path")) {
4755 if (!arg
|| *arg
== '\0') {
4759 if (lttng_is_setuid_setgid()) {
4760 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4761 "--consumerd64-path");
4763 config_string_set(&config
.consumerd64_bin_path
,
4765 if (!config
.consumerd64_bin_path
.value
) {
4770 } else if (string_match(optname
, "consumerd64-libdir")) {
4771 if (!arg
|| *arg
== '\0') {
4775 if (lttng_is_setuid_setgid()) {
4776 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4777 "--consumerd64-libdir");
4779 config_string_set(&config
.consumerd64_lib_dir
,
4781 if (!config
.consumerd64_lib_dir
.value
) {
4786 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4787 if (!arg
|| *arg
== '\0') {
4791 if (lttng_is_setuid_setgid()) {
4792 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4795 config_string_set(&config
.pid_file_path
, strdup(arg
));
4796 if (!config
.pid_file_path
.value
) {
4801 } else if (string_match(optname
, "agent-tcp-port")) {
4802 if (!arg
|| *arg
== '\0') {
4806 if (lttng_is_setuid_setgid()) {
4807 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4808 "--agent-tcp-port");
4813 v
= strtoul(arg
, NULL
, 0);
4814 if (errno
!= 0 || !isdigit(arg
[0])) {
4815 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4818 if (v
== 0 || v
>= 65535) {
4819 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4822 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
4823 DBG3("Agent TCP port set to non default: %i", (int) v
);
4825 } else if (string_match(optname
, "load") || opt
== 'l') {
4826 if (!arg
|| *arg
== '\0') {
4830 if (lttng_is_setuid_setgid()) {
4831 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4834 config_string_set(&config
.load_session_path
, strdup(arg
));
4835 if (!config
.load_session_path
.value
) {
4840 } else if (string_match(optname
, "kmod-probes")) {
4841 if (!arg
|| *arg
== '\0') {
4845 if (lttng_is_setuid_setgid()) {
4846 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4849 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
4850 if (!config
.kmod_probes_list
.value
) {
4855 } else if (string_match(optname
, "extra-kmod-probes")) {
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 "--extra-kmod-probes");
4864 config_string_set(&config
.kmod_extra_probes_list
,
4866 if (!config
.kmod_extra_probes_list
.value
) {
4871 } else if (string_match(optname
, "config") || opt
== 'f') {
4872 /* This is handled in set_options() thus silent skip. */
4875 /* Unknown option or other error.
4876 * Error is printed by getopt, just return */
4881 if (ret
== -EINVAL
) {
4882 const char *opt_name
= "unknown";
4885 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4887 if (opt
== long_options
[i
].val
) {
4888 opt_name
= long_options
[i
].name
;
4893 WARN("Invalid argument provided for option \"%s\", using default value.",
4901 * config_entry_handler_cb used to handle options read from a config file.
4902 * See config_entry_handler_cb comment in common/config/session-config.h for the
4903 * return value conventions.
4905 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4909 if (!entry
|| !entry
->name
|| !entry
->value
) {
4914 /* Check if the option is to be ignored */
4915 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4916 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4921 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4924 /* Ignore if not fully matched. */
4925 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4930 * If the option takes no argument on the command line, we have to
4931 * check if the value is "true". We support non-zero numeric values,
4934 if (!long_options
[i
].has_arg
) {
4935 ret
= config_parse_value(entry
->value
);
4938 WARN("Invalid configuration value \"%s\" for option %s",
4939 entry
->value
, entry
->name
);
4941 /* False, skip boolean config option. */
4946 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4950 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4957 * daemon configuration loading and argument parsing
4959 static int set_options(int argc
, char **argv
)
4961 int ret
= 0, c
= 0, option_index
= 0;
4962 int orig_optopt
= optopt
, orig_optind
= optind
;
4964 const char *config_path
= NULL
;
4966 optstring
= utils_generate_optstring(long_options
,
4967 sizeof(long_options
) / sizeof(struct option
));
4973 /* Check for the --config option */
4974 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4975 &option_index
)) != -1) {
4979 } else if (c
!= 'f') {
4980 /* if not equal to --config option. */
4984 if (lttng_is_setuid_setgid()) {
4985 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4988 config_path
= utils_expand_path(optarg
);
4990 ERR("Failed to resolve path: %s", optarg
);
4995 ret
= config_get_section_entries(config_path
, config_section_name
,
4996 config_entry_handler
, NULL
);
4999 ERR("Invalid configuration option at line %i", ret
);
5005 /* Reset getopt's global state */
5006 optopt
= orig_optopt
;
5007 optind
= orig_optind
;
5011 * getopt_long() will not set option_index if it encounters a
5014 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5021 * Pass NULL as the long option name if popt left the index
5024 ret
= set_option(c
, optarg
,
5025 option_index
< 0 ? NULL
:
5026 long_options
[option_index
].name
);
5038 * Creates the two needed socket by the daemon.
5039 * apps_sock - The communication socket for all UST apps.
5040 * client_sock - The communication of the cli tool (lttng).
5042 static int init_daemon_socket(void)
5047 old_umask
= umask(0);
5049 /* Create client tool unix socket */
5050 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5051 if (client_sock
< 0) {
5052 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5057 /* Set the cloexec flag */
5058 ret
= utils_set_fd_cloexec(client_sock
);
5060 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5061 "Continuing but note that the consumer daemon will have a "
5062 "reference to this socket on exec()", client_sock
);
5065 /* File permission MUST be 660 */
5066 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5068 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5073 /* Create the application unix socket */
5074 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5075 if (apps_sock
< 0) {
5076 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5081 /* Set the cloexec flag */
5082 ret
= utils_set_fd_cloexec(apps_sock
);
5084 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5085 "Continuing but note that the consumer daemon will have a "
5086 "reference to this socket on exec()", apps_sock
);
5089 /* File permission MUST be 666 */
5090 ret
= chmod(config
.apps_unix_sock_path
.value
,
5091 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5093 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5098 DBG3("Session daemon client socket %d and application socket %d created",
5099 client_sock
, apps_sock
);
5107 * Create lockfile using the rundir and return its fd.
5109 static int create_lockfile(void)
5111 return utils_create_lock_file(config
.lock_file_path
.value
);
5115 * Check if the global socket is available, and if a daemon is answering at the
5116 * other side. If yes, error is returned.
5118 * Also attempts to create and hold the lock file.
5120 static int check_existing_daemon(void)
5124 /* Is there anybody out there ? */
5125 if (lttng_session_daemon_alive()) {
5130 lockfile_fd
= create_lockfile();
5131 if (lockfile_fd
< 0) {
5139 static void sessiond_cleanup_lock_file(void)
5144 * Cleanup lock file by deleting it and finaly closing it which will
5145 * release the file system lock.
5147 if (lockfile_fd
>= 0) {
5148 ret
= remove(config
.lock_file_path
.value
);
5150 PERROR("remove lock file");
5152 ret
= close(lockfile_fd
);
5154 PERROR("close lock file");
5160 * Set the tracing group gid onto the client socket.
5162 * Race window between mkdir and chown is OK because we are going from more
5163 * permissive (root.root) to less permissive (root.tracing).
5165 static int set_permissions(char *rundir
)
5170 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5172 /* Set lttng run dir */
5173 ret
= chown(rundir
, 0, gid
);
5175 ERR("Unable to set group on %s", rundir
);
5180 * Ensure all applications and tracing group can search the run
5181 * dir. Allow everyone to read the directory, since it does not
5182 * buy us anything to hide its content.
5184 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5186 ERR("Unable to set permissions on %s", rundir
);
5190 /* lttng client socket path */
5191 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5193 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5197 /* kconsumer error socket path */
5198 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5200 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5204 /* 64-bit ustconsumer error socket path */
5205 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5207 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5211 /* 32-bit ustconsumer compat32 error socket path */
5212 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5214 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5218 DBG("All permissions are set");
5224 * Create the lttng run directory needed for all global sockets and pipe.
5226 static int create_lttng_rundir(void)
5230 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5232 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5234 if (errno
!= EEXIST
) {
5235 ERR("Unable to create %s", config
.rundir
.value
);
5247 * Setup sockets and directory needed by the consumerds' communication with the
5250 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5255 switch (consumer_data
->type
) {
5256 case LTTNG_CONSUMER_KERNEL
:
5257 path
= config
.kconsumerd_path
.value
;
5259 case LTTNG_CONSUMER64_UST
:
5260 path
= config
.consumerd64_path
.value
;
5262 case LTTNG_CONSUMER32_UST
:
5263 path
= config
.consumerd32_path
.value
;
5266 ERR("Consumer type unknown");
5272 DBG2("Creating consumer directory: %s", path
);
5274 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5275 if (ret
< 0 && errno
!= EEXIST
) {
5277 ERR("Failed to create %s", path
);
5281 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5283 ERR("Unable to set group on %s", path
);
5289 /* Create the consumerd error unix socket */
5290 consumer_data
->err_sock
=
5291 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5292 if (consumer_data
->err_sock
< 0) {
5293 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5299 * Set the CLOEXEC flag. Return code is useless because either way, the
5302 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5304 PERROR("utils_set_fd_cloexec");
5305 /* continue anyway */
5308 /* File permission MUST be 660 */
5309 ret
= chmod(consumer_data
->err_unix_sock_path
,
5310 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5312 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5322 * Signal handler for the daemon
5324 * Simply stop all worker threads, leaving main() return gracefully after
5325 * joining all threads and calling cleanup().
5327 static void sighandler(int sig
)
5331 DBG("SIGINT caught");
5335 DBG("SIGTERM caught");
5339 CMM_STORE_SHARED(recv_child_signal
, 1);
5347 * Setup signal handler for :
5348 * SIGINT, SIGTERM, SIGPIPE
5350 static int set_signal_handler(void)
5353 struct sigaction sa
;
5356 if ((ret
= sigemptyset(&sigset
)) < 0) {
5357 PERROR("sigemptyset");
5361 sa
.sa_mask
= sigset
;
5364 sa
.sa_handler
= sighandler
;
5365 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5366 PERROR("sigaction");
5370 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5371 PERROR("sigaction");
5375 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5376 PERROR("sigaction");
5380 sa
.sa_handler
= SIG_IGN
;
5381 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5382 PERROR("sigaction");
5386 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5392 * Set open files limit to unlimited. This daemon can open a large number of
5393 * file descriptors in order to consume multiple kernel traces.
5395 static void set_ulimit(void)
5400 /* The kernel does not allow an infinite limit for open files */
5401 lim
.rlim_cur
= 65535;
5402 lim
.rlim_max
= 65535;
5404 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5406 PERROR("failed to set open files limit");
5410 static int write_pidfile(void)
5412 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5415 static int set_clock_plugin_env(void)
5418 char *env_value
= NULL
;
5420 if (!config
.lttng_ust_clock_plugin
.value
) {
5424 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5425 config
.lttng_ust_clock_plugin
.value
);
5431 ret
= putenv(env_value
);
5434 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5438 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5439 config
.lttng_ust_clock_plugin
.value
);
5444 static void destroy_all_sessions_and_wait(void)
5446 struct ltt_session
*session
, *tmp
;
5447 struct ltt_session_list
*session_list
;
5449 session_list
= session_get_list();
5450 DBG("Initiating destruction of all sessions");
5452 if (!session_list
) {
5457 * Ensure that the client thread is no longer accepting new commands,
5458 * which could cause new sessions to be created.
5460 sessiond_wait_client_thread_stopped();
5462 session_lock_list();
5463 /* Initiate the destruction of all sessions. */
5464 cds_list_for_each_entry_safe(session
, tmp
,
5465 &session_list
->head
, list
) {
5466 if (!session_get(session
)) {
5470 session_lock(session
);
5471 if (session
->destroyed
) {
5472 goto unlock_session
;
5474 (void) cmd_destroy_session(session
,
5475 notification_thread_handle
);
5477 session_unlock(session
);
5478 session_put(session
);
5480 session_unlock_list();
5482 /* Wait for the destruction of all sessions to complete. */
5483 DBG("Waiting for the destruction of all sessions to complete");
5484 session_list_wait_empty();
5485 DBG("Destruction of all sessions completed");
5491 int main(int argc
, char **argv
)
5493 int ret
= 0, retval
= 0;
5495 const char *env_app_timeout
;
5496 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5497 *ust64_channel_monitor_pipe
= NULL
,
5498 *kernel_channel_monitor_pipe
= NULL
;
5499 bool rotation_thread_launched
= false;
5500 bool timer_thread_launched
= false;
5501 struct lttng_thread
*ht_cleanup_thread
= NULL
;
5502 struct timer_thread_parameters timer_thread_ctx
;
5503 /* Queue of rotation jobs populated by the sessiond-timer. */
5504 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5506 init_kernel_workarounds();
5508 rcu_register_thread();
5510 if (set_signal_handler()) {
5512 goto exit_set_signal_handler
;
5515 if (timer_signal_init()) {
5517 goto exit_set_signal_handler
;
5520 page_size
= sysconf(_SC_PAGESIZE
);
5521 if (page_size
< 0) {
5522 PERROR("sysconf _SC_PAGESIZE");
5523 page_size
= LONG_MAX
;
5524 WARN("Fallback page size to %ld", page_size
);
5527 ret
= sessiond_config_init(&config
);
5530 goto exit_set_signal_handler
;
5534 * Init config from environment variables.
5535 * Command line option override env configuration per-doc. Do env first.
5537 sessiond_config_apply_env_config(&config
);
5540 * Parse arguments and load the daemon configuration file.
5542 * We have an exit_options exit path to free memory reserved by
5543 * set_options. This is needed because the rest of sessiond_cleanup()
5544 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5545 * depends on set_options.
5548 if (set_options(argc
, argv
)) {
5554 * Resolve all paths received as arguments, configuration option, or
5555 * through environment variable as absolute paths. This is necessary
5556 * since daemonizing causes the sessiond's current working directory
5559 ret
= sessiond_config_resolve_paths(&config
);
5565 lttng_opt_verbose
= config
.verbose
;
5566 lttng_opt_quiet
= config
.quiet
;
5567 kconsumer_data
.err_unix_sock_path
=
5568 config
.kconsumerd_err_unix_sock_path
.value
;
5569 kconsumer_data
.cmd_unix_sock_path
=
5570 config
.kconsumerd_cmd_unix_sock_path
.value
;
5571 ustconsumer32_data
.err_unix_sock_path
=
5572 config
.consumerd32_err_unix_sock_path
.value
;
5573 ustconsumer32_data
.cmd_unix_sock_path
=
5574 config
.consumerd32_cmd_unix_sock_path
.value
;
5575 ustconsumer64_data
.err_unix_sock_path
=
5576 config
.consumerd64_err_unix_sock_path
.value
;
5577 ustconsumer64_data
.cmd_unix_sock_path
=
5578 config
.consumerd64_cmd_unix_sock_path
.value
;
5579 set_clock_plugin_env();
5581 sessiond_config_log(&config
);
5583 if (create_lttng_rundir()) {
5588 /* Abort launch if a session daemon is already running. */
5589 if (check_existing_daemon()) {
5590 ERR("A session daemon is already running.");
5596 if (config
.daemonize
|| config
.background
) {
5599 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5600 !config
.background
);
5607 * We are in the child. Make sure all other file descriptors are
5608 * closed, in case we are called with more opened file
5609 * descriptors than the standard ones and the lock file.
5611 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5612 if (i
== lockfile_fd
) {
5619 if (run_as_create_worker(argv
[0]) < 0) {
5620 goto exit_create_run_as_worker_cleanup
;
5624 * Starting from here, we can create threads. This needs to be after
5625 * lttng_daemonize due to RCU.
5629 * Initialize the health check subsystem. This call should set the
5630 * appropriate time values.
5632 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5633 if (!health_sessiond
) {
5634 PERROR("health_app_create error");
5636 goto exit_health_sessiond_cleanup
;
5639 /* Create thread to clean up RCU hash tables */
5640 ht_cleanup_thread
= launch_ht_cleanup_thread();
5641 if (!ht_cleanup_thread
) {
5643 goto exit_ht_cleanup
;
5646 /* Create thread quit pipe */
5647 if (sessiond_init_thread_quit_pipe()) {
5649 goto exit_init_data
;
5652 /* Check if daemon is UID = 0 */
5653 is_root
= !getuid();
5655 /* Create global run dir with root access */
5657 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5658 if (!kernel_channel_monitor_pipe
) {
5659 ERR("Failed to create kernel consumer channel monitor pipe");
5661 goto exit_init_data
;
5663 kconsumer_data
.channel_monitor_pipe
=
5664 lttng_pipe_release_writefd(
5665 kernel_channel_monitor_pipe
);
5666 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5668 goto exit_init_data
;
5672 /* Set consumer initial state */
5673 kernel_consumerd_state
= CONSUMER_STOPPED
;
5674 ust_consumerd_state
= CONSUMER_STOPPED
;
5676 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5677 if (!ust32_channel_monitor_pipe
) {
5678 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5680 goto exit_init_data
;
5682 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5683 ust32_channel_monitor_pipe
);
5684 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5686 goto exit_init_data
;
5690 * The rotation_thread_timer_queue structure is shared between the
5691 * sessiond timer thread and the rotation thread. The main thread keeps
5692 * its ownership and destroys it when both threads have been joined.
5694 rotation_timer_queue
= rotation_thread_timer_queue_create();
5695 if (!rotation_timer_queue
) {
5697 goto exit_init_data
;
5699 timer_thread_ctx
.rotation_thread_job_queue
= rotation_timer_queue
;
5701 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5702 if (!ust64_channel_monitor_pipe
) {
5703 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5705 goto exit_init_data
;
5707 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5708 ust64_channel_monitor_pipe
);
5709 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5711 goto exit_init_data
;
5715 * Init UST app hash table. Alloc hash table before this point since
5716 * cleanup() can get called after that point.
5718 if (ust_app_ht_alloc()) {
5719 ERR("Failed to allocate UST app hash table");
5721 goto exit_init_data
;
5725 * Initialize agent app hash table. We allocate the hash table here
5726 * since cleanup() can get called after this point.
5728 if (agent_app_ht_alloc()) {
5729 ERR("Failed to allocate Agent app hash table");
5731 goto exit_init_data
;
5735 * These actions must be executed as root. We do that *after* setting up
5736 * the sockets path because we MUST make the check for another daemon using
5737 * those paths *before* trying to set the kernel consumer sockets and init
5741 if (set_consumer_sockets(&kconsumer_data
)) {
5743 goto exit_init_data
;
5746 /* Setup kernel tracer */
5747 if (!config
.no_kernel
) {
5748 init_kernel_tracer();
5749 if (kernel_tracer_fd
>= 0) {
5750 ret
= syscall_init_table();
5752 ERR("Unable to populate syscall table. "
5753 "Syscall tracing won't work "
5754 "for this session daemon.");
5759 /* Set ulimit for open files */
5762 /* init lttng_fd tracking must be done after set_ulimit. */
5765 if (set_consumer_sockets(&ustconsumer64_data
)) {
5767 goto exit_init_data
;
5770 if (set_consumer_sockets(&ustconsumer32_data
)) {
5772 goto exit_init_data
;
5775 /* Setup the needed unix socket */
5776 if (init_daemon_socket()) {
5778 goto exit_init_data
;
5781 /* Set credentials to socket */
5782 if (is_root
&& set_permissions(config
.rundir
.value
)) {
5784 goto exit_init_data
;
5787 /* Get parent pid if -S, --sig-parent is specified. */
5788 if (config
.sig_parent
) {
5792 /* Setup the kernel pipe for waking up the kernel thread */
5793 if (is_root
&& !config
.no_kernel
) {
5794 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5796 goto exit_init_data
;
5800 /* Setup the thread apps communication pipe. */
5801 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5803 goto exit_init_data
;
5806 /* Setup the thread apps notify communication pipe. */
5807 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5809 goto exit_init_data
;
5812 /* Initialize global buffer per UID and PID registry. */
5813 buffer_reg_init_uid_registry();
5814 buffer_reg_init_pid_registry();
5816 /* Init UST command queue. */
5817 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5821 /* Check for the application socket timeout env variable. */
5822 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5823 if (env_app_timeout
) {
5824 config
.app_socket_timeout
= atoi(env_app_timeout
);
5826 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5829 ret
= write_pidfile();
5831 ERR("Error in write_pidfile");
5833 goto exit_init_data
;
5836 /* Initialize communication library */
5838 /* Initialize TCP timeout values */
5839 lttcomm_inet_init();
5841 if (load_session_init_data(&load_info
) < 0) {
5843 goto exit_init_data
;
5845 load_info
->path
= config
.load_session_path
.value
;
5847 /* Create health-check thread. */
5848 if (!launch_health_management_thread()) {
5853 /* notification_thread_data acquires the pipes' read side. */
5854 notification_thread_handle
= notification_thread_handle_create(
5855 ust32_channel_monitor_pipe
,
5856 ust64_channel_monitor_pipe
,
5857 kernel_channel_monitor_pipe
);
5858 if (!notification_thread_handle
) {
5860 ERR("Failed to create notification thread shared data");
5861 goto exit_notification
;
5864 /* Create notification thread. */
5865 if (!launch_notification_thread(notification_thread_handle
)) {
5867 goto exit_notification
;
5871 /* Create timer thread. */
5872 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
5873 timer_thread_func
, &timer_thread_ctx
);
5876 PERROR("pthread_create timer");
5879 goto exit_notification
;
5881 timer_thread_launched
= true;
5883 /* rotation_thread_data acquires the pipes' read side. */
5884 rotation_thread_handle
= rotation_thread_handle_create(
5885 rotation_timer_queue
,
5886 notification_thread_handle
);
5887 if (!rotation_thread_handle
) {
5889 ERR("Failed to create rotation thread shared data");
5894 /* Create rotation thread. */
5895 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
5896 thread_rotation
, rotation_thread_handle
);
5899 PERROR("pthread_create rotation");
5904 rotation_thread_launched
= true;
5906 /* Create thread to manage the client socket */
5907 ret
= pthread_create(&client_thread
, default_pthread_attr(),
5908 thread_manage_clients
, (void *) NULL
);
5911 PERROR("pthread_create clients");
5917 /* Create thread to dispatch registration */
5918 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
5919 thread_dispatch_ust_registration
, (void *) NULL
);
5922 PERROR("pthread_create dispatch");
5928 /* Create thread to manage application registration. */
5929 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
5930 thread_registration_apps
, (void *) NULL
);
5933 PERROR("pthread_create registration");
5939 /* Create thread to manage application socket */
5940 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
5941 thread_manage_apps
, (void *) NULL
);
5944 PERROR("pthread_create apps");
5950 /* Create thread to manage application notify socket */
5951 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
5952 ust_thread_manage_notify
, (void *) NULL
);
5955 PERROR("pthread_create notify");
5958 goto exit_apps_notify
;
5961 /* Create agent registration thread. */
5962 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
5963 agent_thread_manage_registration
, (void *) NULL
);
5966 PERROR("pthread_create agent");
5969 goto exit_agent_reg
;
5972 /* Don't start this thread if kernel tracing is not requested nor root */
5973 if (is_root
&& !config
.no_kernel
) {
5974 /* Create kernel thread to manage kernel event */
5975 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
5976 thread_manage_kernel
, (void *) NULL
);
5979 PERROR("pthread_create kernel");
5986 /* Create session loading thread. */
5987 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
5988 thread_load_session
, load_info
);
5991 PERROR("pthread_create load_session_thread");
5994 goto exit_load_session
;
5998 * This is where we start awaiting program completion (e.g. through
5999 * signal that asks threads to teardown).
6002 ret
= pthread_join(load_session_thread
, &status
);
6005 PERROR("pthread_join load_session_thread");
6009 /* Initiate teardown once activity occurs on the quit pipe. */
6010 sessiond_wait_for_quit_pipe(-1U);
6011 destroy_all_sessions_and_wait();
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");
6078 lttng_thread_list_shutdown_orphans();
6082 * Wait for all pending call_rcu work to complete before tearing
6083 * down data structures. call_rcu worker may be trying to
6084 * perform lookups in those structures.
6088 * sessiond_cleanup() is called when no other thread is running, except
6089 * the ht_cleanup thread, which is needed to destroy the hash tables.
6091 rcu_thread_online();
6095 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6096 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6097 * the queue is empty before shutting down the clean-up thread.
6101 if (rotation_thread_handle
) {
6102 if (rotation_thread_launched
) {
6103 ret
= pthread_join(rotation_thread
, &status
);
6106 PERROR("pthread_join rotation thread");
6110 rotation_thread_handle_destroy(rotation_thread_handle
);
6113 if (timer_thread_launched
) {
6115 ret
= pthread_join(timer_thread
, &status
);
6118 PERROR("pthread_join timer thread");
6123 if (ht_cleanup_thread
) {
6124 lttng_thread_shutdown(ht_cleanup_thread
);
6125 lttng_thread_put(ht_cleanup_thread
);
6129 * After the rotation and timer thread have quit, we can safely destroy
6130 * the rotation_timer_queue.
6132 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
6134 rcu_thread_offline();
6135 rcu_unregister_thread();
6138 * The teardown of the notification system is performed after the
6139 * session daemon's teardown in order to allow it to be notified
6140 * of the active session and channels at the moment of the teardown.
6142 if (notification_thread_handle
) {
6143 notification_thread_handle_destroy(notification_thread_handle
);
6145 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6146 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6147 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6150 health_app_destroy(health_sessiond
);
6151 exit_health_sessiond_cleanup
:
6152 exit_create_run_as_worker_cleanup
:
6155 sessiond_cleanup_lock_file();
6156 sessiond_cleanup_options();
6158 exit_set_signal_handler
: