2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "common/buffer-view.h"
11 #include "common/compat/socket.h"
12 #include "common/dynamic-buffer.h"
13 #include "common/dynamic-array.h"
14 #include "common/payload.h"
15 #include "common/payload-view.h"
16 #include "common/sessiond-comm/sessiond-comm.h"
17 #include "lttng/lttng-error.h"
18 #include "lttng/tracker.h"
19 #include <common/compat/getenv.h>
20 #include <common/tracker.h>
21 #include <common/unix.h>
22 #include <common/utils.h>
23 #include <lttng/event-internal.h>
24 #include <lttng/session-descriptor-internal.h>
25 #include <lttng/session-internal.h>
26 #include <lttng/userspace-probe-internal.h>
33 #include "lttng-sessiond.h"
37 #include "health-sessiond.h"
38 #include "testpoint.h"
40 #include "manage-consumer.h"
45 static struct thread_state
{
51 static void set_thread_status(bool running
)
53 DBG("Marking client thread's state as %s", running
? "running" : "error");
54 thread_state
.running
= running
;
55 sem_post(&thread_state
.ready
);
58 static bool wait_thread_status(void)
60 DBG("Waiting for client thread to be ready");
61 sem_wait(&thread_state
.ready
);
62 if (thread_state
.running
) {
63 DBG("Client thread is ready");
65 ERR("Initialization of client thread failed");
68 return thread_state
.running
;
72 * Setup the outgoing data buffer for the response (llm) by allocating the
73 * right amount of memory and copying the original information from the lsm
76 * Return 0 on success, negative value on error.
78 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
79 const void *payload_buf
, size_t payload_len
,
80 const void *cmd_header_buf
, size_t cmd_header_len
)
83 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
84 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
85 const struct lttcomm_lttng_msg llm
= {
86 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
87 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
88 .cmd_header_size
= cmd_header_len
,
89 .data_size
= payload_len
,
92 lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
94 cmd_ctx
->lttng_msg_size
= total_msg_size
;
96 /* Append reply header. */
97 ret
= lttng_dynamic_buffer_append(
98 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
103 /* Append command header. */
104 if (cmd_header_len
) {
105 ret
= lttng_dynamic_buffer_append(
106 &cmd_ctx
->reply_payload
.buffer
, cmd_header_buf
,
113 /* Append payload. */
115 ret
= lttng_dynamic_buffer_append(
116 &cmd_ctx
->reply_payload
.buffer
, payload_buf
,
128 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
129 * exec or it will fail.
131 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
133 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
137 * Fork and exec a consumer daemon (consumerd).
139 * Return pid if successful else -1.
141 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
145 const char *consumer_to_use
;
146 const char *verbosity
;
149 DBG("Spawning consumerd");
156 if (config
.verbose_consumer
) {
157 verbosity
= "--verbose";
158 } else if (lttng_opt_quiet
) {
159 verbosity
= "--quiet";
164 switch (consumer_data
->type
) {
165 case LTTNG_CONSUMER_KERNEL
:
167 * Find out which consumerd to execute. We will first try the
168 * 64-bit path, then the sessiond's installation directory, and
169 * fallback on the 32-bit one,
171 DBG3("Looking for a kernel consumer at these locations:");
172 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
173 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
174 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
175 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
176 DBG3("Found location #1");
177 consumer_to_use
= config
.consumerd64_bin_path
.value
;
178 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
179 DBG3("Found location #2");
180 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
181 } else if (config
.consumerd32_bin_path
.value
&&
182 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
183 DBG3("Found location #3");
184 consumer_to_use
= config
.consumerd32_bin_path
.value
;
186 DBG("Could not find any valid consumerd executable");
190 DBG("Using kernel consumer at: %s", consumer_to_use
);
191 (void) execl(consumer_to_use
,
192 "lttng-consumerd", verbosity
, "-k",
193 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
194 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
195 "--group", config
.tracing_group_name
.value
,
198 case LTTNG_CONSUMER64_UST
:
200 if (config
.consumerd64_lib_dir
.value
) {
205 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
209 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
210 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
215 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
216 if (tmp
[0] != '\0') {
220 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
227 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
228 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
229 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
230 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
231 "--group", config
.tracing_group_name
.value
,
235 case LTTNG_CONSUMER32_UST
:
237 if (config
.consumerd32_lib_dir
.value
) {
242 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
246 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
247 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
252 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
253 if (tmp
[0] != '\0') {
257 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
264 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
265 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
266 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
267 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
268 "--group", config
.tracing_group_name
.value
,
273 ERR("unknown consumer type");
277 PERROR("Consumer execl()");
279 /* Reaching this point, we got a failure on our execl(). */
281 } else if (pid
> 0) {
284 PERROR("start consumer fork");
292 * Spawn the consumerd daemon and session daemon thread.
294 static int start_consumerd(struct consumer_data
*consumer_data
)
299 * Set the listen() state on the socket since there is a possible race
300 * between the exec() of the consumer daemon and this call if place in the
301 * consumer thread. See bug #366 for more details.
303 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
308 pthread_mutex_lock(&consumer_data
->pid_mutex
);
309 if (consumer_data
->pid
!= 0) {
310 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
314 ret
= spawn_consumerd(consumer_data
);
316 ERR("Spawning consumerd failed");
317 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
321 /* Setting up the consumer_data pid */
322 consumer_data
->pid
= ret
;
323 DBG2("Consumer pid %d", consumer_data
->pid
);
324 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
326 DBG2("Spawning consumer control thread");
327 ret
= spawn_consumer_thread(consumer_data
);
329 ERR("Fatal error spawning consumer control thread");
337 /* Cleanup already created sockets on error. */
338 if (consumer_data
->err_sock
>= 0) {
341 err
= close(consumer_data
->err_sock
);
343 PERROR("close consumer data error socket");
350 * Copy consumer output from the tracing session to the domain session. The
351 * function also applies the right modification on a per domain basis for the
352 * trace files destination directory.
354 * Should *NOT* be called with RCU read-side lock held.
356 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
359 const char *dir_name
;
360 struct consumer_output
*consumer
;
363 assert(session
->consumer
);
366 case LTTNG_DOMAIN_KERNEL
:
367 DBG3("Copying tracing session consumer output in kernel session");
369 * XXX: We should audit the session creation and what this function
370 * does "extra" in order to avoid a destroy since this function is used
371 * in the domain session creation (kernel and ust) only. Same for UST
374 if (session
->kernel_session
->consumer
) {
375 consumer_output_put(session
->kernel_session
->consumer
);
377 session
->kernel_session
->consumer
=
378 consumer_copy_output(session
->consumer
);
379 /* Ease our life a bit for the next part */
380 consumer
= session
->kernel_session
->consumer
;
381 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
383 case LTTNG_DOMAIN_JUL
:
384 case LTTNG_DOMAIN_LOG4J
:
385 case LTTNG_DOMAIN_PYTHON
:
386 case LTTNG_DOMAIN_UST
:
387 DBG3("Copying tracing session consumer output in UST session");
388 if (session
->ust_session
->consumer
) {
389 consumer_output_put(session
->ust_session
->consumer
);
391 session
->ust_session
->consumer
=
392 consumer_copy_output(session
->consumer
);
393 /* Ease our life a bit for the next part */
394 consumer
= session
->ust_session
->consumer
;
395 dir_name
= DEFAULT_UST_TRACE_DIR
;
398 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
402 /* Append correct directory to subdir */
403 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
404 sizeof(consumer
->domain_subdir
));
409 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
417 * Create an UST session and add it to the session ust list.
419 * Should *NOT* be called with RCU read-side lock held.
421 static int create_ust_session(struct ltt_session
*session
,
422 const struct lttng_domain
*domain
)
425 struct ltt_ust_session
*lus
= NULL
;
429 assert(session
->consumer
);
431 switch (domain
->type
) {
432 case LTTNG_DOMAIN_JUL
:
433 case LTTNG_DOMAIN_LOG4J
:
434 case LTTNG_DOMAIN_PYTHON
:
435 case LTTNG_DOMAIN_UST
:
438 ERR("Unknown UST domain on create session %d", domain
->type
);
439 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
443 DBG("Creating UST session");
445 lus
= trace_ust_create_session(session
->id
);
447 ret
= LTTNG_ERR_UST_SESS_FAIL
;
451 lus
->uid
= session
->uid
;
452 lus
->gid
= session
->gid
;
453 lus
->output_traces
= session
->output_traces
;
454 lus
->snapshot_mode
= session
->snapshot_mode
;
455 lus
->live_timer_interval
= session
->live_timer
;
456 session
->ust_session
= lus
;
457 if (session
->shm_path
[0]) {
458 strncpy(lus
->root_shm_path
, session
->shm_path
,
459 sizeof(lus
->root_shm_path
));
460 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
461 strncpy(lus
->shm_path
, session
->shm_path
,
462 sizeof(lus
->shm_path
));
463 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
464 strncat(lus
->shm_path
, "/ust",
465 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
467 /* Copy session output to the newly created UST session */
468 ret
= copy_session_consumer(domain
->type
, session
);
469 if (ret
!= LTTNG_OK
) {
477 session
->ust_session
= NULL
;
482 * Create a kernel tracer session then create the default channel.
484 static int create_kernel_session(struct ltt_session
*session
)
488 DBG("Creating kernel session");
490 ret
= kernel_create_session(session
);
492 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
496 /* Code flow safety */
497 assert(session
->kernel_session
);
499 /* Copy session output to the newly created Kernel session */
500 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
501 if (ret
!= LTTNG_OK
) {
505 session
->kernel_session
->uid
= session
->uid
;
506 session
->kernel_session
->gid
= session
->gid
;
507 session
->kernel_session
->output_traces
= session
->output_traces
;
508 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
509 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
514 trace_kernel_destroy_session(session
->kernel_session
);
515 session
->kernel_session
= NULL
;
521 * Count number of session permitted by uid/gid.
523 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
526 struct ltt_session
*session
;
527 const struct ltt_session_list
*session_list
= session_get_list();
529 DBG("Counting number of available session for UID %d GID %d",
531 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
532 if (!session_get(session
)) {
535 session_lock(session
);
536 /* Only count the sessions the user can control. */
537 if (session_access_ok(session
, uid
, gid
) &&
538 !session
->destroyed
) {
541 session_unlock(session
);
542 session_put(session
);
547 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
548 int *sock_error
, struct lttng_event
*event
)
551 struct lttng_userspace_probe_location
*probe_location
;
552 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
553 struct lttng_dynamic_buffer probe_location_buffer
;
554 struct lttng_buffer_view buffer_view
;
557 * Create a buffer to store the serialized version of the probe
560 lttng_dynamic_buffer_init(&probe_location_buffer
);
561 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
562 cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
);
564 ret
= LTTNG_ERR_NOMEM
;
569 * Receive the probe location.
571 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
572 probe_location_buffer
.size
);
574 DBG("Nothing recv() from client var len data... continuing");
576 lttng_dynamic_buffer_reset(&probe_location_buffer
);
577 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
581 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
582 &probe_location_buffer
, 0, probe_location_buffer
.size
);
585 * Extract the probe location from the serialized version.
587 ret
= lttng_userspace_probe_location_create_from_buffer(
588 &buffer_view
, &probe_location
);
590 WARN("Failed to create a userspace probe location from the received buffer");
591 lttng_dynamic_buffer_reset( &probe_location_buffer
);
592 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
597 * Receive the file descriptor to the target binary from the client.
599 DBG("Receiving userspace probe target FD from client ...");
600 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
602 DBG("Nothing recv() from client userspace probe fd... continuing");
604 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
609 * Set the file descriptor received from the client through the unix
610 * socket in the probe location.
612 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
614 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
619 * From the kernel tracer's perspective, all userspace probe event types
620 * are all the same: a file and an offset.
622 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
623 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
624 ret
= lttng_userspace_probe_location_function_set_binary_fd(
627 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
628 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
632 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
637 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
641 /* Attach the probe location to the event. */
642 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
644 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
648 lttng_dynamic_buffer_reset(&probe_location_buffer
);
654 * Version of setup_lttng_msg() without command header.
656 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
657 void *payload_buf
, size_t payload_len
)
659 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
663 * Check if the current kernel tracer supports the session rotation feature.
664 * Return 1 if it does, 0 otherwise.
666 static int check_rotate_compatible(void)
670 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
671 DBG("Kernel tracer version is not compatible with the rotation feature");
679 * Send data on a unix socket using the liblttsessiondcomm API.
681 * Return lttcomm error code.
683 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
687 /* Check valid length */
688 if (view
->buffer
.size
== 0) {
693 ret
= lttcomm_send_unix_sock(
694 sock
, view
->buffer
.data
, view
->buffer
.size
);
699 if (lttng_dynamic_array_get_count(&view
->_fds
) > 0) {
700 ret
= lttcomm_send_fds_unix_sock(sock
,
701 (const int *) view
->_fds
.buffer
.data
,
702 lttng_dynamic_array_get_count(&view
->_fds
));
710 * Process the command requested by the lttng client within the command
711 * context structure. This function make sure that the return structure (llm)
712 * is set and ready for transmission before returning.
714 * Return any error encountered or 0 for success.
716 * "sock" is only used for special-case var. len data.
717 * A command may assume the ownership of the socket, in which case its value
718 * should be set to -1.
720 * Should *NOT* be called with RCU read-side lock held.
722 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
726 int need_tracing_session
= 1;
729 DBG("Processing client command %d", cmd_ctx
->lsm
.cmd_type
);
731 assert(!rcu_read_ongoing());
735 switch (cmd_ctx
->lsm
.cmd_type
) {
736 case LTTNG_CREATE_SESSION_EXT
:
737 case LTTNG_DESTROY_SESSION
:
738 case LTTNG_LIST_SESSIONS
:
739 case LTTNG_LIST_DOMAINS
:
740 case LTTNG_START_TRACE
:
741 case LTTNG_STOP_TRACE
:
742 case LTTNG_DATA_PENDING
:
743 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
744 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
745 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
746 case LTTNG_SNAPSHOT_RECORD
:
747 case LTTNG_SAVE_SESSION
:
748 case LTTNG_SET_SESSION_SHM_PATH
:
749 case LTTNG_REGENERATE_METADATA
:
750 case LTTNG_REGENERATE_STATEDUMP
:
751 case LTTNG_REGISTER_TRIGGER
:
752 case LTTNG_UNREGISTER_TRIGGER
:
753 case LTTNG_ROTATE_SESSION
:
754 case LTTNG_ROTATION_GET_INFO
:
755 case LTTNG_ROTATION_SET_SCHEDULE
:
756 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
757 case LTTNG_CLEAR_SESSION
:
764 if (config
.no_kernel
&& need_domain
765 && cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
767 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
769 ret
= LTTNG_ERR_KERN_NA
;
774 /* Deny register consumer if we already have a spawned consumer. */
775 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
776 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
777 if (kconsumer_data
.pid
> 0) {
778 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
779 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
782 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
786 * Check for command that don't needs to allocate a returned payload. We do
787 * this here so we don't have to make the call for no payload at each
790 switch(cmd_ctx
->lsm
.cmd_type
) {
791 case LTTNG_LIST_SESSIONS
:
792 case LTTNG_LIST_TRACEPOINTS
:
793 case LTTNG_LIST_TRACEPOINT_FIELDS
:
794 case LTTNG_LIST_DOMAINS
:
795 case LTTNG_LIST_CHANNELS
:
796 case LTTNG_LIST_EVENTS
:
797 case LTTNG_LIST_SYSCALLS
:
798 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
799 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
800 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
801 case LTTNG_DATA_PENDING
:
802 case LTTNG_ROTATE_SESSION
:
803 case LTTNG_ROTATION_GET_INFO
:
806 /* Setup lttng message with no payload */
807 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
809 /* This label does not try to unlock the session */
810 goto init_setup_error
;
814 /* Commands that DO NOT need a session. */
815 switch (cmd_ctx
->lsm
.cmd_type
) {
816 case LTTNG_CREATE_SESSION_EXT
:
817 case LTTNG_LIST_SESSIONS
:
818 case LTTNG_LIST_TRACEPOINTS
:
819 case LTTNG_LIST_SYSCALLS
:
820 case LTTNG_LIST_TRACEPOINT_FIELDS
:
821 case LTTNG_SAVE_SESSION
:
822 case LTTNG_REGISTER_TRIGGER
:
823 case LTTNG_UNREGISTER_TRIGGER
:
824 need_tracing_session
= 0;
827 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
829 * We keep the session list lock across _all_ commands
830 * for now, because the per-session lock does not
831 * handle teardown properly.
834 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
835 if (cmd_ctx
->session
== NULL
) {
836 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
839 /* Acquire lock for the session */
840 session_lock(cmd_ctx
->session
);
846 * Commands that need a valid session but should NOT create one if none
847 * exists. Instead of creating one and destroying it when the command is
848 * handled, process that right before so we save some round trip in useless
851 switch (cmd_ctx
->lsm
.cmd_type
) {
852 case LTTNG_DISABLE_CHANNEL
:
853 case LTTNG_DISABLE_EVENT
:
854 switch (cmd_ctx
->lsm
.domain
.type
) {
855 case LTTNG_DOMAIN_KERNEL
:
856 if (!cmd_ctx
->session
->kernel_session
) {
857 ret
= LTTNG_ERR_NO_CHANNEL
;
861 case LTTNG_DOMAIN_JUL
:
862 case LTTNG_DOMAIN_LOG4J
:
863 case LTTNG_DOMAIN_PYTHON
:
864 case LTTNG_DOMAIN_UST
:
865 if (!cmd_ctx
->session
->ust_session
) {
866 ret
= LTTNG_ERR_NO_CHANNEL
;
871 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
883 * Check domain type for specific "pre-action".
885 switch (cmd_ctx
->lsm
.domain
.type
) {
886 case LTTNG_DOMAIN_KERNEL
:
888 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
892 /* Kernel tracer check */
893 if (!kernel_tracer_is_initialized()) {
894 /* Basically, load kernel tracer modules */
895 ret
= init_kernel_tracer();
901 /* Consumer is in an ERROR state. Report back to client */
902 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
903 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
907 /* Need a session for kernel command */
908 if (need_tracing_session
) {
909 if (cmd_ctx
->session
->kernel_session
== NULL
) {
910 ret
= create_kernel_session(cmd_ctx
->session
);
911 if (ret
!= LTTNG_OK
) {
912 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
917 /* Start the kernel consumer daemon */
918 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
919 if (kconsumer_data
.pid
== 0 &&
920 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
921 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
922 ret
= start_consumerd(&kconsumer_data
);
924 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
927 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
929 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
933 * The consumer was just spawned so we need to add the socket to
934 * the consumer output of the session if exist.
936 ret
= consumer_create_socket(&kconsumer_data
,
937 cmd_ctx
->session
->kernel_session
->consumer
);
944 case LTTNG_DOMAIN_JUL
:
945 case LTTNG_DOMAIN_LOG4J
:
946 case LTTNG_DOMAIN_PYTHON
:
947 case LTTNG_DOMAIN_UST
:
949 if (!ust_app_supported()) {
950 ret
= LTTNG_ERR_NO_UST
;
953 /* Consumer is in an ERROR state. Report back to client */
954 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
955 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
959 if (need_tracing_session
) {
960 /* Create UST session if none exist. */
961 if (cmd_ctx
->session
->ust_session
== NULL
) {
962 ret
= create_ust_session(cmd_ctx
->session
,
963 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
));
964 if (ret
!= LTTNG_OK
) {
969 /* Start the UST consumer daemons */
971 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
972 if (config
.consumerd64_bin_path
.value
&&
973 ustconsumer64_data
.pid
== 0 &&
974 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
975 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
976 ret
= start_consumerd(&ustconsumer64_data
);
978 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
979 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
983 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
984 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
986 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
990 * Setup socket for consumer 64 bit. No need for atomic access
991 * since it was set above and can ONLY be set in this thread.
993 ret
= consumer_create_socket(&ustconsumer64_data
,
994 cmd_ctx
->session
->ust_session
->consumer
);
1000 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
1001 if (config
.consumerd32_bin_path
.value
&&
1002 ustconsumer32_data
.pid
== 0 &&
1003 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1004 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1005 ret
= start_consumerd(&ustconsumer32_data
);
1007 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1008 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
1012 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
1013 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1015 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1019 * Setup socket for consumer 32 bit. No need for atomic access
1020 * since it was set above and can ONLY be set in this thread.
1022 ret
= consumer_create_socket(&ustconsumer32_data
,
1023 cmd_ctx
->session
->ust_session
->consumer
);
1035 /* Validate consumer daemon state when start/stop trace command */
1036 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1037 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1038 switch (cmd_ctx
->lsm
.domain
.type
) {
1039 case LTTNG_DOMAIN_NONE
:
1041 case LTTNG_DOMAIN_JUL
:
1042 case LTTNG_DOMAIN_LOG4J
:
1043 case LTTNG_DOMAIN_PYTHON
:
1044 case LTTNG_DOMAIN_UST
:
1045 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1046 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1050 case LTTNG_DOMAIN_KERNEL
:
1051 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1052 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1057 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1063 * Check that the UID or GID match that of the tracing session.
1064 * The root user can interact with all sessions.
1066 if (need_tracing_session
) {
1067 if (!session_access_ok(cmd_ctx
->session
,
1068 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1069 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1070 cmd_ctx
->session
->destroyed
) {
1071 ret
= LTTNG_ERR_EPERM
;
1077 * Send relayd information to consumer as soon as we have a domain and a
1080 if (cmd_ctx
->session
&& need_domain
) {
1082 * Setup relayd if not done yet. If the relayd information was already
1083 * sent to the consumer, this call will gracefully return.
1085 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1086 if (ret
!= LTTNG_OK
) {
1091 /* Process by command type */
1092 switch (cmd_ctx
->lsm
.cmd_type
) {
1093 case LTTNG_ADD_CONTEXT
:
1096 * An LTTNG_ADD_CONTEXT command might have a supplementary
1097 * payload if the context being added is an application context.
1099 if (cmd_ctx
->lsm
.u
.context
.ctx
.ctx
==
1100 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1101 char *provider_name
= NULL
, *context_name
= NULL
;
1102 size_t provider_name_len
=
1103 cmd_ctx
->lsm
.u
.context
.provider_name_len
;
1104 size_t context_name_len
=
1105 cmd_ctx
->lsm
.u
.context
.context_name_len
;
1107 if (provider_name_len
== 0 || context_name_len
== 0) {
1109 * Application provider and context names MUST
1112 ret
= -LTTNG_ERR_INVALID
;
1116 provider_name
= zmalloc(provider_name_len
+ 1);
1117 if (!provider_name
) {
1118 ret
= -LTTNG_ERR_NOMEM
;
1121 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
=
1124 context_name
= zmalloc(context_name_len
+ 1);
1125 if (!context_name
) {
1126 ret
= -LTTNG_ERR_NOMEM
;
1127 goto error_add_context
;
1129 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1132 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1135 goto error_add_context
;
1138 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1141 goto error_add_context
;
1146 * cmd_add_context assumes ownership of the provider and context
1149 ret
= cmd_add_context(cmd_ctx
->session
,
1150 cmd_ctx
->lsm
.domain
.type
,
1151 cmd_ctx
->lsm
.u
.context
.channel_name
,
1152 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.context
.ctx
),
1153 kernel_poll_pipe
[1]);
1155 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1156 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1158 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
);
1159 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1165 case LTTNG_DISABLE_CHANNEL
:
1167 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1168 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1171 case LTTNG_DISABLE_EVENT
:
1175 * FIXME: handle filter; for now we just receive the filter's
1176 * bytecode along with the filter expression which are sent by
1177 * liblttng-ctl and discard them.
1179 * This fixes an issue where the client may block while sending
1180 * the filter payload and encounter an error because the session
1181 * daemon closes the socket without ever handling this data.
1183 size_t count
= cmd_ctx
->lsm
.u
.disable
.expression_len
+
1184 cmd_ctx
->lsm
.u
.disable
.bytecode_len
;
1187 char data
[LTTNG_FILTER_MAX_LEN
];
1189 DBG("Discarding disable event command payload of size %zu", count
);
1191 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1192 count
> sizeof(data
) ? sizeof(data
) : count
);
1197 count
-= (size_t) ret
;
1200 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1201 cmd_ctx
->lsm
.u
.disable
.channel_name
,
1202 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.disable
.event
));
1205 case LTTNG_ENABLE_CHANNEL
:
1207 cmd_ctx
->lsm
.u
.channel
.chan
.attr
.extended
.ptr
=
1208 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
.u
.channel
.extended
;
1209 ret
= cmd_enable_channel(cmd_ctx
->session
,
1210 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1211 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.channel
.chan
),
1212 kernel_poll_pipe
[1]);
1215 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1216 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1218 struct lttng_dynamic_buffer payload
;
1219 struct lttng_buffer_view payload_view
;
1220 const bool add_value
=
1221 cmd_ctx
->lsm
.cmd_type
==
1222 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1223 const size_t name_len
=
1224 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1226 const enum lttng_domain_type domain_type
=
1227 (enum lttng_domain_type
)
1228 cmd_ctx
->lsm
.domain
.type
;
1229 const enum lttng_process_attr process_attr
=
1230 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1231 .process_attr_tracker_add_remove_include_value
1233 const enum lttng_process_attr_value_type value_type
=
1234 (enum lttng_process_attr_value_type
) cmd_ctx
1236 .process_attr_tracker_add_remove_include_value
1238 struct process_attr_value
*value
;
1239 enum lttng_error_code ret_code
;
1241 /* Receive remaining variable length payload if applicable. */
1242 if (name_len
> LOGIN_NAME_MAX
) {
1244 * POSIX mandates user and group names that are at least
1245 * 8 characters long. Note that although shadow-utils
1246 * (useradd, groupaadd, etc.) use 32 chars as their
1247 * limit (from bits/utmp.h, UT_NAMESIZE),
1248 * LOGIN_NAME_MAX is defined to 256.
1250 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %d",
1251 add_value
? "addition" : "removal",
1252 name_len
, LOGIN_NAME_MAX
);
1253 ret
= LTTNG_ERR_INVALID
;
1257 lttng_dynamic_buffer_init(&payload
);
1258 if (name_len
!= 0) {
1260 * Receive variable payload for user/group name
1263 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1265 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1266 add_value
? "add" : "remove");
1267 ret
= LTTNG_ERR_NOMEM
;
1268 goto error_add_remove_tracker_value
;
1271 ret
= lttcomm_recv_unix_sock(
1272 *sock
, payload
.data
, name_len
);
1274 ERR("Failed to receive payload of %s process attribute tracker value argument",
1275 add_value
? "add" : "remove");
1277 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1278 goto error_add_remove_tracker_value
;
1282 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1283 &payload
, 0, name_len
);
1285 * Validate the value type and domains are legal for the process
1286 * attribute tracker that is specified and convert the value to
1287 * add/remove to the internal sessiond representation.
1289 ret_code
= process_attr_value_from_comm(domain_type
,
1290 process_attr
, value_type
,
1291 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1293 &payload_view
, &value
);
1294 if (ret_code
!= LTTNG_OK
) {
1296 goto error_add_remove_tracker_value
;
1300 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1301 cmd_ctx
->session
, domain_type
,
1302 process_attr
, value
);
1304 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1305 cmd_ctx
->session
, domain_type
,
1306 process_attr
, value
);
1308 process_attr_value_destroy(value
);
1309 error_add_remove_tracker_value
:
1310 lttng_dynamic_buffer_reset(&payload
);
1313 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1315 enum lttng_tracking_policy tracking_policy
;
1316 const enum lttng_domain_type domain_type
=
1317 (enum lttng_domain_type
)
1318 cmd_ctx
->lsm
.domain
.type
;
1319 const enum lttng_process_attr process_attr
=
1320 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1321 .process_attr_tracker_get_tracking_policy
1324 ret
= cmd_process_attr_tracker_get_tracking_policy(
1325 cmd_ctx
->session
, domain_type
, process_attr
,
1327 if (ret
!= LTTNG_OK
) {
1331 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1332 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1334 ret
= LTTNG_ERR_NOMEM
;
1340 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1342 const enum lttng_tracking_policy tracking_policy
=
1343 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1344 .process_attr_tracker_set_tracking_policy
1346 const enum lttng_domain_type domain_type
=
1347 (enum lttng_domain_type
)
1348 cmd_ctx
->lsm
.domain
.type
;
1349 const enum lttng_process_attr process_attr
=
1350 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1351 .process_attr_tracker_set_tracking_policy
1354 ret
= cmd_process_attr_tracker_set_tracking_policy(
1355 cmd_ctx
->session
, domain_type
, process_attr
,
1357 if (ret
!= LTTNG_OK
) {
1362 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1364 struct lttng_process_attr_values
*values
;
1365 struct lttng_dynamic_buffer reply
;
1366 const enum lttng_domain_type domain_type
=
1367 (enum lttng_domain_type
)
1368 cmd_ctx
->lsm
.domain
.type
;
1369 const enum lttng_process_attr process_attr
=
1370 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1371 .process_attr_tracker_get_inclusion_set
1374 ret
= cmd_process_attr_tracker_get_inclusion_set(
1375 cmd_ctx
->session
, domain_type
, process_attr
,
1377 if (ret
!= LTTNG_OK
) {
1381 lttng_dynamic_buffer_init(&reply
);
1382 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1384 goto error_tracker_get_inclusion_set
;
1387 ret
= setup_lttng_msg_no_cmd_header(
1388 cmd_ctx
, reply
.data
, reply
.size
);
1390 ret
= LTTNG_ERR_NOMEM
;
1391 goto error_tracker_get_inclusion_set
;
1395 error_tracker_get_inclusion_set
:
1396 lttng_process_attr_values_destroy(values
);
1397 lttng_dynamic_buffer_reset(&reply
);
1400 case LTTNG_ENABLE_EVENT
:
1402 struct lttng_event
*ev
= NULL
;
1403 struct lttng_event_exclusion
*exclusion
= NULL
;
1404 struct lttng_filter_bytecode
*bytecode
= NULL
;
1405 char *filter_expression
= NULL
;
1407 /* Handle exclusion events and receive it from the client. */
1408 if (cmd_ctx
->lsm
.u
.enable
.exclusion_count
> 0) {
1409 size_t count
= cmd_ctx
->lsm
.u
.enable
.exclusion_count
;
1411 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1412 (count
* LTTNG_SYMBOL_NAME_LEN
));
1414 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1418 DBG("Receiving var len exclusion event list from client ...");
1419 exclusion
->count
= count
;
1420 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1421 count
* LTTNG_SYMBOL_NAME_LEN
);
1423 DBG("Nothing recv() from client var len data... continuing");
1426 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1431 /* Get filter expression from client. */
1432 if (cmd_ctx
->lsm
.u
.enable
.expression_len
> 0) {
1433 size_t expression_len
=
1434 cmd_ctx
->lsm
.u
.enable
.expression_len
;
1436 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1437 ret
= LTTNG_ERR_FILTER_INVAL
;
1442 filter_expression
= zmalloc(expression_len
);
1443 if (!filter_expression
) {
1445 ret
= LTTNG_ERR_FILTER_NOMEM
;
1449 /* Receive var. len. data */
1450 DBG("Receiving var len filter's expression from client ...");
1451 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1454 DBG("Nothing recv() from client var len data... continuing");
1456 free(filter_expression
);
1458 ret
= LTTNG_ERR_FILTER_INVAL
;
1463 /* Handle filter and get bytecode from client. */
1464 if (cmd_ctx
->lsm
.u
.enable
.bytecode_len
> 0) {
1465 size_t bytecode_len
= cmd_ctx
->lsm
.u
.enable
.bytecode_len
;
1467 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1468 ret
= LTTNG_ERR_FILTER_INVAL
;
1469 free(filter_expression
);
1474 bytecode
= zmalloc(bytecode_len
);
1476 free(filter_expression
);
1478 ret
= LTTNG_ERR_FILTER_NOMEM
;
1482 /* Receive var. len. data */
1483 DBG("Receiving var len filter's bytecode from client ...");
1484 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1486 DBG("Nothing recv() from client var len data... continuing");
1488 free(filter_expression
);
1491 ret
= LTTNG_ERR_FILTER_INVAL
;
1495 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1496 free(filter_expression
);
1499 ret
= LTTNG_ERR_FILTER_INVAL
;
1504 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.enable
.event
));
1506 DBG("Failed to copy event: %s",
1507 cmd_ctx
->lsm
.u
.enable
.event
.name
);
1508 free(filter_expression
);
1511 ret
= LTTNG_ERR_NOMEM
;
1516 if (cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
> 0) {
1517 /* Expect a userspace probe description. */
1518 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1520 free(filter_expression
);
1523 lttng_event_destroy(ev
);
1528 ret
= cmd_enable_event(cmd_ctx
->session
,
1529 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1530 cmd_ctx
->lsm
.u
.enable
.channel_name
,
1532 filter_expression
, bytecode
, exclusion
,
1533 kernel_poll_pipe
[1]);
1534 lttng_event_destroy(ev
);
1537 case LTTNG_LIST_TRACEPOINTS
:
1539 struct lttng_event
*events
;
1542 session_lock_list();
1543 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &events
);
1544 session_unlock_list();
1545 if (nb_events
< 0) {
1546 /* Return value is a negative lttng_error_code. */
1552 * Setup lttng message with payload size set to the event list size in
1553 * bytes and then copy list into the llm payload.
1555 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1556 sizeof(struct lttng_event
) * nb_events
);
1566 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1568 struct lttng_event_field
*fields
;
1571 session_lock_list();
1572 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1574 session_unlock_list();
1575 if (nb_fields
< 0) {
1576 /* Return value is a negative lttng_error_code. */
1582 * Setup lttng message with payload size set to the event list size in
1583 * bytes and then copy list into the llm payload.
1585 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1586 sizeof(struct lttng_event_field
) * nb_fields
);
1596 case LTTNG_LIST_SYSCALLS
:
1598 struct lttng_event
*events
;
1601 nb_events
= cmd_list_syscalls(&events
);
1602 if (nb_events
< 0) {
1603 /* Return value is a negative lttng_error_code. */
1609 * Setup lttng message with payload size set to the event list size in
1610 * bytes and then copy list into the llm payload.
1612 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1613 sizeof(struct lttng_event
) * nb_events
);
1623 case LTTNG_SET_CONSUMER_URI
:
1626 struct lttng_uri
*uris
;
1628 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1629 len
= nb_uri
* sizeof(struct lttng_uri
);
1632 ret
= LTTNG_ERR_INVALID
;
1636 uris
= zmalloc(len
);
1638 ret
= LTTNG_ERR_FATAL
;
1642 /* Receive variable len data */
1643 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1644 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1646 DBG("No URIs received from client... continuing");
1648 ret
= LTTNG_ERR_SESSION_FAIL
;
1653 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1655 if (ret
!= LTTNG_OK
) {
1662 case LTTNG_START_TRACE
:
1665 * On the first start, if we have a kernel session and we have
1666 * enabled time or size-based rotations, we have to make sure
1667 * the kernel tracer supports it.
1669 if (!cmd_ctx
->session
->has_been_started
&& \
1670 cmd_ctx
->session
->kernel_session
&& \
1671 (cmd_ctx
->session
->rotate_timer_period
|| \
1672 cmd_ctx
->session
->rotate_size
) && \
1673 !check_rotate_compatible()) {
1674 DBG("Kernel tracer version is not compatible with the rotation feature");
1675 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1678 ret
= cmd_start_trace(cmd_ctx
->session
);
1681 case LTTNG_STOP_TRACE
:
1683 ret
= cmd_stop_trace(cmd_ctx
->session
);
1686 case LTTNG_DESTROY_SESSION
:
1688 ret
= cmd_destroy_session(cmd_ctx
->session
,
1689 notification_thread_handle
,
1693 case LTTNG_LIST_DOMAINS
:
1696 struct lttng_domain
*domains
= NULL
;
1698 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1700 /* Return value is a negative lttng_error_code. */
1705 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1706 nb_dom
* sizeof(struct lttng_domain
));
1716 case LTTNG_LIST_CHANNELS
:
1718 ssize_t payload_size
;
1719 struct lttng_channel
*channels
= NULL
;
1721 payload_size
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1722 cmd_ctx
->session
, &channels
);
1723 if (payload_size
< 0) {
1724 /* Return value is a negative lttng_error_code. */
1725 ret
= -payload_size
;
1729 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1740 case LTTNG_LIST_EVENTS
:
1743 struct lttng_event
*events
= NULL
;
1744 struct lttcomm_event_command_header cmd_header
;
1747 memset(&cmd_header
, 0, sizeof(cmd_header
));
1748 /* Extended infos are included at the end of events */
1749 nb_event
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1750 cmd_ctx
->session
, cmd_ctx
->lsm
.u
.list
.channel_name
,
1751 &events
, &total_size
);
1754 /* Return value is a negative lttng_error_code. */
1759 cmd_header
.nb_events
= nb_event
;
1760 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1761 &cmd_header
, sizeof(cmd_header
));
1771 case LTTNG_LIST_SESSIONS
:
1773 unsigned int nr_sessions
;
1774 void *sessions_payload
;
1777 session_lock_list();
1778 nr_sessions
= lttng_sessions_count(
1779 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1780 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1782 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1783 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1784 sessions_payload
= zmalloc(payload_len
);
1786 if (!sessions_payload
) {
1787 session_unlock_list();
1792 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1793 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1794 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1795 session_unlock_list();
1797 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1799 free(sessions_payload
);
1808 case LTTNG_REGISTER_CONSUMER
:
1810 struct consumer_data
*cdata
;
1812 switch (cmd_ctx
->lsm
.domain
.type
) {
1813 case LTTNG_DOMAIN_KERNEL
:
1814 cdata
= &kconsumer_data
;
1817 ret
= LTTNG_ERR_UND
;
1821 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1822 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1825 case LTTNG_DATA_PENDING
:
1828 uint8_t pending_ret_byte
;
1830 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1835 * This function may returns 0 or 1 to indicate whether or not
1836 * there is data pending. In case of error, it should return an
1837 * LTTNG_ERR code. However, some code paths may still return
1838 * a nondescript error code, which we handle by returning an
1841 if (pending_ret
== 0 || pending_ret
== 1) {
1843 * ret will be set to LTTNG_OK at the end of
1846 } else if (pending_ret
< 0) {
1847 ret
= LTTNG_ERR_UNK
;
1854 pending_ret_byte
= (uint8_t) pending_ret
;
1856 /* 1 byte to return whether or not data is pending */
1857 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1858 &pending_ret_byte
, 1);
1867 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1869 uint32_t snapshot_id
;
1870 struct lttcomm_lttng_output_id reply
;
1872 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1873 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
),
1875 if (ret
!= LTTNG_OK
) {
1878 reply
.id
= snapshot_id
;
1880 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1886 /* Copy output list into message payload */
1890 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1892 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1893 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
));
1896 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1899 struct lttng_snapshot_output
*outputs
= NULL
;
1901 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1902 if (nb_output
< 0) {
1907 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1908 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1909 nb_output
* sizeof(struct lttng_snapshot_output
));
1919 case LTTNG_SNAPSHOT_RECORD
:
1921 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1922 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_record
.output
),
1923 cmd_ctx
->lsm
.u
.snapshot_record
.wait
);
1926 case LTTNG_CREATE_SESSION_EXT
:
1928 struct lttng_dynamic_buffer payload
;
1929 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1931 lttng_dynamic_buffer_init(&payload
);
1932 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1933 if (ret
!= LTTNG_OK
) {
1937 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1940 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1941 lttng_session_descriptor_destroy(return_descriptor
);
1942 ret
= LTTNG_ERR_NOMEM
;
1945 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1948 lttng_session_descriptor_destroy(return_descriptor
);
1949 ret
= LTTNG_ERR_NOMEM
;
1952 lttng_dynamic_buffer_reset(&payload
);
1953 lttng_session_descriptor_destroy(return_descriptor
);
1957 case LTTNG_SAVE_SESSION
:
1959 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
1963 case LTTNG_SET_SESSION_SHM_PATH
:
1965 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1966 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
1969 case LTTNG_REGENERATE_METADATA
:
1971 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1974 case LTTNG_REGENERATE_STATEDUMP
:
1976 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1979 case LTTNG_REGISTER_TRIGGER
:
1981 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
1982 notification_thread_handle
);
1985 case LTTNG_UNREGISTER_TRIGGER
:
1987 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
1988 notification_thread_handle
);
1991 case LTTNG_ROTATE_SESSION
:
1993 struct lttng_rotate_session_return rotate_return
;
1995 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1997 memset(&rotate_return
, 0, sizeof(rotate_return
));
1998 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1999 DBG("Kernel tracer version is not compatible with the rotation feature");
2000 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2004 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2006 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2012 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2013 sizeof(rotate_return
));
2022 case LTTNG_ROTATION_GET_INFO
:
2024 struct lttng_rotation_get_info_return get_info_return
;
2026 memset(&get_info_return
, 0, sizeof(get_info_return
));
2027 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2028 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2034 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2035 sizeof(get_info_return
));
2044 case LTTNG_ROTATION_SET_SCHEDULE
:
2047 enum lttng_rotation_schedule_type schedule_type
;
2050 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2051 DBG("Kernel tracer version does not support session rotations");
2052 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2056 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2057 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2058 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2060 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
2064 notification_thread_handle
);
2065 if (ret
!= LTTNG_OK
) {
2071 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2073 struct lttng_session_list_schedules_return schedules
= {
2074 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2075 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2076 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2077 .size
.value
= cmd_ctx
->session
->rotate_size
,
2080 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2090 case LTTNG_CLEAR_SESSION
:
2092 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2096 ret
= LTTNG_ERR_UND
;
2101 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2102 DBG("Missing llm header, creating one.");
2103 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2107 /* Set return code */
2108 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2110 if (cmd_ctx
->session
) {
2111 session_unlock(cmd_ctx
->session
);
2112 session_put(cmd_ctx
->session
);
2113 cmd_ctx
->session
= NULL
;
2115 if (need_tracing_session
) {
2116 session_unlock_list();
2119 assert(!rcu_read_ongoing());
2123 static int create_client_sock(void)
2125 int ret
, client_sock
;
2126 const mode_t old_umask
= umask(0);
2128 /* Create client tool unix socket */
2129 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2130 if (client_sock
< 0) {
2131 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2136 /* Set the cloexec flag */
2137 ret
= utils_set_fd_cloexec(client_sock
);
2139 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2140 "Continuing but note that the consumer daemon will have a "
2141 "reference to this socket on exec()", client_sock
);
2144 /* File permission MUST be 660 */
2145 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2147 ERR("Set file permissions failed: %s",
2148 config
.client_unix_sock_path
.value
);
2150 (void) lttcomm_close_unix_sock(client_sock
);
2154 DBG("Created client socket (fd = %i)", client_sock
);
2161 static void cleanup_client_thread(void *data
)
2163 struct lttng_pipe
*quit_pipe
= data
;
2165 lttng_pipe_destroy(quit_pipe
);
2168 static void thread_init_cleanup(void *data
)
2170 set_thread_status(false);
2174 * This thread manage all clients request using the unix client socket for
2177 static void *thread_manage_clients(void *data
)
2179 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2181 uint32_t revents
, nb_fd
;
2182 struct lttng_poll_event events
;
2183 const int client_sock
= thread_state
.client_sock
;
2184 struct lttng_pipe
*quit_pipe
= data
;
2185 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2186 struct command_ctx cmd_ctx
= {};
2188 DBG("[thread] Manage client started");
2190 lttng_payload_init(&cmd_ctx
.reply_payload
);
2192 is_root
= (getuid() == 0);
2194 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2196 rcu_register_thread();
2198 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2200 health_code_update();
2202 ret
= lttcomm_listen_unix_sock(client_sock
);
2208 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2209 * more will be added to this poll set.
2211 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2213 goto error_create_poll
;
2216 /* Add the application registration socket */
2217 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2222 /* Add thread quit pipe */
2223 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2228 /* Set state as running. */
2229 set_thread_status(true);
2230 pthread_cleanup_pop(0);
2232 /* This testpoint is after we signal readiness to the parent. */
2233 if (testpoint(sessiond_thread_manage_clients
)) {
2237 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2241 health_code_update();
2244 const struct cmd_completion_handler
*cmd_completion_handler
;
2246 cmd_ctx
.creds
= (lttng_sock_cred
) {
2250 cmd_ctx
.session
= NULL
;
2251 lttng_dynamic_buffer_set_size(&cmd_ctx
.reply_payload
.buffer
, 0);
2252 lttng_dynamic_array_clear(&cmd_ctx
.reply_payload
._fds
);
2254 DBG("Accepting client command ...");
2256 /* Inifinite blocking call, waiting for transmission */
2258 health_poll_entry();
2259 ret
= lttng_poll_wait(&events
, -1);
2263 * Restart interrupted system call.
2265 if (errno
== EINTR
) {
2273 for (i
= 0; i
< nb_fd
; i
++) {
2274 revents
= LTTNG_POLL_GETEV(&events
, i
);
2275 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2277 health_code_update();
2279 if (pollfd
== thread_quit_pipe_fd
) {
2283 /* Event on the registration socket */
2284 if (revents
& LPOLLIN
) {
2286 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2287 ERR("Client socket poll error");
2290 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2296 DBG("Wait for client response");
2298 health_code_update();
2300 sock
= lttcomm_accept_unix_sock(client_sock
);
2306 * Set the CLOEXEC flag. Return code is useless because either way, the
2309 (void) utils_set_fd_cloexec(sock
);
2311 /* Set socket option for credentials retrieval */
2312 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2317 health_code_update();
2320 * Data is received from the lttng client. The struct
2321 * lttcomm_session_msg (lsm) contains the command and data request of
2324 DBG("Receiving data from client ...");
2325 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2326 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2327 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2328 DBG("Incomplete recv() from client... continuing");
2337 health_code_update();
2339 // TODO: Validate cmd_ctx including sanity check for
2340 // security purpose.
2342 rcu_thread_online();
2344 * This function dispatch the work to the kernel or userspace tracer
2345 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2346 * informations for the client. The command context struct contains
2347 * everything this function may needs.
2349 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2350 rcu_thread_offline();
2360 * TODO: Inform client somehow of the fatal error. At
2361 * this point, ret < 0 means that a zmalloc failed
2362 * (ENOMEM). Error detected but still accept
2363 * command, unless a socket error has been
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
) {
2380 health_code_update();
2383 struct lttng_payload_view view
=
2384 lttng_payload_view_from_payload(
2385 &cmd_ctx
.reply_payload
,
2387 const struct lttcomm_lttng_msg
*llm
= (typeof(
2388 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2390 assert(cmd_ctx
.reply_payload
.buffer
.size
>=
2392 assert(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2394 DBG("Sending response (size: %d, retcode: %s (%d))",
2395 cmd_ctx
.lttng_msg_size
,
2396 lttng_strerror(-llm
->ret_code
),
2398 ret
= send_unix_sock(sock
, &view
);
2400 ERR("Failed to send data back to client");
2403 /* End of transmission */
2411 health_code_update();
2423 lttng_poll_clean(&events
);
2427 unlink(config
.client_unix_sock_path
.value
);
2428 ret
= close(client_sock
);
2435 ERR("Health error occurred in %s", __func__
);
2438 health_unregister(health_sessiond
);
2440 DBG("Client thread dying");
2441 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2442 rcu_unregister_thread();
2447 bool shutdown_client_thread(void *thread_data
)
2449 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2450 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2452 return notify_thread_pipe(write_fd
) == 1;
2455 struct lttng_thread
*launch_client_thread(void)
2457 bool thread_running
;
2458 struct lttng_pipe
*client_quit_pipe
;
2459 struct lttng_thread
*thread
= NULL
;
2460 int client_sock_fd
= -1;
2462 sem_init(&thread_state
.ready
, 0, 0);
2463 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2464 if (!client_quit_pipe
) {
2468 client_sock_fd
= create_client_sock();
2469 if (client_sock_fd
< 0) {
2473 thread_state
.client_sock
= client_sock_fd
;
2474 thread
= lttng_thread_create("Client management",
2475 thread_manage_clients
,
2476 shutdown_client_thread
,
2477 cleanup_client_thread
,
2482 /* The client thread now owns the client sock fd and the quit pipe. */
2483 client_sock_fd
= -1;
2484 client_quit_pipe
= NULL
;
2487 * This thread is part of the threads that need to be fully
2488 * initialized before the session daemon is marked as "ready".
2490 thread_running
= wait_thread_status();
2491 if (!thread_running
) {
2496 if (client_sock_fd
>= 0) {
2497 if (close(client_sock_fd
)) {
2498 PERROR("Failed to close client socket");
2501 lttng_thread_put(thread
);
2502 cleanup_client_thread(client_quit_pipe
);