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
;
498 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
503 trace_kernel_destroy_session(session
->kernel_session
);
504 session
->kernel_session
= NULL
;
510 * Count number of session permitted by uid/gid.
512 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
515 struct ltt_session
*session
;
516 const struct ltt_session_list
*session_list
= session_get_list();
518 DBG("Counting number of available session for UID %d GID %d",
520 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
521 if (!session_get(session
)) {
524 session_lock(session
);
525 /* Only count the sessions the user can control. */
526 if (session_access_ok(session
, uid
, gid
) &&
527 !session
->destroyed
) {
530 session_unlock(session
);
531 session_put(session
);
536 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
537 int *sock_error
, struct lttng_event
*event
)
540 struct lttng_userspace_probe_location
*probe_location
;
541 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
542 struct lttng_dynamic_buffer probe_location_buffer
;
543 struct lttng_buffer_view buffer_view
;
546 * Create a buffer to store the serialized version of the probe
549 lttng_dynamic_buffer_init(&probe_location_buffer
);
550 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
551 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
553 ret
= LTTNG_ERR_NOMEM
;
558 * Receive the probe location.
560 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
561 probe_location_buffer
.size
);
563 DBG("Nothing recv() from client var len data... continuing");
565 lttng_dynamic_buffer_reset(&probe_location_buffer
);
566 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
570 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
571 &probe_location_buffer
, 0, probe_location_buffer
.size
);
574 * Extract the probe location from the serialized version.
576 ret
= lttng_userspace_probe_location_create_from_buffer(
577 &buffer_view
, &probe_location
);
579 WARN("Failed to create a userspace probe location from the received buffer");
580 lttng_dynamic_buffer_reset( &probe_location_buffer
);
581 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
586 * Receive the file descriptor to the target binary from the client.
588 DBG("Receiving userspace probe target FD from client ...");
589 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
591 DBG("Nothing recv() from client userspace probe fd... continuing");
593 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
598 * Set the file descriptor received from the client through the unix
599 * socket in the probe location.
601 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
603 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
608 * From the kernel tracer's perspective, all userspace probe event types
609 * are all the same: a file and an offset.
611 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
612 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
613 ret
= lttng_userspace_probe_location_function_set_binary_fd(
616 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
617 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
621 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
626 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
630 /* Attach the probe location to the event. */
631 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
633 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
637 lttng_dynamic_buffer_reset(&probe_location_buffer
);
643 * Version of setup_lttng_msg() without command header.
645 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
646 void *payload_buf
, size_t payload_len
)
648 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
652 * Free memory of a command context structure.
654 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
656 DBG("Clean command context structure");
658 if ((*cmd_ctx
)->llm
) {
659 free((*cmd_ctx
)->llm
);
661 if ((*cmd_ctx
)->lsm
) {
662 free((*cmd_ctx
)->lsm
);
670 * Check if the current kernel tracer supports the session rotation feature.
671 * Return 1 if it does, 0 otherwise.
673 static int check_rotate_compatible(void)
677 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
678 DBG("Kernel tracer version is not compatible with the rotation feature");
686 * Send data on a unix socket using the liblttsessiondcomm API.
688 * Return lttcomm error code.
690 static int send_unix_sock(int sock
, void *buf
, size_t len
)
692 /* Check valid length */
697 return lttcomm_send_unix_sock(sock
, buf
, len
);
701 * Process the command requested by the lttng client within the command
702 * context structure. This function make sure that the return structure (llm)
703 * is set and ready for transmission before returning.
705 * Return any error encountered or 0 for success.
707 * "sock" is only used for special-case var. len data.
708 * A command may assume the ownership of the socket, in which case its value
709 * should be set to -1.
711 * Should *NOT* be called with RCU read-side lock held.
713 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
717 int need_tracing_session
= 1;
720 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
722 assert(!rcu_read_ongoing());
726 switch (cmd_ctx
->lsm
->cmd_type
) {
727 case LTTNG_CREATE_SESSION_EXT
:
728 case LTTNG_DESTROY_SESSION
:
729 case LTTNG_LIST_SESSIONS
:
730 case LTTNG_LIST_DOMAINS
:
731 case LTTNG_START_TRACE
:
732 case LTTNG_STOP_TRACE
:
733 case LTTNG_DATA_PENDING
:
734 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
735 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
736 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
737 case LTTNG_SNAPSHOT_RECORD
:
738 case LTTNG_SAVE_SESSION
:
739 case LTTNG_SET_SESSION_SHM_PATH
:
740 case LTTNG_REGENERATE_METADATA
:
741 case LTTNG_REGENERATE_STATEDUMP
:
742 case LTTNG_REGISTER_TRIGGER
:
743 case LTTNG_UNREGISTER_TRIGGER
:
744 case LTTNG_ROTATE_SESSION
:
745 case LTTNG_ROTATION_GET_INFO
:
746 case LTTNG_ROTATION_SET_SCHEDULE
:
747 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
748 case LTTNG_CLEAR_SESSION
:
755 if (config
.no_kernel
&& need_domain
756 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
758 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
760 ret
= LTTNG_ERR_KERN_NA
;
765 /* Deny register consumer if we already have a spawned consumer. */
766 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
767 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
768 if (kconsumer_data
.pid
> 0) {
769 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
770 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
773 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
777 * Check for command that don't needs to allocate a returned payload. We do
778 * this here so we don't have to make the call for no payload at each
781 switch(cmd_ctx
->lsm
->cmd_type
) {
782 case LTTNG_LIST_SESSIONS
:
783 case LTTNG_LIST_TRACEPOINTS
:
784 case LTTNG_LIST_TRACEPOINT_FIELDS
:
785 case LTTNG_LIST_DOMAINS
:
786 case LTTNG_LIST_CHANNELS
:
787 case LTTNG_LIST_EVENTS
:
788 case LTTNG_LIST_SYSCALLS
:
789 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
790 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
791 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
792 case LTTNG_DATA_PENDING
:
793 case LTTNG_ROTATE_SESSION
:
794 case LTTNG_ROTATION_GET_INFO
:
797 /* Setup lttng message with no payload */
798 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
800 /* This label does not try to unlock the session */
801 goto init_setup_error
;
805 /* Commands that DO NOT need a session. */
806 switch (cmd_ctx
->lsm
->cmd_type
) {
807 case LTTNG_CREATE_SESSION_EXT
:
808 case LTTNG_LIST_SESSIONS
:
809 case LTTNG_LIST_TRACEPOINTS
:
810 case LTTNG_LIST_SYSCALLS
:
811 case LTTNG_LIST_TRACEPOINT_FIELDS
:
812 case LTTNG_SAVE_SESSION
:
813 case LTTNG_REGISTER_TRIGGER
:
814 case LTTNG_UNREGISTER_TRIGGER
:
815 need_tracing_session
= 0;
818 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
820 * We keep the session list lock across _all_ commands
821 * for now, because the per-session lock does not
822 * handle teardown properly.
825 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
826 if (cmd_ctx
->session
== NULL
) {
827 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
830 /* Acquire lock for the session */
831 session_lock(cmd_ctx
->session
);
837 * Commands that need a valid session but should NOT create one if none
838 * exists. Instead of creating one and destroying it when the command is
839 * handled, process that right before so we save some round trip in useless
842 switch (cmd_ctx
->lsm
->cmd_type
) {
843 case LTTNG_DISABLE_CHANNEL
:
844 case LTTNG_DISABLE_EVENT
:
845 switch (cmd_ctx
->lsm
->domain
.type
) {
846 case LTTNG_DOMAIN_KERNEL
:
847 if (!cmd_ctx
->session
->kernel_session
) {
848 ret
= LTTNG_ERR_NO_CHANNEL
;
852 case LTTNG_DOMAIN_JUL
:
853 case LTTNG_DOMAIN_LOG4J
:
854 case LTTNG_DOMAIN_PYTHON
:
855 case LTTNG_DOMAIN_UST
:
856 if (!cmd_ctx
->session
->ust_session
) {
857 ret
= LTTNG_ERR_NO_CHANNEL
;
862 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
874 * Check domain type for specific "pre-action".
876 switch (cmd_ctx
->lsm
->domain
.type
) {
877 case LTTNG_DOMAIN_KERNEL
:
879 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
883 /* Kernel tracer check */
884 if (!kernel_tracer_is_initialized()) {
885 /* Basically, load kernel tracer modules */
886 ret
= init_kernel_tracer();
892 /* Consumer is in an ERROR state. Report back to client */
893 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
894 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
898 /* Need a session for kernel command */
899 if (need_tracing_session
) {
900 if (cmd_ctx
->session
->kernel_session
== NULL
) {
901 ret
= create_kernel_session(cmd_ctx
->session
);
902 if (ret
!= LTTNG_OK
) {
903 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
908 /* Start the kernel consumer daemon */
909 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
910 if (kconsumer_data
.pid
== 0 &&
911 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
912 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
913 ret
= start_consumerd(&kconsumer_data
);
915 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
918 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
920 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
924 * The consumer was just spawned so we need to add the socket to
925 * the consumer output of the session if exist.
927 ret
= consumer_create_socket(&kconsumer_data
,
928 cmd_ctx
->session
->kernel_session
->consumer
);
935 case LTTNG_DOMAIN_JUL
:
936 case LTTNG_DOMAIN_LOG4J
:
937 case LTTNG_DOMAIN_PYTHON
:
938 case LTTNG_DOMAIN_UST
:
940 if (!ust_app_supported()) {
941 ret
= LTTNG_ERR_NO_UST
;
944 /* Consumer is in an ERROR state. Report back to client */
945 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
946 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
950 if (need_tracing_session
) {
951 /* Create UST session if none exist. */
952 if (cmd_ctx
->session
->ust_session
== NULL
) {
953 ret
= create_ust_session(cmd_ctx
->session
,
954 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
));
955 if (ret
!= LTTNG_OK
) {
960 /* Start the UST consumer daemons */
962 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
963 if (config
.consumerd64_bin_path
.value
&&
964 ustconsumer64_data
.pid
== 0 &&
965 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
966 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
967 ret
= start_consumerd(&ustconsumer64_data
);
969 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
970 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
974 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
975 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
977 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
981 * Setup socket for consumer 64 bit. No need for atomic access
982 * since it was set above and can ONLY be set in this thread.
984 ret
= consumer_create_socket(&ustconsumer64_data
,
985 cmd_ctx
->session
->ust_session
->consumer
);
991 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
992 if (config
.consumerd32_bin_path
.value
&&
993 ustconsumer32_data
.pid
== 0 &&
994 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
995 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
996 ret
= start_consumerd(&ustconsumer32_data
);
998 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
999 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
1003 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
1004 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1006 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1010 * Setup socket for consumer 32 bit. No need for atomic access
1011 * since it was set above and can ONLY be set in this thread.
1013 ret
= consumer_create_socket(&ustconsumer32_data
,
1014 cmd_ctx
->session
->ust_session
->consumer
);
1026 /* Validate consumer daemon state when start/stop trace command */
1027 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1028 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1029 switch (cmd_ctx
->lsm
->domain
.type
) {
1030 case LTTNG_DOMAIN_NONE
:
1032 case LTTNG_DOMAIN_JUL
:
1033 case LTTNG_DOMAIN_LOG4J
:
1034 case LTTNG_DOMAIN_PYTHON
:
1035 case LTTNG_DOMAIN_UST
:
1036 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1037 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1041 case LTTNG_DOMAIN_KERNEL
:
1042 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1043 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1048 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1054 * Check that the UID or GID match that of the tracing session.
1055 * The root user can interact with all sessions.
1057 if (need_tracing_session
) {
1058 if (!session_access_ok(cmd_ctx
->session
,
1059 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1060 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1061 cmd_ctx
->session
->destroyed
) {
1062 ret
= LTTNG_ERR_EPERM
;
1068 * Send relayd information to consumer as soon as we have a domain and a
1071 if (cmd_ctx
->session
&& need_domain
) {
1073 * Setup relayd if not done yet. If the relayd information was already
1074 * sent to the consumer, this call will gracefully return.
1076 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1077 if (ret
!= LTTNG_OK
) {
1082 /* Process by command type */
1083 switch (cmd_ctx
->lsm
->cmd_type
) {
1084 case LTTNG_ADD_CONTEXT
:
1087 * An LTTNG_ADD_CONTEXT command might have a supplementary
1088 * payload if the context being added is an application context.
1090 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1091 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1092 char *provider_name
= NULL
, *context_name
= NULL
;
1093 size_t provider_name_len
=
1094 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1095 size_t context_name_len
=
1096 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1098 if (provider_name_len
== 0 || context_name_len
== 0) {
1100 * Application provider and context names MUST
1103 ret
= -LTTNG_ERR_INVALID
;
1107 provider_name
= zmalloc(provider_name_len
+ 1);
1108 if (!provider_name
) {
1109 ret
= -LTTNG_ERR_NOMEM
;
1112 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1115 context_name
= zmalloc(context_name_len
+ 1);
1116 if (!context_name
) {
1117 ret
= -LTTNG_ERR_NOMEM
;
1118 goto error_add_context
;
1120 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1123 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1126 goto error_add_context
;
1129 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1132 goto error_add_context
;
1137 * cmd_add_context assumes ownership of the provider and context
1140 ret
= cmd_add_context(cmd_ctx
->session
,
1141 cmd_ctx
->lsm
->domain
.type
,
1142 cmd_ctx
->lsm
->u
.context
.channel_name
,
1143 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.context
.ctx
),
1144 kernel_poll_pipe
[1]);
1146 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1147 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1149 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1150 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1156 case LTTNG_DISABLE_CHANNEL
:
1158 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1159 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1162 case LTTNG_DISABLE_EVENT
:
1166 * FIXME: handle filter; for now we just receive the filter's
1167 * bytecode along with the filter expression which are sent by
1168 * liblttng-ctl and discard them.
1170 * This fixes an issue where the client may block while sending
1171 * the filter payload and encounter an error because the session
1172 * daemon closes the socket without ever handling this data.
1174 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1175 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1178 char data
[LTTNG_FILTER_MAX_LEN
];
1180 DBG("Discarding disable event command payload of size %zu", count
);
1182 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1183 count
> sizeof(data
) ? sizeof(data
) : count
);
1188 count
-= (size_t) ret
;
1191 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1192 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1193 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.disable
.event
));
1196 case LTTNG_ENABLE_CHANNEL
:
1198 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1199 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1200 ret
= cmd_enable_channel(cmd_ctx
->session
,
1201 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1202 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.channel
.chan
),
1203 kernel_poll_pipe
[1]);
1206 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1207 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1209 struct lttng_dynamic_buffer payload
;
1210 struct lttng_buffer_view payload_view
;
1211 const bool add_value
=
1212 cmd_ctx
->lsm
->cmd_type
==
1213 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1214 const size_t name_len
=
1215 cmd_ctx
->lsm
->u
.process_attr_tracker_add_remove_include_value
1217 const enum lttng_domain_type domain_type
=
1218 (enum lttng_domain_type
)
1219 cmd_ctx
->lsm
->domain
.type
;
1220 const enum lttng_process_attr process_attr
=
1221 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1222 .process_attr_tracker_add_remove_include_value
1224 const enum lttng_process_attr_value_type value_type
=
1225 (enum lttng_process_attr_value_type
) cmd_ctx
1227 .process_attr_tracker_add_remove_include_value
1229 struct process_attr_value
*value
;
1230 enum lttng_error_code ret_code
;
1232 /* Receive remaining variable length payload if applicable. */
1233 if (name_len
> LOGIN_NAME_MAX
) {
1235 * POSIX mandates user and group names that are at least
1236 * 8 characters long. Note that although shadow-utils
1237 * (useradd, groupaadd, etc.) use 32 chars as their
1238 * limit (from bits/utmp.h, UT_NAMESIZE),
1239 * LOGIN_NAME_MAX is defined to 256.
1241 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %d",
1242 add_value
? "addition" : "removal",
1243 name_len
, LOGIN_NAME_MAX
);
1244 ret
= LTTNG_ERR_INVALID
;
1248 lttng_dynamic_buffer_init(&payload
);
1249 if (name_len
!= 0) {
1251 * Receive variable payload for user/group name
1254 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1256 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1257 add_value
? "add" : "remove");
1258 ret
= LTTNG_ERR_NOMEM
;
1259 goto error_add_remove_tracker_value
;
1262 ret
= lttcomm_recv_unix_sock(
1263 *sock
, payload
.data
, name_len
);
1265 ERR("Failed to receive payload of %s process attribute tracker value argument",
1266 add_value
? "add" : "remove");
1268 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1269 goto error_add_remove_tracker_value
;
1273 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1274 &payload
, 0, name_len
);
1276 * Validate the value type and domains are legal for the process
1277 * attribute tracker that is specified and convert the value to
1278 * add/remove to the internal sessiond representation.
1280 ret_code
= process_attr_value_from_comm(domain_type
,
1281 process_attr
, value_type
,
1282 &cmd_ctx
->lsm
->u
.process_attr_tracker_add_remove_include_value
1284 &payload_view
, &value
);
1285 if (ret_code
!= LTTNG_OK
) {
1287 goto error_add_remove_tracker_value
;
1291 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1292 cmd_ctx
->session
, domain_type
,
1293 process_attr
, value
);
1295 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1296 cmd_ctx
->session
, domain_type
,
1297 process_attr
, value
);
1299 process_attr_value_destroy(value
);
1300 error_add_remove_tracker_value
:
1301 lttng_dynamic_buffer_reset(&payload
);
1304 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1306 enum lttng_tracking_policy tracking_policy
;
1307 const enum lttng_domain_type domain_type
=
1308 (enum lttng_domain_type
)
1309 cmd_ctx
->lsm
->domain
.type
;
1310 const enum lttng_process_attr process_attr
=
1311 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1312 .process_attr_tracker_get_tracking_policy
1315 ret
= cmd_process_attr_tracker_get_tracking_policy(
1316 cmd_ctx
->session
, domain_type
, process_attr
,
1318 if (ret
!= LTTNG_OK
) {
1322 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1323 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1325 ret
= LTTNG_ERR_NOMEM
;
1331 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1333 const enum lttng_tracking_policy tracking_policy
=
1334 (enum lttng_tracking_policy
) cmd_ctx
->lsm
->u
1335 .process_attr_tracker_set_tracking_policy
1337 const enum lttng_domain_type domain_type
=
1338 (enum lttng_domain_type
)
1339 cmd_ctx
->lsm
->domain
.type
;
1340 const enum lttng_process_attr process_attr
=
1341 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1342 .process_attr_tracker_set_tracking_policy
1345 ret
= cmd_process_attr_tracker_set_tracking_policy(
1346 cmd_ctx
->session
, domain_type
, process_attr
,
1348 if (ret
!= LTTNG_OK
) {
1353 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1355 struct lttng_process_attr_values
*values
;
1356 struct lttng_dynamic_buffer reply
;
1357 const enum lttng_domain_type domain_type
=
1358 (enum lttng_domain_type
)
1359 cmd_ctx
->lsm
->domain
.type
;
1360 const enum lttng_process_attr process_attr
=
1361 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1362 .process_attr_tracker_get_inclusion_set
1365 ret
= cmd_process_attr_tracker_get_inclusion_set(
1366 cmd_ctx
->session
, domain_type
, process_attr
,
1368 if (ret
!= LTTNG_OK
) {
1372 lttng_dynamic_buffer_init(&reply
);
1373 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1375 goto error_tracker_get_inclusion_set
;
1378 ret
= setup_lttng_msg_no_cmd_header(
1379 cmd_ctx
, reply
.data
, reply
.size
);
1381 ret
= LTTNG_ERR_NOMEM
;
1382 goto error_tracker_get_inclusion_set
;
1386 error_tracker_get_inclusion_set
:
1387 lttng_process_attr_values_destroy(values
);
1388 lttng_dynamic_buffer_reset(&reply
);
1391 case LTTNG_ENABLE_EVENT
:
1393 struct lttng_event
*ev
= NULL
;
1394 struct lttng_event_exclusion
*exclusion
= NULL
;
1395 struct lttng_filter_bytecode
*bytecode
= NULL
;
1396 char *filter_expression
= NULL
;
1398 /* Handle exclusion events and receive it from the client. */
1399 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1400 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1402 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1403 (count
* LTTNG_SYMBOL_NAME_LEN
));
1405 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1409 DBG("Receiving var len exclusion event list from client ...");
1410 exclusion
->count
= count
;
1411 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1412 count
* LTTNG_SYMBOL_NAME_LEN
);
1414 DBG("Nothing recv() from client var len data... continuing");
1417 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1422 /* Get filter expression from client. */
1423 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1424 size_t expression_len
=
1425 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1427 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1428 ret
= LTTNG_ERR_FILTER_INVAL
;
1433 filter_expression
= zmalloc(expression_len
);
1434 if (!filter_expression
) {
1436 ret
= LTTNG_ERR_FILTER_NOMEM
;
1440 /* Receive var. len. data */
1441 DBG("Receiving var len filter's expression from client ...");
1442 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1445 DBG("Nothing recv() from client var len data... continuing");
1447 free(filter_expression
);
1449 ret
= LTTNG_ERR_FILTER_INVAL
;
1454 /* Handle filter and get bytecode from client. */
1455 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1456 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1458 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1459 ret
= LTTNG_ERR_FILTER_INVAL
;
1460 free(filter_expression
);
1465 bytecode
= zmalloc(bytecode_len
);
1467 free(filter_expression
);
1469 ret
= LTTNG_ERR_FILTER_NOMEM
;
1473 /* Receive var. len. data */
1474 DBG("Receiving var len filter's bytecode from client ...");
1475 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1477 DBG("Nothing recv() from client var len data... continuing");
1479 free(filter_expression
);
1482 ret
= LTTNG_ERR_FILTER_INVAL
;
1486 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1487 free(filter_expression
);
1490 ret
= LTTNG_ERR_FILTER_INVAL
;
1495 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.enable
.event
));
1497 DBG("Failed to copy event: %s",
1498 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1499 free(filter_expression
);
1502 ret
= LTTNG_ERR_NOMEM
;
1507 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1508 /* Expect a userspace probe description. */
1509 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1511 free(filter_expression
);
1514 lttng_event_destroy(ev
);
1519 ret
= cmd_enable_event(cmd_ctx
->session
,
1520 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1521 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1523 filter_expression
, bytecode
, exclusion
,
1524 kernel_poll_pipe
[1]);
1525 lttng_event_destroy(ev
);
1528 case LTTNG_LIST_TRACEPOINTS
:
1530 struct lttng_event
*events
;
1533 session_lock_list();
1534 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1535 session_unlock_list();
1536 if (nb_events
< 0) {
1537 /* Return value is a negative lttng_error_code. */
1543 * Setup lttng message with payload size set to the event list size in
1544 * bytes and then copy list into the llm payload.
1546 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1547 sizeof(struct lttng_event
) * nb_events
);
1557 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1559 struct lttng_event_field
*fields
;
1562 session_lock_list();
1563 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1565 session_unlock_list();
1566 if (nb_fields
< 0) {
1567 /* Return value is a negative lttng_error_code. */
1573 * Setup lttng message with payload size set to the event list size in
1574 * bytes and then copy list into the llm payload.
1576 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1577 sizeof(struct lttng_event_field
) * nb_fields
);
1587 case LTTNG_LIST_SYSCALLS
:
1589 struct lttng_event
*events
;
1592 nb_events
= cmd_list_syscalls(&events
);
1593 if (nb_events
< 0) {
1594 /* Return value is a negative lttng_error_code. */
1600 * Setup lttng message with payload size set to the event list size in
1601 * bytes and then copy list into the llm payload.
1603 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1604 sizeof(struct lttng_event
) * nb_events
);
1614 case LTTNG_SET_CONSUMER_URI
:
1617 struct lttng_uri
*uris
;
1619 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1620 len
= nb_uri
* sizeof(struct lttng_uri
);
1623 ret
= LTTNG_ERR_INVALID
;
1627 uris
= zmalloc(len
);
1629 ret
= LTTNG_ERR_FATAL
;
1633 /* Receive variable len data */
1634 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1635 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1637 DBG("No URIs received from client... continuing");
1639 ret
= LTTNG_ERR_SESSION_FAIL
;
1644 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1646 if (ret
!= LTTNG_OK
) {
1653 case LTTNG_START_TRACE
:
1656 * On the first start, if we have a kernel session and we have
1657 * enabled time or size-based rotations, we have to make sure
1658 * the kernel tracer supports it.
1660 if (!cmd_ctx
->session
->has_been_started
&& \
1661 cmd_ctx
->session
->kernel_session
&& \
1662 (cmd_ctx
->session
->rotate_timer_period
|| \
1663 cmd_ctx
->session
->rotate_size
) && \
1664 !check_rotate_compatible()) {
1665 DBG("Kernel tracer version is not compatible with the rotation feature");
1666 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1669 ret
= cmd_start_trace(cmd_ctx
->session
);
1672 case LTTNG_STOP_TRACE
:
1674 ret
= cmd_stop_trace(cmd_ctx
->session
);
1677 case LTTNG_DESTROY_SESSION
:
1679 ret
= cmd_destroy_session(cmd_ctx
->session
,
1680 notification_thread_handle
,
1684 case LTTNG_LIST_DOMAINS
:
1687 struct lttng_domain
*domains
= NULL
;
1689 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1691 /* Return value is a negative lttng_error_code. */
1696 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1697 nb_dom
* sizeof(struct lttng_domain
));
1707 case LTTNG_LIST_CHANNELS
:
1709 ssize_t payload_size
;
1710 struct lttng_channel
*channels
= NULL
;
1712 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1713 cmd_ctx
->session
, &channels
);
1714 if (payload_size
< 0) {
1715 /* Return value is a negative lttng_error_code. */
1716 ret
= -payload_size
;
1720 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1731 case LTTNG_LIST_EVENTS
:
1734 struct lttng_event
*events
= NULL
;
1735 struct lttcomm_event_command_header cmd_header
;
1738 memset(&cmd_header
, 0, sizeof(cmd_header
));
1739 /* Extended infos are included at the end of events */
1740 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1741 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1742 &events
, &total_size
);
1745 /* Return value is a negative lttng_error_code. */
1750 cmd_header
.nb_events
= nb_event
;
1751 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1752 &cmd_header
, sizeof(cmd_header
));
1762 case LTTNG_LIST_SESSIONS
:
1764 unsigned int nr_sessions
;
1765 void *sessions_payload
;
1768 session_lock_list();
1769 nr_sessions
= lttng_sessions_count(
1770 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1771 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1773 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1774 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1775 sessions_payload
= zmalloc(payload_len
);
1777 if (!sessions_payload
) {
1778 session_unlock_list();
1783 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1784 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1785 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1786 session_unlock_list();
1788 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1790 free(sessions_payload
);
1799 case LTTNG_REGISTER_CONSUMER
:
1801 struct consumer_data
*cdata
;
1803 switch (cmd_ctx
->lsm
->domain
.type
) {
1804 case LTTNG_DOMAIN_KERNEL
:
1805 cdata
= &kconsumer_data
;
1808 ret
= LTTNG_ERR_UND
;
1812 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1813 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1816 case LTTNG_DATA_PENDING
:
1819 uint8_t pending_ret_byte
;
1821 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1826 * This function may returns 0 or 1 to indicate whether or not
1827 * there is data pending. In case of error, it should return an
1828 * LTTNG_ERR code. However, some code paths may still return
1829 * a nondescript error code, which we handle by returning an
1832 if (pending_ret
== 0 || pending_ret
== 1) {
1834 * ret will be set to LTTNG_OK at the end of
1837 } else if (pending_ret
< 0) {
1838 ret
= LTTNG_ERR_UNK
;
1845 pending_ret_byte
= (uint8_t) pending_ret
;
1847 /* 1 byte to return whether or not data is pending */
1848 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1849 &pending_ret_byte
, 1);
1858 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1860 uint32_t snapshot_id
;
1861 struct lttcomm_lttng_output_id reply
;
1863 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1864 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
),
1866 if (ret
!= LTTNG_OK
) {
1869 reply
.id
= snapshot_id
;
1871 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1877 /* Copy output list into message payload */
1881 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1883 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1884 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
));
1887 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1890 struct lttng_snapshot_output
*outputs
= NULL
;
1892 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1893 if (nb_output
< 0) {
1898 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1899 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1900 nb_output
* sizeof(struct lttng_snapshot_output
));
1910 case LTTNG_SNAPSHOT_RECORD
:
1912 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1913 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_record
.output
),
1914 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1917 case LTTNG_CREATE_SESSION_EXT
:
1919 struct lttng_dynamic_buffer payload
;
1920 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1922 lttng_dynamic_buffer_init(&payload
);
1923 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1924 if (ret
!= LTTNG_OK
) {
1928 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1931 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1932 lttng_session_descriptor_destroy(return_descriptor
);
1933 ret
= LTTNG_ERR_NOMEM
;
1936 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1939 lttng_session_descriptor_destroy(return_descriptor
);
1940 ret
= LTTNG_ERR_NOMEM
;
1943 lttng_dynamic_buffer_reset(&payload
);
1944 lttng_session_descriptor_destroy(return_descriptor
);
1948 case LTTNG_SAVE_SESSION
:
1950 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1954 case LTTNG_SET_SESSION_SHM_PATH
:
1956 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1957 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1960 case LTTNG_REGENERATE_METADATA
:
1962 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1965 case LTTNG_REGENERATE_STATEDUMP
:
1967 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1970 case LTTNG_REGISTER_TRIGGER
:
1972 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
1973 notification_thread_handle
);
1976 case LTTNG_UNREGISTER_TRIGGER
:
1978 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
1979 notification_thread_handle
);
1982 case LTTNG_ROTATE_SESSION
:
1984 struct lttng_rotate_session_return rotate_return
;
1986 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1988 memset(&rotate_return
, 0, sizeof(rotate_return
));
1989 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1990 DBG("Kernel tracer version is not compatible with the rotation feature");
1991 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1995 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
1997 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2003 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2004 sizeof(rotate_return
));
2013 case LTTNG_ROTATION_GET_INFO
:
2015 struct lttng_rotation_get_info_return get_info_return
;
2017 memset(&get_info_return
, 0, sizeof(get_info_return
));
2018 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2019 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
2025 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2026 sizeof(get_info_return
));
2035 case LTTNG_ROTATION_SET_SCHEDULE
:
2038 enum lttng_rotation_schedule_type schedule_type
;
2041 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2042 DBG("Kernel tracer version does not support session rotations");
2043 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2047 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
2048 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
2049 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
2051 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
2055 notification_thread_handle
);
2056 if (ret
!= LTTNG_OK
) {
2062 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2064 struct lttng_session_list_schedules_return schedules
= {
2065 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2066 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2067 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2068 .size
.value
= cmd_ctx
->session
->rotate_size
,
2071 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2081 case LTTNG_CLEAR_SESSION
:
2083 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2087 ret
= LTTNG_ERR_UND
;
2092 if (cmd_ctx
->llm
== NULL
) {
2093 DBG("Missing llm structure. Allocating one.");
2094 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2098 /* Set return code */
2099 cmd_ctx
->llm
->ret_code
= ret
;
2101 if (cmd_ctx
->session
) {
2102 session_unlock(cmd_ctx
->session
);
2103 session_put(cmd_ctx
->session
);
2104 cmd_ctx
->session
= NULL
;
2106 if (need_tracing_session
) {
2107 session_unlock_list();
2110 assert(!rcu_read_ongoing());
2114 static int create_client_sock(void)
2116 int ret
, client_sock
;
2117 const mode_t old_umask
= umask(0);
2119 /* Create client tool unix socket */
2120 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2121 if (client_sock
< 0) {
2122 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2127 /* Set the cloexec flag */
2128 ret
= utils_set_fd_cloexec(client_sock
);
2130 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2131 "Continuing but note that the consumer daemon will have a "
2132 "reference to this socket on exec()", client_sock
);
2135 /* File permission MUST be 660 */
2136 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2138 ERR("Set file permissions failed: %s",
2139 config
.client_unix_sock_path
.value
);
2141 (void) lttcomm_close_unix_sock(client_sock
);
2145 DBG("Created client socket (fd = %i)", client_sock
);
2152 static void cleanup_client_thread(void *data
)
2154 struct lttng_pipe
*quit_pipe
= data
;
2156 lttng_pipe_destroy(quit_pipe
);
2159 static void thread_init_cleanup(void *data
)
2161 set_thread_status(false);
2165 * This thread manage all clients request using the unix client socket for
2168 static void *thread_manage_clients(void *data
)
2170 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2172 uint32_t revents
, nb_fd
;
2173 struct command_ctx
*cmd_ctx
= NULL
;
2174 struct lttng_poll_event events
;
2175 const int client_sock
= thread_state
.client_sock
;
2176 struct lttng_pipe
*quit_pipe
= data
;
2177 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2179 DBG("[thread] Manage client started");
2181 is_root
= (getuid() == 0);
2183 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2185 rcu_register_thread();
2187 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2189 health_code_update();
2191 ret
= lttcomm_listen_unix_sock(client_sock
);
2197 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2198 * more will be added to this poll set.
2200 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2202 goto error_create_poll
;
2205 /* Add the application registration socket */
2206 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2211 /* Add thread quit pipe */
2212 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2217 /* Set state as running. */
2218 set_thread_status(true);
2219 pthread_cleanup_pop(0);
2221 /* This testpoint is after we signal readiness to the parent. */
2222 if (testpoint(sessiond_thread_manage_clients
)) {
2226 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2230 health_code_update();
2233 const struct cmd_completion_handler
*cmd_completion_handler
;
2235 DBG("Accepting client command ...");
2237 /* Inifinite blocking call, waiting for transmission */
2239 health_poll_entry();
2240 ret
= lttng_poll_wait(&events
, -1);
2244 * Restart interrupted system call.
2246 if (errno
== EINTR
) {
2254 for (i
= 0; i
< nb_fd
; i
++) {
2255 revents
= LTTNG_POLL_GETEV(&events
, i
);
2256 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2258 health_code_update();
2260 if (pollfd
== thread_quit_pipe_fd
) {
2264 /* Event on the registration socket */
2265 if (revents
& LPOLLIN
) {
2267 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2268 ERR("Client socket poll error");
2271 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2277 DBG("Wait for client response");
2279 health_code_update();
2281 sock
= lttcomm_accept_unix_sock(client_sock
);
2287 * Set the CLOEXEC flag. Return code is useless because either way, the
2290 (void) utils_set_fd_cloexec(sock
);
2292 /* Set socket option for credentials retrieval */
2293 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2298 /* Allocate context command to process the client request */
2299 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2300 if (cmd_ctx
== NULL
) {
2301 PERROR("zmalloc cmd_ctx");
2305 /* Allocate data buffer for reception */
2306 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2307 if (cmd_ctx
->lsm
== NULL
) {
2308 PERROR("zmalloc cmd_ctx->lsm");
2312 cmd_ctx
->llm
= NULL
;
2313 cmd_ctx
->session
= NULL
;
2315 health_code_update();
2318 * Data is received from the lttng client. The struct
2319 * lttcomm_session_msg (lsm) contains the command and data request of
2322 DBG("Receiving data from client ...");
2323 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2324 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2326 DBG("Nothing recv() from client... continuing");
2332 clean_command_ctx(&cmd_ctx
);
2336 health_code_update();
2338 // TODO: Validate cmd_ctx including sanity check for
2339 // security purpose.
2341 rcu_thread_online();
2343 * This function dispatch the work to the kernel or userspace tracer
2344 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2345 * informations for the client. The command context struct contains
2346 * everything this function may needs.
2348 ret
= process_client_msg(cmd_ctx
, &sock
, &sock_error
);
2349 rcu_thread_offline();
2359 * TODO: Inform client somehow of the fatal error. At
2360 * this point, ret < 0 means that a zmalloc failed
2361 * (ENOMEM). Error detected but still accept
2362 * command, unless a socket error has been
2365 clean_command_ctx(&cmd_ctx
);
2369 cmd_completion_handler
= cmd_pop_completion_handler();
2370 if (cmd_completion_handler
) {
2371 enum lttng_error_code completion_code
;
2373 completion_code
= cmd_completion_handler
->run(
2374 cmd_completion_handler
->data
);
2375 if (completion_code
!= LTTNG_OK
) {
2376 clean_command_ctx(&cmd_ctx
);
2381 health_code_update();
2384 DBG("Sending response (size: %d, retcode: %s (%d))",
2385 cmd_ctx
->lttng_msg_size
,
2386 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2387 cmd_ctx
->llm
->ret_code
);
2388 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2389 cmd_ctx
->lttng_msg_size
);
2391 ERR("Failed to send data back to client");
2394 /* End of transmission */
2402 clean_command_ctx(&cmd_ctx
);
2404 health_code_update();
2416 lttng_poll_clean(&events
);
2417 clean_command_ctx(&cmd_ctx
);
2421 unlink(config
.client_unix_sock_path
.value
);
2422 ret
= close(client_sock
);
2429 ERR("Health error occurred in %s", __func__
);
2432 health_unregister(health_sessiond
);
2434 DBG("Client thread dying");
2436 rcu_unregister_thread();
2441 bool shutdown_client_thread(void *thread_data
)
2443 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2444 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2446 return notify_thread_pipe(write_fd
) == 1;
2449 struct lttng_thread
*launch_client_thread(void)
2451 bool thread_running
;
2452 struct lttng_pipe
*client_quit_pipe
;
2453 struct lttng_thread
*thread
= NULL
;
2454 int client_sock_fd
= -1;
2456 sem_init(&thread_state
.ready
, 0, 0);
2457 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2458 if (!client_quit_pipe
) {
2462 client_sock_fd
= create_client_sock();
2463 if (client_sock_fd
< 0) {
2467 thread_state
.client_sock
= client_sock_fd
;
2468 thread
= lttng_thread_create("Client management",
2469 thread_manage_clients
,
2470 shutdown_client_thread
,
2471 cleanup_client_thread
,
2476 /* The client thread now owns the client sock fd and the quit pipe. */
2477 client_sock_fd
= -1;
2478 client_quit_pipe
= NULL
;
2481 * This thread is part of the threads that need to be fully
2482 * initialized before the session daemon is marked as "ready".
2484 thread_running
= wait_thread_status();
2485 if (!thread_running
) {
2490 if (client_sock_fd
>= 0) {
2491 if (close(client_sock_fd
)) {
2492 PERROR("Failed to close client socket");
2495 lttng_thread_put(thread
);
2496 cleanup_client_thread(client_quit_pipe
);