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 "agent-thread.hpp"
14 #include "health-sessiond.hpp"
16 #include "lttng-sessiond.hpp"
17 #include "manage-consumer.hpp"
19 #include "testpoint.hpp"
22 #include <common/buffer-view.hpp>
23 #include <common/compat/getenv.hpp>
24 #include <common/compat/socket.hpp>
25 #include <common/dynamic-array.hpp>
26 #include <common/dynamic-buffer.hpp>
27 #include <common/exception.hpp>
28 #include <common/fd-handle.hpp>
29 #include <common/payload-view.hpp>
30 #include <common/payload.hpp>
31 #include <common/pthread-lock.hpp>
32 #include <common/scope-exit.hpp>
33 #include <common/sessiond-comm/sessiond-comm.hpp>
34 #include <common/tracker.hpp>
35 #include <common/unix.hpp>
36 #include <common/utils.hpp>
38 #include <lttng/error-query-internal.hpp>
39 #include <lttng/event-internal.hpp>
40 #include <lttng/lttng.h>
41 #include <lttng/session-descriptor-internal.hpp>
42 #include <lttng/session-internal.hpp>
43 #include <lttng/userspace-probe-internal.hpp>
53 namespace ls
= lttng::sessiond
;
65 static void set_thread_status(bool running
)
67 DBG("Marking client thread's state as %s", running
? "running" : "error");
68 thread_state
.running
= running
;
69 sem_post(&thread_state
.ready
);
72 static bool wait_thread_status()
74 DBG("Waiting for client thread to be ready");
75 sem_wait(&thread_state
.ready
);
76 if (thread_state
.running
) {
77 DBG("Client thread is ready");
79 ERR("Initialization of client thread failed");
82 return thread_state
.running
;
86 * Setup the outgoing data buffer for the response (llm) by allocating the
87 * right amount of memory and copying the original information from the lsm
90 static void setup_lttng_msg(struct command_ctx
*cmd_ctx
,
91 const void *payload_buf
,
93 const void *cmd_header_buf
,
94 size_t cmd_header_len
)
96 const auto header_len
= sizeof(struct lttcomm_lttng_msg
);
97 const auto total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
98 lttcomm_lttng_msg llm
{};
100 llm
.cmd_type
= cmd_ctx
->lsm
.cmd_type
;
101 llm
.pid
= (uint32_t) cmd_ctx
->lsm
.domain
.attr
.pid
;
102 llm
.cmd_header_size
= (uint32_t) cmd_header_len
;
103 llm
.data_size
= (uint32_t) payload_len
;
105 const auto zero_ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
106 LTTNG_ASSERT(zero_ret
== 0);
108 lttng_dynamic_pointer_array_clear(&cmd_ctx
->reply_payload
._fd_handles
);
110 cmd_ctx
->lttng_msg_size
= total_msg_size
;
112 /* Append reply header. */
113 if (lttng_dynamic_buffer_append(&cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
))) {
114 LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
115 "Failed to append the reply header to a client reply", sizeof(llm
));
118 /* Append command header. */
119 if (cmd_header_len
) {
120 if (lttng_dynamic_buffer_append(
121 &cmd_ctx
->reply_payload
.buffer
, cmd_header_buf
, cmd_header_len
)) {
122 LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
123 "Failed to append the command header to a client reply",
128 /* Append payload. */
130 if (lttng_dynamic_buffer_append(
131 &cmd_ctx
->reply_payload
.buffer
, payload_buf
, payload_len
)) {
132 LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
133 "Failed to append the payload to a client reply", payload_len
);
138 static void setup_empty_lttng_msg(struct command_ctx
*cmd_ctx
)
140 const struct lttcomm_lttng_msg llm
= {};
142 const auto zero_ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
143 LTTNG_ASSERT(zero_ret
== 0);
145 /* Append place-holder reply header. */
146 if (lttng_dynamic_buffer_append(&cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
))) {
147 LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
148 "Failed to append the reply header to a client reply", sizeof(llm
));
151 cmd_ctx
->lttng_msg_size
= sizeof(llm
);
154 static void update_lttng_msg(struct command_ctx
*cmd_ctx
, size_t cmd_header_len
, size_t payload_len
)
156 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
157 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
158 struct lttcomm_lttng_msg
*p_llm
;
159 lttcomm_lttng_msg llm
{};
161 llm
.cmd_type
= cmd_ctx
->lsm
.cmd_type
;
162 llm
.pid
= (uint32_t) cmd_ctx
->lsm
.domain
.attr
.pid
;
163 llm
.cmd_header_size
= (uint32_t) cmd_header_len
;
164 llm
.data_size
= (uint32_t) payload_len
;
166 LTTNG_ASSERT(cmd_ctx
->reply_payload
.buffer
.size
>= sizeof(llm
));
168 p_llm
= (typeof(p_llm
)) cmd_ctx
->reply_payload
.buffer
.data
;
170 /* Update existing header. */
171 memcpy(p_llm
, &llm
, sizeof(llm
));
173 cmd_ctx
->lttng_msg_size
= total_msg_size
;
177 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
178 * exec or it will fail.
180 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
182 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
186 * Fork and exec a consumer daemon (consumerd).
188 * Return pid if successful else -1.
190 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
194 const char *consumer_to_use
;
195 const char *verbosity
;
198 DBG("Spawning consumerd");
205 if (the_config
.verbose_consumer
) {
206 verbosity
= "--verbose";
207 } else if (lttng_opt_quiet
) {
208 verbosity
= "--quiet";
213 switch (consumer_data
->type
) {
214 case LTTNG_CONSUMER_KERNEL
:
216 * Find out which consumerd to execute. We will first try the
217 * 64-bit path, then the sessiond's installation directory, and
218 * fallback on the 32-bit one,
220 DBG3("Looking for a kernel consumer at these locations:");
221 DBG3(" 1) %s", the_config
.consumerd64_bin_path
.value
?: "NULL");
222 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
223 DBG3(" 3) %s", the_config
.consumerd32_bin_path
.value
?: "NULL");
224 if (stat(the_config
.consumerd64_bin_path
.value
, &st
) == 0) {
225 DBG3("Found location #1");
226 consumer_to_use
= the_config
.consumerd64_bin_path
.value
;
227 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
228 DBG3("Found location #2");
229 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
230 } else if (the_config
.consumerd32_bin_path
.value
&&
231 stat(the_config
.consumerd32_bin_path
.value
, &st
) == 0) {
232 DBG3("Found location #3");
233 consumer_to_use
= the_config
.consumerd32_bin_path
.value
;
235 DBG("Could not find any valid consumerd executable");
239 DBG("Using kernel consumer at: %s", consumer_to_use
);
240 (void) execl(consumer_to_use
,
244 "--consumerd-cmd-sock",
245 consumer_data
->cmd_unix_sock_path
,
246 "--consumerd-err-sock",
247 consumer_data
->err_unix_sock_path
,
249 the_config
.tracing_group_name
.value
,
252 case LTTNG_CONSUMER64_UST
:
254 if (the_config
.consumerd64_lib_dir
.value
) {
259 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
263 tmplen
= strlen(the_config
.consumerd64_lib_dir
.value
) + 1 /* : */ +
265 tmpnew
= zmalloc
<char>(tmplen
+ 1 /* \0 */);
270 strcat(tmpnew
, the_config
.consumerd64_lib_dir
.value
);
271 if (tmp
[0] != '\0') {
275 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
282 DBG("Using 64-bit UST consumer at: %s",
283 the_config
.consumerd64_bin_path
.value
);
284 (void) execl(the_config
.consumerd64_bin_path
.value
,
288 "--consumerd-cmd-sock",
289 consumer_data
->cmd_unix_sock_path
,
290 "--consumerd-err-sock",
291 consumer_data
->err_unix_sock_path
,
293 the_config
.tracing_group_name
.value
,
297 case LTTNG_CONSUMER32_UST
:
299 if (the_config
.consumerd32_lib_dir
.value
) {
304 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
308 tmplen
= strlen(the_config
.consumerd32_lib_dir
.value
) + 1 /* : */ +
310 tmpnew
= zmalloc
<char>(tmplen
+ 1 /* \0 */);
315 strcat(tmpnew
, the_config
.consumerd32_lib_dir
.value
);
316 if (tmp
[0] != '\0') {
320 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
327 DBG("Using 32-bit UST consumer at: %s",
328 the_config
.consumerd32_bin_path
.value
);
329 (void) execl(the_config
.consumerd32_bin_path
.value
,
333 "--consumerd-cmd-sock",
334 consumer_data
->cmd_unix_sock_path
,
335 "--consumerd-err-sock",
336 consumer_data
->err_unix_sock_path
,
338 the_config
.tracing_group_name
.value
,
343 ERR("unknown consumer type");
347 PERROR("Consumer execl()");
349 /* Reaching this point, we got a failure on our execl(). */
351 } else if (pid
> 0) {
354 PERROR("start consumer fork");
362 * Spawn the consumerd daemon and session daemon thread.
364 static int start_consumerd(struct consumer_data
*consumer_data
)
369 * Set the listen() state on the socket since there is a possible race
370 * between the exec() of the consumer daemon and this call if place in the
371 * consumer thread. See bug #366 for more details.
373 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
378 pthread_mutex_lock(&consumer_data
->pid_mutex
);
379 if (consumer_data
->pid
!= 0) {
380 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
384 ret
= spawn_consumerd(consumer_data
);
386 ERR("Spawning consumerd failed");
387 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
391 /* Setting up the consumer_data pid */
392 consumer_data
->pid
= ret
;
393 DBG2("Consumer pid %d", consumer_data
->pid
);
394 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
396 DBG2("Spawning consumer control thread");
397 ret
= spawn_consumer_thread(consumer_data
);
399 ERR("Fatal error spawning consumer control thread");
407 /* Cleanup already created sockets on error. */
408 if (consumer_data
->err_sock
>= 0) {
411 err
= close(consumer_data
->err_sock
);
413 PERROR("close consumer data error socket");
420 * Copy consumer output from the tracing session to the domain session. The
421 * function also applies the right modification on a per domain basis for the
422 * trace files destination directory.
424 static int copy_session_consumer(int domain
, const ltt_session::locked_ref
& session
)
427 const char *dir_name
;
428 struct consumer_output
*consumer
;
430 LTTNG_ASSERT(session
->consumer
);
433 case LTTNG_DOMAIN_KERNEL
:
434 DBG3("Copying tracing session consumer output in kernel session");
436 * XXX: We should audit the session creation and what this function
437 * does "extra" in order to avoid a destroy since this function is used
438 * in the domain session creation (kernel and ust) only. Same for UST
441 if (session
->kernel_session
->consumer
) {
442 consumer_output_put(session
->kernel_session
->consumer
);
444 session
->kernel_session
->consumer
= consumer_copy_output(session
->consumer
);
445 /* Ease our life a bit for the next part */
446 consumer
= session
->kernel_session
->consumer
;
447 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
449 case LTTNG_DOMAIN_JUL
:
450 case LTTNG_DOMAIN_LOG4J
:
451 case LTTNG_DOMAIN_PYTHON
:
452 case LTTNG_DOMAIN_UST
:
453 DBG3("Copying tracing session consumer output in UST session");
454 if (session
->ust_session
->consumer
) {
455 consumer_output_put(session
->ust_session
->consumer
);
457 session
->ust_session
->consumer
= consumer_copy_output(session
->consumer
);
458 /* Ease our life a bit for the next part */
459 consumer
= session
->ust_session
->consumer
;
460 dir_name
= DEFAULT_UST_TRACE_DIR
;
463 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
467 /* Append correct directory to subdir */
468 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
, sizeof(consumer
->domain_subdir
));
473 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
481 * Create an UST session and add it to the session ust list.
483 static int create_ust_session(const ltt_session::locked_ref
& session
,
484 const struct lttng_domain
*domain
)
487 struct ltt_ust_session
*lus
= nullptr;
489 LTTNG_ASSERT(domain
);
490 LTTNG_ASSERT(session
->consumer
);
492 switch (domain
->type
) {
493 case LTTNG_DOMAIN_JUL
:
494 case LTTNG_DOMAIN_LOG4J
:
495 case LTTNG_DOMAIN_PYTHON
:
496 case LTTNG_DOMAIN_UST
:
499 ERR("Unknown UST domain on create session %d", domain
->type
);
500 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
504 DBG("Creating UST session");
506 lus
= trace_ust_create_session(session
->id
);
507 if (lus
== nullptr) {
508 ret
= LTTNG_ERR_UST_SESS_FAIL
;
512 lus
->uid
= session
->uid
;
513 lus
->gid
= session
->gid
;
514 lus
->output_traces
= session
->output_traces
;
515 lus
->snapshot_mode
= session
->snapshot_mode
;
516 lus
->live_timer_interval
= session
->live_timer
;
517 session
->ust_session
= lus
;
518 if (session
->shm_path
[0]) {
519 strncpy(lus
->root_shm_path
, session
->shm_path
, sizeof(lus
->root_shm_path
));
520 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
521 strncpy(lus
->shm_path
, session
->shm_path
, sizeof(lus
->shm_path
));
522 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
523 strncat(lus
->shm_path
, "/ust", sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
525 /* Copy session output to the newly created UST session */
526 ret
= copy_session_consumer(domain
->type
, session
);
527 if (ret
!= LTTNG_OK
) {
535 session
->ust_session
= nullptr;
540 * Create a kernel tracer session then create the default channel.
542 static int create_kernel_session(const ltt_session::locked_ref
& session
)
546 DBG("Creating kernel session");
548 ret
= kernel_create_session(session
);
550 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
554 /* Code flow safety */
555 LTTNG_ASSERT(session
->kernel_session
);
557 /* Copy session output to the newly created Kernel session */
558 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
559 if (ret
!= LTTNG_OK
) {
563 session
->kernel_session
->uid
= session
->uid
;
564 session
->kernel_session
->gid
= session
->gid
;
565 session
->kernel_session
->output_traces
= session
->output_traces
;
566 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
567 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
572 trace_kernel_destroy_session(session
->kernel_session
);
573 session
->kernel_session
= nullptr;
579 * Count number of session permitted by uid/gid.
581 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
__attribute__((unused
)))
584 struct ltt_session
*raw_session_ptr
;
585 const struct ltt_session_list
*session_list
= session_get_list();
587 DBG("Counting number of available session for UID %d", uid
);
588 cds_list_for_each_entry (raw_session_ptr
, &session_list
->head
, list
) {
589 auto session
= [raw_session_ptr
]() {
590 session_get(raw_session_ptr
);
591 raw_session_ptr
->lock();
592 return ltt_session::locked_ref(*raw_session_ptr
);
595 /* Only count the sessions the user can control. */
596 if (session_access_ok(session
, uid
) && !session
->destroyed
) {
604 static lttng::ctl::trigger
605 receive_lttng_trigger(struct command_ctx
*cmd_ctx
, int sock
, int *sock_error
)
609 ssize_t sock_recv_len
;
610 struct lttng_payload trigger_payload
;
611 struct lttng_trigger
*trigger
= nullptr;
613 lttng_payload_init(&trigger_payload
);
614 const auto reset_payload_on_exit
= lttng::make_scope_exit(
615 [&trigger_payload
]() noexcept
{ lttng_payload_reset(&trigger_payload
); });
617 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
618 ret
= lttng_dynamic_buffer_set_size(&trigger_payload
.buffer
, trigger_len
);
620 LTTNG_THROW_CTL("Failed to allocate buffer for trigger receptio", LTTNG_ERR_NOMEM
);
623 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_payload
.buffer
.data
, trigger_len
);
624 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
626 LTTNG_THROW_PROTOCOL_ERROR("Failed to receive trigger in command payload");
629 /* Receive fds, if any. */
630 if (cmd_ctx
->lsm
.fd_count
> 0) {
631 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
632 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
633 if (sock_recv_len
> 0 && sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
635 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
636 "Failed to receive all file descriptors for trigger in command payload: expected_fd_count={}, ret={}",
637 [cmd_ctx
]() { return cmd_ctx
->lsm
.fd_count
; }(),
639 } else if (sock_recv_len
<= 0) {
641 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
642 "Failed to receive file descriptors for trigger in command payload: expected_fd_count={}, ret={}",
643 [cmd_ctx
]() { return cmd_ctx
->lsm
.fd_count
; }(),
648 /* Deserialize trigger. */
650 struct lttng_payload_view view
=
651 lttng_payload_view_from_payload(&trigger_payload
, 0, -1);
653 const auto trigger_create_ret
= lttng_trigger_create_from_payload(&view
, &trigger
);
655 if (trigger_create_ret
!= trigger_len
) {
656 lttng_trigger_put(trigger
);
657 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
658 "Trigger of unexpected size received as part of command payload: expected_size={}, actual_size={}",
660 trigger_create_ret
));
661 } else if (trigger_create_ret
< 0) {
662 LTTNG_THROW_CTL("Failed to allocate trigger", LTTNG_ERR_NOMEM
);
666 return lttng::ctl::trigger(trigger
);
669 static enum lttng_error_code
receive_lttng_error_query(struct command_ctx
*cmd_ctx
,
672 struct lttng_error_query
**_query
)
676 ssize_t sock_recv_len
;
677 enum lttng_error_code ret_code
;
678 struct lttng_payload query_payload
;
679 struct lttng_error_query
*query
= nullptr;
681 lttng_payload_init(&query_payload
);
682 query_len
= (size_t) cmd_ctx
->lsm
.u
.error_query
.length
;
683 ret
= lttng_dynamic_buffer_set_size(&query_payload
.buffer
, query_len
);
685 ret_code
= LTTNG_ERR_NOMEM
;
689 sock_recv_len
= lttcomm_recv_unix_sock(sock
, query_payload
.buffer
.data
, query_len
);
690 if (sock_recv_len
< 0 || sock_recv_len
!= query_len
) {
691 ERR("Failed to receive error query in command payload");
693 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
697 /* Receive fds, if any. */
698 if (cmd_ctx
->lsm
.fd_count
> 0) {
699 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
700 sock
, cmd_ctx
->lsm
.fd_count
, &query_payload
);
701 if (sock_recv_len
> 0 && sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
702 ERR("Failed to receive all file descriptors for error query in command payload: expected fd count = %u, ret = %d",
703 cmd_ctx
->lsm
.fd_count
,
705 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
708 } else if (sock_recv_len
<= 0) {
709 ERR("Failed to receive file descriptors for error query in command payload: expected fd count = %u, ret = %d",
710 cmd_ctx
->lsm
.fd_count
,
712 ret_code
= LTTNG_ERR_FATAL
;
718 /* Deserialize error query. */
720 struct lttng_payload_view view
=
721 lttng_payload_view_from_payload(&query_payload
, 0, -1);
723 if (lttng_error_query_create_from_payload(&view
, &query
) != query_len
) {
724 ERR("Invalid error query received as part of command payload");
725 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
734 lttng_payload_reset(&query_payload
);
738 static enum lttng_error_code
receive_lttng_event(struct command_ctx
*cmd_ctx
,
741 struct lttng_event
**out_event
,
742 char **out_filter_expression
,
743 struct lttng_bytecode
**out_bytecode
,
744 struct lttng_event_exclusion
**out_exclusion
)
748 ssize_t sock_recv_len
;
749 enum lttng_error_code ret_code
;
750 struct lttng_payload event_payload
;
751 struct lttng_event
*local_event
= nullptr;
752 char *local_filter_expression
= nullptr;
753 struct lttng_bytecode
*local_bytecode
= nullptr;
754 struct lttng_event_exclusion
*local_exclusion
= nullptr;
756 lttng_payload_init(&event_payload
);
757 if (cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
) {
758 event_len
= (size_t) cmd_ctx
->lsm
.u
.enable
.length
;
759 } else if (cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
) {
760 event_len
= (size_t) cmd_ctx
->lsm
.u
.disable
.length
;
765 ret
= lttng_dynamic_buffer_set_size(&event_payload
.buffer
, event_len
);
767 ret_code
= LTTNG_ERR_NOMEM
;
771 sock_recv_len
= lttcomm_recv_unix_sock(sock
, event_payload
.buffer
.data
, event_len
);
772 if (sock_recv_len
< 0 || sock_recv_len
!= event_len
) {
773 ERR("Failed to receive event in command payload");
775 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
779 /* Receive fds, if any. */
780 if (cmd_ctx
->lsm
.fd_count
> 0) {
781 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
782 sock
, cmd_ctx
->lsm
.fd_count
, &event_payload
);
783 if (sock_recv_len
> 0 && sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
784 ERR("Failed to receive all file descriptors for event in command payload: expected fd count = %u, ret = %d",
785 cmd_ctx
->lsm
.fd_count
,
787 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
790 } else if (sock_recv_len
<= 0) {
791 ERR("Failed to receive file descriptors for event in command payload: expected fd count = %u, ret = %d",
792 cmd_ctx
->lsm
.fd_count
,
794 ret_code
= LTTNG_ERR_FATAL
;
800 /* Deserialize event. */
803 struct lttng_payload_view event_view
=
804 lttng_payload_view_from_payload(&event_payload
, 0, -1);
806 len
= lttng_event_create_from_payload(&event_view
,
809 &local_filter_expression
,
813 ERR("Failed to create an event from the received buffer");
814 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
818 if (len
!= event_len
) {
819 ERR("Userspace probe location from the received buffer is not the advertised length: header length = %zu" PRIu32
820 ", payload length = %zd",
823 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
828 *out_event
= local_event
;
829 *out_exclusion
= local_exclusion
;
830 *out_filter_expression
= local_filter_expression
;
831 *out_bytecode
= local_bytecode
;
832 local_event
= nullptr;
833 local_exclusion
= nullptr;
834 local_filter_expression
= nullptr;
835 local_bytecode
= nullptr;
840 lttng_payload_reset(&event_payload
);
841 lttng_event_destroy(local_event
);
842 free(local_filter_expression
);
843 free(local_bytecode
);
844 free(local_exclusion
);
848 static enum lttng_error_code
849 receive_lttng_event_context(const struct command_ctx
*cmd_ctx
,
852 struct lttng_event_context
**out_event_context
)
855 const size_t event_context_len
= (size_t) cmd_ctx
->lsm
.u
.context
.length
;
856 ssize_t sock_recv_len
;
857 enum lttng_error_code ret_code
;
858 struct lttng_payload event_context_payload
;
859 struct lttng_event_context
*context
= nullptr;
861 lttng_payload_init(&event_context_payload
);
863 ret
= lttng_dynamic_buffer_set_size(&event_context_payload
.buffer
, event_context_len
);
865 ret_code
= LTTNG_ERR_NOMEM
;
870 lttcomm_recv_unix_sock(sock
, event_context_payload
.buffer
.data
, event_context_len
);
871 if (sock_recv_len
< 0 || sock_recv_len
!= event_context_len
) {
872 ERR("Failed to receive event context in command payload");
874 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
878 /* Deserialize event. */
881 struct lttng_payload_view event_context_view
=
882 lttng_payload_view_from_payload(&event_context_payload
, 0, -1);
884 len
= lttng_event_context_create_from_payload(&event_context_view
, &context
);
887 ERR("Failed to create a event context from the received buffer");
888 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
892 if (len
!= event_context_len
) {
893 ERR("Event context from the received buffer is not the advertised length: expected length = %zu, payload length = %zd",
896 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
901 *out_event_context
= context
;
906 lttng_event_context_destroy(context
);
907 lttng_payload_reset(&event_context_payload
);
912 * Version of setup_lttng_msg() without command header.
915 setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
, void *payload_buf
, size_t payload_len
)
917 setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, nullptr, 0);
921 * Check if the current kernel tracer supports the session rotation feature.
922 * Return 1 if it does, 0 otherwise.
924 static int check_rotate_compatible()
928 if (the_kernel_tracer_version
.major
!= 2 || the_kernel_tracer_version
.minor
< 11) {
929 DBG("Kernel tracer version is not compatible with the rotation feature");
937 * Send data on a unix socket using the liblttsessiondcomm API.
939 * Return lttcomm error code.
941 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
944 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
946 /* Check valid length */
947 if (view
->buffer
.size
== 0) {
952 ret
= lttcomm_send_unix_sock(sock
, view
->buffer
.data
, view
->buffer
.size
);
958 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
968 static void command_ctx_set_status_code(command_ctx
& cmd_ctx
, enum lttng_error_code status_code
)
970 LTTNG_ASSERT(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(lttcomm_lttng_msg
));
971 ((struct lttcomm_lttng_msg
*) (cmd_ctx
.reply_payload
.buffer
.data
))->ret_code
= status_code
;
975 * Process the command requested by the lttng client within the command
976 * context structure. This function make sure that the return structure (llm)
977 * is set and ready for transmission before returning.
979 * Return any error encountered or 0 for success.
981 * "sock" is only used for special-case var. len data.
982 * A command may assume the ownership of the socket, in which case its value
983 * should be set to -1.
985 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
, int *sock_error
)
988 bool need_tracing_session
= true;
992 if (!lttcomm_sessiond_command_is_valid((lttcomm_sessiond_command
) cmd_ctx
->lsm
.cmd_type
)) {
993 /* The lambda is used since fmt can't bind a packed field. */
994 LTTNG_THROW_CTL(fmt::format("Unknown client command: command_id={}",
995 [&cmd_ctx
]() { return cmd_ctx
->lsm
.cmd_type
; }()),
999 DBG_FMT("Processing client command: name=`{}`, id={}",
1000 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) cmd_ctx
->lsm
.cmd_type
),
1001 [&cmd_ctx
]() { return cmd_ctx
->lsm
.cmd_type
; }());
1005 switch (cmd_ctx
->lsm
.cmd_type
) {
1006 case LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
:
1007 case LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
:
1008 case LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
:
1009 case LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
:
1010 case LTTCOMM_SESSIOND_COMMAND_START_TRACE
:
1011 case LTTCOMM_SESSIOND_COMMAND_STOP_TRACE
:
1012 case LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
:
1013 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_ADD_OUTPUT
:
1014 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_DEL_OUTPUT
:
1015 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_LIST_OUTPUT
:
1016 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_RECORD
:
1017 case LTTCOMM_SESSIOND_COMMAND_SAVE_SESSION
:
1018 case LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH
:
1019 case LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA
:
1020 case LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP
:
1021 case LTTCOMM_SESSIOND_COMMAND_ROTATE_SESSION
:
1022 case LTTCOMM_SESSIOND_COMMAND_ROTATION_GET_INFO
:
1023 case LTTCOMM_SESSIOND_COMMAND_ROTATION_SET_SCHEDULE
:
1024 case LTTCOMM_SESSIOND_COMMAND_SESSION_LIST_ROTATION_SCHEDULES
:
1025 case LTTCOMM_SESSIOND_COMMAND_CLEAR_SESSION
:
1026 case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
:
1027 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
1028 case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
:
1029 need_domain
= false;
1035 /* Needs a functioning consumerd? */
1036 switch (cmd_ctx
->lsm
.cmd_type
) {
1037 case LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
:
1038 case LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
:
1039 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
1040 need_consumerd
= false;
1043 need_consumerd
= true;
1047 if (the_config
.no_kernel
&& need_domain
&&
1048 cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
1051 "Can't run a kernel-domain command since the session daemon is not running as root",
1052 LTTNG_ERR_NEED_ROOT_SESSIOND
);
1055 "Can't run a kernel-domain command since kernel tracing is disabled",
1060 /* Deny register consumer if we already have a spawned consumer. */
1061 if (cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
) {
1062 const lttng::pthread::lock_guard
kconsumer_lock(the_kconsumer_data
.pid_mutex
);
1064 if (the_kconsumer_data
.pid
> 0) {
1066 "Can't register a consumer since a kernel-domain consumer was already launched",
1067 LTTNG_ERR_KERN_CONSUMER_FAIL
);
1072 * Check for command that don't needs to allocate a returned payload. We do
1073 * this here so we don't have to make the call for no payload at each
1076 switch (cmd_ctx
->lsm
.cmd_type
) {
1077 case LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
:
1078 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
:
1079 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
:
1080 case LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
:
1081 case LTTCOMM_SESSIOND_COMMAND_LIST_CHANNELS
:
1082 case LTTCOMM_SESSIOND_COMMAND_LIST_EVENTS
:
1083 case LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS
:
1084 case LTTCOMM_SESSIOND_COMMAND_SESSION_LIST_ROTATION_SCHEDULES
:
1085 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_GET_POLICY
:
1086 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1087 case LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
:
1088 case LTTCOMM_SESSIOND_COMMAND_ROTATE_SESSION
:
1089 case LTTCOMM_SESSIOND_COMMAND_ROTATION_GET_INFO
:
1090 case LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
:
1091 case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
:
1092 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
1095 /* Setup lttng message with no payload */
1096 setup_lttng_msg_no_cmd_header(cmd_ctx
, nullptr, 0);
1100 * The list lock is only acquired when processing a command that is applied
1101 * against a session. As such, a unique_lock that holds the list lock is move()'d to
1102 * list_lock during the execution of those commands. The list lock is then released
1103 * as the instance leaves this scope.
1105 * Mind the order of the declaration of list_lock vs target_session:
1106 * the session list lock must always be released _after_ the release of
1107 * a session's reference (the destruction of a ref/locked_ref) to ensure
1108 * since the reference's release may unpublish the session from the list of
1111 std::unique_lock
<std::mutex
> list_lock
;
1113 * A locked_ref is typically "never null" (hence its name). However, due to the
1114 * structure of this function, target_session remains unset for commands that don't
1115 * have a target session.
1117 nonstd::optional
<ltt_session::locked_ref
> target_session
;
1119 /* Commands that DO NOT need a session. */
1120 switch (cmd_ctx
->lsm
.cmd_type
) {
1121 case LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
:
1122 case LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
:
1123 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
:
1124 case LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS
:
1125 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
:
1126 case LTTCOMM_SESSIOND_COMMAND_SAVE_SESSION
:
1127 case LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
:
1128 case LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
:
1129 case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
:
1130 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
1131 case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
:
1132 need_tracing_session
= false;
1135 if (strnlen(cmd_ctx
->lsm
.session
.name
, sizeof(cmd_ctx
->lsm
.session
.name
)) ==
1136 sizeof(cmd_ctx
->lsm
.session
.name
)) {
1137 LTTNG_THROW_INVALID_ARGUMENT_ERROR(
1138 "Session name received from lttng-ctl client is not null-terminated");
1141 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
1143 * We keep the session list lock across _all_ commands
1144 * for now, because the per-session lock does not
1145 * handle teardown properly.
1147 list_lock
= lttng::sessiond::lock_session_list();
1149 target_session
.emplace(
1150 ltt_session::find_locked_session(cmd_ctx
->lsm
.session
.name
));
1152 std::throw_with_nested(lttng::ctl::error(
1154 "Target session of command doesn't exist: command='{}'",
1155 lttcomm_sessiond_command_str(
1156 (lttcomm_sessiond_command
) cmd_ctx
->lsm
.cmd_type
)),
1157 LTTNG_ERR_SESS_NOT_FOUND
,
1158 LTTNG_SOURCE_LOCATION()));
1161 LTTNG_ASSERT(target_session
);
1166 * Commands that need a valid session but should NOT create one if none
1167 * exists. Instead of creating one and destroying it when the command is
1168 * handled, process that right before so we save some round trip in useless
1171 switch (cmd_ctx
->lsm
.cmd_type
) {
1172 case LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL
:
1173 case LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
:
1174 switch (cmd_ctx
->lsm
.domain
.type
) {
1175 case LTTNG_DOMAIN_KERNEL
:
1176 if (!(*target_session
)->kernel_session
) {
1177 return LTTNG_ERR_NO_CHANNEL
;
1180 case LTTNG_DOMAIN_JUL
:
1181 case LTTNG_DOMAIN_LOG4J
:
1182 case LTTNG_DOMAIN_PYTHON
:
1183 case LTTNG_DOMAIN_UST
:
1184 if (!(*target_session
)->ust_session
) {
1185 return LTTNG_ERR_NO_CHANNEL
;
1190 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1202 * Check domain type for specific "pre-action".
1204 switch (cmd_ctx
->lsm
.domain
.type
) {
1205 case LTTNG_DOMAIN_KERNEL
:
1207 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1211 /* Kernel tracer check */
1212 if (!kernel_tracer_is_initialized()) {
1213 /* Basically, load kernel tracer modules */
1214 ret
= init_kernel_tracer();
1220 /* Consumer is in an ERROR state. Report back to client */
1221 if (need_consumerd
&& uatomic_read(&the_kernel_consumerd_state
) == CONSUMER_ERROR
) {
1222 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1226 /* Need a session for kernel command */
1227 if (need_tracing_session
) {
1228 if ((*target_session
)->kernel_session
== nullptr) {
1229 ret
= create_kernel_session(*target_session
);
1230 if (ret
!= LTTNG_OK
) {
1231 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1236 /* Start the kernel consumer daemon */
1237 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1238 if (the_kconsumer_data
.pid
== 0 &&
1239 cmd_ctx
->lsm
.cmd_type
!= LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
) {
1240 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1241 ret
= start_consumerd(&the_kconsumer_data
);
1243 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1246 uatomic_set(&the_kernel_consumerd_state
, CONSUMER_STARTED
);
1248 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1252 * The consumer was just spawned so we need to add the socket to
1253 * the consumer output of the session if exist.
1255 ret
= consumer_create_socket(&the_kconsumer_data
,
1256 (*target_session
)->kernel_session
->consumer
);
1263 case LTTNG_DOMAIN_JUL
:
1264 case LTTNG_DOMAIN_LOG4J
:
1265 case LTTNG_DOMAIN_PYTHON
:
1266 if (!agent_tracing_is_enabled()) {
1267 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1271 case LTTNG_DOMAIN_UST
:
1273 if (!ust_app_supported()) {
1274 ret
= LTTNG_ERR_NO_UST
;
1278 /* Consumer is in an ERROR state. Report back to client */
1279 if (need_consumerd
&& uatomic_read(&the_ust_consumerd_state
) == CONSUMER_ERROR
) {
1280 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1284 if (need_tracing_session
) {
1285 /* Create UST session if none exist. */
1286 if ((*target_session
)->ust_session
== nullptr) {
1287 lttng_domain domain
= cmd_ctx
->lsm
.domain
;
1288 ret
= create_ust_session(*target_session
, &domain
);
1289 if (ret
!= LTTNG_OK
) {
1294 /* Start the UST consumer daemons */
1296 pthread_mutex_lock(&the_ustconsumer64_data
.pid_mutex
);
1297 if (the_config
.consumerd64_bin_path
.value
&&
1298 the_ustconsumer64_data
.pid
== 0 &&
1299 cmd_ctx
->lsm
.cmd_type
!= LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
) {
1300 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1301 ret
= start_consumerd(&the_ustconsumer64_data
);
1303 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1304 uatomic_set(&the_ust_consumerd64_fd
, -EINVAL
);
1308 uatomic_set(&the_ust_consumerd64_fd
,
1309 the_ustconsumer64_data
.cmd_sock
);
1310 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1312 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1316 * Setup socket for consumer 64 bit. No need for atomic access
1317 * since it was set above and can ONLY be set in this thread.
1319 ret
= consumer_create_socket(&the_ustconsumer64_data
,
1320 (*target_session
)->ust_session
->consumer
);
1326 pthread_mutex_lock(&the_ustconsumer32_data
.pid_mutex
);
1327 if (the_config
.consumerd32_bin_path
.value
&&
1328 the_ustconsumer32_data
.pid
== 0 &&
1329 cmd_ctx
->lsm
.cmd_type
!= LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
) {
1330 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1331 ret
= start_consumerd(&the_ustconsumer32_data
);
1333 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1334 uatomic_set(&the_ust_consumerd32_fd
, -EINVAL
);
1338 uatomic_set(&the_ust_consumerd32_fd
,
1339 the_ustconsumer32_data
.cmd_sock
);
1340 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1342 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1346 * Setup socket for consumer 32 bit. No need for atomic access
1347 * since it was set above and can ONLY be set in this thread.
1349 ret
= consumer_create_socket(&the_ustconsumer32_data
,
1350 (*target_session
)->ust_session
->consumer
);
1362 /* Validate consumer daemon state when start/stop trace command */
1363 if (cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_START_TRACE
||
1364 cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_STOP_TRACE
) {
1365 switch (cmd_ctx
->lsm
.domain
.type
) {
1366 case LTTNG_DOMAIN_NONE
:
1368 case LTTNG_DOMAIN_JUL
:
1369 case LTTNG_DOMAIN_LOG4J
:
1370 case LTTNG_DOMAIN_PYTHON
:
1371 case LTTNG_DOMAIN_UST
:
1372 if (uatomic_read(&the_ust_consumerd_state
) != CONSUMER_STARTED
) {
1373 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1377 case LTTNG_DOMAIN_KERNEL
:
1378 if (uatomic_read(&the_kernel_consumerd_state
) != CONSUMER_STARTED
) {
1379 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1384 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1390 * Check that the UID matches that of the tracing session.
1391 * The root user can interact with all sessions.
1393 if (need_tracing_session
) {
1394 if (!session_access_ok(*target_session
, LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1395 (*target_session
)->destroyed
) {
1396 ret
= LTTNG_ERR_EPERM
;
1402 * Send relayd information to consumer as soon as we have a domain and a
1405 if (target_session
&& need_domain
) {
1407 * Setup relayd if not done yet. If the relayd information was already
1408 * sent to the consumer, this call will gracefully return.
1410 ret
= cmd_setup_relayd(*target_session
);
1411 if (ret
!= LTTNG_OK
) {
1416 /* Process by command type */
1417 switch (cmd_ctx
->lsm
.cmd_type
) {
1418 case LTTCOMM_SESSIOND_COMMAND_ADD_CONTEXT
:
1420 struct lttng_event_context
*event_context
= nullptr;
1421 const enum lttng_error_code ret_code
=
1422 receive_lttng_event_context(cmd_ctx
, *sock
, sock_error
, &event_context
);
1424 if (ret_code
!= LTTNG_OK
) {
1425 ret
= (int) ret_code
;
1429 ret
= cmd_add_context(
1430 cmd_ctx
, *target_session
, event_context
, the_kernel_poll_pipe
[1]);
1431 lttng_event_context_destroy(event_context
);
1434 case LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL
:
1436 ret
= cmd_disable_channel(*target_session
,
1437 cmd_ctx
->lsm
.domain
.type
,
1438 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1441 case LTTCOMM_SESSIOND_COMMAND_ENABLE_CHANNEL
:
1443 ret
= cmd_enable_channel(cmd_ctx
, *target_session
, *sock
, the_kernel_poll_pipe
[1]);
1446 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1447 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1449 struct lttng_dynamic_buffer payload
;
1450 struct lttng_buffer_view payload_view
;
1451 const bool add_value
= cmd_ctx
->lsm
.cmd_type
==
1452 LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1453 const size_t name_len
=
1454 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
.name_len
;
1455 const enum lttng_domain_type domain_type
=
1456 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1457 const enum lttng_process_attr process_attr
=
1458 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1459 .process_attr_tracker_add_remove_include_value
.process_attr
;
1460 const enum lttng_process_attr_value_type value_type
=
1461 (enum lttng_process_attr_value_type
) cmd_ctx
->lsm
.u
1462 .process_attr_tracker_add_remove_include_value
.value_type
;
1463 struct process_attr_value
*value
;
1464 enum lttng_error_code ret_code
;
1465 long login_name_max
;
1467 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1468 if (login_name_max
< 0) {
1469 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1470 ret
= LTTNG_ERR_INVALID
;
1474 /* Receive remaining variable length payload if applicable. */
1475 if (name_len
> login_name_max
) {
1477 * POSIX mandates user and group names that are at least
1478 * 8 characters long. Note that although shadow-utils
1479 * (useradd, groupaadd, etc.) use 32 chars as their
1480 * limit (from bits/utmp.h, UT_NAMESIZE),
1481 * LOGIN_NAME_MAX is defined to 256.
1483 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1484 add_value
? "addition" : "removal",
1487 ret
= LTTNG_ERR_INVALID
;
1491 lttng_dynamic_buffer_init(&payload
);
1492 if (name_len
!= 0) {
1494 * Receive variable payload for user/group name
1497 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1499 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1500 add_value
? "add" : "remove");
1501 ret
= LTTNG_ERR_NOMEM
;
1502 goto error_add_remove_tracker_value
;
1505 ret
= lttcomm_recv_unix_sock(*sock
, payload
.data
, name_len
);
1507 ERR("Failed to receive payload of %s process attribute tracker value argument",
1508 add_value
? "add" : "remove");
1510 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1511 goto error_add_remove_tracker_value
;
1515 payload_view
= lttng_buffer_view_from_dynamic_buffer(&payload
, 0, name_len
);
1516 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1517 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1518 goto error_add_remove_tracker_value
;
1522 * Validate the value type and domains are legal for the process
1523 * attribute tracker that is specified and convert the value to
1524 * add/remove to the internal sessiond representation.
1526 ret_code
= process_attr_value_from_comm(
1530 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
.integral_value
,
1533 if (ret_code
!= LTTNG_OK
) {
1535 goto error_add_remove_tracker_value
;
1539 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1540 *target_session
, domain_type
, process_attr
, value
);
1542 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1543 *target_session
, domain_type
, process_attr
, value
);
1545 process_attr_value_destroy(value
);
1546 error_add_remove_tracker_value
:
1547 lttng_dynamic_buffer_reset(&payload
);
1550 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_GET_POLICY
:
1552 enum lttng_tracking_policy tracking_policy
;
1553 const enum lttng_domain_type domain_type
=
1554 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1555 const enum lttng_process_attr process_attr
=
1556 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1557 .process_attr_tracker_get_tracking_policy
.process_attr
;
1559 ret
= cmd_process_attr_tracker_get_tracking_policy(
1560 *target_session
, domain_type
, process_attr
, &tracking_policy
);
1561 if (ret
!= LTTNG_OK
) {
1565 uint32_t tracking_policy_u32
= tracking_policy
;
1566 setup_lttng_msg_no_cmd_header(cmd_ctx
, &tracking_policy_u32
, sizeof(uint32_t));
1571 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_SET_POLICY
:
1573 const enum lttng_tracking_policy tracking_policy
=
1574 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1575 .process_attr_tracker_set_tracking_policy
.tracking_policy
;
1576 const enum lttng_domain_type domain_type
=
1577 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1578 const enum lttng_process_attr process_attr
=
1579 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1580 .process_attr_tracker_set_tracking_policy
.process_attr
;
1582 ret
= cmd_process_attr_tracker_set_tracking_policy(
1583 *target_session
, domain_type
, process_attr
, tracking_policy
);
1584 if (ret
!= LTTNG_OK
) {
1589 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1591 struct lttng_process_attr_values
*values
;
1592 struct lttng_dynamic_buffer reply
;
1593 const enum lttng_domain_type domain_type
=
1594 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1595 const enum lttng_process_attr process_attr
=
1596 (enum lttng_process_attr
)
1597 cmd_ctx
->lsm
.u
.process_attr_tracker_get_inclusion_set
.process_attr
;
1599 ret
= cmd_process_attr_tracker_get_inclusion_set(
1600 *target_session
, domain_type
, process_attr
, &values
);
1601 if (ret
!= LTTNG_OK
) {
1605 lttng_dynamic_buffer_init(&reply
);
1606 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1608 goto error_tracker_get_inclusion_set
;
1611 setup_lttng_msg_no_cmd_header(cmd_ctx
, reply
.data
, reply
.size
);
1614 error_tracker_get_inclusion_set
:
1615 lttng_process_attr_values_destroy(values
);
1616 lttng_dynamic_buffer_reset(&reply
);
1619 case LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
:
1620 case LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
:
1622 struct lttng_event
*event
;
1623 char *filter_expression
;
1624 struct lttng_event_exclusion
*exclusions
;
1625 struct lttng_bytecode
*bytecode
;
1626 const enum lttng_error_code ret_code
= receive_lttng_event(cmd_ctx
,
1634 if (ret_code
!= LTTNG_OK
) {
1635 ret
= (int) ret_code
;
1640 * Ownership of filter_expression, exclusions, and bytecode is
1641 * always transferred.
1643 ret
= cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
?
1644 cmd_enable_event(cmd_ctx
,
1650 the_kernel_poll_pipe
[1]) :
1651 cmd_disable_event(cmd_ctx
,
1657 lttng_event_destroy(event
);
1660 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
:
1662 enum lttng_error_code ret_code
;
1663 size_t original_payload_size
;
1664 size_t payload_size
;
1665 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1667 setup_empty_lttng_msg(cmd_ctx
);
1669 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1671 list_lock
= lttng::sessiond::lock_session_list();
1672 ret_code
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &cmd_ctx
->reply_payload
);
1673 if (ret_code
!= LTTNG_OK
) {
1674 ret
= (int) ret_code
;
1678 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1679 original_payload_size
;
1680 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1685 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
:
1687 enum lttng_error_code ret_code
;
1688 size_t original_payload_size
;
1689 size_t payload_size
;
1690 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1692 setup_empty_lttng_msg(cmd_ctx
);
1694 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1696 list_lock
= lttng::sessiond::lock_session_list();
1697 ret_code
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1698 &cmd_ctx
->reply_payload
);
1700 if (ret_code
!= LTTNG_OK
) {
1701 ret
= (int) ret_code
;
1705 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1706 original_payload_size
;
1707 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1712 case LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS
:
1714 enum lttng_error_code ret_code
;
1715 size_t original_payload_size
;
1716 size_t payload_size
;
1717 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1719 setup_empty_lttng_msg(cmd_ctx
);
1721 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1723 ret_code
= cmd_list_syscalls(&cmd_ctx
->reply_payload
);
1724 if (ret_code
!= LTTNG_OK
) {
1725 ret
= (int) ret_code
;
1729 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1730 original_payload_size
;
1731 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1736 case LTTCOMM_SESSIOND_COMMAND_SET_CONSUMER_URI
:
1739 struct lttng_uri
*uris
;
1741 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1742 len
= nb_uri
* sizeof(struct lttng_uri
);
1745 ret
= LTTNG_ERR_INVALID
;
1749 uris
= calloc
<lttng_uri
>(nb_uri
);
1750 if (uris
== nullptr) {
1751 ret
= LTTNG_ERR_FATAL
;
1755 /* Receive variable len data */
1756 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1757 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1759 DBG("No URIs received from client... continuing");
1761 ret
= LTTNG_ERR_SESSION_FAIL
;
1766 ret
= cmd_set_consumer_uri(*target_session
, nb_uri
, uris
);
1768 if (ret
!= LTTNG_OK
) {
1774 case LTTCOMM_SESSIOND_COMMAND_START_TRACE
:
1777 * On the first start, if we have a kernel session and we have
1778 * enabled time or size-based rotations, we have to make sure
1779 * the kernel tracer supports it.
1781 if (!(*target_session
)->has_been_started
&& (*target_session
)->kernel_session
&&
1782 ((*target_session
)->rotate_timer_period
|| (*target_session
)->rotate_size
) &&
1783 !check_rotate_compatible()) {
1784 DBG("Kernel tracer version is not compatible with the rotation feature");
1785 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1788 ret
= cmd_start_trace(*target_session
);
1791 case LTTCOMM_SESSIOND_COMMAND_STOP_TRACE
:
1793 ret
= cmd_stop_trace(*target_session
);
1796 case LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
:
1798 ret
= cmd_destroy_session(*target_session
, sock
);
1801 case LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
:
1804 struct lttng_domain
*domains
= nullptr;
1806 nb_dom
= cmd_list_domains(*target_session
, &domains
);
1808 /* Return value is a negative lttng_error_code. */
1813 setup_lttng_msg_no_cmd_header(
1814 cmd_ctx
, domains
, nb_dom
* sizeof(struct lttng_domain
));
1820 case LTTCOMM_SESSIOND_COMMAND_LIST_CHANNELS
:
1822 enum lttng_error_code ret_code
;
1823 size_t original_payload_size
;
1824 size_t payload_size
;
1825 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1827 setup_empty_lttng_msg(cmd_ctx
);
1829 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1831 ret_code
= cmd_list_channels(
1832 cmd_ctx
->lsm
.domain
.type
, *target_session
, &cmd_ctx
->reply_payload
);
1833 if (ret_code
!= LTTNG_OK
) {
1834 ret
= (int) ret_code
;
1838 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1839 original_payload_size
;
1840 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1845 case LTTCOMM_SESSIOND_COMMAND_LIST_EVENTS
:
1847 enum lttng_error_code ret_code
;
1848 size_t original_payload_size
;
1849 size_t payload_size
;
1850 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1852 setup_empty_lttng_msg(cmd_ctx
);
1854 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1856 ret_code
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1858 cmd_ctx
->lsm
.u
.list
.channel_name
,
1859 &cmd_ctx
->reply_payload
);
1860 if (ret_code
!= LTTNG_OK
) {
1861 ret
= (int) ret_code
;
1865 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1866 original_payload_size
;
1867 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1872 case LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
:
1874 unsigned int nr_sessions
;
1875 lttng_session
*sessions_payload
= nullptr;
1876 size_t payload_len
= 0;
1878 list_lock
= lttng::sessiond::lock_session_list();
1879 nr_sessions
= lttng_sessions_count(LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1880 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1882 if (nr_sessions
> 0) {
1883 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1884 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1885 sessions_payload
= zmalloc
<lttng_session
>(payload_len
);
1886 if (!sessions_payload
) {
1887 LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
1888 "Failed to allocate session list reply payload",
1892 cmd_list_lttng_sessions(sessions_payload
,
1894 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1895 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1898 setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
, payload_len
);
1899 free(sessions_payload
);
1904 case LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
:
1906 struct consumer_data
*cdata
;
1908 switch (cmd_ctx
->lsm
.domain
.type
) {
1909 case LTTNG_DOMAIN_KERNEL
:
1910 cdata
= &the_kconsumer_data
;
1913 ret
= LTTNG_ERR_UND
;
1917 ret
= cmd_register_consumer(
1918 *target_session
, cmd_ctx
->lsm
.domain
.type
, cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1921 case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
:
1924 enum lttng_kernel_tracer_status status
;
1926 ret
= cmd_kernel_tracer_status(&status
);
1927 if (ret
!= LTTNG_OK
) {
1931 u_status
= (uint32_t) status
;
1932 setup_lttng_msg_no_cmd_header(cmd_ctx
, &u_status
, 4);
1937 case LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
:
1940 uint8_t pending_ret_byte
;
1942 pending_ret
= cmd_data_pending(*target_session
);
1947 * This function may returns 0 or 1 to indicate whether or not
1948 * there is data pending. In case of error, it should return an
1949 * LTTNG_ERR code. However, some code paths may still return
1950 * a nondescript error code, which we handle by returning an
1953 if (pending_ret
== 0 || pending_ret
== 1) {
1955 * ret will be set to LTTNG_OK at the end of
1958 } else if (pending_ret
<= LTTNG_OK
|| pending_ret
>= LTTNG_ERR_NR
) {
1959 ret
= LTTNG_ERR_UNK
;
1966 pending_ret_byte
= (uint8_t) pending_ret
;
1968 /* 1 byte to return whether or not data is pending */
1969 setup_lttng_msg_no_cmd_header(cmd_ctx
, &pending_ret_byte
, 1);
1974 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_ADD_OUTPUT
:
1976 uint32_t snapshot_id
;
1977 struct lttcomm_lttng_output_id reply
;
1978 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
1980 ret
= cmd_snapshot_add_output(*target_session
, &output
, &snapshot_id
);
1981 if (ret
!= LTTNG_OK
) {
1984 reply
.id
= snapshot_id
;
1986 setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
, sizeof(reply
));
1988 /* Copy output list into message payload */
1992 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_DEL_OUTPUT
:
1994 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
1995 ret
= cmd_snapshot_del_output(*target_session
, &output
);
1998 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_LIST_OUTPUT
:
2001 struct lttng_snapshot_output
*outputs
= nullptr;
2003 nb_output
= cmd_snapshot_list_outputs(*target_session
, &outputs
);
2004 if (nb_output
< 0) {
2009 LTTNG_ASSERT((nb_output
> 0 && outputs
) || nb_output
== 0);
2010 setup_lttng_msg_no_cmd_header(
2011 cmd_ctx
, outputs
, nb_output
* sizeof(struct lttng_snapshot_output
));
2017 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_RECORD
:
2019 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_record
.output
;
2020 ret
= cmd_snapshot_record(*target_session
, &output
, 0); // RFC: set to zero
2021 // since it's ignored
2023 // cmd_snapshot_record
2026 case LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
:
2028 struct lttng_dynamic_buffer payload
;
2030 lttng_dynamic_buffer_init(&payload
);
2032 lttng::ctl::session_descriptor reply_session_descriptor
= [cmd_ctx
, sock
]() {
2033 lttng_session_descriptor
*raw_descriptor
;
2034 const auto create_ret
= cmd_create_session(cmd_ctx
, *sock
, &raw_descriptor
);
2035 if (create_ret
!= LTTNG_OK
) {
2036 LTTNG_THROW_CTL("Failed to create session", create_ret
);
2039 return lttng::ctl::session_descriptor(raw_descriptor
);
2042 ret
= lttng_session_descriptor_serialize(reply_session_descriptor
.get(), &payload
);
2045 "Failed to serialize session descriptor in reply to \"create session\" command",
2049 setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
, payload
.size
);
2051 lttng_dynamic_buffer_reset(&payload
);
2055 case LTTCOMM_SESSIOND_COMMAND_SAVE_SESSION
:
2057 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
, &cmd_ctx
->creds
);
2060 case LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH
:
2062 ret
= cmd_set_session_shm_path(*target_session
,
2063 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2066 case LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA
:
2068 ret
= cmd_regenerate_metadata(*target_session
);
2071 case LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP
:
2073 ret
= cmd_regenerate_statedump(*target_session
);
2076 case LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
:
2078 size_t original_reply_payload_size
;
2079 size_t reply_payload_size
;
2080 const struct lttng_credentials cmd_creds
= {
2081 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2082 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2085 setup_empty_lttng_msg(cmd_ctx
);
2087 auto payload_trigger
= receive_lttng_trigger(cmd_ctx
, *sock
, sock_error
);
2088 if (ret
!= LTTNG_OK
) {
2092 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2094 auto return_trigger
=
2095 cmd_register_trigger(&cmd_creds
,
2096 payload_trigger
.get(),
2097 cmd_ctx
->lsm
.u
.trigger
.is_trigger_anonymous
,
2098 the_notification_thread_handle
);
2100 ret
= lttng_trigger_serialize(return_trigger
.get(), &cmd_ctx
->reply_payload
);
2103 "Failed to serialize trigger in reply to \"register trigger\" command",
2107 reply_payload_size
=
2108 cmd_ctx
->reply_payload
.buffer
.size
- original_reply_payload_size
;
2110 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2115 case LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
:
2117 const struct lttng_credentials cmd_creds
= {
2118 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2119 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2122 auto payload_trigger
= receive_lttng_trigger(cmd_ctx
, *sock
, sock_error
);
2124 ret
= cmd_unregister_trigger(
2125 &cmd_creds
, payload_trigger
.get(), the_notification_thread_handle
);
2128 case LTTCOMM_SESSIOND_COMMAND_ROTATE_SESSION
:
2130 struct lttng_rotate_session_return rotate_return
;
2132 DBG("Client rotate session \"%s\"", (*target_session
)->name
);
2134 memset(&rotate_return
, 0, sizeof(rotate_return
));
2135 if ((*target_session
)->kernel_session
&& !check_rotate_compatible()) {
2136 DBG("Kernel tracer version is not compatible with the rotation feature");
2137 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2141 ret
= cmd_rotate_session(*target_session
,
2144 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2150 setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
, sizeof(rotate_return
));
2155 case LTTCOMM_SESSIOND_COMMAND_ROTATION_GET_INFO
:
2157 struct lttng_rotation_get_info_return get_info_return
;
2159 memset(&get_info_return
, 0, sizeof(get_info_return
));
2160 ret
= cmd_rotate_get_info(*target_session
,
2162 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2168 setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
, sizeof(get_info_return
));
2173 case LTTCOMM_SESSIOND_COMMAND_ROTATION_SET_SCHEDULE
:
2176 enum lttng_rotation_schedule_type schedule_type
;
2179 if ((*target_session
)->kernel_session
&& !check_rotate_compatible()) {
2180 DBG("Kernel tracer version does not support session rotations");
2181 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2185 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2186 schedule_type
= (enum lttng_rotation_schedule_type
)
2187 cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2188 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2190 ret
= cmd_rotation_set_schedule(
2191 *target_session
, set_schedule
, schedule_type
, value
);
2192 if (ret
!= LTTNG_OK
) {
2198 case LTTCOMM_SESSIOND_COMMAND_SESSION_LIST_ROTATION_SCHEDULES
:
2200 lttng_session_list_schedules_return schedules
;
2202 schedules
.periodic
.set
= !!(*target_session
)->rotate_timer_period
;
2203 schedules
.periodic
.value
= (*target_session
)->rotate_timer_period
;
2204 schedules
.size
.set
= !!(*target_session
)->rotate_size
;
2205 schedules
.size
.value
= (*target_session
)->rotate_size
;
2207 setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
, sizeof(schedules
));
2212 case LTTCOMM_SESSIOND_COMMAND_CLEAR_SESSION
:
2214 ret
= cmd_clear_session(*target_session
, sock
);
2217 case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
:
2219 struct lttng_triggers
*return_triggers
= nullptr;
2220 size_t original_payload_size
;
2221 size_t payload_size
;
2223 setup_empty_lttng_msg(cmd_ctx
);
2225 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2227 ret
= cmd_list_triggers(cmd_ctx
, the_notification_thread_handle
, &return_triggers
);
2228 if (ret
!= LTTNG_OK
) {
2232 LTTNG_ASSERT(return_triggers
);
2233 ret
= lttng_triggers_serialize(return_triggers
, &cmd_ctx
->reply_payload
);
2234 lttng_triggers_destroy(return_triggers
);
2236 ERR("Failed to serialize triggers in reply to `list triggers` command");
2237 ret
= LTTNG_ERR_NOMEM
;
2241 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- original_payload_size
;
2243 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2248 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
2250 struct lttng_error_query
*query
;
2251 const struct lttng_credentials cmd_creds
= {
2252 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2253 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2255 struct lttng_error_query_results
*results
= nullptr;
2256 size_t original_payload_size
;
2257 size_t payload_size
;
2259 setup_empty_lttng_msg(cmd_ctx
);
2261 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2263 ret
= receive_lttng_error_query(cmd_ctx
, *sock
, sock_error
, &query
);
2264 if (ret
!= LTTNG_OK
) {
2268 ret
= cmd_execute_error_query(
2269 &cmd_creds
, query
, &results
, the_notification_thread_handle
);
2270 lttng_error_query_destroy(query
);
2271 if (ret
!= LTTNG_OK
) {
2275 LTTNG_ASSERT(results
);
2276 ret
= lttng_error_query_results_serialize(results
, &cmd_ctx
->reply_payload
);
2277 lttng_error_query_results_destroy(results
);
2279 ERR("Failed to serialize error query result set in reply to `execute error query` command");
2280 ret
= LTTNG_ERR_NOMEM
;
2284 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- original_payload_size
;
2286 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2293 ret
= LTTNG_ERR_UND
;
2298 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2299 DBG("Missing llm header, creating one.");
2300 setup_lttng_msg_no_cmd_header(cmd_ctx
, nullptr, 0);
2303 command_ctx_set_status_code(*cmd_ctx
, static_cast<lttng_error_code
>(ret
));
2304 LTTNG_ASSERT(!rcu_read_ongoing());
2308 static int create_client_sock()
2310 int ret
, client_sock
;
2312 /* Create client tool unix socket */
2313 client_sock
= lttcomm_create_unix_sock(the_config
.client_unix_sock_path
.value
);
2314 if (client_sock
< 0) {
2315 ERR("Create unix sock failed: %s", the_config
.client_unix_sock_path
.value
);
2320 /* Set the cloexec flag */
2321 ret
= utils_set_fd_cloexec(client_sock
);
2323 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2324 "Continuing but note that the consumer daemon will have a "
2325 "reference to this socket on exec()",
2329 /* File permission MUST be 660 */
2330 ret
= chmod(the_config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2332 ERR("Set file permissions failed: %s", the_config
.client_unix_sock_path
.value
);
2334 (void) lttcomm_close_unix_sock(client_sock
);
2338 DBG("Created client socket (fd = %i)", client_sock
);
2344 static void cleanup_client_thread(void *data
)
2346 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2348 lttng_pipe_destroy(quit_pipe
);
2351 static void thread_init_cleanup(void *data
__attribute__((unused
)))
2353 set_thread_status(false);
2356 // Helper function to log the source_location if the exception is derived from lttng::runtime_error
2357 template <typename ExceptionType
>
2358 typename
std::enable_if
<std::is_base_of
<lttng::runtime_error
, ExceptionType
>::value
,
2360 formatted_source_location(const ExceptionType
& ex
)
2362 return fmt::format("{}", ex
.source_location
);
2365 template <typename ExceptionType
>
2366 typename
std::enable_if
<!std::is_base_of
<lttng::runtime_error
, ExceptionType
>::value
,
2368 formatted_source_location(const ExceptionType
&)
2373 template <class ExceptionType
>
2374 static void log_nested_exceptions(const ExceptionType
& ex
, unsigned int level
= 0)
2376 const auto location
= formatted_source_location(ex
);
2379 if (location
.size()) {
2380 WARN_FMT("Client request failed: {}, location='{}'", ex
.what(), location
);
2382 WARN_FMT("Client request failed: {}", ex
.what());
2385 if (location
.size()) {
2386 WARN_FMT("\t{}, location='{}'", ex
.what(), location
);
2388 WARN_FMT("\t{}", ex
.what());
2393 std::rethrow_if_nested(ex
);
2394 } catch (const lttng::runtime_error
& nested_ex
) {
2395 log_nested_exceptions(nested_ex
, level
+ 1);
2396 } catch (const std::exception
& nested_ex
) {
2397 log_nested_exceptions(nested_ex
, level
+ 1);
2402 * This thread manage all clients request using the unix client socket for
2405 static void *thread_manage_clients(void *data
)
2407 int sock
= -1, ret
, i
, err
= -1;
2410 struct lttng_poll_event events
;
2411 const int client_sock
= thread_state
.client_sock
;
2412 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2413 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2414 struct command_ctx cmd_ctx
= {};
2416 DBG("[thread] Manage client started");
2418 lttng_payload_init(&cmd_ctx
.reply_payload
);
2420 is_root
= (getuid() == 0);
2422 pthread_cleanup_push(thread_init_cleanup
, nullptr);
2424 rcu_register_thread();
2426 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2428 health_code_update();
2430 ret
= lttcomm_listen_unix_sock(client_sock
);
2436 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2437 * more will be added to this poll set.
2439 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2441 goto error_create_poll
;
2444 /* Add the application registration socket */
2445 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2450 /* Add thread quit pipe */
2451 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
);
2456 /* Set state as running. */
2457 set_thread_status(true);
2458 pthread_cleanup_pop(0);
2460 /* This testpoint is after we signal readiness to the parent. */
2461 if (testpoint(sessiond_thread_manage_clients
)) {
2465 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2469 health_code_update();
2472 const struct cmd_completion_handler
*cmd_completion_handler
;
2474 cmd_ctx
.creds
.uid
= UINT32_MAX
;
2475 cmd_ctx
.creds
.gid
= UINT32_MAX
;
2476 cmd_ctx
.creds
.pid
= 0;
2477 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2478 cmd_ctx
.lttng_msg_size
= 0;
2480 DBG("Accepting client command ...");
2482 /* Inifinite blocking call, waiting for transmission */
2484 health_poll_entry();
2485 ret
= lttng_poll_wait(&events
, -1);
2489 * Restart interrupted system call.
2491 if (errno
== EINTR
) {
2499 for (i
= 0; i
< nb_fd
; i
++) {
2500 /* Fetch once the poll data. */
2501 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
2502 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2504 health_code_update();
2506 /* Activity on thread quit pipe, exiting. */
2507 if (pollfd
== thread_quit_pipe_fd
) {
2508 DBG("Activity on thread quit pipe");
2513 /* Event on the registration socket */
2514 if (revents
& LPOLLIN
) {
2516 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2517 ERR("Client socket poll error");
2520 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2525 DBG("Wait for client response");
2527 health_code_update();
2529 sock
= lttcomm_accept_unix_sock(client_sock
);
2535 * Set the CLOEXEC flag. Return code is useless because either way, the
2538 (void) utils_set_fd_cloexec(sock
);
2540 /* Set socket option for credentials retrieval */
2541 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2546 health_code_update();
2549 * Data is received from the lttng client. The struct
2550 * lttcomm_session_msg (lsm) contains the command and data request of
2553 DBG("Receiving data from client ...");
2554 ret
= lttcomm_recv_creds_unix_sock(
2555 sock
, &cmd_ctx
.lsm
, sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2556 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2557 DBG("Incomplete recv() from client... continuing");
2566 health_code_update();
2568 // TODO: Validate cmd_ctx including sanity check for
2569 // security purpose.
2571 rcu_thread_online();
2573 * This function dispatch the work to the kernel or userspace tracer
2574 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2575 * informations for the client. The command context struct contains
2576 * everything this function may needs.
2579 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2580 rcu_thread_offline();
2581 } catch (const std::bad_alloc
& ex
) {
2582 log_nested_exceptions(ex
);
2583 ret
= LTTNG_ERR_NOMEM
;
2584 } catch (const lttng::ctl::error
& ex
) {
2585 log_nested_exceptions(ex
);
2587 } catch (const lttng::invalid_argument_error
& ex
) {
2588 log_nested_exceptions(ex
);
2589 ret
= LTTNG_ERR_INVALID
;
2590 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
2591 log_nested_exceptions(ex
);
2592 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2593 } catch (const lttng::runtime_error
& ex
) {
2594 log_nested_exceptions(ex
);
2595 ret
= LTTNG_ERR_UNK
;
2596 } catch (const std::exception
& ex
) {
2597 log_nested_exceptions(ex
);
2598 ret
= LTTNG_ERR_UNK
;
2601 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2602 WARN("Command returned an invalid status code, returning unknown error: "
2603 "command type = %s (%d), ret = %d",
2604 lttcomm_sessiond_command_str(
2605 (lttcomm_sessiond_command
) cmd_ctx
.lsm
.cmd_type
),
2606 cmd_ctx
.lsm
.cmd_type
,
2608 ret
= LTTNG_ERR_UNK
;
2611 if (ret
!= LTTNG_OK
) {
2613 * Reset the payload contents as the command may have left them in an
2614 * inconsistent state.
2616 setup_empty_lttng_msg(&cmd_ctx
);
2619 command_ctx_set_status_code(cmd_ctx
, static_cast<lttng_error_code
>(ret
));
2621 cmd_completion_handler
= cmd_pop_completion_handler();
2622 if (cmd_completion_handler
) {
2623 enum lttng_error_code completion_code
;
2625 completion_code
= cmd_completion_handler
->run(cmd_completion_handler
->data
);
2626 if (completion_code
!= LTTNG_OK
) {
2631 health_code_update();
2634 struct lttng_payload_view view
=
2635 lttng_payload_view_from_payload(&cmd_ctx
.reply_payload
, 0, -1);
2636 struct lttcomm_lttng_msg
*llm
=
2637 (typeof(llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2639 LTTNG_ASSERT(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2640 LTTNG_ASSERT(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2642 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2644 DBG("Sending response (size: %d, retcode: %s (%d))",
2645 cmd_ctx
.lttng_msg_size
,
2646 lttng_strerror(-llm
->ret_code
),
2648 ret
= send_unix_sock(sock
, &view
);
2650 ERR("Failed to send data back to client");
2653 /* End of transmission */
2661 health_code_update();
2673 lttng_poll_clean(&events
);
2677 unlink(the_config
.client_unix_sock_path
.value
);
2678 ret
= close(client_sock
);
2685 ERR("Health error occurred in %s", __func__
);
2688 health_unregister(the_health_sessiond
);
2690 DBG("Client thread dying");
2691 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2692 rcu_unregister_thread();
2696 static bool shutdown_client_thread(void *thread_data
)
2698 struct lttng_pipe
*client_quit_pipe
= (lttng_pipe
*) thread_data
;
2699 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2701 return notify_thread_pipe(write_fd
) == 1;
2704 struct lttng_thread
*launch_client_thread()
2706 bool thread_running
;
2707 struct lttng_pipe
*client_quit_pipe
;
2708 struct lttng_thread
*thread
= nullptr;
2709 int client_sock_fd
= -1;
2711 sem_init(&thread_state
.ready
, 0, 0);
2712 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2713 if (!client_quit_pipe
) {
2717 client_sock_fd
= create_client_sock();
2718 if (client_sock_fd
< 0) {
2722 thread_state
.client_sock
= client_sock_fd
;
2723 thread
= lttng_thread_create("Client management",
2724 thread_manage_clients
,
2725 shutdown_client_thread
,
2726 cleanup_client_thread
,
2731 /* The client thread now owns the client sock fd and the quit pipe. */
2732 client_sock_fd
= -1;
2733 client_quit_pipe
= nullptr;
2736 * This thread is part of the threads that need to be fully
2737 * initialized before the session daemon is marked as "ready".
2739 thread_running
= wait_thread_status();
2740 if (!thread_running
) {
2745 if (client_sock_fd
>= 0) {
2746 if (close(client_sock_fd
)) {
2747 PERROR("Failed to close client socket");
2750 lttng_thread_put(thread
);
2751 cleanup_client_thread(client_quit_pipe
);