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
14 #include <common/compat/getenv.h>
15 #include <common/unix.h>
16 #include <common/utils.h>
17 #include <lttng/userspace-probe-internal.h>
18 #include <lttng/event-internal.h>
19 #include <lttng/session-internal.h>
20 #include <lttng/session-descriptor-internal.h>
21 #include <lttng/tracker-internal.h>
24 #include "lttng-sessiond.h"
28 #include "health-sessiond.h"
29 #include "testpoint.h"
31 #include "manage-consumer.h"
36 static struct thread_state
{
42 static void set_thread_status(bool running
)
44 DBG("Marking client thread's state as %s", running
? "running" : "error");
45 thread_state
.running
= running
;
46 sem_post(&thread_state
.ready
);
49 static bool wait_thread_status(void)
51 DBG("Waiting for client thread to be ready");
52 sem_wait(&thread_state
.ready
);
53 if (thread_state
.running
) {
54 DBG("Client thread is ready");
56 ERR("Initialization of client thread failed");
59 return thread_state
.running
;
63 * Setup the outgoing data buffer for the response (llm) by allocating the
64 * right amount of memory and copying the original information from the lsm
67 * Return 0 on success, negative value on error.
69 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
70 const void *payload_buf
, size_t payload_len
,
71 const void *cmd_header_buf
, size_t cmd_header_len
)
74 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
75 const size_t cmd_header_offset
= header_len
;
76 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
77 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
80 cmd_ctx
->llm
= zmalloc(total_msg_size
);
82 if (cmd_ctx
->llm
== NULL
) {
88 /* Copy common data */
89 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
90 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
91 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
92 cmd_ctx
->llm
->data_size
= payload_len
;
93 cmd_ctx
->lttng_msg_size
= total_msg_size
;
95 /* Copy command header */
97 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
103 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
112 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
113 * exec or it will fail.
115 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
117 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
121 * Fork and exec a consumer daemon (consumerd).
123 * Return pid if successful else -1.
125 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
129 const char *consumer_to_use
;
130 const char *verbosity
;
133 DBG("Spawning consumerd");
140 if (config
.verbose_consumer
) {
141 verbosity
= "--verbose";
142 } else if (lttng_opt_quiet
) {
143 verbosity
= "--quiet";
148 switch (consumer_data
->type
) {
149 case LTTNG_CONSUMER_KERNEL
:
151 * Find out which consumerd to execute. We will first try the
152 * 64-bit path, then the sessiond's installation directory, and
153 * fallback on the 32-bit one,
155 DBG3("Looking for a kernel consumer at these locations:");
156 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
157 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
158 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
159 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
160 DBG3("Found location #1");
161 consumer_to_use
= config
.consumerd64_bin_path
.value
;
162 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
163 DBG3("Found location #2");
164 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
165 } else if (config
.consumerd32_bin_path
.value
&&
166 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
167 DBG3("Found location #3");
168 consumer_to_use
= config
.consumerd32_bin_path
.value
;
170 DBG("Could not find any valid consumerd executable");
174 DBG("Using kernel consumer at: %s", consumer_to_use
);
175 (void) execl(consumer_to_use
,
176 "lttng-consumerd", verbosity
, "-k",
177 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
178 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
179 "--group", config
.tracing_group_name
.value
,
182 case LTTNG_CONSUMER64_UST
:
184 if (config
.consumerd64_lib_dir
.value
) {
189 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
193 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
194 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
199 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
200 if (tmp
[0] != '\0') {
204 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
211 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
212 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
213 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
214 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
215 "--group", config
.tracing_group_name
.value
,
219 case LTTNG_CONSUMER32_UST
:
221 if (config
.consumerd32_lib_dir
.value
) {
226 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
230 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
231 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
236 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
237 if (tmp
[0] != '\0') {
241 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
248 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
249 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
250 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
251 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
252 "--group", config
.tracing_group_name
.value
,
257 ERR("unknown consumer type");
261 PERROR("Consumer execl()");
263 /* Reaching this point, we got a failure on our execl(). */
265 } else if (pid
> 0) {
268 PERROR("start consumer fork");
276 * Spawn the consumerd daemon and session daemon thread.
278 static int start_consumerd(struct consumer_data
*consumer_data
)
283 * Set the listen() state on the socket since there is a possible race
284 * between the exec() of the consumer daemon and this call if place in the
285 * consumer thread. See bug #366 for more details.
287 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
292 pthread_mutex_lock(&consumer_data
->pid_mutex
);
293 if (consumer_data
->pid
!= 0) {
294 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
298 ret
= spawn_consumerd(consumer_data
);
300 ERR("Spawning consumerd failed");
301 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
305 /* Setting up the consumer_data pid */
306 consumer_data
->pid
= ret
;
307 DBG2("Consumer pid %d", consumer_data
->pid
);
308 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
310 DBG2("Spawning consumer control thread");
311 ret
= spawn_consumer_thread(consumer_data
);
313 ERR("Fatal error spawning consumer control thread");
321 /* Cleanup already created sockets on error. */
322 if (consumer_data
->err_sock
>= 0) {
325 err
= close(consumer_data
->err_sock
);
327 PERROR("close consumer data error socket");
334 * Copy consumer output from the tracing session to the domain session. The
335 * function also applies the right modification on a per domain basis for the
336 * trace files destination directory.
338 * Should *NOT* be called with RCU read-side lock held.
340 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
343 const char *dir_name
;
344 struct consumer_output
*consumer
;
347 assert(session
->consumer
);
350 case LTTNG_DOMAIN_KERNEL
:
351 DBG3("Copying tracing session consumer output in kernel session");
353 * XXX: We should audit the session creation and what this function
354 * does "extra" in order to avoid a destroy since this function is used
355 * in the domain session creation (kernel and ust) only. Same for UST
358 if (session
->kernel_session
->consumer
) {
359 consumer_output_put(session
->kernel_session
->consumer
);
361 session
->kernel_session
->consumer
=
362 consumer_copy_output(session
->consumer
);
363 /* Ease our life a bit for the next part */
364 consumer
= session
->kernel_session
->consumer
;
365 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
367 case LTTNG_DOMAIN_JUL
:
368 case LTTNG_DOMAIN_LOG4J
:
369 case LTTNG_DOMAIN_PYTHON
:
370 case LTTNG_DOMAIN_UST
:
371 DBG3("Copying tracing session consumer output in UST session");
372 if (session
->ust_session
->consumer
) {
373 consumer_output_put(session
->ust_session
->consumer
);
375 session
->ust_session
->consumer
=
376 consumer_copy_output(session
->consumer
);
377 /* Ease our life a bit for the next part */
378 consumer
= session
->ust_session
->consumer
;
379 dir_name
= DEFAULT_UST_TRACE_DIR
;
382 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
386 /* Append correct directory to subdir */
387 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
388 sizeof(consumer
->domain_subdir
));
393 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
401 * Create an UST session and add it to the session ust list.
403 * Should *NOT* be called with RCU read-side lock held.
405 static int create_ust_session(struct ltt_session
*session
,
406 const struct lttng_domain
*domain
)
409 struct ltt_ust_session
*lus
= NULL
;
413 assert(session
->consumer
);
415 switch (domain
->type
) {
416 case LTTNG_DOMAIN_JUL
:
417 case LTTNG_DOMAIN_LOG4J
:
418 case LTTNG_DOMAIN_PYTHON
:
419 case LTTNG_DOMAIN_UST
:
422 ERR("Unknown UST domain on create session %d", domain
->type
);
423 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
427 DBG("Creating UST session");
429 lus
= trace_ust_create_session(session
->id
);
431 ret
= LTTNG_ERR_UST_SESS_FAIL
;
435 lus
->uid
= session
->uid
;
436 lus
->gid
= session
->gid
;
437 lus
->output_traces
= session
->output_traces
;
438 lus
->snapshot_mode
= session
->snapshot_mode
;
439 lus
->live_timer_interval
= session
->live_timer
;
440 session
->ust_session
= lus
;
441 if (session
->shm_path
[0]) {
442 strncpy(lus
->root_shm_path
, session
->shm_path
,
443 sizeof(lus
->root_shm_path
));
444 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
445 strncpy(lus
->shm_path
, session
->shm_path
,
446 sizeof(lus
->shm_path
));
447 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
448 strncat(lus
->shm_path
, "/ust",
449 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
451 /* Copy session output to the newly created UST session */
452 ret
= copy_session_consumer(domain
->type
, session
);
453 if (ret
!= LTTNG_OK
) {
461 session
->ust_session
= NULL
;
466 * Create a kernel tracer session then create the default channel.
468 static int create_kernel_session(struct ltt_session
*session
)
472 DBG("Creating kernel session");
474 ret
= kernel_create_session(session
);
476 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
480 /* Code flow safety */
481 assert(session
->kernel_session
);
483 /* Copy session output to the newly created Kernel session */
484 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
485 if (ret
!= LTTNG_OK
) {
489 session
->kernel_session
->uid
= session
->uid
;
490 session
->kernel_session
->gid
= session
->gid
;
491 session
->kernel_session
->output_traces
= session
->output_traces
;
492 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
497 trace_kernel_destroy_session(session
->kernel_session
);
498 session
->kernel_session
= NULL
;
504 * Count number of session permitted by uid/gid.
506 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
509 struct ltt_session
*session
;
510 const struct ltt_session_list
*session_list
= session_get_list();
512 DBG("Counting number of available session for UID %d GID %d",
514 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
515 if (!session_get(session
)) {
518 session_lock(session
);
519 /* Only count the sessions the user can control. */
520 if (session_access_ok(session
, uid
, gid
) &&
521 !session
->destroyed
) {
524 session_unlock(session
);
525 session_put(session
);
530 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
531 int *sock_error
, struct lttng_event
*event
)
534 struct lttng_userspace_probe_location
*probe_location
;
535 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
536 struct lttng_dynamic_buffer probe_location_buffer
;
537 struct lttng_buffer_view buffer_view
;
540 * Create a buffer to store the serialized version of the probe
543 lttng_dynamic_buffer_init(&probe_location_buffer
);
544 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
545 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
547 ret
= LTTNG_ERR_NOMEM
;
552 * Receive the probe location.
554 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
555 probe_location_buffer
.size
);
557 DBG("Nothing recv() from client var len data... continuing");
559 lttng_dynamic_buffer_reset(&probe_location_buffer
);
560 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
564 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
565 &probe_location_buffer
, 0, probe_location_buffer
.size
);
568 * Extract the probe location from the serialized version.
570 ret
= lttng_userspace_probe_location_create_from_buffer(
571 &buffer_view
, &probe_location
);
573 WARN("Failed to create a userspace probe location from the received buffer");
574 lttng_dynamic_buffer_reset( &probe_location_buffer
);
575 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
580 * Receive the file descriptor to the target binary from the client.
582 DBG("Receiving userspace probe target FD from client ...");
583 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
585 DBG("Nothing recv() from client userspace probe fd... continuing");
587 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
592 * Set the file descriptor received from the client through the unix
593 * socket in the probe location.
595 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
597 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
602 * From the kernel tracer's perspective, all userspace probe event types
603 * are all the same: a file and an offset.
605 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
606 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
607 ret
= lttng_userspace_probe_location_function_set_binary_fd(
610 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
611 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
615 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
620 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
624 /* Attach the probe location to the event. */
625 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
627 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
631 lttng_dynamic_buffer_reset(&probe_location_buffer
);
637 * Version of setup_lttng_msg() without command header.
639 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
640 void *payload_buf
, size_t payload_len
)
642 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
646 * Free memory of a command context structure.
648 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
650 DBG("Clean command context structure");
652 if ((*cmd_ctx
)->llm
) {
653 free((*cmd_ctx
)->llm
);
655 if ((*cmd_ctx
)->lsm
) {
656 free((*cmd_ctx
)->lsm
);
664 * Check if the current kernel tracer supports the session rotation feature.
665 * Return 1 if it does, 0 otherwise.
667 static int check_rotate_compatible(void)
671 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
672 DBG("Kernel tracer version is not compatible with the rotation feature");
680 * Send data on a unix socket using the liblttsessiondcomm API.
682 * Return lttcomm error code.
684 static int send_unix_sock(int sock
, void *buf
, size_t len
)
686 /* Check valid length */
691 return lttcomm_send_unix_sock(sock
, buf
, len
);
695 * Process the command requested by the lttng client within the command
696 * context structure. This function make sure that the return structure (llm)
697 * is set and ready for transmission before returning.
699 * Return any error encountered or 0 for success.
701 * "sock" is only used for special-case var. len data.
702 * A command may assume the ownership of the socket, in which case its value
703 * should be set to -1.
705 * Should *NOT* be called with RCU read-side lock held.
707 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
711 int need_tracing_session
= 1;
714 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
716 assert(!rcu_read_ongoing());
720 switch (cmd_ctx
->lsm
->cmd_type
) {
721 case LTTNG_CREATE_SESSION_EXT
:
722 case LTTNG_DESTROY_SESSION
:
723 case LTTNG_LIST_SESSIONS
:
724 case LTTNG_LIST_DOMAINS
:
725 case LTTNG_START_TRACE
:
726 case LTTNG_STOP_TRACE
:
727 case LTTNG_DATA_PENDING
:
728 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
729 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
730 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
731 case LTTNG_SNAPSHOT_RECORD
:
732 case LTTNG_SAVE_SESSION
:
733 case LTTNG_SET_SESSION_SHM_PATH
:
734 case LTTNG_REGENERATE_METADATA
:
735 case LTTNG_REGENERATE_STATEDUMP
:
736 case LTTNG_REGISTER_TRIGGER
:
737 case LTTNG_UNREGISTER_TRIGGER
:
738 case LTTNG_ROTATE_SESSION
:
739 case LTTNG_ROTATION_GET_INFO
:
740 case LTTNG_ROTATION_SET_SCHEDULE
:
741 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
742 case LTTNG_CLEAR_SESSION
:
749 if (config
.no_kernel
&& need_domain
750 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
752 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
754 ret
= LTTNG_ERR_KERN_NA
;
759 /* Deny register consumer if we already have a spawned consumer. */
760 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
761 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
762 if (kconsumer_data
.pid
> 0) {
763 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
764 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
767 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
771 * Check for command that don't needs to allocate a returned payload. We do
772 * this here so we don't have to make the call for no payload at each
775 switch(cmd_ctx
->lsm
->cmd_type
) {
776 case LTTNG_LIST_SESSIONS
:
777 case LTTNG_LIST_TRACEPOINTS
:
778 case LTTNG_LIST_TRACEPOINT_FIELDS
:
779 case LTTNG_LIST_DOMAINS
:
780 case LTTNG_LIST_CHANNELS
:
781 case LTTNG_LIST_EVENTS
:
782 case LTTNG_LIST_SYSCALLS
:
783 case LTTNG_LIST_TRACKER_IDS
:
784 case LTTNG_DATA_PENDING
:
785 case LTTNG_ROTATE_SESSION
:
786 case LTTNG_ROTATION_GET_INFO
:
787 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
790 /* Setup lttng message with no payload */
791 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
793 /* This label does not try to unlock the session */
794 goto init_setup_error
;
798 /* Commands that DO NOT need a session. */
799 switch (cmd_ctx
->lsm
->cmd_type
) {
800 case LTTNG_CREATE_SESSION_EXT
:
801 case LTTNG_LIST_SESSIONS
:
802 case LTTNG_LIST_TRACEPOINTS
:
803 case LTTNG_LIST_SYSCALLS
:
804 case LTTNG_LIST_TRACEPOINT_FIELDS
:
805 case LTTNG_SAVE_SESSION
:
806 case LTTNG_REGISTER_TRIGGER
:
807 case LTTNG_UNREGISTER_TRIGGER
:
808 need_tracing_session
= 0;
811 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
813 * We keep the session list lock across _all_ commands
814 * for now, because the per-session lock does not
815 * handle teardown properly.
818 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
819 if (cmd_ctx
->session
== NULL
) {
820 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
823 /* Acquire lock for the session */
824 session_lock(cmd_ctx
->session
);
830 * Commands that need a valid session but should NOT create one if none
831 * exists. Instead of creating one and destroying it when the command is
832 * handled, process that right before so we save some round trip in useless
835 switch (cmd_ctx
->lsm
->cmd_type
) {
836 case LTTNG_DISABLE_CHANNEL
:
837 case LTTNG_DISABLE_EVENT
:
838 switch (cmd_ctx
->lsm
->domain
.type
) {
839 case LTTNG_DOMAIN_KERNEL
:
840 if (!cmd_ctx
->session
->kernel_session
) {
841 ret
= LTTNG_ERR_NO_CHANNEL
;
845 case LTTNG_DOMAIN_JUL
:
846 case LTTNG_DOMAIN_LOG4J
:
847 case LTTNG_DOMAIN_PYTHON
:
848 case LTTNG_DOMAIN_UST
:
849 if (!cmd_ctx
->session
->ust_session
) {
850 ret
= LTTNG_ERR_NO_CHANNEL
;
855 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
867 * Check domain type for specific "pre-action".
869 switch (cmd_ctx
->lsm
->domain
.type
) {
870 case LTTNG_DOMAIN_KERNEL
:
872 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
876 /* Kernel tracer check */
877 if (!kernel_tracer_is_initialized()) {
878 /* Basically, load kernel tracer modules */
879 ret
= init_kernel_tracer();
885 /* Consumer is in an ERROR state. Report back to client */
886 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
887 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
891 /* Need a session for kernel command */
892 if (need_tracing_session
) {
893 if (cmd_ctx
->session
->kernel_session
== NULL
) {
894 ret
= create_kernel_session(cmd_ctx
->session
);
895 if (ret
!= LTTNG_OK
) {
896 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
901 /* Start the kernel consumer daemon */
902 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
903 if (kconsumer_data
.pid
== 0 &&
904 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
905 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
906 ret
= start_consumerd(&kconsumer_data
);
908 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
911 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
913 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
917 * The consumer was just spawned so we need to add the socket to
918 * the consumer output of the session if exist.
920 ret
= consumer_create_socket(&kconsumer_data
,
921 cmd_ctx
->session
->kernel_session
->consumer
);
928 case LTTNG_DOMAIN_JUL
:
929 case LTTNG_DOMAIN_LOG4J
:
930 case LTTNG_DOMAIN_PYTHON
:
931 case LTTNG_DOMAIN_UST
:
933 if (!ust_app_supported()) {
934 ret
= LTTNG_ERR_NO_UST
;
937 /* Consumer is in an ERROR state. Report back to client */
938 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
939 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
943 if (need_tracing_session
) {
944 /* Create UST session if none exist. */
945 if (cmd_ctx
->session
->ust_session
== NULL
) {
946 ret
= create_ust_session(cmd_ctx
->session
,
947 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
));
948 if (ret
!= LTTNG_OK
) {
953 /* Start the UST consumer daemons */
955 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
956 if (config
.consumerd64_bin_path
.value
&&
957 ustconsumer64_data
.pid
== 0 &&
958 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
959 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
960 ret
= start_consumerd(&ustconsumer64_data
);
962 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
963 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
967 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
968 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
970 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
974 * Setup socket for consumer 64 bit. No need for atomic access
975 * since it was set above and can ONLY be set in this thread.
977 ret
= consumer_create_socket(&ustconsumer64_data
,
978 cmd_ctx
->session
->ust_session
->consumer
);
984 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
985 if (config
.consumerd32_bin_path
.value
&&
986 ustconsumer32_data
.pid
== 0 &&
987 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
988 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
989 ret
= start_consumerd(&ustconsumer32_data
);
991 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
992 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
996 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
997 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
999 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1003 * Setup socket for consumer 32 bit. No need for atomic access
1004 * since it was set above and can ONLY be set in this thread.
1006 ret
= consumer_create_socket(&ustconsumer32_data
,
1007 cmd_ctx
->session
->ust_session
->consumer
);
1019 /* Validate consumer daemon state when start/stop trace command */
1020 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1021 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1022 switch (cmd_ctx
->lsm
->domain
.type
) {
1023 case LTTNG_DOMAIN_NONE
:
1025 case LTTNG_DOMAIN_JUL
:
1026 case LTTNG_DOMAIN_LOG4J
:
1027 case LTTNG_DOMAIN_PYTHON
:
1028 case LTTNG_DOMAIN_UST
:
1029 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1030 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1034 case LTTNG_DOMAIN_KERNEL
:
1035 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1036 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1041 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1047 * Check that the UID or GID match that of the tracing session.
1048 * The root user can interact with all sessions.
1050 if (need_tracing_session
) {
1051 if (!session_access_ok(cmd_ctx
->session
,
1052 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1053 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1054 cmd_ctx
->session
->destroyed
) {
1055 ret
= LTTNG_ERR_EPERM
;
1061 * Send relayd information to consumer as soon as we have a domain and a
1064 if (cmd_ctx
->session
&& need_domain
) {
1066 * Setup relayd if not done yet. If the relayd information was already
1067 * sent to the consumer, this call will gracefully return.
1069 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1070 if (ret
!= LTTNG_OK
) {
1075 /* Process by command type */
1076 switch (cmd_ctx
->lsm
->cmd_type
) {
1077 case LTTNG_ADD_CONTEXT
:
1080 * An LTTNG_ADD_CONTEXT command might have a supplementary
1081 * payload if the context being added is an application context.
1083 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1084 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1085 char *provider_name
= NULL
, *context_name
= NULL
;
1086 size_t provider_name_len
=
1087 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1088 size_t context_name_len
=
1089 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1091 if (provider_name_len
== 0 || context_name_len
== 0) {
1093 * Application provider and context names MUST
1096 ret
= -LTTNG_ERR_INVALID
;
1100 provider_name
= zmalloc(provider_name_len
+ 1);
1101 if (!provider_name
) {
1102 ret
= -LTTNG_ERR_NOMEM
;
1105 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1108 context_name
= zmalloc(context_name_len
+ 1);
1109 if (!context_name
) {
1110 ret
= -LTTNG_ERR_NOMEM
;
1111 goto error_add_context
;
1113 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1116 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1119 goto error_add_context
;
1122 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1125 goto error_add_context
;
1130 * cmd_add_context assumes ownership of the provider and context
1133 ret
= cmd_add_context(cmd_ctx
->session
,
1134 cmd_ctx
->lsm
->domain
.type
,
1135 cmd_ctx
->lsm
->u
.context
.channel_name
,
1136 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.context
.ctx
),
1137 kernel_poll_pipe
[1]);
1139 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1140 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1142 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1143 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1149 case LTTNG_DISABLE_CHANNEL
:
1151 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1152 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1155 case LTTNG_DISABLE_EVENT
:
1159 * FIXME: handle filter; for now we just receive the filter's
1160 * bytecode along with the filter expression which are sent by
1161 * liblttng-ctl and discard them.
1163 * This fixes an issue where the client may block while sending
1164 * the filter payload and encounter an error because the session
1165 * daemon closes the socket without ever handling this data.
1167 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1168 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1171 char data
[LTTNG_FILTER_MAX_LEN
];
1173 DBG("Discarding disable event command payload of size %zu", count
);
1175 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1176 count
> sizeof(data
) ? sizeof(data
) : count
);
1181 count
-= (size_t) ret
;
1184 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1185 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1186 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.disable
.event
));
1189 case LTTNG_ENABLE_CHANNEL
:
1191 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1192 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1193 ret
= cmd_enable_channel(cmd_ctx
->session
,
1194 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1195 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.channel
.chan
),
1196 kernel_poll_pipe
[1]);
1199 case LTTNG_TRACK_ID
:
1201 struct lttng_tracker_id
*id
= NULL
;
1202 enum lttng_tracker_id_status status
;
1204 id
= lttng_tracker_id_create();
1206 ret
= LTTNG_ERR_NOMEM
;
1210 switch (cmd_ctx
->lsm
->u
.id_tracker
.id_type
) {
1212 status
= lttng_tracker_id_set_all(id
);
1214 case LTTNG_ID_VALUE
:
1215 status
= lttng_tracker_id_set_value(
1216 id
, cmd_ctx
->lsm
->u
.id_tracker
.u
.value
);
1218 case LTTNG_ID_STRING
:
1220 const size_t var_len
= cmd_ctx
->lsm
->u
.id_tracker
.u
.var_len
;
1221 char *string
= NULL
;
1223 string
= zmalloc(var_len
);
1225 lttng_tracker_id_destroy(id
);
1226 ret
= LTTNG_ERR_NOMEM
;
1229 DBG("Receiving var len tracker id string from client");
1230 ret
= lttcomm_recv_unix_sock(*sock
, string
, var_len
);
1232 DBG("Nothing received");
1235 lttng_tracker_id_destroy(id
);
1236 ret
= LTTNG_ERR_INVALID
;
1239 if (strnlen(string
, var_len
) != var_len
- 1) {
1240 DBG("String received as tracker ID is not NULL-terminated");
1242 lttng_tracker_id_destroy(id
);
1243 ret
= LTTNG_ERR_INVALID
;
1247 status
= lttng_tracker_id_set_string(id
, string
);
1252 lttng_tracker_id_destroy(id
);
1253 ret
= LTTNG_ERR_INVALID
;
1257 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
1258 ERR("Invalid value for tracker id");
1259 ret
= LTTNG_ERR_INVALID
;
1260 lttng_tracker_id_destroy(id
);
1264 ret
= cmd_track_id(cmd_ctx
->session
,
1265 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
1266 cmd_ctx
->lsm
->domain
.type
, id
);
1267 lttng_tracker_id_destroy(id
);
1270 case LTTNG_UNTRACK_ID
:
1272 struct lttng_tracker_id
*id
= NULL
;
1273 enum lttng_tracker_id_status status
;
1275 id
= lttng_tracker_id_create();
1277 switch (cmd_ctx
->lsm
->u
.id_tracker
.id_type
) {
1279 status
= lttng_tracker_id_set_all(id
);
1281 case LTTNG_ID_VALUE
:
1282 status
= lttng_tracker_id_set_value(
1283 id
, cmd_ctx
->lsm
->u
.id_tracker
.u
.value
);
1285 case LTTNG_ID_STRING
:
1287 const size_t var_len
= cmd_ctx
->lsm
->u
.id_tracker
.u
.var_len
;
1288 char *string
= NULL
;
1290 string
= zmalloc(var_len
);
1292 ret
= LTTNG_ERR_NOMEM
;
1293 lttng_tracker_id_destroy(id
);
1296 DBG("Receiving var len tracker id string from client");
1297 ret
= lttcomm_recv_unix_sock(*sock
, string
, var_len
);
1299 DBG("Nothing received");
1301 lttng_tracker_id_destroy(id
);
1303 ret
= LTTNG_ERR_INVALID
;
1306 if (strnlen(string
, var_len
) != var_len
- 1) {
1307 DBG("String received as tracker ID is not NULL-terminated");
1308 lttng_tracker_id_destroy(id
);
1310 ret
= LTTNG_ERR_INVALID
;
1313 status
= lttng_tracker_id_set_string(id
, string
);
1318 lttng_tracker_id_destroy(id
);
1319 ret
= LTTNG_ERR_INVALID
;
1323 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
1324 ERR("Invalid tracker id");
1325 lttng_tracker_id_destroy(id
);
1326 ret
= LTTNG_ERR_INVALID
;
1330 ret
= cmd_untrack_id(cmd_ctx
->session
,
1331 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
1332 cmd_ctx
->lsm
->domain
.type
, id
);
1333 lttng_tracker_id_destroy(id
);
1336 case LTTNG_ENABLE_EVENT
:
1338 struct lttng_event
*ev
= NULL
;
1339 struct lttng_event_exclusion
*exclusion
= NULL
;
1340 struct lttng_filter_bytecode
*bytecode
= NULL
;
1341 char *filter_expression
= NULL
;
1343 /* Handle exclusion events and receive it from the client. */
1344 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1345 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1347 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1348 (count
* LTTNG_SYMBOL_NAME_LEN
));
1350 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1354 DBG("Receiving var len exclusion event list from client ...");
1355 exclusion
->count
= count
;
1356 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1357 count
* LTTNG_SYMBOL_NAME_LEN
);
1359 DBG("Nothing recv() from client var len data... continuing");
1362 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1367 /* Get filter expression from client. */
1368 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1369 size_t expression_len
=
1370 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1372 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1373 ret
= LTTNG_ERR_FILTER_INVAL
;
1378 filter_expression
= zmalloc(expression_len
);
1379 if (!filter_expression
) {
1381 ret
= LTTNG_ERR_FILTER_NOMEM
;
1385 /* Receive var. len. data */
1386 DBG("Receiving var len filter's expression from client ...");
1387 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1390 DBG("Nothing recv() from client var len data... continuing");
1392 free(filter_expression
);
1394 ret
= LTTNG_ERR_FILTER_INVAL
;
1399 /* Handle filter and get bytecode from client. */
1400 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1401 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1403 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1404 ret
= LTTNG_ERR_FILTER_INVAL
;
1405 free(filter_expression
);
1410 bytecode
= zmalloc(bytecode_len
);
1412 free(filter_expression
);
1414 ret
= LTTNG_ERR_FILTER_NOMEM
;
1418 /* Receive var. len. data */
1419 DBG("Receiving var len filter's bytecode from client ...");
1420 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1422 DBG("Nothing recv() from client var len data... continuing");
1424 free(filter_expression
);
1427 ret
= LTTNG_ERR_FILTER_INVAL
;
1431 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1432 free(filter_expression
);
1435 ret
= LTTNG_ERR_FILTER_INVAL
;
1440 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.enable
.event
));
1442 DBG("Failed to copy event: %s",
1443 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1444 free(filter_expression
);
1447 ret
= LTTNG_ERR_NOMEM
;
1452 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1453 /* Expect a userspace probe description. */
1454 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1456 free(filter_expression
);
1459 lttng_event_destroy(ev
);
1464 ret
= cmd_enable_event(cmd_ctx
->session
,
1465 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1466 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1468 filter_expression
, bytecode
, exclusion
,
1469 kernel_poll_pipe
[1]);
1470 lttng_event_destroy(ev
);
1473 case LTTNG_LIST_TRACEPOINTS
:
1475 struct lttng_event
*events
;
1478 session_lock_list();
1479 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1480 session_unlock_list();
1481 if (nb_events
< 0) {
1482 /* Return value is a negative lttng_error_code. */
1488 * Setup lttng message with payload size set to the event list size in
1489 * bytes and then copy list into the llm payload.
1491 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1492 sizeof(struct lttng_event
) * nb_events
);
1502 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1504 struct lttng_event_field
*fields
;
1507 session_lock_list();
1508 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1510 session_unlock_list();
1511 if (nb_fields
< 0) {
1512 /* Return value is a negative lttng_error_code. */
1518 * Setup lttng message with payload size set to the event list size in
1519 * bytes and then copy list into the llm payload.
1521 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1522 sizeof(struct lttng_event_field
) * nb_fields
);
1532 case LTTNG_LIST_SYSCALLS
:
1534 struct lttng_event
*events
;
1537 nb_events
= cmd_list_syscalls(&events
);
1538 if (nb_events
< 0) {
1539 /* Return value is a negative lttng_error_code. */
1545 * Setup lttng message with payload size set to the event list size in
1546 * bytes and then copy list into the llm payload.
1548 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1549 sizeof(struct lttng_event
) * nb_events
);
1559 case LTTNG_LIST_TRACKER_IDS
:
1561 struct lttcomm_tracker_command_header cmd_header
;
1562 struct lttng_tracker_ids
*ids
= NULL
;
1563 enum lttng_tracker_id_status status
;
1564 unsigned int nr_ids
;
1565 struct lttng_dynamic_buffer buf
;
1567 ret
= cmd_list_tracker_ids(
1568 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
1569 cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1571 if (ret
!= LTTNG_OK
) {
1575 lttng_dynamic_buffer_init(&buf
);
1577 status
= lttng_tracker_ids_get_count(ids
, &nr_ids
);
1578 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
1579 ret
= -LTTNG_ERR_INVALID
;
1580 goto error_list_tracker
;
1583 cmd_header
.nb_tracker_id
= nr_ids
;
1585 ret
= lttng_tracker_ids_serialize(ids
, &buf
);
1587 goto error_list_tracker
;
1590 ret
= setup_lttng_msg(cmd_ctx
, buf
.data
, buf
.size
, &cmd_header
,
1591 sizeof(cmd_header
));
1593 lttng_tracker_ids_destroy(ids
);
1594 lttng_dynamic_buffer_reset(&buf
);
1602 case LTTNG_SET_CONSUMER_URI
:
1605 struct lttng_uri
*uris
;
1607 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1608 len
= nb_uri
* sizeof(struct lttng_uri
);
1611 ret
= LTTNG_ERR_INVALID
;
1615 uris
= zmalloc(len
);
1617 ret
= LTTNG_ERR_FATAL
;
1621 /* Receive variable len data */
1622 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1623 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1625 DBG("No URIs received from client... continuing");
1627 ret
= LTTNG_ERR_SESSION_FAIL
;
1632 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1634 if (ret
!= LTTNG_OK
) {
1641 case LTTNG_START_TRACE
:
1644 * On the first start, if we have a kernel session and we have
1645 * enabled time or size-based rotations, we have to make sure
1646 * the kernel tracer supports it.
1648 if (!cmd_ctx
->session
->has_been_started
&& \
1649 cmd_ctx
->session
->kernel_session
&& \
1650 (cmd_ctx
->session
->rotate_timer_period
|| \
1651 cmd_ctx
->session
->rotate_size
) && \
1652 !check_rotate_compatible()) {
1653 DBG("Kernel tracer version is not compatible with the rotation feature");
1654 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1657 ret
= cmd_start_trace(cmd_ctx
->session
);
1660 case LTTNG_STOP_TRACE
:
1662 ret
= cmd_stop_trace(cmd_ctx
->session
);
1665 case LTTNG_DESTROY_SESSION
:
1667 ret
= cmd_destroy_session(cmd_ctx
->session
,
1668 notification_thread_handle
,
1672 case LTTNG_LIST_DOMAINS
:
1675 struct lttng_domain
*domains
= NULL
;
1677 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1679 /* Return value is a negative lttng_error_code. */
1684 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1685 nb_dom
* sizeof(struct lttng_domain
));
1695 case LTTNG_LIST_CHANNELS
:
1697 ssize_t payload_size
;
1698 struct lttng_channel
*channels
= NULL
;
1700 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1701 cmd_ctx
->session
, &channels
);
1702 if (payload_size
< 0) {
1703 /* Return value is a negative lttng_error_code. */
1704 ret
= -payload_size
;
1708 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1719 case LTTNG_LIST_EVENTS
:
1722 struct lttng_event
*events
= NULL
;
1723 struct lttcomm_event_command_header cmd_header
;
1726 memset(&cmd_header
, 0, sizeof(cmd_header
));
1727 /* Extended infos are included at the end of events */
1728 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1729 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1730 &events
, &total_size
);
1733 /* Return value is a negative lttng_error_code. */
1738 cmd_header
.nb_events
= nb_event
;
1739 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1740 &cmd_header
, sizeof(cmd_header
));
1750 case LTTNG_LIST_SESSIONS
:
1752 unsigned int nr_sessions
;
1753 void *sessions_payload
;
1756 session_lock_list();
1757 nr_sessions
= lttng_sessions_count(
1758 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1759 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1761 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1762 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1763 sessions_payload
= zmalloc(payload_len
);
1765 if (!sessions_payload
) {
1766 session_unlock_list();
1771 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1772 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1773 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1774 session_unlock_list();
1776 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1778 free(sessions_payload
);
1787 case LTTNG_REGISTER_CONSUMER
:
1789 struct consumer_data
*cdata
;
1791 switch (cmd_ctx
->lsm
->domain
.type
) {
1792 case LTTNG_DOMAIN_KERNEL
:
1793 cdata
= &kconsumer_data
;
1796 ret
= LTTNG_ERR_UND
;
1800 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1801 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1804 case LTTNG_DATA_PENDING
:
1807 uint8_t pending_ret_byte
;
1809 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1814 * This function may returns 0 or 1 to indicate whether or not
1815 * there is data pending. In case of error, it should return an
1816 * LTTNG_ERR code. However, some code paths may still return
1817 * a nondescript error code, which we handle by returning an
1820 if (pending_ret
== 0 || pending_ret
== 1) {
1822 * ret will be set to LTTNG_OK at the end of
1825 } else if (pending_ret
< 0) {
1826 ret
= LTTNG_ERR_UNK
;
1833 pending_ret_byte
= (uint8_t) pending_ret
;
1835 /* 1 byte to return whether or not data is pending */
1836 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1837 &pending_ret_byte
, 1);
1846 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1848 uint32_t snapshot_id
;
1849 struct lttcomm_lttng_output_id reply
;
1851 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1852 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
),
1854 if (ret
!= LTTNG_OK
) {
1857 reply
.id
= snapshot_id
;
1859 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1865 /* Copy output list into message payload */
1869 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1871 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1872 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
));
1875 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1878 struct lttng_snapshot_output
*outputs
= NULL
;
1880 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1881 if (nb_output
< 0) {
1886 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1887 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1888 nb_output
* sizeof(struct lttng_snapshot_output
));
1898 case LTTNG_SNAPSHOT_RECORD
:
1900 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1901 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_record
.output
),
1902 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1905 case LTTNG_CREATE_SESSION_EXT
:
1907 struct lttng_dynamic_buffer payload
;
1908 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1910 lttng_dynamic_buffer_init(&payload
);
1911 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1912 if (ret
!= LTTNG_OK
) {
1916 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1919 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1920 lttng_session_descriptor_destroy(return_descriptor
);
1921 ret
= LTTNG_ERR_NOMEM
;
1924 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1927 lttng_session_descriptor_destroy(return_descriptor
);
1928 ret
= LTTNG_ERR_NOMEM
;
1931 lttng_dynamic_buffer_reset(&payload
);
1932 lttng_session_descriptor_destroy(return_descriptor
);
1936 case LTTNG_SAVE_SESSION
:
1938 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1942 case LTTNG_SET_SESSION_SHM_PATH
:
1944 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1945 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1948 case LTTNG_REGENERATE_METADATA
:
1950 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1953 case LTTNG_REGENERATE_STATEDUMP
:
1955 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1958 case LTTNG_REGISTER_TRIGGER
:
1960 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
1961 notification_thread_handle
);
1964 case LTTNG_UNREGISTER_TRIGGER
:
1966 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
1967 notification_thread_handle
);
1970 case LTTNG_ROTATE_SESSION
:
1972 struct lttng_rotate_session_return rotate_return
;
1974 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1976 memset(&rotate_return
, 0, sizeof(rotate_return
));
1977 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1978 DBG("Kernel tracer version is not compatible with the rotation feature");
1979 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1983 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
1985 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
1991 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
1992 sizeof(rotate_return
));
2001 case LTTNG_ROTATION_GET_INFO
:
2003 struct lttng_rotation_get_info_return get_info_return
;
2005 memset(&get_info_return
, 0, sizeof(get_info_return
));
2006 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2007 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
2013 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2014 sizeof(get_info_return
));
2023 case LTTNG_ROTATION_SET_SCHEDULE
:
2026 enum lttng_rotation_schedule_type schedule_type
;
2029 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2030 DBG("Kernel tracer version does not support session rotations");
2031 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2035 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
2036 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
2037 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
2039 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
2043 notification_thread_handle
);
2044 if (ret
!= LTTNG_OK
) {
2050 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2052 struct lttng_session_list_schedules_return schedules
= {
2053 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2054 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2055 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2056 .size
.value
= cmd_ctx
->session
->rotate_size
,
2059 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2069 case LTTNG_CLEAR_SESSION
:
2071 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2075 ret
= LTTNG_ERR_UND
;
2080 if (cmd_ctx
->llm
== NULL
) {
2081 DBG("Missing llm structure. Allocating one.");
2082 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2086 /* Set return code */
2087 cmd_ctx
->llm
->ret_code
= ret
;
2089 if (cmd_ctx
->session
) {
2090 session_unlock(cmd_ctx
->session
);
2091 session_put(cmd_ctx
->session
);
2092 cmd_ctx
->session
= NULL
;
2094 if (need_tracing_session
) {
2095 session_unlock_list();
2098 assert(!rcu_read_ongoing());
2102 static int create_client_sock(void)
2104 int ret
, client_sock
;
2105 const mode_t old_umask
= umask(0);
2107 /* Create client tool unix socket */
2108 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2109 if (client_sock
< 0) {
2110 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2115 /* Set the cloexec flag */
2116 ret
= utils_set_fd_cloexec(client_sock
);
2118 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2119 "Continuing but note that the consumer daemon will have a "
2120 "reference to this socket on exec()", client_sock
);
2123 /* File permission MUST be 660 */
2124 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2126 ERR("Set file permissions failed: %s",
2127 config
.client_unix_sock_path
.value
);
2129 (void) lttcomm_close_unix_sock(client_sock
);
2133 DBG("Created client socket (fd = %i)", client_sock
);
2140 static void cleanup_client_thread(void *data
)
2142 struct lttng_pipe
*quit_pipe
= data
;
2144 lttng_pipe_destroy(quit_pipe
);
2147 static void thread_init_cleanup(void *data
)
2149 set_thread_status(false);
2153 * This thread manage all clients request using the unix client socket for
2156 static void *thread_manage_clients(void *data
)
2158 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2160 uint32_t revents
, nb_fd
;
2161 struct command_ctx
*cmd_ctx
= NULL
;
2162 struct lttng_poll_event events
;
2163 const int client_sock
= thread_state
.client_sock
;
2164 struct lttng_pipe
*quit_pipe
= data
;
2165 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2167 DBG("[thread] Manage client started");
2169 is_root
= (getuid() == 0);
2171 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2173 rcu_register_thread();
2175 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2177 health_code_update();
2179 ret
= lttcomm_listen_unix_sock(client_sock
);
2185 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2186 * more will be added to this poll set.
2188 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2190 goto error_create_poll
;
2193 /* Add the application registration socket */
2194 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2199 /* Add thread quit pipe */
2200 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2205 /* Set state as running. */
2206 set_thread_status(true);
2207 pthread_cleanup_pop(0);
2209 /* This testpoint is after we signal readiness to the parent. */
2210 if (testpoint(sessiond_thread_manage_clients
)) {
2214 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2218 health_code_update();
2221 const struct cmd_completion_handler
*cmd_completion_handler
;
2223 DBG("Accepting client command ...");
2225 /* Inifinite blocking call, waiting for transmission */
2227 health_poll_entry();
2228 ret
= lttng_poll_wait(&events
, -1);
2232 * Restart interrupted system call.
2234 if (errno
== EINTR
) {
2242 for (i
= 0; i
< nb_fd
; i
++) {
2243 revents
= LTTNG_POLL_GETEV(&events
, i
);
2244 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2246 health_code_update();
2248 if (pollfd
== thread_quit_pipe_fd
) {
2252 /* Event on the registration socket */
2253 if (revents
& LPOLLIN
) {
2255 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2256 ERR("Client socket poll error");
2259 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2265 DBG("Wait for client response");
2267 health_code_update();
2269 sock
= lttcomm_accept_unix_sock(client_sock
);
2275 * Set the CLOEXEC flag. Return code is useless because either way, the
2278 (void) utils_set_fd_cloexec(sock
);
2280 /* Set socket option for credentials retrieval */
2281 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2286 /* Allocate context command to process the client request */
2287 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2288 if (cmd_ctx
== NULL
) {
2289 PERROR("zmalloc cmd_ctx");
2293 /* Allocate data buffer for reception */
2294 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2295 if (cmd_ctx
->lsm
== NULL
) {
2296 PERROR("zmalloc cmd_ctx->lsm");
2300 cmd_ctx
->llm
= NULL
;
2301 cmd_ctx
->session
= NULL
;
2303 health_code_update();
2306 * Data is received from the lttng client. The struct
2307 * lttcomm_session_msg (lsm) contains the command and data request of
2310 DBG("Receiving data from client ...");
2311 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2312 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2314 DBG("Nothing recv() from client... continuing");
2320 clean_command_ctx(&cmd_ctx
);
2324 health_code_update();
2326 // TODO: Validate cmd_ctx including sanity check for
2327 // security purpose.
2329 rcu_thread_online();
2331 * This function dispatch the work to the kernel or userspace tracer
2332 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2333 * informations for the client. The command context struct contains
2334 * everything this function may needs.
2336 ret
= process_client_msg(cmd_ctx
, &sock
, &sock_error
);
2337 rcu_thread_offline();
2347 * TODO: Inform client somehow of the fatal error. At
2348 * this point, ret < 0 means that a zmalloc failed
2349 * (ENOMEM). Error detected but still accept
2350 * command, unless a socket error has been
2353 clean_command_ctx(&cmd_ctx
);
2357 cmd_completion_handler
= cmd_pop_completion_handler();
2358 if (cmd_completion_handler
) {
2359 enum lttng_error_code completion_code
;
2361 completion_code
= cmd_completion_handler
->run(
2362 cmd_completion_handler
->data
);
2363 if (completion_code
!= LTTNG_OK
) {
2364 clean_command_ctx(&cmd_ctx
);
2369 health_code_update();
2372 DBG("Sending response (size: %d, retcode: %s (%d))",
2373 cmd_ctx
->lttng_msg_size
,
2374 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2375 cmd_ctx
->llm
->ret_code
);
2376 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2377 cmd_ctx
->lttng_msg_size
);
2379 ERR("Failed to send data back to client");
2382 /* End of transmission */
2390 clean_command_ctx(&cmd_ctx
);
2392 health_code_update();
2404 lttng_poll_clean(&events
);
2405 clean_command_ctx(&cmd_ctx
);
2409 unlink(config
.client_unix_sock_path
.value
);
2410 ret
= close(client_sock
);
2417 ERR("Health error occurred in %s", __func__
);
2420 health_unregister(health_sessiond
);
2422 DBG("Client thread dying");
2424 rcu_unregister_thread();
2429 bool shutdown_client_thread(void *thread_data
)
2431 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2432 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2434 return notify_thread_pipe(write_fd
) == 1;
2437 struct lttng_thread
*launch_client_thread(void)
2439 bool thread_running
;
2440 struct lttng_pipe
*client_quit_pipe
;
2441 struct lttng_thread
*thread
= NULL
;
2442 int client_sock_fd
= -1;
2444 sem_init(&thread_state
.ready
, 0, 0);
2445 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2446 if (!client_quit_pipe
) {
2450 client_sock_fd
= create_client_sock();
2451 if (client_sock_fd
< 0) {
2455 thread_state
.client_sock
= client_sock_fd
;
2456 thread
= lttng_thread_create("Client management",
2457 thread_manage_clients
,
2458 shutdown_client_thread
,
2459 cleanup_client_thread
,
2464 /* The client thread now owns the client sock fd and the quit pipe. */
2465 client_sock_fd
= -1;
2466 client_quit_pipe
= NULL
;
2469 * This thread is part of the threads that need to be fully
2470 * initialized before the session daemon is marked as "ready".
2472 thread_running
= wait_thread_status();
2473 if (!thread_running
) {
2478 if (client_sock_fd
>= 0) {
2479 if (close(client_sock_fd
)) {
2480 PERROR("Failed to close client socket");
2483 lttng_thread_put(thread
);
2484 cleanup_client_thread(client_quit_pipe
);