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
;
199 * UST registration command queue. This queue is tied with a futex and uses a N
200 * wakers / 1 waiter implemented and detailed in futex.c/.h
202 * The thread_registration_apps and thread_dispatch_ust_registration uses this
203 * queue along with the wait/wake scheme. The thread_manage_apps receives down
204 * the line new application socket and monitors it for any I/O error or clean
205 * close that triggers an unregistration of the application.
207 static struct ust_cmd_queue ust_cmd_queue
;
209 static const char *module_proc_lttng
= "/proc/lttng";
212 * Consumer daemon state which is changed when spawning it, killing it or in
213 * case of a fatal error.
215 enum consumerd_state
{
216 CONSUMER_STARTED
= 1,
217 CONSUMER_STOPPED
= 2,
222 * This consumer daemon state is used to validate if a client command will be
223 * able to reach the consumer. If not, the client is informed. For instance,
224 * doing a "lttng start" when the consumer state is set to ERROR will return an
225 * error to the client.
227 * The following example shows a possible race condition of this scheme:
229 * consumer thread error happens
231 * client cmd checks state -> still OK
232 * consumer thread exit, sets error
233 * client cmd try to talk to consumer
236 * However, since the consumer is a different daemon, we have no way of making
237 * sure the command will reach it safely even with this state flag. This is why
238 * we consider that up to the state validation during command processing, the
239 * command is safe. After that, we can not guarantee the correctness of the
240 * client request vis-a-vis the consumer.
242 static enum consumerd_state ust_consumerd_state
;
243 static enum consumerd_state kernel_consumerd_state
;
245 /* Load session thread information to operate. */
246 static struct load_session_thread_data
*load_info
;
249 * Section name to look for in the daemon configuration file.
251 static const char * const config_section_name
= "sessiond";
253 /* Am I root or not. Set to 1 if the daemon is running as root */
257 * Stop all threads by closing the thread quit pipe.
259 static void stop_threads(void)
263 /* Stopping all threads */
264 DBG("Terminating all threads");
265 ret
= sessiond_notify_quit_pipe();
267 ERR("write error on thread quit pipe");
270 /* Dispatch thread */
271 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
272 futex_nto1_wake(&ust_cmd_queue
.futex
);
276 * Close every consumer sockets.
278 static void close_consumer_sockets(void)
282 if (kconsumer_data
.err_sock
>= 0) {
283 ret
= close(kconsumer_data
.err_sock
);
285 PERROR("kernel consumer err_sock close");
288 if (ustconsumer32_data
.err_sock
>= 0) {
289 ret
= close(ustconsumer32_data
.err_sock
);
291 PERROR("UST consumerd32 err_sock close");
294 if (ustconsumer64_data
.err_sock
>= 0) {
295 ret
= close(ustconsumer64_data
.err_sock
);
297 PERROR("UST consumerd64 err_sock close");
300 if (kconsumer_data
.cmd_sock
>= 0) {
301 ret
= close(kconsumer_data
.cmd_sock
);
303 PERROR("kernel consumer cmd_sock close");
306 if (ustconsumer32_data
.cmd_sock
>= 0) {
307 ret
= close(ustconsumer32_data
.cmd_sock
);
309 PERROR("UST consumerd32 cmd_sock close");
312 if (ustconsumer64_data
.cmd_sock
>= 0) {
313 ret
= close(ustconsumer64_data
.cmd_sock
);
315 PERROR("UST consumerd64 cmd_sock close");
318 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
319 ret
= close(kconsumer_data
.channel_monitor_pipe
);
321 PERROR("kernel consumer channel monitor pipe close");
324 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
325 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
327 PERROR("UST consumerd32 channel monitor pipe close");
330 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
331 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
333 PERROR("UST consumerd64 channel monitor pipe close");
339 * Wait on consumer process termination.
341 * Need to be called with the consumer data lock held or from a context
342 * ensuring no concurrent access to data (e.g: cleanup).
344 static void wait_consumer(struct consumer_data
*consumer_data
)
349 if (consumer_data
->pid
<= 0) {
353 DBG("Waiting for complete teardown of consumerd (PID: %d)",
355 ret
= waitpid(consumer_data
->pid
, &status
, 0);
357 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
358 } else if (!WIFEXITED(status
)) {
359 ERR("consumerd termination with error: %d",
362 consumer_data
->pid
= 0;
366 * Cleanup the session daemon's data structures.
368 static void sessiond_cleanup(void)
371 struct ltt_session_list
*session_list
= session_get_list();
373 DBG("Cleanup sessiond");
376 * Close the thread quit pipe. It has already done its job,
377 * since we are now called.
379 sessiond_close_quit_pipe();
381 ret
= remove(config
.pid_file_path
.value
);
383 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
386 DBG("Removing sessiond and consumerd content of directory %s",
387 config
.rundir
.value
);
390 DBG("Removing %s", config
.pid_file_path
.value
);
391 (void) unlink(config
.pid_file_path
.value
);
393 DBG("Removing %s", config
.agent_port_file_path
.value
);
394 (void) unlink(config
.agent_port_file_path
.value
);
397 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
398 (void) unlink(kconsumer_data
.err_unix_sock_path
);
400 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
401 (void) rmdir(config
.kconsumerd_path
.value
);
403 /* ust consumerd 32 */
404 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
405 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
407 DBG("Removing directory %s", config
.consumerd32_path
.value
);
408 (void) rmdir(config
.consumerd32_path
.value
);
410 /* ust consumerd 64 */
411 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
412 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
414 DBG("Removing directory %s", config
.consumerd64_path
.value
);
415 (void) rmdir(config
.consumerd64_path
.value
);
417 pthread_mutex_destroy(&session_list
->lock
);
419 wait_consumer(&kconsumer_data
);
420 wait_consumer(&ustconsumer64_data
);
421 wait_consumer(&ustconsumer32_data
);
423 DBG("Cleaning up all agent apps");
424 agent_app_ht_clean();
426 DBG("Closing all UST sockets");
427 ust_app_clean_list();
428 buffer_reg_destroy_registries();
430 if (is_root
&& !config
.no_kernel
) {
431 DBG2("Closing kernel fd");
432 if (kernel_tracer_fd
>= 0) {
433 ret
= close(kernel_tracer_fd
);
438 DBG("Unloading kernel modules");
439 modprobe_remove_lttng_all();
443 close_consumer_sockets();
446 load_session_destroy_data(load_info
);
451 * We do NOT rmdir rundir because there are other processes
452 * using it, for instance lttng-relayd, which can start in
453 * parallel with this teardown.
458 * Cleanup the daemon's option data structures.
460 static void sessiond_cleanup_options(void)
462 DBG("Cleaning up options");
464 sessiond_config_fini(&config
);
466 run_as_destroy_worker();
470 * Send data on a unix socket using the liblttsessiondcomm API.
472 * Return lttcomm error code.
474 static int send_unix_sock(int sock
, void *buf
, size_t len
)
476 /* Check valid length */
481 return lttcomm_send_unix_sock(sock
, buf
, len
);
485 * Free memory of a command context structure.
487 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
489 DBG("Clean command context structure");
491 if ((*cmd_ctx
)->llm
) {
492 free((*cmd_ctx
)->llm
);
494 if ((*cmd_ctx
)->lsm
) {
495 free((*cmd_ctx
)->lsm
);
503 * Notify UST applications using the shm mmap futex.
505 static int notify_ust_apps(int active
)
509 DBG("Notifying applications of session daemon state: %d", active
);
511 /* See shm.c for this call implying mmap, shm and futex calls */
512 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
513 if (wait_shm_mmap
== NULL
) {
517 /* Wake waiting process */
518 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
520 /* Apps notified successfully */
528 * Setup the outgoing data buffer for the response (llm) by allocating the
529 * right amount of memory and copying the original information from the lsm
532 * Return 0 on success, negative value on error.
534 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
535 const void *payload_buf
, size_t payload_len
,
536 const void *cmd_header_buf
, size_t cmd_header_len
)
539 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
540 const size_t cmd_header_offset
= header_len
;
541 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
542 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
544 cmd_ctx
->llm
= zmalloc(total_msg_size
);
546 if (cmd_ctx
->llm
== NULL
) {
552 /* Copy common data */
553 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
554 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
555 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
556 cmd_ctx
->llm
->data_size
= payload_len
;
557 cmd_ctx
->lttng_msg_size
= total_msg_size
;
559 /* Copy command header */
560 if (cmd_header_len
) {
561 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
567 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
576 * Version of setup_lttng_msg() without command header.
578 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
579 void *payload_buf
, size_t payload_len
)
581 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
584 * Update the kernel poll set of all channel fd available over all tracing
585 * session. Add the wakeup pipe at the end of the set.
587 static int update_kernel_poll(struct lttng_poll_event
*events
)
590 struct ltt_kernel_channel
*channel
;
591 struct ltt_session
*session
;
592 const struct ltt_session_list
*session_list
= session_get_list();
594 DBG("Updating kernel poll set");
597 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
598 if (!session_get(session
)) {
601 session_lock(session
);
602 if (session
->kernel_session
== NULL
) {
603 session_unlock(session
);
604 session_put(session
);
608 cds_list_for_each_entry(channel
,
609 &session
->kernel_session
->channel_list
.head
, list
) {
610 /* Add channel fd to the kernel poll set */
611 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
613 session_unlock(session
);
614 session_put(session
);
617 DBG("Channel fd %d added to kernel set", channel
->fd
);
619 session_unlock(session
);
621 session_unlock_list();
626 session_unlock_list();
631 * Find the channel fd from 'fd' over all tracing session. When found, check
632 * for new channel stream and send those stream fds to the kernel consumer.
634 * Useful for CPU hotplug feature.
636 static int update_kernel_stream(int fd
)
639 struct ltt_session
*session
;
640 struct ltt_kernel_session
*ksess
;
641 struct ltt_kernel_channel
*channel
;
642 const struct ltt_session_list
*session_list
= session_get_list();
644 DBG("Updating kernel streams for channel fd %d", fd
);
647 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
648 if (!session_get(session
)) {
651 session_lock(session
);
652 if (session
->kernel_session
== NULL
) {
653 session_unlock(session
);
654 session_put(session
);
657 ksess
= session
->kernel_session
;
659 cds_list_for_each_entry(channel
,
660 &ksess
->channel_list
.head
, list
) {
661 struct lttng_ht_iter iter
;
662 struct consumer_socket
*socket
;
664 if (channel
->fd
!= fd
) {
667 DBG("Channel found, updating kernel streams");
668 ret
= kernel_open_channel_stream(channel
);
672 /* Update the stream global counter */
673 ksess
->stream_count_global
+= ret
;
676 * Have we already sent fds to the consumer? If yes, it
677 * means that tracing is started so it is safe to send
678 * our updated stream fds.
680 if (ksess
->consumer_fds_sent
!= 1
681 || ksess
->consumer
== NULL
) {
687 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
688 &iter
.iter
, socket
, node
.node
) {
689 pthread_mutex_lock(socket
->lock
);
690 ret
= kernel_consumer_send_channel_streams(socket
,
692 session
->output_traces
? 1 : 0);
693 pthread_mutex_unlock(socket
->lock
);
701 session_unlock(session
);
702 session_put(session
);
704 session_unlock_list();
708 session_unlock(session
);
709 session_put(session
);
710 session_unlock_list();
715 * For each tracing session, update newly registered apps. The session list
716 * lock MUST be acquired before calling this.
718 static void update_ust_app(int app_sock
)
720 struct ltt_session
*sess
, *stmp
;
721 const struct ltt_session_list
*session_list
= session_get_list();
723 /* Consumer is in an ERROR state. Stop any application update. */
724 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
725 /* Stop the update process since the consumer is dead. */
729 /* For all tracing session(s) */
730 cds_list_for_each_entry_safe(sess
, stmp
, &session_list
->head
, list
) {
733 if (!session_get(sess
)) {
737 if (!sess
->ust_session
) {
742 assert(app_sock
>= 0);
743 app
= ust_app_find_by_sock(app_sock
);
746 * Application can be unregistered before so
747 * this is possible hence simply stopping the
750 DBG3("UST app update failed to find app sock %d",
754 ust_app_global_update(sess
->ust_session
, app
);
758 session_unlock(sess
);
764 * This thread manage event coming from the kernel.
766 * Features supported in this thread:
769 static void *thread_manage_kernel(void *data
)
771 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
772 uint32_t revents
, nb_fd
;
774 struct lttng_poll_event events
;
776 DBG("[thread] Thread manage kernel started");
778 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
781 * This first step of the while is to clean this structure which could free
782 * non NULL pointers so initialize it before the loop.
784 lttng_poll_init(&events
);
786 if (testpoint(sessiond_thread_manage_kernel
)) {
787 goto error_testpoint
;
790 health_code_update();
792 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
793 goto error_testpoint
;
797 health_code_update();
799 if (update_poll_flag
== 1) {
800 /* Clean events object. We are about to populate it again. */
801 lttng_poll_clean(&events
);
803 ret
= sessiond_set_thread_pollset(&events
, 2);
805 goto error_poll_create
;
808 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
813 /* This will add the available kernel channel if any. */
814 ret
= update_kernel_poll(&events
);
818 update_poll_flag
= 0;
821 DBG("Thread kernel polling");
823 /* Poll infinite value of time */
826 ret
= lttng_poll_wait(&events
, -1);
827 DBG("Thread kernel return from poll on %d fds",
828 LTTNG_POLL_GETNB(&events
));
832 * Restart interrupted system call.
834 if (errno
== EINTR
) {
838 } else if (ret
== 0) {
839 /* Should not happen since timeout is infinite */
840 ERR("Return value of poll is 0 with an infinite timeout.\n"
841 "This should not have happened! Continuing...");
847 for (i
= 0; i
< nb_fd
; i
++) {
848 /* Fetch once the poll data */
849 revents
= LTTNG_POLL_GETEV(&events
, i
);
850 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
852 health_code_update();
855 /* No activity for this FD (poll implementation). */
859 /* Thread quit pipe has been closed. Killing thread. */
860 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
866 /* Check for data on kernel pipe */
867 if (revents
& LPOLLIN
) {
868 if (pollfd
== kernel_poll_pipe
[0]) {
869 (void) lttng_read(kernel_poll_pipe
[0],
872 * Ret value is useless here, if this pipe gets any actions an
873 * update is required anyway.
875 update_poll_flag
= 1;
879 * New CPU detected by the kernel. Adding kernel stream to
880 * kernel session and updating the kernel consumer
882 ret
= update_kernel_stream(pollfd
);
888 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
889 update_poll_flag
= 1;
892 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
900 lttng_poll_clean(&events
);
903 utils_close_pipe(kernel_poll_pipe
);
904 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
907 ERR("Health error occurred in %s", __func__
);
908 WARN("Kernel thread died unexpectedly. "
909 "Kernel tracing can continue but CPU hotplug is disabled.");
911 health_unregister(health_sessiond
);
912 DBG("Kernel thread dying");
917 * Signal pthread condition of the consumer data that the thread.
919 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
921 pthread_mutex_lock(&data
->cond_mutex
);
924 * The state is set before signaling. It can be any value, it's the waiter
925 * job to correctly interpret this condition variable associated to the
926 * consumer pthread_cond.
928 * A value of 0 means that the corresponding thread of the consumer data
929 * was not started. 1 indicates that the thread has started and is ready
930 * for action. A negative value means that there was an error during the
933 data
->consumer_thread_is_ready
= state
;
934 (void) pthread_cond_signal(&data
->cond
);
936 pthread_mutex_unlock(&data
->cond_mutex
);
940 * This thread manage the consumer error sent back to the session daemon.
942 static void *thread_manage_consumer(void *data
)
944 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
945 uint32_t revents
, nb_fd
;
946 enum lttcomm_return_code code
;
947 struct lttng_poll_event events
;
948 struct consumer_data
*consumer_data
= data
;
949 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
951 DBG("[thread] Manage consumer started");
953 rcu_register_thread();
956 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
958 health_code_update();
961 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
962 * metadata_sock. Nothing more will be added to this poll set.
964 ret
= sessiond_set_thread_pollset(&events
, 3);
970 * The error socket here is already in a listening state which was done
971 * just before spawning this thread to avoid a race between the consumer
972 * daemon exec trying to connect and the listen() call.
974 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
979 health_code_update();
981 /* Infinite blocking call, waiting for transmission */
985 if (testpoint(sessiond_thread_manage_consumer
)) {
989 ret
= lttng_poll_wait(&events
, -1);
993 * Restart interrupted system call.
995 if (errno
== EINTR
) {
1003 for (i
= 0; i
< nb_fd
; i
++) {
1004 /* Fetch once the poll data */
1005 revents
= LTTNG_POLL_GETEV(&events
, i
);
1006 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1008 health_code_update();
1011 /* No activity for this FD (poll implementation). */
1015 /* Thread quit pipe has been closed. Killing thread. */
1016 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1022 /* Event on the registration socket */
1023 if (pollfd
== consumer_data
->err_sock
) {
1024 if (revents
& LPOLLIN
) {
1026 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1027 ERR("consumer err socket poll error");
1030 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1036 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1042 * Set the CLOEXEC flag. Return code is useless because either way, the
1045 (void) utils_set_fd_cloexec(sock
);
1047 health_code_update();
1049 DBG2("Receiving code from consumer err_sock");
1051 /* Getting status code from kconsumerd */
1052 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1053 sizeof(enum lttcomm_return_code
));
1058 health_code_update();
1059 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1060 ERR("consumer error when waiting for SOCK_READY : %s",
1061 lttcomm_get_readable_code(-code
));
1065 /* Connect both command and metadata sockets. */
1066 consumer_data
->cmd_sock
=
1067 lttcomm_connect_unix_sock(
1068 consumer_data
->cmd_unix_sock_path
);
1069 consumer_data
->metadata_fd
=
1070 lttcomm_connect_unix_sock(
1071 consumer_data
->cmd_unix_sock_path
);
1072 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1073 PERROR("consumer connect cmd socket");
1074 /* On error, signal condition and quit. */
1075 signal_consumer_condition(consumer_data
, -1);
1079 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1081 /* Create metadata socket lock. */
1082 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1083 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1084 PERROR("zmalloc pthread mutex");
1087 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1089 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1090 DBG("Consumer metadata socket ready (fd: %d)",
1091 consumer_data
->metadata_fd
);
1094 * Remove the consumerd error sock since we've established a connection.
1096 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1101 /* Add new accepted error socket. */
1102 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1107 /* Add metadata socket that is successfully connected. */
1108 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1109 LPOLLIN
| LPOLLRDHUP
);
1114 health_code_update();
1117 * Transfer the write-end of the channel monitoring and rotate pipe
1118 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1120 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1121 if (!cmd_socket_wrapper
) {
1124 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1126 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1127 consumer_data
->channel_monitor_pipe
);
1132 /* Discard the socket wrapper as it is no longer needed. */
1133 consumer_destroy_socket(cmd_socket_wrapper
);
1134 cmd_socket_wrapper
= NULL
;
1136 /* The thread is completely initialized, signal that it is ready. */
1137 signal_consumer_condition(consumer_data
, 1);
1139 /* Infinite blocking call, waiting for transmission */
1142 health_code_update();
1144 /* Exit the thread because the thread quit pipe has been triggered. */
1146 /* Not a health error. */
1151 health_poll_entry();
1152 ret
= lttng_poll_wait(&events
, -1);
1156 * Restart interrupted system call.
1158 if (errno
== EINTR
) {
1166 for (i
= 0; i
< nb_fd
; i
++) {
1167 /* Fetch once the poll data */
1168 revents
= LTTNG_POLL_GETEV(&events
, i
);
1169 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1171 health_code_update();
1174 /* No activity for this FD (poll implementation). */
1179 * Thread quit pipe has been triggered, flag that we should stop
1180 * but continue the current loop to handle potential data from
1183 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1185 if (pollfd
== sock
) {
1186 /* Event on the consumerd socket */
1187 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1188 && !(revents
& LPOLLIN
)) {
1189 ERR("consumer err socket second poll error");
1192 health_code_update();
1193 /* Wait for any kconsumerd error */
1194 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1195 sizeof(enum lttcomm_return_code
));
1197 ERR("consumer closed the command socket");
1201 ERR("consumer return code : %s",
1202 lttcomm_get_readable_code(-code
));
1205 } else if (pollfd
== consumer_data
->metadata_fd
) {
1206 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1207 && !(revents
& LPOLLIN
)) {
1208 ERR("consumer err metadata socket second poll error");
1211 /* UST metadata requests */
1212 ret
= ust_consumer_metadata_request(
1213 &consumer_data
->metadata_sock
);
1215 ERR("Handling metadata request");
1219 /* No need for an else branch all FDs are tested prior. */
1221 health_code_update();
1227 * We lock here because we are about to close the sockets and some other
1228 * thread might be using them so get exclusive access which will abort all
1229 * other consumer command by other threads.
1231 pthread_mutex_lock(&consumer_data
->lock
);
1233 /* Immediately set the consumerd state to stopped */
1234 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1235 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1236 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1237 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1238 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1240 /* Code flow error... */
1244 if (consumer_data
->err_sock
>= 0) {
1245 ret
= close(consumer_data
->err_sock
);
1249 consumer_data
->err_sock
= -1;
1251 if (consumer_data
->cmd_sock
>= 0) {
1252 ret
= close(consumer_data
->cmd_sock
);
1256 consumer_data
->cmd_sock
= -1;
1258 if (consumer_data
->metadata_sock
.fd_ptr
&&
1259 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1260 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1272 unlink(consumer_data
->err_unix_sock_path
);
1273 unlink(consumer_data
->cmd_unix_sock_path
);
1274 pthread_mutex_unlock(&consumer_data
->lock
);
1276 /* Cleanup metadata socket mutex. */
1277 if (consumer_data
->metadata_sock
.lock
) {
1278 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1279 free(consumer_data
->metadata_sock
.lock
);
1281 lttng_poll_clean(&events
);
1283 if (cmd_socket_wrapper
) {
1284 consumer_destroy_socket(cmd_socket_wrapper
);
1289 ERR("Health error occurred in %s", __func__
);
1291 health_unregister(health_sessiond
);
1292 DBG("consumer thread cleanup completed");
1294 rcu_thread_offline();
1295 rcu_unregister_thread();
1301 * This thread receives application command sockets (FDs) on the
1302 * apps_cmd_pipe and waits (polls) on them until they are closed
1303 * or an error occurs.
1305 * At that point, it flushes the data (tracing and metadata) associated
1306 * with this application and tears down ust app sessions and other
1307 * associated data structures through ust_app_unregister().
1309 * Note that this thread never sends commands to the applications
1310 * through the command sockets; it merely listens for hang-ups
1311 * and errors on those sockets and cleans-up as they occur.
1313 static void *thread_manage_apps(void *data
)
1315 int i
, ret
, pollfd
, err
= -1;
1317 uint32_t revents
, nb_fd
;
1318 struct lttng_poll_event events
;
1320 DBG("[thread] Manage application started");
1322 rcu_register_thread();
1323 rcu_thread_online();
1325 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1327 if (testpoint(sessiond_thread_manage_apps
)) {
1328 goto error_testpoint
;
1331 health_code_update();
1333 ret
= sessiond_set_thread_pollset(&events
, 2);
1335 goto error_poll_create
;
1338 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1343 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1347 health_code_update();
1350 DBG("Apps thread polling");
1352 /* Inifinite blocking call, waiting for transmission */
1354 health_poll_entry();
1355 ret
= lttng_poll_wait(&events
, -1);
1356 DBG("Apps thread return from poll on %d fds",
1357 LTTNG_POLL_GETNB(&events
));
1361 * Restart interrupted system call.
1363 if (errno
== EINTR
) {
1371 for (i
= 0; i
< nb_fd
; i
++) {
1372 /* Fetch once the poll data */
1373 revents
= LTTNG_POLL_GETEV(&events
, i
);
1374 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1376 health_code_update();
1379 /* No activity for this FD (poll implementation). */
1383 /* Thread quit pipe has been closed. Killing thread. */
1384 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1390 /* Inspect the apps cmd pipe */
1391 if (pollfd
== apps_cmd_pipe
[0]) {
1392 if (revents
& LPOLLIN
) {
1396 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1397 if (size_ret
< sizeof(sock
)) {
1398 PERROR("read apps cmd pipe");
1402 health_code_update();
1405 * Since this is a command socket (write then read),
1406 * we only monitor the error events of the socket.
1408 ret
= lttng_poll_add(&events
, sock
,
1409 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1414 DBG("Apps with sock %d added to poll set", sock
);
1415 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1416 ERR("Apps command pipe error");
1419 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1424 * At this point, we know that a registered application made
1425 * the event at poll_wait.
1427 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1428 /* Removing from the poll set */
1429 ret
= lttng_poll_del(&events
, pollfd
);
1434 /* Socket closed on remote end. */
1435 ust_app_unregister(pollfd
);
1437 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1442 health_code_update();
1448 lttng_poll_clean(&events
);
1451 utils_close_pipe(apps_cmd_pipe
);
1452 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1455 * We don't clean the UST app hash table here since already registered
1456 * applications can still be controlled so let them be until the session
1457 * daemon dies or the applications stop.
1462 ERR("Health error occurred in %s", __func__
);
1464 health_unregister(health_sessiond
);
1465 DBG("Application communication apps thread cleanup complete");
1466 rcu_thread_offline();
1467 rcu_unregister_thread();
1472 * Send a socket to a thread This is called from the dispatch UST registration
1473 * thread once all sockets are set for the application.
1475 * The sock value can be invalid, we don't really care, the thread will handle
1476 * it and make the necessary cleanup if so.
1478 * On success, return 0 else a negative value being the errno message of the
1481 static int send_socket_to_thread(int fd
, int sock
)
1486 * It's possible that the FD is set as invalid with -1 concurrently just
1487 * before calling this function being a shutdown state of the thread.
1494 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1495 if (ret
< sizeof(sock
)) {
1496 PERROR("write apps pipe %d", fd
);
1503 /* All good. Don't send back the write positive ret value. */
1510 * Sanitize the wait queue of the dispatch registration thread meaning removing
1511 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1512 * notify socket is never received.
1514 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1516 int ret
, nb_fd
= 0, i
;
1517 unsigned int fd_added
= 0;
1518 struct lttng_poll_event events
;
1519 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1523 lttng_poll_init(&events
);
1525 /* Just skip everything for an empty queue. */
1526 if (!wait_queue
->count
) {
1530 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1535 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1536 &wait_queue
->head
, head
) {
1537 assert(wait_node
->app
);
1538 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1539 LPOLLHUP
| LPOLLERR
);
1552 * Poll but don't block so we can quickly identify the faulty events and
1553 * clean them afterwards from the wait queue.
1555 ret
= lttng_poll_wait(&events
, 0);
1561 for (i
= 0; i
< nb_fd
; i
++) {
1562 /* Get faulty FD. */
1563 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1564 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1567 /* No activity for this FD (poll implementation). */
1571 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1572 &wait_queue
->head
, head
) {
1573 if (pollfd
== wait_node
->app
->sock
&&
1574 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1575 cds_list_del(&wait_node
->head
);
1576 wait_queue
->count
--;
1577 ust_app_destroy(wait_node
->app
);
1580 * Silence warning of use-after-free in
1581 * cds_list_for_each_entry_safe which uses
1582 * __typeof__(*wait_node).
1587 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1594 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1598 lttng_poll_clean(&events
);
1602 lttng_poll_clean(&events
);
1604 ERR("Unable to sanitize wait queue");
1609 * Dispatch request from the registration threads to the application
1610 * communication thread.
1612 static void *thread_dispatch_ust_registration(void *data
)
1615 struct cds_wfcq_node
*node
;
1616 struct ust_command
*ust_cmd
= NULL
;
1617 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1618 struct ust_reg_wait_queue wait_queue
= {
1622 rcu_register_thread();
1624 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1626 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1627 goto error_testpoint
;
1630 health_code_update();
1632 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1634 DBG("[thread] Dispatch UST command started");
1637 health_code_update();
1639 /* Atomically prepare the queue futex */
1640 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1642 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1647 struct ust_app
*app
= NULL
;
1651 * Make sure we don't have node(s) that have hung up before receiving
1652 * the notify socket. This is to clean the list in order to avoid
1653 * memory leaks from notify socket that are never seen.
1655 sanitize_wait_queue(&wait_queue
);
1657 health_code_update();
1658 /* Dequeue command for registration */
1659 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1661 DBG("Woken up but nothing in the UST command queue");
1662 /* Continue thread execution */
1666 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1668 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1669 " gid:%d sock:%d name:%s (version %d.%d)",
1670 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1671 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1672 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1673 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1675 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1676 wait_node
= zmalloc(sizeof(*wait_node
));
1678 PERROR("zmalloc wait_node dispatch");
1679 ret
= close(ust_cmd
->sock
);
1681 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1683 lttng_fd_put(LTTNG_FD_APPS
, 1);
1687 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1689 /* Create application object if socket is CMD. */
1690 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1692 if (!wait_node
->app
) {
1693 ret
= close(ust_cmd
->sock
);
1695 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1697 lttng_fd_put(LTTNG_FD_APPS
, 1);
1703 * Add application to the wait queue so we can set the notify
1704 * socket before putting this object in the global ht.
1706 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1711 * We have to continue here since we don't have the notify
1712 * socket and the application MUST be added to the hash table
1713 * only at that moment.
1718 * Look for the application in the local wait queue and set the
1719 * notify socket if found.
1721 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1722 &wait_queue
.head
, head
) {
1723 health_code_update();
1724 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1725 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1726 cds_list_del(&wait_node
->head
);
1728 app
= wait_node
->app
;
1730 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1736 * With no application at this stage the received socket is
1737 * basically useless so close it before we free the cmd data
1738 * structure for good.
1741 ret
= close(ust_cmd
->sock
);
1743 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1745 lttng_fd_put(LTTNG_FD_APPS
, 1);
1752 * @session_lock_list
1754 * Lock the global session list so from the register up to the
1755 * registration done message, no thread can see the application
1756 * and change its state.
1758 session_lock_list();
1762 * Add application to the global hash table. This needs to be
1763 * done before the update to the UST registry can locate the
1768 /* Set app version. This call will print an error if needed. */
1769 (void) ust_app_version(app
);
1771 /* Send notify socket through the notify pipe. */
1772 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1776 session_unlock_list();
1778 * No notify thread, stop the UST tracing. However, this is
1779 * not an internal error of the this thread thus setting
1780 * the health error code to a normal exit.
1787 * Update newly registered application with the tracing
1788 * registry info already enabled information.
1790 update_ust_app(app
->sock
);
1793 * Don't care about return value. Let the manage apps threads
1794 * handle app unregistration upon socket close.
1796 (void) ust_app_register_done(app
);
1799 * Even if the application socket has been closed, send the app
1800 * to the thread and unregistration will take place at that
1803 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1806 session_unlock_list();
1808 * No apps. thread, stop the UST tracing. However, this is
1809 * not an internal error of the this thread thus setting
1810 * the health error code to a normal exit.
1817 session_unlock_list();
1819 } while (node
!= NULL
);
1821 health_poll_entry();
1822 /* Futex wait on queue. Blocking call on futex() */
1823 futex_nto1_wait(&ust_cmd_queue
.futex
);
1826 /* Normal exit, no error */
1830 /* Clean up wait queue. */
1831 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1832 &wait_queue
.head
, head
) {
1833 cds_list_del(&wait_node
->head
);
1838 /* Empty command queue. */
1840 /* Dequeue command for registration */
1841 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1845 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1846 ret
= close(ust_cmd
->sock
);
1848 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
1850 lttng_fd_put(LTTNG_FD_APPS
, 1);
1855 DBG("Dispatch thread dying");
1858 ERR("Health error occurred in %s", __func__
);
1860 health_unregister(health_sessiond
);
1861 rcu_unregister_thread();
1866 * This thread manage application registration.
1868 static void *thread_registration_apps(void *data
)
1870 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1871 uint32_t revents
, nb_fd
;
1872 struct lttng_poll_event events
;
1874 * Get allocated in this thread, enqueued to a global queue, dequeued and
1875 * freed in the manage apps thread.
1877 struct ust_command
*ust_cmd
= NULL
;
1879 DBG("[thread] Manage application registration started");
1881 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1883 if (testpoint(sessiond_thread_registration_apps
)) {
1884 goto error_testpoint
;
1887 ret
= lttcomm_listen_unix_sock(apps_sock
);
1893 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1894 * more will be added to this poll set.
1896 ret
= sessiond_set_thread_pollset(&events
, 2);
1898 goto error_create_poll
;
1901 /* Add the application registration socket */
1902 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1904 goto error_poll_add
;
1907 /* Notify all applications to register */
1908 ret
= notify_ust_apps(1);
1910 ERR("Failed to notify applications or create the wait shared memory.\n"
1911 "Execution continues but there might be problem for already\n"
1912 "running applications that wishes to register.");
1916 DBG("Accepting application registration");
1918 /* Inifinite blocking call, waiting for transmission */
1920 health_poll_entry();
1921 ret
= lttng_poll_wait(&events
, -1);
1925 * Restart interrupted system call.
1927 if (errno
== EINTR
) {
1935 for (i
= 0; i
< nb_fd
; i
++) {
1936 health_code_update();
1938 /* Fetch once the poll data */
1939 revents
= LTTNG_POLL_GETEV(&events
, i
);
1940 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1943 /* No activity for this FD (poll implementation). */
1947 /* Thread quit pipe has been closed. Killing thread. */
1948 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1954 /* Event on the registration socket */
1955 if (pollfd
== apps_sock
) {
1956 if (revents
& LPOLLIN
) {
1957 sock
= lttcomm_accept_unix_sock(apps_sock
);
1963 * Set socket timeout for both receiving and ending.
1964 * app_socket_timeout is in seconds, whereas
1965 * lttcomm_setsockopt_rcv_timeout and
1966 * lttcomm_setsockopt_snd_timeout expect msec as
1969 if (config
.app_socket_timeout
>= 0) {
1970 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1971 config
.app_socket_timeout
* 1000);
1972 (void) lttcomm_setsockopt_snd_timeout(sock
,
1973 config
.app_socket_timeout
* 1000);
1977 * Set the CLOEXEC flag. Return code is useless because
1978 * either way, the show must go on.
1980 (void) utils_set_fd_cloexec(sock
);
1982 /* Create UST registration command for enqueuing */
1983 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1984 if (ust_cmd
== NULL
) {
1985 PERROR("ust command zmalloc");
1994 * Using message-based transmissions to ensure we don't
1995 * have to deal with partially received messages.
1997 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1999 ERR("Exhausted file descriptors allowed for applications.");
2009 health_code_update();
2010 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2013 /* Close socket of the application. */
2018 lttng_fd_put(LTTNG_FD_APPS
, 1);
2022 health_code_update();
2024 ust_cmd
->sock
= sock
;
2027 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2028 " gid:%d sock:%d name:%s (version %d.%d)",
2029 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2030 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2031 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2032 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2035 * Lock free enqueue the registration request. The red pill
2036 * has been taken! This apps will be part of the *system*.
2038 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2041 * Wake the registration queue futex. Implicit memory
2042 * barrier with the exchange in cds_wfcq_enqueue.
2044 futex_nto1_wake(&ust_cmd_queue
.futex
);
2045 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2046 ERR("Register apps socket poll error");
2049 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2058 /* Notify that the registration thread is gone */
2061 if (apps_sock
>= 0) {
2062 ret
= close(apps_sock
);
2072 lttng_fd_put(LTTNG_FD_APPS
, 1);
2074 unlink(config
.apps_unix_sock_path
.value
);
2077 lttng_poll_clean(&events
);
2081 DBG("UST Registration thread cleanup complete");
2084 ERR("Health error occurred in %s", __func__
);
2086 health_unregister(health_sessiond
);
2092 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2093 * exec or it will fails.
2095 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2098 struct timespec timeout
;
2101 * Make sure we set the readiness flag to 0 because we are NOT ready.
2102 * This access to consumer_thread_is_ready does not need to be
2103 * protected by consumer_data.cond_mutex (yet) since the consumer
2104 * management thread has not been started at this point.
2106 consumer_data
->consumer_thread_is_ready
= 0;
2108 /* Setup pthread condition */
2109 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2112 PERROR("pthread_condattr_init consumer data");
2117 * Set the monotonic clock in order to make sure we DO NOT jump in time
2118 * between the clock_gettime() call and the timedwait call. See bug #324
2119 * for a more details and how we noticed it.
2121 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2124 PERROR("pthread_condattr_setclock consumer data");
2128 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2131 PERROR("pthread_cond_init consumer data");
2135 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2136 thread_manage_consumer
, consumer_data
);
2139 PERROR("pthread_create consumer");
2144 /* We are about to wait on a pthread condition */
2145 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2147 /* Get time for sem_timedwait absolute timeout */
2148 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2150 * Set the timeout for the condition timed wait even if the clock gettime
2151 * call fails since we might loop on that call and we want to avoid to
2152 * increment the timeout too many times.
2154 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2157 * The following loop COULD be skipped in some conditions so this is why we
2158 * set ret to 0 in order to make sure at least one round of the loop is
2164 * Loop until the condition is reached or when a timeout is reached. Note
2165 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2166 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2167 * possible. This loop does not take any chances and works with both of
2170 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2171 if (clock_ret
< 0) {
2172 PERROR("clock_gettime spawn consumer");
2173 /* Infinite wait for the consumerd thread to be ready */
2174 ret
= pthread_cond_wait(&consumer_data
->cond
,
2175 &consumer_data
->cond_mutex
);
2177 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2178 &consumer_data
->cond_mutex
, &timeout
);
2182 /* Release the pthread condition */
2183 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2187 if (ret
== ETIMEDOUT
) {
2191 * Call has timed out so we kill the kconsumerd_thread and return
2194 ERR("Condition timed out. The consumer thread was never ready."
2196 pth_ret
= pthread_cancel(consumer_data
->thread
);
2198 PERROR("pthread_cancel consumer thread");
2201 PERROR("pthread_cond_wait failed consumer thread");
2203 /* Caller is expecting a negative value on failure. */
2208 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2209 if (consumer_data
->pid
== 0) {
2210 ERR("Consumerd did not start");
2211 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2214 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2223 * Join consumer thread
2225 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2229 /* Consumer pid must be a real one. */
2230 if (consumer_data
->pid
> 0) {
2232 ret
= kill(consumer_data
->pid
, SIGTERM
);
2234 PERROR("Error killing consumer daemon");
2237 return pthread_join(consumer_data
->thread
, &status
);
2244 * Fork and exec a consumer daemon (consumerd).
2246 * Return pid if successful else -1.
2248 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2252 const char *consumer_to_use
;
2253 const char *verbosity
;
2256 DBG("Spawning consumerd");
2263 if (config
.verbose_consumer
) {
2264 verbosity
= "--verbose";
2265 } else if (lttng_opt_quiet
) {
2266 verbosity
= "--quiet";
2271 switch (consumer_data
->type
) {
2272 case LTTNG_CONSUMER_KERNEL
:
2274 * Find out which consumerd to execute. We will first try the
2275 * 64-bit path, then the sessiond's installation directory, and
2276 * fallback on the 32-bit one,
2278 DBG3("Looking for a kernel consumer at these locations:");
2279 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2280 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2281 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2282 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2283 DBG3("Found location #1");
2284 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2285 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2286 DBG3("Found location #2");
2287 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2288 } else if (config
.consumerd32_bin_path
.value
&&
2289 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2290 DBG3("Found location #3");
2291 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2293 DBG("Could not find any valid consumerd executable");
2297 DBG("Using kernel consumer at: %s", consumer_to_use
);
2298 (void) execl(consumer_to_use
,
2299 "lttng-consumerd", verbosity
, "-k",
2300 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2301 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2302 "--group", config
.tracing_group_name
.value
,
2305 case LTTNG_CONSUMER64_UST
:
2307 if (config
.consumerd64_lib_dir
.value
) {
2312 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2316 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2317 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2322 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2323 if (tmp
[0] != '\0') {
2324 strcat(tmpnew
, ":");
2325 strcat(tmpnew
, tmp
);
2327 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2334 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2335 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2336 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2337 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2338 "--group", config
.tracing_group_name
.value
,
2342 case LTTNG_CONSUMER32_UST
:
2344 if (config
.consumerd32_lib_dir
.value
) {
2349 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2353 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2354 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2359 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2360 if (tmp
[0] != '\0') {
2361 strcat(tmpnew
, ":");
2362 strcat(tmpnew
, tmp
);
2364 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2371 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2372 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2373 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2374 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2375 "--group", config
.tracing_group_name
.value
,
2380 ERR("unknown consumer type");
2384 PERROR("Consumer execl()");
2386 /* Reaching this point, we got a failure on our execl(). */
2388 } else if (pid
> 0) {
2391 PERROR("start consumer fork");
2399 * Spawn the consumerd daemon and session daemon thread.
2401 static int start_consumerd(struct consumer_data
*consumer_data
)
2406 * Set the listen() state on the socket since there is a possible race
2407 * between the exec() of the consumer daemon and this call if place in the
2408 * consumer thread. See bug #366 for more details.
2410 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2415 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2416 if (consumer_data
->pid
!= 0) {
2417 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2421 ret
= spawn_consumerd(consumer_data
);
2423 ERR("Spawning consumerd failed");
2424 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2428 /* Setting up the consumer_data pid */
2429 consumer_data
->pid
= ret
;
2430 DBG2("Consumer pid %d", consumer_data
->pid
);
2431 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2433 DBG2("Spawning consumer control thread");
2434 ret
= spawn_consumer_thread(consumer_data
);
2436 ERR("Fatal error spawning consumer control thread");
2444 /* Cleanup already created sockets on error. */
2445 if (consumer_data
->err_sock
>= 0) {
2448 err
= close(consumer_data
->err_sock
);
2450 PERROR("close consumer data error socket");
2457 * Setup necessary data for kernel tracer action.
2459 static int init_kernel_tracer(void)
2463 /* Modprobe lttng kernel modules */
2464 ret
= modprobe_lttng_control();
2469 /* Open debugfs lttng */
2470 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2471 if (kernel_tracer_fd
< 0) {
2472 DBG("Failed to open %s", module_proc_lttng
);
2476 /* Validate kernel version */
2477 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2478 &kernel_tracer_abi_version
);
2483 ret
= modprobe_lttng_data();
2488 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2495 WARN("Kernel tracer does not support buffer monitoring. "
2496 "The monitoring timer of channels in the kernel domain "
2497 "will be set to 0 (disabled).");
2500 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2504 modprobe_remove_lttng_control();
2505 ret
= close(kernel_tracer_fd
);
2509 kernel_tracer_fd
= -1;
2510 return LTTNG_ERR_KERN_VERSION
;
2513 ret
= close(kernel_tracer_fd
);
2519 modprobe_remove_lttng_control();
2522 WARN("No kernel tracer available");
2523 kernel_tracer_fd
= -1;
2525 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2527 return LTTNG_ERR_KERN_NA
;
2533 * Copy consumer output from the tracing session to the domain session. The
2534 * function also applies the right modification on a per domain basis for the
2535 * trace files destination directory.
2537 * Should *NOT* be called with RCU read-side lock held.
2539 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2542 const char *dir_name
;
2543 struct consumer_output
*consumer
;
2546 assert(session
->consumer
);
2549 case LTTNG_DOMAIN_KERNEL
:
2550 DBG3("Copying tracing session consumer output in kernel session");
2552 * XXX: We should audit the session creation and what this function
2553 * does "extra" in order to avoid a destroy since this function is used
2554 * in the domain session creation (kernel and ust) only. Same for UST
2557 if (session
->kernel_session
->consumer
) {
2558 consumer_output_put(session
->kernel_session
->consumer
);
2560 session
->kernel_session
->consumer
=
2561 consumer_copy_output(session
->consumer
);
2562 /* Ease our life a bit for the next part */
2563 consumer
= session
->kernel_session
->consumer
;
2564 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2566 case LTTNG_DOMAIN_JUL
:
2567 case LTTNG_DOMAIN_LOG4J
:
2568 case LTTNG_DOMAIN_PYTHON
:
2569 case LTTNG_DOMAIN_UST
:
2570 DBG3("Copying tracing session consumer output in UST session");
2571 if (session
->ust_session
->consumer
) {
2572 consumer_output_put(session
->ust_session
->consumer
);
2574 session
->ust_session
->consumer
=
2575 consumer_copy_output(session
->consumer
);
2576 /* Ease our life a bit for the next part */
2577 consumer
= session
->ust_session
->consumer
;
2578 dir_name
= DEFAULT_UST_TRACE_DIR
;
2581 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2585 /* Append correct directory to subdir */
2586 strncat(consumer
->subdir
, dir_name
,
2587 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2588 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2597 * Create an UST session and add it to the session ust list.
2599 * Should *NOT* be called with RCU read-side lock held.
2601 static int create_ust_session(struct ltt_session
*session
,
2602 struct lttng_domain
*domain
)
2605 struct ltt_ust_session
*lus
= NULL
;
2609 assert(session
->consumer
);
2611 switch (domain
->type
) {
2612 case LTTNG_DOMAIN_JUL
:
2613 case LTTNG_DOMAIN_LOG4J
:
2614 case LTTNG_DOMAIN_PYTHON
:
2615 case LTTNG_DOMAIN_UST
:
2618 ERR("Unknown UST domain on create session %d", domain
->type
);
2619 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2623 DBG("Creating UST session");
2625 lus
= trace_ust_create_session(session
->id
);
2627 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2631 lus
->uid
= session
->uid
;
2632 lus
->gid
= session
->gid
;
2633 lus
->output_traces
= session
->output_traces
;
2634 lus
->snapshot_mode
= session
->snapshot_mode
;
2635 lus
->live_timer_interval
= session
->live_timer
;
2636 session
->ust_session
= lus
;
2637 if (session
->shm_path
[0]) {
2638 strncpy(lus
->root_shm_path
, session
->shm_path
,
2639 sizeof(lus
->root_shm_path
));
2640 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2641 strncpy(lus
->shm_path
, session
->shm_path
,
2642 sizeof(lus
->shm_path
));
2643 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2644 strncat(lus
->shm_path
, "/ust",
2645 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2647 /* Copy session output to the newly created UST session */
2648 ret
= copy_session_consumer(domain
->type
, session
);
2649 if (ret
!= LTTNG_OK
) {
2657 session
->ust_session
= NULL
;
2662 * Create a kernel tracer session then create the default channel.
2664 static int create_kernel_session(struct ltt_session
*session
)
2668 DBG("Creating kernel session");
2670 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2672 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2676 /* Code flow safety */
2677 assert(session
->kernel_session
);
2679 /* Copy session output to the newly created Kernel session */
2680 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2681 if (ret
!= LTTNG_OK
) {
2685 session
->kernel_session
->uid
= session
->uid
;
2686 session
->kernel_session
->gid
= session
->gid
;
2687 session
->kernel_session
->output_traces
= session
->output_traces
;
2688 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2693 trace_kernel_destroy_session(session
->kernel_session
);
2694 session
->kernel_session
= NULL
;
2699 * Count number of session permitted by uid/gid.
2701 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2704 struct ltt_session
*session
;
2705 const struct ltt_session_list
*session_list
= session_get_list();
2707 DBG("Counting number of available session for UID %d GID %d",
2709 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
2710 if (!session_get(session
)) {
2713 session_lock(session
);
2714 /* Only count the sessions the user can control. */
2715 if (session_access_ok(session
, uid
, gid
) &&
2716 !session
->destroyed
) {
2719 session_unlock(session
);
2720 session_put(session
);
2725 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2726 int *sock_error
, struct lttng_event
*event
)
2729 struct lttng_userspace_probe_location
*probe_location
;
2730 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2731 struct lttng_dynamic_buffer probe_location_buffer
;
2732 struct lttng_buffer_view buffer_view
;
2735 * Create a buffer to store the serialized version of the probe
2738 lttng_dynamic_buffer_init(&probe_location_buffer
);
2739 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2740 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2742 ret
= LTTNG_ERR_NOMEM
;
2747 * Receive the probe location.
2749 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2750 probe_location_buffer
.size
);
2752 DBG("Nothing recv() from client var len data... continuing");
2754 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2755 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2759 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2760 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2763 * Extract the probe location from the serialized version.
2765 ret
= lttng_userspace_probe_location_create_from_buffer(
2766 &buffer_view
, &probe_location
);
2768 WARN("Failed to create a userspace probe location from the received buffer");
2769 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2770 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2775 * Receive the file descriptor to the target binary from the client.
2777 DBG("Receiving userspace probe target FD from client ...");
2778 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2780 DBG("Nothing recv() from client userspace probe fd... continuing");
2782 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2787 * Set the file descriptor received from the client through the unix
2788 * socket in the probe location.
2790 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
2792 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2797 * From the kernel tracer's perspective, all userspace probe event types
2798 * are all the same: a file and an offset.
2800 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
2801 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
2802 ret
= lttng_userspace_probe_location_function_set_binary_fd(
2803 probe_location
, fd
);
2805 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
2806 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
2807 probe_location
, fd
);
2810 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2815 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2819 /* Attach the probe location to the event. */
2820 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
2822 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2826 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2832 * Check if the current kernel tracer supports the session rotation feature.
2833 * Return 1 if it does, 0 otherwise.
2835 static int check_rotate_compatible(void)
2839 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2840 DBG("Kernel tracer version is not compatible with the rotation feature");
2848 * Process the command requested by the lttng client within the command
2849 * context structure. This function make sure that the return structure (llm)
2850 * is set and ready for transmission before returning.
2852 * Return any error encountered or 0 for success.
2854 * "sock" is only used for special-case var. len data.
2856 * Should *NOT* be called with RCU read-side lock held.
2858 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2862 int need_tracing_session
= 1;
2865 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2867 assert(!rcu_read_ongoing());
2871 switch (cmd_ctx
->lsm
->cmd_type
) {
2872 case LTTNG_CREATE_SESSION
:
2873 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2874 case LTTNG_CREATE_SESSION_LIVE
:
2875 case LTTNG_DESTROY_SESSION
:
2876 case LTTNG_LIST_SESSIONS
:
2877 case LTTNG_LIST_DOMAINS
:
2878 case LTTNG_START_TRACE
:
2879 case LTTNG_STOP_TRACE
:
2880 case LTTNG_DATA_PENDING
:
2881 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2882 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2883 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2884 case LTTNG_SNAPSHOT_RECORD
:
2885 case LTTNG_SAVE_SESSION
:
2886 case LTTNG_SET_SESSION_SHM_PATH
:
2887 case LTTNG_REGENERATE_METADATA
:
2888 case LTTNG_REGENERATE_STATEDUMP
:
2889 case LTTNG_REGISTER_TRIGGER
:
2890 case LTTNG_UNREGISTER_TRIGGER
:
2891 case LTTNG_ROTATE_SESSION
:
2892 case LTTNG_ROTATION_GET_INFO
:
2893 case LTTNG_ROTATION_SET_SCHEDULE
:
2894 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2901 if (config
.no_kernel
&& need_domain
2902 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2904 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2906 ret
= LTTNG_ERR_KERN_NA
;
2911 /* Deny register consumer if we already have a spawned consumer. */
2912 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2913 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2914 if (kconsumer_data
.pid
> 0) {
2915 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2916 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2919 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2923 * Check for command that don't needs to allocate a returned payload. We do
2924 * this here so we don't have to make the call for no payload at each
2927 switch(cmd_ctx
->lsm
->cmd_type
) {
2928 case LTTNG_LIST_SESSIONS
:
2929 case LTTNG_LIST_TRACEPOINTS
:
2930 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2931 case LTTNG_LIST_DOMAINS
:
2932 case LTTNG_LIST_CHANNELS
:
2933 case LTTNG_LIST_EVENTS
:
2934 case LTTNG_LIST_SYSCALLS
:
2935 case LTTNG_LIST_TRACKER_PIDS
:
2936 case LTTNG_DATA_PENDING
:
2937 case LTTNG_ROTATE_SESSION
:
2938 case LTTNG_ROTATION_GET_INFO
:
2939 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2942 /* Setup lttng message with no payload */
2943 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2945 /* This label does not try to unlock the session */
2946 goto init_setup_error
;
2950 /* Commands that DO NOT need a session. */
2951 switch (cmd_ctx
->lsm
->cmd_type
) {
2952 case LTTNG_CREATE_SESSION
:
2953 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2954 case LTTNG_CREATE_SESSION_LIVE
:
2955 case LTTNG_LIST_SESSIONS
:
2956 case LTTNG_LIST_TRACEPOINTS
:
2957 case LTTNG_LIST_SYSCALLS
:
2958 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2959 case LTTNG_SAVE_SESSION
:
2960 case LTTNG_REGISTER_TRIGGER
:
2961 case LTTNG_UNREGISTER_TRIGGER
:
2962 need_tracing_session
= 0;
2965 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2967 * We keep the session list lock across _all_ commands
2968 * for now, because the per-session lock does not
2969 * handle teardown properly.
2971 session_lock_list();
2972 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2973 if (cmd_ctx
->session
== NULL
) {
2974 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2977 /* Acquire lock for the session */
2978 session_lock(cmd_ctx
->session
);
2984 * Commands that need a valid session but should NOT create one if none
2985 * exists. Instead of creating one and destroying it when the command is
2986 * handled, process that right before so we save some round trip in useless
2989 switch (cmd_ctx
->lsm
->cmd_type
) {
2990 case LTTNG_DISABLE_CHANNEL
:
2991 case LTTNG_DISABLE_EVENT
:
2992 switch (cmd_ctx
->lsm
->domain
.type
) {
2993 case LTTNG_DOMAIN_KERNEL
:
2994 if (!cmd_ctx
->session
->kernel_session
) {
2995 ret
= LTTNG_ERR_NO_CHANNEL
;
2999 case LTTNG_DOMAIN_JUL
:
3000 case LTTNG_DOMAIN_LOG4J
:
3001 case LTTNG_DOMAIN_PYTHON
:
3002 case LTTNG_DOMAIN_UST
:
3003 if (!cmd_ctx
->session
->ust_session
) {
3004 ret
= LTTNG_ERR_NO_CHANNEL
;
3009 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3021 * Check domain type for specific "pre-action".
3023 switch (cmd_ctx
->lsm
->domain
.type
) {
3024 case LTTNG_DOMAIN_KERNEL
:
3026 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3030 /* Kernel tracer check */
3031 if (kernel_tracer_fd
== -1) {
3032 /* Basically, load kernel tracer modules */
3033 ret
= init_kernel_tracer();
3039 /* Consumer is in an ERROR state. Report back to client */
3040 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3041 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3045 /* Need a session for kernel command */
3046 if (need_tracing_session
) {
3047 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3048 ret
= create_kernel_session(cmd_ctx
->session
);
3050 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3055 /* Start the kernel consumer daemon */
3056 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3057 if (kconsumer_data
.pid
== 0 &&
3058 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3059 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3060 ret
= start_consumerd(&kconsumer_data
);
3062 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3065 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3067 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3071 * The consumer was just spawned so we need to add the socket to
3072 * the consumer output of the session if exist.
3074 ret
= consumer_create_socket(&kconsumer_data
,
3075 cmd_ctx
->session
->kernel_session
->consumer
);
3082 case LTTNG_DOMAIN_JUL
:
3083 case LTTNG_DOMAIN_LOG4J
:
3084 case LTTNG_DOMAIN_PYTHON
:
3085 case LTTNG_DOMAIN_UST
:
3087 if (!ust_app_supported()) {
3088 ret
= LTTNG_ERR_NO_UST
;
3091 /* Consumer is in an ERROR state. Report back to client */
3092 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3093 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3097 if (need_tracing_session
) {
3098 /* Create UST session if none exist. */
3099 if (cmd_ctx
->session
->ust_session
== NULL
) {
3100 ret
= create_ust_session(cmd_ctx
->session
,
3101 &cmd_ctx
->lsm
->domain
);
3102 if (ret
!= LTTNG_OK
) {
3107 /* Start the UST consumer daemons */
3109 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3110 if (config
.consumerd64_bin_path
.value
&&
3111 ustconsumer64_data
.pid
== 0 &&
3112 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3113 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3114 ret
= start_consumerd(&ustconsumer64_data
);
3116 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3117 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3121 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3122 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3124 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3128 * Setup socket for consumer 64 bit. No need for atomic access
3129 * since it was set above and can ONLY be set in this thread.
3131 ret
= consumer_create_socket(&ustconsumer64_data
,
3132 cmd_ctx
->session
->ust_session
->consumer
);
3138 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3139 if (config
.consumerd32_bin_path
.value
&&
3140 ustconsumer32_data
.pid
== 0 &&
3141 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3142 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3143 ret
= start_consumerd(&ustconsumer32_data
);
3145 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3146 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3150 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3151 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3153 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3157 * Setup socket for consumer 32 bit. No need for atomic access
3158 * since it was set above and can ONLY be set in this thread.
3160 ret
= consumer_create_socket(&ustconsumer32_data
,
3161 cmd_ctx
->session
->ust_session
->consumer
);
3173 /* Validate consumer daemon state when start/stop trace command */
3174 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3175 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3176 switch (cmd_ctx
->lsm
->domain
.type
) {
3177 case LTTNG_DOMAIN_NONE
:
3179 case LTTNG_DOMAIN_JUL
:
3180 case LTTNG_DOMAIN_LOG4J
:
3181 case LTTNG_DOMAIN_PYTHON
:
3182 case LTTNG_DOMAIN_UST
:
3183 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3184 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3188 case LTTNG_DOMAIN_KERNEL
:
3189 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3190 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3195 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3201 * Check that the UID or GID match that of the tracing session.
3202 * The root user can interact with all sessions.
3204 if (need_tracing_session
) {
3205 if (!session_access_ok(cmd_ctx
->session
,
3206 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3207 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
3208 cmd_ctx
->session
->destroyed
) {
3209 ret
= LTTNG_ERR_EPERM
;
3215 * Send relayd information to consumer as soon as we have a domain and a