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/dynamic-buffer.h"
12 #include "common/sessiond-comm/sessiond-comm.h"
13 #include "lttng/lttng-error.h"
14 #include "lttng/tracker.h"
15 #include <common/compat/getenv.h>
16 #include <common/tracker.h>
17 #include <common/unix.h>
18 #include <common/utils.h>
19 #include <lttng/event-internal.h>
20 #include <lttng/session-descriptor-internal.h>
21 #include <lttng/session-internal.h>
22 #include <lttng/userspace-probe-internal.h>
29 #include "lttng-sessiond.h"
33 #include "health-sessiond.h"
34 #include "testpoint.h"
36 #include "manage-consumer.h"
41 static struct thread_state
{
47 static void set_thread_status(bool running
)
49 DBG("Marking client thread's state as %s", running
? "running" : "error");
50 thread_state
.running
= running
;
51 sem_post(&thread_state
.ready
);
54 static bool wait_thread_status(void)
56 DBG("Waiting for client thread to be ready");
57 sem_wait(&thread_state
.ready
);
58 if (thread_state
.running
) {
59 DBG("Client thread is ready");
61 ERR("Initialization of client thread failed");
64 return thread_state
.running
;
68 * Setup the outgoing data buffer for the response (llm) by allocating the
69 * right amount of memory and copying the original information from the lsm
72 * Return 0 on success, negative value on error.
74 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
75 const void *payload_buf
, size_t payload_len
,
76 const void *cmd_header_buf
, size_t cmd_header_len
)
79 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
80 const size_t cmd_header_offset
= header_len
;
81 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
82 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
85 cmd_ctx
->llm
= zmalloc(total_msg_size
);
87 if (cmd_ctx
->llm
== NULL
) {
93 /* Copy common data */
94 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
95 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
96 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
97 cmd_ctx
->llm
->data_size
= payload_len
;
98 cmd_ctx
->lttng_msg_size
= total_msg_size
;
100 /* Copy command header */
101 if (cmd_header_len
) {
102 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
108 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
117 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
118 * exec or it will fail.
120 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
122 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
126 * Fork and exec a consumer daemon (consumerd).
128 * Return pid if successful else -1.
130 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
134 const char *consumer_to_use
;
135 const char *verbosity
;
138 DBG("Spawning consumerd");
145 if (config
.verbose_consumer
) {
146 verbosity
= "--verbose";
147 } else if (lttng_opt_quiet
) {
148 verbosity
= "--quiet";
153 switch (consumer_data
->type
) {
154 case LTTNG_CONSUMER_KERNEL
:
156 * Find out which consumerd to execute. We will first try the
157 * 64-bit path, then the sessiond's installation directory, and
158 * fallback on the 32-bit one,
160 DBG3("Looking for a kernel consumer at these locations:");
161 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
162 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
163 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
164 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
165 DBG3("Found location #1");
166 consumer_to_use
= config
.consumerd64_bin_path
.value
;
167 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
168 DBG3("Found location #2");
169 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
170 } else if (config
.consumerd32_bin_path
.value
&&
171 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
172 DBG3("Found location #3");
173 consumer_to_use
= config
.consumerd32_bin_path
.value
;
175 DBG("Could not find any valid consumerd executable");
179 DBG("Using kernel consumer at: %s", consumer_to_use
);
180 (void) execl(consumer_to_use
,
181 "lttng-consumerd", verbosity
, "-k",
182 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
183 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
184 "--group", config
.tracing_group_name
.value
,
187 case LTTNG_CONSUMER64_UST
:
189 if (config
.consumerd64_lib_dir
.value
) {
194 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
198 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
199 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
204 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
205 if (tmp
[0] != '\0') {
209 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
216 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
217 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
218 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
219 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
220 "--group", config
.tracing_group_name
.value
,
224 case LTTNG_CONSUMER32_UST
:
226 if (config
.consumerd32_lib_dir
.value
) {
231 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
235 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
236 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
241 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
242 if (tmp
[0] != '\0') {
246 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
253 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
254 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
255 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
256 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
257 "--group", config
.tracing_group_name
.value
,
262 ERR("unknown consumer type");
266 PERROR("Consumer execl()");
268 /* Reaching this point, we got a failure on our execl(). */
270 } else if (pid
> 0) {
273 PERROR("start consumer fork");
281 * Spawn the consumerd daemon and session daemon thread.
283 static int start_consumerd(struct consumer_data
*consumer_data
)
288 * Set the listen() state on the socket since there is a possible race
289 * between the exec() of the consumer daemon and this call if place in the
290 * consumer thread. See bug #366 for more details.
292 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
297 pthread_mutex_lock(&consumer_data
->pid_mutex
);
298 if (consumer_data
->pid
!= 0) {
299 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
303 ret
= spawn_consumerd(consumer_data
);
305 ERR("Spawning consumerd failed");
306 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
310 /* Setting up the consumer_data pid */
311 consumer_data
->pid
= ret
;
312 DBG2("Consumer pid %d", consumer_data
->pid
);
313 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
315 DBG2("Spawning consumer control thread");
316 ret
= spawn_consumer_thread(consumer_data
);
318 ERR("Fatal error spawning consumer control thread");
326 /* Cleanup already created sockets on error. */
327 if (consumer_data
->err_sock
>= 0) {
330 err
= close(consumer_data
->err_sock
);
332 PERROR("close consumer data error socket");
339 * Copy consumer output from the tracing session to the domain session. The
340 * function also applies the right modification on a per domain basis for the
341 * trace files destination directory.
343 * Should *NOT* be called with RCU read-side lock held.
345 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
348 const char *dir_name
;
349 struct consumer_output
*consumer
;
352 assert(session
->consumer
);
355 case LTTNG_DOMAIN_KERNEL
:
356 DBG3("Copying tracing session consumer output in kernel session");
358 * XXX: We should audit the session creation and what this function
359 * does "extra" in order to avoid a destroy since this function is used
360 * in the domain session creation (kernel and ust) only. Same for UST
363 if (session
->kernel_session
->consumer
) {
364 consumer_output_put(session
->kernel_session
->consumer
);
366 session
->kernel_session
->consumer
=
367 consumer_copy_output(session
->consumer
);
368 /* Ease our life a bit for the next part */
369 consumer
= session
->kernel_session
->consumer
;
370 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
372 case LTTNG_DOMAIN_JUL
:
373 case LTTNG_DOMAIN_LOG4J
:
374 case LTTNG_DOMAIN_PYTHON
:
375 case LTTNG_DOMAIN_UST
:
376 DBG3("Copying tracing session consumer output in UST session");
377 if (session
->ust_session
->consumer
) {
378 consumer_output_put(session
->ust_session
->consumer
);
380 session
->ust_session
->consumer
=
381 consumer_copy_output(session
->consumer
);
382 /* Ease our life a bit for the next part */
383 consumer
= session
->ust_session
->consumer
;
384 dir_name
= DEFAULT_UST_TRACE_DIR
;
387 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
391 /* Append correct directory to subdir */
392 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
393 sizeof(consumer
->domain_subdir
));
398 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
406 * Create an UST session and add it to the session ust list.
408 * Should *NOT* be called with RCU read-side lock held.
410 static int create_ust_session(struct ltt_session
*session
,
411 const struct lttng_domain
*domain
)
414 struct ltt_ust_session
*lus
= NULL
;
418 assert(session
->consumer
);
420 switch (domain
->type
) {
421 case LTTNG_DOMAIN_JUL
:
422 case LTTNG_DOMAIN_LOG4J
:
423 case LTTNG_DOMAIN_PYTHON
:
424 case LTTNG_DOMAIN_UST
:
427 ERR("Unknown UST domain on create session %d", domain
->type
);
428 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
432 DBG("Creating UST session");
434 lus
= trace_ust_create_session(session
->id
);
436 ret
= LTTNG_ERR_UST_SESS_FAIL
;
440 lus
->uid
= session
->uid
;
441 lus
->gid
= session
->gid
;
442 lus
->output_traces
= session
->output_traces
;
443 lus
->snapshot_mode
= session
->snapshot_mode
;
444 lus
->live_timer_interval
= session
->live_timer
;
445 session
->ust_session
= lus
;
446 if (session
->shm_path
[0]) {
447 strncpy(lus
->root_shm_path
, session
->shm_path
,
448 sizeof(lus
->root_shm_path
));
449 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
450 strncpy(lus
->shm_path
, session
->shm_path
,
451 sizeof(lus
->shm_path
));
452 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
453 strncat(lus
->shm_path
, "/ust",
454 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
456 /* Copy session output to the newly created UST session */
457 ret
= copy_session_consumer(domain
->type
, session
);
458 if (ret
!= LTTNG_OK
) {
466 session
->ust_session
= NULL
;
471 * Create a kernel tracer session then create the default channel.
473 static int create_kernel_session(struct ltt_session
*session
)
477 DBG("Creating kernel session");
479 ret
= kernel_create_session(session
);
481 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
485 /* Code flow safety */
486 assert(session
->kernel_session
);
488 /* Copy session output to the newly created Kernel session */
489 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
490 if (ret
!= LTTNG_OK
) {
494 session
->kernel_session
->uid
= session
->uid
;
495 session
->kernel_session
->gid
= session
->gid
;
496 session
->kernel_session
->output_traces
= session
->output_traces
;
497 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
502 trace_kernel_destroy_session(session
->kernel_session
);
503 session
->kernel_session
= NULL
;
509 * Count number of session permitted by uid/gid.
511 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
514 struct ltt_session
*session
;
515 const struct ltt_session_list
*session_list
= session_get_list();
517 DBG("Counting number of available session for UID %d GID %d",
519 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
520 if (!session_get(session
)) {
523 session_lock(session
);
524 /* Only count the sessions the user can control. */
525 if (session_access_ok(session
, uid
, gid
) &&
526 !session
->destroyed
) {
529 session_unlock(session
);
530 session_put(session
);
535 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
536 int *sock_error
, struct lttng_event
*event
)
539 struct lttng_userspace_probe_location
*probe_location
;
540 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
541 struct lttng_dynamic_buffer probe_location_buffer
;
542 struct lttng_buffer_view buffer_view
;
545 * Create a buffer to store the serialized version of the probe
548 lttng_dynamic_buffer_init(&probe_location_buffer
);
549 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
550 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
552 ret
= LTTNG_ERR_NOMEM
;
557 * Receive the probe location.
559 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
560 probe_location_buffer
.size
);
562 DBG("Nothing recv() from client var len data... continuing");
564 lttng_dynamic_buffer_reset(&probe_location_buffer
);
565 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
569 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
570 &probe_location_buffer
, 0, probe_location_buffer
.size
);
573 * Extract the probe location from the serialized version.
575 ret
= lttng_userspace_probe_location_create_from_buffer(
576 &buffer_view
, &probe_location
);
578 WARN("Failed to create a userspace probe location from the received buffer");
579 lttng_dynamic_buffer_reset( &probe_location_buffer
);
580 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
585 * Receive the file descriptor to the target binary from the client.
587 DBG("Receiving userspace probe target FD from client ...");
588 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
590 DBG("Nothing recv() from client userspace probe fd... continuing");
592 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
597 * Set the file descriptor received from the client through the unix
598 * socket in the probe location.
600 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
602 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
607 * From the kernel tracer's perspective, all userspace probe event types
608 * are all the same: a file and an offset.
610 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
611 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
612 ret
= lttng_userspace_probe_location_function_set_binary_fd(
615 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
616 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
620 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
625 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
629 /* Attach the probe location to the event. */
630 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
632 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
636 lttng_dynamic_buffer_reset(&probe_location_buffer
);
642 * Version of setup_lttng_msg() without command header.
644 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
645 void *payload_buf
, size_t payload_len
)
647 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
651 * Free memory of a command context structure.
653 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
655 DBG("Clean command context structure");
657 if ((*cmd_ctx
)->llm
) {
658 free((*cmd_ctx
)->llm
);
660 if ((*cmd_ctx
)->lsm
) {
661 free((*cmd_ctx
)->lsm
);
669 * Check if the current kernel tracer supports the session rotation feature.
670 * Return 1 if it does, 0 otherwise.
672 static int check_rotate_compatible(void)
676 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
677 DBG("Kernel tracer version is not compatible with the rotation feature");
685 * Send data on a unix socket using the liblttsessiondcomm API.
687 * Return lttcomm error code.
689 static int send_unix_sock(int sock
, void *buf
, size_t len
)
691 /* Check valid length */
696 return lttcomm_send_unix_sock(sock
, buf
, len
);
700 * Process the command requested by the lttng client within the command
701 * context structure. This function make sure that the return structure (llm)
702 * is set and ready for transmission before returning.
704 * Return any error encountered or 0 for success.
706 * "sock" is only used for special-case var. len data.
707 * A command may assume the ownership of the socket, in which case its value
708 * should be set to -1.
710 * Should *NOT* be called with RCU read-side lock held.
712 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
716 int need_tracing_session
= 1;
719 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
721 assert(!rcu_read_ongoing());
725 switch (cmd_ctx
->lsm
->cmd_type
) {
726 case LTTNG_CREATE_SESSION_EXT
:
727 case LTTNG_DESTROY_SESSION
:
728 case LTTNG_LIST_SESSIONS
:
729 case LTTNG_LIST_DOMAINS
:
730 case LTTNG_START_TRACE
:
731 case LTTNG_STOP_TRACE
:
732 case LTTNG_DATA_PENDING
:
733 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
734 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
735 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
736 case LTTNG_SNAPSHOT_RECORD
:
737 case LTTNG_SAVE_SESSION
:
738 case LTTNG_SET_SESSION_SHM_PATH
:
739 case LTTNG_REGENERATE_METADATA
:
740 case LTTNG_REGENERATE_STATEDUMP
:
741 case LTTNG_REGISTER_TRIGGER
:
742 case LTTNG_UNREGISTER_TRIGGER
:
743 case LTTNG_ROTATE_SESSION
:
744 case LTTNG_ROTATION_GET_INFO
:
745 case LTTNG_ROTATION_SET_SCHEDULE
:
746 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
747 case LTTNG_CLEAR_SESSION
:
754 if (config
.no_kernel
&& need_domain
755 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
757 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
759 ret
= LTTNG_ERR_KERN_NA
;
764 /* Deny register consumer if we already have a spawned consumer. */
765 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
766 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
767 if (kconsumer_data
.pid
> 0) {
768 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
769 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
772 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
776 * Check for command that don't needs to allocate a returned payload. We do
777 * this here so we don't have to make the call for no payload at each
780 switch(cmd_ctx
->lsm
->cmd_type
) {
781 case LTTNG_LIST_SESSIONS
:
782 case LTTNG_LIST_TRACEPOINTS
:
783 case LTTNG_LIST_TRACEPOINT_FIELDS
:
784 case LTTNG_LIST_DOMAINS
:
785 case LTTNG_LIST_CHANNELS
:
786 case LTTNG_LIST_EVENTS
:
787 case LTTNG_LIST_SYSCALLS
:
788 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
789 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
790 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
791 case LTTNG_DATA_PENDING
:
792 case LTTNG_ROTATE_SESSION
:
793 case LTTNG_ROTATION_GET_INFO
:
796 /* Setup lttng message with no payload */
797 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
799 /* This label does not try to unlock the session */
800 goto init_setup_error
;
804 /* Commands that DO NOT need a session. */
805 switch (cmd_ctx
->lsm
->cmd_type
) {
806 case LTTNG_CREATE_SESSION_EXT
:
807 case LTTNG_LIST_SESSIONS
:
808 case LTTNG_LIST_TRACEPOINTS
:
809 case LTTNG_LIST_SYSCALLS
:
810 case LTTNG_LIST_TRACEPOINT_FIELDS
:
811 case LTTNG_SAVE_SESSION
:
812 case LTTNG_REGISTER_TRIGGER
:
813 case LTTNG_UNREGISTER_TRIGGER
:
814 need_tracing_session
= 0;
817 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
819 * We keep the session list lock across _all_ commands
820 * for now, because the per-session lock does not
821 * handle teardown properly.
824 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
825 if (cmd_ctx
->session
== NULL
) {
826 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
829 /* Acquire lock for the session */
830 session_lock(cmd_ctx
->session
);
836 * Commands that need a valid session but should NOT create one if none
837 * exists. Instead of creating one and destroying it when the command is
838 * handled, process that right before so we save some round trip in useless
841 switch (cmd_ctx
->lsm
->cmd_type
) {
842 case LTTNG_DISABLE_CHANNEL
:
843 case LTTNG_DISABLE_EVENT
:
844 switch (cmd_ctx
->lsm
->domain
.type
) {
845 case LTTNG_DOMAIN_KERNEL
:
846 if (!cmd_ctx
->session
->kernel_session
) {
847 ret
= LTTNG_ERR_NO_CHANNEL
;
851 case LTTNG_DOMAIN_JUL
:
852 case LTTNG_DOMAIN_LOG4J
:
853 case LTTNG_DOMAIN_PYTHON
:
854 case LTTNG_DOMAIN_UST
:
855 if (!cmd_ctx
->session
->ust_session
) {
856 ret
= LTTNG_ERR_NO_CHANNEL
;
861 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
873 * Check domain type for specific "pre-action".
875 switch (cmd_ctx
->lsm
->domain
.type
) {
876 case LTTNG_DOMAIN_KERNEL
:
878 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
882 /* Kernel tracer check */
883 if (!kernel_tracer_is_initialized()) {
884 /* Basically, load kernel tracer modules */
885 ret
= init_kernel_tracer();
891 /* Consumer is in an ERROR state. Report back to client */
892 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
893 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
897 /* Need a session for kernel command */
898 if (need_tracing_session
) {
899 if (cmd_ctx
->session
->kernel_session
== NULL
) {
900 ret
= create_kernel_session(cmd_ctx
->session
);
901 if (ret
!= LTTNG_OK
) {
902 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
907 /* Start the kernel consumer daemon */
908 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
909 if (kconsumer_data
.pid
== 0 &&
910 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
911 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
912 ret
= start_consumerd(&kconsumer_data
);
914 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
917 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
919 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
923 * The consumer was just spawned so we need to add the socket to
924 * the consumer output of the session if exist.
926 ret
= consumer_create_socket(&kconsumer_data
,
927 cmd_ctx
->session
->kernel_session
->consumer
);
934 case LTTNG_DOMAIN_JUL
:
935 case LTTNG_DOMAIN_LOG4J
:
936 case LTTNG_DOMAIN_PYTHON
:
937 case LTTNG_DOMAIN_UST
:
939 if (!ust_app_supported()) {
940 ret
= LTTNG_ERR_NO_UST
;
943 /* Consumer is in an ERROR state. Report back to client */
944 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
945 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
949 if (need_tracing_session
) {
950 /* Create UST session if none exist. */
951 if (cmd_ctx
->session
->ust_session
== NULL
) {
952 ret
= create_ust_session(cmd_ctx
->session
,
953 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
));
954 if (ret
!= LTTNG_OK
) {
959 /* Start the UST consumer daemons */
961 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
962 if (config
.consumerd64_bin_path
.value
&&
963 ustconsumer64_data
.pid
== 0 &&
964 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
965 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
966 ret
= start_consumerd(&ustconsumer64_data
);
968 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
969 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
973 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
974 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
976 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
980 * Setup socket for consumer 64 bit. No need for atomic access
981 * since it was set above and can ONLY be set in this thread.
983 ret
= consumer_create_socket(&ustconsumer64_data
,
984 cmd_ctx
->session
->ust_session
->consumer
);
990 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
991 if (config
.consumerd32_bin_path
.value
&&
992 ustconsumer32_data
.pid
== 0 &&
993 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
994 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
995 ret
= start_consumerd(&ustconsumer32_data
);
997 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
998 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
1002 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
1003 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1005 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1009 * Setup socket for consumer 32 bit. No need for atomic access
1010 * since it was set above and can ONLY be set in this thread.
1012 ret
= consumer_create_socket(&ustconsumer32_data
,
1013 cmd_ctx
->session
->ust_session
->consumer
);
1025 /* Validate consumer daemon state when start/stop trace command */
1026 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1027 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1028 switch (cmd_ctx
->lsm
->domain
.type
) {
1029 case LTTNG_DOMAIN_NONE
:
1031 case LTTNG_DOMAIN_JUL
:
1032 case LTTNG_DOMAIN_LOG4J
:
1033 case LTTNG_DOMAIN_PYTHON
:
1034 case LTTNG_DOMAIN_UST
:
1035 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1036 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1040 case LTTNG_DOMAIN_KERNEL
:
1041 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1042 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1047 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1053 * Check that the UID or GID match that of the tracing session.
1054 * The root user can interact with all sessions.
1056 if (need_tracing_session
) {
1057 if (!session_access_ok(cmd_ctx
->session
,
1058 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1059 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1060 cmd_ctx
->session
->destroyed
) {
1061 ret
= LTTNG_ERR_EPERM
;
1067 * Send relayd information to consumer as soon as we have a domain and a
1070 if (cmd_ctx
->session
&& need_domain
) {
1072 * Setup relayd if not done yet. If the relayd information was already
1073 * sent to the consumer, this call will gracefully return.
1075 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1076 if (ret
!= LTTNG_OK
) {
1081 /* Process by command type */
1082 switch (cmd_ctx
->lsm
->cmd_type
) {
1083 case LTTNG_ADD_CONTEXT
:
1086 * An LTTNG_ADD_CONTEXT command might have a supplementary
1087 * payload if the context being added is an application context.
1089 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1090 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1091 char *provider_name
= NULL
, *context_name
= NULL
;
1092 size_t provider_name_len
=
1093 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1094 size_t context_name_len
=
1095 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1097 if (provider_name_len
== 0 || context_name_len
== 0) {
1099 * Application provider and context names MUST
1102 ret
= -LTTNG_ERR_INVALID
;
1106 provider_name
= zmalloc(provider_name_len
+ 1);
1107 if (!provider_name
) {
1108 ret
= -LTTNG_ERR_NOMEM
;
1111 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1114 context_name
= zmalloc(context_name_len
+ 1);
1115 if (!context_name
) {
1116 ret
= -LTTNG_ERR_NOMEM
;
1117 goto error_add_context
;
1119 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1122 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1125 goto error_add_context
;
1128 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1131 goto error_add_context
;
1136 * cmd_add_context assumes ownership of the provider and context
1139 ret
= cmd_add_context(cmd_ctx
->session
,
1140 cmd_ctx
->lsm
->domain
.type
,
1141 cmd_ctx
->lsm
->u
.context
.channel_name
,
1142 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.context
.ctx
),
1143 kernel_poll_pipe
[1]);
1145 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1146 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1148 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1149 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1155 case LTTNG_DISABLE_CHANNEL
:
1157 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1158 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1161 case LTTNG_DISABLE_EVENT
:
1165 * FIXME: handle filter; for now we just receive the filter's
1166 * bytecode along with the filter expression which are sent by
1167 * liblttng-ctl and discard them.
1169 * This fixes an issue where the client may block while sending
1170 * the filter payload and encounter an error because the session
1171 * daemon closes the socket without ever handling this data.
1173 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1174 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1177 char data
[LTTNG_FILTER_MAX_LEN
];
1179 DBG("Discarding disable event command payload of size %zu", count
);
1181 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1182 count
> sizeof(data
) ? sizeof(data
) : count
);
1187 count
-= (size_t) ret
;
1190 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1191 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1192 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.disable
.event
));
1195 case LTTNG_ENABLE_CHANNEL
:
1197 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1198 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1199 ret
= cmd_enable_channel(cmd_ctx
->session
,
1200 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1201 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.channel
.chan
),
1202 kernel_poll_pipe
[1]);
1205 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1206 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1208 struct lttng_dynamic_buffer payload
;
1209 struct lttng_buffer_view payload_view
;
1210 const bool add_value
=
1211 cmd_ctx
->lsm
->cmd_type
==
1212 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1213 const size_t name_len
=
1214 cmd_ctx
->lsm
->u
.process_attr_tracker_add_remove_include_value
1216 const enum lttng_domain_type domain_type
=
1217 (enum lttng_domain_type
)
1218 cmd_ctx
->lsm
->domain
.type
;
1219 const enum lttng_process_attr process_attr
=
1220 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1221 .process_attr_tracker_add_remove_include_value
1223 const enum lttng_process_attr_value_type value_type
=
1224 (enum lttng_process_attr_value_type
) cmd_ctx
1226 .process_attr_tracker_add_remove_include_value
1228 struct process_attr_value
*value
;
1229 enum lttng_error_code ret_code
;
1231 /* Receive remaining variable length payload if applicable. */
1232 if (name_len
> LOGIN_NAME_MAX
) {
1234 * POSIX mandates user and group names that are at least
1235 * 8 characters long. Note that although shadow-utils
1236 * (useradd, groupaadd, etc.) use 32 chars as their
1237 * limit (from bits/utmp.h, UT_NAMESIZE),
1238 * LOGIN_NAME_MAX is defined to 256.
1240 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %d",
1241 add_value
? "addition" : "removal",
1242 name_len
, LOGIN_NAME_MAX
);
1243 ret
= LTTNG_ERR_INVALID
;
1247 lttng_dynamic_buffer_init(&payload
);
1248 if (name_len
!= 0) {
1250 * Receive variable payload for user/group name
1253 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1255 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1256 add_value
? "add" : "remove");
1257 ret
= LTTNG_ERR_NOMEM
;
1258 goto error_add_remove_tracker_value
;
1261 ret
= lttcomm_recv_unix_sock(
1262 *sock
, payload
.data
, name_len
);
1264 ERR("Failed to receive payload of %s process attribute tracker value argument",
1265 add_value
? "add" : "remove");
1267 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1268 goto error_add_remove_tracker_value
;
1272 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1273 &payload
, 0, name_len
);
1275 * Validate the value type and domains are legal for the process
1276 * attribute tracker that is specified and convert the value to
1277 * add/remove to the internal sessiond representation.
1279 ret_code
= process_attr_value_from_comm(domain_type
,
1280 process_attr
, value_type
,
1281 &cmd_ctx
->lsm
->u
.process_attr_tracker_add_remove_include_value
1283 &payload_view
, &value
);
1284 if (ret_code
!= LTTNG_OK
) {
1286 goto error_add_remove_tracker_value
;
1290 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1291 cmd_ctx
->session
, domain_type
,
1292 process_attr
, value
);
1294 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1295 cmd_ctx
->session
, domain_type
,
1296 process_attr
, value
);
1298 process_attr_value_destroy(value
);
1299 error_add_remove_tracker_value
:
1300 lttng_dynamic_buffer_reset(&payload
);
1303 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1305 enum lttng_tracking_policy tracking_policy
;
1306 const enum lttng_domain_type domain_type
=
1307 (enum lttng_domain_type
)
1308 cmd_ctx
->lsm
->domain
.type
;
1309 const enum lttng_process_attr process_attr
=
1310 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1311 .process_attr_tracker_get_tracking_policy
1314 ret
= cmd_process_attr_tracker_get_tracking_policy(
1315 cmd_ctx
->session
, domain_type
, process_attr
,
1317 if (ret
!= LTTNG_OK
) {
1321 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1322 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1324 ret
= LTTNG_ERR_NOMEM
;
1330 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1332 const enum lttng_tracking_policy tracking_policy
=
1333 (enum lttng_tracking_policy
) cmd_ctx
->lsm
->u
1334 .process_attr_tracker_set_tracking_policy
1336 const enum lttng_domain_type domain_type
=
1337 (enum lttng_domain_type
)
1338 cmd_ctx
->lsm
->domain
.type
;
1339 const enum lttng_process_attr process_attr
=
1340 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1341 .process_attr_tracker_set_tracking_policy
1344 ret
= cmd_process_attr_tracker_set_tracking_policy(
1345 cmd_ctx
->session
, domain_type
, process_attr
,
1347 if (ret
!= LTTNG_OK
) {
1352 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1354 struct lttng_process_attr_values
*values
;
1355 struct lttng_dynamic_buffer reply
;
1356 const enum lttng_domain_type domain_type
=
1357 (enum lttng_domain_type
)
1358 cmd_ctx
->lsm
->domain
.type
;
1359 const enum lttng_process_attr process_attr
=
1360 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1361 .process_attr_tracker_get_inclusion_set
1364 ret
= cmd_process_attr_tracker_get_inclusion_set(
1365 cmd_ctx
->session
, domain_type
, process_attr
,
1367 if (ret
!= LTTNG_OK
) {
1371 lttng_dynamic_buffer_init(&reply
);
1372 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1374 goto error_tracker_get_inclusion_set
;
1377 ret
= setup_lttng_msg_no_cmd_header(
1378 cmd_ctx
, reply
.data
, reply
.size
);
1380 ret
= LTTNG_ERR_NOMEM
;
1381 goto error_tracker_get_inclusion_set
;
1385 error_tracker_get_inclusion_set
:
1386 lttng_process_attr_values_destroy(values
);
1387 lttng_dynamic_buffer_reset(&reply
);
1390 case LTTNG_ENABLE_EVENT
:
1392 struct lttng_event
*ev
= NULL
;
1393 struct lttng_event_exclusion
*exclusion
= NULL
;
1394 struct lttng_filter_bytecode
*bytecode
= NULL
;
1395 char *filter_expression
= NULL
;
1397 /* Handle exclusion events and receive it from the client. */
1398 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1399 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1401 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1402 (count
* LTTNG_SYMBOL_NAME_LEN
));
1404 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1408 DBG("Receiving var len exclusion event list from client ...");
1409 exclusion
->count
= count
;
1410 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1411 count
* LTTNG_SYMBOL_NAME_LEN
);
1413 DBG("Nothing recv() from client var len data... continuing");
1416 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1421 /* Get filter expression from client. */
1422 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1423 size_t expression_len
=
1424 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1426 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1427 ret
= LTTNG_ERR_FILTER_INVAL
;
1432 filter_expression
= zmalloc(expression_len
);
1433 if (!filter_expression
) {
1435 ret
= LTTNG_ERR_FILTER_NOMEM
;
1439 /* Receive var. len. data */
1440 DBG("Receiving var len filter's expression from client ...");
1441 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1444 DBG("Nothing recv() from client var len data... continuing");
1446 free(filter_expression
);
1448 ret
= LTTNG_ERR_FILTER_INVAL
;
1453 /* Handle filter and get bytecode from client. */
1454 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1455 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1457 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1458 ret
= LTTNG_ERR_FILTER_INVAL
;
1459 free(filter_expression
);
1464 bytecode
= zmalloc(bytecode_len
);
1466 free(filter_expression
);
1468 ret
= LTTNG_ERR_FILTER_NOMEM
;
1472 /* Receive var. len. data */
1473 DBG("Receiving var len filter's bytecode from client ...");
1474 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1476 DBG("Nothing recv() from client var len data... continuing");
1478 free(filter_expression
);
1481 ret
= LTTNG_ERR_FILTER_INVAL
;
1485 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1486 free(filter_expression
);
1489 ret
= LTTNG_ERR_FILTER_INVAL
;
1494 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.enable
.event
));
1496 DBG("Failed to copy event: %s",
1497 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1498 free(filter_expression
);
1501 ret
= LTTNG_ERR_NOMEM
;
1506 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1507 /* Expect a userspace probe description. */
1508 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1510 free(filter_expression
);
1513 lttng_event_destroy(ev
);
1518 ret
= cmd_enable_event(cmd_ctx
->session
,
1519 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1520 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1522 filter_expression
, bytecode
, exclusion
,
1523 kernel_poll_pipe
[1]);
1524 lttng_event_destroy(ev
);
1527 case LTTNG_LIST_TRACEPOINTS
:
1529 struct lttng_event
*events
;
1532 session_lock_list();
1533 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1534 session_unlock_list();
1535 if (nb_events
< 0) {
1536 /* Return value is a negative lttng_error_code. */
1542 * Setup lttng message with payload size set to the event list size in
1543 * bytes and then copy list into the llm payload.
1545 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1546 sizeof(struct lttng_event
) * nb_events
);
1556 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1558 struct lttng_event_field
*fields
;
1561 session_lock_list();
1562 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1564 session_unlock_list();
1565 if (nb_fields
< 0) {
1566 /* Return value is a negative lttng_error_code. */
1572 * Setup lttng message with payload size set to the event list size in
1573 * bytes and then copy list into the llm payload.
1575 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1576 sizeof(struct lttng_event_field
) * nb_fields
);
1586 case LTTNG_LIST_SYSCALLS
:
1588 struct lttng_event
*events
;
1591 nb_events
= cmd_list_syscalls(&events
);
1592 if (nb_events
< 0) {
1593 /* Return value is a negative lttng_error_code. */
1599 * Setup lttng message with payload size set to the event list size in
1600 * bytes and then copy list into the llm payload.
1602 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1603 sizeof(struct lttng_event
) * nb_events
);
1613 case LTTNG_SET_CONSUMER_URI
:
1616 struct lttng_uri
*uris
;
1618 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1619 len
= nb_uri
* sizeof(struct lttng_uri
);
1622 ret
= LTTNG_ERR_INVALID
;
1626 uris
= zmalloc(len
);
1628 ret
= LTTNG_ERR_FATAL
;
1632 /* Receive variable len data */
1633 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1634 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1636 DBG("No URIs received from client... continuing");
1638 ret
= LTTNG_ERR_SESSION_FAIL
;
1643 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1645 if (ret
!= LTTNG_OK
) {
1652 case LTTNG_START_TRACE
:
1655 * On the first start, if we have a kernel session and we have
1656 * enabled time or size-based rotations, we have to make sure
1657 * the kernel tracer supports it.
1659 if (!cmd_ctx
->session
->has_been_started
&& \
1660 cmd_ctx
->session
->kernel_session
&& \
1661 (cmd_ctx
->session
->rotate_timer_period
|| \
1662 cmd_ctx
->session
->rotate_size
) && \
1663 !check_rotate_compatible()) {
1664 DBG("Kernel tracer version is not compatible with the rotation feature");
1665 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1668 ret
= cmd_start_trace(cmd_ctx
->session
);
1671 case LTTNG_STOP_TRACE
:
1673 ret
= cmd_stop_trace(cmd_ctx
->session
);
1676 case LTTNG_DESTROY_SESSION
:
1678 ret
= cmd_destroy_session(cmd_ctx
->session
,
1679 notification_thread_handle
,
1683 case LTTNG_LIST_DOMAINS
:
1686 struct lttng_domain
*domains
= NULL
;
1688 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1690 /* Return value is a negative lttng_error_code. */
1695 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1696 nb_dom
* sizeof(struct lttng_domain
));
1706 case LTTNG_LIST_CHANNELS
:
1708 ssize_t payload_size
;
1709 struct lttng_channel
*channels
= NULL
;
1711 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1712 cmd_ctx
->session
, &channels
);
1713 if (payload_size
< 0) {
1714 /* Return value is a negative lttng_error_code. */
1715 ret
= -payload_size
;
1719 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1730 case LTTNG_LIST_EVENTS
:
1733 struct lttng_event
*events
= NULL
;
1734 struct lttcomm_event_command_header cmd_header
;
1737 memset(&cmd_header
, 0, sizeof(cmd_header
));
1738 /* Extended infos are included at the end of events */
1739 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1740 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1741 &events
, &total_size
);
1744 /* Return value is a negative lttng_error_code. */
1749 cmd_header
.nb_events
= nb_event
;
1750 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1751 &cmd_header
, sizeof(cmd_header
));
1761 case LTTNG_LIST_SESSIONS
:
1763 unsigned int nr_sessions
;
1764 void *sessions_payload
;
1767 session_lock_list();
1768 nr_sessions
= lttng_sessions_count(
1769 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1770 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1772 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1773 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1774 sessions_payload
= zmalloc(payload_len
);
1776 if (!sessions_payload
) {
1777 session_unlock_list();
1782 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1783 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1784 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1785 session_unlock_list();
1787 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1789 free(sessions_payload
);
1798 case LTTNG_REGISTER_CONSUMER
:
1800 struct consumer_data
*cdata
;
1802 switch (cmd_ctx
->lsm
->domain
.type
) {
1803 case LTTNG_DOMAIN_KERNEL
:
1804 cdata
= &kconsumer_data
;
1807 ret
= LTTNG_ERR_UND
;
1811 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1812 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1815 case LTTNG_DATA_PENDING
:
1818 uint8_t pending_ret_byte
;
1820 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1825 * This function may returns 0 or 1 to indicate whether or not
1826 * there is data pending. In case of error, it should return an
1827 * LTTNG_ERR code. However, some code paths may still return
1828 * a nondescript error code, which we handle by returning an
1831 if (pending_ret
== 0 || pending_ret
== 1) {
1833 * ret will be set to LTTNG_OK at the end of
1836 } else if (pending_ret
< 0) {
1837 ret
= LTTNG_ERR_UNK
;
1844 pending_ret_byte
= (uint8_t) pending_ret
;
1846 /* 1 byte to return whether or not data is pending */
1847 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1848 &pending_ret_byte
, 1);
1857 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1859 uint32_t snapshot_id
;
1860 struct lttcomm_lttng_output_id reply
;
1862 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1863 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
),
1865 if (ret
!= LTTNG_OK
) {
1868 reply
.id
= snapshot_id
;
1870 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1876 /* Copy output list into message payload */
1880 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1882 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1883 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
));
1886 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1889 struct lttng_snapshot_output
*outputs
= NULL
;
1891 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1892 if (nb_output
< 0) {
1897 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1898 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1899 nb_output
* sizeof(struct lttng_snapshot_output
));
1909 case LTTNG_SNAPSHOT_RECORD
:
1911 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1912 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_record
.output
),
1913 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1916 case LTTNG_CREATE_SESSION_EXT
:
1918 struct lttng_dynamic_buffer payload
;
1919 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1921 lttng_dynamic_buffer_init(&payload
);
1922 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1923 if (ret
!= LTTNG_OK
) {
1927 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1930 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1931 lttng_session_descriptor_destroy(return_descriptor
);
1932 ret
= LTTNG_ERR_NOMEM
;
1935 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1938 lttng_session_descriptor_destroy(return_descriptor
);
1939 ret
= LTTNG_ERR_NOMEM
;
1942 lttng_dynamic_buffer_reset(&payload
);
1943 lttng_session_descriptor_destroy(return_descriptor
);
1947 case LTTNG_SAVE_SESSION
:
1949 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1953 case LTTNG_SET_SESSION_SHM_PATH
:
1955 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1956 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1959 case LTTNG_REGENERATE_METADATA
:
1961 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1964 case LTTNG_REGENERATE_STATEDUMP
:
1966 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1969 case LTTNG_REGISTER_TRIGGER
:
1971 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
1972 notification_thread_handle
);
1975 case LTTNG_UNREGISTER_TRIGGER
:
1977 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
1978 notification_thread_handle
);
1981 case LTTNG_ROTATE_SESSION
:
1983 struct lttng_rotate_session_return rotate_return
;
1985 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1987 memset(&rotate_return
, 0, sizeof(rotate_return
));
1988 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1989 DBG("Kernel tracer version is not compatible with the rotation feature");
1990 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1994 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
1996 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2002 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2003 sizeof(rotate_return
));
2012 case LTTNG_ROTATION_GET_INFO
:
2014 struct lttng_rotation_get_info_return get_info_return
;
2016 memset(&get_info_return
, 0, sizeof(get_info_return
));
2017 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2018 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
2024 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2025 sizeof(get_info_return
));
2034 case LTTNG_ROTATION_SET_SCHEDULE
:
2037 enum lttng_rotation_schedule_type schedule_type
;
2040 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2041 DBG("Kernel tracer version does not support session rotations");
2042 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2046 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
2047 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
2048 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
2050 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
2054 notification_thread_handle
);
2055 if (ret
!= LTTNG_OK
) {
2061 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2063 struct lttng_session_list_schedules_return schedules
= {
2064 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2065 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2066 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2067 .size
.value
= cmd_ctx
->session
->rotate_size
,
2070 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2080 case LTTNG_CLEAR_SESSION
:
2082 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2086 ret
= LTTNG_ERR_UND
;
2091 if (cmd_ctx
->llm
== NULL
) {
2092 DBG("Missing llm structure. Allocating one.");
2093 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2097 /* Set return code */
2098 cmd_ctx
->llm
->ret_code
= ret
;
2100 if (cmd_ctx
->session
) {
2101 session_unlock(cmd_ctx
->session
);
2102 session_put(cmd_ctx
->session
);
2103 cmd_ctx
->session
= NULL
;
2105 if (need_tracing_session
) {
2106 session_unlock_list();
2109 assert(!rcu_read_ongoing());
2113 static int create_client_sock(void)
2115 int ret
, client_sock
;
2116 const mode_t old_umask
= umask(0);
2118 /* Create client tool unix socket */
2119 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2120 if (client_sock
< 0) {
2121 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2126 /* Set the cloexec flag */
2127 ret
= utils_set_fd_cloexec(client_sock
);
2129 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2130 "Continuing but note that the consumer daemon will have a "
2131 "reference to this socket on exec()", client_sock
);
2134 /* File permission MUST be 660 */
2135 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2137 ERR("Set file permissions failed: %s",
2138 config
.client_unix_sock_path
.value
);
2140 (void) lttcomm_close_unix_sock(client_sock
);
2144 DBG("Created client socket (fd = %i)", client_sock
);
2151 static void cleanup_client_thread(void *data
)
2153 struct lttng_pipe
*quit_pipe
= data
;
2155 lttng_pipe_destroy(quit_pipe
);
2158 static void thread_init_cleanup(void *data
)
2160 set_thread_status(false);
2164 * This thread manage all clients request using the unix client socket for
2167 static void *thread_manage_clients(void *data
)
2169 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2171 uint32_t revents
, nb_fd
;
2172 struct command_ctx
*cmd_ctx
= NULL
;
2173 struct lttng_poll_event events
;
2174 const int client_sock
= thread_state
.client_sock
;
2175 struct lttng_pipe
*quit_pipe
= data
;
2176 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2178 DBG("[thread] Manage client started");
2180 is_root
= (getuid() == 0);
2182 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2184 rcu_register_thread();
2186 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2188 health_code_update();
2190 ret
= lttcomm_listen_unix_sock(client_sock
);
2196 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2197 * more will be added to this poll set.
2199 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2201 goto error_create_poll
;
2204 /* Add the application registration socket */
2205 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2210 /* Add thread quit pipe */
2211 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2216 /* Set state as running. */
2217 set_thread_status(true);
2218 pthread_cleanup_pop(0);
2220 /* This testpoint is after we signal readiness to the parent. */
2221 if (testpoint(sessiond_thread_manage_clients
)) {
2225 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2229 health_code_update();
2232 const struct cmd_completion_handler
*cmd_completion_handler
;
2234 DBG("Accepting client command ...");
2236 /* Inifinite blocking call, waiting for transmission */
2238 health_poll_entry();
2239 ret
= lttng_poll_wait(&events
, -1);
2243 * Restart interrupted system call.
2245 if (errno
== EINTR
) {
2253 for (i
= 0; i
< nb_fd
; i
++) {
2254 revents
= LTTNG_POLL_GETEV(&events
, i
);
2255 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2257 health_code_update();
2259 if (pollfd
== thread_quit_pipe_fd
) {
2263 /* Event on the registration socket */
2264 if (revents
& LPOLLIN
) {
2266 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2267 ERR("Client socket poll error");
2270 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2276 DBG("Wait for client response");
2278 health_code_update();
2280 sock
= lttcomm_accept_unix_sock(client_sock
);
2286 * Set the CLOEXEC flag. Return code is useless because either way, the
2289 (void) utils_set_fd_cloexec(sock
);
2291 /* Set socket option for credentials retrieval */
2292 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2297 /* Allocate context command to process the client request */
2298 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2299 if (cmd_ctx
== NULL
) {
2300 PERROR("zmalloc cmd_ctx");
2304 /* Allocate data buffer for reception */
2305 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2306 if (cmd_ctx
->lsm
== NULL
) {
2307 PERROR("zmalloc cmd_ctx->lsm");
2311 cmd_ctx
->llm
= NULL
;
2312 cmd_ctx
->session
= NULL
;
2314 health_code_update();
2317 * Data is received from the lttng client. The struct
2318 * lttcomm_session_msg (lsm) contains the command and data request of
2321 DBG("Receiving data from client ...");
2322 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2323 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2325 DBG("Nothing recv() from client... continuing");
2331 clean_command_ctx(&cmd_ctx
);
2335 health_code_update();
2337 // TODO: Validate cmd_ctx including sanity check for
2338 // security purpose.
2340 rcu_thread_online();
2342 * This function dispatch the work to the kernel or userspace tracer
2343 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2344 * informations for the client. The command context struct contains
2345 * everything this function may needs.
2347 ret
= process_client_msg(cmd_ctx
, &sock
, &sock_error
);
2348 rcu_thread_offline();
2358 * TODO: Inform client somehow of the fatal error. At
2359 * this point, ret < 0 means that a zmalloc failed
2360 * (ENOMEM). Error detected but still accept
2361 * command, unless a socket error has been
2364 clean_command_ctx(&cmd_ctx
);
2368 cmd_completion_handler
= cmd_pop_completion_handler();
2369 if (cmd_completion_handler
) {
2370 enum lttng_error_code completion_code
;
2372 completion_code
= cmd_completion_handler
->run(
2373 cmd_completion_handler
->data
);
2374 if (completion_code
!= LTTNG_OK
) {
2375 clean_command_ctx(&cmd_ctx
);
2380 health_code_update();
2383 DBG("Sending response (size: %d, retcode: %s (%d))",
2384 cmd_ctx
->lttng_msg_size
,
2385 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2386 cmd_ctx
->llm
->ret_code
);
2387 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2388 cmd_ctx
->lttng_msg_size
);
2390 ERR("Failed to send data back to client");
2393 /* End of transmission */
2401 clean_command_ctx(&cmd_ctx
);
2403 health_code_update();
2415 lttng_poll_clean(&events
);
2416 clean_command_ctx(&cmd_ctx
);
2420 unlink(config
.client_unix_sock_path
.value
);
2421 ret
= close(client_sock
);
2428 ERR("Health error occurred in %s", __func__
);
2431 health_unregister(health_sessiond
);
2433 DBG("Client thread dying");
2435 rcu_unregister_thread();
2440 bool shutdown_client_thread(void *thread_data
)
2442 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2443 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2445 return notify_thread_pipe(write_fd
) == 1;
2448 struct lttng_thread
*launch_client_thread(void)
2450 bool thread_running
;
2451 struct lttng_pipe
*client_quit_pipe
;
2452 struct lttng_thread
*thread
= NULL
;
2453 int client_sock_fd
= -1;
2455 sem_init(&thread_state
.ready
, 0, 0);
2456 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2457 if (!client_quit_pipe
) {
2461 client_sock_fd
= create_client_sock();
2462 if (client_sock_fd
< 0) {
2466 thread_state
.client_sock
= client_sock_fd
;
2467 thread
= lttng_thread_create("Client management",
2468 thread_manage_clients
,
2469 shutdown_client_thread
,
2470 cleanup_client_thread
,
2475 /* The client thread now owns the client sock fd and the quit pipe. */
2476 client_sock_fd
= -1;
2477 client_quit_pipe
= NULL
;
2480 * This thread is part of the threads that need to be fully
2481 * initialized before the session daemon is marked as "ready".
2483 thread_running
= wait_thread_status();
2484 if (!thread_running
) {
2489 if (client_sock_fd
>= 0) {
2490 if (close(client_sock_fd
)) {
2491 PERROR("Failed to close client socket");
2494 lttng_thread_put(thread
);
2495 cleanup_client_thread(client_quit_pipe
);