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 notification_thread
;
198 static pthread_t rotation_thread
;
199 static pthread_t timer_thread
;
202 * UST registration command queue. This queue is tied with a futex and uses a N
203 * wakers / 1 waiter implemented and detailed in futex.c/.h
205 * The thread_registration_apps and thread_dispatch_ust_registration uses this
206 * queue along with the wait/wake scheme. The thread_manage_apps receives down
207 * the line new application socket and monitors it for any I/O error or clean
208 * close that triggers an unregistration of the application.
210 static struct ust_cmd_queue ust_cmd_queue
;
212 static const char *module_proc_lttng
= "/proc/lttng";
215 * Consumer daemon state which is changed when spawning it, killing it or in
216 * case of a fatal error.
218 enum consumerd_state
{
219 CONSUMER_STARTED
= 1,
220 CONSUMER_STOPPED
= 2,
225 * This consumer daemon state is used to validate if a client command will be
226 * able to reach the consumer. If not, the client is informed. For instance,
227 * doing a "lttng start" when the consumer state is set to ERROR will return an
228 * error to the client.
230 * The following example shows a possible race condition of this scheme:
232 * consumer thread error happens
234 * client cmd checks state -> still OK
235 * consumer thread exit, sets error
236 * client cmd try to talk to consumer
239 * However, since the consumer is a different daemon, we have no way of making
240 * sure the command will reach it safely even with this state flag. This is why
241 * we consider that up to the state validation during command processing, the
242 * command is safe. After that, we can not guarantee the correctness of the
243 * client request vis-a-vis the consumer.
245 static enum consumerd_state ust_consumerd_state
;
246 static enum consumerd_state kernel_consumerd_state
;
248 /* Load session thread information to operate. */
249 static struct load_session_thread_data
*load_info
;
252 * Section name to look for in the daemon configuration file.
254 static const char * const config_section_name
= "sessiond";
256 /* Am I root or not. Set to 1 if the daemon is running as root */
259 /* Rotation thread handle. */
260 static struct rotation_thread_handle
*rotation_thread_handle
;
263 * Stop all threads by closing the thread quit pipe.
265 static void stop_threads(void)
269 /* Stopping all threads */
270 DBG("Terminating all threads");
271 ret
= sessiond_notify_quit_pipe();
273 ERR("write error on thread quit pipe");
276 /* Dispatch thread */
277 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
278 futex_nto1_wake(&ust_cmd_queue
.futex
);
282 * Close every consumer sockets.
284 static void close_consumer_sockets(void)
288 if (kconsumer_data
.err_sock
>= 0) {
289 ret
= close(kconsumer_data
.err_sock
);
291 PERROR("kernel consumer err_sock close");
294 if (ustconsumer32_data
.err_sock
>= 0) {
295 ret
= close(ustconsumer32_data
.err_sock
);
297 PERROR("UST consumerd32 err_sock close");
300 if (ustconsumer64_data
.err_sock
>= 0) {
301 ret
= close(ustconsumer64_data
.err_sock
);
303 PERROR("UST consumerd64 err_sock close");
306 if (kconsumer_data
.cmd_sock
>= 0) {
307 ret
= close(kconsumer_data
.cmd_sock
);
309 PERROR("kernel consumer cmd_sock close");
312 if (ustconsumer32_data
.cmd_sock
>= 0) {
313 ret
= close(ustconsumer32_data
.cmd_sock
);
315 PERROR("UST consumerd32 cmd_sock close");
318 if (ustconsumer64_data
.cmd_sock
>= 0) {
319 ret
= close(ustconsumer64_data
.cmd_sock
);
321 PERROR("UST consumerd64 cmd_sock close");
324 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
325 ret
= close(kconsumer_data
.channel_monitor_pipe
);
327 PERROR("kernel consumer channel monitor pipe close");
330 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
331 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
333 PERROR("UST consumerd32 channel monitor pipe close");
336 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
337 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
339 PERROR("UST consumerd64 channel monitor pipe close");
345 * Wait on consumer process termination.
347 * Need to be called with the consumer data lock held or from a context
348 * ensuring no concurrent access to data (e.g: cleanup).
350 static void wait_consumer(struct consumer_data
*consumer_data
)
355 if (consumer_data
->pid
<= 0) {
359 DBG("Waiting for complete teardown of consumerd (PID: %d)",
361 ret
= waitpid(consumer_data
->pid
, &status
, 0);
363 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
364 } else if (!WIFEXITED(status
)) {
365 ERR("consumerd termination with error: %d",
368 consumer_data
->pid
= 0;
372 * Cleanup the session daemon's data structures.
374 static void sessiond_cleanup(void)
377 struct ltt_session_list
*session_list
= session_get_list();
379 DBG("Cleanup sessiond");
382 * Close the thread quit pipe. It has already done its job,
383 * since we are now called.
385 sessiond_close_quit_pipe();
387 ret
= remove(config
.pid_file_path
.value
);
389 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
392 DBG("Removing sessiond and consumerd content of directory %s",
393 config
.rundir
.value
);
396 DBG("Removing %s", config
.pid_file_path
.value
);
397 (void) unlink(config
.pid_file_path
.value
);
399 DBG("Removing %s", config
.agent_port_file_path
.value
);
400 (void) unlink(config
.agent_port_file_path
.value
);
403 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
404 (void) unlink(kconsumer_data
.err_unix_sock_path
);
406 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
407 (void) rmdir(config
.kconsumerd_path
.value
);
409 /* ust consumerd 32 */
410 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
411 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
413 DBG("Removing directory %s", config
.consumerd32_path
.value
);
414 (void) rmdir(config
.consumerd32_path
.value
);
416 /* ust consumerd 64 */
417 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
418 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
420 DBG("Removing directory %s", config
.consumerd64_path
.value
);
421 (void) rmdir(config
.consumerd64_path
.value
);
423 pthread_mutex_destroy(&session_list
->lock
);
425 wait_consumer(&kconsumer_data
);
426 wait_consumer(&ustconsumer64_data
);
427 wait_consumer(&ustconsumer32_data
);
429 DBG("Cleaning up all agent apps");
430 agent_app_ht_clean();
432 DBG("Closing all UST sockets");
433 ust_app_clean_list();
434 buffer_reg_destroy_registries();
436 if (is_root
&& !config
.no_kernel
) {
437 DBG2("Closing kernel fd");
438 if (kernel_tracer_fd
>= 0) {
439 ret
= close(kernel_tracer_fd
);
444 DBG("Unloading kernel modules");
445 modprobe_remove_lttng_all();
449 close_consumer_sockets();
452 load_session_destroy_data(load_info
);
457 * We do NOT rmdir rundir because there are other processes
458 * using it, for instance lttng-relayd, which can start in
459 * parallel with this teardown.
464 * Cleanup the daemon's option data structures.
466 static void sessiond_cleanup_options(void)
468 DBG("Cleaning up options");
470 sessiond_config_fini(&config
);
472 run_as_destroy_worker();
476 * Send data on a unix socket using the liblttsessiondcomm API.
478 * Return lttcomm error code.
480 static int send_unix_sock(int sock
, void *buf
, size_t len
)
482 /* Check valid length */
487 return lttcomm_send_unix_sock(sock
, buf
, len
);
491 * Free memory of a command context structure.
493 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
495 DBG("Clean command context structure");
497 if ((*cmd_ctx
)->llm
) {
498 free((*cmd_ctx
)->llm
);
500 if ((*cmd_ctx
)->lsm
) {
501 free((*cmd_ctx
)->lsm
);
509 * Notify UST applications using the shm mmap futex.
511 static int notify_ust_apps(int active
)
515 DBG("Notifying applications of session daemon state: %d", active
);
517 /* See shm.c for this call implying mmap, shm and futex calls */
518 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
519 if (wait_shm_mmap
== NULL
) {
523 /* Wake waiting process */
524 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
526 /* Apps notified successfully */
534 * Setup the outgoing data buffer for the response (llm) by allocating the
535 * right amount of memory and copying the original information from the lsm
538 * Return 0 on success, negative value on error.
540 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
541 const void *payload_buf
, size_t payload_len
,
542 const void *cmd_header_buf
, size_t cmd_header_len
)
545 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
546 const size_t cmd_header_offset
= header_len
;
547 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
548 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
550 cmd_ctx
->llm
= zmalloc(total_msg_size
);
552 if (cmd_ctx
->llm
== NULL
) {
558 /* Copy common data */
559 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
560 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
561 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
562 cmd_ctx
->llm
->data_size
= payload_len
;
563 cmd_ctx
->lttng_msg_size
= total_msg_size
;
565 /* Copy command header */
566 if (cmd_header_len
) {
567 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
573 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
582 * Version of setup_lttng_msg() without command header.
584 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
585 void *payload_buf
, size_t payload_len
)
587 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
590 * Update the kernel poll set of all channel fd available over all tracing
591 * session. Add the wakeup pipe at the end of the set.
593 static int update_kernel_poll(struct lttng_poll_event
*events
)
596 struct ltt_kernel_channel
*channel
;
597 struct ltt_session
*session
;
598 const struct ltt_session_list
*session_list
= session_get_list();
600 DBG("Updating kernel poll set");
603 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
604 if (!session_get(session
)) {
607 session_lock(session
);
608 if (session
->kernel_session
== NULL
) {
609 session_unlock(session
);
610 session_put(session
);
614 cds_list_for_each_entry(channel
,
615 &session
->kernel_session
->channel_list
.head
, list
) {
616 /* Add channel fd to the kernel poll set */
617 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
619 session_unlock(session
);
620 session_put(session
);
623 DBG("Channel fd %d added to kernel set", channel
->fd
);
625 session_unlock(session
);
627 session_unlock_list();
632 session_unlock_list();
637 * Find the channel fd from 'fd' over all tracing session. When found, check
638 * for new channel stream and send those stream fds to the kernel consumer.
640 * Useful for CPU hotplug feature.
642 static int update_kernel_stream(int fd
)
645 struct ltt_session
*session
;
646 struct ltt_kernel_session
*ksess
;
647 struct ltt_kernel_channel
*channel
;
648 const struct ltt_session_list
*session_list
= session_get_list();
650 DBG("Updating kernel streams for channel fd %d", fd
);
653 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
654 if (!session_get(session
)) {
657 session_lock(session
);
658 if (session
->kernel_session
== NULL
) {
659 session_unlock(session
);
660 session_put(session
);
663 ksess
= session
->kernel_session
;
665 cds_list_for_each_entry(channel
,
666 &ksess
->channel_list
.head
, list
) {
667 struct lttng_ht_iter iter
;
668 struct consumer_socket
*socket
;
670 if (channel
->fd
!= fd
) {
673 DBG("Channel found, updating kernel streams");
674 ret
= kernel_open_channel_stream(channel
);
678 /* Update the stream global counter */
679 ksess
->stream_count_global
+= ret
;
682 * Have we already sent fds to the consumer? If yes, it
683 * means that tracing is started so it is safe to send
684 * our updated stream fds.
686 if (ksess
->consumer_fds_sent
!= 1
687 || ksess
->consumer
== NULL
) {
693 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
694 &iter
.iter
, socket
, node
.node
) {
695 pthread_mutex_lock(socket
->lock
);
696 ret
= kernel_consumer_send_channel_streams(socket
,
698 session
->output_traces
? 1 : 0);
699 pthread_mutex_unlock(socket
->lock
);
707 session_unlock(session
);
708 session_put(session
);
710 session_unlock_list();
714 session_unlock(session
);
715 session_put(session
);
716 session_unlock_list();
721 * For each tracing session, update newly registered apps. The session list
722 * lock MUST be acquired before calling this.
724 static void update_ust_app(int app_sock
)
726 struct ltt_session
*sess
, *stmp
;
727 const struct ltt_session_list
*session_list
= session_get_list();
729 /* Consumer is in an ERROR state. Stop any application update. */
730 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
731 /* Stop the update process since the consumer is dead. */
735 /* For all tracing session(s) */
736 cds_list_for_each_entry_safe(sess
, stmp
, &session_list
->head
, list
) {
739 if (!session_get(sess
)) {
743 if (!sess
->ust_session
) {
748 assert(app_sock
>= 0);
749 app
= ust_app_find_by_sock(app_sock
);
752 * Application can be unregistered before so
753 * this is possible hence simply stopping the
756 DBG3("UST app update failed to find app sock %d",
760 ust_app_global_update(sess
->ust_session
, app
);
764 session_unlock(sess
);
770 * This thread manage event coming from the kernel.
772 * Features supported in this thread:
775 static void *thread_manage_kernel(void *data
)
777 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
778 uint32_t revents
, nb_fd
;
780 struct lttng_poll_event events
;
782 DBG("[thread] Thread manage kernel started");
784 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
787 * This first step of the while is to clean this structure which could free
788 * non NULL pointers so initialize it before the loop.
790 lttng_poll_init(&events
);
792 if (testpoint(sessiond_thread_manage_kernel
)) {
793 goto error_testpoint
;
796 health_code_update();
798 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
799 goto error_testpoint
;
803 health_code_update();
805 if (update_poll_flag
== 1) {
806 /* Clean events object. We are about to populate it again. */
807 lttng_poll_clean(&events
);
809 ret
= sessiond_set_thread_pollset(&events
, 2);
811 goto error_poll_create
;
814 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
819 /* This will add the available kernel channel if any. */
820 ret
= update_kernel_poll(&events
);
824 update_poll_flag
= 0;
827 DBG("Thread kernel polling");
829 /* Poll infinite value of time */
832 ret
= lttng_poll_wait(&events
, -1);
833 DBG("Thread kernel return from poll on %d fds",
834 LTTNG_POLL_GETNB(&events
));
838 * Restart interrupted system call.
840 if (errno
== EINTR
) {
844 } else if (ret
== 0) {
845 /* Should not happen since timeout is infinite */
846 ERR("Return value of poll is 0 with an infinite timeout.\n"
847 "This should not have happened! Continuing...");
853 for (i
= 0; i
< nb_fd
; i
++) {
854 /* Fetch once the poll data */
855 revents
= LTTNG_POLL_GETEV(&events
, i
);
856 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
858 health_code_update();
861 /* No activity for this FD (poll implementation). */
865 /* Thread quit pipe has been closed. Killing thread. */
866 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
872 /* Check for data on kernel pipe */
873 if (revents
& LPOLLIN
) {
874 if (pollfd
== kernel_poll_pipe
[0]) {
875 (void) lttng_read(kernel_poll_pipe
[0],
878 * Ret value is useless here, if this pipe gets any actions an
879 * update is required anyway.
881 update_poll_flag
= 1;
885 * New CPU detected by the kernel. Adding kernel stream to
886 * kernel session and updating the kernel consumer
888 ret
= update_kernel_stream(pollfd
);
894 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
895 update_poll_flag
= 1;
898 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
906 lttng_poll_clean(&events
);
909 utils_close_pipe(kernel_poll_pipe
);
910 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
913 ERR("Health error occurred in %s", __func__
);
914 WARN("Kernel thread died unexpectedly. "
915 "Kernel tracing can continue but CPU hotplug is disabled.");
917 health_unregister(health_sessiond
);
918 DBG("Kernel thread dying");
923 * Signal pthread condition of the consumer data that the thread.
925 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
927 pthread_mutex_lock(&data
->cond_mutex
);
930 * The state is set before signaling. It can be any value, it's the waiter
931 * job to correctly interpret this condition variable associated to the
932 * consumer pthread_cond.
934 * A value of 0 means that the corresponding thread of the consumer data
935 * was not started. 1 indicates that the thread has started and is ready
936 * for action. A negative value means that there was an error during the
939 data
->consumer_thread_is_ready
= state
;
940 (void) pthread_cond_signal(&data
->cond
);
942 pthread_mutex_unlock(&data
->cond_mutex
);
946 * This thread manage the consumer error sent back to the session daemon.
948 static void *thread_manage_consumer(void *data
)
950 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
951 uint32_t revents
, nb_fd
;
952 enum lttcomm_return_code code
;
953 struct lttng_poll_event events
;
954 struct consumer_data
*consumer_data
= data
;
955 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
957 DBG("[thread] Manage consumer started");
959 rcu_register_thread();
962 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
964 health_code_update();
967 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
968 * metadata_sock. Nothing more will be added to this poll set.
970 ret
= sessiond_set_thread_pollset(&events
, 3);
976 * The error socket here is already in a listening state which was done
977 * just before spawning this thread to avoid a race between the consumer
978 * daemon exec trying to connect and the listen() call.
980 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
985 health_code_update();
987 /* Infinite blocking call, waiting for transmission */
991 if (testpoint(sessiond_thread_manage_consumer
)) {
995 ret
= lttng_poll_wait(&events
, -1);
999 * Restart interrupted system call.
1001 if (errno
== EINTR
) {
1009 for (i
= 0; i
< nb_fd
; i
++) {
1010 /* Fetch once the poll data */
1011 revents
= LTTNG_POLL_GETEV(&events
, i
);
1012 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1014 health_code_update();
1017 /* No activity for this FD (poll implementation). */
1021 /* Thread quit pipe has been closed. Killing thread. */
1022 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1028 /* Event on the registration socket */
1029 if (pollfd
== consumer_data
->err_sock
) {
1030 if (revents
& LPOLLIN
) {
1032 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1033 ERR("consumer err socket poll error");
1036 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1042 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1048 * Set the CLOEXEC flag. Return code is useless because either way, the
1051 (void) utils_set_fd_cloexec(sock
);
1053 health_code_update();
1055 DBG2("Receiving code from consumer err_sock");
1057 /* Getting status code from kconsumerd */
1058 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1059 sizeof(enum lttcomm_return_code
));
1064 health_code_update();
1065 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1066 ERR("consumer error when waiting for SOCK_READY : %s",
1067 lttcomm_get_readable_code(-code
));
1071 /* Connect both command and metadata sockets. */
1072 consumer_data
->cmd_sock
=
1073 lttcomm_connect_unix_sock(
1074 consumer_data
->cmd_unix_sock_path
);
1075 consumer_data
->metadata_fd
=
1076 lttcomm_connect_unix_sock(
1077 consumer_data
->cmd_unix_sock_path
);
1078 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1079 PERROR("consumer connect cmd socket");
1080 /* On error, signal condition and quit. */
1081 signal_consumer_condition(consumer_data
, -1);
1085 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1087 /* Create metadata socket lock. */
1088 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1089 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1090 PERROR("zmalloc pthread mutex");
1093 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1095 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1096 DBG("Consumer metadata socket ready (fd: %d)",
1097 consumer_data
->metadata_fd
);
1100 * Remove the consumerd error sock since we've established a connection.
1102 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1107 /* Add new accepted error socket. */
1108 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1113 /* Add metadata socket that is successfully connected. */
1114 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1115 LPOLLIN
| LPOLLRDHUP
);
1120 health_code_update();
1123 * Transfer the write-end of the channel monitoring and rotate pipe
1124 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1126 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1127 if (!cmd_socket_wrapper
) {
1130 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1132 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1133 consumer_data
->channel_monitor_pipe
);
1138 /* Discard the socket wrapper as it is no longer needed. */
1139 consumer_destroy_socket(cmd_socket_wrapper
);
1140 cmd_socket_wrapper
= NULL
;
1142 /* The thread is completely initialized, signal that it is ready. */
1143 signal_consumer_condition(consumer_data
, 1);
1145 /* Infinite blocking call, waiting for transmission */
1148 health_code_update();
1150 /* Exit the thread because the thread quit pipe has been triggered. */
1152 /* Not a health error. */
1157 health_poll_entry();
1158 ret
= lttng_poll_wait(&events
, -1);
1162 * Restart interrupted system call.
1164 if (errno
== EINTR
) {
1172 for (i
= 0; i
< nb_fd
; i
++) {
1173 /* Fetch once the poll data */
1174 revents
= LTTNG_POLL_GETEV(&events
, i
);
1175 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1177 health_code_update();
1180 /* No activity for this FD (poll implementation). */
1185 * Thread quit pipe has been triggered, flag that we should stop
1186 * but continue the current loop to handle potential data from
1189 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1191 if (pollfd
== sock
) {
1192 /* Event on the consumerd socket */
1193 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1194 && !(revents
& LPOLLIN
)) {
1195 ERR("consumer err socket second poll error");
1198 health_code_update();
1199 /* Wait for any kconsumerd error */
1200 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1201 sizeof(enum lttcomm_return_code
));
1203 ERR("consumer closed the command socket");
1207 ERR("consumer return code : %s",
1208 lttcomm_get_readable_code(-code
));
1211 } else if (pollfd
== consumer_data
->metadata_fd
) {
1212 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1213 && !(revents
& LPOLLIN
)) {
1214 ERR("consumer err metadata socket second poll error");
1217 /* UST metadata requests */
1218 ret
= ust_consumer_metadata_request(
1219 &consumer_data
->metadata_sock
);
1221 ERR("Handling metadata request");
1225 /* No need for an else branch all FDs are tested prior. */
1227 health_code_update();
1233 * We lock here because we are about to close the sockets and some other
1234 * thread might be using them so get exclusive access which will abort all
1235 * other consumer command by other threads.
1237 pthread_mutex_lock(&consumer_data
->lock
);
1239 /* Immediately set the consumerd state to stopped */
1240 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1241 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1242 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1243 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1244 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1246 /* Code flow error... */
1250 if (consumer_data
->err_sock
>= 0) {
1251 ret
= close(consumer_data
->err_sock
);
1255 consumer_data
->err_sock
= -1;
1257 if (consumer_data
->cmd_sock
>= 0) {
1258 ret
= close(consumer_data
->cmd_sock
);
1262 consumer_data
->cmd_sock
= -1;
1264 if (consumer_data
->metadata_sock
.fd_ptr
&&
1265 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1266 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1278 unlink(consumer_data
->err_unix_sock_path
);
1279 unlink(consumer_data
->cmd_unix_sock_path
);
1280 pthread_mutex_unlock(&consumer_data
->lock
);
1282 /* Cleanup metadata socket mutex. */
1283 if (consumer_data
->metadata_sock
.lock
) {
1284 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1285 free(consumer_data
->metadata_sock
.lock
);
1287 lttng_poll_clean(&events
);
1289 if (cmd_socket_wrapper
) {
1290 consumer_destroy_socket(cmd_socket_wrapper
);
1295 ERR("Health error occurred in %s", __func__
);
1297 health_unregister(health_sessiond
);
1298 DBG("consumer thread cleanup completed");
1300 rcu_thread_offline();
1301 rcu_unregister_thread();
1307 * This thread receives application command sockets (FDs) on the
1308 * apps_cmd_pipe and waits (polls) on them until they are closed
1309 * or an error occurs.
1311 * At that point, it flushes the data (tracing and metadata) associated
1312 * with this application and tears down ust app sessions and other
1313 * associated data structures through ust_app_unregister().
1315 * Note that this thread never sends commands to the applications
1316 * through the command sockets; it merely listens for hang-ups
1317 * and errors on those sockets and cleans-up as they occur.
1319 static void *thread_manage_apps(void *data
)
1321 int i
, ret
, pollfd
, err
= -1;
1323 uint32_t revents
, nb_fd
;
1324 struct lttng_poll_event events
;
1326 DBG("[thread] Manage application started");
1328 rcu_register_thread();
1329 rcu_thread_online();
1331 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1333 if (testpoint(sessiond_thread_manage_apps
)) {
1334 goto error_testpoint
;
1337 health_code_update();
1339 ret
= sessiond_set_thread_pollset(&events
, 2);
1341 goto error_poll_create
;
1344 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1349 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1353 health_code_update();
1356 DBG("Apps thread polling");
1358 /* Inifinite blocking call, waiting for transmission */
1360 health_poll_entry();
1361 ret
= lttng_poll_wait(&events
, -1);
1362 DBG("Apps thread return from poll on %d fds",
1363 LTTNG_POLL_GETNB(&events
));
1367 * Restart interrupted system call.
1369 if (errno
== EINTR
) {
1377 for (i
= 0; i
< nb_fd
; i
++) {
1378 /* Fetch once the poll data */
1379 revents
= LTTNG_POLL_GETEV(&events
, i
);
1380 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1382 health_code_update();
1385 /* No activity for this FD (poll implementation). */
1389 /* Thread quit pipe has been closed. Killing thread. */
1390 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1396 /* Inspect the apps cmd pipe */
1397 if (pollfd
== apps_cmd_pipe
[0]) {
1398 if (revents
& LPOLLIN
) {
1402 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1403 if (size_ret
< sizeof(sock
)) {
1404 PERROR("read apps cmd pipe");
1408 health_code_update();
1411 * Since this is a command socket (write then read),
1412 * we only monitor the error events of the socket.
1414 ret
= lttng_poll_add(&events
, sock
,
1415 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1420 DBG("Apps with sock %d added to poll set", sock
);
1421 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1422 ERR("Apps command pipe error");
1425 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1430 * At this point, we know that a registered application made
1431 * the event at poll_wait.
1433 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1434 /* Removing from the poll set */
1435 ret
= lttng_poll_del(&events
, pollfd
);
1440 /* Socket closed on remote end. */
1441 ust_app_unregister(pollfd
);
1443 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1448 health_code_update();
1454 lttng_poll_clean(&events
);
1457 utils_close_pipe(apps_cmd_pipe
);
1458 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1461 * We don't clean the UST app hash table here since already registered
1462 * applications can still be controlled so let them be until the session
1463 * daemon dies or the applications stop.
1468 ERR("Health error occurred in %s", __func__
);
1470 health_unregister(health_sessiond
);
1471 DBG("Application communication apps thread cleanup complete");
1472 rcu_thread_offline();
1473 rcu_unregister_thread();
1478 * Send a socket to a thread This is called from the dispatch UST registration
1479 * thread once all sockets are set for the application.
1481 * The sock value can be invalid, we don't really care, the thread will handle
1482 * it and make the necessary cleanup if so.
1484 * On success, return 0 else a negative value being the errno message of the
1487 static int send_socket_to_thread(int fd
, int sock
)
1492 * It's possible that the FD is set as invalid with -1 concurrently just
1493 * before calling this function being a shutdown state of the thread.
1500 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1501 if (ret
< sizeof(sock
)) {
1502 PERROR("write apps pipe %d", fd
);
1509 /* All good. Don't send back the write positive ret value. */
1516 * Sanitize the wait queue of the dispatch registration thread meaning removing
1517 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1518 * notify socket is never received.
1520 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1522 int ret
, nb_fd
= 0, i
;
1523 unsigned int fd_added
= 0;
1524 struct lttng_poll_event events
;
1525 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1529 lttng_poll_init(&events
);
1531 /* Just skip everything for an empty queue. */
1532 if (!wait_queue
->count
) {
1536 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1541 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1542 &wait_queue
->head
, head
) {
1543 assert(wait_node
->app
);
1544 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1545 LPOLLHUP
| LPOLLERR
);
1558 * Poll but don't block so we can quickly identify the faulty events and
1559 * clean them afterwards from the wait queue.
1561 ret
= lttng_poll_wait(&events
, 0);
1567 for (i
= 0; i
< nb_fd
; i
++) {
1568 /* Get faulty FD. */
1569 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1570 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1573 /* No activity for this FD (poll implementation). */
1577 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1578 &wait_queue
->head
, head
) {
1579 if (pollfd
== wait_node
->app
->sock
&&
1580 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1581 cds_list_del(&wait_node
->head
);
1582 wait_queue
->count
--;
1583 ust_app_destroy(wait_node
->app
);
1586 * Silence warning of use-after-free in
1587 * cds_list_for_each_entry_safe which uses
1588 * __typeof__(*wait_node).
1593 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1600 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1604 lttng_poll_clean(&events
);
1608 lttng_poll_clean(&events
);
1610 ERR("Unable to sanitize wait queue");
1615 * Dispatch request from the registration threads to the application
1616 * communication thread.
1618 static void *thread_dispatch_ust_registration(void *data
)
1621 struct cds_wfcq_node
*node
;
1622 struct ust_command
*ust_cmd
= NULL
;
1623 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1624 struct ust_reg_wait_queue wait_queue
= {
1628 rcu_register_thread();
1630 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1632 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1633 goto error_testpoint
;
1636 health_code_update();
1638 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1640 DBG("[thread] Dispatch UST command started");
1643 health_code_update();
1645 /* Atomically prepare the queue futex */
1646 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1648 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1653 struct ust_app
*app
= NULL
;
1657 * Make sure we don't have node(s) that have hung up before receiving
1658 * the notify socket. This is to clean the list in order to avoid
1659 * memory leaks from notify socket that are never seen.
1661 sanitize_wait_queue(&wait_queue
);
1663 health_code_update();
1664 /* Dequeue command for registration */
1665 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1667 DBG("Woken up but nothing in the UST command queue");
1668 /* Continue thread execution */
1672 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1674 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1675 " gid:%d sock:%d name:%s (version %d.%d)",
1676 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1677 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1678 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1679 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1681 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1682 wait_node
= zmalloc(sizeof(*wait_node
));
1684 PERROR("zmalloc wait_node dispatch");
1685 ret
= close(ust_cmd
->sock
);
1687 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1689 lttng_fd_put(LTTNG_FD_APPS
, 1);
1693 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1695 /* Create application object if socket is CMD. */
1696 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1698 if (!wait_node
->app
) {
1699 ret
= close(ust_cmd
->sock
);
1701 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1703 lttng_fd_put(LTTNG_FD_APPS
, 1);
1709 * Add application to the wait queue so we can set the notify
1710 * socket before putting this object in the global ht.
1712 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1717 * We have to continue here since we don't have the notify
1718 * socket and the application MUST be added to the hash table
1719 * only at that moment.
1724 * Look for the application in the local wait queue and set the
1725 * notify socket if found.
1727 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1728 &wait_queue
.head
, head
) {
1729 health_code_update();
1730 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1731 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1732 cds_list_del(&wait_node
->head
);
1734 app
= wait_node
->app
;
1736 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1742 * With no application at this stage the received socket is
1743 * basically useless so close it before we free the cmd data
1744 * structure for good.
1747 ret
= close(ust_cmd
->sock
);
1749 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1751 lttng_fd_put(LTTNG_FD_APPS
, 1);
1758 * @session_lock_list
1760 * Lock the global session list so from the register up to the
1761 * registration done message, no thread can see the application
1762 * and change its state.
1764 session_lock_list();
1768 * Add application to the global hash table. This needs to be
1769 * done before the update to the UST registry can locate the
1774 /* Set app version. This call will print an error if needed. */
1775 (void) ust_app_version(app
);
1777 /* Send notify socket through the notify pipe. */
1778 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1782 session_unlock_list();
1784 * No notify thread, stop the UST tracing. However, this is
1785 * not an internal error of the this thread thus setting
1786 * the health error code to a normal exit.
1793 * Update newly registered application with the tracing
1794 * registry info already enabled information.
1796 update_ust_app(app
->sock
);
1799 * Don't care about return value. Let the manage apps threads
1800 * handle app unregistration upon socket close.
1802 (void) ust_app_register_done(app
);
1805 * Even if the application socket has been closed, send the app
1806 * to the thread and unregistration will take place at that
1809 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1812 session_unlock_list();
1814 * No apps. thread, stop the UST tracing. However, this is
1815 * not an internal error of the this thread thus setting
1816 * the health error code to a normal exit.
1823 session_unlock_list();
1825 } while (node
!= NULL
);
1827 health_poll_entry();
1828 /* Futex wait on queue. Blocking call on futex() */
1829 futex_nto1_wait(&ust_cmd_queue
.futex
);
1832 /* Normal exit, no error */
1836 /* Clean up wait queue. */
1837 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1838 &wait_queue
.head
, head
) {
1839 cds_list_del(&wait_node
->head
);
1844 /* Empty command queue. */
1846 /* Dequeue command for registration */
1847 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1851 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1852 ret
= close(ust_cmd
->sock
);
1854 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
1856 lttng_fd_put(LTTNG_FD_APPS
, 1);
1861 DBG("Dispatch thread dying");
1864 ERR("Health error occurred in %s", __func__
);
1866 health_unregister(health_sessiond
);
1867 rcu_unregister_thread();
1872 * This thread manage application registration.
1874 static void *thread_registration_apps(void *data
)
1876 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1877 uint32_t revents
, nb_fd
;
1878 struct lttng_poll_event events
;
1880 * Get allocated in this thread, enqueued to a global queue, dequeued and
1881 * freed in the manage apps thread.
1883 struct ust_command
*ust_cmd
= NULL
;
1885 DBG("[thread] Manage application registration started");
1887 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1889 if (testpoint(sessiond_thread_registration_apps
)) {
1890 goto error_testpoint
;
1893 ret
= lttcomm_listen_unix_sock(apps_sock
);
1899 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1900 * more will be added to this poll set.
1902 ret
= sessiond_set_thread_pollset(&events
, 2);
1904 goto error_create_poll
;
1907 /* Add the application registration socket */
1908 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1910 goto error_poll_add
;
1913 /* Notify all applications to register */
1914 ret
= notify_ust_apps(1);
1916 ERR("Failed to notify applications or create the wait shared memory.\n"
1917 "Execution continues but there might be problem for already\n"
1918 "running applications that wishes to register.");
1922 DBG("Accepting application registration");
1924 /* Inifinite blocking call, waiting for transmission */
1926 health_poll_entry();
1927 ret
= lttng_poll_wait(&events
, -1);
1931 * Restart interrupted system call.
1933 if (errno
== EINTR
) {
1941 for (i
= 0; i
< nb_fd
; i
++) {
1942 health_code_update();
1944 /* Fetch once the poll data */
1945 revents
= LTTNG_POLL_GETEV(&events
, i
);
1946 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1949 /* No activity for this FD (poll implementation). */
1953 /* Thread quit pipe has been closed. Killing thread. */
1954 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1960 /* Event on the registration socket */
1961 if (pollfd
== apps_sock
) {
1962 if (revents
& LPOLLIN
) {
1963 sock
= lttcomm_accept_unix_sock(apps_sock
);
1969 * Set socket timeout for both receiving and ending.
1970 * app_socket_timeout is in seconds, whereas
1971 * lttcomm_setsockopt_rcv_timeout and
1972 * lttcomm_setsockopt_snd_timeout expect msec as
1975 if (config
.app_socket_timeout
>= 0) {
1976 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1977 config
.app_socket_timeout
* 1000);
1978 (void) lttcomm_setsockopt_snd_timeout(sock
,
1979 config
.app_socket_timeout
* 1000);
1983 * Set the CLOEXEC flag. Return code is useless because
1984 * either way, the show must go on.
1986 (void) utils_set_fd_cloexec(sock
);
1988 /* Create UST registration command for enqueuing */
1989 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1990 if (ust_cmd
== NULL
) {
1991 PERROR("ust command zmalloc");
2000 * Using message-based transmissions to ensure we don't
2001 * have to deal with partially received messages.
2003 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2005 ERR("Exhausted file descriptors allowed for applications.");
2015 health_code_update();
2016 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2019 /* Close socket of the application. */
2024 lttng_fd_put(LTTNG_FD_APPS
, 1);
2028 health_code_update();
2030 ust_cmd
->sock
= sock
;
2033 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2034 " gid:%d sock:%d name:%s (version %d.%d)",
2035 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2036 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2037 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2038 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2041 * Lock free enqueue the registration request. The red pill
2042 * has been taken! This apps will be part of the *system*.
2044 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2047 * Wake the registration queue futex. Implicit memory
2048 * barrier with the exchange in cds_wfcq_enqueue.
2050 futex_nto1_wake(&ust_cmd_queue
.futex
);
2051 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2052 ERR("Register apps socket poll error");
2055 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2064 /* Notify that the registration thread is gone */
2067 if (apps_sock
>= 0) {
2068 ret
= close(apps_sock
);
2078 lttng_fd_put(LTTNG_FD_APPS
, 1);
2080 unlink(config
.apps_unix_sock_path
.value
);
2083 lttng_poll_clean(&events
);
2087 DBG("UST Registration thread cleanup complete");
2090 ERR("Health error occurred in %s", __func__
);
2092 health_unregister(health_sessiond
);
2098 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2099 * exec or it will fails.
2101 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2104 struct timespec timeout
;
2107 * Make sure we set the readiness flag to 0 because we are NOT ready.
2108 * This access to consumer_thread_is_ready does not need to be
2109 * protected by consumer_data.cond_mutex (yet) since the consumer
2110 * management thread has not been started at this point.
2112 consumer_data
->consumer_thread_is_ready
= 0;
2114 /* Setup pthread condition */
2115 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2118 PERROR("pthread_condattr_init consumer data");
2123 * Set the monotonic clock in order to make sure we DO NOT jump in time
2124 * between the clock_gettime() call and the timedwait call. See bug #324
2125 * for a more details and how we noticed it.
2127 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2130 PERROR("pthread_condattr_setclock consumer data");
2134 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2137 PERROR("pthread_cond_init consumer data");
2141 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2142 thread_manage_consumer
, consumer_data
);
2145 PERROR("pthread_create consumer");
2150 /* We are about to wait on a pthread condition */
2151 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2153 /* Get time for sem_timedwait absolute timeout */
2154 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2156 * Set the timeout for the condition timed wait even if the clock gettime
2157 * call fails since we might loop on that call and we want to avoid to
2158 * increment the timeout too many times.
2160 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2163 * The following loop COULD be skipped in some conditions so this is why we
2164 * set ret to 0 in order to make sure at least one round of the loop is
2170 * Loop until the condition is reached or when a timeout is reached. Note
2171 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2172 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2173 * possible. This loop does not take any chances and works with both of
2176 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2177 if (clock_ret
< 0) {
2178 PERROR("clock_gettime spawn consumer");
2179 /* Infinite wait for the consumerd thread to be ready */
2180 ret
= pthread_cond_wait(&consumer_data
->cond
,
2181 &consumer_data
->cond_mutex
);
2183 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2184 &consumer_data
->cond_mutex
, &timeout
);
2188 /* Release the pthread condition */
2189 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2193 if (ret
== ETIMEDOUT
) {
2197 * Call has timed out so we kill the kconsumerd_thread and return
2200 ERR("Condition timed out. The consumer thread was never ready."
2202 pth_ret
= pthread_cancel(consumer_data
->thread
);
2204 PERROR("pthread_cancel consumer thread");
2207 PERROR("pthread_cond_wait failed consumer thread");
2209 /* Caller is expecting a negative value on failure. */
2214 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2215 if (consumer_data
->pid
== 0) {
2216 ERR("Consumerd did not start");
2217 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2220 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2229 * Join consumer thread
2231 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2235 /* Consumer pid must be a real one. */
2236 if (consumer_data
->pid
> 0) {
2238 ret
= kill(consumer_data
->pid
, SIGTERM
);
2240 PERROR("Error killing consumer daemon");
2243 return pthread_join(consumer_data
->thread
, &status
);
2250 * Fork and exec a consumer daemon (consumerd).
2252 * Return pid if successful else -1.
2254 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2258 const char *consumer_to_use
;
2259 const char *verbosity
;
2262 DBG("Spawning consumerd");
2269 if (config
.verbose_consumer
) {
2270 verbosity
= "--verbose";
2271 } else if (lttng_opt_quiet
) {
2272 verbosity
= "--quiet";
2277 switch (consumer_data
->type
) {
2278 case LTTNG_CONSUMER_KERNEL
:
2280 * Find out which consumerd to execute. We will first try the
2281 * 64-bit path, then the sessiond's installation directory, and
2282 * fallback on the 32-bit one,
2284 DBG3("Looking for a kernel consumer at these locations:");
2285 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2286 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2287 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2288 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2289 DBG3("Found location #1");
2290 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2291 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2292 DBG3("Found location #2");
2293 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2294 } else if (config
.consumerd32_bin_path
.value
&&
2295 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2296 DBG3("Found location #3");
2297 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2299 DBG("Could not find any valid consumerd executable");
2303 DBG("Using kernel consumer at: %s", consumer_to_use
);
2304 (void) execl(consumer_to_use
,
2305 "lttng-consumerd", verbosity
, "-k",
2306 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2307 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2308 "--group", config
.tracing_group_name
.value
,
2311 case LTTNG_CONSUMER64_UST
:
2313 if (config
.consumerd64_lib_dir
.value
) {
2318 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2322 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2323 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2328 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2329 if (tmp
[0] != '\0') {
2330 strcat(tmpnew
, ":");
2331 strcat(tmpnew
, tmp
);
2333 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2340 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2341 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2342 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2343 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2344 "--group", config
.tracing_group_name
.value
,
2348 case LTTNG_CONSUMER32_UST
:
2350 if (config
.consumerd32_lib_dir
.value
) {
2355 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2359 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2360 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2365 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2366 if (tmp
[0] != '\0') {
2367 strcat(tmpnew
, ":");
2368 strcat(tmpnew
, tmp
);
2370 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2377 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2378 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2379 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2380 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2381 "--group", config
.tracing_group_name
.value
,
2386 ERR("unknown consumer type");
2390 PERROR("Consumer execl()");
2392 /* Reaching this point, we got a failure on our execl(). */
2394 } else if (pid
> 0) {
2397 PERROR("start consumer fork");
2405 * Spawn the consumerd daemon and session daemon thread.
2407 static int start_consumerd(struct consumer_data
*consumer_data
)
2412 * Set the listen() state on the socket since there is a possible race
2413 * between the exec() of the consumer daemon and this call if place in the
2414 * consumer thread. See bug #366 for more details.
2416 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2421 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2422 if (consumer_data
->pid
!= 0) {
2423 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2427 ret
= spawn_consumerd(consumer_data
);
2429 ERR("Spawning consumerd failed");
2430 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2434 /* Setting up the consumer_data pid */
2435 consumer_data
->pid
= ret
;
2436 DBG2("Consumer pid %d", consumer_data
->pid
);
2437 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2439 DBG2("Spawning consumer control thread");
2440 ret
= spawn_consumer_thread(consumer_data
);
2442 ERR("Fatal error spawning consumer control thread");
2450 /* Cleanup already created sockets on error. */
2451 if (consumer_data
->err_sock
>= 0) {
2454 err
= close(consumer_data
->err_sock
);
2456 PERROR("close consumer data error socket");
2463 * Setup necessary data for kernel tracer action.
2465 static int init_kernel_tracer(void)
2469 /* Modprobe lttng kernel modules */
2470 ret
= modprobe_lttng_control();
2475 /* Open debugfs lttng */
2476 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2477 if (kernel_tracer_fd
< 0) {
2478 DBG("Failed to open %s", module_proc_lttng
);
2482 /* Validate kernel version */
2483 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2484 &kernel_tracer_abi_version
);
2489 ret
= modprobe_lttng_data();
2494 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2501 WARN("Kernel tracer does not support buffer monitoring. "
2502 "The monitoring timer of channels in the kernel domain "
2503 "will be set to 0 (disabled).");
2506 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2510 modprobe_remove_lttng_control();
2511 ret
= close(kernel_tracer_fd
);
2515 kernel_tracer_fd
= -1;
2516 return LTTNG_ERR_KERN_VERSION
;
2519 ret
= close(kernel_tracer_fd
);
2525 modprobe_remove_lttng_control();
2528 WARN("No kernel tracer available");
2529 kernel_tracer_fd
= -1;
2531 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2533 return LTTNG_ERR_KERN_NA
;
2539 * Copy consumer output from the tracing session to the domain session. The
2540 * function also applies the right modification on a per domain basis for the
2541 * trace files destination directory.
2543 * Should *NOT* be called with RCU read-side lock held.
2545 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2548 const char *dir_name
;
2549 struct consumer_output
*consumer
;
2552 assert(session
->consumer
);
2555 case LTTNG_DOMAIN_KERNEL
:
2556 DBG3("Copying tracing session consumer output in kernel session");
2558 * XXX: We should audit the session creation and what this function
2559 * does "extra" in order to avoid a destroy since this function is used
2560 * in the domain session creation (kernel and ust) only. Same for UST
2563 if (session
->kernel_session
->consumer
) {
2564 consumer_output_put(session
->kernel_session
->consumer
);
2566 session
->kernel_session
->consumer
=
2567 consumer_copy_output(session
->consumer
);
2568 /* Ease our life a bit for the next part */
2569 consumer
= session
->kernel_session
->consumer
;
2570 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2572 case LTTNG_DOMAIN_JUL
:
2573 case LTTNG_DOMAIN_LOG4J
:
2574 case LTTNG_DOMAIN_PYTHON
:
2575 case LTTNG_DOMAIN_UST
:
2576 DBG3("Copying tracing session consumer output in UST session");
2577 if (session
->ust_session
->consumer
) {
2578 consumer_output_put(session
->ust_session
->consumer
);
2580 session
->ust_session
->consumer
=
2581 consumer_copy_output(session
->consumer
);
2582 /* Ease our life a bit for the next part */
2583 consumer
= session
->ust_session
->consumer
;
2584 dir_name
= DEFAULT_UST_TRACE_DIR
;
2587 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2591 /* Append correct directory to subdir */
2592 strncat(consumer
->subdir
, dir_name
,
2593 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2594 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2603 * Create an UST session and add it to the session ust list.
2605 * Should *NOT* be called with RCU read-side lock held.
2607 static int create_ust_session(struct ltt_session
*session
,
2608 struct lttng_domain
*domain
)
2611 struct ltt_ust_session
*lus
= NULL
;
2615 assert(session
->consumer
);
2617 switch (domain
->type
) {
2618 case LTTNG_DOMAIN_JUL
:
2619 case LTTNG_DOMAIN_LOG4J
:
2620 case LTTNG_DOMAIN_PYTHON
:
2621 case LTTNG_DOMAIN_UST
:
2624 ERR("Unknown UST domain on create session %d", domain
->type
);
2625 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2629 DBG("Creating UST session");
2631 lus
= trace_ust_create_session(session
->id
);
2633 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2637 lus
->uid
= session
->uid
;
2638 lus
->gid
= session
->gid
;
2639 lus
->output_traces
= session
->output_traces
;
2640 lus
->snapshot_mode
= session
->snapshot_mode
;
2641 lus
->live_timer_interval
= session
->live_timer
;
2642 session
->ust_session
= lus
;
2643 if (session
->shm_path
[0]) {
2644 strncpy(lus
->root_shm_path
, session
->shm_path
,
2645 sizeof(lus
->root_shm_path
));
2646 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2647 strncpy(lus
->shm_path
, session
->shm_path
,
2648 sizeof(lus
->shm_path
));
2649 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2650 strncat(lus
->shm_path
, "/ust",
2651 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2653 /* Copy session output to the newly created UST session */
2654 ret
= copy_session_consumer(domain
->type
, session
);
2655 if (ret
!= LTTNG_OK
) {
2663 session
->ust_session
= NULL
;
2668 * Create a kernel tracer session then create the default channel.
2670 static int create_kernel_session(struct ltt_session
*session
)
2674 DBG("Creating kernel session");
2676 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2678 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2682 /* Code flow safety */
2683 assert(session
->kernel_session
);
2685 /* Copy session output to the newly created Kernel session */
2686 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2687 if (ret
!= LTTNG_OK
) {
2691 session
->kernel_session
->uid
= session
->uid
;
2692 session
->kernel_session
->gid
= session
->gid
;
2693 session
->kernel_session
->output_traces
= session
->output_traces
;
2694 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2699 trace_kernel_destroy_session(session
->kernel_session
);
2700 session
->kernel_session
= NULL
;
2705 * Count number of session permitted by uid/gid.
2707 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2710 struct ltt_session
*session
;
2711 const struct ltt_session_list
*session_list
= session_get_list();
2713 DBG("Counting number of available session for UID %d GID %d",
2715 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
2716 if (!session_get(session
)) {
2719 session_lock(session
);
2720 /* Only count the sessions the user can control. */
2721 if (session_access_ok(session
, uid
, gid
) &&
2722 !session
->destroyed
) {
2725 session_unlock(session
);
2726 session_put(session
);
2731 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2732 int *sock_error
, struct lttng_event
*event
)
2735 struct lttng_userspace_probe_location
*probe_location
;
2736 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2737 struct lttng_dynamic_buffer probe_location_buffer
;
2738 struct lttng_buffer_view buffer_view
;
2741 * Create a buffer to store the serialized version of the probe
2744 lttng_dynamic_buffer_init(&probe_location_buffer
);
2745 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2746 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2748 ret
= LTTNG_ERR_NOMEM
;
2753 * Receive the probe location.
2755 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2756 probe_location_buffer
.size
);
2758 DBG("Nothing recv() from client var len data... continuing");
2760 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2761 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2765 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2766 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2769 * Extract the probe location from the serialized version.
2771 ret
= lttng_userspace_probe_location_create_from_buffer(
2772 &buffer_view
, &probe_location
);
2774 WARN("Failed to create a userspace probe location from the received buffer");
2775 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2776 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2781 * Receive the file descriptor to the target binary from the client.
2783 DBG("Receiving userspace probe target FD from client ...");
2784 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2786 DBG("Nothing recv() from client userspace probe fd... continuing");
2788 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2793 * Set the file descriptor received from the client through the unix
2794 * socket in the probe location.
2796 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
2798 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2803 * From the kernel tracer's perspective, all userspace probe event types
2804 * are all the same: a file and an offset.
2806 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
2807 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
2808 ret
= lttng_userspace_probe_location_function_set_binary_fd(
2809 probe_location
, fd
);
2811 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
2812 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
2813 probe_location
, fd
);
2816 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2821 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2825 /* Attach the probe location to the event. */
2826 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
2828 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2832 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2838 * Check if the current kernel tracer supports the session rotation feature.
2839 * Return 1 if it does, 0 otherwise.
2841 static int check_rotate_compatible(void)
2845 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2846 DBG("Kernel tracer version is not compatible with the rotation feature");
2854 * Process the command requested by the lttng client within the command
2855 * context structure. This function make sure that the return structure (llm)
2856 * is set and ready for transmission before returning.
2858 * Return any error encountered or 0 for success.
2860 * "sock" is only used for special-case var. len data.
2862 * Should *NOT* be called with RCU read-side lock held.
2864 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2868 int need_tracing_session
= 1;
2871 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2873 assert(!rcu_read_ongoing());
2877 switch (cmd_ctx
->lsm
->cmd_type
) {
2878 case LTTNG_CREATE_SESSION
:
2879 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2880 case LTTNG_CREATE_SESSION_LIVE
:
2881 case LTTNG_DESTROY_SESSION
:
2882 case LTTNG_LIST_SESSIONS
:
2883 case LTTNG_LIST_DOMAINS
:
2884 case LTTNG_START_TRACE
:
2885 case LTTNG_STOP_TRACE
:
2886 case LTTNG_DATA_PENDING
:
2887 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2888 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2889 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2890 case LTTNG_SNAPSHOT_RECORD
:
2891 case LTTNG_SAVE_SESSION
:
2892 case LTTNG_SET_SESSION_SHM_PATH
:
2893 case LTTNG_REGENERATE_METADATA
:
2894 case LTTNG_REGENERATE_STATEDUMP
:
2895 case LTTNG_REGISTER_TRIGGER
:
2896 case LTTNG_UNREGISTER_TRIGGER
:
2897 case LTTNG_ROTATE_SESSION
:
2898 case LTTNG_ROTATION_GET_INFO
:
2899 case LTTNG_ROTATION_SET_SCHEDULE
:
2900 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2907 if (config
.no_kernel
&& need_domain
2908 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2910 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2912 ret
= LTTNG_ERR_KERN_NA
;
2917 /* Deny register consumer if we already have a spawned consumer. */
2918 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2919 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2920 if (kconsumer_data
.pid
> 0) {
2921 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2922 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2925 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2929 * Check for command that don't needs to allocate a returned payload. We do
2930 * this here so we don't have to make the call for no payload at each
2933 switch(cmd_ctx
->lsm
->cmd_type
) {
2934 case LTTNG_LIST_SESSIONS
:
2935 case LTTNG_LIST_TRACEPOINTS
:
2936 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2937 case LTTNG_LIST_DOMAINS
:
2938 case LTTNG_LIST_CHANNELS
:
2939 case LTTNG_LIST_EVENTS
:
2940 case LTTNG_LIST_SYSCALLS
:
2941 case LTTNG_LIST_TRACKER_PIDS
:
2942 case LTTNG_DATA_PENDING
:
2943 case LTTNG_ROTATE_SESSION
:
2944 case LTTNG_ROTATION_GET_INFO
:
2945 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2948 /* Setup lttng message with no payload */
2949 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2951 /* This label does not try to unlock the session */
2952 goto init_setup_error
;
2956 /* Commands that DO NOT need a session. */
2957 switch (cmd_ctx
->lsm
->cmd_type
) {
2958 case LTTNG_CREATE_SESSION
:
2959 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2960 case LTTNG_CREATE_SESSION_LIVE
:
2961 case LTTNG_LIST_SESSIONS
:
2962 case LTTNG_LIST_TRACEPOINTS
:
2963 case LTTNG_LIST_SYSCALLS
:
2964 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2965 case LTTNG_SAVE_SESSION
:
2966 case LTTNG_REGISTER_TRIGGER
:
2967 case LTTNG_UNREGISTER_TRIGGER
:
2968 need_tracing_session
= 0;
2971 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2973 * We keep the session list lock across _all_ commands
2974 * for now, because the per-session lock does not
2975 * handle teardown properly.
2977 session_lock_list();
2978 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2979 if (cmd_ctx
->session
== NULL
) {
2980 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2983 /* Acquire lock for the session */
2984 session_lock(cmd_ctx
->session
);
2990 * Commands that need a valid session but should NOT create one if none
2991 * exists. Instead of creating one and destroying it when the command is
2992 * handled, process that right before so we save some round trip in useless
2995 switch (cmd_ctx
->lsm
->cmd_type
) {
2996 case LTTNG_DISABLE_CHANNEL
:
2997 case LTTNG_DISABLE_EVENT
:
2998 switch (cmd_ctx
->lsm
->domain
.type
) {
2999 case LTTNG_DOMAIN_KERNEL
:
3000 if (!cmd_ctx
->session
->kernel_session
) {
3001 ret
= LTTNG_ERR_NO_CHANNEL
;
3005 case LTTNG_DOMAIN_JUL
:
3006 case LTTNG_DOMAIN_LOG4J
:
3007 case LTTNG_DOMAIN_PYTHON
:
3008 case LTTNG_DOMAIN_UST
:
3009 if (!cmd_ctx
->session
->ust_session
) {
3010 ret
= LTTNG_ERR_NO_CHANNEL
;
3015 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3027 * Check domain type for specific "pre-action".
3029 switch (cmd_ctx
->lsm
->domain
.type
) {
3030 case LTTNG_DOMAIN_KERNEL
:
3032 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3036 /* Kernel tracer check */
3037 if (kernel_tracer_fd
== -1) {
3038 /* Basically, load kernel tracer modules */
3039 ret
= init_kernel_tracer();
3045 /* Consumer is in an ERROR state. Report back to client */
3046 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3047 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3051 /* Need a session for kernel command */
3052 if (need_tracing_session
) {
3053 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3054 ret
= create_kernel_session(cmd_ctx
->session
);
3056 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3061 /* Start the kernel consumer daemon */
3062 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3063 if (kconsumer_data
.pid
== 0 &&
3064 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3065 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3066 ret
= start_consumerd(&kconsumer_data
);
3068 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3071 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3073 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3077 * The consumer was just spawned so we need to add the socket to
3078 * the consumer output of the session if exist.
3080 ret
= consumer_create_socket(&kconsumer_data
,
3081 cmd_ctx
->session
->kernel_session
->consumer
);
3088 case LTTNG_DOMAIN_JUL
:
3089 case LTTNG_DOMAIN_LOG4J
:
3090 case LTTNG_DOMAIN_PYTHON
:
3091 case LTTNG_DOMAIN_UST
:
3093 if (!ust_app_supported()) {
3094 ret
= LTTNG_ERR_NO_UST
;
3097 /* Consumer is in an ERROR state. Report back to client */
3098 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3099 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3103 if (need_tracing_session
) {
3104 /* Create UST session if none exist. */
3105 if (cmd_ctx
->session
->ust_session
== NULL
) {
3106 ret
= create_ust_session(cmd_ctx
->session
,
3107 &cmd_ctx
->lsm
->domain
);
3108 if (ret
!= LTTNG_OK
) {
3113 /* Start the UST consumer daemons */
3115 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3116 if (config
.consumerd64_bin_path
.value
&&
3117 ustconsumer64_data
.pid
== 0 &&
3118 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3119 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3120 ret
= start_consumerd(&ustconsumer64_data
);
3122 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3123 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3127 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3128 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3130 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3134 * Setup socket for consumer 64 bit. No need for atomic access
3135 * since it was set above and can ONLY be set in this thread.
3137 ret
= consumer_create_socket(&ustconsumer64_data
,
3138 cmd_ctx
->session
->ust_session
->consumer
);
3144 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3145 if (config
.consumerd32_bin_path
.value
&&
3146 ustconsumer32_data
.pid
== 0 &&
3147 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3148 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3149 ret
= start_consumerd(&ustconsumer32_data
);
3151 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3152 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3156 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3157 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3159 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3163 * Setup socket for consumer 32 bit. No need for atomic access
3164 * since it was set above and can ONLY be set in this thread.
3166 ret
= consumer_create_socket(&ustconsumer32_data
,
3167 cmd_ctx
->session
->ust_session
->consumer
);
3179 /* Validate consumer daemon state when start/stop trace command */
3180 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3181 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3182 switch (cmd_ctx
->lsm
->domain
.type
) {
3183 case LTTNG_DOMAIN_NONE
:
3185 case LTTNG_DOMAIN_JUL
:
3186 case LTTNG_DOMAIN_LOG4J
:
3187 case LTTNG_DOMAIN_PYTHON
:
3188 case LTTNG_DOMAIN_UST
:
3189 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3190 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3194 case LTTNG_DOMAIN_KERNEL
:
3195 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3196 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3201 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3207 * Check that the UID or GID match that of the tracing session.
3208 * The root user can interact with all sessions.
3210 if (need_tracing_session
) {
3211 if (!session_access_ok(cmd_ctx
->session
,
3212 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3213 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
3214 cmd_ctx
->session
->destroyed
) {
3215 ret
= LTTNG_ERR_EPERM
;
3221 * Send relayd information to consumer as soon as we have a domain and a
3224 if (cmd_ctx
->session
&& need_domain
) {
3226 * Setup relayd if not done yet. If the relayd information was already
3227 * sent to the consumer, this call will gracefully return.
3229 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3230 if (ret
!= LTTNG_OK
) {
3235 /* Process by command type */
3236 switch (cmd_ctx
->lsm
->cmd_type
) {
3237 case LTTNG_ADD_CONTEXT
:
3240 * An LTTNG_ADD_CONTEXT command might have a supplementary
3241 * payload if the context being added is an application context.
3243 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3244 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3245 char *provider_name
= NULL
, *context_name
= NULL
;
3246 size_t provider_name_len
=
3247 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3248 size_t context_name_len
=
3249 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3251 if (provider_name_len
== 0 || context_name_len
== 0) {
3253 * Application provider and context names MUST
3256 ret
= -LTTNG_ERR_INVALID
;
3260 provider_name
= zmalloc(provider_name_len
+ 1);
3261 if (!provider_name
) {
3262 ret
= -LTTNG_ERR_NOMEM
;
3265 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3268 context_name
= zmalloc(context_name_len
+ 1);
3269 if (!context_name
) {
3270 ret
= -LTTNG_ERR_NOMEM
;
3271 goto error_add_context
;
3273 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3276 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3279 goto error_add_context
;
3282 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3285 goto error_add_context
;
3290 * cmd_add_context assumes ownership of the provider and context
3293 ret
= cmd_add_context(cmd_ctx
->session
,
3294 cmd_ctx
->lsm
->domain
.type
,
3295 cmd_ctx
->lsm
->u
.context
.channel_name
,
3296 &cmd_ctx
->lsm
->u
.context
.ctx
,
3297 kernel_poll_pipe
[1]);
3299 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3300 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3302 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3303 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3309 case LTTNG_DISABLE_CHANNEL
:
3311 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3312 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3315 case LTTNG_DISABLE_EVENT
:
3319 * FIXME: handle filter; for now we just receive the filter's
3320 * bytecode along with the filter expression which are sent by
3321 * liblttng-ctl and discard them.
3323 * This fixes an issue where the client may block while sending
3324 * the filter payload and encounter an error because the session
3325 * daemon closes the socket without ever handling this data.
3327 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3328 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3331 char data
[LTTNG_FILTER_MAX_LEN
];
3333 DBG("Discarding disable event command payload of size %zu", count
);
3335 ret
= lttcomm_recv_unix_sock(sock
, data
,
3336 count
> sizeof(data
) ? sizeof(data
) : count
);
3341 count
-= (size_t) ret
;
3344 /* FIXME: passing packed structure to non-packed pointer */
3345 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3346 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3347 &cmd_ctx
->lsm
->u
.disable
.event
);
3350 case LTTNG_ENABLE_CHANNEL
:
3352 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3353 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3354 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3355 &cmd_ctx
->lsm
->u
.channel
.chan
,
3356 kernel_poll_pipe
[1]);
3359 case LTTNG_TRACK_PID
:
3361 ret
= cmd_track_pid(cmd_ctx
->session
,
3362 cmd_ctx
->lsm
->domain
.type
,
3363 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3366 case LTTNG_UNTRACK_PID
:
3368 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3369 cmd_ctx
->lsm
->domain
.type
,
3370 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3373 case LTTNG_ENABLE_EVENT
:
3375 struct lttng_event
*ev
= NULL
;
3376 struct lttng_event_exclusion
*exclusion
= NULL
;
3377 struct lttng_filter_bytecode
*bytecode
= NULL
;
3378 char *filter_expression
= NULL
;
3380 /* Handle exclusion events and receive it from the client. */
3381 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3382 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3384 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3385 (count
* LTTNG_SYMBOL_NAME_LEN
));
3387 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3391 DBG("Receiving var len exclusion event list from client ...");
3392 exclusion
->count
= count
;
3393 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3394 count
* LTTNG_SYMBOL_NAME_LEN
);
3396 DBG("Nothing recv() from client var len data... continuing");
3399 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3404 /* Get filter expression from client. */
3405 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3406 size_t expression_len
=
3407 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3409 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3410 ret
= LTTNG_ERR_FILTER_INVAL
;
3415 filter_expression
= zmalloc(expression_len
);
3416 if (!filter_expression
) {
3418 ret
= LTTNG_ERR_FILTER_NOMEM
;
3422 /* Receive var. len. data */
3423 DBG("Receiving var len filter's expression from client ...");
3424 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3427 DBG("Nothing recv() from client var len data... continuing");
3429 free(filter_expression
);
3431 ret
= LTTNG_ERR_FILTER_INVAL
;
3436 /* Handle filter and get bytecode from client. */
3437 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3438 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3440 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3441 ret
= LTTNG_ERR_FILTER_INVAL
;
3442 free(filter_expression
);
3447 bytecode
= zmalloc(bytecode_len
);
3449 free(filter_expression
);
3451 ret
= LTTNG_ERR_FILTER_NOMEM
;
3455 /* Receive var. len. data */
3456 DBG("Receiving var len filter's bytecode from client ...");
3457 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3459 DBG("Nothing recv() from client var len data... continuing");
3461 free(filter_expression
);
3464 ret
= LTTNG_ERR_FILTER_INVAL
;
3468 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3469 free(filter_expression
);
3472 ret
= LTTNG_ERR_FILTER_INVAL
;
3477 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
3479 DBG("Failed to copy event: %s",
3480 cmd_ctx
->lsm
->u
.enable
.event
.name
);
3481 free(filter_expression
);
3484 ret
= LTTNG_ERR_NOMEM
;
3489 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
3490 /* Expect a userspace probe description. */
3491 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
3493 free(filter_expression
);
3496 lttng_event_destroy(ev
);
3501 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3502 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3504 filter_expression
, bytecode
, exclusion
,
3505 kernel_poll_pipe
[1]);
3506 lttng_event_destroy(ev
);
3509 case LTTNG_LIST_TRACEPOINTS
:
3511 struct lttng_event
*events
;
3514 session_lock_list();
3515 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3516 session_unlock_list();
3517 if (nb_events
< 0) {
3518 /* Return value is a negative lttng_error_code. */
3524 * Setup lttng message with payload size set to the event list size in
3525 * bytes and then copy list into the llm payload.
3527 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3528 sizeof(struct lttng_event
) * nb_events
);
3538 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3540 struct lttng_event_field
*fields
;
3543 session_lock_list();
3544 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3546 session_unlock_list();
3547 if (nb_fields
< 0) {
3548 /* Return value is a negative lttng_error_code. */
3554 * Setup lttng message with payload size set to the event list size in
3555 * bytes and then copy list into the llm payload.
3557 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3558 sizeof(struct lttng_event_field
) * nb_fields
);
3568 case LTTNG_LIST_SYSCALLS
:
3570 struct lttng_event
*events
;
3573 nb_events
= cmd_list_syscalls(&events
);
3574 if (nb_events
< 0) {
3575 /* Return value is a negative lttng_error_code. */
3581 * Setup lttng message with payload size set to the event list size in
3582 * bytes and then copy list into the llm payload.
3584 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3585 sizeof(struct lttng_event
) * nb_events
);
3595 case LTTNG_LIST_TRACKER_PIDS
:
3597 int32_t *pids
= NULL
;
3600 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3601 cmd_ctx
->lsm
->domain
.type
, &pids
);
3603 /* Return value is a negative lttng_error_code. */
3609 * Setup lttng message with payload size set to the event list size in
3610 * bytes and then copy list into the llm payload.
3612 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3613 sizeof(int32_t) * nr_pids
);
3623 case LTTNG_SET_CONSUMER_URI
:
3626 struct lttng_uri
*uris
;
3628 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3629 len
= nb_uri
* sizeof(struct lttng_uri
);
3632 ret
= LTTNG_ERR_INVALID
;
3636 uris
= zmalloc(len
);
3638 ret
= LTTNG_ERR_FATAL
;
3642 /* Receive variable len data */
3643 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3644 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3646 DBG("No URIs received from client... continuing");
3648 ret
= LTTNG_ERR_SESSION_FAIL
;
3653 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3655 if (ret
!= LTTNG_OK
) {
3662 case LTTNG_START_TRACE
:
3665 * On the first start, if we have a kernel session and we have
3666 * enabled time or size-based rotations, we have to make sure
3667 * the kernel tracer supports it.
3669 if (!cmd_ctx
->session
->has_been_started
&& \
3670 cmd_ctx
->session
->kernel_session
&& \
3671 (cmd_ctx
->session
->rotate_timer_period
|| \
3672 cmd_ctx
->session
->rotate_size
) && \
3673 !check_rotate_compatible()) {
3674 DBG("Kernel tracer version is not compatible with the rotation feature");
3675 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3678 ret
= cmd_start_trace(cmd_ctx
->session
);
3681 case LTTNG_STOP_TRACE
:
3683 ret
= cmd_stop_trace(cmd_ctx
->session
);
3686 case LTTNG_CREATE_SESSION
:
3689 struct lttng_uri
*uris
= NULL
;
3691 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3692 len
= nb_uri
* sizeof(struct lttng_uri
);
3695 uris
= zmalloc(len
);
3697 ret
= LTTNG_ERR_FATAL
;
3701 /* Receive variable len data */
3702 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3703 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3705 DBG("No URIs received from client... continuing");
3707 ret
= LTTNG_ERR_SESSION_FAIL
;