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
, struct ltt_session
*session
)
427 const char *dir_name
;
428 struct consumer_output
*consumer
;
430 LTTNG_ASSERT(session
);
431 LTTNG_ASSERT(session
->consumer
);
434 case LTTNG_DOMAIN_KERNEL
:
435 DBG3("Copying tracing session consumer output in kernel session");
437 * XXX: We should audit the session creation and what this function
438 * does "extra" in order to avoid a destroy since this function is used
439 * in the domain session creation (kernel and ust) only. Same for UST
442 if (session
->kernel_session
->consumer
) {
443 consumer_output_put(session
->kernel_session
->consumer
);
445 session
->kernel_session
->consumer
= consumer_copy_output(session
->consumer
);
446 /* Ease our life a bit for the next part */
447 consumer
= session
->kernel_session
->consumer
;
448 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
450 case LTTNG_DOMAIN_JUL
:
451 case LTTNG_DOMAIN_LOG4J
:
452 case LTTNG_DOMAIN_PYTHON
:
453 case LTTNG_DOMAIN_UST
:
454 DBG3("Copying tracing session consumer output in UST session");
455 if (session
->ust_session
->consumer
) {
456 consumer_output_put(session
->ust_session
->consumer
);
458 session
->ust_session
->consumer
= consumer_copy_output(session
->consumer
);
459 /* Ease our life a bit for the next part */
460 consumer
= session
->ust_session
->consumer
;
461 dir_name
= DEFAULT_UST_TRACE_DIR
;
464 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
468 /* Append correct directory to subdir */
469 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
, sizeof(consumer
->domain_subdir
));
474 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
482 * Create an UST session and add it to the session ust list.
484 static int create_ust_session(struct ltt_session
*session
, const struct lttng_domain
*domain
)
487 struct ltt_ust_session
*lus
= nullptr;
489 LTTNG_ASSERT(session
);
490 LTTNG_ASSERT(domain
);
491 LTTNG_ASSERT(session
->consumer
);
493 switch (domain
->type
) {
494 case LTTNG_DOMAIN_JUL
:
495 case LTTNG_DOMAIN_LOG4J
:
496 case LTTNG_DOMAIN_PYTHON
:
497 case LTTNG_DOMAIN_UST
:
500 ERR("Unknown UST domain on create session %d", domain
->type
);
501 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
505 DBG("Creating UST session");
507 lus
= trace_ust_create_session(session
->id
);
508 if (lus
== nullptr) {
509 ret
= LTTNG_ERR_UST_SESS_FAIL
;
513 lus
->uid
= session
->uid
;
514 lus
->gid
= session
->gid
;
515 lus
->output_traces
= session
->output_traces
;
516 lus
->snapshot_mode
= session
->snapshot_mode
;
517 lus
->live_timer_interval
= session
->live_timer
;
518 session
->ust_session
= lus
;
519 if (session
->shm_path
[0]) {
520 strncpy(lus
->root_shm_path
, session
->shm_path
, sizeof(lus
->root_shm_path
));
521 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
522 strncpy(lus
->shm_path
, session
->shm_path
, sizeof(lus
->shm_path
));
523 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
524 strncat(lus
->shm_path
, "/ust", sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
526 /* Copy session output to the newly created UST session */
527 ret
= copy_session_consumer(domain
->type
, session
);
528 if (ret
!= LTTNG_OK
) {
536 session
->ust_session
= nullptr;
541 * Create a kernel tracer session then create the default channel.
543 static int create_kernel_session(struct ltt_session
*session
)
547 DBG("Creating kernel session");
549 ret
= kernel_create_session(session
);
551 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
555 /* Code flow safety */
556 LTTNG_ASSERT(session
->kernel_session
);
558 /* Copy session output to the newly created Kernel session */
559 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
560 if (ret
!= LTTNG_OK
) {
564 session
->kernel_session
->uid
= session
->uid
;
565 session
->kernel_session
->gid
= session
->gid
;
566 session
->kernel_session
->output_traces
= session
->output_traces
;
567 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
568 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
573 trace_kernel_destroy_session(session
->kernel_session
);
574 session
->kernel_session
= nullptr;
580 * Count number of session permitted by uid/gid.
582 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
__attribute__((unused
)))
585 struct ltt_session
*session
;
586 const struct ltt_session_list
*session_list
= session_get_list();
588 DBG("Counting number of available session for UID %d", uid
);
589 cds_list_for_each_entry (session
, &session_list
->head
, list
) {
590 if (!session_get(session
)) {
593 session_lock(session
);
594 /* Only count the sessions the user can control. */
595 if (session_access_ok(session
, uid
) && !session
->destroyed
) {
598 session_unlock(session
);
599 session_put(session
);
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 null for commands that don't
1115 * have a target session.
1117 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();
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
.get());
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
.get(), &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
.get(),
1395 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1396 target_session
->destroyed
) {
1397 ret
= LTTNG_ERR_EPERM
;
1403 * Send relayd information to consumer as soon as we have a domain and a
1406 if (target_session
&& need_domain
) {
1408 * Setup relayd if not done yet. If the relayd information was already
1409 * sent to the consumer, this call will gracefully return.
1411 ret
= cmd_setup_relayd(target_session
.get());
1412 if (ret
!= LTTNG_OK
) {
1417 /* Process by command type */
1418 switch (cmd_ctx
->lsm
.cmd_type
) {
1419 case LTTCOMM_SESSIOND_COMMAND_ADD_CONTEXT
:
1421 struct lttng_event_context
*event_context
= nullptr;
1422 const enum lttng_error_code ret_code
=
1423 receive_lttng_event_context(cmd_ctx
, *sock
, sock_error
, &event_context
);
1425 if (ret_code
!= LTTNG_OK
) {
1426 ret
= (int) ret_code
;
1430 ret
= cmd_add_context(
1431 cmd_ctx
, target_session
, event_context
, the_kernel_poll_pipe
[1]);
1432 lttng_event_context_destroy(event_context
);
1435 case LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL
:
1437 ret
= cmd_disable_channel(target_session
.get(),
1438 cmd_ctx
->lsm
.domain
.type
,
1439 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1442 case LTTCOMM_SESSIOND_COMMAND_ENABLE_CHANNEL
:
1444 ret
= cmd_enable_channel(cmd_ctx
, target_session
, *sock
, the_kernel_poll_pipe
[1]);
1447 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1448 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1450 struct lttng_dynamic_buffer payload
;
1451 struct lttng_buffer_view payload_view
;
1452 const bool add_value
= cmd_ctx
->lsm
.cmd_type
==
1453 LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1454 const size_t name_len
=
1455 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
.name_len
;
1456 const enum lttng_domain_type domain_type
=
1457 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1458 const enum lttng_process_attr process_attr
=
1459 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1460 .process_attr_tracker_add_remove_include_value
.process_attr
;
1461 const enum lttng_process_attr_value_type value_type
=
1462 (enum lttng_process_attr_value_type
) cmd_ctx
->lsm
.u
1463 .process_attr_tracker_add_remove_include_value
.value_type
;
1464 struct process_attr_value
*value
;
1465 enum lttng_error_code ret_code
;
1466 long login_name_max
;
1468 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1469 if (login_name_max
< 0) {
1470 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1471 ret
= LTTNG_ERR_INVALID
;
1475 /* Receive remaining variable length payload if applicable. */
1476 if (name_len
> login_name_max
) {
1478 * POSIX mandates user and group names that are at least
1479 * 8 characters long. Note that although shadow-utils
1480 * (useradd, groupaadd, etc.) use 32 chars as their
1481 * limit (from bits/utmp.h, UT_NAMESIZE),
1482 * LOGIN_NAME_MAX is defined to 256.
1484 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1485 add_value
? "addition" : "removal",
1488 ret
= LTTNG_ERR_INVALID
;
1492 lttng_dynamic_buffer_init(&payload
);
1493 if (name_len
!= 0) {
1495 * Receive variable payload for user/group name
1498 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1500 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1501 add_value
? "add" : "remove");
1502 ret
= LTTNG_ERR_NOMEM
;
1503 goto error_add_remove_tracker_value
;
1506 ret
= lttcomm_recv_unix_sock(*sock
, payload
.data
, name_len
);
1508 ERR("Failed to receive payload of %s process attribute tracker value argument",
1509 add_value
? "add" : "remove");
1511 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1512 goto error_add_remove_tracker_value
;
1516 payload_view
= lttng_buffer_view_from_dynamic_buffer(&payload
, 0, name_len
);
1517 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1518 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1519 goto error_add_remove_tracker_value
;
1523 * Validate the value type and domains are legal for the process
1524 * attribute tracker that is specified and convert the value to
1525 * add/remove to the internal sessiond representation.
1527 ret_code
= process_attr_value_from_comm(
1531 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
.integral_value
,
1534 if (ret_code
!= LTTNG_OK
) {
1536 goto error_add_remove_tracker_value
;
1540 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1541 target_session
.get(), domain_type
, process_attr
, value
);
1543 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1544 target_session
.get(), domain_type
, process_attr
, value
);
1546 process_attr_value_destroy(value
);
1547 error_add_remove_tracker_value
:
1548 lttng_dynamic_buffer_reset(&payload
);
1551 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_GET_POLICY
:
1553 enum lttng_tracking_policy tracking_policy
;
1554 const enum lttng_domain_type domain_type
=
1555 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1556 const enum lttng_process_attr process_attr
=
1557 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1558 .process_attr_tracker_get_tracking_policy
.process_attr
;
1560 ret
= cmd_process_attr_tracker_get_tracking_policy(
1561 target_session
.get(), domain_type
, process_attr
, &tracking_policy
);
1562 if (ret
!= LTTNG_OK
) {
1566 uint32_t tracking_policy_u32
= tracking_policy
;
1567 setup_lttng_msg_no_cmd_header(cmd_ctx
, &tracking_policy_u32
, sizeof(uint32_t));
1572 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_SET_POLICY
:
1574 const enum lttng_tracking_policy tracking_policy
=
1575 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1576 .process_attr_tracker_set_tracking_policy
.tracking_policy
;
1577 const enum lttng_domain_type domain_type
=
1578 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1579 const enum lttng_process_attr process_attr
=
1580 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1581 .process_attr_tracker_set_tracking_policy
.process_attr
;
1583 ret
= cmd_process_attr_tracker_set_tracking_policy(
1584 target_session
.get(), domain_type
, process_attr
, tracking_policy
);
1585 if (ret
!= LTTNG_OK
) {
1590 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1592 struct lttng_process_attr_values
*values
;
1593 struct lttng_dynamic_buffer reply
;
1594 const enum lttng_domain_type domain_type
=
1595 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1596 const enum lttng_process_attr process_attr
=
1597 (enum lttng_process_attr
)
1598 cmd_ctx
->lsm
.u
.process_attr_tracker_get_inclusion_set
.process_attr
;
1600 ret
= cmd_process_attr_tracker_get_inclusion_set(
1601 target_session
.get(), domain_type
, process_attr
, &values
);
1602 if (ret
!= LTTNG_OK
) {
1606 lttng_dynamic_buffer_init(&reply
);
1607 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1609 goto error_tracker_get_inclusion_set
;
1612 setup_lttng_msg_no_cmd_header(cmd_ctx
, reply
.data
, reply
.size
);
1615 error_tracker_get_inclusion_set
:
1616 lttng_process_attr_values_destroy(values
);
1617 lttng_dynamic_buffer_reset(&reply
);
1620 case LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
:
1621 case LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
:
1623 struct lttng_event
*event
;
1624 char *filter_expression
;
1625 struct lttng_event_exclusion
*exclusions
;
1626 struct lttng_bytecode
*bytecode
;
1627 const enum lttng_error_code ret_code
= receive_lttng_event(cmd_ctx
,
1635 if (ret_code
!= LTTNG_OK
) {
1636 ret
= (int) ret_code
;
1641 * Ownership of filter_expression, exclusions, and bytecode is
1642 * always transferred.
1644 ret
= cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
?
1645 cmd_enable_event(cmd_ctx
,
1651 the_kernel_poll_pipe
[1]) :
1652 cmd_disable_event(cmd_ctx
,
1658 lttng_event_destroy(event
);
1661 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
:
1663 enum lttng_error_code ret_code
;
1664 size_t original_payload_size
;
1665 size_t payload_size
;
1666 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1668 setup_empty_lttng_msg(cmd_ctx
);
1670 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1672 list_lock
= lttng::sessiond::lock_session_list();
1673 ret_code
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &cmd_ctx
->reply_payload
);
1674 if (ret_code
!= LTTNG_OK
) {
1675 ret
= (int) ret_code
;
1679 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1680 original_payload_size
;
1681 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1686 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
:
1688 enum lttng_error_code ret_code
;
1689 size_t original_payload_size
;
1690 size_t payload_size
;
1691 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1693 setup_empty_lttng_msg(cmd_ctx
);
1695 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1697 list_lock
= lttng::sessiond::lock_session_list();
1698 ret_code
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1699 &cmd_ctx
->reply_payload
);
1701 if (ret_code
!= LTTNG_OK
) {
1702 ret
= (int) ret_code
;
1706 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1707 original_payload_size
;
1708 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1713 case LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS
:
1715 enum lttng_error_code ret_code
;
1716 size_t original_payload_size
;
1717 size_t payload_size
;
1718 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1720 setup_empty_lttng_msg(cmd_ctx
);
1722 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1724 ret_code
= cmd_list_syscalls(&cmd_ctx
->reply_payload
);
1725 if (ret_code
!= LTTNG_OK
) {
1726 ret
= (int) ret_code
;
1730 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1731 original_payload_size
;
1732 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1737 case LTTCOMM_SESSIOND_COMMAND_SET_CONSUMER_URI
:
1740 struct lttng_uri
*uris
;
1742 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1743 len
= nb_uri
* sizeof(struct lttng_uri
);
1746 ret
= LTTNG_ERR_INVALID
;
1750 uris
= calloc
<lttng_uri
>(nb_uri
);
1751 if (uris
== nullptr) {
1752 ret
= LTTNG_ERR_FATAL
;
1756 /* Receive variable len data */
1757 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1758 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1760 DBG("No URIs received from client... continuing");
1762 ret
= LTTNG_ERR_SESSION_FAIL
;
1767 ret
= cmd_set_consumer_uri(target_session
.get(), nb_uri
, uris
);
1769 if (ret
!= LTTNG_OK
) {
1775 case LTTCOMM_SESSIOND_COMMAND_START_TRACE
:
1778 * On the first start, if we have a kernel session and we have
1779 * enabled time or size-based rotations, we have to make sure
1780 * the kernel tracer supports it.
1782 if (!target_session
->has_been_started
&& target_session
->kernel_session
&&
1783 (target_session
->rotate_timer_period
|| target_session
->rotate_size
) &&
1784 !check_rotate_compatible()) {
1785 DBG("Kernel tracer version is not compatible with the rotation feature");
1786 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1789 ret
= cmd_start_trace(target_session
.get());
1792 case LTTCOMM_SESSIOND_COMMAND_STOP_TRACE
:
1794 ret
= cmd_stop_trace(target_session
.get());
1797 case LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
:
1799 ret
= cmd_destroy_session(target_session
.get(), sock
);
1802 case LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
:
1805 struct lttng_domain
*domains
= nullptr;
1807 nb_dom
= cmd_list_domains(target_session
.get(), &domains
);
1809 /* Return value is a negative lttng_error_code. */
1814 setup_lttng_msg_no_cmd_header(
1815 cmd_ctx
, domains
, nb_dom
* sizeof(struct lttng_domain
));
1821 case LTTCOMM_SESSIOND_COMMAND_LIST_CHANNELS
:
1823 enum lttng_error_code ret_code
;
1824 size_t original_payload_size
;
1825 size_t payload_size
;
1826 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1828 setup_empty_lttng_msg(cmd_ctx
);
1830 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1832 ret_code
= cmd_list_channels(
1833 cmd_ctx
->lsm
.domain
.type
, target_session
.get(), &cmd_ctx
->reply_payload
);
1834 if (ret_code
!= LTTNG_OK
) {
1835 ret
= (int) ret_code
;
1839 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1840 original_payload_size
;
1841 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1846 case LTTCOMM_SESSIOND_COMMAND_LIST_EVENTS
:
1848 enum lttng_error_code ret_code
;
1849 size_t original_payload_size
;
1850 size_t payload_size
;
1851 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1853 setup_empty_lttng_msg(cmd_ctx
);
1855 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1857 ret_code
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1858 target_session
.get(),
1859 cmd_ctx
->lsm
.u
.list
.channel_name
,
1860 &cmd_ctx
->reply_payload
);
1861 if (ret_code
!= LTTNG_OK
) {
1862 ret
= (int) ret_code
;
1866 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1867 original_payload_size
;
1868 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1873 case LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
:
1875 unsigned int nr_sessions
;
1876 lttng_session
*sessions_payload
= nullptr;
1877 size_t payload_len
= 0;
1879 list_lock
= lttng::sessiond::lock_session_list();
1880 nr_sessions
= lttng_sessions_count(LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1881 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1883 if (nr_sessions
> 0) {
1884 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1885 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1886 sessions_payload
= zmalloc
<lttng_session
>(payload_len
);
1887 if (!sessions_payload
) {
1888 LTTNG_THROW_ALLOCATION_FAILURE_ERROR(
1889 "Failed to allocate session list reply payload",
1893 cmd_list_lttng_sessions(sessions_payload
,
1895 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1896 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1899 setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
, payload_len
);
1900 free(sessions_payload
);
1905 case LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
:
1907 struct consumer_data
*cdata
;
1909 switch (cmd_ctx
->lsm
.domain
.type
) {
1910 case LTTNG_DOMAIN_KERNEL
:
1911 cdata
= &the_kconsumer_data
;
1914 ret
= LTTNG_ERR_UND
;
1918 ret
= cmd_register_consumer(target_session
.get(),
1919 cmd_ctx
->lsm
.domain
.type
,
1920 cmd_ctx
->lsm
.u
.reg
.path
,
1924 case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
:
1927 enum lttng_kernel_tracer_status status
;
1929 ret
= cmd_kernel_tracer_status(&status
);
1930 if (ret
!= LTTNG_OK
) {
1934 u_status
= (uint32_t) status
;
1935 setup_lttng_msg_no_cmd_header(cmd_ctx
, &u_status
, 4);
1940 case LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
:
1943 uint8_t pending_ret_byte
;
1945 pending_ret
= cmd_data_pending(target_session
.get());
1950 * This function may returns 0 or 1 to indicate whether or not
1951 * there is data pending. In case of error, it should return an
1952 * LTTNG_ERR code. However, some code paths may still return
1953 * a nondescript error code, which we handle by returning an
1956 if (pending_ret
== 0 || pending_ret
== 1) {
1958 * ret will be set to LTTNG_OK at the end of
1961 } else if (pending_ret
<= LTTNG_OK
|| pending_ret
>= LTTNG_ERR_NR
) {
1962 ret
= LTTNG_ERR_UNK
;
1969 pending_ret_byte
= (uint8_t) pending_ret
;
1971 /* 1 byte to return whether or not data is pending */
1972 setup_lttng_msg_no_cmd_header(cmd_ctx
, &pending_ret_byte
, 1);
1977 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_ADD_OUTPUT
:
1979 uint32_t snapshot_id
;
1980 struct lttcomm_lttng_output_id reply
;
1981 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
1983 ret
= cmd_snapshot_add_output(target_session
.get(), &output
, &snapshot_id
);
1984 if (ret
!= LTTNG_OK
) {
1987 reply
.id
= snapshot_id
;
1989 setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
, sizeof(reply
));
1991 /* Copy output list into message payload */
1995 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_DEL_OUTPUT
:
1997 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
1998 ret
= cmd_snapshot_del_output(target_session
.get(), &output
);
2001 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_LIST_OUTPUT
:
2004 struct lttng_snapshot_output
*outputs
= nullptr;
2006 nb_output
= cmd_snapshot_list_outputs(target_session
.get(), &outputs
);
2007 if (nb_output
< 0) {
2012 LTTNG_ASSERT((nb_output
> 0 && outputs
) || nb_output
== 0);
2013 setup_lttng_msg_no_cmd_header(
2014 cmd_ctx
, outputs
, nb_output
* sizeof(struct lttng_snapshot_output
));
2020 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_RECORD
:
2022 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_record
.output
;
2023 ret
= cmd_snapshot_record(target_session
.get(), &output
, 0); // RFC: set to zero
2024 // since it's ignored
2026 // cmd_snapshot_record
2029 case LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
:
2031 struct lttng_dynamic_buffer payload
;
2033 lttng_dynamic_buffer_init(&payload
);
2035 lttng::ctl::session_descriptor reply_session_descriptor
= [cmd_ctx
, sock
]() {
2036 lttng_session_descriptor
*raw_descriptor
;
2037 const auto create_ret
= cmd_create_session(cmd_ctx
, *sock
, &raw_descriptor
);
2038 if (create_ret
!= LTTNG_OK
) {
2039 LTTNG_THROW_CTL("Failed to create session", create_ret
);
2042 return lttng::ctl::session_descriptor(raw_descriptor
);
2045 ret
= lttng_session_descriptor_serialize(reply_session_descriptor
.get(), &payload
);
2048 "Failed to serialize session descriptor in reply to \"create session\" command",
2052 setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
, payload
.size
);
2054 lttng_dynamic_buffer_reset(&payload
);
2058 case LTTCOMM_SESSIOND_COMMAND_SAVE_SESSION
:
2060 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
, &cmd_ctx
->creds
);
2063 case LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH
:
2065 ret
= cmd_set_session_shm_path(target_session
.get(),
2066 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2069 case LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA
:
2071 ret
= cmd_regenerate_metadata(target_session
.get());
2074 case LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP
:
2076 ret
= cmd_regenerate_statedump(target_session
.get());
2079 case LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
:
2081 size_t original_reply_payload_size
;
2082 size_t reply_payload_size
;
2083 const struct lttng_credentials cmd_creds
= {
2084 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2085 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2088 setup_empty_lttng_msg(cmd_ctx
);
2090 auto payload_trigger
= receive_lttng_trigger(cmd_ctx
, *sock
, sock_error
);
2091 if (ret
!= LTTNG_OK
) {
2095 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2097 auto return_trigger
=
2098 cmd_register_trigger(&cmd_creds
,
2099 payload_trigger
.get(),
2100 cmd_ctx
->lsm
.u
.trigger
.is_trigger_anonymous
,
2101 the_notification_thread_handle
);
2103 ret
= lttng_trigger_serialize(return_trigger
.get(), &cmd_ctx
->reply_payload
);
2106 "Failed to serialize trigger in reply to \"register trigger\" command",
2110 reply_payload_size
=
2111 cmd_ctx
->reply_payload
.buffer
.size
- original_reply_payload_size
;
2113 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2118 case LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
:
2120 const struct lttng_credentials cmd_creds
= {
2121 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2122 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2125 auto payload_trigger
= receive_lttng_trigger(cmd_ctx
, *sock
, sock_error
);
2127 ret
= cmd_unregister_trigger(
2128 &cmd_creds
, payload_trigger
.get(), the_notification_thread_handle
);
2131 case LTTCOMM_SESSIOND_COMMAND_ROTATE_SESSION
:
2133 struct lttng_rotate_session_return rotate_return
;
2135 DBG("Client rotate session \"%s\"", target_session
->name
);
2137 memset(&rotate_return
, 0, sizeof(rotate_return
));
2138 if (target_session
->kernel_session
&& !check_rotate_compatible()) {
2139 DBG("Kernel tracer version is not compatible with the rotation feature");
2140 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2144 ret
= cmd_rotate_session(target_session
.get(),
2147 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2153 setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
, sizeof(rotate_return
));
2158 case LTTCOMM_SESSIOND_COMMAND_ROTATION_GET_INFO
:
2160 struct lttng_rotation_get_info_return get_info_return
;
2162 memset(&get_info_return
, 0, sizeof(get_info_return
));
2163 ret
= cmd_rotate_get_info(target_session
.get(),
2165 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2171 setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
, sizeof(get_info_return
));
2176 case LTTCOMM_SESSIOND_COMMAND_ROTATION_SET_SCHEDULE
:
2179 enum lttng_rotation_schedule_type schedule_type
;
2182 if (target_session
->kernel_session
&& !check_rotate_compatible()) {
2183 DBG("Kernel tracer version does not support session rotations");
2184 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2188 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2189 schedule_type
= (enum lttng_rotation_schedule_type
)
2190 cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2191 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2193 ret
= cmd_rotation_set_schedule(
2194 target_session
.get(), set_schedule
, schedule_type
, value
);
2195 if (ret
!= LTTNG_OK
) {
2201 case LTTCOMM_SESSIOND_COMMAND_SESSION_LIST_ROTATION_SCHEDULES
:
2203 lttng_session_list_schedules_return schedules
;
2205 schedules
.periodic
.set
= !!target_session
->rotate_timer_period
;
2206 schedules
.periodic
.value
= target_session
->rotate_timer_period
;
2207 schedules
.size
.set
= !!target_session
->rotate_size
;
2208 schedules
.size
.value
= target_session
->rotate_size
;
2210 setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
, sizeof(schedules
));
2215 case LTTCOMM_SESSIOND_COMMAND_CLEAR_SESSION
:
2217 ret
= cmd_clear_session(target_session
.get(), sock
);
2220 case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
:
2222 struct lttng_triggers
*return_triggers
= nullptr;
2223 size_t original_payload_size
;
2224 size_t payload_size
;
2226 setup_empty_lttng_msg(cmd_ctx
);
2228 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2230 ret
= cmd_list_triggers(cmd_ctx
, the_notification_thread_handle
, &return_triggers
);
2231 if (ret
!= LTTNG_OK
) {
2235 LTTNG_ASSERT(return_triggers
);
2236 ret
= lttng_triggers_serialize(return_triggers
, &cmd_ctx
->reply_payload
);
2237 lttng_triggers_destroy(return_triggers
);
2239 ERR("Failed to serialize triggers in reply to `list triggers` command");
2240 ret
= LTTNG_ERR_NOMEM
;
2244 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- original_payload_size
;
2246 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2251 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
2253 struct lttng_error_query
*query
;
2254 const struct lttng_credentials cmd_creds
= {
2255 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2256 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2258 struct lttng_error_query_results
*results
= nullptr;
2259 size_t original_payload_size
;
2260 size_t payload_size
;
2262 setup_empty_lttng_msg(cmd_ctx
);
2264 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2266 ret
= receive_lttng_error_query(cmd_ctx
, *sock
, sock_error
, &query
);
2267 if (ret
!= LTTNG_OK
) {
2271 ret
= cmd_execute_error_query(
2272 &cmd_creds
, query
, &results
, the_notification_thread_handle
);
2273 lttng_error_query_destroy(query
);
2274 if (ret
!= LTTNG_OK
) {
2278 LTTNG_ASSERT(results
);
2279 ret
= lttng_error_query_results_serialize(results
, &cmd_ctx
->reply_payload
);
2280 lttng_error_query_results_destroy(results
);
2282 ERR("Failed to serialize error query result set in reply to `execute error query` command");
2283 ret
= LTTNG_ERR_NOMEM
;
2287 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- original_payload_size
;
2289 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2296 ret
= LTTNG_ERR_UND
;
2301 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2302 DBG("Missing llm header, creating one.");
2303 setup_lttng_msg_no_cmd_header(cmd_ctx
, nullptr, 0);
2306 command_ctx_set_status_code(*cmd_ctx
, static_cast<lttng_error_code
>(ret
));
2307 LTTNG_ASSERT(!rcu_read_ongoing());
2311 static int create_client_sock()
2313 int ret
, client_sock
;
2315 /* Create client tool unix socket */
2316 client_sock
= lttcomm_create_unix_sock(the_config
.client_unix_sock_path
.value
);
2317 if (client_sock
< 0) {
2318 ERR("Create unix sock failed: %s", the_config
.client_unix_sock_path
.value
);
2323 /* Set the cloexec flag */
2324 ret
= utils_set_fd_cloexec(client_sock
);
2326 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2327 "Continuing but note that the consumer daemon will have a "
2328 "reference to this socket on exec()",
2332 /* File permission MUST be 660 */
2333 ret
= chmod(the_config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2335 ERR("Set file permissions failed: %s", the_config
.client_unix_sock_path
.value
);
2337 (void) lttcomm_close_unix_sock(client_sock
);
2341 DBG("Created client socket (fd = %i)", client_sock
);
2347 static void cleanup_client_thread(void *data
)
2349 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2351 lttng_pipe_destroy(quit_pipe
);
2354 static void thread_init_cleanup(void *data
__attribute__((unused
)))
2356 set_thread_status(false);
2359 // Helper function to log the source_location if the exception is derived from lttng::runtime_error
2360 template <typename ExceptionType
>
2361 typename
std::enable_if
<std::is_base_of
<lttng::runtime_error
, ExceptionType
>::value
,
2363 formatted_source_location(const ExceptionType
& ex
)
2365 return fmt::format("{}", ex
.source_location
);
2368 template <typename ExceptionType
>
2369 typename
std::enable_if
<!std::is_base_of
<lttng::runtime_error
, ExceptionType
>::value
,
2371 formatted_source_location(const ExceptionType
&)
2376 template <class ExceptionType
>
2377 static void log_nested_exceptions(const ExceptionType
& ex
, unsigned int level
= 0)
2379 const auto location
= formatted_source_location(ex
);
2382 if (location
.size()) {
2383 WARN_FMT("Client request failed: {}, location='{}'", ex
.what(), location
);
2385 WARN_FMT("Client request failed: {}", ex
.what());
2388 if (location
.size()) {
2389 WARN_FMT("\t{}, location='{}'", ex
.what(), location
);
2391 WARN_FMT("\t{}", ex
.what());
2396 std::rethrow_if_nested(ex
);
2397 } catch (const lttng::runtime_error
& nested_ex
) {
2398 log_nested_exceptions(nested_ex
, level
+ 1);
2399 } catch (const std::exception
& nested_ex
) {
2400 log_nested_exceptions(nested_ex
, level
+ 1);
2405 * This thread manage all clients request using the unix client socket for
2408 static void *thread_manage_clients(void *data
)
2410 int sock
= -1, ret
, i
, err
= -1;
2413 struct lttng_poll_event events
;
2414 const int client_sock
= thread_state
.client_sock
;
2415 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2416 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2417 struct command_ctx cmd_ctx
= {};
2419 DBG("[thread] Manage client started");
2421 lttng_payload_init(&cmd_ctx
.reply_payload
);
2423 is_root
= (getuid() == 0);
2425 pthread_cleanup_push(thread_init_cleanup
, nullptr);
2427 rcu_register_thread();
2429 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2431 health_code_update();
2433 ret
= lttcomm_listen_unix_sock(client_sock
);
2439 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2440 * more will be added to this poll set.
2442 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2444 goto error_create_poll
;
2447 /* Add the application registration socket */
2448 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2453 /* Add thread quit pipe */
2454 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
);
2459 /* Set state as running. */
2460 set_thread_status(true);
2461 pthread_cleanup_pop(0);
2463 /* This testpoint is after we signal readiness to the parent. */
2464 if (testpoint(sessiond_thread_manage_clients
)) {
2468 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2472 health_code_update();
2475 const struct cmd_completion_handler
*cmd_completion_handler
;
2477 cmd_ctx
.creds
.uid
= UINT32_MAX
;
2478 cmd_ctx
.creds
.gid
= UINT32_MAX
;
2479 cmd_ctx
.creds
.pid
= 0;
2480 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2481 cmd_ctx
.lttng_msg_size
= 0;
2483 DBG("Accepting client command ...");
2485 /* Inifinite blocking call, waiting for transmission */
2487 health_poll_entry();
2488 ret
= lttng_poll_wait(&events
, -1);
2492 * Restart interrupted system call.
2494 if (errno
== EINTR
) {
2502 for (i
= 0; i
< nb_fd
; i
++) {
2503 /* Fetch once the poll data. */
2504 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
2505 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2507 health_code_update();
2509 /* Activity on thread quit pipe, exiting. */
2510 if (pollfd
== thread_quit_pipe_fd
) {
2511 DBG("Activity on thread quit pipe");
2516 /* Event on the registration socket */
2517 if (revents
& LPOLLIN
) {
2519 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2520 ERR("Client socket poll error");
2523 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2528 DBG("Wait for client response");
2530 health_code_update();
2532 sock
= lttcomm_accept_unix_sock(client_sock
);
2538 * Set the CLOEXEC flag. Return code is useless because either way, the
2541 (void) utils_set_fd_cloexec(sock
);
2543 /* Set socket option for credentials retrieval */
2544 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2549 health_code_update();
2552 * Data is received from the lttng client. The struct
2553 * lttcomm_session_msg (lsm) contains the command and data request of
2556 DBG("Receiving data from client ...");
2557 ret
= lttcomm_recv_creds_unix_sock(
2558 sock
, &cmd_ctx
.lsm
, sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2559 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2560 DBG("Incomplete recv() from client... continuing");
2569 health_code_update();
2571 // TODO: Validate cmd_ctx including sanity check for
2572 // security purpose.
2574 rcu_thread_online();
2576 * This function dispatch the work to the kernel or userspace tracer
2577 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2578 * informations for the client. The command context struct contains
2579 * everything this function may needs.
2582 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2583 rcu_thread_offline();
2584 } catch (const std::bad_alloc
& ex
) {
2585 log_nested_exceptions(ex
);
2586 ret
= LTTNG_ERR_NOMEM
;
2587 } catch (const lttng::ctl::error
& ex
) {
2588 log_nested_exceptions(ex
);
2590 } catch (const lttng::invalid_argument_error
& ex
) {
2591 log_nested_exceptions(ex
);
2592 ret
= LTTNG_ERR_INVALID
;
2593 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
2594 log_nested_exceptions(ex
);
2595 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2596 } catch (const lttng::runtime_error
& ex
) {
2597 log_nested_exceptions(ex
);
2598 ret
= LTTNG_ERR_UNK
;
2599 } catch (const std::exception
& ex
) {
2600 log_nested_exceptions(ex
);
2601 ret
= LTTNG_ERR_UNK
;
2604 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2605 WARN("Command returned an invalid status code, returning unknown error: "
2606 "command type = %s (%d), ret = %d",
2607 lttcomm_sessiond_command_str(
2608 (lttcomm_sessiond_command
) cmd_ctx
.lsm
.cmd_type
),
2609 cmd_ctx
.lsm
.cmd_type
,
2611 ret
= LTTNG_ERR_UNK
;
2614 if (ret
!= LTTNG_OK
) {
2616 * Reset the payload contents as the command may have left them in an
2617 * inconsistent state.
2619 setup_empty_lttng_msg(&cmd_ctx
);
2622 command_ctx_set_status_code(cmd_ctx
, static_cast<lttng_error_code
>(ret
));
2624 cmd_completion_handler
= cmd_pop_completion_handler();
2625 if (cmd_completion_handler
) {
2626 enum lttng_error_code completion_code
;
2628 completion_code
= cmd_completion_handler
->run(cmd_completion_handler
->data
);
2629 if (completion_code
!= LTTNG_OK
) {
2634 health_code_update();
2637 struct lttng_payload_view view
=
2638 lttng_payload_view_from_payload(&cmd_ctx
.reply_payload
, 0, -1);
2639 struct lttcomm_lttng_msg
*llm
=
2640 (typeof(llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2642 LTTNG_ASSERT(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2643 LTTNG_ASSERT(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2645 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2647 DBG("Sending response (size: %d, retcode: %s (%d))",
2648 cmd_ctx
.lttng_msg_size
,
2649 lttng_strerror(-llm
->ret_code
),
2651 ret
= send_unix_sock(sock
, &view
);
2653 ERR("Failed to send data back to client");
2656 /* End of transmission */
2664 health_code_update();
2676 lttng_poll_clean(&events
);
2680 unlink(the_config
.client_unix_sock_path
.value
);
2681 ret
= close(client_sock
);
2688 ERR("Health error occurred in %s", __func__
);
2691 health_unregister(the_health_sessiond
);
2693 DBG("Client thread dying");
2694 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2695 rcu_unregister_thread();
2699 static bool shutdown_client_thread(void *thread_data
)
2701 struct lttng_pipe
*client_quit_pipe
= (lttng_pipe
*) thread_data
;
2702 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2704 return notify_thread_pipe(write_fd
) == 1;
2707 struct lttng_thread
*launch_client_thread()
2709 bool thread_running
;
2710 struct lttng_pipe
*client_quit_pipe
;
2711 struct lttng_thread
*thread
= nullptr;
2712 int client_sock_fd
= -1;
2714 sem_init(&thread_state
.ready
, 0, 0);
2715 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2716 if (!client_quit_pipe
) {
2720 client_sock_fd
= create_client_sock();
2721 if (client_sock_fd
< 0) {
2725 thread_state
.client_sock
= client_sock_fd
;
2726 thread
= lttng_thread_create("Client management",
2727 thread_manage_clients
,
2728 shutdown_client_thread
,
2729 cleanup_client_thread
,
2734 /* The client thread now owns the client sock fd and the quit pipe. */
2735 client_sock_fd
= -1;
2736 client_quit_pipe
= nullptr;
2739 * This thread is part of the threads that need to be fully
2740 * initialized before the session daemon is marked as "ready".
2742 thread_running
= wait_thread_status();
2743 if (!thread_running
) {
2748 if (client_sock_fd
>= 0) {
2749 if (close(client_sock_fd
)) {
2750 PERROR("Failed to close client socket");
2753 lttng_thread_put(thread
);
2754 cleanup_client_thread(client_quit_pipe
);