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/sessiond-comm/sessiond-comm.hpp>
32 #include <common/tracker.hpp>
33 #include <common/unix.hpp>
34 #include <common/utils.hpp>
36 #include <lttng/error-query-internal.hpp>
37 #include <lttng/event-internal.hpp>
38 #include <lttng/lttng.h>
39 #include <lttng/session-descriptor-internal.hpp>
40 #include <lttng/session-internal.hpp>
41 #include <lttng/userspace-probe-internal.hpp>
61 static void set_thread_status(bool running
)
63 DBG("Marking client thread's state as %s", running
? "running" : "error");
64 thread_state
.running
= running
;
65 sem_post(&thread_state
.ready
);
68 static bool wait_thread_status()
70 DBG("Waiting for client thread to be ready");
71 sem_wait(&thread_state
.ready
);
72 if (thread_state
.running
) {
73 DBG("Client thread is ready");
75 ERR("Initialization of client thread failed");
78 return thread_state
.running
;
82 * Setup the outgoing data buffer for the response (llm) by allocating the
83 * right amount of memory and copying the original information from the lsm
86 * Return 0 on success, negative value on error.
88 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
89 const void *payload_buf
,
91 const void *cmd_header_buf
,
92 size_t cmd_header_len
)
95 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
96 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
97 lttcomm_lttng_msg llm
{};
99 llm
.cmd_type
= cmd_ctx
->lsm
.cmd_type
;
100 llm
.pid
= (uint32_t) cmd_ctx
->lsm
.domain
.attr
.pid
;
101 llm
.cmd_header_size
= (uint32_t) cmd_header_len
;
102 llm
.data_size
= (uint32_t) payload_len
;
104 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
109 lttng_dynamic_pointer_array_clear(&cmd_ctx
->reply_payload
._fd_handles
);
111 cmd_ctx
->lttng_msg_size
= total_msg_size
;
113 /* Append reply header. */
114 ret
= lttng_dynamic_buffer_append(&cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
119 /* Append command header. */
120 if (cmd_header_len
) {
121 ret
= lttng_dynamic_buffer_append(
122 &cmd_ctx
->reply_payload
.buffer
, cmd_header_buf
, cmd_header_len
);
128 /* Append payload. */
130 ret
= lttng_dynamic_buffer_append(
131 &cmd_ctx
->reply_payload
.buffer
, payload_buf
, payload_len
);
141 static int setup_empty_lttng_msg(struct command_ctx
*cmd_ctx
)
144 const struct lttcomm_lttng_msg llm
= {};
146 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
151 /* Append place-holder reply header. */
152 ret
= lttng_dynamic_buffer_append(&cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
157 cmd_ctx
->lttng_msg_size
= sizeof(llm
);
162 static void update_lttng_msg(struct command_ctx
*cmd_ctx
, size_t cmd_header_len
, size_t payload_len
)
164 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
165 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
166 struct lttcomm_lttng_msg
*p_llm
;
167 lttcomm_lttng_msg llm
{};
169 llm
.cmd_type
= cmd_ctx
->lsm
.cmd_type
;
170 llm
.pid
= (uint32_t) cmd_ctx
->lsm
.domain
.attr
.pid
;
171 llm
.cmd_header_size
= (uint32_t) cmd_header_len
;
172 llm
.data_size
= (uint32_t) payload_len
;
174 LTTNG_ASSERT(cmd_ctx
->reply_payload
.buffer
.size
>= sizeof(llm
));
176 p_llm
= (typeof(p_llm
)) cmd_ctx
->reply_payload
.buffer
.data
;
178 /* Update existing header. */
179 memcpy(p_llm
, &llm
, sizeof(llm
));
181 cmd_ctx
->lttng_msg_size
= total_msg_size
;
185 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
186 * exec or it will fail.
188 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
190 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
194 * Fork and exec a consumer daemon (consumerd).
196 * Return pid if successful else -1.
198 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
202 const char *consumer_to_use
;
203 const char *verbosity
;
206 DBG("Spawning consumerd");
213 if (the_config
.verbose_consumer
) {
214 verbosity
= "--verbose";
215 } else if (lttng_opt_quiet
) {
216 verbosity
= "--quiet";
221 switch (consumer_data
->type
) {
222 case LTTNG_CONSUMER_KERNEL
:
224 * Find out which consumerd to execute. We will first try the
225 * 64-bit path, then the sessiond's installation directory, and
226 * fallback on the 32-bit one,
228 DBG3("Looking for a kernel consumer at these locations:");
229 DBG3(" 1) %s", the_config
.consumerd64_bin_path
.value
?: "NULL");
230 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
231 DBG3(" 3) %s", the_config
.consumerd32_bin_path
.value
?: "NULL");
232 if (stat(the_config
.consumerd64_bin_path
.value
, &st
) == 0) {
233 DBG3("Found location #1");
234 consumer_to_use
= the_config
.consumerd64_bin_path
.value
;
235 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
236 DBG3("Found location #2");
237 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
238 } else if (the_config
.consumerd32_bin_path
.value
&&
239 stat(the_config
.consumerd32_bin_path
.value
, &st
) == 0) {
240 DBG3("Found location #3");
241 consumer_to_use
= the_config
.consumerd32_bin_path
.value
;
243 DBG("Could not find any valid consumerd executable");
247 DBG("Using kernel consumer at: %s", consumer_to_use
);
248 (void) execl(consumer_to_use
,
252 "--consumerd-cmd-sock",
253 consumer_data
->cmd_unix_sock_path
,
254 "--consumerd-err-sock",
255 consumer_data
->err_unix_sock_path
,
257 the_config
.tracing_group_name
.value
,
260 case LTTNG_CONSUMER64_UST
:
262 if (the_config
.consumerd64_lib_dir
.value
) {
267 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
271 tmplen
= strlen(the_config
.consumerd64_lib_dir
.value
) + 1 /* : */ +
273 tmpnew
= zmalloc
<char>(tmplen
+ 1 /* \0 */);
278 strcat(tmpnew
, the_config
.consumerd64_lib_dir
.value
);
279 if (tmp
[0] != '\0') {
283 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
290 DBG("Using 64-bit UST consumer at: %s",
291 the_config
.consumerd64_bin_path
.value
);
292 (void) execl(the_config
.consumerd64_bin_path
.value
,
296 "--consumerd-cmd-sock",
297 consumer_data
->cmd_unix_sock_path
,
298 "--consumerd-err-sock",
299 consumer_data
->err_unix_sock_path
,
301 the_config
.tracing_group_name
.value
,
305 case LTTNG_CONSUMER32_UST
:
307 if (the_config
.consumerd32_lib_dir
.value
) {
312 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
316 tmplen
= strlen(the_config
.consumerd32_lib_dir
.value
) + 1 /* : */ +
318 tmpnew
= zmalloc
<char>(tmplen
+ 1 /* \0 */);
323 strcat(tmpnew
, the_config
.consumerd32_lib_dir
.value
);
324 if (tmp
[0] != '\0') {
328 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
335 DBG("Using 32-bit UST consumer at: %s",
336 the_config
.consumerd32_bin_path
.value
);
337 (void) execl(the_config
.consumerd32_bin_path
.value
,
341 "--consumerd-cmd-sock",
342 consumer_data
->cmd_unix_sock_path
,
343 "--consumerd-err-sock",
344 consumer_data
->err_unix_sock_path
,
346 the_config
.tracing_group_name
.value
,
351 ERR("unknown consumer type");
355 PERROR("Consumer execl()");
357 /* Reaching this point, we got a failure on our execl(). */
359 } else if (pid
> 0) {
362 PERROR("start consumer fork");
370 * Spawn the consumerd daemon and session daemon thread.
372 static int start_consumerd(struct consumer_data
*consumer_data
)
377 * Set the listen() state on the socket since there is a possible race
378 * between the exec() of the consumer daemon and this call if place in the
379 * consumer thread. See bug #366 for more details.
381 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
386 pthread_mutex_lock(&consumer_data
->pid_mutex
);
387 if (consumer_data
->pid
!= 0) {
388 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
392 ret
= spawn_consumerd(consumer_data
);
394 ERR("Spawning consumerd failed");
395 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
399 /* Setting up the consumer_data pid */
400 consumer_data
->pid
= ret
;
401 DBG2("Consumer pid %d", consumer_data
->pid
);
402 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
404 DBG2("Spawning consumer control thread");
405 ret
= spawn_consumer_thread(consumer_data
);
407 ERR("Fatal error spawning consumer control thread");
415 /* Cleanup already created sockets on error. */
416 if (consumer_data
->err_sock
>= 0) {
419 err
= close(consumer_data
->err_sock
);
421 PERROR("close consumer data error socket");
428 * Copy consumer output from the tracing session to the domain session. The
429 * function also applies the right modification on a per domain basis for the
430 * trace files destination directory.
432 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
435 const char *dir_name
;
436 struct consumer_output
*consumer
;
438 LTTNG_ASSERT(session
);
439 LTTNG_ASSERT(session
->consumer
);
442 case LTTNG_DOMAIN_KERNEL
:
443 DBG3("Copying tracing session consumer output in kernel session");
445 * XXX: We should audit the session creation and what this function
446 * does "extra" in order to avoid a destroy since this function is used
447 * in the domain session creation (kernel and ust) only. Same for UST
450 if (session
->kernel_session
->consumer
) {
451 consumer_output_put(session
->kernel_session
->consumer
);
453 session
->kernel_session
->consumer
= consumer_copy_output(session
->consumer
);
454 /* Ease our life a bit for the next part */
455 consumer
= session
->kernel_session
->consumer
;
456 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
458 case LTTNG_DOMAIN_JUL
:
459 case LTTNG_DOMAIN_LOG4J
:
460 case LTTNG_DOMAIN_PYTHON
:
461 case LTTNG_DOMAIN_UST
:
462 DBG3("Copying tracing session consumer output in UST session");
463 if (session
->ust_session
->consumer
) {
464 consumer_output_put(session
->ust_session
->consumer
);
466 session
->ust_session
->consumer
= consumer_copy_output(session
->consumer
);
467 /* Ease our life a bit for the next part */
468 consumer
= session
->ust_session
->consumer
;
469 dir_name
= DEFAULT_UST_TRACE_DIR
;
472 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
476 /* Append correct directory to subdir */
477 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
, sizeof(consumer
->domain_subdir
));
482 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
490 * Create an UST session and add it to the session ust list.
492 static int create_ust_session(struct ltt_session
*session
, const struct lttng_domain
*domain
)
495 struct ltt_ust_session
*lus
= nullptr;
497 LTTNG_ASSERT(session
);
498 LTTNG_ASSERT(domain
);
499 LTTNG_ASSERT(session
->consumer
);
501 switch (domain
->type
) {
502 case LTTNG_DOMAIN_JUL
:
503 case LTTNG_DOMAIN_LOG4J
:
504 case LTTNG_DOMAIN_PYTHON
:
505 case LTTNG_DOMAIN_UST
:
508 ERR("Unknown UST domain on create session %d", domain
->type
);
509 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
513 DBG("Creating UST session");
515 lus
= trace_ust_create_session(session
->id
);
516 if (lus
== nullptr) {
517 ret
= LTTNG_ERR_UST_SESS_FAIL
;
521 lus
->uid
= session
->uid
;
522 lus
->gid
= session
->gid
;
523 lus
->output_traces
= session
->output_traces
;
524 lus
->snapshot_mode
= session
->snapshot_mode
;
525 lus
->live_timer_interval
= session
->live_timer
;
526 session
->ust_session
= lus
;
527 if (session
->shm_path
[0]) {
528 strncpy(lus
->root_shm_path
, session
->shm_path
, sizeof(lus
->root_shm_path
));
529 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
530 strncpy(lus
->shm_path
, session
->shm_path
, sizeof(lus
->shm_path
));
531 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
532 strncat(lus
->shm_path
, "/ust", sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
534 /* Copy session output to the newly created UST session */
535 ret
= copy_session_consumer(domain
->type
, session
);
536 if (ret
!= LTTNG_OK
) {
544 session
->ust_session
= nullptr;
549 * Create a kernel tracer session then create the default channel.
551 static int create_kernel_session(struct ltt_session
*session
)
555 DBG("Creating kernel session");
557 ret
= kernel_create_session(session
);
559 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
563 /* Code flow safety */
564 LTTNG_ASSERT(session
->kernel_session
);
566 /* Copy session output to the newly created Kernel session */
567 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
568 if (ret
!= LTTNG_OK
) {
572 session
->kernel_session
->uid
= session
->uid
;
573 session
->kernel_session
->gid
= session
->gid
;
574 session
->kernel_session
->output_traces
= session
->output_traces
;
575 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
576 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
581 trace_kernel_destroy_session(session
->kernel_session
);
582 session
->kernel_session
= nullptr;
588 * Count number of session permitted by uid/gid.
590 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
__attribute__((unused
)))
593 struct ltt_session
*session
;
594 const struct ltt_session_list
*session_list
= session_get_list();
596 DBG("Counting number of available session for UID %d", uid
);
597 cds_list_for_each_entry (session
, &session_list
->head
, list
) {
598 if (!session_get(session
)) {
601 session_lock(session
);
602 /* Only count the sessions the user can control. */
603 if (session_access_ok(session
, uid
) && !session
->destroyed
) {
606 session_unlock(session
);
607 session_put(session
);
612 static enum lttng_error_code
receive_lttng_trigger(struct command_ctx
*cmd_ctx
,
615 struct lttng_trigger
**_trigger
)
619 ssize_t sock_recv_len
;
620 enum lttng_error_code ret_code
;
621 struct lttng_payload trigger_payload
;
622 struct lttng_trigger
*trigger
= nullptr;
624 lttng_payload_init(&trigger_payload
);
625 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
626 ret
= lttng_dynamic_buffer_set_size(&trigger_payload
.buffer
, trigger_len
);
628 ret_code
= LTTNG_ERR_NOMEM
;
632 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_payload
.buffer
.data
, trigger_len
);
633 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
634 ERR("Failed to receive trigger in command payload");
636 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
640 /* Receive fds, if any. */
641 if (cmd_ctx
->lsm
.fd_count
> 0) {
642 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
643 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
644 if (sock_recv_len
> 0 && sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
645 ERR("Failed to receive all file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
646 cmd_ctx
->lsm
.fd_count
,
648 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
651 } else if (sock_recv_len
<= 0) {
652 ERR("Failed to receive file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
653 cmd_ctx
->lsm
.fd_count
,
655 ret_code
= LTTNG_ERR_FATAL
;
661 /* Deserialize trigger. */
663 struct lttng_payload_view view
=
664 lttng_payload_view_from_payload(&trigger_payload
, 0, -1);
666 if (lttng_trigger_create_from_payload(&view
, &trigger
) != trigger_len
) {
667 ERR("Invalid trigger received as part of command payload");
668 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
669 lttng_trigger_put(trigger
);
678 lttng_payload_reset(&trigger_payload
);
682 static enum lttng_error_code
receive_lttng_error_query(struct command_ctx
*cmd_ctx
,
685 struct lttng_error_query
**_query
)
689 ssize_t sock_recv_len
;
690 enum lttng_error_code ret_code
;
691 struct lttng_payload query_payload
;
692 struct lttng_error_query
*query
= nullptr;
694 lttng_payload_init(&query_payload
);
695 query_len
= (size_t) cmd_ctx
->lsm
.u
.error_query
.length
;
696 ret
= lttng_dynamic_buffer_set_size(&query_payload
.buffer
, query_len
);
698 ret_code
= LTTNG_ERR_NOMEM
;
702 sock_recv_len
= lttcomm_recv_unix_sock(sock
, query_payload
.buffer
.data
, query_len
);
703 if (sock_recv_len
< 0 || sock_recv_len
!= query_len
) {
704 ERR("Failed to receive error query in command payload");
706 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
710 /* Receive fds, if any. */
711 if (cmd_ctx
->lsm
.fd_count
> 0) {
712 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
713 sock
, cmd_ctx
->lsm
.fd_count
, &query_payload
);
714 if (sock_recv_len
> 0 && sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
715 ERR("Failed to receive all file descriptors for error query in command payload: expected fd count = %u, ret = %d",
716 cmd_ctx
->lsm
.fd_count
,
718 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
721 } else if (sock_recv_len
<= 0) {
722 ERR("Failed to receive file descriptors for error query in command payload: expected fd count = %u, ret = %d",
723 cmd_ctx
->lsm
.fd_count
,
725 ret_code
= LTTNG_ERR_FATAL
;
731 /* Deserialize error query. */
733 struct lttng_payload_view view
=
734 lttng_payload_view_from_payload(&query_payload
, 0, -1);
736 if (lttng_error_query_create_from_payload(&view
, &query
) != query_len
) {
737 ERR("Invalid error query received as part of command payload");
738 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
747 lttng_payload_reset(&query_payload
);
751 static enum lttng_error_code
receive_lttng_event(struct command_ctx
*cmd_ctx
,
754 struct lttng_event
**out_event
,
755 char **out_filter_expression
,
756 struct lttng_bytecode
**out_bytecode
,
757 struct lttng_event_exclusion
**out_exclusion
)
761 ssize_t sock_recv_len
;
762 enum lttng_error_code ret_code
;
763 struct lttng_payload event_payload
;
764 struct lttng_event
*local_event
= nullptr;
765 char *local_filter_expression
= nullptr;
766 struct lttng_bytecode
*local_bytecode
= nullptr;
767 struct lttng_event_exclusion
*local_exclusion
= nullptr;
769 lttng_payload_init(&event_payload
);
770 if (cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
) {
771 event_len
= (size_t) cmd_ctx
->lsm
.u
.enable
.length
;
772 } else if (cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
) {
773 event_len
= (size_t) cmd_ctx
->lsm
.u
.disable
.length
;
778 ret
= lttng_dynamic_buffer_set_size(&event_payload
.buffer
, event_len
);
780 ret_code
= LTTNG_ERR_NOMEM
;
784 sock_recv_len
= lttcomm_recv_unix_sock(sock
, event_payload
.buffer
.data
, event_len
);
785 if (sock_recv_len
< 0 || sock_recv_len
!= event_len
) {
786 ERR("Failed to receive event in command payload");
788 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
792 /* Receive fds, if any. */
793 if (cmd_ctx
->lsm
.fd_count
> 0) {
794 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
795 sock
, cmd_ctx
->lsm
.fd_count
, &event_payload
);
796 if (sock_recv_len
> 0 && sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
797 ERR("Failed to receive all file descriptors for event in command payload: expected fd count = %u, ret = %d",
798 cmd_ctx
->lsm
.fd_count
,
800 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
803 } else if (sock_recv_len
<= 0) {
804 ERR("Failed to receive file descriptors for event in command payload: expected fd count = %u, ret = %d",
805 cmd_ctx
->lsm
.fd_count
,
807 ret_code
= LTTNG_ERR_FATAL
;
813 /* Deserialize event. */
816 struct lttng_payload_view event_view
=
817 lttng_payload_view_from_payload(&event_payload
, 0, -1);
819 len
= lttng_event_create_from_payload(&event_view
,
822 &local_filter_expression
,
826 ERR("Failed to create an event from the received buffer");
827 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
831 if (len
!= event_len
) {
832 ERR("Userspace probe location from the received buffer is not the advertised length: header length = %zu" PRIu32
833 ", payload length = %zd",
836 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
841 *out_event
= local_event
;
842 *out_exclusion
= local_exclusion
;
843 *out_filter_expression
= local_filter_expression
;
844 *out_bytecode
= local_bytecode
;
845 local_event
= nullptr;
846 local_exclusion
= nullptr;
847 local_filter_expression
= nullptr;
848 local_bytecode
= nullptr;
853 lttng_payload_reset(&event_payload
);
854 lttng_event_destroy(local_event
);
855 free(local_filter_expression
);
856 free(local_bytecode
);
857 free(local_exclusion
);
861 static enum lttng_error_code
862 receive_lttng_event_context(const struct command_ctx
*cmd_ctx
,
865 struct lttng_event_context
**out_event_context
)
868 const size_t event_context_len
= (size_t) cmd_ctx
->lsm
.u
.context
.length
;
869 ssize_t sock_recv_len
;
870 enum lttng_error_code ret_code
;
871 struct lttng_payload event_context_payload
;
872 struct lttng_event_context
*context
= nullptr;
874 lttng_payload_init(&event_context_payload
);
876 ret
= lttng_dynamic_buffer_set_size(&event_context_payload
.buffer
, event_context_len
);
878 ret_code
= LTTNG_ERR_NOMEM
;
883 lttcomm_recv_unix_sock(sock
, event_context_payload
.buffer
.data
, event_context_len
);
884 if (sock_recv_len
< 0 || sock_recv_len
!= event_context_len
) {
885 ERR("Failed to receive event context in command payload");
887 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
891 /* Deserialize event. */
894 struct lttng_payload_view event_context_view
=
895 lttng_payload_view_from_payload(&event_context_payload
, 0, -1);
897 len
= lttng_event_context_create_from_payload(&event_context_view
, &context
);
900 ERR("Failed to create a event context from the received buffer");
901 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
905 if (len
!= event_context_len
) {
906 ERR("Event context from the received buffer is not the advertised length: expected length = %zu, payload length = %zd",
909 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
914 *out_event_context
= context
;
919 lttng_event_context_destroy(context
);
920 lttng_payload_reset(&event_context_payload
);
925 * Version of setup_lttng_msg() without command header.
928 setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
, void *payload_buf
, size_t payload_len
)
930 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, nullptr, 0);
934 * Check if the current kernel tracer supports the session rotation feature.
935 * Return 1 if it does, 0 otherwise.
937 static int check_rotate_compatible()
941 if (the_kernel_tracer_version
.major
!= 2 || the_kernel_tracer_version
.minor
< 11) {
942 DBG("Kernel tracer version is not compatible with the rotation feature");
950 * Send data on a unix socket using the liblttsessiondcomm API.
952 * Return lttcomm error code.
954 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
957 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
959 /* Check valid length */
960 if (view
->buffer
.size
== 0) {
965 ret
= lttcomm_send_unix_sock(sock
, view
->buffer
.data
, view
->buffer
.size
);
971 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
981 static void command_ctx_set_status_code(command_ctx
& cmd_ctx
, enum lttng_error_code status_code
)
983 LTTNG_ASSERT(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(lttcomm_lttng_msg
));
984 ((struct lttcomm_lttng_msg
*) (cmd_ctx
.reply_payload
.buffer
.data
))->ret_code
= status_code
;
988 * Process the command requested by the lttng client within the command
989 * context structure. This function make sure that the return structure (llm)
990 * is set and ready for transmission before returning.
992 * Return any error encountered or 0 for success.
994 * "sock" is only used for special-case var. len data.
995 * A command may assume the ownership of the socket, in which case its value
996 * should be set to -1.
998 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
, int *sock_error
)
1001 bool need_tracing_session
= true;
1003 bool need_consumerd
;
1005 if (!lttcomm_sessiond_command_is_valid((lttcomm_sessiond_command
) cmd_ctx
->lsm
.cmd_type
)) {
1006 ERR("Unknown client command received: command id = %" PRIu32
,
1007 cmd_ctx
->lsm
.cmd_type
);
1008 ret
= LTTNG_ERR_UND
;
1012 DBG("Processing client command '%s\' (%d)",
1013 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) cmd_ctx
->lsm
.cmd_type
),
1014 cmd_ctx
->lsm
.cmd_type
);
1018 switch (cmd_ctx
->lsm
.cmd_type
) {
1019 case LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
:
1020 case LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
:
1021 case LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
:
1022 case LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
:
1023 case LTTCOMM_SESSIOND_COMMAND_START_TRACE
:
1024 case LTTCOMM_SESSIOND_COMMAND_STOP_TRACE
:
1025 case LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
:
1026 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_ADD_OUTPUT
:
1027 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_DEL_OUTPUT
:
1028 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_LIST_OUTPUT
:
1029 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_RECORD
:
1030 case LTTCOMM_SESSIOND_COMMAND_SAVE_SESSION
:
1031 case LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH
:
1032 case LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA
:
1033 case LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP
:
1034 case LTTCOMM_SESSIOND_COMMAND_ROTATE_SESSION
:
1035 case LTTCOMM_SESSIOND_COMMAND_ROTATION_GET_INFO
:
1036 case LTTCOMM_SESSIOND_COMMAND_ROTATION_SET_SCHEDULE
:
1037 case LTTCOMM_SESSIOND_COMMAND_SESSION_LIST_ROTATION_SCHEDULES
:
1038 case LTTCOMM_SESSIOND_COMMAND_CLEAR_SESSION
:
1039 case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
:
1040 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
1041 case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
:
1042 need_domain
= false;
1048 /* Needs a functioning consumerd? */
1049 switch (cmd_ctx
->lsm
.cmd_type
) {
1050 case LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
:
1051 case LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
:
1052 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
1053 need_consumerd
= false;
1056 need_consumerd
= true;
1060 if (the_config
.no_kernel
&& need_domain
&&
1061 cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
1063 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1065 ret
= LTTNG_ERR_KERN_NA
;
1070 /* Deny register consumer if we already have a spawned consumer. */
1071 if (cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
) {
1072 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1073 if (the_kconsumer_data
.pid
> 0) {
1074 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1075 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1078 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1082 * Check for command that don't needs to allocate a returned payload. We do
1083 * this here so we don't have to make the call for no payload at each
1086 switch (cmd_ctx
->lsm
.cmd_type
) {
1087 case LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
:
1088 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
:
1089 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
:
1090 case LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
:
1091 case LTTCOMM_SESSIOND_COMMAND_LIST_CHANNELS
:
1092 case LTTCOMM_SESSIOND_COMMAND_LIST_EVENTS
:
1093 case LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS
:
1094 case LTTCOMM_SESSIOND_COMMAND_SESSION_LIST_ROTATION_SCHEDULES
:
1095 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_GET_POLICY
:
1096 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1097 case LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
:
1098 case LTTCOMM_SESSIOND_COMMAND_ROTATE_SESSION
:
1099 case LTTCOMM_SESSIOND_COMMAND_ROTATION_GET_INFO
:
1100 case LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
:
1101 case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
:
1102 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
1105 /* Setup lttng message with no payload */
1106 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, nullptr, 0);
1108 /* This label does not try to unlock the session */
1109 goto init_setup_error
;
1113 /* Commands that DO NOT need a session. */
1114 switch (cmd_ctx
->lsm
.cmd_type
) {
1115 case LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
:
1116 case LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
:
1117 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
:
1118 case LTTCOMM_SESSIOND_COMMAND_LIST_SYSCALLS
:
1119 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
:
1120 case LTTCOMM_SESSIOND_COMMAND_SAVE_SESSION
:
1121 case LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
:
1122 case LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
:
1123 case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
:
1124 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
1125 case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
:
1126 need_tracing_session
= false;
1129 if (strnlen(cmd_ctx
->lsm
.session
.name
, sizeof(cmd_ctx
->lsm
.session
.name
)) ==
1130 sizeof(cmd_ctx
->lsm
.session
.name
)) {
1131 LTTNG_THROW_INVALID_ARGUMENT_ERROR(
1132 "Session name received from lttng-ctl client is not null-terminated");
1135 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
1137 * We keep the session list lock across _all_ commands
1138 * for now, because the per-session lock does not
1139 * handle teardown properly.
1141 session_lock_list();
1142 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
1143 if (cmd_ctx
->session
== nullptr) {
1144 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1147 /* Acquire lock for the session */
1148 session_lock(cmd_ctx
->session
);
1154 * Commands that need a valid session but should NOT create one if none
1155 * exists. Instead of creating one and destroying it when the command is
1156 * handled, process that right before so we save some round trip in useless
1159 switch (cmd_ctx
->lsm
.cmd_type
) {
1160 case LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL
:
1161 case LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
:
1162 switch (cmd_ctx
->lsm
.domain
.type
) {
1163 case LTTNG_DOMAIN_KERNEL
:
1164 if (!cmd_ctx
->session
->kernel_session
) {
1165 ret
= LTTNG_ERR_NO_CHANNEL
;
1169 case LTTNG_DOMAIN_JUL
:
1170 case LTTNG_DOMAIN_LOG4J
:
1171 case LTTNG_DOMAIN_PYTHON
:
1172 case LTTNG_DOMAIN_UST
:
1173 if (!cmd_ctx
->session
->ust_session
) {
1174 ret
= LTTNG_ERR_NO_CHANNEL
;
1179 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1191 * Check domain type for specific "pre-action".
1193 switch (cmd_ctx
->lsm
.domain
.type
) {
1194 case LTTNG_DOMAIN_KERNEL
:
1196 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1200 /* Kernel tracer check */
1201 if (!kernel_tracer_is_initialized()) {
1202 /* Basically, load kernel tracer modules */
1203 ret
= init_kernel_tracer();
1209 /* Consumer is in an ERROR state. Report back to client */
1210 if (need_consumerd
&& uatomic_read(&the_kernel_consumerd_state
) == CONSUMER_ERROR
) {
1211 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1215 /* Need a session for kernel command */
1216 if (need_tracing_session
) {
1217 if (cmd_ctx
->session
->kernel_session
== nullptr) {
1218 ret
= create_kernel_session(cmd_ctx
->session
);
1219 if (ret
!= LTTNG_OK
) {
1220 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1225 /* Start the kernel consumer daemon */
1226 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1227 if (the_kconsumer_data
.pid
== 0 &&
1228 cmd_ctx
->lsm
.cmd_type
!= LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
) {
1229 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1230 ret
= start_consumerd(&the_kconsumer_data
);
1232 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1235 uatomic_set(&the_kernel_consumerd_state
, CONSUMER_STARTED
);
1237 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1241 * The consumer was just spawned so we need to add the socket to
1242 * the consumer output of the session if exist.
1244 ret
= consumer_create_socket(&the_kconsumer_data
,
1245 cmd_ctx
->session
->kernel_session
->consumer
);
1252 case LTTNG_DOMAIN_JUL
:
1253 case LTTNG_DOMAIN_LOG4J
:
1254 case LTTNG_DOMAIN_PYTHON
:
1255 if (!agent_tracing_is_enabled()) {
1256 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1260 case LTTNG_DOMAIN_UST
:
1262 if (!ust_app_supported()) {
1263 ret
= LTTNG_ERR_NO_UST
;
1267 /* Consumer is in an ERROR state. Report back to client */
1268 if (need_consumerd
&& uatomic_read(&the_ust_consumerd_state
) == CONSUMER_ERROR
) {
1269 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1273 if (need_tracing_session
) {
1274 /* Create UST session if none exist. */
1275 if (cmd_ctx
->session
->ust_session
== nullptr) {
1276 lttng_domain domain
= cmd_ctx
->lsm
.domain
;
1277 ret
= create_ust_session(cmd_ctx
->session
, &domain
);
1278 if (ret
!= LTTNG_OK
) {
1283 /* Start the UST consumer daemons */
1285 pthread_mutex_lock(&the_ustconsumer64_data
.pid_mutex
);
1286 if (the_config
.consumerd64_bin_path
.value
&&
1287 the_ustconsumer64_data
.pid
== 0 &&
1288 cmd_ctx
->lsm
.cmd_type
!= LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
) {
1289 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1290 ret
= start_consumerd(&the_ustconsumer64_data
);
1292 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1293 uatomic_set(&the_ust_consumerd64_fd
, -EINVAL
);
1297 uatomic_set(&the_ust_consumerd64_fd
,
1298 the_ustconsumer64_data
.cmd_sock
);
1299 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1301 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1305 * Setup socket for consumer 64 bit. No need for atomic access
1306 * since it was set above and can ONLY be set in this thread.
1308 ret
= consumer_create_socket(&the_ustconsumer64_data
,
1309 cmd_ctx
->session
->ust_session
->consumer
);
1315 pthread_mutex_lock(&the_ustconsumer32_data
.pid_mutex
);
1316 if (the_config
.consumerd32_bin_path
.value
&&
1317 the_ustconsumer32_data
.pid
== 0 &&
1318 cmd_ctx
->lsm
.cmd_type
!= LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
) {
1319 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1320 ret
= start_consumerd(&the_ustconsumer32_data
);
1322 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1323 uatomic_set(&the_ust_consumerd32_fd
, -EINVAL
);
1327 uatomic_set(&the_ust_consumerd32_fd
,
1328 the_ustconsumer32_data
.cmd_sock
);
1329 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1331 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1335 * Setup socket for consumer 32 bit. No need for atomic access
1336 * since it was set above and can ONLY be set in this thread.
1338 ret
= consumer_create_socket(&the_ustconsumer32_data
,
1339 cmd_ctx
->session
->ust_session
->consumer
);
1351 /* Validate consumer daemon state when start/stop trace command */
1352 if (cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_START_TRACE
||
1353 cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_STOP_TRACE
) {
1354 switch (cmd_ctx
->lsm
.domain
.type
) {
1355 case LTTNG_DOMAIN_NONE
:
1357 case LTTNG_DOMAIN_JUL
:
1358 case LTTNG_DOMAIN_LOG4J
:
1359 case LTTNG_DOMAIN_PYTHON
:
1360 case LTTNG_DOMAIN_UST
:
1361 if (uatomic_read(&the_ust_consumerd_state
) != CONSUMER_STARTED
) {
1362 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1366 case LTTNG_DOMAIN_KERNEL
:
1367 if (uatomic_read(&the_kernel_consumerd_state
) != CONSUMER_STARTED
) {
1368 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1373 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1379 * Check that the UID matches that of the tracing session.
1380 * The root user can interact with all sessions.
1382 if (need_tracing_session
) {
1383 if (!session_access_ok(cmd_ctx
->session
,
1384 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1385 cmd_ctx
->session
->destroyed
) {
1386 ret
= LTTNG_ERR_EPERM
;
1392 * Send relayd information to consumer as soon as we have a domain and a
1395 if (cmd_ctx
->session
&& need_domain
) {
1397 * Setup relayd if not done yet. If the relayd information was already
1398 * sent to the consumer, this call will gracefully return.
1400 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1401 if (ret
!= LTTNG_OK
) {
1406 /* Process by command type */
1407 switch (cmd_ctx
->lsm
.cmd_type
) {
1408 case LTTCOMM_SESSIOND_COMMAND_ADD_CONTEXT
:
1410 struct lttng_event_context
*event_context
= nullptr;
1411 const enum lttng_error_code ret_code
=
1412 receive_lttng_event_context(cmd_ctx
, *sock
, sock_error
, &event_context
);
1414 if (ret_code
!= LTTNG_OK
) {
1415 ret
= (int) ret_code
;
1419 ret
= cmd_add_context(cmd_ctx
, event_context
, the_kernel_poll_pipe
[1]);
1420 lttng_event_context_destroy(event_context
);
1423 case LTTCOMM_SESSIOND_COMMAND_DISABLE_CHANNEL
:
1425 ret
= cmd_disable_channel(cmd_ctx
->session
,
1426 cmd_ctx
->lsm
.domain
.type
,
1427 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1430 case LTTCOMM_SESSIOND_COMMAND_ENABLE_CHANNEL
:
1432 ret
= cmd_enable_channel(cmd_ctx
, *sock
, the_kernel_poll_pipe
[1]);
1435 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1436 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1438 struct lttng_dynamic_buffer payload
;
1439 struct lttng_buffer_view payload_view
;
1440 const bool add_value
= cmd_ctx
->lsm
.cmd_type
==
1441 LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1442 const size_t name_len
=
1443 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
.name_len
;
1444 const enum lttng_domain_type domain_type
=
1445 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1446 const enum lttng_process_attr process_attr
=
1447 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1448 .process_attr_tracker_add_remove_include_value
.process_attr
;
1449 const enum lttng_process_attr_value_type value_type
=
1450 (enum lttng_process_attr_value_type
) cmd_ctx
->lsm
.u
1451 .process_attr_tracker_add_remove_include_value
.value_type
;
1452 struct process_attr_value
*value
;
1453 enum lttng_error_code ret_code
;
1454 long login_name_max
;
1456 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1457 if (login_name_max
< 0) {
1458 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1459 ret
= LTTNG_ERR_INVALID
;
1463 /* Receive remaining variable length payload if applicable. */
1464 if (name_len
> login_name_max
) {
1466 * POSIX mandates user and group names that are at least
1467 * 8 characters long. Note that although shadow-utils
1468 * (useradd, groupaadd, etc.) use 32 chars as their
1469 * limit (from bits/utmp.h, UT_NAMESIZE),
1470 * LOGIN_NAME_MAX is defined to 256.
1472 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1473 add_value
? "addition" : "removal",
1476 ret
= LTTNG_ERR_INVALID
;
1480 lttng_dynamic_buffer_init(&payload
);
1481 if (name_len
!= 0) {
1483 * Receive variable payload for user/group name
1486 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1488 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1489 add_value
? "add" : "remove");
1490 ret
= LTTNG_ERR_NOMEM
;
1491 goto error_add_remove_tracker_value
;
1494 ret
= lttcomm_recv_unix_sock(*sock
, payload
.data
, name_len
);
1496 ERR("Failed to receive payload of %s process attribute tracker value argument",
1497 add_value
? "add" : "remove");
1499 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1500 goto error_add_remove_tracker_value
;
1504 payload_view
= lttng_buffer_view_from_dynamic_buffer(&payload
, 0, name_len
);
1505 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1506 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1507 goto error_add_remove_tracker_value
;
1511 * Validate the value type and domains are legal for the process
1512 * attribute tracker that is specified and convert the value to
1513 * add/remove to the internal sessiond representation.
1515 ret_code
= process_attr_value_from_comm(
1519 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
.integral_value
,
1522 if (ret_code
!= LTTNG_OK
) {
1524 goto error_add_remove_tracker_value
;
1528 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1529 cmd_ctx
->session
, domain_type
, process_attr
, value
);
1531 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1532 cmd_ctx
->session
, domain_type
, process_attr
, value
);
1534 process_attr_value_destroy(value
);
1535 error_add_remove_tracker_value
:
1536 lttng_dynamic_buffer_reset(&payload
);
1539 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_GET_POLICY
:
1541 enum lttng_tracking_policy tracking_policy
;
1542 const enum lttng_domain_type domain_type
=
1543 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1544 const enum lttng_process_attr process_attr
=
1545 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1546 .process_attr_tracker_get_tracking_policy
.process_attr
;
1548 ret
= cmd_process_attr_tracker_get_tracking_policy(
1549 cmd_ctx
->session
, domain_type
, process_attr
, &tracking_policy
);
1550 if (ret
!= LTTNG_OK
) {
1554 uint32_t tracking_policy_u32
= tracking_policy
;
1555 ret
= setup_lttng_msg_no_cmd_header(
1556 cmd_ctx
, &tracking_policy_u32
, sizeof(uint32_t));
1558 ret
= LTTNG_ERR_NOMEM
;
1564 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_SET_POLICY
:
1566 const enum lttng_tracking_policy tracking_policy
=
1567 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1568 .process_attr_tracker_set_tracking_policy
.tracking_policy
;
1569 const enum lttng_domain_type domain_type
=
1570 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1571 const enum lttng_process_attr process_attr
=
1572 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1573 .process_attr_tracker_set_tracking_policy
.process_attr
;
1575 ret
= cmd_process_attr_tracker_set_tracking_policy(
1576 cmd_ctx
->session
, domain_type
, process_attr
, tracking_policy
);
1577 if (ret
!= LTTNG_OK
) {
1582 case LTTCOMM_SESSIOND_COMMAND_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1584 struct lttng_process_attr_values
*values
;
1585 struct lttng_dynamic_buffer reply
;
1586 const enum lttng_domain_type domain_type
=
1587 (enum lttng_domain_type
) cmd_ctx
->lsm
.domain
.type
;
1588 const enum lttng_process_attr process_attr
=
1589 (enum lttng_process_attr
)
1590 cmd_ctx
->lsm
.u
.process_attr_tracker_get_inclusion_set
.process_attr
;
1592 ret
= cmd_process_attr_tracker_get_inclusion_set(
1593 cmd_ctx
->session
, domain_type
, process_attr
, &values
);
1594 if (ret
!= LTTNG_OK
) {
1598 lttng_dynamic_buffer_init(&reply
);
1599 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1601 goto error_tracker_get_inclusion_set
;
1604 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, reply
.data
, reply
.size
);
1606 ret
= LTTNG_ERR_NOMEM
;
1607 goto error_tracker_get_inclusion_set
;
1611 error_tracker_get_inclusion_set
:
1612 lttng_process_attr_values_destroy(values
);
1613 lttng_dynamic_buffer_reset(&reply
);
1616 case LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
:
1617 case LTTCOMM_SESSIOND_COMMAND_DISABLE_EVENT
:
1619 struct lttng_event
*event
;
1620 char *filter_expression
;
1621 struct lttng_event_exclusion
*exclusions
;
1622 struct lttng_bytecode
*bytecode
;
1623 const enum lttng_error_code ret_code
= receive_lttng_event(cmd_ctx
,
1631 if (ret_code
!= LTTNG_OK
) {
1632 ret
= (int) ret_code
;
1637 * Ownership of filter_expression, exclusions, and bytecode is
1638 * always transferred.
1640 ret
= cmd_ctx
->lsm
.cmd_type
== LTTCOMM_SESSIOND_COMMAND_ENABLE_EVENT
?
1641 cmd_enable_event(cmd_ctx
,
1646 the_kernel_poll_pipe
[1]) :
1647 cmd_disable_event(cmd_ctx
, event
, filter_expression
, bytecode
, exclusions
);
1648 lttng_event_destroy(event
);
1651 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINTS
:
1653 enum lttng_error_code ret_code
;
1654 size_t original_payload_size
;
1655 size_t payload_size
;
1656 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1658 ret
= setup_empty_lttng_msg(cmd_ctx
);
1660 ret
= LTTNG_ERR_NOMEM
;
1664 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1666 session_lock_list();
1667 ret_code
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &cmd_ctx
->reply_payload
);
1668 session_unlock_list();
1669 if (ret_code
!= LTTNG_OK
) {
1670 ret
= (int) ret_code
;
1674 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1675 original_payload_size
;
1676 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1681 case LTTCOMM_SESSIOND_COMMAND_LIST_TRACEPOINT_FIELDS
:
1683 enum lttng_error_code ret_code
;
1684 size_t original_payload_size
;
1685 size_t payload_size
;
1686 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1688 ret
= setup_empty_lttng_msg(cmd_ctx
);
1690 ret
= LTTNG_ERR_NOMEM
;
1694 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1696 session_lock_list();
1697 ret_code
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1698 &cmd_ctx
->reply_payload
);
1699 session_unlock_list();
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 ret
= setup_empty_lttng_msg(cmd_ctx
);
1721 ret
= LTTNG_ERR_NOMEM
;
1725 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1727 ret_code
= cmd_list_syscalls(&cmd_ctx
->reply_payload
);
1728 if (ret_code
!= LTTNG_OK
) {
1729 ret
= (int) ret_code
;
1733 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1734 original_payload_size
;
1735 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1740 case LTTCOMM_SESSIOND_COMMAND_SET_CONSUMER_URI
:
1743 struct lttng_uri
*uris
;
1745 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1746 len
= nb_uri
* sizeof(struct lttng_uri
);
1749 ret
= LTTNG_ERR_INVALID
;
1753 uris
= calloc
<lttng_uri
>(nb_uri
);
1754 if (uris
== nullptr) {
1755 ret
= LTTNG_ERR_FATAL
;
1759 /* Receive variable len data */
1760 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1761 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1763 DBG("No URIs received from client... continuing");
1765 ret
= LTTNG_ERR_SESSION_FAIL
;
1770 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1772 if (ret
!= LTTNG_OK
) {
1778 case LTTCOMM_SESSIOND_COMMAND_START_TRACE
:
1781 * On the first start, if we have a kernel session and we have
1782 * enabled time or size-based rotations, we have to make sure
1783 * the kernel tracer supports it.
1785 if (!cmd_ctx
->session
->has_been_started
&& cmd_ctx
->session
->kernel_session
&&
1786 (cmd_ctx
->session
->rotate_timer_period
|| cmd_ctx
->session
->rotate_size
) &&
1787 !check_rotate_compatible()) {
1788 DBG("Kernel tracer version is not compatible with the rotation feature");
1789 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1792 ret
= cmd_start_trace(cmd_ctx
->session
);
1795 case LTTCOMM_SESSIOND_COMMAND_STOP_TRACE
:
1797 ret
= cmd_stop_trace(cmd_ctx
->session
);
1800 case LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
:
1802 ret
= cmd_destroy_session(cmd_ctx
->session
, sock
);
1805 case LTTCOMM_SESSIOND_COMMAND_LIST_DOMAINS
:
1808 struct lttng_domain
*domains
= nullptr;
1810 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1812 /* Return value is a negative lttng_error_code. */
1817 ret
= setup_lttng_msg_no_cmd_header(
1818 cmd_ctx
, domains
, nb_dom
* sizeof(struct lttng_domain
));
1828 case LTTCOMM_SESSIOND_COMMAND_LIST_CHANNELS
:
1830 enum lttng_error_code ret_code
;
1831 size_t original_payload_size
;
1832 size_t payload_size
;
1833 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1835 ret
= setup_empty_lttng_msg(cmd_ctx
);
1837 ret
= LTTNG_ERR_NOMEM
;
1841 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1843 ret_code
= cmd_list_channels(
1844 cmd_ctx
->lsm
.domain
.type
, cmd_ctx
->session
, &cmd_ctx
->reply_payload
);
1845 if (ret_code
!= LTTNG_OK
) {
1846 ret
= (int) ret_code
;
1850 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1851 original_payload_size
;
1852 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1857 case LTTCOMM_SESSIOND_COMMAND_LIST_EVENTS
:
1859 enum lttng_error_code ret_code
;
1860 size_t original_payload_size
;
1861 size_t payload_size
;
1862 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1864 ret
= setup_empty_lttng_msg(cmd_ctx
);
1866 ret
= LTTNG_ERR_NOMEM
;
1870 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1872 ret_code
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1874 cmd_ctx
->lsm
.u
.list
.channel_name
,
1875 &cmd_ctx
->reply_payload
);
1876 if (ret_code
!= LTTNG_OK
) {
1877 ret
= (int) ret_code
;
1881 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1882 original_payload_size
;
1883 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1888 case LTTCOMM_SESSIOND_COMMAND_LIST_SESSIONS
:
1890 unsigned int nr_sessions
;
1891 lttng_session
*sessions_payload
= nullptr;
1892 size_t payload_len
= 0;
1894 session_lock_list();
1895 nr_sessions
= lttng_sessions_count(LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1896 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1898 if (nr_sessions
> 0) {
1899 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1900 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1901 sessions_payload
= zmalloc
<lttng_session
>(payload_len
);
1902 if (!sessions_payload
) {
1903 session_unlock_list();
1908 cmd_list_lttng_sessions(sessions_payload
,
1910 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1911 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1914 session_unlock_list();
1916 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
, payload_len
);
1917 free(sessions_payload
);
1926 case LTTCOMM_SESSIOND_COMMAND_REGISTER_CONSUMER
:
1928 struct consumer_data
*cdata
;
1930 switch (cmd_ctx
->lsm
.domain
.type
) {
1931 case LTTNG_DOMAIN_KERNEL
:
1932 cdata
= &the_kconsumer_data
;
1935 ret
= LTTNG_ERR_UND
;
1939 ret
= cmd_register_consumer(
1940 cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
, cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1943 case LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
:
1946 enum lttng_kernel_tracer_status status
;
1948 ret
= cmd_kernel_tracer_status(&status
);
1949 if (ret
!= LTTNG_OK
) {
1953 u_status
= (uint32_t) status
;
1954 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &u_status
, 4);
1962 case LTTCOMM_SESSIOND_COMMAND_DATA_PENDING
:
1965 uint8_t pending_ret_byte
;
1967 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1972 * This function may returns 0 or 1 to indicate whether or not
1973 * there is data pending. In case of error, it should return an
1974 * LTTNG_ERR code. However, some code paths may still return
1975 * a nondescript error code, which we handle by returning an
1978 if (pending_ret
== 0 || pending_ret
== 1) {
1980 * ret will be set to LTTNG_OK at the end of
1983 } else if (pending_ret
<= LTTNG_OK
|| pending_ret
>= LTTNG_ERR_NR
) {
1984 ret
= LTTNG_ERR_UNK
;
1991 pending_ret_byte
= (uint8_t) pending_ret
;
1993 /* 1 byte to return whether or not data is pending */
1994 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &pending_ret_byte
, 1);
2003 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_ADD_OUTPUT
:
2005 uint32_t snapshot_id
;
2006 struct lttcomm_lttng_output_id reply
;
2007 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
2009 ret
= cmd_snapshot_add_output(cmd_ctx
->session
, &output
, &snapshot_id
);
2010 if (ret
!= LTTNG_OK
) {
2013 reply
.id
= snapshot_id
;
2015 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
, sizeof(reply
));
2020 /* Copy output list into message payload */
2024 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_DEL_OUTPUT
:
2026 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
2027 ret
= cmd_snapshot_del_output(cmd_ctx
->session
, &output
);
2030 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_LIST_OUTPUT
:
2033 struct lttng_snapshot_output
*outputs
= nullptr;
2035 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
2036 if (nb_output
< 0) {
2041 LTTNG_ASSERT((nb_output
> 0 && outputs
) || nb_output
== 0);
2042 ret
= setup_lttng_msg_no_cmd_header(
2043 cmd_ctx
, outputs
, nb_output
* sizeof(struct lttng_snapshot_output
));
2053 case LTTCOMM_SESSIOND_COMMAND_SNAPSHOT_RECORD
:
2055 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_record
.output
;
2056 ret
= cmd_snapshot_record(cmd_ctx
->session
, &output
, 0); // RFC: set to zero since
2058 // cmd_snapshot_record
2061 case LTTCOMM_SESSIOND_COMMAND_CREATE_SESSION_EXT
:
2063 struct lttng_dynamic_buffer payload
;
2064 struct lttng_session_descriptor
*return_descriptor
= nullptr;
2066 lttng_dynamic_buffer_init(&payload
);
2067 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2068 if (ret
!= LTTNG_OK
) {
2072 ret
= lttng_session_descriptor_serialize(return_descriptor
, &payload
);
2074 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2075 lttng_session_descriptor_destroy(return_descriptor
);
2076 ret
= LTTNG_ERR_NOMEM
;
2079 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
, payload
.size
);
2081 lttng_session_descriptor_destroy(return_descriptor
);
2082 ret
= LTTNG_ERR_NOMEM
;
2085 lttng_dynamic_buffer_reset(&payload
);
2086 lttng_session_descriptor_destroy(return_descriptor
);
2090 case LTTCOMM_SESSIOND_COMMAND_SAVE_SESSION
:
2092 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
, &cmd_ctx
->creds
);
2095 case LTTCOMM_SESSIOND_COMMAND_SET_SESSION_SHM_PATH
:
2097 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2098 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2101 case LTTCOMM_SESSIOND_COMMAND_REGENERATE_METADATA
:
2103 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2106 case LTTCOMM_SESSIOND_COMMAND_REGENERATE_STATEDUMP
:
2108 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2111 case LTTCOMM_SESSIOND_COMMAND_REGISTER_TRIGGER
:
2113 struct lttng_trigger
*payload_trigger
;
2114 struct lttng_trigger
*return_trigger
;
2115 size_t original_reply_payload_size
;
2116 size_t reply_payload_size
;
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 ret
= setup_empty_lttng_msg(cmd_ctx
);
2124 ret
= LTTNG_ERR_NOMEM
;
2128 ret
= receive_lttng_trigger(cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2129 if (ret
!= LTTNG_OK
) {
2133 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2135 ret
= cmd_register_trigger(&cmd_creds
,
2137 cmd_ctx
->lsm
.u
.trigger
.is_trigger_anonymous
,
2138 the_notification_thread_handle
,
2140 if (ret
!= LTTNG_OK
) {
2141 lttng_trigger_put(payload_trigger
);
2145 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2146 lttng_trigger_put(payload_trigger
);
2147 lttng_trigger_put(return_trigger
);
2149 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2150 ret
= LTTNG_ERR_NOMEM
;
2154 reply_payload_size
=
2155 cmd_ctx
->reply_payload
.buffer
.size
- original_reply_payload_size
;
2157 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2162 case LTTCOMM_SESSIOND_COMMAND_UNREGISTER_TRIGGER
:
2164 struct lttng_trigger
*payload_trigger
;
2165 const struct lttng_credentials cmd_creds
= {
2166 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2167 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2170 ret
= receive_lttng_trigger(cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2171 if (ret
!= LTTNG_OK
) {
2175 ret
= cmd_unregister_trigger(
2176 &cmd_creds
, payload_trigger
, the_notification_thread_handle
);
2177 lttng_trigger_put(payload_trigger
);
2180 case LTTCOMM_SESSIOND_COMMAND_ROTATE_SESSION
:
2182 struct lttng_rotate_session_return rotate_return
;
2184 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2186 memset(&rotate_return
, 0, sizeof(rotate_return
));
2187 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2188 DBG("Kernel tracer version is not compatible with the rotation feature");
2189 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2193 ret
= cmd_rotate_session(cmd_ctx
->session
,
2196 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2202 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
, sizeof(rotate_return
));
2211 case LTTCOMM_SESSIOND_COMMAND_ROTATION_GET_INFO
:
2213 struct lttng_rotation_get_info_return get_info_return
;
2215 memset(&get_info_return
, 0, sizeof(get_info_return
));
2216 ret
= cmd_rotate_get_info(cmd_ctx
->session
,
2218 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2224 ret
= setup_lttng_msg_no_cmd_header(
2225 cmd_ctx
, &get_info_return
, sizeof(get_info_return
));
2234 case LTTCOMM_SESSIOND_COMMAND_ROTATION_SET_SCHEDULE
:
2237 enum lttng_rotation_schedule_type schedule_type
;
2240 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2241 DBG("Kernel tracer version does not support session rotations");
2242 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2246 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2247 schedule_type
= (enum lttng_rotation_schedule_type
)
2248 cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2249 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2251 ret
= cmd_rotation_set_schedule(
2252 cmd_ctx
->session
, set_schedule
, schedule_type
, value
);
2253 if (ret
!= LTTNG_OK
) {
2259 case LTTCOMM_SESSIOND_COMMAND_SESSION_LIST_ROTATION_SCHEDULES
:
2261 lttng_session_list_schedules_return schedules
;
2263 schedules
.periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
;
2264 schedules
.periodic
.value
= cmd_ctx
->session
->rotate_timer_period
;
2265 schedules
.size
.set
= !!cmd_ctx
->session
->rotate_size
;
2266 schedules
.size
.value
= cmd_ctx
->session
->rotate_size
;
2268 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
, sizeof(schedules
));
2277 case LTTCOMM_SESSIOND_COMMAND_CLEAR_SESSION
:
2279 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2282 case LTTCOMM_SESSIOND_COMMAND_LIST_TRIGGERS
:
2284 struct lttng_triggers
*return_triggers
= nullptr;
2285 size_t original_payload_size
;
2286 size_t payload_size
;
2288 ret
= setup_empty_lttng_msg(cmd_ctx
);
2290 ret
= LTTNG_ERR_NOMEM
;
2294 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2296 ret
= cmd_list_triggers(cmd_ctx
, the_notification_thread_handle
, &return_triggers
);
2297 if (ret
!= LTTNG_OK
) {
2301 LTTNG_ASSERT(return_triggers
);
2302 ret
= lttng_triggers_serialize(return_triggers
, &cmd_ctx
->reply_payload
);
2303 lttng_triggers_destroy(return_triggers
);
2305 ERR("Failed to serialize triggers in reply to `list triggers` command");
2306 ret
= LTTNG_ERR_NOMEM
;
2310 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- original_payload_size
;
2312 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2317 case LTTCOMM_SESSIOND_COMMAND_EXECUTE_ERROR_QUERY
:
2319 struct lttng_error_query
*query
;
2320 const struct lttng_credentials cmd_creds
= {
2321 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2322 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2324 struct lttng_error_query_results
*results
= nullptr;
2325 size_t original_payload_size
;
2326 size_t payload_size
;
2328 ret
= setup_empty_lttng_msg(cmd_ctx
);
2330 ret
= LTTNG_ERR_NOMEM
;
2334 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2336 ret
= receive_lttng_error_query(cmd_ctx
, *sock
, sock_error
, &query
);
2337 if (ret
!= LTTNG_OK
) {
2341 ret
= cmd_execute_error_query(
2342 &cmd_creds
, query
, &results
, the_notification_thread_handle
);
2343 lttng_error_query_destroy(query
);
2344 if (ret
!= LTTNG_OK
) {
2348 LTTNG_ASSERT(results
);
2349 ret
= lttng_error_query_results_serialize(results
, &cmd_ctx
->reply_payload
);
2350 lttng_error_query_results_destroy(results
);
2352 ERR("Failed to serialize error query result set in reply to `execute error query` command");
2353 ret
= LTTNG_ERR_NOMEM
;
2357 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- original_payload_size
;
2359 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2366 ret
= LTTNG_ERR_UND
;
2371 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2372 DBG("Missing llm header, creating one.");
2373 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, nullptr, 0) < 0) {
2378 command_ctx_set_status_code(*cmd_ctx
, static_cast<lttng_error_code
>(ret
));
2381 if (cmd_ctx
->session
) {
2382 session_unlock(cmd_ctx
->session
);
2383 session_put(cmd_ctx
->session
);
2384 cmd_ctx
->session
= nullptr;
2386 if (need_tracing_session
) {
2387 session_unlock_list();
2390 LTTNG_ASSERT(!rcu_read_ongoing());
2394 static int create_client_sock()
2396 int ret
, client_sock
;
2398 /* Create client tool unix socket */
2399 client_sock
= lttcomm_create_unix_sock(the_config
.client_unix_sock_path
.value
);
2400 if (client_sock
< 0) {
2401 ERR("Create unix sock failed: %s", the_config
.client_unix_sock_path
.value
);
2406 /* Set the cloexec flag */
2407 ret
= utils_set_fd_cloexec(client_sock
);
2409 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2410 "Continuing but note that the consumer daemon will have a "
2411 "reference to this socket on exec()",
2415 /* File permission MUST be 660 */
2416 ret
= chmod(the_config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2418 ERR("Set file permissions failed: %s", the_config
.client_unix_sock_path
.value
);
2420 (void) lttcomm_close_unix_sock(client_sock
);
2424 DBG("Created client socket (fd = %i)", client_sock
);
2430 static void cleanup_client_thread(void *data
)
2432 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2434 lttng_pipe_destroy(quit_pipe
);
2437 static void thread_init_cleanup(void *data
__attribute__((unused
)))
2439 set_thread_status(false);
2443 * This thread manage all clients request using the unix client socket for
2446 static void *thread_manage_clients(void *data
)
2448 int sock
= -1, ret
, i
, err
= -1;
2451 struct lttng_poll_event events
;
2452 const int client_sock
= thread_state
.client_sock
;
2453 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2454 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2455 struct command_ctx cmd_ctx
= {};
2457 DBG("[thread] Manage client started");
2459 lttng_payload_init(&cmd_ctx
.reply_payload
);
2461 is_root
= (getuid() == 0);
2463 pthread_cleanup_push(thread_init_cleanup
, nullptr);
2465 rcu_register_thread();
2467 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2469 health_code_update();
2471 ret
= lttcomm_listen_unix_sock(client_sock
);
2477 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2478 * more will be added to this poll set.
2480 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2482 goto error_create_poll
;
2485 /* Add the application registration socket */
2486 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2491 /* Add thread quit pipe */
2492 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
);
2497 /* Set state as running. */
2498 set_thread_status(true);
2499 pthread_cleanup_pop(0);
2501 /* This testpoint is after we signal readiness to the parent. */
2502 if (testpoint(sessiond_thread_manage_clients
)) {
2506 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2510 health_code_update();
2513 const struct cmd_completion_handler
*cmd_completion_handler
;
2515 cmd_ctx
.creds
.uid
= UINT32_MAX
;
2516 cmd_ctx
.creds
.gid
= UINT32_MAX
;
2517 cmd_ctx
.creds
.pid
= 0;
2518 cmd_ctx
.session
= nullptr;
2519 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2520 cmd_ctx
.lttng_msg_size
= 0;
2522 DBG("Accepting client command ...");
2524 /* Inifinite blocking call, waiting for transmission */
2526 health_poll_entry();
2527 ret
= lttng_poll_wait(&events
, -1);
2531 * Restart interrupted system call.
2533 if (errno
== EINTR
) {
2541 for (i
= 0; i
< nb_fd
; i
++) {
2542 /* Fetch once the poll data. */
2543 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
2544 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2546 health_code_update();
2548 /* Activity on thread quit pipe, exiting. */
2549 if (pollfd
== thread_quit_pipe_fd
) {
2550 DBG("Activity on thread quit pipe");
2555 /* Event on the registration socket */
2556 if (revents
& LPOLLIN
) {
2558 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2559 ERR("Client socket poll error");
2562 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2567 DBG("Wait for client response");
2569 health_code_update();
2571 sock
= lttcomm_accept_unix_sock(client_sock
);
2577 * Set the CLOEXEC flag. Return code is useless because either way, the
2580 (void) utils_set_fd_cloexec(sock
);
2582 /* Set socket option for credentials retrieval */
2583 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2588 health_code_update();
2591 * Data is received from the lttng client. The struct
2592 * lttcomm_session_msg (lsm) contains the command and data request of
2595 DBG("Receiving data from client ...");
2596 ret
= lttcomm_recv_creds_unix_sock(
2597 sock
, &cmd_ctx
.lsm
, sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2598 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2599 DBG("Incomplete recv() from client... continuing");
2608 health_code_update();
2610 // TODO: Validate cmd_ctx including sanity check for
2611 // security purpose.
2613 rcu_thread_online();
2615 * This function dispatch the work to the kernel or userspace tracer
2616 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2617 * informations for the client. The command context struct contains
2618 * everything this function may needs.
2621 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2622 rcu_thread_offline();
2632 * TODO: Inform client somehow of the fatal error. At
2633 * this point, ret < 0 means that a zmalloc failed
2634 * (ENOMEM). Error detected but still accept
2635 * command, unless a socket error has been
2640 } catch (const std::bad_alloc
& ex
) {
2641 WARN_FMT("Failed to allocate memory while handling client request: {}",
2645 * Reset the payload contents as the command may have left them in an
2646 * inconsistent state.
2648 (void) setup_empty_lttng_msg(&cmd_ctx
);
2649 command_ctx_set_status_code(cmd_ctx
, LTTNG_ERR_NOMEM
);
2650 } catch (const lttng::ctl::error
& ex
) {
2651 WARN_FMT("Client request failed: {}", ex
.what());
2653 (void) setup_empty_lttng_msg(&cmd_ctx
);
2654 command_ctx_set_status_code(cmd_ctx
, ex
.code());
2655 } catch (const lttng::invalid_argument_error
& ex
) {
2656 WARN_FMT("Client request failed: {}", ex
.what());
2658 (void) setup_empty_lttng_msg(&cmd_ctx
);
2659 command_ctx_set_status_code(cmd_ctx
, LTTNG_ERR_INVALID
);
2660 } catch (const std::exception
& ex
) {
2661 WARN_FMT("Client request failed: {}", ex
.what());
2663 (void) setup_empty_lttng_msg(&cmd_ctx
);
2664 command_ctx_set_status_code(cmd_ctx
, LTTNG_ERR_UNK
);
2667 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2668 WARN("Command returned an invalid status code, returning unknown error: "
2669 "command type = %s (%d), ret = %d",
2670 lttcomm_sessiond_command_str(
2671 (lttcomm_sessiond_command
) cmd_ctx
.lsm
.cmd_type
),
2672 cmd_ctx
.lsm
.cmd_type
,
2674 ret
= LTTNG_ERR_UNK
;
2677 cmd_completion_handler
= cmd_pop_completion_handler();
2678 if (cmd_completion_handler
) {
2679 enum lttng_error_code completion_code
;
2681 completion_code
= cmd_completion_handler
->run(cmd_completion_handler
->data
);
2682 if (completion_code
!= LTTNG_OK
) {
2687 health_code_update();
2690 struct lttng_payload_view view
=
2691 lttng_payload_view_from_payload(&cmd_ctx
.reply_payload
, 0, -1);
2692 struct lttcomm_lttng_msg
*llm
=
2693 (typeof(llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2695 LTTNG_ASSERT(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2696 LTTNG_ASSERT(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2698 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2700 DBG("Sending response (size: %d, retcode: %s (%d))",
2701 cmd_ctx
.lttng_msg_size
,
2702 lttng_strerror(-llm
->ret_code
),
2704 ret
= send_unix_sock(sock
, &view
);
2706 ERR("Failed to send data back to client");
2709 /* End of transmission */
2717 health_code_update();
2729 lttng_poll_clean(&events
);
2733 unlink(the_config
.client_unix_sock_path
.value
);
2734 ret
= close(client_sock
);
2741 ERR("Health error occurred in %s", __func__
);
2744 health_unregister(the_health_sessiond
);
2746 DBG("Client thread dying");
2747 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2748 rcu_unregister_thread();
2752 static bool shutdown_client_thread(void *thread_data
)
2754 struct lttng_pipe
*client_quit_pipe
= (lttng_pipe
*) thread_data
;
2755 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2757 return notify_thread_pipe(write_fd
) == 1;
2760 struct lttng_thread
*launch_client_thread()
2762 bool thread_running
;
2763 struct lttng_pipe
*client_quit_pipe
;
2764 struct lttng_thread
*thread
= nullptr;
2765 int client_sock_fd
= -1;
2767 sem_init(&thread_state
.ready
, 0, 0);
2768 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2769 if (!client_quit_pipe
) {
2773 client_sock_fd
= create_client_sock();
2774 if (client_sock_fd
< 0) {
2778 thread_state
.client_sock
= client_sock_fd
;
2779 thread
= lttng_thread_create("Client management",
2780 thread_manage_clients
,
2781 shutdown_client_thread
,
2782 cleanup_client_thread
,
2787 /* The client thread now owns the client sock fd and the quit pipe. */
2788 client_sock_fd
= -1;
2789 client_quit_pipe
= nullptr;
2792 * This thread is part of the threads that need to be fully
2793 * initialized before the session daemon is marked as "ready".
2795 thread_running
= wait_thread_status();
2796 if (!thread_running
) {
2801 if (client_sock_fd
>= 0) {
2802 if (close(client_sock_fd
)) {
2803 PERROR("Failed to close client socket");
2806 lttng_thread_put(thread
);
2807 cleanup_client_thread(client_quit_pipe
);