2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "common/buffer-view.h"
11 #include "common/dynamic-buffer.h"
12 #include "common/sessiond-comm/sessiond-comm.h"
13 #include "lttng/lttng-error.h"
14 #include "lttng/tracker.h"
15 #include <common/compat/getenv.h>
16 #include <common/tracker.h>
17 #include <common/unix.h>
18 #include <common/utils.h>
19 #include <lttng/event-internal.h>
20 #include <lttng/session-descriptor-internal.h>
21 #include <lttng/session-internal.h>
22 #include <lttng/userspace-probe-internal.h>
30 #include "lttng-sessiond.h"
34 #include "health-sessiond.h"
35 #include "testpoint.h"
37 #include "manage-consumer.h"
42 static struct thread_state
{
48 static void set_thread_status(bool running
)
50 DBG("Marking client thread's state as %s", running
? "running" : "error");
51 thread_state
.running
= running
;
52 sem_post(&thread_state
.ready
);
55 static bool wait_thread_status(void)
57 DBG("Waiting for client thread to be ready");
58 sem_wait(&thread_state
.ready
);
59 if (thread_state
.running
) {
60 DBG("Client thread is ready");
62 ERR("Initialization of client thread failed");
65 return thread_state
.running
;
69 * Setup the outgoing data buffer for the response (llm) by allocating the
70 * right amount of memory and copying the original information from the lsm
73 * Return 0 on success, negative value on error.
75 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
76 const void *payload_buf
, size_t payload_len
,
77 const void *cmd_header_buf
, size_t cmd_header_len
)
80 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
81 const size_t cmd_header_offset
= header_len
;
82 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
83 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
86 cmd_ctx
->llm
= zmalloc(total_msg_size
);
88 if (cmd_ctx
->llm
== NULL
) {
94 /* Copy common data */
95 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
96 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
97 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
98 cmd_ctx
->llm
->data_size
= payload_len
;
99 cmd_ctx
->lttng_msg_size
= total_msg_size
;
101 /* Copy command header */
102 if (cmd_header_len
) {
103 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
109 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
118 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
119 * exec or it will fail.
121 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
123 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
127 * Fork and exec a consumer daemon (consumerd).
129 * Return pid if successful else -1.
131 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
135 const char *consumer_to_use
;
136 const char *verbosity
;
139 DBG("Spawning consumerd");
146 if (config
.verbose_consumer
) {
147 verbosity
= "--verbose";
148 } else if (lttng_opt_quiet
) {
149 verbosity
= "--quiet";
154 switch (consumer_data
->type
) {
155 case LTTNG_CONSUMER_KERNEL
:
157 * Find out which consumerd to execute. We will first try the
158 * 64-bit path, then the sessiond's installation directory, and
159 * fallback on the 32-bit one,
161 DBG3("Looking for a kernel consumer at these locations:");
162 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
163 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
164 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
165 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
166 DBG3("Found location #1");
167 consumer_to_use
= config
.consumerd64_bin_path
.value
;
168 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
169 DBG3("Found location #2");
170 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
171 } else if (config
.consumerd32_bin_path
.value
&&
172 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
173 DBG3("Found location #3");
174 consumer_to_use
= config
.consumerd32_bin_path
.value
;
176 DBG("Could not find any valid consumerd executable");
180 DBG("Using kernel consumer at: %s", consumer_to_use
);
181 (void) execl(consumer_to_use
,
182 "lttng-consumerd", verbosity
, "-k",
183 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
184 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
185 "--group", config
.tracing_group_name
.value
,
188 case LTTNG_CONSUMER64_UST
:
190 if (config
.consumerd64_lib_dir
.value
) {
195 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
199 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
200 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
205 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
206 if (tmp
[0] != '\0') {
210 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
217 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
218 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
219 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
220 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
221 "--group", config
.tracing_group_name
.value
,
225 case LTTNG_CONSUMER32_UST
:
227 if (config
.consumerd32_lib_dir
.value
) {
232 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
236 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
237 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
242 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
243 if (tmp
[0] != '\0') {
247 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
254 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
255 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
256 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
257 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
258 "--group", config
.tracing_group_name
.value
,
263 ERR("unknown consumer type");
267 PERROR("Consumer execl()");
269 /* Reaching this point, we got a failure on our execl(). */
271 } else if (pid
> 0) {
274 PERROR("start consumer fork");
282 * Spawn the consumerd daemon and session daemon thread.
284 static int start_consumerd(struct consumer_data
*consumer_data
)
289 * Set the listen() state on the socket since there is a possible race
290 * between the exec() of the consumer daemon and this call if place in the
291 * consumer thread. See bug #366 for more details.
293 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
298 pthread_mutex_lock(&consumer_data
->pid_mutex
);
299 if (consumer_data
->pid
!= 0) {
300 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
304 ret
= spawn_consumerd(consumer_data
);
306 ERR("Spawning consumerd failed");
307 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
311 /* Setting up the consumer_data pid */
312 consumer_data
->pid
= ret
;
313 DBG2("Consumer pid %d", consumer_data
->pid
);
314 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
316 DBG2("Spawning consumer control thread");
317 ret
= spawn_consumer_thread(consumer_data
);
319 ERR("Fatal error spawning consumer control thread");
327 /* Cleanup already created sockets on error. */
328 if (consumer_data
->err_sock
>= 0) {
331 err
= close(consumer_data
->err_sock
);
333 PERROR("close consumer data error socket");
340 * Copy consumer output from the tracing session to the domain session. The
341 * function also applies the right modification on a per domain basis for the
342 * trace files destination directory.
344 * Should *NOT* be called with RCU read-side lock held.
346 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
349 const char *dir_name
;
350 struct consumer_output
*consumer
;
353 assert(session
->consumer
);
356 case LTTNG_DOMAIN_KERNEL
:
357 DBG3("Copying tracing session consumer output in kernel session");
359 * XXX: We should audit the session creation and what this function
360 * does "extra" in order to avoid a destroy since this function is used
361 * in the domain session creation (kernel and ust) only. Same for UST
364 if (session
->kernel_session
->consumer
) {
365 consumer_output_put(session
->kernel_session
->consumer
);
367 session
->kernel_session
->consumer
=
368 consumer_copy_output(session
->consumer
);
369 /* Ease our life a bit for the next part */
370 consumer
= session
->kernel_session
->consumer
;
371 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
373 case LTTNG_DOMAIN_JUL
:
374 case LTTNG_DOMAIN_LOG4J
:
375 case LTTNG_DOMAIN_PYTHON
:
376 case LTTNG_DOMAIN_UST
:
377 DBG3("Copying tracing session consumer output in UST session");
378 if (session
->ust_session
->consumer
) {
379 consumer_output_put(session
->ust_session
->consumer
);
381 session
->ust_session
->consumer
=
382 consumer_copy_output(session
->consumer
);
383 /* Ease our life a bit for the next part */
384 consumer
= session
->ust_session
->consumer
;
385 dir_name
= DEFAULT_UST_TRACE_DIR
;
388 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
392 /* Append correct directory to subdir */
393 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
394 sizeof(consumer
->domain_subdir
));
399 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
407 * Create an UST session and add it to the session ust list.
409 * Should *NOT* be called with RCU read-side lock held.
411 static int create_ust_session(struct ltt_session
*session
,
412 const struct lttng_domain
*domain
)
415 struct ltt_ust_session
*lus
= NULL
;
419 assert(session
->consumer
);
421 switch (domain
->type
) {
422 case LTTNG_DOMAIN_JUL
:
423 case LTTNG_DOMAIN_LOG4J
:
424 case LTTNG_DOMAIN_PYTHON
:
425 case LTTNG_DOMAIN_UST
:
428 ERR("Unknown UST domain on create session %d", domain
->type
);
429 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
433 DBG("Creating UST session");
435 lus
= trace_ust_create_session(session
->id
);
437 ret
= LTTNG_ERR_UST_SESS_FAIL
;
441 lus
->uid
= session
->uid
;
442 lus
->gid
= session
->gid
;
443 lus
->output_traces
= session
->output_traces
;
444 lus
->snapshot_mode
= session
->snapshot_mode
;
445 lus
->live_timer_interval
= session
->live_timer
;
446 session
->ust_session
= lus
;
447 if (session
->shm_path
[0]) {
448 strncpy(lus
->root_shm_path
, session
->shm_path
,
449 sizeof(lus
->root_shm_path
));
450 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
451 strncpy(lus
->shm_path
, session
->shm_path
,
452 sizeof(lus
->shm_path
));
453 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
454 strncat(lus
->shm_path
, "/ust",
455 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
457 /* Copy session output to the newly created UST session */
458 ret
= copy_session_consumer(domain
->type
, session
);
459 if (ret
!= LTTNG_OK
) {
467 session
->ust_session
= NULL
;
472 * Create a kernel tracer session then create the default channel.
474 static int create_kernel_session(struct ltt_session
*session
)
478 DBG("Creating kernel session");
480 ret
= kernel_create_session(session
);
482 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
486 /* Code flow safety */
487 assert(session
->kernel_session
);
489 /* Copy session output to the newly created Kernel session */
490 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
491 if (ret
!= LTTNG_OK
) {
495 session
->kernel_session
->uid
= session
->uid
;
496 session
->kernel_session
->gid
= session
->gid
;
497 session
->kernel_session
->output_traces
= session
->output_traces
;
498 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
499 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
504 trace_kernel_destroy_session(session
->kernel_session
);
505 session
->kernel_session
= NULL
;
511 * Count number of session permitted by uid/gid.
513 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
516 struct ltt_session
*session
;
517 const struct ltt_session_list
*session_list
= session_get_list();
519 DBG("Counting number of available session for UID %d GID %d",
521 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
522 if (!session_get(session
)) {
525 session_lock(session
);
526 /* Only count the sessions the user can control. */
527 if (session_access_ok(session
, uid
, gid
) &&
528 !session
->destroyed
) {
531 session_unlock(session
);
532 session_put(session
);
538 * Version of setup_lttng_msg() without command header.
540 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
541 void *payload_buf
, size_t payload_len
)
543 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
547 * Free memory of a command context structure.
549 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
551 DBG("Clean command context structure");
553 if ((*cmd_ctx
)->llm
) {
554 free((*cmd_ctx
)->llm
);
556 if ((*cmd_ctx
)->lsm
) {
557 free((*cmd_ctx
)->lsm
);
565 * Check if the current kernel tracer supports the session rotation feature.
566 * Return 1 if it does, 0 otherwise.
568 static int check_rotate_compatible(void)
572 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
573 DBG("Kernel tracer version is not compatible with the rotation feature");
581 * Send data on a unix socket using the liblttsessiondcomm API.
583 * Return lttcomm error code.
585 static int send_unix_sock(int sock
, void *buf
, size_t len
)
587 /* Check valid length */
592 return lttcomm_send_unix_sock(sock
, buf
, len
);
596 * Process the command requested by the lttng client within the command
597 * context structure. This function make sure that the return structure (llm)
598 * is set and ready for transmission before returning.
600 * Return any error encountered or 0 for success.
602 * "sock" is only used for special-case var. len data.
603 * A command may assume the ownership of the socket, in which case its value
604 * should be set to -1.
606 * Should *NOT* be called with RCU read-side lock held.
608 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
612 int need_tracing_session
= 1;
614 struct lttng_dynamic_buffer payload
;
616 lttng_dynamic_buffer_init(&payload
);
618 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
620 assert(!rcu_read_ongoing());
624 switch (cmd_ctx
->lsm
->cmd_type
) {
625 case LTTNG_CREATE_SESSION_EXT
:
626 case LTTNG_DESTROY_SESSION
:
627 case LTTNG_LIST_SESSIONS
:
628 case LTTNG_LIST_DOMAINS
:
629 case LTTNG_START_TRACE
:
630 case LTTNG_STOP_TRACE
:
631 case LTTNG_DATA_PENDING
:
632 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
633 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
634 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
635 case LTTNG_SNAPSHOT_RECORD
:
636 case LTTNG_SAVE_SESSION
:
637 case LTTNG_SET_SESSION_SHM_PATH
:
638 case LTTNG_REGENERATE_METADATA
:
639 case LTTNG_REGENERATE_STATEDUMP
:
640 case LTTNG_REGISTER_TRIGGER
:
641 case LTTNG_UNREGISTER_TRIGGER
:
642 case LTTNG_ROTATE_SESSION
:
643 case LTTNG_ROTATION_GET_INFO
:
644 case LTTNG_ROTATION_SET_SCHEDULE
:
645 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
646 case LTTNG_CLEAR_SESSION
:
653 if (config
.no_kernel
&& need_domain
654 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
656 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
658 ret
= LTTNG_ERR_KERN_NA
;
663 /* Deny register consumer if we already have a spawned consumer. */
664 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
665 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
666 if (kconsumer_data
.pid
> 0) {
667 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
668 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
671 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
675 * Check for command that don't needs to allocate a returned payload. We do
676 * this here so we don't have to make the call for no payload at each
679 switch(cmd_ctx
->lsm
->cmd_type
) {
680 case LTTNG_LIST_SESSIONS
:
681 case LTTNG_LIST_TRACEPOINTS
:
682 case LTTNG_LIST_TRACEPOINT_FIELDS
:
683 case LTTNG_LIST_DOMAINS
:
684 case LTTNG_LIST_CHANNELS
:
685 case LTTNG_LIST_EVENTS
:
686 case LTTNG_LIST_SYSCALLS
:
687 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
688 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
689 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
690 case LTTNG_DATA_PENDING
:
691 case LTTNG_ROTATE_SESSION
:
692 case LTTNG_ROTATION_GET_INFO
:
695 /* Setup lttng message with no payload */
696 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
698 /* This label does not try to unlock the session */
699 goto init_setup_error
;
703 /* Commands that DO NOT need a session. */
704 switch (cmd_ctx
->lsm
->cmd_type
) {
705 case LTTNG_CREATE_SESSION_EXT
:
706 case LTTNG_LIST_SESSIONS
:
707 case LTTNG_LIST_TRACEPOINTS
:
708 case LTTNG_LIST_SYSCALLS
:
709 case LTTNG_LIST_TRACEPOINT_FIELDS
:
710 case LTTNG_SAVE_SESSION
:
711 case LTTNG_REGISTER_TRIGGER
:
712 case LTTNG_UNREGISTER_TRIGGER
:
713 need_tracing_session
= 0;
716 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
718 * We keep the session list lock across _all_ commands
719 * for now, because the per-session lock does not
720 * handle teardown properly.
723 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
724 if (cmd_ctx
->session
== NULL
) {
725 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
728 /* Acquire lock for the session */
729 session_lock(cmd_ctx
->session
);
735 * Commands that need a valid session but should NOT create one if none
736 * exists. Instead of creating one and destroying it when the command is
737 * handled, process that right before so we save some round trip in useless
740 switch (cmd_ctx
->lsm
->cmd_type
) {
741 case LTTNG_DISABLE_CHANNEL
:
742 case LTTNG_DISABLE_EVENT
:
743 switch (cmd_ctx
->lsm
->domain
.type
) {
744 case LTTNG_DOMAIN_KERNEL
:
745 if (!cmd_ctx
->session
->kernel_session
) {
746 ret
= LTTNG_ERR_NO_CHANNEL
;
750 case LTTNG_DOMAIN_JUL
:
751 case LTTNG_DOMAIN_LOG4J
:
752 case LTTNG_DOMAIN_PYTHON
:
753 case LTTNG_DOMAIN_UST
:
754 if (!cmd_ctx
->session
->ust_session
) {
755 ret
= LTTNG_ERR_NO_CHANNEL
;
760 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
772 * Check domain type for specific "pre-action".
774 switch (cmd_ctx
->lsm
->domain
.type
) {
775 case LTTNG_DOMAIN_KERNEL
:
777 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
781 /* Kernel tracer check */
782 if (!kernel_tracer_is_initialized()) {
783 /* Basically, load kernel tracer modules */
784 ret
= init_kernel_tracer();
790 /* Consumer is in an ERROR state. Report back to client */
791 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
792 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
796 /* Need a session for kernel command */
797 if (need_tracing_session
) {
798 if (cmd_ctx
->session
->kernel_session
== NULL
) {
799 ret
= create_kernel_session(cmd_ctx
->session
);
800 if (ret
!= LTTNG_OK
) {
801 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
806 /* Start the kernel consumer daemon */
807 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
808 if (kconsumer_data
.pid
== 0 &&
809 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
810 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
811 ret
= start_consumerd(&kconsumer_data
);
813 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
816 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
818 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
822 * The consumer was just spawned so we need to add the socket to
823 * the consumer output of the session if exist.
825 ret
= consumer_create_socket(&kconsumer_data
,
826 cmd_ctx
->session
->kernel_session
->consumer
);
833 case LTTNG_DOMAIN_JUL
:
834 case LTTNG_DOMAIN_LOG4J
:
835 case LTTNG_DOMAIN_PYTHON
:
836 case LTTNG_DOMAIN_UST
:
838 if (!ust_app_supported()) {
839 ret
= LTTNG_ERR_NO_UST
;
842 /* Consumer is in an ERROR state. Report back to client */
843 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
844 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
848 if (need_tracing_session
) {
849 /* Create UST session if none exist. */
850 if (cmd_ctx
->session
->ust_session
== NULL
) {
851 ret
= create_ust_session(cmd_ctx
->session
,
852 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
));
853 if (ret
!= LTTNG_OK
) {
858 /* Start the UST consumer daemons */
860 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
861 if (config
.consumerd64_bin_path
.value
&&
862 ustconsumer64_data
.pid
== 0 &&
863 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
864 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
865 ret
= start_consumerd(&ustconsumer64_data
);
867 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
868 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
872 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
873 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
875 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
879 * Setup socket for consumer 64 bit. No need for atomic access
880 * since it was set above and can ONLY be set in this thread.
882 ret
= consumer_create_socket(&ustconsumer64_data
,
883 cmd_ctx
->session
->ust_session
->consumer
);
889 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
890 if (config
.consumerd32_bin_path
.value
&&
891 ustconsumer32_data
.pid
== 0 &&
892 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
893 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
894 ret
= start_consumerd(&ustconsumer32_data
);
896 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
897 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
901 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
902 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
904 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
908 * Setup socket for consumer 32 bit. No need for atomic access
909 * since it was set above and can ONLY be set in this thread.
911 ret
= consumer_create_socket(&ustconsumer32_data
,
912 cmd_ctx
->session
->ust_session
->consumer
);
924 /* Validate consumer daemon state when start/stop trace command */
925 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
926 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
927 switch (cmd_ctx
->lsm
->domain
.type
) {
928 case LTTNG_DOMAIN_NONE
:
930 case LTTNG_DOMAIN_JUL
:
931 case LTTNG_DOMAIN_LOG4J
:
932 case LTTNG_DOMAIN_PYTHON
:
933 case LTTNG_DOMAIN_UST
:
934 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
935 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
939 case LTTNG_DOMAIN_KERNEL
:
940 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
941 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
946 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
952 * Check that the UID or GID match that of the tracing session.
953 * The root user can interact with all sessions.
955 if (need_tracing_session
) {
956 if (!session_access_ok(cmd_ctx
->session
,
957 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
958 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
959 cmd_ctx
->session
->destroyed
) {
960 ret
= LTTNG_ERR_EPERM
;
966 * Send relayd information to consumer as soon as we have a domain and a
969 if (cmd_ctx
->session
&& need_domain
) {
971 * Setup relayd if not done yet. If the relayd information was already
972 * sent to the consumer, this call will gracefully return.
974 ret
= cmd_setup_relayd(cmd_ctx
->session
);
975 if (ret
!= LTTNG_OK
) {
980 /* Process by command type */
981 switch (cmd_ctx
->lsm
->cmd_type
) {
982 case LTTNG_ADD_CONTEXT
:
985 * An LTTNG_ADD_CONTEXT command might have a supplementary
986 * payload if the context being added is an application context.
988 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
989 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
990 char *provider_name
= NULL
, *context_name
= NULL
;
991 size_t provider_name_len
=
992 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
993 size_t context_name_len
=
994 cmd_ctx
->lsm
->u
.context
.context_name_len
;
996 if (provider_name_len
== 0 || context_name_len
== 0) {
998 * Application provider and context names MUST
1001 ret
= -LTTNG_ERR_INVALID
;
1005 provider_name
= zmalloc(provider_name_len
+ 1);
1006 if (!provider_name
) {
1007 ret
= -LTTNG_ERR_NOMEM
;
1010 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1013 context_name
= zmalloc(context_name_len
+ 1);
1014 if (!context_name
) {
1015 ret
= -LTTNG_ERR_NOMEM
;
1016 goto error_add_context
;
1018 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1021 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1024 goto error_add_context
;
1027 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1030 goto error_add_context
;
1035 * cmd_add_context assumes ownership of the provider and context
1038 ret
= cmd_add_context(cmd_ctx
->session
,
1039 cmd_ctx
->lsm
->domain
.type
,
1040 cmd_ctx
->lsm
->u
.context
.channel_name
,
1041 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.context
.ctx
),
1042 kernel_poll_pipe
[1]);
1044 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1045 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1047 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1048 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1054 case LTTNG_DISABLE_CHANNEL
:
1056 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1057 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1060 case LTTNG_DISABLE_EVENT
:
1062 ret
= cmd_disable_event(cmd_ctx
, *sock
);
1065 case LTTNG_ENABLE_CHANNEL
:
1067 ret
= cmd_enable_channel(cmd_ctx
, *sock
, kernel_poll_pipe
[1]);
1070 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1071 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1073 struct lttng_dynamic_buffer payload
;
1074 struct lttng_buffer_view payload_view
;
1075 const bool add_value
=
1076 cmd_ctx
->lsm
->cmd_type
==
1077 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1078 const size_t name_len
=
1079 cmd_ctx
->lsm
->u
.process_attr_tracker_add_remove_include_value
1081 const enum lttng_domain_type domain_type
=
1082 (enum lttng_domain_type
)
1083 cmd_ctx
->lsm
->domain
.type
;
1084 const enum lttng_process_attr process_attr
=
1085 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1086 .process_attr_tracker_add_remove_include_value
1088 const enum lttng_process_attr_value_type value_type
=
1089 (enum lttng_process_attr_value_type
) cmd_ctx
1091 .process_attr_tracker_add_remove_include_value
1093 struct process_attr_value
*value
;
1094 enum lttng_error_code ret_code
;
1096 /* Receive remaining variable length payload if applicable. */
1097 if (name_len
> LOGIN_NAME_MAX
) {
1099 * POSIX mandates user and group names that are at least
1100 * 8 characters long. Note that although shadow-utils
1101 * (useradd, groupaadd, etc.) use 32 chars as their
1102 * limit (from bits/utmp.h, UT_NAMESIZE),
1103 * LOGIN_NAME_MAX is defined to 256.
1105 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %d",
1106 add_value
? "addition" : "removal",
1107 name_len
, LOGIN_NAME_MAX
);
1108 ret
= LTTNG_ERR_INVALID
;
1112 lttng_dynamic_buffer_init(&payload
);
1113 if (name_len
!= 0) {
1115 * Receive variable payload for user/group name
1118 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1120 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1121 add_value
? "add" : "remove");
1122 ret
= LTTNG_ERR_NOMEM
;
1123 goto error_add_remove_tracker_value
;
1126 ret
= lttcomm_recv_unix_sock(
1127 *sock
, payload
.data
, name_len
);
1129 ERR("Failed to receive payload of %s process attribute tracker value argument",
1130 add_value
? "add" : "remove");
1132 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1133 goto error_add_remove_tracker_value
;
1137 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1138 &payload
, 0, name_len
);
1140 * Validate the value type and domains are legal for the process
1141 * attribute tracker that is specified and convert the value to
1142 * add/remove to the internal sessiond representation.
1144 ret_code
= process_attr_value_from_comm(domain_type
,
1145 process_attr
, value_type
,
1146 &cmd_ctx
->lsm
->u
.process_attr_tracker_add_remove_include_value
1148 &payload_view
, &value
);
1149 if (ret_code
!= LTTNG_OK
) {
1151 goto error_add_remove_tracker_value
;
1155 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1156 cmd_ctx
->session
, domain_type
,
1157 process_attr
, value
);
1159 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1160 cmd_ctx
->session
, domain_type
,
1161 process_attr
, value
);
1163 process_attr_value_destroy(value
);
1164 error_add_remove_tracker_value
:
1165 lttng_dynamic_buffer_reset(&payload
);
1168 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1170 enum lttng_tracking_policy tracking_policy
;
1171 const enum lttng_domain_type domain_type
=
1172 (enum lttng_domain_type
)
1173 cmd_ctx
->lsm
->domain
.type
;
1174 const enum lttng_process_attr process_attr
=
1175 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1176 .process_attr_tracker_get_tracking_policy
1179 ret
= cmd_process_attr_tracker_get_tracking_policy(
1180 cmd_ctx
->session
, domain_type
, process_attr
,
1182 if (ret
!= LTTNG_OK
) {
1186 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1187 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1189 ret
= LTTNG_ERR_NOMEM
;
1195 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1197 const enum lttng_tracking_policy tracking_policy
=
1198 (enum lttng_tracking_policy
) cmd_ctx
->lsm
->u
1199 .process_attr_tracker_set_tracking_policy
1201 const enum lttng_domain_type domain_type
=
1202 (enum lttng_domain_type
)
1203 cmd_ctx
->lsm
->domain
.type
;
1204 const enum lttng_process_attr process_attr
=
1205 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1206 .process_attr_tracker_set_tracking_policy
1209 ret
= cmd_process_attr_tracker_set_tracking_policy(
1210 cmd_ctx
->session
, domain_type
, process_attr
,
1212 if (ret
!= LTTNG_OK
) {
1217 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1219 struct lttng_process_attr_values
*values
;
1220 struct lttng_dynamic_buffer reply
;
1221 const enum lttng_domain_type domain_type
=
1222 (enum lttng_domain_type
)
1223 cmd_ctx
->lsm
->domain
.type
;
1224 const enum lttng_process_attr process_attr
=
1225 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1226 .process_attr_tracker_get_inclusion_set
1229 ret
= cmd_process_attr_tracker_get_inclusion_set(
1230 cmd_ctx
->session
, domain_type
, process_attr
,
1232 if (ret
!= LTTNG_OK
) {
1236 lttng_dynamic_buffer_init(&reply
);
1237 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1239 goto error_tracker_get_inclusion_set
;
1242 ret
= setup_lttng_msg_no_cmd_header(
1243 cmd_ctx
, reply
.data
, reply
.size
);
1245 ret
= LTTNG_ERR_NOMEM
;
1246 goto error_tracker_get_inclusion_set
;
1250 error_tracker_get_inclusion_set
:
1251 lttng_process_attr_values_destroy(values
);
1252 lttng_dynamic_buffer_reset(&reply
);
1255 case LTTNG_ENABLE_EVENT
:
1257 ret
= cmd_enable_event(cmd_ctx
, *sock
, kernel_poll_pipe
[1]);
1260 case LTTNG_LIST_TRACEPOINTS
:
1262 enum lttng_error_code ret_code
;
1263 unsigned int nb_events
;
1264 struct lttcomm_list_command_header cmd_header
= { 0 };
1266 session_lock_list();
1267 ret_code
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
,
1268 &payload
, &nb_events
);
1269 session_unlock_list();
1270 if (ret_code
!= LTTNG_OK
) {
1271 ret
= (int) ret_code
;
1275 if (nb_events
> UINT32_MAX
) {
1276 ret
= LTTNG_ERR_OVERFLOW
;
1280 cmd_header
.count
= (uint32_t) nb_events
;
1281 ret
= setup_lttng_msg(cmd_ctx
, payload
.data
, payload
.size
,
1282 &cmd_header
, sizeof(cmd_header
));
1291 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1293 struct lttng_event_field
*fields
;
1296 session_lock_list();
1297 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1299 session_unlock_list();
1300 if (nb_fields
< 0) {
1301 /* Return value is a negative lttng_error_code. */
1307 * Setup lttng message with payload size set to the event list size in
1308 * bytes and then copy list into the llm payload.
1310 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1311 sizeof(struct lttng_event_field
) * nb_fields
);
1321 case LTTNG_LIST_SYSCALLS
:
1323 enum lttng_error_code ret_code
;
1324 uint32_t nb_syscalls
;
1325 struct lttcomm_list_command_header cmd_header
= { 0 };
1327 ret_code
= cmd_list_syscalls(&payload
, &nb_syscalls
);
1328 if (ret_code
!= LTTNG_OK
) {
1329 ret
= (int) ret_code
;
1333 if (nb_syscalls
> UINT32_MAX
) {
1334 ret
= LTTNG_ERR_OVERFLOW
;
1338 cmd_header
.count
= (uint32_t) nb_syscalls
;
1339 ret
= setup_lttng_msg(cmd_ctx
, payload
.data
, payload
.size
,
1340 &cmd_header
, sizeof(cmd_header
));
1349 case LTTNG_SET_CONSUMER_URI
:
1352 struct lttng_uri
*uris
;
1354 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1355 len
= nb_uri
* sizeof(struct lttng_uri
);
1358 ret
= LTTNG_ERR_INVALID
;
1362 uris
= zmalloc(len
);
1364 ret
= LTTNG_ERR_FATAL
;
1368 /* Receive variable len data */
1369 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1370 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1372 DBG("No URIs received from client... continuing");
1374 ret
= LTTNG_ERR_SESSION_FAIL
;
1379 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1381 if (ret
!= LTTNG_OK
) {
1388 case LTTNG_START_TRACE
:
1391 * On the first start, if we have a kernel session and we have
1392 * enabled time or size-based rotations, we have to make sure
1393 * the kernel tracer supports it.
1395 if (!cmd_ctx
->session
->has_been_started
&& \
1396 cmd_ctx
->session
->kernel_session
&& \
1397 (cmd_ctx
->session
->rotate_timer_period
|| \
1398 cmd_ctx
->session
->rotate_size
) && \
1399 !check_rotate_compatible()) {
1400 DBG("Kernel tracer version is not compatible with the rotation feature");
1401 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1404 ret
= cmd_start_trace(cmd_ctx
->session
);
1407 case LTTNG_STOP_TRACE
:
1409 ret
= cmd_stop_trace(cmd_ctx
->session
);
1412 case LTTNG_DESTROY_SESSION
:
1414 ret
= cmd_destroy_session(cmd_ctx
->session
,
1415 notification_thread_handle
,
1419 case LTTNG_LIST_DOMAINS
:
1422 struct lttng_domain
*domains
= NULL
;
1424 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1426 /* Return value is a negative lttng_error_code. */
1431 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1432 nb_dom
* sizeof(struct lttng_domain
));
1442 case LTTNG_LIST_CHANNELS
:
1444 uint32_t nb_channel
;
1445 enum lttng_error_code ret_code
;
1446 struct lttcomm_list_command_header cmd_header
= { 0 };
1448 ret_code
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1449 cmd_ctx
->session
, &payload
, &nb_channel
);
1450 if (ret_code
!= LTTNG_OK
) {
1451 ret
= (int) ret_code
;
1455 cmd_header
.count
= nb_channel
;
1456 ret
= setup_lttng_msg(cmd_ctx
, payload
.data
, payload
.size
,
1457 &cmd_header
, sizeof(cmd_header
));
1466 case LTTNG_LIST_EVENTS
:
1468 enum lttng_error_code ret_code
;
1469 unsigned int nb_events
;
1470 struct lttcomm_list_command_header cmd_header
= { 0 };
1472 ret_code
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1474 cmd_ctx
->lsm
->u
.list
.channel_name
, &payload
,
1477 if (ret_code
!= LTTNG_OK
) {
1478 ret
= (int) ret_code
;
1482 if (nb_events
> UINT32_MAX
) {
1483 ret
= LTTNG_ERR_OVERFLOW
;
1487 cmd_header
.count
= (uint32_t) nb_events
;
1488 ret
= setup_lttng_msg(cmd_ctx
, payload
.data
, payload
.size
,
1489 &cmd_header
, sizeof(cmd_header
));
1498 case LTTNG_LIST_SESSIONS
:
1500 unsigned int nr_sessions
;
1501 void *sessions_payload
;
1504 session_lock_list();
1505 nr_sessions
= lttng_sessions_count(
1506 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1507 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1509 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1510 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1511 sessions_payload
= zmalloc(payload_len
);
1513 if (!sessions_payload
) {
1514 session_unlock_list();
1519 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1520 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1521 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1522 session_unlock_list();
1524 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1526 free(sessions_payload
);
1535 case LTTNG_REGISTER_CONSUMER
:
1537 struct consumer_data
*cdata
;
1539 switch (cmd_ctx
->lsm
->domain
.type
) {
1540 case LTTNG_DOMAIN_KERNEL
:
1541 cdata
= &kconsumer_data
;
1544 ret
= LTTNG_ERR_UND
;
1548 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1549 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1552 case LTTNG_DATA_PENDING
:
1555 uint8_t pending_ret_byte
;
1557 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1562 * This function may returns 0 or 1 to indicate whether or not
1563 * there is data pending. In case of error, it should return an
1564 * LTTNG_ERR code. However, some code paths may still return
1565 * a nondescript error code, which we handle by returning an
1568 if (pending_ret
== 0 || pending_ret
== 1) {
1570 * ret will be set to LTTNG_OK at the end of
1573 } else if (pending_ret
< 0) {
1574 ret
= LTTNG_ERR_UNK
;
1581 pending_ret_byte
= (uint8_t) pending_ret
;
1583 /* 1 byte to return whether or not data is pending */
1584 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1585 &pending_ret_byte
, 1);
1594 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1596 uint32_t snapshot_id
;
1597 struct lttcomm_lttng_output_id reply
;
1599 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1600 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
),
1602 if (ret
!= LTTNG_OK
) {
1605 reply
.id
= snapshot_id
;
1607 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1613 /* Copy output list into message payload */
1617 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1619 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1620 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
));
1623 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1626 struct lttng_snapshot_output
*outputs
= NULL
;
1628 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1629 if (nb_output
< 0) {
1634 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1635 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1636 nb_output
* sizeof(struct lttng_snapshot_output
));
1646 case LTTNG_SNAPSHOT_RECORD
:
1648 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1649 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_record
.output
),
1650 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1653 case LTTNG_CREATE_SESSION_EXT
:
1655 struct lttng_dynamic_buffer payload
;
1656 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1658 lttng_dynamic_buffer_init(&payload
);
1659 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1660 if (ret
!= LTTNG_OK
) {
1664 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1667 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1668 lttng_session_descriptor_destroy(return_descriptor
);
1669 ret
= LTTNG_ERR_NOMEM
;
1672 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1675 lttng_session_descriptor_destroy(return_descriptor
);
1676 ret
= LTTNG_ERR_NOMEM
;
1679 lttng_dynamic_buffer_reset(&payload
);
1680 lttng_session_descriptor_destroy(return_descriptor
);
1684 case LTTNG_SAVE_SESSION
:
1686 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1690 case LTTNG_SET_SESSION_SHM_PATH
:
1692 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1693 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1696 case LTTNG_REGENERATE_METADATA
:
1698 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1701 case LTTNG_REGENERATE_STATEDUMP
:
1703 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1706 case LTTNG_REGISTER_TRIGGER
:
1708 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
1709 notification_thread_handle
);
1712 case LTTNG_UNREGISTER_TRIGGER
:
1714 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
1715 notification_thread_handle
);
1718 case LTTNG_ROTATE_SESSION
:
1720 struct lttng_rotate_session_return rotate_return
;
1722 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1724 memset(&rotate_return
, 0, sizeof(rotate_return
));
1725 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1726 DBG("Kernel tracer version is not compatible with the rotation feature");
1727 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1731 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
1733 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
1739 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
1740 sizeof(rotate_return
));
1749 case LTTNG_ROTATION_GET_INFO
:
1751 struct lttng_rotation_get_info_return get_info_return
;
1753 memset(&get_info_return
, 0, sizeof(get_info_return
));
1754 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
1755 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
1761 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
1762 sizeof(get_info_return
));
1771 case LTTNG_ROTATION_SET_SCHEDULE
:
1774 enum lttng_rotation_schedule_type schedule_type
;
1777 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1778 DBG("Kernel tracer version does not support session rotations");
1779 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1783 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
1784 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
1785 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
1787 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
1791 notification_thread_handle
);
1792 if (ret
!= LTTNG_OK
) {
1798 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1800 struct lttng_session_list_schedules_return schedules
= {
1801 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
1802 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
1803 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
1804 .size
.value
= cmd_ctx
->session
->rotate_size
,
1807 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
1817 case LTTNG_CLEAR_SESSION
:
1819 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
1823 ret
= LTTNG_ERR_UND
;
1828 if (cmd_ctx
->llm
== NULL
) {
1829 DBG("Missing llm structure. Allocating one.");
1830 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
1834 /* Set return code */
1835 cmd_ctx
->llm
->ret_code
= ret
;
1837 if (cmd_ctx
->session
) {
1838 session_unlock(cmd_ctx
->session
);
1839 session_put(cmd_ctx
->session
);
1840 cmd_ctx
->session
= NULL
;
1842 if (need_tracing_session
) {
1843 session_unlock_list();
1846 assert(!rcu_read_ongoing());
1847 lttng_dynamic_buffer_reset(&payload
);
1851 static int create_client_sock(void)
1853 int ret
, client_sock
;
1854 const mode_t old_umask
= umask(0);
1856 /* Create client tool unix socket */
1857 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
1858 if (client_sock
< 0) {
1859 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
1864 /* Set the cloexec flag */
1865 ret
= utils_set_fd_cloexec(client_sock
);
1867 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
1868 "Continuing but note that the consumer daemon will have a "
1869 "reference to this socket on exec()", client_sock
);
1872 /* File permission MUST be 660 */
1873 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1875 ERR("Set file permissions failed: %s",
1876 config
.client_unix_sock_path
.value
);
1878 (void) lttcomm_close_unix_sock(client_sock
);
1882 DBG("Created client socket (fd = %i)", client_sock
);
1889 static void cleanup_client_thread(void *data
)
1891 struct lttng_pipe
*quit_pipe
= data
;
1893 lttng_pipe_destroy(quit_pipe
);
1896 static void thread_init_cleanup(void *data
)
1898 set_thread_status(false);
1902 * This thread manage all clients request using the unix client socket for
1905 static void *thread_manage_clients(void *data
)
1907 int sock
= -1, ret
, i
, pollfd
, err
= -1;
1909 uint32_t revents
, nb_fd
;
1910 struct command_ctx
*cmd_ctx
= NULL
;
1911 struct lttng_poll_event events
;
1912 const int client_sock
= thread_state
.client_sock
;
1913 struct lttng_pipe
*quit_pipe
= data
;
1914 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
1916 DBG("[thread] Manage client started");
1918 is_root
= (getuid() == 0);
1920 pthread_cleanup_push(thread_init_cleanup
, NULL
);
1922 rcu_register_thread();
1924 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
1926 health_code_update();
1928 ret
= lttcomm_listen_unix_sock(client_sock
);
1934 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
1935 * more will be added to this poll set.
1937 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1939 goto error_create_poll
;
1942 /* Add the application registration socket */
1943 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
1948 /* Add thread quit pipe */
1949 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
1954 /* Set state as running. */
1955 set_thread_status(true);
1956 pthread_cleanup_pop(0);
1958 /* This testpoint is after we signal readiness to the parent. */
1959 if (testpoint(sessiond_thread_manage_clients
)) {
1963 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
1967 health_code_update();
1970 const struct cmd_completion_handler
*cmd_completion_handler
;
1972 DBG("Accepting client command ...");
1974 /* Inifinite blocking call, waiting for transmission */
1976 health_poll_entry();
1977 ret
= lttng_poll_wait(&events
, -1);
1981 * Restart interrupted system call.
1983 if (errno
== EINTR
) {
1991 for (i
= 0; i
< nb_fd
; i
++) {
1992 revents
= LTTNG_POLL_GETEV(&events
, i
);
1993 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1995 health_code_update();
1997 if (pollfd
== thread_quit_pipe_fd
) {
2001 /* Event on the registration socket */
2002 if (revents
& LPOLLIN
) {
2004 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2005 ERR("Client socket poll error");
2008 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2014 DBG("Wait for client response");
2016 health_code_update();
2018 sock
= lttcomm_accept_unix_sock(client_sock
);
2024 * Set the CLOEXEC flag. Return code is useless because either way, the
2027 (void) utils_set_fd_cloexec(sock
);
2029 /* Set socket option for credentials retrieval */
2030 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2035 /* Allocate context command to process the client request */
2036 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2037 if (cmd_ctx
== NULL
) {
2038 PERROR("zmalloc cmd_ctx");
2042 /* Allocate data buffer for reception */
2043 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2044 if (cmd_ctx
->lsm
== NULL
) {
2045 PERROR("zmalloc cmd_ctx->lsm");
2049 cmd_ctx
->llm
= NULL
;
2050 cmd_ctx
->session
= NULL
;
2052 health_code_update();
2055 * Data is received from the lttng client. The struct
2056 * lttcomm_session_msg (lsm) contains the command and data request of
2059 DBG("Receiving data from client ...");
2060 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2061 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2063 DBG("Nothing recv() from client... continuing");
2069 clean_command_ctx(&cmd_ctx
);
2073 health_code_update();
2075 // TODO: Validate cmd_ctx including sanity check for
2076 // security purpose.
2078 rcu_thread_online();
2080 * This function dispatch the work to the kernel or userspace tracer
2081 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2082 * informations for the client. The command context struct contains
2083 * everything this function may needs.
2085 ret
= process_client_msg(cmd_ctx
, &sock
, &sock_error
);
2086 rcu_thread_offline();
2096 * TODO: Inform client somehow of the fatal error. At
2097 * this point, ret < 0 means that a zmalloc failed
2098 * (ENOMEM). Error detected but still accept
2099 * command, unless a socket error has been
2102 clean_command_ctx(&cmd_ctx
);
2106 cmd_completion_handler
= cmd_pop_completion_handler();
2107 if (cmd_completion_handler
) {
2108 enum lttng_error_code completion_code
;
2110 completion_code
= cmd_completion_handler
->run(
2111 cmd_completion_handler
->data
);
2112 if (completion_code
!= LTTNG_OK
) {
2113 clean_command_ctx(&cmd_ctx
);
2118 health_code_update();
2121 DBG("Sending response (size: %d, retcode: %s (%d))",
2122 cmd_ctx
->lttng_msg_size
,
2123 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2124 cmd_ctx
->llm
->ret_code
);
2125 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2126 cmd_ctx
->lttng_msg_size
);
2128 ERR("Failed to send data back to client");
2131 /* End of transmission */
2139 clean_command_ctx(&cmd_ctx
);
2141 health_code_update();
2153 lttng_poll_clean(&events
);
2154 clean_command_ctx(&cmd_ctx
);
2158 unlink(config
.client_unix_sock_path
.value
);
2159 ret
= close(client_sock
);
2166 ERR("Health error occurred in %s", __func__
);
2169 health_unregister(health_sessiond
);
2171 DBG("Client thread dying");
2173 rcu_unregister_thread();
2178 bool shutdown_client_thread(void *thread_data
)
2180 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2181 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2183 return notify_thread_pipe(write_fd
) == 1;
2186 struct lttng_thread
*launch_client_thread(void)
2188 bool thread_running
;
2189 struct lttng_pipe
*client_quit_pipe
;
2190 struct lttng_thread
*thread
= NULL
;
2191 int client_sock_fd
= -1;
2193 sem_init(&thread_state
.ready
, 0, 0);
2194 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2195 if (!client_quit_pipe
) {
2199 client_sock_fd
= create_client_sock();
2200 if (client_sock_fd
< 0) {
2204 thread_state
.client_sock
= client_sock_fd
;
2205 thread
= lttng_thread_create("Client management",
2206 thread_manage_clients
,
2207 shutdown_client_thread
,
2208 cleanup_client_thread
,
2213 /* The client thread now owns the client sock fd and the quit pipe. */
2214 client_sock_fd
= -1;
2215 client_quit_pipe
= NULL
;
2218 * This thread is part of the threads that need to be fully
2219 * initialized before the session daemon is marked as "ready".
2221 thread_running
= wait_thread_status();
2222 if (!thread_running
) {
2227 if (client_sock_fd
>= 0) {
2228 if (close(client_sock_fd
)) {
2229 PERROR("Failed to close client socket");
2232 lttng_thread_put(thread
);
2233 cleanup_client_thread(client_quit_pipe
);