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 timer_thread
;
200 * UST registration command queue. This queue is tied with a futex and uses a N
201 * wakers / 1 waiter implemented and detailed in futex.c/.h
203 * The thread_registration_apps and thread_dispatch_ust_registration uses this
204 * queue along with the wait/wake scheme. The thread_manage_apps receives down
205 * the line new application socket and monitors it for any I/O error or clean
206 * close that triggers an unregistration of the application.
208 static struct ust_cmd_queue ust_cmd_queue
;
210 static const char *module_proc_lttng
= "/proc/lttng";
213 * Consumer daemon state which is changed when spawning it, killing it or in
214 * case of a fatal error.
216 enum consumerd_state
{
217 CONSUMER_STARTED
= 1,
218 CONSUMER_STOPPED
= 2,
223 * This consumer daemon state is used to validate if a client command will be
224 * able to reach the consumer. If not, the client is informed. For instance,
225 * doing a "lttng start" when the consumer state is set to ERROR will return an
226 * error to the client.
228 * The following example shows a possible race condition of this scheme:
230 * consumer thread error happens
232 * client cmd checks state -> still OK
233 * consumer thread exit, sets error
234 * client cmd try to talk to consumer
237 * However, since the consumer is a different daemon, we have no way of making
238 * sure the command will reach it safely even with this state flag. This is why
239 * we consider that up to the state validation during command processing, the
240 * command is safe. After that, we can not guarantee the correctness of the
241 * client request vis-a-vis the consumer.
243 static enum consumerd_state ust_consumerd_state
;
244 static enum consumerd_state kernel_consumerd_state
;
246 /* Load session thread information to operate. */
247 static struct load_session_thread_data
*load_info
;
250 * Section name to look for in the daemon configuration file.
252 static const char * const config_section_name
= "sessiond";
254 /* Am I root or not. Set to 1 if the daemon is running as root */
258 * Stop all threads by closing the thread quit pipe.
260 static void stop_threads(void)
264 /* Stopping all threads */
265 DBG("Terminating all threads");
266 ret
= sessiond_notify_quit_pipe();
268 ERR("write error on thread quit pipe");
271 /* Dispatch thread */
272 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
273 futex_nto1_wake(&ust_cmd_queue
.futex
);
277 * Close every consumer sockets.
279 static void close_consumer_sockets(void)
283 if (kconsumer_data
.err_sock
>= 0) {
284 ret
= close(kconsumer_data
.err_sock
);
286 PERROR("kernel consumer err_sock close");
289 if (ustconsumer32_data
.err_sock
>= 0) {
290 ret
= close(ustconsumer32_data
.err_sock
);
292 PERROR("UST consumerd32 err_sock close");
295 if (ustconsumer64_data
.err_sock
>= 0) {
296 ret
= close(ustconsumer64_data
.err_sock
);
298 PERROR("UST consumerd64 err_sock close");
301 if (kconsumer_data
.cmd_sock
>= 0) {
302 ret
= close(kconsumer_data
.cmd_sock
);
304 PERROR("kernel consumer cmd_sock close");
307 if (ustconsumer32_data
.cmd_sock
>= 0) {
308 ret
= close(ustconsumer32_data
.cmd_sock
);
310 PERROR("UST consumerd32 cmd_sock close");
313 if (ustconsumer64_data
.cmd_sock
>= 0) {
314 ret
= close(ustconsumer64_data
.cmd_sock
);
316 PERROR("UST consumerd64 cmd_sock close");
319 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
320 ret
= close(kconsumer_data
.channel_monitor_pipe
);
322 PERROR("kernel consumer channel monitor pipe close");
325 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
326 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
328 PERROR("UST consumerd32 channel monitor pipe close");
331 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
332 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
334 PERROR("UST consumerd64 channel monitor pipe close");
340 * Wait on consumer process termination.
342 * Need to be called with the consumer data lock held or from a context
343 * ensuring no concurrent access to data (e.g: cleanup).
345 static void wait_consumer(struct consumer_data
*consumer_data
)
350 if (consumer_data
->pid
<= 0) {
354 DBG("Waiting for complete teardown of consumerd (PID: %d)",
356 ret
= waitpid(consumer_data
->pid
, &status
, 0);
358 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
359 } else if (!WIFEXITED(status
)) {
360 ERR("consumerd termination with error: %d",
363 consumer_data
->pid
= 0;
367 * Cleanup the session daemon's data structures.
369 static void sessiond_cleanup(void)
372 struct ltt_session_list
*session_list
= session_get_list();
374 DBG("Cleanup sessiond");
377 * Close the thread quit pipe. It has already done its job,
378 * since we are now called.
380 sessiond_close_quit_pipe();
382 ret
= remove(config
.pid_file_path
.value
);
384 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
387 DBG("Removing sessiond and consumerd content of directory %s",
388 config
.rundir
.value
);
391 DBG("Removing %s", config
.pid_file_path
.value
);
392 (void) unlink(config
.pid_file_path
.value
);
394 DBG("Removing %s", config
.agent_port_file_path
.value
);
395 (void) unlink(config
.agent_port_file_path
.value
);
398 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
399 (void) unlink(kconsumer_data
.err_unix_sock_path
);
401 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
402 (void) rmdir(config
.kconsumerd_path
.value
);
404 /* ust consumerd 32 */
405 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
406 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
408 DBG("Removing directory %s", config
.consumerd32_path
.value
);
409 (void) rmdir(config
.consumerd32_path
.value
);
411 /* ust consumerd 64 */
412 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
413 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
415 DBG("Removing directory %s", config
.consumerd64_path
.value
);
416 (void) rmdir(config
.consumerd64_path
.value
);
418 pthread_mutex_destroy(&session_list
->lock
);
420 wait_consumer(&kconsumer_data
);
421 wait_consumer(&ustconsumer64_data
);
422 wait_consumer(&ustconsumer32_data
);
424 DBG("Cleaning up all agent apps");
425 agent_app_ht_clean();
427 DBG("Closing all UST sockets");
428 ust_app_clean_list();
429 buffer_reg_destroy_registries();
431 if (is_root
&& !config
.no_kernel
) {
432 DBG2("Closing kernel fd");
433 if (kernel_tracer_fd
>= 0) {
434 ret
= close(kernel_tracer_fd
);
439 DBG("Unloading kernel modules");
440 modprobe_remove_lttng_all();
444 close_consumer_sockets();
447 load_session_destroy_data(load_info
);
452 * We do NOT rmdir rundir because there are other processes
453 * using it, for instance lttng-relayd, which can start in
454 * parallel with this teardown.
459 * Cleanup the daemon's option data structures.
461 static void sessiond_cleanup_options(void)
463 DBG("Cleaning up options");
465 sessiond_config_fini(&config
);
467 run_as_destroy_worker();
471 * Send data on a unix socket using the liblttsessiondcomm API.
473 * Return lttcomm error code.
475 static int send_unix_sock(int sock
, void *buf
, size_t len
)
477 /* Check valid length */
482 return lttcomm_send_unix_sock(sock
, buf
, len
);
486 * Free memory of a command context structure.
488 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
490 DBG("Clean command context structure");
492 if ((*cmd_ctx
)->llm
) {
493 free((*cmd_ctx
)->llm
);
495 if ((*cmd_ctx
)->lsm
) {
496 free((*cmd_ctx
)->lsm
);
504 * Notify UST applications using the shm mmap futex.
506 static int notify_ust_apps(int active
)
510 DBG("Notifying applications of session daemon state: %d", active
);
512 /* See shm.c for this call implying mmap, shm and futex calls */
513 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
514 if (wait_shm_mmap
== NULL
) {
518 /* Wake waiting process */
519 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
521 /* Apps notified successfully */
529 * Setup the outgoing data buffer for the response (llm) by allocating the
530 * right amount of memory and copying the original information from the lsm
533 * Return 0 on success, negative value on error.
535 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
536 const void *payload_buf
, size_t payload_len
,
537 const void *cmd_header_buf
, size_t cmd_header_len
)
540 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
541 const size_t cmd_header_offset
= header_len
;
542 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
543 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
545 cmd_ctx
->llm
= zmalloc(total_msg_size
);
547 if (cmd_ctx
->llm
== NULL
) {
553 /* Copy common data */
554 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
555 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
556 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
557 cmd_ctx
->llm
->data_size
= payload_len
;
558 cmd_ctx
->lttng_msg_size
= total_msg_size
;
560 /* Copy command header */
561 if (cmd_header_len
) {
562 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
568 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
577 * Version of setup_lttng_msg() without command header.
579 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
580 void *payload_buf
, size_t payload_len
)
582 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
585 * Update the kernel poll set of all channel fd available over all tracing
586 * session. Add the wakeup pipe at the end of the set.
588 static int update_kernel_poll(struct lttng_poll_event
*events
)
591 struct ltt_kernel_channel
*channel
;
592 struct ltt_session
*session
;
593 const struct ltt_session_list
*session_list
= session_get_list();
595 DBG("Updating kernel poll set");
598 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
599 if (!session_get(session
)) {
602 session_lock(session
);
603 if (session
->kernel_session
== NULL
) {
604 session_unlock(session
);
605 session_put(session
);
609 cds_list_for_each_entry(channel
,
610 &session
->kernel_session
->channel_list
.head
, list
) {
611 /* Add channel fd to the kernel poll set */
612 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
614 session_unlock(session
);
615 session_put(session
);
618 DBG("Channel fd %d added to kernel set", channel
->fd
);
620 session_unlock(session
);
622 session_unlock_list();
627 session_unlock_list();
632 * Find the channel fd from 'fd' over all tracing session. When found, check
633 * for new channel stream and send those stream fds to the kernel consumer.
635 * Useful for CPU hotplug feature.
637 static int update_kernel_stream(int fd
)
640 struct ltt_session
*session
;
641 struct ltt_kernel_session
*ksess
;
642 struct ltt_kernel_channel
*channel
;
643 const struct ltt_session_list
*session_list
= session_get_list();
645 DBG("Updating kernel streams for channel fd %d", fd
);
648 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
649 if (!session_get(session
)) {
652 session_lock(session
);
653 if (session
->kernel_session
== NULL
) {
654 session_unlock(session
);
655 session_put(session
);
658 ksess
= session
->kernel_session
;
660 cds_list_for_each_entry(channel
,
661 &ksess
->channel_list
.head
, list
) {
662 struct lttng_ht_iter iter
;
663 struct consumer_socket
*socket
;
665 if (channel
->fd
!= fd
) {
668 DBG("Channel found, updating kernel streams");
669 ret
= kernel_open_channel_stream(channel
);
673 /* Update the stream global counter */
674 ksess
->stream_count_global
+= ret
;
677 * Have we already sent fds to the consumer? If yes, it
678 * means that tracing is started so it is safe to send
679 * our updated stream fds.
681 if (ksess
->consumer_fds_sent
!= 1
682 || ksess
->consumer
== NULL
) {
688 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
689 &iter
.iter
, socket
, node
.node
) {
690 pthread_mutex_lock(socket
->lock
);
691 ret
= kernel_consumer_send_channel_streams(socket
,
693 session
->output_traces
? 1 : 0);
694 pthread_mutex_unlock(socket
->lock
);
702 session_unlock(session
);
703 session_put(session
);
705 session_unlock_list();
709 session_unlock(session
);
710 session_put(session
);
711 session_unlock_list();
716 * For each tracing session, update newly registered apps. The session list
717 * lock MUST be acquired before calling this.
719 static void update_ust_app(int app_sock
)
721 struct ltt_session
*sess
, *stmp
;
722 const struct ltt_session_list
*session_list
= session_get_list();
724 /* Consumer is in an ERROR state. Stop any application update. */
725 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
726 /* Stop the update process since the consumer is dead. */
730 /* For all tracing session(s) */
731 cds_list_for_each_entry_safe(sess
, stmp
, &session_list
->head
, list
) {
734 if (!session_get(sess
)) {
738 if (!sess
->ust_session
) {
743 assert(app_sock
>= 0);
744 app
= ust_app_find_by_sock(app_sock
);
747 * Application can be unregistered before so
748 * this is possible hence simply stopping the
751 DBG3("UST app update failed to find app sock %d",
755 ust_app_global_update(sess
->ust_session
, app
);
759 session_unlock(sess
);
765 * This thread manage event coming from the kernel.
767 * Features supported in this thread:
770 static void *thread_manage_kernel(void *data
)
772 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
773 uint32_t revents
, nb_fd
;
775 struct lttng_poll_event events
;
777 DBG("[thread] Thread manage kernel started");
779 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
782 * This first step of the while is to clean this structure which could free
783 * non NULL pointers so initialize it before the loop.
785 lttng_poll_init(&events
);
787 if (testpoint(sessiond_thread_manage_kernel
)) {
788 goto error_testpoint
;
791 health_code_update();
793 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
794 goto error_testpoint
;
798 health_code_update();
800 if (update_poll_flag
== 1) {
801 /* Clean events object. We are about to populate it again. */
802 lttng_poll_clean(&events
);
804 ret
= sessiond_set_thread_pollset(&events
, 2);
806 goto error_poll_create
;
809 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
814 /* This will add the available kernel channel if any. */
815 ret
= update_kernel_poll(&events
);
819 update_poll_flag
= 0;
822 DBG("Thread kernel polling");
824 /* Poll infinite value of time */
827 ret
= lttng_poll_wait(&events
, -1);
828 DBG("Thread kernel return from poll on %d fds",
829 LTTNG_POLL_GETNB(&events
));
833 * Restart interrupted system call.
835 if (errno
== EINTR
) {
839 } else if (ret
== 0) {
840 /* Should not happen since timeout is infinite */
841 ERR("Return value of poll is 0 with an infinite timeout.\n"
842 "This should not have happened! Continuing...");
848 for (i
= 0; i
< nb_fd
; i
++) {
849 /* Fetch once the poll data */
850 revents
= LTTNG_POLL_GETEV(&events
, i
);
851 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
853 health_code_update();
856 /* No activity for this FD (poll implementation). */
860 /* Thread quit pipe has been closed. Killing thread. */
861 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
867 /* Check for data on kernel pipe */
868 if (revents
& LPOLLIN
) {
869 if (pollfd
== kernel_poll_pipe
[0]) {
870 (void) lttng_read(kernel_poll_pipe
[0],
873 * Ret value is useless here, if this pipe gets any actions an
874 * update is required anyway.
876 update_poll_flag
= 1;
880 * New CPU detected by the kernel. Adding kernel stream to
881 * kernel session and updating the kernel consumer
883 ret
= update_kernel_stream(pollfd
);
889 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
890 update_poll_flag
= 1;
893 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
901 lttng_poll_clean(&events
);
904 utils_close_pipe(kernel_poll_pipe
);
905 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
908 ERR("Health error occurred in %s", __func__
);
909 WARN("Kernel thread died unexpectedly. "
910 "Kernel tracing can continue but CPU hotplug is disabled.");
912 health_unregister(health_sessiond
);
913 DBG("Kernel thread dying");
918 * Signal pthread condition of the consumer data that the thread.
920 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
922 pthread_mutex_lock(&data
->cond_mutex
);
925 * The state is set before signaling. It can be any value, it's the waiter
926 * job to correctly interpret this condition variable associated to the
927 * consumer pthread_cond.
929 * A value of 0 means that the corresponding thread of the consumer data
930 * was not started. 1 indicates that the thread has started and is ready
931 * for action. A negative value means that there was an error during the
934 data
->consumer_thread_is_ready
= state
;
935 (void) pthread_cond_signal(&data
->cond
);
937 pthread_mutex_unlock(&data
->cond_mutex
);
941 * This thread manage the consumer error sent back to the session daemon.
943 static void *thread_manage_consumer(void *data
)
945 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
946 uint32_t revents
, nb_fd
;
947 enum lttcomm_return_code code
;
948 struct lttng_poll_event events
;
949 struct consumer_data
*consumer_data
= data
;
950 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
952 DBG("[thread] Manage consumer started");
954 rcu_register_thread();
957 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
959 health_code_update();
962 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
963 * metadata_sock. Nothing more will be added to this poll set.
965 ret
= sessiond_set_thread_pollset(&events
, 3);
971 * The error socket here is already in a listening state which was done
972 * just before spawning this thread to avoid a race between the consumer
973 * daemon exec trying to connect and the listen() call.
975 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
980 health_code_update();
982 /* Infinite blocking call, waiting for transmission */
986 if (testpoint(sessiond_thread_manage_consumer
)) {
990 ret
= lttng_poll_wait(&events
, -1);
994 * Restart interrupted system call.
996 if (errno
== EINTR
) {
1004 for (i
= 0; i
< nb_fd
; i
++) {
1005 /* Fetch once the poll data */
1006 revents
= LTTNG_POLL_GETEV(&events
, i
);
1007 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1009 health_code_update();
1012 /* No activity for this FD (poll implementation). */
1016 /* Thread quit pipe has been closed. Killing thread. */
1017 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1023 /* Event on the registration socket */
1024 if (pollfd
== consumer_data
->err_sock
) {
1025 if (revents
& LPOLLIN
) {
1027 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1028 ERR("consumer err socket poll error");
1031 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1037 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1043 * Set the CLOEXEC flag. Return code is useless because either way, the
1046 (void) utils_set_fd_cloexec(sock
);
1048 health_code_update();
1050 DBG2("Receiving code from consumer err_sock");
1052 /* Getting status code from kconsumerd */
1053 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1054 sizeof(enum lttcomm_return_code
));
1059 health_code_update();
1060 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1061 ERR("consumer error when waiting for SOCK_READY : %s",
1062 lttcomm_get_readable_code(-code
));
1066 /* Connect both command and metadata sockets. */
1067 consumer_data
->cmd_sock
=
1068 lttcomm_connect_unix_sock(
1069 consumer_data
->cmd_unix_sock_path
);
1070 consumer_data
->metadata_fd
=
1071 lttcomm_connect_unix_sock(
1072 consumer_data
->cmd_unix_sock_path
);
1073 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1074 PERROR("consumer connect cmd socket");
1075 /* On error, signal condition and quit. */
1076 signal_consumer_condition(consumer_data
, -1);
1080 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1082 /* Create metadata socket lock. */
1083 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1084 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1085 PERROR("zmalloc pthread mutex");
1088 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1090 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1091 DBG("Consumer metadata socket ready (fd: %d)",
1092 consumer_data
->metadata_fd
);
1095 * Remove the consumerd error sock since we've established a connection.
1097 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1102 /* Add new accepted error socket. */
1103 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1108 /* Add metadata socket that is successfully connected. */
1109 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1110 LPOLLIN
| LPOLLRDHUP
);
1115 health_code_update();
1118 * Transfer the write-end of the channel monitoring and rotate pipe
1119 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1121 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1122 if (!cmd_socket_wrapper
) {
1125 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1127 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1128 consumer_data
->channel_monitor_pipe
);
1133 /* Discard the socket wrapper as it is no longer needed. */
1134 consumer_destroy_socket(cmd_socket_wrapper
);
1135 cmd_socket_wrapper
= NULL
;
1137 /* The thread is completely initialized, signal that it is ready. */
1138 signal_consumer_condition(consumer_data
, 1);
1140 /* Infinite blocking call, waiting for transmission */
1143 health_code_update();
1145 /* Exit the thread because the thread quit pipe has been triggered. */
1147 /* Not a health error. */
1152 health_poll_entry();
1153 ret
= lttng_poll_wait(&events
, -1);
1157 * Restart interrupted system call.
1159 if (errno
== EINTR
) {
1167 for (i
= 0; i
< nb_fd
; i
++) {
1168 /* Fetch once the poll data */
1169 revents
= LTTNG_POLL_GETEV(&events
, i
);
1170 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1172 health_code_update();
1175 /* No activity for this FD (poll implementation). */
1180 * Thread quit pipe has been triggered, flag that we should stop
1181 * but continue the current loop to handle potential data from
1184 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1186 if (pollfd
== sock
) {
1187 /* Event on the consumerd socket */
1188 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1189 && !(revents
& LPOLLIN
)) {
1190 ERR("consumer err socket second poll error");
1193 health_code_update();
1194 /* Wait for any kconsumerd error */
1195 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1196 sizeof(enum lttcomm_return_code
));
1198 ERR("consumer closed the command socket");
1202 ERR("consumer return code : %s",
1203 lttcomm_get_readable_code(-code
));
1206 } else if (pollfd
== consumer_data
->metadata_fd
) {
1207 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1208 && !(revents
& LPOLLIN
)) {
1209 ERR("consumer err metadata socket second poll error");
1212 /* UST metadata requests */
1213 ret
= ust_consumer_metadata_request(
1214 &consumer_data
->metadata_sock
);
1216 ERR("Handling metadata request");
1220 /* No need for an else branch all FDs are tested prior. */
1222 health_code_update();
1228 * We lock here because we are about to close the sockets and some other
1229 * thread might be using them so get exclusive access which will abort all
1230 * other consumer command by other threads.
1232 pthread_mutex_lock(&consumer_data
->lock
);
1234 /* Immediately set the consumerd state to stopped */
1235 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1236 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1237 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1238 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1239 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1241 /* Code flow error... */
1245 if (consumer_data
->err_sock
>= 0) {
1246 ret
= close(consumer_data
->err_sock
);
1250 consumer_data
->err_sock
= -1;
1252 if (consumer_data
->cmd_sock
>= 0) {
1253 ret
= close(consumer_data
->cmd_sock
);
1257 consumer_data
->cmd_sock
= -1;
1259 if (consumer_data
->metadata_sock
.fd_ptr
&&
1260 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1261 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1273 unlink(consumer_data
->err_unix_sock_path
);
1274 unlink(consumer_data
->cmd_unix_sock_path
);
1275 pthread_mutex_unlock(&consumer_data
->lock
);
1277 /* Cleanup metadata socket mutex. */
1278 if (consumer_data
->metadata_sock
.lock
) {
1279 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1280 free(consumer_data
->metadata_sock
.lock
);
1282 lttng_poll_clean(&events
);
1284 if (cmd_socket_wrapper
) {
1285 consumer_destroy_socket(cmd_socket_wrapper
);
1290 ERR("Health error occurred in %s", __func__
);
1292 health_unregister(health_sessiond
);
1293 DBG("consumer thread cleanup completed");
1295 rcu_thread_offline();
1296 rcu_unregister_thread();
1302 * This thread receives application command sockets (FDs) on the
1303 * apps_cmd_pipe and waits (polls) on them until they are closed
1304 * or an error occurs.
1306 * At that point, it flushes the data (tracing and metadata) associated
1307 * with this application and tears down ust app sessions and other
1308 * associated data structures through ust_app_unregister().
1310 * Note that this thread never sends commands to the applications
1311 * through the command sockets; it merely listens for hang-ups
1312 * and errors on those sockets and cleans-up as they occur.
1314 static void *thread_manage_apps(void *data
)
1316 int i
, ret
, pollfd
, err
= -1;
1318 uint32_t revents
, nb_fd
;
1319 struct lttng_poll_event events
;
1321 DBG("[thread] Manage application started");
1323 rcu_register_thread();
1324 rcu_thread_online();
1326 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1328 if (testpoint(sessiond_thread_manage_apps
)) {
1329 goto error_testpoint
;
1332 health_code_update();
1334 ret
= sessiond_set_thread_pollset(&events
, 2);
1336 goto error_poll_create
;
1339 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1344 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1348 health_code_update();
1351 DBG("Apps thread polling");
1353 /* Inifinite blocking call, waiting for transmission */
1355 health_poll_entry();
1356 ret
= lttng_poll_wait(&events
, -1);
1357 DBG("Apps thread return from poll on %d fds",
1358 LTTNG_POLL_GETNB(&events
));
1362 * Restart interrupted system call.
1364 if (errno
== EINTR
) {
1372 for (i
= 0; i
< nb_fd
; i
++) {
1373 /* Fetch once the poll data */
1374 revents
= LTTNG_POLL_GETEV(&events
, i
);
1375 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1377 health_code_update();
1380 /* No activity for this FD (poll implementation). */
1384 /* Thread quit pipe has been closed. Killing thread. */
1385 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1391 /* Inspect the apps cmd pipe */
1392 if (pollfd
== apps_cmd_pipe
[0]) {
1393 if (revents
& LPOLLIN
) {
1397 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1398 if (size_ret
< sizeof(sock
)) {
1399 PERROR("read apps cmd pipe");
1403 health_code_update();
1406 * Since this is a command socket (write then read),
1407 * we only monitor the error events of the socket.
1409 ret
= lttng_poll_add(&events
, sock
,
1410 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1415 DBG("Apps with sock %d added to poll set", sock
);
1416 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1417 ERR("Apps command pipe error");
1420 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1425 * At this point, we know that a registered application made
1426 * the event at poll_wait.
1428 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1429 /* Removing from the poll set */
1430 ret
= lttng_poll_del(&events
, pollfd
);
1435 /* Socket closed on remote end. */
1436 ust_app_unregister(pollfd
);
1438 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1443 health_code_update();
1449 lttng_poll_clean(&events
);
1452 utils_close_pipe(apps_cmd_pipe
);
1453 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1456 * We don't clean the UST app hash table here since already registered
1457 * applications can still be controlled so let them be until the session
1458 * daemon dies or the applications stop.
1463 ERR("Health error occurred in %s", __func__
);
1465 health_unregister(health_sessiond
);
1466 DBG("Application communication apps thread cleanup complete");
1467 rcu_thread_offline();
1468 rcu_unregister_thread();
1473 * Send a socket to a thread This is called from the dispatch UST registration
1474 * thread once all sockets are set for the application.
1476 * The sock value can be invalid, we don't really care, the thread will handle
1477 * it and make the necessary cleanup if so.
1479 * On success, return 0 else a negative value being the errno message of the
1482 static int send_socket_to_thread(int fd
, int sock
)
1487 * It's possible that the FD is set as invalid with -1 concurrently just
1488 * before calling this function being a shutdown state of the thread.
1495 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1496 if (ret
< sizeof(sock
)) {
1497 PERROR("write apps pipe %d", fd
);
1504 /* All good. Don't send back the write positive ret value. */
1511 * Sanitize the wait queue of the dispatch registration thread meaning removing
1512 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1513 * notify socket is never received.
1515 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1517 int ret
, nb_fd
= 0, i
;
1518 unsigned int fd_added
= 0;
1519 struct lttng_poll_event events
;
1520 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1524 lttng_poll_init(&events
);
1526 /* Just skip everything for an empty queue. */
1527 if (!wait_queue
->count
) {
1531 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1536 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1537 &wait_queue
->head
, head
) {
1538 assert(wait_node
->app
);
1539 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1540 LPOLLHUP
| LPOLLERR
);
1553 * Poll but don't block so we can quickly identify the faulty events and
1554 * clean them afterwards from the wait queue.
1556 ret
= lttng_poll_wait(&events
, 0);
1562 for (i
= 0; i
< nb_fd
; i
++) {
1563 /* Get faulty FD. */
1564 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1565 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1568 /* No activity for this FD (poll implementation). */
1572 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1573 &wait_queue
->head
, head
) {
1574 if (pollfd
== wait_node
->app
->sock
&&
1575 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1576 cds_list_del(&wait_node
->head
);
1577 wait_queue
->count
--;
1578 ust_app_destroy(wait_node
->app
);
1581 * Silence warning of use-after-free in
1582 * cds_list_for_each_entry_safe which uses
1583 * __typeof__(*wait_node).
1588 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1595 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1599 lttng_poll_clean(&events
);
1603 lttng_poll_clean(&events
);
1605 ERR("Unable to sanitize wait queue");
1610 * Dispatch request from the registration threads to the application
1611 * communication thread.
1613 static void *thread_dispatch_ust_registration(void *data
)
1616 struct cds_wfcq_node
*node
;
1617 struct ust_command
*ust_cmd
= NULL
;
1618 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1619 struct ust_reg_wait_queue wait_queue
= {
1623 rcu_register_thread();
1625 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1627 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1628 goto error_testpoint
;
1631 health_code_update();
1633 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1635 DBG("[thread] Dispatch UST command started");
1638 health_code_update();
1640 /* Atomically prepare the queue futex */
1641 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1643 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1648 struct ust_app
*app
= NULL
;
1652 * Make sure we don't have node(s) that have hung up before receiving
1653 * the notify socket. This is to clean the list in order to avoid
1654 * memory leaks from notify socket that are never seen.
1656 sanitize_wait_queue(&wait_queue
);
1658 health_code_update();
1659 /* Dequeue command for registration */
1660 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1662 DBG("Woken up but nothing in the UST command queue");
1663 /* Continue thread execution */
1667 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1669 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1670 " gid:%d sock:%d name:%s (version %d.%d)",
1671 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1672 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1673 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1674 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1676 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1677 wait_node
= zmalloc(sizeof(*wait_node
));
1679 PERROR("zmalloc wait_node dispatch");
1680 ret
= close(ust_cmd
->sock
);
1682 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1684 lttng_fd_put(LTTNG_FD_APPS
, 1);
1688 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1690 /* Create application object if socket is CMD. */
1691 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1693 if (!wait_node
->app
) {
1694 ret
= close(ust_cmd
->sock
);
1696 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1698 lttng_fd_put(LTTNG_FD_APPS
, 1);
1704 * Add application to the wait queue so we can set the notify
1705 * socket before putting this object in the global ht.
1707 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1712 * We have to continue here since we don't have the notify
1713 * socket and the application MUST be added to the hash table
1714 * only at that moment.
1719 * Look for the application in the local wait queue and set the
1720 * notify socket if found.
1722 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1723 &wait_queue
.head
, head
) {
1724 health_code_update();
1725 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1726 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1727 cds_list_del(&wait_node
->head
);
1729 app
= wait_node
->app
;
1731 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1737 * With no application at this stage the received socket is
1738 * basically useless so close it before we free the cmd data
1739 * structure for good.
1742 ret
= close(ust_cmd
->sock
);
1744 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1746 lttng_fd_put(LTTNG_FD_APPS
, 1);
1753 * @session_lock_list
1755 * Lock the global session list so from the register up to the
1756 * registration done message, no thread can see the application
1757 * and change its state.
1759 session_lock_list();
1763 * Add application to the global hash table. This needs to be
1764 * done before the update to the UST registry can locate the
1769 /* Set app version. This call will print an error if needed. */
1770 (void) ust_app_version(app
);
1772 /* Send notify socket through the notify pipe. */
1773 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1777 session_unlock_list();
1779 * No notify thread, stop the UST tracing. However, this is
1780 * not an internal error of the this thread thus setting
1781 * the health error code to a normal exit.
1788 * Update newly registered application with the tracing
1789 * registry info already enabled information.
1791 update_ust_app(app
->sock
);
1794 * Don't care about return value. Let the manage apps threads
1795 * handle app unregistration upon socket close.
1797 (void) ust_app_register_done(app
);
1800 * Even if the application socket has been closed, send the app
1801 * to the thread and unregistration will take place at that
1804 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1807 session_unlock_list();
1809 * No apps. thread, stop the UST tracing. However, this is
1810 * not an internal error of the this thread thus setting
1811 * the health error code to a normal exit.
1818 session_unlock_list();
1820 } while (node
!= NULL
);
1822 health_poll_entry();
1823 /* Futex wait on queue. Blocking call on futex() */
1824 futex_nto1_wait(&ust_cmd_queue
.futex
);
1827 /* Normal exit, no error */
1831 /* Clean up wait queue. */
1832 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1833 &wait_queue
.head
, head
) {
1834 cds_list_del(&wait_node
->head
);
1839 /* Empty command queue. */
1841 /* Dequeue command for registration */
1842 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1846 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1847 ret
= close(ust_cmd
->sock
);
1849 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
1851 lttng_fd_put(LTTNG_FD_APPS
, 1);
1856 DBG("Dispatch thread dying");
1859 ERR("Health error occurred in %s", __func__
);
1861 health_unregister(health_sessiond
);
1862 rcu_unregister_thread();
1867 * This thread manage application registration.
1869 static void *thread_registration_apps(void *data
)
1871 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1872 uint32_t revents
, nb_fd
;
1873 struct lttng_poll_event events
;
1875 * Get allocated in this thread, enqueued to a global queue, dequeued and
1876 * freed in the manage apps thread.
1878 struct ust_command
*ust_cmd
= NULL
;
1880 DBG("[thread] Manage application registration started");
1882 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1884 if (testpoint(sessiond_thread_registration_apps
)) {
1885 goto error_testpoint
;
1888 ret
= lttcomm_listen_unix_sock(apps_sock
);
1894 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1895 * more will be added to this poll set.
1897 ret
= sessiond_set_thread_pollset(&events
, 2);
1899 goto error_create_poll
;
1902 /* Add the application registration socket */
1903 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1905 goto error_poll_add
;
1908 /* Notify all applications to register */
1909 ret
= notify_ust_apps(1);
1911 ERR("Failed to notify applications or create the wait shared memory.\n"
1912 "Execution continues but there might be problem for already\n"
1913 "running applications that wishes to register.");
1917 DBG("Accepting application registration");
1919 /* Inifinite blocking call, waiting for transmission */
1921 health_poll_entry();
1922 ret
= lttng_poll_wait(&events
, -1);
1926 * Restart interrupted system call.
1928 if (errno
== EINTR
) {
1936 for (i
= 0; i
< nb_fd
; i
++) {
1937 health_code_update();
1939 /* Fetch once the poll data */
1940 revents
= LTTNG_POLL_GETEV(&events
, i
);
1941 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1944 /* No activity for this FD (poll implementation). */
1948 /* Thread quit pipe has been closed. Killing thread. */
1949 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1955 /* Event on the registration socket */
1956 if (pollfd
== apps_sock
) {
1957 if (revents
& LPOLLIN
) {
1958 sock
= lttcomm_accept_unix_sock(apps_sock
);
1964 * Set socket timeout for both receiving and ending.
1965 * app_socket_timeout is in seconds, whereas
1966 * lttcomm_setsockopt_rcv_timeout and
1967 * lttcomm_setsockopt_snd_timeout expect msec as
1970 if (config
.app_socket_timeout
>= 0) {
1971 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1972 config
.app_socket_timeout
* 1000);
1973 (void) lttcomm_setsockopt_snd_timeout(sock
,
1974 config
.app_socket_timeout
* 1000);
1978 * Set the CLOEXEC flag. Return code is useless because
1979 * either way, the show must go on.
1981 (void) utils_set_fd_cloexec(sock
);
1983 /* Create UST registration command for enqueuing */
1984 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1985 if (ust_cmd
== NULL
) {
1986 PERROR("ust command zmalloc");
1995 * Using message-based transmissions to ensure we don't
1996 * have to deal with partially received messages.
1998 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2000 ERR("Exhausted file descriptors allowed for applications.");
2010 health_code_update();
2011 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2014 /* Close socket of the application. */
2019 lttng_fd_put(LTTNG_FD_APPS
, 1);
2023 health_code_update();
2025 ust_cmd
->sock
= sock
;
2028 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2029 " gid:%d sock:%d name:%s (version %d.%d)",
2030 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2031 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2032 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2033 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2036 * Lock free enqueue the registration request. The red pill
2037 * has been taken! This apps will be part of the *system*.
2039 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2042 * Wake the registration queue futex. Implicit memory
2043 * barrier with the exchange in cds_wfcq_enqueue.
2045 futex_nto1_wake(&ust_cmd_queue
.futex
);
2046 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2047 ERR("Register apps socket poll error");
2050 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2059 /* Notify that the registration thread is gone */
2062 if (apps_sock
>= 0) {
2063 ret
= close(apps_sock
);
2073 lttng_fd_put(LTTNG_FD_APPS
, 1);
2075 unlink(config
.apps_unix_sock_path
.value
);
2078 lttng_poll_clean(&events
);
2082 DBG("UST Registration thread cleanup complete");
2085 ERR("Health error occurred in %s", __func__
);
2087 health_unregister(health_sessiond
);
2093 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2094 * exec or it will fails.
2096 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2099 struct timespec timeout
;
2102 * Make sure we set the readiness flag to 0 because we are NOT ready.
2103 * This access to consumer_thread_is_ready does not need to be
2104 * protected by consumer_data.cond_mutex (yet) since the consumer
2105 * management thread has not been started at this point.
2107 consumer_data
->consumer_thread_is_ready
= 0;
2109 /* Setup pthread condition */
2110 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2113 PERROR("pthread_condattr_init consumer data");
2118 * Set the monotonic clock in order to make sure we DO NOT jump in time
2119 * between the clock_gettime() call and the timedwait call. See bug #324
2120 * for a more details and how we noticed it.
2122 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2125 PERROR("pthread_condattr_setclock consumer data");
2129 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2132 PERROR("pthread_cond_init consumer data");
2136 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2137 thread_manage_consumer
, consumer_data
);
2140 PERROR("pthread_create consumer");
2145 /* We are about to wait on a pthread condition */
2146 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2148 /* Get time for sem_timedwait absolute timeout */
2149 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2151 * Set the timeout for the condition timed wait even if the clock gettime
2152 * call fails since we might loop on that call and we want to avoid to
2153 * increment the timeout too many times.
2155 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2158 * The following loop COULD be skipped in some conditions so this is why we
2159 * set ret to 0 in order to make sure at least one round of the loop is
2165 * Loop until the condition is reached or when a timeout is reached. Note
2166 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2167 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2168 * possible. This loop does not take any chances and works with both of
2171 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2172 if (clock_ret
< 0) {
2173 PERROR("clock_gettime spawn consumer");
2174 /* Infinite wait for the consumerd thread to be ready */
2175 ret
= pthread_cond_wait(&consumer_data
->cond
,
2176 &consumer_data
->cond_mutex
);
2178 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2179 &consumer_data
->cond_mutex
, &timeout
);
2183 /* Release the pthread condition */
2184 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2188 if (ret
== ETIMEDOUT
) {
2192 * Call has timed out so we kill the kconsumerd_thread and return
2195 ERR("Condition timed out. The consumer thread was never ready."
2197 pth_ret
= pthread_cancel(consumer_data
->thread
);
2199 PERROR("pthread_cancel consumer thread");
2202 PERROR("pthread_cond_wait failed consumer thread");
2204 /* Caller is expecting a negative value on failure. */
2209 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2210 if (consumer_data
->pid
== 0) {
2211 ERR("Consumerd did not start");
2212 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2215 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2224 * Join consumer thread
2226 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2230 /* Consumer pid must be a real one. */
2231 if (consumer_data
->pid
> 0) {
2233 ret
= kill(consumer_data
->pid
, SIGTERM
);
2235 PERROR("Error killing consumer daemon");
2238 return pthread_join(consumer_data
->thread
, &status
);
2245 * Fork and exec a consumer daemon (consumerd).
2247 * Return pid if successful else -1.
2249 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2253 const char *consumer_to_use
;
2254 const char *verbosity
;
2257 DBG("Spawning consumerd");
2264 if (config
.verbose_consumer
) {
2265 verbosity
= "--verbose";
2266 } else if (lttng_opt_quiet
) {
2267 verbosity
= "--quiet";
2272 switch (consumer_data
->type
) {
2273 case LTTNG_CONSUMER_KERNEL
:
2275 * Find out which consumerd to execute. We will first try the
2276 * 64-bit path, then the sessiond's installation directory, and
2277 * fallback on the 32-bit one,
2279 DBG3("Looking for a kernel consumer at these locations:");
2280 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2281 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2282 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2283 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2284 DBG3("Found location #1");
2285 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2286 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2287 DBG3("Found location #2");
2288 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2289 } else if (config
.consumerd32_bin_path
.value
&&
2290 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2291 DBG3("Found location #3");
2292 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2294 DBG("Could not find any valid consumerd executable");
2298 DBG("Using kernel consumer at: %s", consumer_to_use
);
2299 (void) execl(consumer_to_use
,
2300 "lttng-consumerd", verbosity
, "-k",
2301 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2302 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2303 "--group", config
.tracing_group_name
.value
,
2306 case LTTNG_CONSUMER64_UST
:
2308 if (config
.consumerd64_lib_dir
.value
) {
2313 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2317 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2318 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2323 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2324 if (tmp
[0] != '\0') {
2325 strcat(tmpnew
, ":");
2326 strcat(tmpnew
, tmp
);
2328 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2335 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2336 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2337 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2338 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2339 "--group", config
.tracing_group_name
.value
,
2343 case LTTNG_CONSUMER32_UST
:
2345 if (config
.consumerd32_lib_dir
.value
) {
2350 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2354 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2355 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2360 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2361 if (tmp
[0] != '\0') {
2362 strcat(tmpnew
, ":");
2363 strcat(tmpnew
, tmp
);
2365 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2372 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2373 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2374 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2375 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2376 "--group", config
.tracing_group_name
.value
,
2381 ERR("unknown consumer type");
2385 PERROR("Consumer execl()");
2387 /* Reaching this point, we got a failure on our execl(). */
2389 } else if (pid
> 0) {
2392 PERROR("start consumer fork");
2400 * Spawn the consumerd daemon and session daemon thread.
2402 static int start_consumerd(struct consumer_data
*consumer_data
)
2407 * Set the listen() state on the socket since there is a possible race
2408 * between the exec() of the consumer daemon and this call if place in the
2409 * consumer thread. See bug #366 for more details.
2411 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2416 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2417 if (consumer_data
->pid
!= 0) {
2418 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2422 ret
= spawn_consumerd(consumer_data
);
2424 ERR("Spawning consumerd failed");
2425 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2429 /* Setting up the consumer_data pid */
2430 consumer_data
->pid
= ret
;
2431 DBG2("Consumer pid %d", consumer_data
->pid
);
2432 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2434 DBG2("Spawning consumer control thread");
2435 ret
= spawn_consumer_thread(consumer_data
);
2437 ERR("Fatal error spawning consumer control thread");
2445 /* Cleanup already created sockets on error. */
2446 if (consumer_data
->err_sock
>= 0) {
2449 err
= close(consumer_data
->err_sock
);
2451 PERROR("close consumer data error socket");
2458 * Setup necessary data for kernel tracer action.
2460 static int init_kernel_tracer(void)
2464 /* Modprobe lttng kernel modules */
2465 ret
= modprobe_lttng_control();
2470 /* Open debugfs lttng */
2471 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2472 if (kernel_tracer_fd
< 0) {
2473 DBG("Failed to open %s", module_proc_lttng
);
2477 /* Validate kernel version */
2478 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2479 &kernel_tracer_abi_version
);
2484 ret
= modprobe_lttng_data();
2489 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2496 WARN("Kernel tracer does not support buffer monitoring. "
2497 "The monitoring timer of channels in the kernel domain "
2498 "will be set to 0 (disabled).");
2501 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2505 modprobe_remove_lttng_control();
2506 ret
= close(kernel_tracer_fd
);
2510 kernel_tracer_fd
= -1;
2511 return LTTNG_ERR_KERN_VERSION
;
2514 ret
= close(kernel_tracer_fd
);
2520 modprobe_remove_lttng_control();
2523 WARN("No kernel tracer available");
2524 kernel_tracer_fd
= -1;
2526 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2528 return LTTNG_ERR_KERN_NA
;
2534 * Copy consumer output from the tracing session to the domain session. The
2535 * function also applies the right modification on a per domain basis for the
2536 * trace files destination directory.
2538 * Should *NOT* be called with RCU read-side lock held.
2540 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2543 const char *dir_name
;
2544 struct consumer_output
*consumer
;
2547 assert(session
->consumer
);
2550 case LTTNG_DOMAIN_KERNEL
:
2551 DBG3("Copying tracing session consumer output in kernel session");
2553 * XXX: We should audit the session creation and what this function
2554 * does "extra" in order to avoid a destroy since this function is used
2555 * in the domain session creation (kernel and ust) only. Same for UST
2558 if (session
->kernel_session
->consumer
) {
2559 consumer_output_put(session
->kernel_session
->consumer
);
2561 session
->kernel_session
->consumer
=
2562 consumer_copy_output(session
->consumer
);
2563 /* Ease our life a bit for the next part */
2564 consumer
= session
->kernel_session
->consumer
;
2565 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2567 case LTTNG_DOMAIN_JUL
:
2568 case LTTNG_DOMAIN_LOG4J
:
2569 case LTTNG_DOMAIN_PYTHON
:
2570 case LTTNG_DOMAIN_UST
:
2571 DBG3("Copying tracing session consumer output in UST session");
2572 if (session
->ust_session
->consumer
) {
2573 consumer_output_put(session
->ust_session
->consumer
);
2575 session
->ust_session
->consumer
=
2576 consumer_copy_output(session
->consumer
);
2577 /* Ease our life a bit for the next part */
2578 consumer
= session
->ust_session
->consumer
;
2579 dir_name
= DEFAULT_UST_TRACE_DIR
;
2582 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2586 /* Append correct directory to subdir */
2587 strncat(consumer
->subdir
, dir_name
,
2588 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2589 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2598 * Create an UST session and add it to the session ust list.
2600 * Should *NOT* be called with RCU read-side lock held.
2602 static int create_ust_session(struct ltt_session
*session
,
2603 struct lttng_domain
*domain
)
2606 struct ltt_ust_session
*lus
= NULL
;
2610 assert(session
->consumer
);
2612 switch (domain
->type
) {
2613 case LTTNG_DOMAIN_JUL
:
2614 case LTTNG_DOMAIN_LOG4J
:
2615 case LTTNG_DOMAIN_PYTHON
:
2616 case LTTNG_DOMAIN_UST
:
2619 ERR("Unknown UST domain on create session %d", domain
->type
);
2620 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2624 DBG("Creating UST session");
2626 lus
= trace_ust_create_session(session
->id
);
2628 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2632 lus
->uid
= session
->uid
;
2633 lus
->gid
= session
->gid
;
2634 lus
->output_traces
= session
->output_traces
;
2635 lus
->snapshot_mode
= session
->snapshot_mode
;
2636 lus
->live_timer_interval
= session
->live_timer
;
2637 session
->ust_session
= lus
;
2638 if (session
->shm_path
[0]) {
2639 strncpy(lus
->root_shm_path
, session
->shm_path
,
2640 sizeof(lus
->root_shm_path
));
2641 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2642 strncpy(lus
->shm_path
, session
->shm_path
,
2643 sizeof(lus
->shm_path
));
2644 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2645 strncat(lus
->shm_path
, "/ust",
2646 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2648 /* Copy session output to the newly created UST session */
2649 ret
= copy_session_consumer(domain
->type
, session
);
2650 if (ret
!= LTTNG_OK
) {
2658 session
->ust_session
= NULL
;
2663 * Create a kernel tracer session then create the default channel.
2665 static int create_kernel_session(struct ltt_session
*session
)
2669 DBG("Creating kernel session");
2671 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2673 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2677 /* Code flow safety */
2678 assert(session
->kernel_session
);
2680 /* Copy session output to the newly created Kernel session */
2681 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2682 if (ret
!= LTTNG_OK
) {
2686 session
->kernel_session
->uid
= session
->uid
;
2687 session
->kernel_session
->gid
= session
->gid
;
2688 session
->kernel_session
->output_traces
= session
->output_traces
;
2689 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2694 trace_kernel_destroy_session(session
->kernel_session
);
2695 session
->kernel_session
= NULL
;
2700 * Count number of session permitted by uid/gid.
2702 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2705 struct ltt_session
*session
;
2706 const struct ltt_session_list
*session_list
= session_get_list();
2708 DBG("Counting number of available session for UID %d GID %d",
2710 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
2711 if (!session_get(session
)) {
2714 session_lock(session
);
2715 /* Only count the sessions the user can control. */
2716 if (session_access_ok(session
, uid
, gid
) &&
2717 !session
->destroyed
) {
2720 session_unlock(session
);
2721 session_put(session
);
2726 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2727 int *sock_error
, struct lttng_event
*event
)
2730 struct lttng_userspace_probe_location
*probe_location
;
2731 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2732 struct lttng_dynamic_buffer probe_location_buffer
;
2733 struct lttng_buffer_view buffer_view
;
2736 * Create a buffer to store the serialized version of the probe
2739 lttng_dynamic_buffer_init(&probe_location_buffer
);
2740 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2741 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2743 ret
= LTTNG_ERR_NOMEM
;
2748 * Receive the probe location.
2750 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2751 probe_location_buffer
.size
);
2753 DBG("Nothing recv() from client var len data... continuing");
2755 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2756 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2760 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2761 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2764 * Extract the probe location from the serialized version.
2766 ret
= lttng_userspace_probe_location_create_from_buffer(
2767 &buffer_view
, &probe_location
);
2769 WARN("Failed to create a userspace probe location from the received buffer");
2770 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2771 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2776 * Receive the file descriptor to the target binary from the client.
2778 DBG("Receiving userspace probe target FD from client ...");
2779 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2781 DBG("Nothing recv() from client userspace probe fd... continuing");
2783 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2788 * Set the file descriptor received from the client through the unix
2789 * socket in the probe location.
2791 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
2793 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2798 * From the kernel tracer's perspective, all userspace probe event types
2799 * are all the same: a file and an offset.
2801 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
2802 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
2803 ret
= lttng_userspace_probe_location_function_set_binary_fd(
2804 probe_location
, fd
);
2806 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
2807 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
2808 probe_location
, fd
);
2811 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2816 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2820 /* Attach the probe location to the event. */
2821 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
2823 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2827 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2833 * Check if the current kernel tracer supports the session rotation feature.
2834 * Return 1 if it does, 0 otherwise.
2836 static int check_rotate_compatible(void)
2840 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2841 DBG("Kernel tracer version is not compatible with the rotation feature");
2849 * Process the command requested by the lttng client within the command
2850 * context structure. This function make sure that the return structure (llm)
2851 * is set and ready for transmission before returning.
2853 * Return any error encountered or 0 for success.
2855 * "sock" is only used for special-case var. len data.
2857 * Should *NOT* be called with RCU read-side lock held.
2859 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2863 int need_tracing_session
= 1;
2866 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2868 assert(!rcu_read_ongoing());
2872 switch (cmd_ctx
->lsm
->cmd_type
) {
2873 case LTTNG_CREATE_SESSION
:
2874 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2875 case LTTNG_CREATE_SESSION_LIVE
:
2876 case LTTNG_DESTROY_SESSION
:
2877 case LTTNG_LIST_SESSIONS
:
2878 case LTTNG_LIST_DOMAINS
:
2879 case LTTNG_START_TRACE
:
2880 case LTTNG_STOP_TRACE
:
2881 case LTTNG_DATA_PENDING
:
2882 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2883 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2884 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2885 case LTTNG_SNAPSHOT_RECORD
:
2886 case LTTNG_SAVE_SESSION
:
2887 case LTTNG_SET_SESSION_SHM_PATH
:
2888 case LTTNG_REGENERATE_METADATA
:
2889 case LTTNG_REGENERATE_STATEDUMP
:
2890 case LTTNG_REGISTER_TRIGGER
:
2891 case LTTNG_UNREGISTER_TRIGGER
:
2892 case LTTNG_ROTATE_SESSION
:
2893 case LTTNG_ROTATION_GET_INFO
:
2894 case LTTNG_ROTATION_SET_SCHEDULE
:
2895 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2902 if (config
.no_kernel
&& need_domain
2903 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2905 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2907 ret
= LTTNG_ERR_KERN_NA
;
2912 /* Deny register consumer if we already have a spawned consumer. */
2913 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2914 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2915 if (kconsumer_data
.pid
> 0) {
2916 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2917 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2920 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2924 * Check for command that don't needs to allocate a returned payload. We do
2925 * this here so we don't have to make the call for no payload at each
2928 switch(cmd_ctx
->lsm
->cmd_type
) {
2929 case LTTNG_LIST_SESSIONS
:
2930 case LTTNG_LIST_TRACEPOINTS
:
2931 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2932 case LTTNG_LIST_DOMAINS
:
2933 case LTTNG_LIST_CHANNELS
:
2934 case LTTNG_LIST_EVENTS
:
2935 case LTTNG_LIST_SYSCALLS
:
2936 case LTTNG_LIST_TRACKER_PIDS
:
2937 case LTTNG_DATA_PENDING
:
2938 case LTTNG_ROTATE_SESSION
:
2939 case LTTNG_ROTATION_GET_INFO
:
2940 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2943 /* Setup lttng message with no payload */
2944 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2946 /* This label does not try to unlock the session */
2947 goto init_setup_error
;
2951 /* Commands that DO NOT need a session. */
2952 switch (cmd_ctx
->lsm
->cmd_type
) {
2953 case LTTNG_CREATE_SESSION
:
2954 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2955 case LTTNG_CREATE_SESSION_LIVE
:
2956 case LTTNG_LIST_SESSIONS
:
2957 case LTTNG_LIST_TRACEPOINTS
:
2958 case LTTNG_LIST_SYSCALLS
:
2959 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2960 case LTTNG_SAVE_SESSION
:
2961 case LTTNG_REGISTER_TRIGGER
:
2962 case LTTNG_UNREGISTER_TRIGGER
:
2963 need_tracing_session
= 0;
2966 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2968 * We keep the session list lock across _all_ commands
2969 * for now, because the per-session lock does not
2970 * handle teardown properly.
2972 session_lock_list();
2973 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2974 if (cmd_ctx
->session
== NULL
) {
2975 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2978 /* Acquire lock for the session */
2979 session_lock(cmd_ctx
->session
);
2985 * Commands that need a valid session but should NOT create one if none
2986 * exists. Instead of creating one and destroying it when the command is
2987 * handled, process that right before so we save some round trip in useless
2990 switch (cmd_ctx
->lsm
->cmd_type
) {
2991 case LTTNG_DISABLE_CHANNEL
:
2992 case LTTNG_DISABLE_EVENT
:
2993 switch (cmd_ctx
->lsm
->domain
.type
) {
2994 case LTTNG_DOMAIN_KERNEL
:
2995 if (!cmd_ctx
->session
->kernel_session
) {
2996 ret
= LTTNG_ERR_NO_CHANNEL
;
3000 case LTTNG_DOMAIN_JUL
:
3001 case LTTNG_DOMAIN_LOG4J
:
3002 case LTTNG_DOMAIN_PYTHON
:
3003 case LTTNG_DOMAIN_UST
:
3004 if (!cmd_ctx
->session
->ust_session
) {
3005 ret
= LTTNG_ERR_NO_CHANNEL
;
3010 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3022 * Check domain type for specific "pre-action".
3024 switch (cmd_ctx
->lsm
->domain
.type
) {
3025 case LTTNG_DOMAIN_KERNEL
:
3027 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3031 /* Kernel tracer check */
3032 if (kernel_tracer_fd
== -1) {
3033 /* Basically, load kernel tracer modules */
3034 ret
= init_kernel_tracer();
3040 /* Consumer is in an ERROR state. Report back to client */
3041 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3042 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3046 /* Need a session for kernel command */
3047 if (need_tracing_session
) {
3048 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3049 ret
= create_kernel_session(cmd_ctx
->session
);
3051 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3056 /* Start the kernel consumer daemon */
3057 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3058 if (kconsumer_data
.pid
== 0 &&
3059 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3060 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3061 ret
= start_consumerd(&kconsumer_data
);
3063 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3066 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3068 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3072 * The consumer was just spawned so we need to add the socket to
3073 * the consumer output of the session if exist.
3075 ret
= consumer_create_socket(&kconsumer_data
,
3076 cmd_ctx
->session
->kernel_session
->consumer
);
3083 case LTTNG_DOMAIN_JUL
:
3084 case LTTNG_DOMAIN_LOG4J
:
3085 case LTTNG_DOMAIN_PYTHON
:
3086 case LTTNG_DOMAIN_UST
:
3088 if (!ust_app_supported()) {
3089 ret
= LTTNG_ERR_NO_UST
;
3092 /* Consumer is in an ERROR state. Report back to client */
3093 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3094 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3098 if (need_tracing_session
) {
3099 /* Create UST session if none exist. */
3100 if (cmd_ctx
->session
->ust_session
== NULL
) {
3101 ret
= create_ust_session(cmd_ctx
->session
,
3102 &cmd_ctx
->lsm
->domain
);
3103 if (ret
!= LTTNG_OK
) {
3108 /* Start the UST consumer daemons */
3110 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3111 if (config
.consumerd64_bin_path
.value
&&
3112 ustconsumer64_data
.pid
== 0 &&
3113 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3114 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3115 ret
= start_consumerd(&ustconsumer64_data
);
3117 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3118 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3122 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3123 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3125 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3129 * Setup socket for consumer 64 bit. No need for atomic access
3130 * since it was set above and can ONLY be set in this thread.
3132 ret
= consumer_create_socket(&ustconsumer64_data
,
3133 cmd_ctx
->session
->ust_session
->consumer
);
3139 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3140 if (config
.consumerd32_bin_path
.value
&&
3141 ustconsumer32_data
.pid
== 0 &&
3142 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3143 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3144 ret
= start_consumerd(&ustconsumer32_data
);
3146 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3147 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3151 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3152 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3154 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3158 * Setup socket for consumer 32 bit. No need for atomic access
3159 * since it was set above and can ONLY be set in this thread.
3161 ret
= consumer_create_socket(&ustconsumer32_data
,
3162 cmd_ctx
->session
->ust_session
->consumer
);
3174 /* Validate consumer daemon state when start/stop trace command */
3175 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3176 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3177 switch (cmd_ctx
->lsm
->domain
.type
) {
3178 case LTTNG_DOMAIN_NONE
:
3180 case LTTNG_DOMAIN_JUL
:
3181 case LTTNG_DOMAIN_LOG4J
:
3182 case LTTNG_DOMAIN_PYTHON
:
3183 case LTTNG_DOMAIN_UST
:
3184 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3185 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3189 case LTTNG_DOMAIN_KERNEL
:
3190 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3191 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3196 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3202 * Check that the UID or GID match that of the tracing session.
3203 * The root user can interact with all sessions.
3205 if (need_tracing_session
) {
3206 if (!session_access_ok(cmd_ctx
->session
,
3207 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3208 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
3209 cmd_ctx
->session
->destroyed
) {
3210 ret
= LTTNG_ERR_EPERM
;
3216 * Send relayd information to consumer as soon as we have a domain and a
3219 if (cmd_ctx
->session
&& need_domain
) {
3221 * Setup relayd if not done yet. If the relayd information was already
3222 * sent to the consumer, this call will gracefully return.
3224 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3225 if (ret
!= LTTNG_OK
) {
3230 /* Process by command type */
3231 switch (cmd_ctx
->lsm
->cmd_type
) {
3232 case LTTNG_ADD_CONTEXT
:
3235 * An LTTNG_ADD_CONTEXT command might have a supplementary
3236 * payload if the context being added is an application context.
3238 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3239 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3240 char *provider_name
= NULL
, *context_name
= NULL
;
3241 size_t provider_name_len
=
3242 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3243 size_t context_name_len
=
3244 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3246 if (provider_name_len
== 0 || context_name_len
== 0) {
3248 * Application provider and context names MUST
3251 ret
= -LTTNG_ERR_INVALID
;
3255 provider_name
= zmalloc(provider_name_len
+ 1);
3256 if (!provider_name
) {
3257 ret
= -LTTNG_ERR_NOMEM
;
3260 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3263 context_name
= zmalloc(context_name_len
+ 1);
3264 if (!context_name
) {
3265 ret
= -LTTNG_ERR_NOMEM
;
3266 goto error_add_context
;
3268 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3271 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3274 goto error_add_context
;
3277 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3280 goto error_add_context
;
3285 * cmd_add_context assumes ownership of the provider and context
3288 ret
= cmd_add_context(cmd_ctx
->session
,
3289 cmd_ctx
->lsm
->domain
.type
,
3290 cmd_ctx
->lsm
->u
.context
.channel_name
,
3291 &cmd_ctx
->lsm
->u
.context
.ctx
,
3292 kernel_poll_pipe
[1]);
3294 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3295 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3297 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3298 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3304 case LTTNG_DISABLE_CHANNEL
:
3306 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3307 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3310 case LTTNG_DISABLE_EVENT
:
3314 * FIXME: handle filter; for now we just receive the filter's
3315 * bytecode along with the filter expression which are sent by
3316 * liblttng-ctl and discard them.
3318 * This fixes an issue where the client may block while sending
3319 * the filter payload and encounter an error because the session
3320 * daemon closes the socket without ever handling this data.
3322 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3323 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3326 char data
[LTTNG_FILTER_MAX_LEN
];
3328 DBG("Discarding disable event command payload of size %zu", count
);
3330 ret
= lttcomm_recv_unix_sock(sock
, data
,
3331 count
> sizeof(data
) ? sizeof(data
) : count
);
3336 count
-= (size_t) ret
;
3339 /* FIXME: passing packed structure to non-packed pointer */
3340 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3341 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3342 &cmd_ctx
->lsm
->u
.disable
.event
);
3345 case LTTNG_ENABLE_CHANNEL
:
3347 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3348 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3349 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3350 &cmd_ctx
->lsm
->u
.channel
.chan
,
3351 kernel_poll_pipe
[1]);
3354 case LTTNG_TRACK_PID
:
3356 ret
= cmd_track_pid(cmd_ctx
->session
,
3357 cmd_ctx
->lsm
->domain
.type
,
3358 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3361 case LTTNG_UNTRACK_PID
:
3363 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3364 cmd_ctx
->lsm
->domain
.type
,
3365 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3368 case LTTNG_ENABLE_EVENT
:
3370 struct lttng_event
*ev
= NULL
;
3371 struct lttng_event_exclusion
*exclusion
= NULL
;
3372 struct lttng_filter_bytecode
*bytecode
= NULL
;
3373 char *filter_expression
= NULL
;
3375 /* Handle exclusion events and receive it from the client. */
3376 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3377 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3379 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3380 (count
* LTTNG_SYMBOL_NAME_LEN
));
3382 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3386 DBG("Receiving var len exclusion event list from client ...");
3387 exclusion
->count
= count
;
3388 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3389 count
* LTTNG_SYMBOL_NAME_LEN
);
3391 DBG("Nothing recv() from client var len data... continuing");
3394 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3399 /* Get filter expression from client. */
3400 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3401 size_t expression_len
=
3402 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3404 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3405 ret
= LTTNG_ERR_FILTER_INVAL
;
3410 filter_expression
= zmalloc(expression_len
);
3411 if (!filter_expression
) {
3413 ret
= LTTNG_ERR_FILTER_NOMEM
;
3417 /* Receive var. len. data */
3418 DBG("Receiving var len filter's expression from client ...");
3419 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3422 DBG("Nothing recv() from client var len data... continuing");
3424 free(filter_expression
);
3426 ret
= LTTNG_ERR_FILTER_INVAL
;
3431 /* Handle filter and get bytecode from client. */
3432 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3433 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3435 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3436 ret
= LTTNG_ERR_FILTER_INVAL
;
3437 free(filter_expression
);
3442 bytecode
= zmalloc(bytecode_len
);
3444 free(filter_expression
);
3446 ret
= LTTNG_ERR_FILTER_NOMEM
;
3450 /* Receive var. len. data */
3451 DBG("Receiving var len filter's bytecode from client ...");
3452 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3454 DBG("Nothing recv() from client var len data... continuing");
3456 free(filter_expression
);
3459 ret
= LTTNG_ERR_FILTER_INVAL
;
3463 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3464 free(filter_expression
);
3467 ret
= LTTNG_ERR_FILTER_INVAL
;
3472 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
3474 DBG("Failed to copy event: %s",
3475 cmd_ctx
->lsm
->u
.enable
.event
.name
);
3476 free(filter_expression
);
3479 ret
= LTTNG_ERR_NOMEM
;
3484 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
3485 /* Expect a userspace probe description. */
3486 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
3488 free(filter_expression
);
3491 lttng_event_destroy(ev
);
3496 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3497 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3499 filter_expression
, bytecode
, exclusion
,
3500 kernel_poll_pipe
[1]);
3501 lttng_event_destroy(ev
);
3504 case LTTNG_LIST_TRACEPOINTS
:
3506 struct lttng_event
*events
;
3509 session_lock_list();
3510 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3511 session_unlock_list();
3512 if (nb_events
< 0) {
3513 /* Return value is a negative lttng_error_code. */
3519 * Setup lttng message with payload size set to the event list size in
3520 * bytes and then copy list into the llm payload.
3522 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3523 sizeof(struct lttng_event
) * nb_events
);
3533 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3535 struct lttng_event_field
*fields
;
3538 session_lock_list();
3539 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3541 session_unlock_list();
3542 if (nb_fields
< 0) {
3543 /* Return value is a negative lttng_error_code. */
3549 * Setup lttng message with payload size set to the event list size in
3550 * bytes and then copy list into the llm payload.
3552 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3553 sizeof(struct lttng_event_field
) * nb_fields
);
3563 case LTTNG_LIST_SYSCALLS
:
3565 struct lttng_event
*events
;
3568 nb_events
= cmd_list_syscalls(&events
);
3569 if (nb_events
< 0) {
3570 /* Return value is a negative lttng_error_code. */
3576 * Setup lttng message with payload size set to the event list size in
3577 * bytes and then copy list into the llm payload.
3579 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3580 sizeof(struct lttng_event
) * nb_events
);
3590 case LTTNG_LIST_TRACKER_PIDS
:
3592 int32_t *pids
= NULL
;
3595 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3596 cmd_ctx
->lsm
->domain
.type
, &pids
);
3598 /* Return value is a negative lttng_error_code. */
3604 * Setup lttng message with payload size set to the event list size in
3605 * bytes and then copy list into the llm payload.
3607 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3608 sizeof(int32_t) * nr_pids
);
3618 case LTTNG_SET_CONSUMER_URI
:
3621 struct lttng_uri
*uris
;
3623 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3624 len
= nb_uri
* sizeof(struct lttng_uri
);
3627 ret
= LTTNG_ERR_INVALID
;
3631 uris
= zmalloc(len
);
3633 ret
= LTTNG_ERR_FATAL
;
3637 /* Receive variable len data */
3638 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3639 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3641 DBG("No URIs received from client... continuing");
3643 ret
= LTTNG_ERR_SESSION_FAIL
;
3648 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3650 if (ret
!= LTTNG_OK
) {
3657 case LTTNG_START_TRACE
:
3660 * On the first start, if we have a kernel session and we have
3661 * enabled time or size-based rotations, we have to make sure
3662 * the kernel tracer supports it.
3664 if (!cmd_ctx
->session
->has_been_started
&& \
3665 cmd_ctx
->session
->kernel_session
&& \
3666 (cmd_ctx
->session
->rotate_timer_period
|| \
3667 cmd_ctx
->session
->rotate_size
) && \
3668 !check_rotate_compatible()) {
3669 DBG("Kernel tracer version is not compatible with the rotation feature");
3670 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3673 ret
= cmd_start_trace(cmd_ctx
->session
);
3676 case LTTNG_STOP_TRACE
:
3678 ret
= cmd_stop_trace(cmd_ctx
->session
);
3681 case LTTNG_CREATE_SESSION
:
3684 struct lttng_uri
*uris
= NULL
;
3686 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3687 len
= nb_uri
* sizeof(struct lttng_uri
);
3690 uris
= zmalloc(len
);
3692 ret
= LTTNG_ERR_FATAL
;
3696 /* Receive variable len data */
3697 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3698 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3700 DBG("No URIs received from client... continuing");
3702 ret
= LTTNG_ERR_SESSION_FAIL
;
3707 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {