2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "common/buffer-view.h"
11 #include "common/compat/socket.h"
12 #include "common/dynamic-buffer.h"
13 #include "common/dynamic-array.h"
14 #include "common/payload.h"
15 #include "common/payload-view.h"
16 #include "common/sessiond-comm/sessiond-comm.h"
17 #include "common/payload.h"
18 #include "common/payload-view.h"
19 #include "lttng/lttng-error.h"
20 #include "lttng/tracker.h"
21 #include <common/compat/getenv.h>
22 #include <common/tracker.h>
23 #include <common/unix.h>
24 #include <common/utils.h>
25 #include <lttng/event-internal.h>
26 #include <lttng/session-descriptor-internal.h>
27 #include <lttng/session-internal.h>
28 #include <lttng/userspace-probe-internal.h>
35 #include "lttng-sessiond.h"
39 #include "health-sessiond.h"
40 #include "testpoint.h"
42 #include "manage-consumer.h"
47 static struct thread_state
{
53 static void set_thread_status(bool running
)
55 DBG("Marking client thread's state as %s", running
? "running" : "error");
56 thread_state
.running
= running
;
57 sem_post(&thread_state
.ready
);
60 static bool wait_thread_status(void)
62 DBG("Waiting for client thread to be ready");
63 sem_wait(&thread_state
.ready
);
64 if (thread_state
.running
) {
65 DBG("Client thread is ready");
67 ERR("Initialization of client thread failed");
70 return thread_state
.running
;
74 * Setup the outgoing data buffer for the response (llm) by allocating the
75 * right amount of memory and copying the original information from the lsm
78 * Return 0 on success, negative value on error.
80 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
81 const void *payload_buf
, size_t payload_len
,
82 const void *cmd_header_buf
, size_t cmd_header_len
)
85 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
86 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
87 const struct lttcomm_lttng_msg llm
= {
88 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
89 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
90 .cmd_header_size
= cmd_header_len
,
91 .data_size
= payload_len
,
94 lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
95 lttng_dynamic_array_clear(&cmd_ctx
->reply_payload
._fds
);
97 cmd_ctx
->lttng_msg_size
= total_msg_size
;
99 /* Append reply header. */
100 ret
= lttng_dynamic_buffer_append(
101 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
106 /* Append command header. */
107 if (cmd_header_len
) {
108 ret
= lttng_dynamic_buffer_append(
109 &cmd_ctx
->reply_payload
.buffer
, cmd_header_buf
,
116 /* Append payload. */
118 ret
= lttng_dynamic_buffer_append(
119 &cmd_ctx
->reply_payload
.buffer
, payload_buf
,
130 static int setup_empty_lttng_msg(struct command_ctx
*cmd_ctx
)
133 const struct lttcomm_lttng_msg llm
= {};
135 lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
137 /* Append place-holder reply header. */
138 ret
= lttng_dynamic_buffer_append(
139 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
144 cmd_ctx
->lttng_msg_size
= sizeof(llm
);
149 static void update_lttng_msg(struct command_ctx
*cmd_ctx
, size_t cmd_header_len
,
152 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
153 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
154 const struct lttcomm_lttng_msg llm
= {
155 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
156 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
157 .cmd_header_size
= cmd_header_len
,
158 .data_size
= payload_len
,
160 struct lttcomm_lttng_msg
*p_llm
;
162 assert(cmd_ctx
->reply_payload
.buffer
.size
>= sizeof(llm
));
164 p_llm
= (typeof(p_llm
)) cmd_ctx
->reply_payload
.buffer
.data
;
166 /* Update existing header. */
167 memcpy(p_llm
, &llm
, sizeof(llm
));
169 cmd_ctx
->lttng_msg_size
= total_msg_size
;
173 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
174 * exec or it will fail.
176 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
178 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
182 * Fork and exec a consumer daemon (consumerd).
184 * Return pid if successful else -1.
186 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
190 const char *consumer_to_use
;
191 const char *verbosity
;
194 DBG("Spawning consumerd");
201 if (config
.verbose_consumer
) {
202 verbosity
= "--verbose";
203 } else if (lttng_opt_quiet
) {
204 verbosity
= "--quiet";
209 switch (consumer_data
->type
) {
210 case LTTNG_CONSUMER_KERNEL
:
212 * Find out which consumerd to execute. We will first try the
213 * 64-bit path, then the sessiond's installation directory, and
214 * fallback on the 32-bit one,
216 DBG3("Looking for a kernel consumer at these locations:");
217 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
218 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
219 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
220 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
221 DBG3("Found location #1");
222 consumer_to_use
= config
.consumerd64_bin_path
.value
;
223 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
224 DBG3("Found location #2");
225 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
226 } else if (config
.consumerd32_bin_path
.value
&&
227 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
228 DBG3("Found location #3");
229 consumer_to_use
= config
.consumerd32_bin_path
.value
;
231 DBG("Could not find any valid consumerd executable");
235 DBG("Using kernel consumer at: %s", consumer_to_use
);
236 (void) execl(consumer_to_use
,
237 "lttng-consumerd", verbosity
, "-k",
238 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
239 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
240 "--group", config
.tracing_group_name
.value
,
243 case LTTNG_CONSUMER64_UST
:
245 if (config
.consumerd64_lib_dir
.value
) {
250 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
254 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
255 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
260 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
261 if (tmp
[0] != '\0') {
265 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
272 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
273 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
274 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
275 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
276 "--group", config
.tracing_group_name
.value
,
280 case LTTNG_CONSUMER32_UST
:
282 if (config
.consumerd32_lib_dir
.value
) {
287 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
291 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
292 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
297 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
298 if (tmp
[0] != '\0') {
302 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
309 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
310 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
311 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
312 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
313 "--group", config
.tracing_group_name
.value
,
318 ERR("unknown consumer type");
322 PERROR("Consumer execl()");
324 /* Reaching this point, we got a failure on our execl(). */
326 } else if (pid
> 0) {
329 PERROR("start consumer fork");
337 * Spawn the consumerd daemon and session daemon thread.
339 static int start_consumerd(struct consumer_data
*consumer_data
)
344 * Set the listen() state on the socket since there is a possible race
345 * between the exec() of the consumer daemon and this call if place in the
346 * consumer thread. See bug #366 for more details.
348 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
353 pthread_mutex_lock(&consumer_data
->pid_mutex
);
354 if (consumer_data
->pid
!= 0) {
355 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
359 ret
= spawn_consumerd(consumer_data
);
361 ERR("Spawning consumerd failed");
362 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
366 /* Setting up the consumer_data pid */
367 consumer_data
->pid
= ret
;
368 DBG2("Consumer pid %d", consumer_data
->pid
);
369 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
371 DBG2("Spawning consumer control thread");
372 ret
= spawn_consumer_thread(consumer_data
);
374 ERR("Fatal error spawning consumer control thread");
382 /* Cleanup already created sockets on error. */
383 if (consumer_data
->err_sock
>= 0) {
386 err
= close(consumer_data
->err_sock
);
388 PERROR("close consumer data error socket");
395 * Copy consumer output from the tracing session to the domain session. The
396 * function also applies the right modification on a per domain basis for the
397 * trace files destination directory.
399 * Should *NOT* be called with RCU read-side lock held.
401 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
404 const char *dir_name
;
405 struct consumer_output
*consumer
;
408 assert(session
->consumer
);
411 case LTTNG_DOMAIN_KERNEL
:
412 DBG3("Copying tracing session consumer output in kernel session");
414 * XXX: We should audit the session creation and what this function
415 * does "extra" in order to avoid a destroy since this function is used
416 * in the domain session creation (kernel and ust) only. Same for UST
419 if (session
->kernel_session
->consumer
) {
420 consumer_output_put(session
->kernel_session
->consumer
);
422 session
->kernel_session
->consumer
=
423 consumer_copy_output(session
->consumer
);
424 /* Ease our life a bit for the next part */
425 consumer
= session
->kernel_session
->consumer
;
426 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
428 case LTTNG_DOMAIN_JUL
:
429 case LTTNG_DOMAIN_LOG4J
:
430 case LTTNG_DOMAIN_PYTHON
:
431 case LTTNG_DOMAIN_UST
:
432 DBG3("Copying tracing session consumer output in UST session");
433 if (session
->ust_session
->consumer
) {
434 consumer_output_put(session
->ust_session
->consumer
);
436 session
->ust_session
->consumer
=
437 consumer_copy_output(session
->consumer
);
438 /* Ease our life a bit for the next part */
439 consumer
= session
->ust_session
->consumer
;
440 dir_name
= DEFAULT_UST_TRACE_DIR
;
443 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
447 /* Append correct directory to subdir */
448 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
449 sizeof(consumer
->domain_subdir
));
454 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
462 * Create an UST session and add it to the session ust list.
464 * Should *NOT* be called with RCU read-side lock held.
466 static int create_ust_session(struct ltt_session
*session
,
467 const struct lttng_domain
*domain
)
470 struct ltt_ust_session
*lus
= NULL
;
474 assert(session
->consumer
);
476 switch (domain
->type
) {
477 case LTTNG_DOMAIN_JUL
:
478 case LTTNG_DOMAIN_LOG4J
:
479 case LTTNG_DOMAIN_PYTHON
:
480 case LTTNG_DOMAIN_UST
:
483 ERR("Unknown UST domain on create session %d", domain
->type
);
484 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
488 DBG("Creating UST session");
490 lus
= trace_ust_create_session(session
->id
);
492 ret
= LTTNG_ERR_UST_SESS_FAIL
;
496 lus
->uid
= session
->uid
;
497 lus
->gid
= session
->gid
;
498 lus
->output_traces
= session
->output_traces
;
499 lus
->snapshot_mode
= session
->snapshot_mode
;
500 lus
->live_timer_interval
= session
->live_timer
;
501 session
->ust_session
= lus
;
502 if (session
->shm_path
[0]) {
503 strncpy(lus
->root_shm_path
, session
->shm_path
,
504 sizeof(lus
->root_shm_path
));
505 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
506 strncpy(lus
->shm_path
, session
->shm_path
,
507 sizeof(lus
->shm_path
));
508 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
509 strncat(lus
->shm_path
, "/ust",
510 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
512 /* Copy session output to the newly created UST session */
513 ret
= copy_session_consumer(domain
->type
, session
);
514 if (ret
!= LTTNG_OK
) {
522 session
->ust_session
= NULL
;
527 * Create a kernel tracer session then create the default channel.
529 static int create_kernel_session(struct ltt_session
*session
)
533 DBG("Creating kernel session");
535 ret
= kernel_create_session(session
);
537 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
541 /* Code flow safety */
542 assert(session
->kernel_session
);
544 /* Copy session output to the newly created Kernel session */
545 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
546 if (ret
!= LTTNG_OK
) {
550 session
->kernel_session
->uid
= session
->uid
;
551 session
->kernel_session
->gid
= session
->gid
;
552 session
->kernel_session
->output_traces
= session
->output_traces
;
553 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
554 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
559 trace_kernel_destroy_session(session
->kernel_session
);
560 session
->kernel_session
= NULL
;
566 * Count number of session permitted by uid/gid.
568 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
571 struct ltt_session
*session
;
572 const struct ltt_session_list
*session_list
= session_get_list();
574 DBG("Counting number of available session for UID %d GID %d",
576 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
577 if (!session_get(session
)) {
580 session_lock(session
);
581 /* Only count the sessions the user can control. */
582 if (session_access_ok(session
, uid
, gid
) &&
583 !session
->destroyed
) {
586 session_unlock(session
);
587 session_put(session
);
592 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
593 int *sock_error
, struct lttng_event
*event
)
596 struct lttng_userspace_probe_location
*probe_location
;
597 struct lttng_payload probe_location_payload
;
600 * Create a payload to store the serialized version of the probe
603 lttng_payload_init(&probe_location_payload
);
605 ret
= lttng_dynamic_buffer_set_size(&probe_location_payload
.buffer
,
606 cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
);
608 ret
= LTTNG_ERR_NOMEM
;
613 * Receive the probe location.
615 ret
= lttcomm_recv_unix_sock(sock
, probe_location_payload
.buffer
.data
,
616 probe_location_payload
.buffer
.size
);
618 DBG("Nothing recv() from client var len data... continuing");
620 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
625 * Receive the file descriptor to the target binary from the client.
627 DBG("Receiving userspace probe target FD from client ...");
628 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
630 DBG("Nothing recv() from client userspace probe fd... continuing");
632 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
636 ret
= lttng_payload_push_fd(&probe_location_payload
, fd
);
638 ERR("Failed to add userspace probe file descriptor to payload");
639 ret
= LTTNG_ERR_NOMEM
;
644 struct lttng_payload_view view
= lttng_payload_view_from_payload(
645 &probe_location_payload
, 0, -1);
647 /* Extract the probe location from the serialized version. */
648 ret
= lttng_userspace_probe_location_create_from_payload(
649 &view
, &probe_location
);
652 WARN("Failed to create a userspace probe location from the received buffer");
653 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
657 /* Attach the probe location to the event. */
658 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
660 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
665 lttng_payload_reset(&probe_location_payload
);
670 * Version of setup_lttng_msg() without command header.
672 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
673 void *payload_buf
, size_t payload_len
)
675 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
679 * Check if the current kernel tracer supports the session rotation feature.
680 * Return 1 if it does, 0 otherwise.
682 static int check_rotate_compatible(void)
686 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
687 DBG("Kernel tracer version is not compatible with the rotation feature");
695 * Send data on a unix socket using the liblttsessiondcomm API.
697 * Return lttcomm error code.
699 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
703 /* Check valid length */
704 if (view
->buffer
.size
== 0) {
709 ret
= lttcomm_send_unix_sock(
710 sock
, view
->buffer
.data
, view
->buffer
.size
);
715 if (lttng_dynamic_array_get_count(&view
->_fds
) > 0) {
716 ret
= lttcomm_send_fds_unix_sock(sock
,
717 (const int *) view
->_fds
.buffer
.data
,
718 lttng_dynamic_array_get_count(&view
->_fds
));
726 * Process the command requested by the lttng client within the command
727 * context structure. This function make sure that the return structure (llm)
728 * is set and ready for transmission before returning.
730 * Return any error encountered or 0 for success.
732 * "sock" is only used for special-case var. len data.
733 * A command may assume the ownership of the socket, in which case its value
734 * should be set to -1.
736 * Should *NOT* be called with RCU read-side lock held.
738 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
742 int need_tracing_session
= 1;
745 DBG("Processing client command %d", cmd_ctx
->lsm
.cmd_type
);
747 assert(!rcu_read_ongoing());
751 switch (cmd_ctx
->lsm
.cmd_type
) {
752 case LTTNG_CREATE_SESSION_EXT
:
753 case LTTNG_DESTROY_SESSION
:
754 case LTTNG_LIST_SESSIONS
:
755 case LTTNG_LIST_DOMAINS
:
756 case LTTNG_START_TRACE
:
757 case LTTNG_STOP_TRACE
:
758 case LTTNG_DATA_PENDING
:
759 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
760 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
761 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
762 case LTTNG_SNAPSHOT_RECORD
:
763 case LTTNG_SAVE_SESSION
:
764 case LTTNG_SET_SESSION_SHM_PATH
:
765 case LTTNG_REGENERATE_METADATA
:
766 case LTTNG_REGENERATE_STATEDUMP
:
767 case LTTNG_REGISTER_TRIGGER
:
768 case LTTNG_UNREGISTER_TRIGGER
:
769 case LTTNG_ROTATE_SESSION
:
770 case LTTNG_ROTATION_GET_INFO
:
771 case LTTNG_ROTATION_SET_SCHEDULE
:
772 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
773 case LTTNG_CLEAR_SESSION
:
780 if (config
.no_kernel
&& need_domain
781 && cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
783 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
785 ret
= LTTNG_ERR_KERN_NA
;
790 /* Deny register consumer if we already have a spawned consumer. */
791 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
792 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
793 if (kconsumer_data
.pid
> 0) {
794 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
795 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
798 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
802 * Check for command that don't needs to allocate a returned payload. We do
803 * this here so we don't have to make the call for no payload at each
806 switch(cmd_ctx
->lsm
.cmd_type
) {
807 case LTTNG_LIST_SESSIONS
:
808 case LTTNG_LIST_TRACEPOINTS
:
809 case LTTNG_LIST_TRACEPOINT_FIELDS
:
810 case LTTNG_LIST_DOMAINS
:
811 case LTTNG_LIST_CHANNELS
:
812 case LTTNG_LIST_EVENTS
:
813 case LTTNG_LIST_SYSCALLS
:
814 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
815 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
816 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
817 case LTTNG_DATA_PENDING
:
818 case LTTNG_ROTATE_SESSION
:
819 case LTTNG_ROTATION_GET_INFO
:
822 /* Setup lttng message with no payload */
823 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
825 /* This label does not try to unlock the session */
826 goto init_setup_error
;
830 /* Commands that DO NOT need a session. */
831 switch (cmd_ctx
->lsm
.cmd_type
) {
832 case LTTNG_CREATE_SESSION_EXT
:
833 case LTTNG_LIST_SESSIONS
:
834 case LTTNG_LIST_TRACEPOINTS
:
835 case LTTNG_LIST_SYSCALLS
:
836 case LTTNG_LIST_TRACEPOINT_FIELDS
:
837 case LTTNG_SAVE_SESSION
:
838 case LTTNG_REGISTER_TRIGGER
:
839 case LTTNG_UNREGISTER_TRIGGER
:
840 need_tracing_session
= 0;
843 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
845 * We keep the session list lock across _all_ commands
846 * for now, because the per-session lock does not
847 * handle teardown properly.
850 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
851 if (cmd_ctx
->session
== NULL
) {
852 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
855 /* Acquire lock for the session */
856 session_lock(cmd_ctx
->session
);
862 * Commands that need a valid session but should NOT create one if none
863 * exists. Instead of creating one and destroying it when the command is
864 * handled, process that right before so we save some round trip in useless
867 switch (cmd_ctx
->lsm
.cmd_type
) {
868 case LTTNG_DISABLE_CHANNEL
:
869 case LTTNG_DISABLE_EVENT
:
870 switch (cmd_ctx
->lsm
.domain
.type
) {
871 case LTTNG_DOMAIN_KERNEL
:
872 if (!cmd_ctx
->session
->kernel_session
) {
873 ret
= LTTNG_ERR_NO_CHANNEL
;
877 case LTTNG_DOMAIN_JUL
:
878 case LTTNG_DOMAIN_LOG4J
:
879 case LTTNG_DOMAIN_PYTHON
:
880 case LTTNG_DOMAIN_UST
:
881 if (!cmd_ctx
->session
->ust_session
) {
882 ret
= LTTNG_ERR_NO_CHANNEL
;
887 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
899 * Check domain type for specific "pre-action".
901 switch (cmd_ctx
->lsm
.domain
.type
) {
902 case LTTNG_DOMAIN_KERNEL
:
904 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
908 /* Kernel tracer check */
909 if (!kernel_tracer_is_initialized()) {
910 /* Basically, load kernel tracer modules */
911 ret
= init_kernel_tracer();
917 /* Consumer is in an ERROR state. Report back to client */
918 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
919 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
923 /* Need a session for kernel command */
924 if (need_tracing_session
) {
925 if (cmd_ctx
->session
->kernel_session
== NULL
) {
926 ret
= create_kernel_session(cmd_ctx
->session
);
927 if (ret
!= LTTNG_OK
) {
928 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
933 /* Start the kernel consumer daemon */
934 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
935 if (kconsumer_data
.pid
== 0 &&
936 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
937 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
938 ret
= start_consumerd(&kconsumer_data
);
940 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
943 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
945 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
949 * The consumer was just spawned so we need to add the socket to
950 * the consumer output of the session if exist.
952 ret
= consumer_create_socket(&kconsumer_data
,
953 cmd_ctx
->session
->kernel_session
->consumer
);
960 case LTTNG_DOMAIN_JUL
:
961 case LTTNG_DOMAIN_LOG4J
:
962 case LTTNG_DOMAIN_PYTHON
:
963 case LTTNG_DOMAIN_UST
:
965 if (!ust_app_supported()) {
966 ret
= LTTNG_ERR_NO_UST
;
969 /* Consumer is in an ERROR state. Report back to client */
970 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
971 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
975 if (need_tracing_session
) {
976 /* Create UST session if none exist. */
977 if (cmd_ctx
->session
->ust_session
== NULL
) {
978 ret
= create_ust_session(cmd_ctx
->session
,
979 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
));
980 if (ret
!= LTTNG_OK
) {
985 /* Start the UST consumer daemons */
987 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
988 if (config
.consumerd64_bin_path
.value
&&
989 ustconsumer64_data
.pid
== 0 &&
990 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
991 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
992 ret
= start_consumerd(&ustconsumer64_data
);
994 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
995 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
999 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
1000 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1002 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
1006 * Setup socket for consumer 64 bit. No need for atomic access
1007 * since it was set above and can ONLY be set in this thread.
1009 ret
= consumer_create_socket(&ustconsumer64_data
,
1010 cmd_ctx
->session
->ust_session
->consumer
);
1016 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
1017 if (config
.consumerd32_bin_path
.value
&&
1018 ustconsumer32_data
.pid
== 0 &&
1019 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1020 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1021 ret
= start_consumerd(&ustconsumer32_data
);
1023 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1024 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
1028 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
1029 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1031 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1035 * Setup socket for consumer 32 bit. No need for atomic access
1036 * since it was set above and can ONLY be set in this thread.
1038 ret
= consumer_create_socket(&ustconsumer32_data
,
1039 cmd_ctx
->session
->ust_session
->consumer
);
1051 /* Validate consumer daemon state when start/stop trace command */
1052 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1053 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1054 switch (cmd_ctx
->lsm
.domain
.type
) {
1055 case LTTNG_DOMAIN_NONE
:
1057 case LTTNG_DOMAIN_JUL
:
1058 case LTTNG_DOMAIN_LOG4J
:
1059 case LTTNG_DOMAIN_PYTHON
:
1060 case LTTNG_DOMAIN_UST
:
1061 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1062 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1066 case LTTNG_DOMAIN_KERNEL
:
1067 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1068 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1073 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1079 * Check that the UID or GID match that of the tracing session.
1080 * The root user can interact with all sessions.
1082 if (need_tracing_session
) {
1083 if (!session_access_ok(cmd_ctx
->session
,
1084 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1085 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1086 cmd_ctx
->session
->destroyed
) {
1087 ret
= LTTNG_ERR_EPERM
;
1093 * Send relayd information to consumer as soon as we have a domain and a
1096 if (cmd_ctx
->session
&& need_domain
) {
1098 * Setup relayd if not done yet. If the relayd information was already
1099 * sent to the consumer, this call will gracefully return.
1101 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1102 if (ret
!= LTTNG_OK
) {
1107 /* Process by command type */
1108 switch (cmd_ctx
->lsm
.cmd_type
) {
1109 case LTTNG_ADD_CONTEXT
:
1112 * An LTTNG_ADD_CONTEXT command might have a supplementary
1113 * payload if the context being added is an application context.
1115 if (cmd_ctx
->lsm
.u
.context
.ctx
.ctx
==
1116 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1117 char *provider_name
= NULL
, *context_name
= NULL
;
1118 size_t provider_name_len
=
1119 cmd_ctx
->lsm
.u
.context
.provider_name_len
;
1120 size_t context_name_len
=
1121 cmd_ctx
->lsm
.u
.context
.context_name_len
;
1123 if (provider_name_len
== 0 || context_name_len
== 0) {
1125 * Application provider and context names MUST
1128 ret
= -LTTNG_ERR_INVALID
;
1132 provider_name
= zmalloc(provider_name_len
+ 1);
1133 if (!provider_name
) {
1134 ret
= -LTTNG_ERR_NOMEM
;
1137 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
=
1140 context_name
= zmalloc(context_name_len
+ 1);
1141 if (!context_name
) {
1142 ret
= -LTTNG_ERR_NOMEM
;
1143 goto error_add_context
;
1145 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1148 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1151 goto error_add_context
;
1154 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1157 goto error_add_context
;
1162 * cmd_add_context assumes ownership of the provider and context
1165 ret
= cmd_add_context(cmd_ctx
->session
,
1166 cmd_ctx
->lsm
.domain
.type
,
1167 cmd_ctx
->lsm
.u
.context
.channel_name
,
1168 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.context
.ctx
),
1169 kernel_poll_pipe
[1]);
1171 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1172 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1174 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
);
1175 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1181 case LTTNG_DISABLE_CHANNEL
:
1183 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1184 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1187 case LTTNG_DISABLE_EVENT
:
1191 * FIXME: handle filter; for now we just receive the filter's
1192 * bytecode along with the filter expression which are sent by
1193 * liblttng-ctl and discard them.
1195 * This fixes an issue where the client may block while sending
1196 * the filter payload and encounter an error because the session
1197 * daemon closes the socket without ever handling this data.
1199 size_t count
= cmd_ctx
->lsm
.u
.disable
.expression_len
+
1200 cmd_ctx
->lsm
.u
.disable
.bytecode_len
;
1203 char data
[LTTNG_FILTER_MAX_LEN
];
1205 DBG("Discarding disable event command payload of size %zu", count
);
1207 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1208 count
> sizeof(data
) ? sizeof(data
) : count
);
1213 count
-= (size_t) ret
;
1216 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1217 cmd_ctx
->lsm
.u
.disable
.channel_name
,
1218 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.disable
.event
));
1221 case LTTNG_ENABLE_CHANNEL
:
1223 cmd_ctx
->lsm
.u
.channel
.chan
.attr
.extended
.ptr
=
1224 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
.u
.channel
.extended
;
1225 ret
= cmd_enable_channel(cmd_ctx
->session
,
1226 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1227 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.channel
.chan
),
1228 kernel_poll_pipe
[1]);
1231 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1232 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1234 struct lttng_dynamic_buffer payload
;
1235 struct lttng_buffer_view payload_view
;
1236 const bool add_value
=
1237 cmd_ctx
->lsm
.cmd_type
==
1238 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1239 const size_t name_len
=
1240 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1242 const enum lttng_domain_type domain_type
=
1243 (enum lttng_domain_type
)
1244 cmd_ctx
->lsm
.domain
.type
;
1245 const enum lttng_process_attr process_attr
=
1246 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1247 .process_attr_tracker_add_remove_include_value
1249 const enum lttng_process_attr_value_type value_type
=
1250 (enum lttng_process_attr_value_type
) cmd_ctx
1252 .process_attr_tracker_add_remove_include_value
1254 struct process_attr_value
*value
;
1255 enum lttng_error_code ret_code
;
1257 /* Receive remaining variable length payload if applicable. */
1258 if (name_len
> LOGIN_NAME_MAX
) {
1260 * POSIX mandates user and group names that are at least
1261 * 8 characters long. Note that although shadow-utils
1262 * (useradd, groupaadd, etc.) use 32 chars as their
1263 * limit (from bits/utmp.h, UT_NAMESIZE),
1264 * LOGIN_NAME_MAX is defined to 256.
1266 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %d",
1267 add_value
? "addition" : "removal",
1268 name_len
, LOGIN_NAME_MAX
);
1269 ret
= LTTNG_ERR_INVALID
;
1273 lttng_dynamic_buffer_init(&payload
);
1274 if (name_len
!= 0) {
1276 * Receive variable payload for user/group name
1279 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1281 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1282 add_value
? "add" : "remove");
1283 ret
= LTTNG_ERR_NOMEM
;
1284 goto error_add_remove_tracker_value
;
1287 ret
= lttcomm_recv_unix_sock(
1288 *sock
, payload
.data
, name_len
);
1290 ERR("Failed to receive payload of %s process attribute tracker value argument",
1291 add_value
? "add" : "remove");
1293 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1294 goto error_add_remove_tracker_value
;
1298 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1299 &payload
, 0, name_len
);
1301 * Validate the value type and domains are legal for the process
1302 * attribute tracker that is specified and convert the value to
1303 * add/remove to the internal sessiond representation.
1305 ret_code
= process_attr_value_from_comm(domain_type
,
1306 process_attr
, value_type
,
1307 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1309 &payload_view
, &value
);
1310 if (ret_code
!= LTTNG_OK
) {
1312 goto error_add_remove_tracker_value
;
1316 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1317 cmd_ctx
->session
, domain_type
,
1318 process_attr
, value
);
1320 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1321 cmd_ctx
->session
, domain_type
,
1322 process_attr
, value
);
1324 process_attr_value_destroy(value
);
1325 error_add_remove_tracker_value
:
1326 lttng_dynamic_buffer_reset(&payload
);
1329 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1331 enum lttng_tracking_policy tracking_policy
;
1332 const enum lttng_domain_type domain_type
=
1333 (enum lttng_domain_type
)
1334 cmd_ctx
->lsm
.domain
.type
;
1335 const enum lttng_process_attr process_attr
=
1336 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1337 .process_attr_tracker_get_tracking_policy
1340 ret
= cmd_process_attr_tracker_get_tracking_policy(
1341 cmd_ctx
->session
, domain_type
, process_attr
,
1343 if (ret
!= LTTNG_OK
) {
1347 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1348 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1350 ret
= LTTNG_ERR_NOMEM
;
1356 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1358 const enum lttng_tracking_policy tracking_policy
=
1359 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1360 .process_attr_tracker_set_tracking_policy
1362 const enum lttng_domain_type domain_type
=
1363 (enum lttng_domain_type
)
1364 cmd_ctx
->lsm
.domain
.type
;
1365 const enum lttng_process_attr process_attr
=
1366 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1367 .process_attr_tracker_set_tracking_policy
1370 ret
= cmd_process_attr_tracker_set_tracking_policy(
1371 cmd_ctx
->session
, domain_type
, process_attr
,
1373 if (ret
!= LTTNG_OK
) {
1378 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1380 struct lttng_process_attr_values
*values
;
1381 struct lttng_dynamic_buffer reply
;
1382 const enum lttng_domain_type domain_type
=
1383 (enum lttng_domain_type
)
1384 cmd_ctx
->lsm
.domain
.type
;
1385 const enum lttng_process_attr process_attr
=
1386 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1387 .process_attr_tracker_get_inclusion_set
1390 ret
= cmd_process_attr_tracker_get_inclusion_set(
1391 cmd_ctx
->session
, domain_type
, process_attr
,
1393 if (ret
!= LTTNG_OK
) {
1397 lttng_dynamic_buffer_init(&reply
);
1398 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1400 goto error_tracker_get_inclusion_set
;
1403 ret
= setup_lttng_msg_no_cmd_header(
1404 cmd_ctx
, reply
.data
, reply
.size
);
1406 ret
= LTTNG_ERR_NOMEM
;
1407 goto error_tracker_get_inclusion_set
;
1411 error_tracker_get_inclusion_set
:
1412 lttng_process_attr_values_destroy(values
);
1413 lttng_dynamic_buffer_reset(&reply
);
1416 case LTTNG_ENABLE_EVENT
:
1418 struct lttng_event
*ev
= NULL
;
1419 struct lttng_event_exclusion
*exclusion
= NULL
;
1420 struct lttng_filter_bytecode
*bytecode
= NULL
;
1421 char *filter_expression
= NULL
;
1423 /* Handle exclusion events and receive it from the client. */
1424 if (cmd_ctx
->lsm
.u
.enable
.exclusion_count
> 0) {
1425 size_t count
= cmd_ctx
->lsm
.u
.enable
.exclusion_count
;
1427 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1428 (count
* LTTNG_SYMBOL_NAME_LEN
));
1430 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1434 DBG("Receiving var len exclusion event list from client ...");
1435 exclusion
->count
= count
;
1436 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1437 count
* LTTNG_SYMBOL_NAME_LEN
);
1439 DBG("Nothing recv() from client var len data... continuing");
1442 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1447 /* Get filter expression from client. */
1448 if (cmd_ctx
->lsm
.u
.enable
.expression_len
> 0) {
1449 size_t expression_len
=
1450 cmd_ctx
->lsm
.u
.enable
.expression_len
;
1452 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1453 ret
= LTTNG_ERR_FILTER_INVAL
;
1458 filter_expression
= zmalloc(expression_len
);
1459 if (!filter_expression
) {
1461 ret
= LTTNG_ERR_FILTER_NOMEM
;
1465 /* Receive var. len. data */
1466 DBG("Receiving var len filter's expression from client ...");
1467 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1470 DBG("Nothing recv() from client var len data... continuing");
1472 free(filter_expression
);
1474 ret
= LTTNG_ERR_FILTER_INVAL
;
1479 /* Handle filter and get bytecode from client. */
1480 if (cmd_ctx
->lsm
.u
.enable
.bytecode_len
> 0) {
1481 size_t bytecode_len
= cmd_ctx
->lsm
.u
.enable
.bytecode_len
;
1483 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1484 ret
= LTTNG_ERR_FILTER_INVAL
;
1485 free(filter_expression
);
1490 bytecode
= zmalloc(bytecode_len
);
1492 free(filter_expression
);
1494 ret
= LTTNG_ERR_FILTER_NOMEM
;
1498 /* Receive var. len. data */
1499 DBG("Receiving var len filter's bytecode from client ...");
1500 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1502 DBG("Nothing recv() from client var len data... continuing");
1504 free(filter_expression
);
1507 ret
= LTTNG_ERR_FILTER_INVAL
;
1511 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1512 free(filter_expression
);
1515 ret
= LTTNG_ERR_FILTER_INVAL
;
1520 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.enable
.event
));
1522 DBG("Failed to copy event: %s",
1523 cmd_ctx
->lsm
.u
.enable
.event
.name
);
1524 free(filter_expression
);
1527 ret
= LTTNG_ERR_NOMEM
;
1532 if (cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
> 0) {
1533 /* Expect a userspace probe description. */
1534 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1536 free(filter_expression
);
1539 lttng_event_destroy(ev
);
1544 ret
= cmd_enable_event(cmd_ctx
->session
,
1545 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1546 cmd_ctx
->lsm
.u
.enable
.channel_name
,
1548 filter_expression
, bytecode
, exclusion
,
1549 kernel_poll_pipe
[1]);
1550 lttng_event_destroy(ev
);
1553 case LTTNG_LIST_TRACEPOINTS
:
1555 struct lttng_event
*events
;
1558 session_lock_list();
1559 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &events
);
1560 session_unlock_list();
1561 if (nb_events
< 0) {
1562 /* Return value is a negative lttng_error_code. */
1568 * Setup lttng message with payload size set to the event list size in
1569 * bytes and then copy list into the llm payload.
1571 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1572 sizeof(struct lttng_event
) * nb_events
);
1582 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1584 struct lttng_event_field
*fields
;
1587 session_lock_list();
1588 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1590 session_unlock_list();
1591 if (nb_fields
< 0) {
1592 /* Return value is a negative lttng_error_code. */
1598 * Setup lttng message with payload size set to the event list size in
1599 * bytes and then copy list into the llm payload.
1601 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1602 sizeof(struct lttng_event_field
) * nb_fields
);
1612 case LTTNG_LIST_SYSCALLS
:
1614 struct lttng_event
*events
;
1617 nb_events
= cmd_list_syscalls(&events
);
1618 if (nb_events
< 0) {
1619 /* Return value is a negative lttng_error_code. */
1625 * Setup lttng message with payload size set to the event list size in
1626 * bytes and then copy list into the llm payload.
1628 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1629 sizeof(struct lttng_event
) * nb_events
);
1639 case LTTNG_SET_CONSUMER_URI
:
1642 struct lttng_uri
*uris
;
1644 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1645 len
= nb_uri
* sizeof(struct lttng_uri
);
1648 ret
= LTTNG_ERR_INVALID
;
1652 uris
= zmalloc(len
);
1654 ret
= LTTNG_ERR_FATAL
;
1658 /* Receive variable len data */
1659 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1660 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1662 DBG("No URIs received from client... continuing");
1664 ret
= LTTNG_ERR_SESSION_FAIL
;
1669 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1671 if (ret
!= LTTNG_OK
) {
1678 case LTTNG_START_TRACE
:
1681 * On the first start, if we have a kernel session and we have
1682 * enabled time or size-based rotations, we have to make sure
1683 * the kernel tracer supports it.
1685 if (!cmd_ctx
->session
->has_been_started
&& \
1686 cmd_ctx
->session
->kernel_session
&& \
1687 (cmd_ctx
->session
->rotate_timer_period
|| \
1688 cmd_ctx
->session
->rotate_size
) && \
1689 !check_rotate_compatible()) {
1690 DBG("Kernel tracer version is not compatible with the rotation feature");
1691 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1694 ret
= cmd_start_trace(cmd_ctx
->session
);
1697 case LTTNG_STOP_TRACE
:
1699 ret
= cmd_stop_trace(cmd_ctx
->session
);
1702 case LTTNG_DESTROY_SESSION
:
1704 ret
= cmd_destroy_session(cmd_ctx
->session
,
1705 notification_thread_handle
,
1709 case LTTNG_LIST_DOMAINS
:
1712 struct lttng_domain
*domains
= NULL
;
1714 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1716 /* Return value is a negative lttng_error_code. */
1721 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1722 nb_dom
* sizeof(struct lttng_domain
));
1732 case LTTNG_LIST_CHANNELS
:
1734 ssize_t payload_size
;
1735 struct lttng_channel
*channels
= NULL
;
1737 payload_size
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1738 cmd_ctx
->session
, &channels
);
1739 if (payload_size
< 0) {
1740 /* Return value is a negative lttng_error_code. */
1741 ret
= -payload_size
;
1745 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1756 case LTTNG_LIST_EVENTS
:
1759 struct lttcomm_event_command_header cmd_header
= {};
1760 size_t original_payload_size
;
1761 size_t payload_size
;
1763 ret
= setup_empty_lttng_msg(cmd_ctx
);
1765 ret
= LTTNG_ERR_NOMEM
;
1769 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1771 /* Extended infos are included at the end of the payload. */
1772 list_ret
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1774 cmd_ctx
->lsm
.u
.list
.channel_name
,
1775 &cmd_ctx
->reply_payload
);
1777 /* Return value is a negative lttng_error_code. */
1782 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1783 sizeof(cmd_header
) - original_payload_size
;
1784 update_lttng_msg(cmd_ctx
, sizeof(cmd_header
), payload_size
);
1789 case LTTNG_LIST_SESSIONS
:
1791 unsigned int nr_sessions
;
1792 void *sessions_payload
;
1795 session_lock_list();
1796 nr_sessions
= lttng_sessions_count(
1797 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1798 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1800 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1801 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1802 sessions_payload
= zmalloc(payload_len
);
1804 if (!sessions_payload
) {
1805 session_unlock_list();
1810 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1811 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1812 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1813 session_unlock_list();
1815 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1817 free(sessions_payload
);
1826 case LTTNG_REGISTER_CONSUMER
:
1828 struct consumer_data
*cdata
;
1830 switch (cmd_ctx
->lsm
.domain
.type
) {
1831 case LTTNG_DOMAIN_KERNEL
:
1832 cdata
= &kconsumer_data
;
1835 ret
= LTTNG_ERR_UND
;
1839 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1840 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1843 case LTTNG_DATA_PENDING
:
1846 uint8_t pending_ret_byte
;
1848 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1853 * This function may returns 0 or 1 to indicate whether or not
1854 * there is data pending. In case of error, it should return an
1855 * LTTNG_ERR code. However, some code paths may still return
1856 * a nondescript error code, which we handle by returning an
1859 if (pending_ret
== 0 || pending_ret
== 1) {
1861 * ret will be set to LTTNG_OK at the end of
1864 } else if (pending_ret
< 0) {
1865 ret
= LTTNG_ERR_UNK
;
1872 pending_ret_byte
= (uint8_t) pending_ret
;
1874 /* 1 byte to return whether or not data is pending */
1875 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1876 &pending_ret_byte
, 1);
1885 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1887 uint32_t snapshot_id
;
1888 struct lttcomm_lttng_output_id reply
;
1890 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1891 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
),
1893 if (ret
!= LTTNG_OK
) {
1896 reply
.id
= snapshot_id
;
1898 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1904 /* Copy output list into message payload */
1908 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1910 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1911 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
));
1914 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1917 struct lttng_snapshot_output
*outputs
= NULL
;
1919 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1920 if (nb_output
< 0) {
1925 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1926 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1927 nb_output
* sizeof(struct lttng_snapshot_output
));
1937 case LTTNG_SNAPSHOT_RECORD
:
1939 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1940 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_record
.output
),
1941 cmd_ctx
->lsm
.u
.snapshot_record
.wait
);
1944 case LTTNG_CREATE_SESSION_EXT
:
1946 struct lttng_dynamic_buffer payload
;
1947 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1949 lttng_dynamic_buffer_init(&payload
);
1950 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1951 if (ret
!= LTTNG_OK
) {
1955 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1958 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1959 lttng_session_descriptor_destroy(return_descriptor
);
1960 ret
= LTTNG_ERR_NOMEM
;
1963 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1966 lttng_session_descriptor_destroy(return_descriptor
);
1967 ret
= LTTNG_ERR_NOMEM
;
1970 lttng_dynamic_buffer_reset(&payload
);
1971 lttng_session_descriptor_destroy(return_descriptor
);
1975 case LTTNG_SAVE_SESSION
:
1977 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
1981 case LTTNG_SET_SESSION_SHM_PATH
:
1983 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1984 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
1987 case LTTNG_REGENERATE_METADATA
:
1989 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1992 case LTTNG_REGENERATE_STATEDUMP
:
1994 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1997 case LTTNG_REGISTER_TRIGGER
:
1999 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
2000 notification_thread_handle
);
2003 case LTTNG_UNREGISTER_TRIGGER
:
2005 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
2006 notification_thread_handle
);
2009 case LTTNG_ROTATE_SESSION
:
2011 struct lttng_rotate_session_return rotate_return
;
2013 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2015 memset(&rotate_return
, 0, sizeof(rotate_return
));
2016 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2017 DBG("Kernel tracer version is not compatible with the rotation feature");
2018 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2022 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2024 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2030 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2031 sizeof(rotate_return
));
2040 case LTTNG_ROTATION_GET_INFO
:
2042 struct lttng_rotation_get_info_return get_info_return
;
2044 memset(&get_info_return
, 0, sizeof(get_info_return
));
2045 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2046 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2052 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2053 sizeof(get_info_return
));
2062 case LTTNG_ROTATION_SET_SCHEDULE
:
2065 enum lttng_rotation_schedule_type schedule_type
;
2068 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2069 DBG("Kernel tracer version does not support session rotations");
2070 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2074 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2075 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2076 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2078 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
2082 notification_thread_handle
);
2083 if (ret
!= LTTNG_OK
) {
2089 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2091 struct lttng_session_list_schedules_return schedules
= {
2092 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2093 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2094 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2095 .size
.value
= cmd_ctx
->session
->rotate_size
,
2098 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2108 case LTTNG_CLEAR_SESSION
:
2110 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2114 ret
= LTTNG_ERR_UND
;
2119 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2120 DBG("Missing llm header, creating one.");
2121 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2125 /* Set return code */
2126 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2128 if (cmd_ctx
->session
) {
2129 session_unlock(cmd_ctx
->session
);
2130 session_put(cmd_ctx
->session
);
2131 cmd_ctx
->session
= NULL
;
2133 if (need_tracing_session
) {
2134 session_unlock_list();
2137 assert(!rcu_read_ongoing());
2141 static int create_client_sock(void)
2143 int ret
, client_sock
;
2144 const mode_t old_umask
= umask(0);
2146 /* Create client tool unix socket */
2147 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2148 if (client_sock
< 0) {
2149 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2154 /* Set the cloexec flag */
2155 ret
= utils_set_fd_cloexec(client_sock
);
2157 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2158 "Continuing but note that the consumer daemon will have a "
2159 "reference to this socket on exec()", client_sock
);
2162 /* File permission MUST be 660 */
2163 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2165 ERR("Set file permissions failed: %s",
2166 config
.client_unix_sock_path
.value
);
2168 (void) lttcomm_close_unix_sock(client_sock
);
2172 DBG("Created client socket (fd = %i)", client_sock
);
2179 static void cleanup_client_thread(void *data
)
2181 struct lttng_pipe
*quit_pipe
= data
;
2183 lttng_pipe_destroy(quit_pipe
);
2186 static void thread_init_cleanup(void *data
)
2188 set_thread_status(false);
2192 * This thread manage all clients request using the unix client socket for
2195 static void *thread_manage_clients(void *data
)
2197 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2199 uint32_t revents
, nb_fd
;
2200 struct lttng_poll_event events
;
2201 const int client_sock
= thread_state
.client_sock
;
2202 struct lttng_pipe
*quit_pipe
= data
;
2203 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2204 struct command_ctx cmd_ctx
= {};
2206 DBG("[thread] Manage client started");
2208 lttng_payload_init(&cmd_ctx
.reply_payload
);
2210 is_root
= (getuid() == 0);
2212 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2214 rcu_register_thread();
2216 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2218 health_code_update();
2220 ret
= lttcomm_listen_unix_sock(client_sock
);
2226 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2227 * more will be added to this poll set.
2229 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2231 goto error_create_poll
;
2234 /* Add the application registration socket */
2235 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2240 /* Add thread quit pipe */
2241 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2246 /* Set state as running. */
2247 set_thread_status(true);
2248 pthread_cleanup_pop(0);
2250 /* This testpoint is after we signal readiness to the parent. */
2251 if (testpoint(sessiond_thread_manage_clients
)) {
2255 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2259 health_code_update();
2262 const struct cmd_completion_handler
*cmd_completion_handler
;
2264 cmd_ctx
.creds
= (lttng_sock_cred
) {
2268 cmd_ctx
.session
= NULL
;
2269 lttng_dynamic_buffer_set_size(&cmd_ctx
.reply_payload
.buffer
, 0);
2270 lttng_dynamic_array_clear(&cmd_ctx
.reply_payload
._fds
);
2271 cmd_ctx
.lttng_msg_size
= 0;
2273 DBG("Accepting client command ...");
2275 /* Inifinite blocking call, waiting for transmission */
2277 health_poll_entry();
2278 ret
= lttng_poll_wait(&events
, -1);
2282 * Restart interrupted system call.
2284 if (errno
== EINTR
) {
2292 for (i
= 0; i
< nb_fd
; i
++) {
2293 revents
= LTTNG_POLL_GETEV(&events
, i
);
2294 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2296 health_code_update();
2298 if (pollfd
== thread_quit_pipe_fd
) {
2302 /* Event on the registration socket */
2303 if (revents
& LPOLLIN
) {
2305 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2306 ERR("Client socket poll error");
2309 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2315 DBG("Wait for client response");
2317 health_code_update();
2319 sock
= lttcomm_accept_unix_sock(client_sock
);
2325 * Set the CLOEXEC flag. Return code is useless because either way, the
2328 (void) utils_set_fd_cloexec(sock
);
2330 /* Set socket option for credentials retrieval */
2331 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2336 health_code_update();
2339 * Data is received from the lttng client. The struct
2340 * lttcomm_session_msg (lsm) contains the command and data request of
2343 DBG("Receiving data from client ...");
2344 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2345 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2346 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2347 DBG("Incomplete recv() from client... continuing");
2356 health_code_update();
2358 // TODO: Validate cmd_ctx including sanity check for
2359 // security purpose.
2361 rcu_thread_online();
2363 * This function dispatch the work to the kernel or userspace tracer
2364 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2365 * informations for the client. The command context struct contains
2366 * everything this function may needs.
2368 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2369 rcu_thread_offline();
2379 * TODO: Inform client somehow of the fatal error. At
2380 * this point, ret < 0 means that a zmalloc failed
2381 * (ENOMEM). Error detected but still accept
2382 * command, unless a socket error has been
2388 cmd_completion_handler
= cmd_pop_completion_handler();
2389 if (cmd_completion_handler
) {
2390 enum lttng_error_code completion_code
;
2392 completion_code
= cmd_completion_handler
->run(
2393 cmd_completion_handler
->data
);
2394 if (completion_code
!= LTTNG_OK
) {
2399 health_code_update();
2402 struct lttng_payload_view view
=
2403 lttng_payload_view_from_payload(
2404 &cmd_ctx
.reply_payload
,
2406 struct lttcomm_lttng_msg
*llm
= (typeof(
2407 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2409 assert(cmd_ctx
.reply_payload
.buffer
.size
>=
2411 assert(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2413 llm
->fd_count
= lttng_payload_view_get_fd_count(&view
);
2415 DBG("Sending response (size: %d, retcode: %s (%d))",
2416 cmd_ctx
.lttng_msg_size
,
2417 lttng_strerror(-llm
->ret_code
),
2419 ret
= send_unix_sock(sock
, &view
);
2421 ERR("Failed to send data back to client");
2424 /* End of transmission */
2432 health_code_update();
2444 lttng_poll_clean(&events
);
2448 unlink(config
.client_unix_sock_path
.value
);
2449 ret
= close(client_sock
);
2456 ERR("Health error occurred in %s", __func__
);
2459 health_unregister(health_sessiond
);
2461 DBG("Client thread dying");
2462 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2463 rcu_unregister_thread();
2468 bool shutdown_client_thread(void *thread_data
)
2470 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2471 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2473 return notify_thread_pipe(write_fd
) == 1;
2476 struct lttng_thread
*launch_client_thread(void)
2478 bool thread_running
;
2479 struct lttng_pipe
*client_quit_pipe
;
2480 struct lttng_thread
*thread
= NULL
;
2481 int client_sock_fd
= -1;
2483 sem_init(&thread_state
.ready
, 0, 0);
2484 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2485 if (!client_quit_pipe
) {
2489 client_sock_fd
= create_client_sock();
2490 if (client_sock_fd
< 0) {
2494 thread_state
.client_sock
= client_sock_fd
;
2495 thread
= lttng_thread_create("Client management",
2496 thread_manage_clients
,
2497 shutdown_client_thread
,
2498 cleanup_client_thread
,
2503 /* The client thread now owns the client sock fd and the quit pipe. */
2504 client_sock_fd
= -1;
2505 client_quit_pipe
= NULL
;
2508 * This thread is part of the threads that need to be fully
2509 * initialized before the session daemon is marked as "ready".
2511 thread_running
= wait_thread_status();
2512 if (!thread_running
) {
2517 if (client_sock_fd
>= 0) {
2518 if (close(client_sock_fd
)) {
2519 PERROR("Failed to close client socket");
2522 lttng_thread_put(thread
);
2523 cleanup_client_thread(client_quit_pipe
);