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
:
984 ret
= cmd_add_context(cmd_ctx
, *sock
, kernel_poll_pipe
[1]);
987 case LTTNG_DISABLE_CHANNEL
:
989 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
990 cmd_ctx
->lsm
->u
.disable
.channel_name
);
993 case LTTNG_DISABLE_EVENT
:
995 ret
= cmd_disable_event(cmd_ctx
, *sock
);
998 case LTTNG_ENABLE_CHANNEL
:
1000 ret
= cmd_enable_channel(cmd_ctx
, *sock
, kernel_poll_pipe
[1]);
1003 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1004 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1006 struct lttng_dynamic_buffer payload
;
1007 struct lttng_buffer_view payload_view
;
1008 const bool add_value
=
1009 cmd_ctx
->lsm
->cmd_type
==
1010 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1011 const size_t name_len
=
1012 cmd_ctx
->lsm
->u
.process_attr_tracker_add_remove_include_value
1014 const enum lttng_domain_type domain_type
=
1015 (enum lttng_domain_type
)
1016 cmd_ctx
->lsm
->domain
.type
;
1017 const enum lttng_process_attr process_attr
=
1018 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1019 .process_attr_tracker_add_remove_include_value
1021 const enum lttng_process_attr_value_type value_type
=
1022 (enum lttng_process_attr_value_type
) cmd_ctx
1024 .process_attr_tracker_add_remove_include_value
1026 struct process_attr_value
*value
;
1027 enum lttng_error_code ret_code
;
1029 /* Receive remaining variable length payload if applicable. */
1030 if (name_len
> LOGIN_NAME_MAX
) {
1032 * POSIX mandates user and group names that are at least
1033 * 8 characters long. Note that although shadow-utils
1034 * (useradd, groupaadd, etc.) use 32 chars as their
1035 * limit (from bits/utmp.h, UT_NAMESIZE),
1036 * LOGIN_NAME_MAX is defined to 256.
1038 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %d",
1039 add_value
? "addition" : "removal",
1040 name_len
, LOGIN_NAME_MAX
);
1041 ret
= LTTNG_ERR_INVALID
;
1045 lttng_dynamic_buffer_init(&payload
);
1046 if (name_len
!= 0) {
1048 * Receive variable payload for user/group name
1051 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1053 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1054 add_value
? "add" : "remove");
1055 ret
= LTTNG_ERR_NOMEM
;
1056 goto error_add_remove_tracker_value
;
1059 ret
= lttcomm_recv_unix_sock(
1060 *sock
, payload
.data
, name_len
);
1062 ERR("Failed to receive payload of %s process attribute tracker value argument",
1063 add_value
? "add" : "remove");
1065 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1066 goto error_add_remove_tracker_value
;
1070 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1071 &payload
, 0, name_len
);
1073 * Validate the value type and domains are legal for the process
1074 * attribute tracker that is specified and convert the value to
1075 * add/remove to the internal sessiond representation.
1077 ret_code
= process_attr_value_from_comm(domain_type
,
1078 process_attr
, value_type
,
1079 &cmd_ctx
->lsm
->u
.process_attr_tracker_add_remove_include_value
1081 &payload_view
, &value
);
1082 if (ret_code
!= LTTNG_OK
) {
1084 goto error_add_remove_tracker_value
;
1088 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1089 cmd_ctx
->session
, domain_type
,
1090 process_attr
, value
);
1092 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1093 cmd_ctx
->session
, domain_type
,
1094 process_attr
, value
);
1096 process_attr_value_destroy(value
);
1097 error_add_remove_tracker_value
:
1098 lttng_dynamic_buffer_reset(&payload
);
1101 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1103 enum lttng_tracking_policy tracking_policy
;
1104 const enum lttng_domain_type domain_type
=
1105 (enum lttng_domain_type
)
1106 cmd_ctx
->lsm
->domain
.type
;
1107 const enum lttng_process_attr process_attr
=
1108 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1109 .process_attr_tracker_get_tracking_policy
1112 ret
= cmd_process_attr_tracker_get_tracking_policy(
1113 cmd_ctx
->session
, domain_type
, process_attr
,
1115 if (ret
!= LTTNG_OK
) {
1119 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1120 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1122 ret
= LTTNG_ERR_NOMEM
;
1128 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1130 const enum lttng_tracking_policy tracking_policy
=
1131 (enum lttng_tracking_policy
) cmd_ctx
->lsm
->u
1132 .process_attr_tracker_set_tracking_policy
1134 const enum lttng_domain_type domain_type
=
1135 (enum lttng_domain_type
)
1136 cmd_ctx
->lsm
->domain
.type
;
1137 const enum lttng_process_attr process_attr
=
1138 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1139 .process_attr_tracker_set_tracking_policy
1142 ret
= cmd_process_attr_tracker_set_tracking_policy(
1143 cmd_ctx
->session
, domain_type
, process_attr
,
1145 if (ret
!= LTTNG_OK
) {
1150 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1152 struct lttng_process_attr_values
*values
;
1153 struct lttng_dynamic_buffer reply
;
1154 const enum lttng_domain_type domain_type
=
1155 (enum lttng_domain_type
)
1156 cmd_ctx
->lsm
->domain
.type
;
1157 const enum lttng_process_attr process_attr
=
1158 (enum lttng_process_attr
) cmd_ctx
->lsm
->u
1159 .process_attr_tracker_get_inclusion_set
1162 ret
= cmd_process_attr_tracker_get_inclusion_set(
1163 cmd_ctx
->session
, domain_type
, process_attr
,
1165 if (ret
!= LTTNG_OK
) {
1169 lttng_dynamic_buffer_init(&reply
);
1170 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1172 goto error_tracker_get_inclusion_set
;
1175 ret
= setup_lttng_msg_no_cmd_header(
1176 cmd_ctx
, reply
.data
, reply
.size
);
1178 ret
= LTTNG_ERR_NOMEM
;
1179 goto error_tracker_get_inclusion_set
;
1183 error_tracker_get_inclusion_set
:
1184 lttng_process_attr_values_destroy(values
);
1185 lttng_dynamic_buffer_reset(&reply
);
1188 case LTTNG_ENABLE_EVENT
:
1190 ret
= cmd_enable_event(cmd_ctx
, *sock
, kernel_poll_pipe
[1]);
1193 case LTTNG_LIST_TRACEPOINTS
:
1195 enum lttng_error_code ret_code
;
1196 unsigned int nb_events
;
1197 struct lttcomm_list_command_header cmd_header
= { 0 };
1199 session_lock_list();
1200 ret_code
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
,
1201 &payload
, &nb_events
);
1202 session_unlock_list();
1203 if (ret_code
!= LTTNG_OK
) {
1204 ret
= (int) ret_code
;
1208 if (nb_events
> UINT32_MAX
) {
1209 ret
= LTTNG_ERR_OVERFLOW
;
1213 cmd_header
.count
= (uint32_t) nb_events
;
1214 ret
= setup_lttng_msg(cmd_ctx
, payload
.data
, payload
.size
,
1215 &cmd_header
, sizeof(cmd_header
));
1224 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1226 enum lttng_error_code ret_code
;
1227 unsigned int nb_fields
;
1228 struct lttcomm_list_command_header cmd_header
;
1230 session_lock_list();
1231 ret_code
= cmd_list_tracepoint_fields(
1232 cmd_ctx
->lsm
->domain
.type
, &payload
, &nb_fields
);
1233 session_unlock_list();
1234 if (ret_code
!= LTTNG_OK
) {
1235 ret
= (int) ret_code
;
1239 if (nb_fields
> UINT32_MAX
) {
1240 ret
= LTTNG_ERR_OVERFLOW
;
1244 cmd_header
.count
= nb_fields
;
1246 ret
= setup_lttng_msg(cmd_ctx
, payload
.data
, payload
.size
,
1247 &cmd_header
, sizeof(cmd_header
));
1256 case LTTNG_LIST_SYSCALLS
:
1258 enum lttng_error_code ret_code
;
1259 uint32_t nb_syscalls
;
1260 struct lttcomm_list_command_header cmd_header
= { 0 };
1262 ret_code
= cmd_list_syscalls(&payload
, &nb_syscalls
);
1263 if (ret_code
!= LTTNG_OK
) {
1264 ret
= (int) ret_code
;
1268 if (nb_syscalls
> UINT32_MAX
) {
1269 ret
= LTTNG_ERR_OVERFLOW
;
1273 cmd_header
.count
= (uint32_t) nb_syscalls
;
1274 ret
= setup_lttng_msg(cmd_ctx
, payload
.data
, payload
.size
,
1275 &cmd_header
, sizeof(cmd_header
));
1284 case LTTNG_SET_CONSUMER_URI
:
1287 struct lttng_uri
*uris
;
1289 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1290 len
= nb_uri
* sizeof(struct lttng_uri
);
1293 ret
= LTTNG_ERR_INVALID
;
1297 uris
= zmalloc(len
);
1299 ret
= LTTNG_ERR_FATAL
;
1303 /* Receive variable len data */
1304 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1305 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1307 DBG("No URIs received from client... continuing");
1309 ret
= LTTNG_ERR_SESSION_FAIL
;
1314 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1316 if (ret
!= LTTNG_OK
) {
1323 case LTTNG_START_TRACE
:
1326 * On the first start, if we have a kernel session and we have
1327 * enabled time or size-based rotations, we have to make sure
1328 * the kernel tracer supports it.
1330 if (!cmd_ctx
->session
->has_been_started
&& \
1331 cmd_ctx
->session
->kernel_session
&& \
1332 (cmd_ctx
->session
->rotate_timer_period
|| \
1333 cmd_ctx
->session
->rotate_size
) && \
1334 !check_rotate_compatible()) {
1335 DBG("Kernel tracer version is not compatible with the rotation feature");
1336 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1339 ret
= cmd_start_trace(cmd_ctx
->session
);
1342 case LTTNG_STOP_TRACE
:
1344 ret
= cmd_stop_trace(cmd_ctx
->session
);
1347 case LTTNG_DESTROY_SESSION
:
1349 ret
= cmd_destroy_session(cmd_ctx
->session
,
1350 notification_thread_handle
,
1354 case LTTNG_LIST_DOMAINS
:
1357 struct lttng_domain
*domains
= NULL
;
1359 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1361 /* Return value is a negative lttng_error_code. */
1366 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1367 nb_dom
* sizeof(struct lttng_domain
));
1377 case LTTNG_LIST_CHANNELS
:
1379 uint32_t nb_channel
;
1380 enum lttng_error_code ret_code
;
1381 struct lttcomm_list_command_header cmd_header
= { 0 };
1383 ret_code
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1384 cmd_ctx
->session
, &payload
, &nb_channel
);
1385 if (ret_code
!= LTTNG_OK
) {
1386 ret
= (int) ret_code
;
1390 cmd_header
.count
= nb_channel
;
1391 ret
= setup_lttng_msg(cmd_ctx
, payload
.data
, payload
.size
,
1392 &cmd_header
, sizeof(cmd_header
));
1401 case LTTNG_LIST_EVENTS
:
1403 enum lttng_error_code ret_code
;
1404 unsigned int nb_events
;
1405 struct lttcomm_list_command_header cmd_header
= { 0 };
1407 ret_code
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1409 cmd_ctx
->lsm
->u
.list
.channel_name
, &payload
,
1412 if (ret_code
!= LTTNG_OK
) {
1413 ret
= (int) ret_code
;
1417 if (nb_events
> UINT32_MAX
) {
1418 ret
= LTTNG_ERR_OVERFLOW
;
1422 cmd_header
.count
= (uint32_t) nb_events
;
1423 ret
= setup_lttng_msg(cmd_ctx
, payload
.data
, payload
.size
,
1424 &cmd_header
, sizeof(cmd_header
));
1433 case LTTNG_LIST_SESSIONS
:
1435 unsigned int nr_sessions
;
1436 void *sessions_payload
;
1439 session_lock_list();
1440 nr_sessions
= lttng_sessions_count(
1441 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1442 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1444 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1445 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1446 sessions_payload
= zmalloc(payload_len
);
1448 if (!sessions_payload
) {
1449 session_unlock_list();
1454 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1455 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1456 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1457 session_unlock_list();
1459 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1461 free(sessions_payload
);
1470 case LTTNG_REGISTER_CONSUMER
:
1472 struct consumer_data
*cdata
;
1474 switch (cmd_ctx
->lsm
->domain
.type
) {
1475 case LTTNG_DOMAIN_KERNEL
:
1476 cdata
= &kconsumer_data
;
1479 ret
= LTTNG_ERR_UND
;
1483 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1484 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1487 case LTTNG_DATA_PENDING
:
1490 uint8_t pending_ret_byte
;
1492 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1497 * This function may returns 0 or 1 to indicate whether or not
1498 * there is data pending. In case of error, it should return an
1499 * LTTNG_ERR code. However, some code paths may still return
1500 * a nondescript error code, which we handle by returning an
1503 if (pending_ret
== 0 || pending_ret
== 1) {
1505 * ret will be set to LTTNG_OK at the end of
1508 } else if (pending_ret
< 0) {
1509 ret
= LTTNG_ERR_UNK
;
1516 pending_ret_byte
= (uint8_t) pending_ret
;
1518 /* 1 byte to return whether or not data is pending */
1519 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1520 &pending_ret_byte
, 1);
1529 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1531 uint32_t snapshot_id
;
1532 struct lttcomm_lttng_output_id reply
;
1534 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1535 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
),
1537 if (ret
!= LTTNG_OK
) {
1540 reply
.id
= snapshot_id
;
1542 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1548 /* Copy output list into message payload */
1552 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1554 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1555 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
));
1558 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1561 struct lttng_snapshot_output
*outputs
= NULL
;
1563 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1564 if (nb_output
< 0) {
1569 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1570 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1571 nb_output
* sizeof(struct lttng_snapshot_output
));
1581 case LTTNG_SNAPSHOT_RECORD
:
1583 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1584 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_record
.output
),
1585 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1588 case LTTNG_CREATE_SESSION_EXT
:
1590 struct lttng_dynamic_buffer payload
;
1591 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1593 lttng_dynamic_buffer_init(&payload
);
1594 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1595 if (ret
!= LTTNG_OK
) {
1599 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1602 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1603 lttng_session_descriptor_destroy(return_descriptor
);
1604 ret
= LTTNG_ERR_NOMEM
;
1607 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1610 lttng_session_descriptor_destroy(return_descriptor
);
1611 ret
= LTTNG_ERR_NOMEM
;
1614 lttng_dynamic_buffer_reset(&payload
);
1615 lttng_session_descriptor_destroy(return_descriptor
);
1619 case LTTNG_SAVE_SESSION
:
1621 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1625 case LTTNG_SET_SESSION_SHM_PATH
:
1627 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1628 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1631 case LTTNG_REGENERATE_METADATA
:
1633 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1636 case LTTNG_REGENERATE_STATEDUMP
:
1638 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1641 case LTTNG_REGISTER_TRIGGER
:
1643 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
1644 notification_thread_handle
);
1647 case LTTNG_UNREGISTER_TRIGGER
:
1649 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
1650 notification_thread_handle
);
1653 case LTTNG_ROTATE_SESSION
:
1655 struct lttng_rotate_session_return rotate_return
;
1657 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1659 memset(&rotate_return
, 0, sizeof(rotate_return
));
1660 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1661 DBG("Kernel tracer version is not compatible with the rotation feature");
1662 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1666 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
1668 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
1674 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
1675 sizeof(rotate_return
));
1684 case LTTNG_ROTATION_GET_INFO
:
1686 struct lttng_rotation_get_info_return get_info_return
;
1688 memset(&get_info_return
, 0, sizeof(get_info_return
));
1689 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
1690 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
1696 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
1697 sizeof(get_info_return
));
1706 case LTTNG_ROTATION_SET_SCHEDULE
:
1709 enum lttng_rotation_schedule_type schedule_type
;
1712 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1713 DBG("Kernel tracer version does not support session rotations");
1714 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1718 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
1719 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
1720 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
1722 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
1726 notification_thread_handle
);
1727 if (ret
!= LTTNG_OK
) {
1733 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1735 struct lttng_session_list_schedules_return schedules
= {
1736 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
1737 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
1738 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
1739 .size
.value
= cmd_ctx
->session
->rotate_size
,
1742 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
1752 case LTTNG_CLEAR_SESSION
:
1754 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
1758 ret
= LTTNG_ERR_UND
;
1763 if (cmd_ctx
->llm
== NULL
) {
1764 DBG("Missing llm structure. Allocating one.");
1765 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
1769 /* Set return code */
1770 cmd_ctx
->llm
->ret_code
= ret
;
1772 if (cmd_ctx
->session
) {
1773 session_unlock(cmd_ctx
->session
);
1774 session_put(cmd_ctx
->session
);
1775 cmd_ctx
->session
= NULL
;
1777 if (need_tracing_session
) {
1778 session_unlock_list();
1781 assert(!rcu_read_ongoing());
1782 lttng_dynamic_buffer_reset(&payload
);
1786 static int create_client_sock(void)
1788 int ret
, client_sock
;
1789 const mode_t old_umask
= umask(0);
1791 /* Create client tool unix socket */
1792 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
1793 if (client_sock
< 0) {
1794 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
1799 /* Set the cloexec flag */
1800 ret
= utils_set_fd_cloexec(client_sock
);
1802 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
1803 "Continuing but note that the consumer daemon will have a "
1804 "reference to this socket on exec()", client_sock
);
1807 /* File permission MUST be 660 */
1808 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1810 ERR("Set file permissions failed: %s",
1811 config
.client_unix_sock_path
.value
);
1813 (void) lttcomm_close_unix_sock(client_sock
);
1817 DBG("Created client socket (fd = %i)", client_sock
);
1824 static void cleanup_client_thread(void *data
)
1826 struct lttng_pipe
*quit_pipe
= data
;
1828 lttng_pipe_destroy(quit_pipe
);
1831 static void thread_init_cleanup(void *data
)
1833 set_thread_status(false);
1837 * This thread manage all clients request using the unix client socket for
1840 static void *thread_manage_clients(void *data
)
1842 int sock
= -1, ret
, i
, pollfd
, err
= -1;
1844 uint32_t revents
, nb_fd
;
1845 struct command_ctx
*cmd_ctx
= NULL
;
1846 struct lttng_poll_event events
;
1847 const int client_sock
= thread_state
.client_sock
;
1848 struct lttng_pipe
*quit_pipe
= data
;
1849 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
1851 DBG("[thread] Manage client started");
1853 is_root
= (getuid() == 0);
1855 pthread_cleanup_push(thread_init_cleanup
, NULL
);
1857 rcu_register_thread();
1859 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
1861 health_code_update();
1863 ret
= lttcomm_listen_unix_sock(client_sock
);
1869 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
1870 * more will be added to this poll set.
1872 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1874 goto error_create_poll
;
1877 /* Add the application registration socket */
1878 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
1883 /* Add thread quit pipe */
1884 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
1889 /* Set state as running. */
1890 set_thread_status(true);
1891 pthread_cleanup_pop(0);
1893 /* This testpoint is after we signal readiness to the parent. */
1894 if (testpoint(sessiond_thread_manage_clients
)) {
1898 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
1902 health_code_update();
1905 const struct cmd_completion_handler
*cmd_completion_handler
;
1907 DBG("Accepting client command ...");
1909 /* Inifinite blocking call, waiting for transmission */
1911 health_poll_entry();
1912 ret
= lttng_poll_wait(&events
, -1);
1916 * Restart interrupted system call.
1918 if (errno
== EINTR
) {
1926 for (i
= 0; i
< nb_fd
; i
++) {
1927 revents
= LTTNG_POLL_GETEV(&events
, i
);
1928 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1930 health_code_update();
1932 if (pollfd
== thread_quit_pipe_fd
) {
1936 /* Event on the registration socket */
1937 if (revents
& LPOLLIN
) {
1939 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1940 ERR("Client socket poll error");
1943 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1949 DBG("Wait for client response");
1951 health_code_update();
1953 sock
= lttcomm_accept_unix_sock(client_sock
);
1959 * Set the CLOEXEC flag. Return code is useless because either way, the
1962 (void) utils_set_fd_cloexec(sock
);
1964 /* Set socket option for credentials retrieval */
1965 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
1970 /* Allocate context command to process the client request */
1971 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
1972 if (cmd_ctx
== NULL
) {
1973 PERROR("zmalloc cmd_ctx");
1977 /* Allocate data buffer for reception */
1978 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
1979 if (cmd_ctx
->lsm
== NULL
) {
1980 PERROR("zmalloc cmd_ctx->lsm");
1984 cmd_ctx
->llm
= NULL
;
1985 cmd_ctx
->session
= NULL
;
1987 health_code_update();
1990 * Data is received from the lttng client. The struct
1991 * lttcomm_session_msg (lsm) contains the command and data request of
1994 DBG("Receiving data from client ...");
1995 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
1996 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
1998 DBG("Nothing recv() from client... continuing");
2004 clean_command_ctx(&cmd_ctx
);
2008 health_code_update();
2010 // TODO: Validate cmd_ctx including sanity check for
2011 // security purpose.
2013 rcu_thread_online();
2015 * This function dispatch the work to the kernel or userspace tracer
2016 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2017 * informations for the client. The command context struct contains
2018 * everything this function may needs.
2020 ret
= process_client_msg(cmd_ctx
, &sock
, &sock_error
);
2021 rcu_thread_offline();
2031 * TODO: Inform client somehow of the fatal error. At
2032 * this point, ret < 0 means that a zmalloc failed
2033 * (ENOMEM). Error detected but still accept
2034 * command, unless a socket error has been
2037 clean_command_ctx(&cmd_ctx
);
2041 cmd_completion_handler
= cmd_pop_completion_handler();
2042 if (cmd_completion_handler
) {
2043 enum lttng_error_code completion_code
;
2045 completion_code
= cmd_completion_handler
->run(
2046 cmd_completion_handler
->data
);
2047 if (completion_code
!= LTTNG_OK
) {
2048 clean_command_ctx(&cmd_ctx
);
2053 health_code_update();
2056 DBG("Sending response (size: %d, retcode: %s (%d))",
2057 cmd_ctx
->lttng_msg_size
,
2058 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2059 cmd_ctx
->llm
->ret_code
);
2060 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2061 cmd_ctx
->lttng_msg_size
);
2063 ERR("Failed to send data back to client");
2066 /* End of transmission */
2074 clean_command_ctx(&cmd_ctx
);
2076 health_code_update();
2088 lttng_poll_clean(&events
);
2089 clean_command_ctx(&cmd_ctx
);
2093 unlink(config
.client_unix_sock_path
.value
);
2094 ret
= close(client_sock
);
2101 ERR("Health error occurred in %s", __func__
);
2104 health_unregister(health_sessiond
);
2106 DBG("Client thread dying");
2108 rcu_unregister_thread();
2113 bool shutdown_client_thread(void *thread_data
)
2115 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2116 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2118 return notify_thread_pipe(write_fd
) == 1;
2121 struct lttng_thread
*launch_client_thread(void)
2123 bool thread_running
;
2124 struct lttng_pipe
*client_quit_pipe
;
2125 struct lttng_thread
*thread
= NULL
;
2126 int client_sock_fd
= -1;
2128 sem_init(&thread_state
.ready
, 0, 0);
2129 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2130 if (!client_quit_pipe
) {
2134 client_sock_fd
= create_client_sock();
2135 if (client_sock_fd
< 0) {
2139 thread_state
.client_sock
= client_sock_fd
;
2140 thread
= lttng_thread_create("Client management",
2141 thread_manage_clients
,
2142 shutdown_client_thread
,
2143 cleanup_client_thread
,
2148 /* The client thread now owns the client sock fd and the quit pipe. */
2149 client_sock_fd
= -1;
2150 client_quit_pipe
= NULL
;
2153 * This thread is part of the threads that need to be fully
2154 * initialized before the session daemon is marked as "ready".
2156 thread_running
= wait_thread_status();
2157 if (!thread_running
) {
2162 if (client_sock_fd
>= 0) {
2163 if (close(client_sock_fd
)) {
2164 PERROR("Failed to close client socket");
2167 lttng_thread_put(thread
);
2168 cleanup_client_thread(client_quit_pipe
);