2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "common/buffer-view.hpp"
11 #include "common/compat/socket.hpp"
12 #include "common/dynamic-array.hpp"
13 #include "common/dynamic-buffer.hpp"
14 #include "common/fd-handle.hpp"
15 #include "common/payload-view.hpp"
16 #include "common/payload.hpp"
17 #include "common/sessiond-comm/sessiond-comm.hpp"
18 #include "lttng/lttng-error.h"
19 #include "lttng/tracker.h"
20 #include <common/compat/getenv.hpp>
21 #include <common/tracker.hpp>
22 #include <common/unix.hpp>
23 #include <common/utils.hpp>
24 #include <lttng/error-query-internal.hpp>
25 #include <lttng/event-internal.hpp>
26 #include <lttng/session-descriptor-internal.hpp>
27 #include <lttng/session-internal.hpp>
28 #include <lttng/userspace-probe-internal.hpp>
36 #include "agent-thread.hpp"
40 #include "health-sessiond.hpp"
42 #include "lttng-sessiond.hpp"
43 #include "manage-consumer.hpp"
45 #include "testpoint.hpp"
58 static void set_thread_status(bool running
)
60 DBG("Marking client thread's state as %s", running
? "running" : "error");
61 thread_state
.running
= running
;
62 sem_post(&thread_state
.ready
);
65 static bool wait_thread_status(void)
67 DBG("Waiting for client thread to be ready");
68 sem_wait(&thread_state
.ready
);
69 if (thread_state
.running
) {
70 DBG("Client thread is ready");
72 ERR("Initialization of client thread failed");
75 return thread_state
.running
;
79 * Setup the outgoing data buffer for the response (llm) by allocating the
80 * right amount of memory and copying the original information from the lsm
83 * Return 0 on success, negative value on error.
85 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
86 const void *payload_buf
, size_t payload_len
,
87 const void *cmd_header_buf
, size_t cmd_header_len
)
90 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
91 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
92 lttcomm_lttng_msg llm
{};
94 llm
.cmd_type
= cmd_ctx
->lsm
.cmd_type
;
95 llm
.pid
= (uint32_t) cmd_ctx
->lsm
.domain
.attr
.pid
;
96 llm
.cmd_header_size
= (uint32_t) cmd_header_len
;
97 llm
.data_size
= (uint32_t) payload_len
;
99 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
104 lttng_dynamic_pointer_array_clear(&cmd_ctx
->reply_payload
._fd_handles
);
106 cmd_ctx
->lttng_msg_size
= total_msg_size
;
108 /* Append reply header. */
109 ret
= lttng_dynamic_buffer_append(
110 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
115 /* Append command header. */
116 if (cmd_header_len
) {
117 ret
= lttng_dynamic_buffer_append(
118 &cmd_ctx
->reply_payload
.buffer
, cmd_header_buf
,
125 /* Append payload. */
127 ret
= lttng_dynamic_buffer_append(
128 &cmd_ctx
->reply_payload
.buffer
, payload_buf
,
139 static int setup_empty_lttng_msg(struct command_ctx
*cmd_ctx
)
142 const struct lttcomm_lttng_msg llm
= {};
144 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
149 /* Append place-holder reply header. */
150 ret
= lttng_dynamic_buffer_append(
151 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
156 cmd_ctx
->lttng_msg_size
= sizeof(llm
);
161 static void update_lttng_msg(struct command_ctx
*cmd_ctx
, size_t cmd_header_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
, "lttng-consumerd",
249 verbosity
, "-k", "--consumerd-cmd-sock",
250 consumer_data
->cmd_unix_sock_path
,
251 "--consumerd-err-sock",
252 consumer_data
->err_unix_sock_path
,
254 the_config
.tracing_group_name
.value
,
257 case LTTNG_CONSUMER64_UST
:
259 if (the_config
.consumerd64_lib_dir
.value
) {
264 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
268 tmplen
= strlen(the_config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
269 tmpnew
= zmalloc
<char>(tmplen
+ 1 /* \0 */);
274 strcat(tmpnew
, the_config
.consumerd64_lib_dir
.value
);
275 if (tmp
[0] != '\0') {
279 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
286 DBG("Using 64-bit UST consumer at: %s",
287 the_config
.consumerd64_bin_path
.value
);
288 (void) execl(the_config
.consumerd64_bin_path
.value
,
289 "lttng-consumerd", verbosity
, "-u",
290 "--consumerd-cmd-sock",
291 consumer_data
->cmd_unix_sock_path
,
292 "--consumerd-err-sock",
293 consumer_data
->err_unix_sock_path
,
295 the_config
.tracing_group_name
.value
,
299 case LTTNG_CONSUMER32_UST
:
301 if (the_config
.consumerd32_lib_dir
.value
) {
306 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
310 tmplen
= strlen(the_config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
311 tmpnew
= zmalloc
<char>(tmplen
+ 1 /* \0 */);
316 strcat(tmpnew
, the_config
.consumerd32_lib_dir
.value
);
317 if (tmp
[0] != '\0') {
321 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
328 DBG("Using 32-bit UST consumer at: %s",
329 the_config
.consumerd32_bin_path
.value
);
330 (void) execl(the_config
.consumerd32_bin_path
.value
,
331 "lttng-consumerd", verbosity
, "-u",
332 "--consumerd-cmd-sock",
333 consumer_data
->cmd_unix_sock_path
,
334 "--consumerd-err-sock",
335 consumer_data
->err_unix_sock_path
,
337 the_config
.tracing_group_name
.value
,
342 ERR("unknown consumer type");
346 PERROR("Consumer execl()");
348 /* Reaching this point, we got a failure on our execl(). */
350 } else if (pid
> 0) {
353 PERROR("start consumer fork");
361 * Spawn the consumerd daemon and session daemon thread.
363 static int start_consumerd(struct consumer_data
*consumer_data
)
368 * Set the listen() state on the socket since there is a possible race
369 * between the exec() of the consumer daemon and this call if place in the
370 * consumer thread. See bug #366 for more details.
372 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
377 pthread_mutex_lock(&consumer_data
->pid_mutex
);
378 if (consumer_data
->pid
!= 0) {
379 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
383 ret
= spawn_consumerd(consumer_data
);
385 ERR("Spawning consumerd failed");
386 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
390 /* Setting up the consumer_data pid */
391 consumer_data
->pid
= ret
;
392 DBG2("Consumer pid %d", consumer_data
->pid
);
393 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
395 DBG2("Spawning consumer control thread");
396 ret
= spawn_consumer_thread(consumer_data
);
398 ERR("Fatal error spawning consumer control thread");
406 /* Cleanup already created sockets on error. */
407 if (consumer_data
->err_sock
>= 0) {
410 err
= close(consumer_data
->err_sock
);
412 PERROR("close consumer data error socket");
419 * Copy consumer output from the tracing session to the domain session. The
420 * function also applies the right modification on a per domain basis for the
421 * trace files destination directory.
423 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
426 const char *dir_name
;
427 struct consumer_output
*consumer
;
429 LTTNG_ASSERT(session
);
430 LTTNG_ASSERT(session
->consumer
);
433 case LTTNG_DOMAIN_KERNEL
:
434 DBG3("Copying tracing session consumer output in kernel session");
436 * XXX: We should audit the session creation and what this function
437 * does "extra" in order to avoid a destroy since this function is used
438 * in the domain session creation (kernel and ust) only. Same for UST
441 if (session
->kernel_session
->consumer
) {
442 consumer_output_put(session
->kernel_session
->consumer
);
444 session
->kernel_session
->consumer
=
445 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
=
459 consumer_copy_output(session
->consumer
);
460 /* Ease our life a bit for the next part */
461 consumer
= session
->ust_session
->consumer
;
462 dir_name
= DEFAULT_UST_TRACE_DIR
;
465 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
469 /* Append correct directory to subdir */
470 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
471 sizeof(consumer
->domain_subdir
));
476 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
484 * Create an UST session and add it to the session ust list.
486 static int create_ust_session(struct ltt_session
*session
,
487 const struct lttng_domain
*domain
)
490 struct ltt_ust_session
*lus
= NULL
;
492 LTTNG_ASSERT(session
);
493 LTTNG_ASSERT(domain
);
494 LTTNG_ASSERT(session
->consumer
);
496 switch (domain
->type
) {
497 case LTTNG_DOMAIN_JUL
:
498 case LTTNG_DOMAIN_LOG4J
:
499 case LTTNG_DOMAIN_PYTHON
:
500 case LTTNG_DOMAIN_UST
:
503 ERR("Unknown UST domain on create session %d", domain
->type
);
504 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
508 DBG("Creating UST session");
510 lus
= trace_ust_create_session(session
->id
);
512 ret
= LTTNG_ERR_UST_SESS_FAIL
;
516 lus
->uid
= session
->uid
;
517 lus
->gid
= session
->gid
;
518 lus
->output_traces
= session
->output_traces
;
519 lus
->snapshot_mode
= session
->snapshot_mode
;
520 lus
->live_timer_interval
= session
->live_timer
;
521 session
->ust_session
= lus
;
522 if (session
->shm_path
[0]) {
523 strncpy(lus
->root_shm_path
, session
->shm_path
,
524 sizeof(lus
->root_shm_path
));
525 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
526 strncpy(lus
->shm_path
, session
->shm_path
,
527 sizeof(lus
->shm_path
));
528 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
529 strncat(lus
->shm_path
, "/ust",
530 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
532 /* Copy session output to the newly created UST session */
533 ret
= copy_session_consumer(domain
->type
, session
);
534 if (ret
!= LTTNG_OK
) {
542 session
->ust_session
= NULL
;
547 * Create a kernel tracer session then create the default channel.
549 static int create_kernel_session(struct ltt_session
*session
)
553 DBG("Creating kernel session");
555 ret
= kernel_create_session(session
);
557 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
561 /* Code flow safety */
562 LTTNG_ASSERT(session
->kernel_session
);
564 /* Copy session output to the newly created Kernel session */
565 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
566 if (ret
!= LTTNG_OK
) {
570 session
->kernel_session
->uid
= session
->uid
;
571 session
->kernel_session
->gid
= session
->gid
;
572 session
->kernel_session
->output_traces
= session
->output_traces
;
573 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
574 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
579 trace_kernel_destroy_session(session
->kernel_session
);
580 session
->kernel_session
= NULL
;
586 * Count number of session permitted by uid/gid.
588 static unsigned int lttng_sessions_count(uid_t uid
,
589 gid_t gid
__attribute__((unused
)))
592 struct ltt_session
*session
;
593 const struct ltt_session_list
*session_list
= session_get_list();
595 DBG("Counting number of available session for UID %d", uid
);
596 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
597 if (!session_get(session
)) {
600 session_lock(session
);
601 /* Only count the sessions the user can control. */
602 if (session_access_ok(session
, uid
) &&
603 !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
= NULL
;
624 lttng_payload_init(&trigger_payload
);
625 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
626 ret
= lttng_dynamic_buffer_set_size(
627 &trigger_payload
.buffer
, trigger_len
);
629 ret_code
= LTTNG_ERR_NOMEM
;
633 sock_recv_len
= lttcomm_recv_unix_sock(
634 sock
, trigger_payload
.buffer
.data
, trigger_len
);
635 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
636 ERR("Failed to receive trigger in command payload");
638 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
642 /* Receive fds, if any. */
643 if (cmd_ctx
->lsm
.fd_count
> 0) {
644 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
645 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
646 if (sock_recv_len
> 0 &&
647 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
648 ERR("Failed to receive all file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
649 cmd_ctx
->lsm
.fd_count
, (int) ret
);
650 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
653 } else if (sock_recv_len
<= 0) {
654 ERR("Failed to receive file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
655 cmd_ctx
->lsm
.fd_count
, (int) ret
);
656 ret_code
= LTTNG_ERR_FATAL
;
662 /* Deserialize trigger. */
664 struct lttng_payload_view view
=
665 lttng_payload_view_from_payload(
666 &trigger_payload
, 0, -1);
668 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
670 ERR("Invalid trigger received as part of command payload");
671 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
672 lttng_trigger_put(trigger
);
681 lttng_payload_reset(&trigger_payload
);
685 static enum lttng_error_code
receive_lttng_error_query(struct command_ctx
*cmd_ctx
,
688 struct lttng_error_query
**_query
)
692 ssize_t sock_recv_len
;
693 enum lttng_error_code ret_code
;
694 struct lttng_payload query_payload
;
695 struct lttng_error_query
*query
= NULL
;
697 lttng_payload_init(&query_payload
);
698 query_len
= (size_t) cmd_ctx
->lsm
.u
.error_query
.length
;
699 ret
= lttng_dynamic_buffer_set_size(&query_payload
.buffer
, query_len
);
701 ret_code
= LTTNG_ERR_NOMEM
;
705 sock_recv_len
= lttcomm_recv_unix_sock(
706 sock
, query_payload
.buffer
.data
, query_len
);
707 if (sock_recv_len
< 0 || sock_recv_len
!= query_len
) {
708 ERR("Failed to receive error query in command payload");
710 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
714 /* Receive fds, if any. */
715 if (cmd_ctx
->lsm
.fd_count
> 0) {
716 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
717 sock
, cmd_ctx
->lsm
.fd_count
, &query_payload
);
718 if (sock_recv_len
> 0 &&
719 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
720 ERR("Failed to receive all file descriptors for error query in command payload: expected fd count = %u, ret = %d",
721 cmd_ctx
->lsm
.fd_count
, (int) ret
);
722 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
725 } else if (sock_recv_len
<= 0) {
726 ERR("Failed to receive file descriptors for error query in command payload: expected fd count = %u, ret = %d",
727 cmd_ctx
->lsm
.fd_count
, (int) ret
);
728 ret_code
= LTTNG_ERR_FATAL
;
734 /* Deserialize error query. */
736 struct lttng_payload_view view
=
737 lttng_payload_view_from_payload(
738 &query_payload
, 0, -1);
740 if (lttng_error_query_create_from_payload(&view
, &query
) !=
742 ERR("Invalid error query received as part of command payload");
743 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
752 lttng_payload_reset(&query_payload
);
756 static enum lttng_error_code
receive_lttng_event(struct command_ctx
*cmd_ctx
,
759 struct lttng_event
**out_event
,
760 char **out_filter_expression
,
761 struct lttng_bytecode
**out_bytecode
,
762 struct lttng_event_exclusion
**out_exclusion
)
766 ssize_t sock_recv_len
;
767 enum lttng_error_code ret_code
;
768 struct lttng_payload event_payload
;
769 struct lttng_event
*local_event
= NULL
;
770 char *local_filter_expression
= NULL
;
771 struct lttng_bytecode
*local_bytecode
= NULL
;
772 struct lttng_event_exclusion
*local_exclusion
= NULL
;
774 lttng_payload_init(&event_payload
);
775 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_ENABLE_EVENT
) {
776 event_len
= (size_t) cmd_ctx
->lsm
.u
.enable
.length
;
777 } else if (cmd_ctx
->lsm
.cmd_type
== LTTNG_DISABLE_EVENT
) {
778 event_len
= (size_t) cmd_ctx
->lsm
.u
.disable
.length
;
783 ret
= lttng_dynamic_buffer_set_size(&event_payload
.buffer
, event_len
);
785 ret_code
= LTTNG_ERR_NOMEM
;
789 sock_recv_len
= lttcomm_recv_unix_sock(
790 sock
, event_payload
.buffer
.data
, event_len
);
791 if (sock_recv_len
< 0 || sock_recv_len
!= event_len
) {
792 ERR("Failed to receive event in command payload");
794 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
798 /* Receive fds, if any. */
799 if (cmd_ctx
->lsm
.fd_count
> 0) {
800 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
801 sock
, cmd_ctx
->lsm
.fd_count
, &event_payload
);
802 if (sock_recv_len
> 0 &&
803 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
804 ERR("Failed to receive all file descriptors for event in command payload: expected fd count = %u, ret = %d",
805 cmd_ctx
->lsm
.fd_count
, (int) ret
);
806 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
809 } else if (sock_recv_len
<= 0) {
810 ERR("Failed to receive file descriptors for event in command payload: expected fd count = %u, ret = %d",
811 cmd_ctx
->lsm
.fd_count
, (int) ret
);
812 ret_code
= LTTNG_ERR_FATAL
;
818 /* Deserialize event. */
821 struct lttng_payload_view event_view
=
822 lttng_payload_view_from_payload(
823 &event_payload
, 0, -1);
825 len
= lttng_event_create_from_payload(&event_view
, &local_event
,
826 &local_exclusion
, &local_filter_expression
,
830 ERR("Failed to create an event from the received buffer");
831 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
835 if (len
!= event_len
) {
836 ERR("Userspace probe location from the received buffer is not the advertised length: header length = %zu" PRIu32
", payload length = %zd", event_len
, len
);
837 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
842 *out_event
= local_event
;
843 *out_exclusion
= local_exclusion
;
844 *out_filter_expression
= local_filter_expression
;
845 *out_bytecode
= local_bytecode
;
847 local_exclusion
= NULL
;
848 local_filter_expression
= NULL
;
849 local_bytecode
= NULL
;
854 lttng_payload_reset(&event_payload
);
855 lttng_event_destroy(local_event
);
856 free(local_filter_expression
);
857 free(local_bytecode
);
858 free(local_exclusion
);
862 static enum lttng_error_code
receive_lttng_event_context(
863 const struct command_ctx
*cmd_ctx
,
866 struct lttng_event_context
**out_event_context
)
869 const size_t event_context_len
=
870 (size_t) cmd_ctx
->lsm
.u
.context
.length
;
871 ssize_t sock_recv_len
;
872 enum lttng_error_code ret_code
;
873 struct lttng_payload event_context_payload
;
874 struct lttng_event_context
*context
= NULL
;
876 lttng_payload_init(&event_context_payload
);
878 ret
= lttng_dynamic_buffer_set_size(&event_context_payload
.buffer
,
881 ret_code
= LTTNG_ERR_NOMEM
;
885 sock_recv_len
= lttcomm_recv_unix_sock(
886 sock
, event_context_payload
.buffer
.data
,
888 if (sock_recv_len
< 0 || sock_recv_len
!= event_context_len
) {
889 ERR("Failed to receive event context in command payload");
891 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
895 /* Deserialize event. */
898 struct lttng_payload_view event_context_view
=
899 lttng_payload_view_from_payload(
900 &event_context_payload
, 0, -1);
902 len
= lttng_event_context_create_from_payload(
903 &event_context_view
, &context
);
906 ERR("Failed to create a event context from the received buffer");
907 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
911 if (len
!= event_context_len
) {
912 ERR("Event context from the received buffer is not the advertised length: expected length = %zu, payload length = %zd", event_context_len
, len
);
913 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
918 *out_event_context
= context
;
923 lttng_event_context_destroy(context
);
924 lttng_payload_reset(&event_context_payload
);
929 * Version of setup_lttng_msg() without command header.
931 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
932 void *payload_buf
, size_t payload_len
)
934 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
938 * Check if the current kernel tracer supports the session rotation feature.
939 * Return 1 if it does, 0 otherwise.
941 static int check_rotate_compatible(void)
945 if (the_kernel_tracer_version
.major
!= 2 ||
946 the_kernel_tracer_version
.minor
< 11) {
947 DBG("Kernel tracer version is not compatible with the rotation feature");
955 * Send data on a unix socket using the liblttsessiondcomm API.
957 * Return lttcomm error code.
959 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
962 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
964 /* Check valid length */
965 if (view
->buffer
.size
== 0) {
970 ret
= lttcomm_send_unix_sock(
971 sock
, view
->buffer
.data
, view
->buffer
.size
);
977 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
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
,
1002 bool need_tracing_session
= true;
1004 bool need_consumerd
;
1006 DBG("Processing client command '%s\' (%d)",
1007 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) cmd_ctx
->lsm
.cmd_type
),
1008 cmd_ctx
->lsm
.cmd_type
);
1012 switch (cmd_ctx
->lsm
.cmd_type
) {
1013 case LTTNG_CREATE_SESSION_EXT
:
1014 case LTTNG_DESTROY_SESSION
:
1015 case LTTNG_LIST_SESSIONS
:
1016 case LTTNG_LIST_DOMAINS
:
1017 case LTTNG_START_TRACE
:
1018 case LTTNG_STOP_TRACE
:
1019 case LTTNG_DATA_PENDING
:
1020 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1021 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1022 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1023 case LTTNG_SNAPSHOT_RECORD
:
1024 case LTTNG_SAVE_SESSION
:
1025 case LTTNG_SET_SESSION_SHM_PATH
:
1026 case LTTNG_REGENERATE_METADATA
:
1027 case LTTNG_REGENERATE_STATEDUMP
:
1028 case LTTNG_ROTATE_SESSION
:
1029 case LTTNG_ROTATION_GET_INFO
:
1030 case LTTNG_ROTATION_SET_SCHEDULE
:
1031 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1032 case LTTNG_CLEAR_SESSION
:
1033 case LTTNG_LIST_TRIGGERS
:
1034 case LTTNG_EXECUTE_ERROR_QUERY
:
1035 need_domain
= false;
1041 /* Needs a functioning consumerd? */
1042 switch (cmd_ctx
->lsm
.cmd_type
) {
1043 case LTTNG_REGISTER_TRIGGER
:
1044 case LTTNG_UNREGISTER_TRIGGER
:
1045 case LTTNG_EXECUTE_ERROR_QUERY
:
1046 need_consumerd
= false;
1049 need_consumerd
= true;
1053 if (the_config
.no_kernel
&& need_domain
&&
1054 cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
1056 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1058 ret
= LTTNG_ERR_KERN_NA
;
1063 /* Deny register consumer if we already have a spawned consumer. */
1064 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
1065 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1066 if (the_kconsumer_data
.pid
> 0) {
1067 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1068 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1071 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1075 * Check for command that don't needs to allocate a returned payload. We do
1076 * this here so we don't have to make the call for no payload at each
1079 switch(cmd_ctx
->lsm
.cmd_type
) {
1080 case LTTNG_LIST_SESSIONS
:
1081 case LTTNG_LIST_TRACEPOINTS
:
1082 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1083 case LTTNG_LIST_DOMAINS
:
1084 case LTTNG_LIST_CHANNELS
:
1085 case LTTNG_LIST_EVENTS
:
1086 case LTTNG_LIST_SYSCALLS
:
1087 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1088 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1089 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1090 case LTTNG_DATA_PENDING
:
1091 case LTTNG_ROTATE_SESSION
:
1092 case LTTNG_ROTATION_GET_INFO
:
1093 case LTTNG_REGISTER_TRIGGER
:
1094 case LTTNG_LIST_TRIGGERS
:
1095 case LTTNG_EXECUTE_ERROR_QUERY
:
1098 /* Setup lttng message with no payload */
1099 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
1101 /* This label does not try to unlock the session */
1102 goto init_setup_error
;
1106 /* Commands that DO NOT need a session. */
1107 switch (cmd_ctx
->lsm
.cmd_type
) {
1108 case LTTNG_CREATE_SESSION_EXT
:
1109 case LTTNG_LIST_SESSIONS
:
1110 case LTTNG_LIST_TRACEPOINTS
:
1111 case LTTNG_LIST_SYSCALLS
:
1112 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1113 case LTTNG_SAVE_SESSION
:
1114 case LTTNG_REGISTER_TRIGGER
:
1115 case LTTNG_UNREGISTER_TRIGGER
:
1116 case LTTNG_LIST_TRIGGERS
:
1117 case LTTNG_EXECUTE_ERROR_QUERY
:
1118 need_tracing_session
= false;
1121 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
1123 * We keep the session list lock across _all_ commands
1124 * for now, because the per-session lock does not
1125 * handle teardown properly.
1127 session_lock_list();
1128 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
1129 if (cmd_ctx
->session
== NULL
) {
1130 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1133 /* Acquire lock for the session */
1134 session_lock(cmd_ctx
->session
);
1140 * Commands that need a valid session but should NOT create one if none
1141 * exists. Instead of creating one and destroying it when the command is
1142 * handled, process that right before so we save some round trip in useless
1145 switch (cmd_ctx
->lsm
.cmd_type
) {
1146 case LTTNG_DISABLE_CHANNEL
:
1147 case LTTNG_DISABLE_EVENT
:
1148 switch (cmd_ctx
->lsm
.domain
.type
) {
1149 case LTTNG_DOMAIN_KERNEL
:
1150 if (!cmd_ctx
->session
->kernel_session
) {
1151 ret
= LTTNG_ERR_NO_CHANNEL
;
1155 case LTTNG_DOMAIN_JUL
:
1156 case LTTNG_DOMAIN_LOG4J
:
1157 case LTTNG_DOMAIN_PYTHON
:
1158 case LTTNG_DOMAIN_UST
:
1159 if (!cmd_ctx
->session
->ust_session
) {
1160 ret
= LTTNG_ERR_NO_CHANNEL
;
1165 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1177 * Check domain type for specific "pre-action".
1179 switch (cmd_ctx
->lsm
.domain
.type
) {
1180 case LTTNG_DOMAIN_KERNEL
:
1182 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1186 /* Kernel tracer check */
1187 if (!kernel_tracer_is_initialized()) {
1188 /* Basically, load kernel tracer modules */
1189 ret
= init_kernel_tracer();
1195 /* Consumer is in an ERROR state. Report back to client */
1196 if (need_consumerd
&& uatomic_read(&the_kernel_consumerd_state
) ==
1198 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1202 /* Need a session for kernel command */
1203 if (need_tracing_session
) {
1204 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1205 ret
= create_kernel_session(cmd_ctx
->session
);
1206 if (ret
!= LTTNG_OK
) {
1207 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1212 /* Start the kernel consumer daemon */
1213 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1214 if (the_kconsumer_data
.pid
== 0 &&
1215 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1216 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1217 ret
= start_consumerd(&the_kconsumer_data
);
1219 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1222 uatomic_set(&the_kernel_consumerd_state
, CONSUMER_STARTED
);
1224 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1228 * The consumer was just spawned so we need to add the socket to
1229 * the consumer output of the session if exist.
1231 ret
= consumer_create_socket(&the_kconsumer_data
,
1232 cmd_ctx
->session
->kernel_session
->consumer
);
1239 case LTTNG_DOMAIN_JUL
:
1240 case LTTNG_DOMAIN_LOG4J
:
1241 case LTTNG_DOMAIN_PYTHON
:
1242 if (!agent_tracing_is_enabled()) {
1243 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1247 case LTTNG_DOMAIN_UST
:
1249 if (!ust_app_supported()) {
1250 ret
= LTTNG_ERR_NO_UST
;
1254 /* Consumer is in an ERROR state. Report back to client */
1255 if (need_consumerd
&&
1256 uatomic_read(&the_ust_consumerd_state
) ==
1258 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1262 if (need_tracing_session
) {
1263 /* Create UST session if none exist. */
1264 if (cmd_ctx
->session
->ust_session
== NULL
) {
1265 lttng_domain domain
= cmd_ctx
->lsm
.domain
;
1266 ret
= create_ust_session(cmd_ctx
->session
, &domain
);
1267 if (ret
!= LTTNG_OK
) {
1272 /* Start the UST consumer daemons */
1274 pthread_mutex_lock(&the_ustconsumer64_data
.pid_mutex
);
1275 if (the_config
.consumerd64_bin_path
.value
&&
1276 the_ustconsumer64_data
.pid
== 0 &&
1277 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1278 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1279 ret
= start_consumerd(&the_ustconsumer64_data
);
1281 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1282 uatomic_set(&the_ust_consumerd64_fd
, -EINVAL
);
1286 uatomic_set(&the_ust_consumerd64_fd
, the_ustconsumer64_data
.cmd_sock
);
1287 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1289 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1293 * Setup socket for consumer 64 bit. No need for atomic access
1294 * since it was set above and can ONLY be set in this thread.
1296 ret
= consumer_create_socket(&the_ustconsumer64_data
,
1297 cmd_ctx
->session
->ust_session
->consumer
);
1303 pthread_mutex_lock(&the_ustconsumer32_data
.pid_mutex
);
1304 if (the_config
.consumerd32_bin_path
.value
&&
1305 the_ustconsumer32_data
.pid
== 0 &&
1306 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1307 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1308 ret
= start_consumerd(&the_ustconsumer32_data
);
1310 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1311 uatomic_set(&the_ust_consumerd32_fd
, -EINVAL
);
1315 uatomic_set(&the_ust_consumerd32_fd
, the_ustconsumer32_data
.cmd_sock
);
1316 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1318 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1322 * Setup socket for consumer 32 bit. No need for atomic access
1323 * since it was set above and can ONLY be set in this thread.
1325 ret
= consumer_create_socket(&the_ustconsumer32_data
,
1326 cmd_ctx
->session
->ust_session
->consumer
);
1338 /* Validate consumer daemon state when start/stop trace command */
1339 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1340 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1341 switch (cmd_ctx
->lsm
.domain
.type
) {
1342 case LTTNG_DOMAIN_NONE
:
1344 case LTTNG_DOMAIN_JUL
:
1345 case LTTNG_DOMAIN_LOG4J
:
1346 case LTTNG_DOMAIN_PYTHON
:
1347 case LTTNG_DOMAIN_UST
:
1348 if (uatomic_read(&the_ust_consumerd_state
) != CONSUMER_STARTED
) {
1349 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1353 case LTTNG_DOMAIN_KERNEL
:
1354 if (uatomic_read(&the_kernel_consumerd_state
) != CONSUMER_STARTED
) {
1355 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1360 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1366 * Check that the UID matches that of the tracing session.
1367 * The root user can interact with all sessions.
1369 if (need_tracing_session
) {
1370 if (!session_access_ok(cmd_ctx
->session
,
1371 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1372 cmd_ctx
->session
->destroyed
) {
1373 ret
= LTTNG_ERR_EPERM
;
1379 * Send relayd information to consumer as soon as we have a domain and a
1382 if (cmd_ctx
->session
&& need_domain
) {
1384 * Setup relayd if not done yet. If the relayd information was already
1385 * sent to the consumer, this call will gracefully return.
1387 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1388 if (ret
!= LTTNG_OK
) {
1393 /* Process by command type */
1394 switch (cmd_ctx
->lsm
.cmd_type
) {
1395 case LTTNG_ADD_CONTEXT
:
1397 struct lttng_event_context
*event_context
= NULL
;
1398 const enum lttng_error_code ret_code
=
1399 receive_lttng_event_context(
1400 cmd_ctx
, *sock
, sock_error
, &event_context
);
1402 if (ret_code
!= LTTNG_OK
) {
1403 ret
= (int) ret_code
;
1407 ret
= cmd_add_context(cmd_ctx
, event_context
, the_kernel_poll_pipe
[1]);
1408 lttng_event_context_destroy(event_context
);
1411 case LTTNG_DISABLE_CHANNEL
:
1413 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1414 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1417 case LTTNG_ENABLE_CHANNEL
:
1419 ret
= cmd_enable_channel(
1420 cmd_ctx
, *sock
, the_kernel_poll_pipe
[1]);
1423 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1424 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1426 struct lttng_dynamic_buffer payload
;
1427 struct lttng_buffer_view payload_view
;
1428 const bool add_value
=
1429 cmd_ctx
->lsm
.cmd_type
==
1430 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1431 const size_t name_len
=
1432 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1434 const enum lttng_domain_type domain_type
=
1435 (enum lttng_domain_type
)
1436 cmd_ctx
->lsm
.domain
.type
;
1437 const enum lttng_process_attr process_attr
=
1438 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1439 .process_attr_tracker_add_remove_include_value
1441 const enum lttng_process_attr_value_type value_type
=
1442 (enum lttng_process_attr_value_type
) cmd_ctx
1444 .process_attr_tracker_add_remove_include_value
1446 struct process_attr_value
*value
;
1447 enum lttng_error_code ret_code
;
1448 long login_name_max
;
1450 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1451 if (login_name_max
< 0) {
1452 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1453 ret
= LTTNG_ERR_INVALID
;
1457 /* Receive remaining variable length payload if applicable. */
1458 if (name_len
> login_name_max
) {
1460 * POSIX mandates user and group names that are at least
1461 * 8 characters long. Note that although shadow-utils
1462 * (useradd, groupaadd, etc.) use 32 chars as their
1463 * limit (from bits/utmp.h, UT_NAMESIZE),
1464 * LOGIN_NAME_MAX is defined to 256.
1466 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1467 add_value
? "addition" : "removal",
1468 name_len
, login_name_max
);
1469 ret
= LTTNG_ERR_INVALID
;
1473 lttng_dynamic_buffer_init(&payload
);
1474 if (name_len
!= 0) {
1476 * Receive variable payload for user/group name
1479 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1481 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1482 add_value
? "add" : "remove");
1483 ret
= LTTNG_ERR_NOMEM
;
1484 goto error_add_remove_tracker_value
;
1487 ret
= lttcomm_recv_unix_sock(
1488 *sock
, payload
.data
, name_len
);
1490 ERR("Failed to receive payload of %s process attribute tracker value argument",
1491 add_value
? "add" : "remove");
1493 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1494 goto error_add_remove_tracker_value
;
1498 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1499 &payload
, 0, name_len
);
1500 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1501 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1502 goto error_add_remove_tracker_value
;
1506 * Validate the value type and domains are legal for the process
1507 * attribute tracker that is specified and convert the value to
1508 * add/remove to the internal sessiond representation.
1510 ret_code
= process_attr_value_from_comm(domain_type
,
1511 process_attr
, value_type
,
1512 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1514 &payload_view
, &value
);
1515 if (ret_code
!= LTTNG_OK
) {
1517 goto error_add_remove_tracker_value
;
1521 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1522 cmd_ctx
->session
, domain_type
,
1523 process_attr
, value
);
1525 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1526 cmd_ctx
->session
, domain_type
,
1527 process_attr
, value
);
1529 process_attr_value_destroy(value
);
1530 error_add_remove_tracker_value
:
1531 lttng_dynamic_buffer_reset(&payload
);
1534 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1536 enum lttng_tracking_policy tracking_policy
;
1537 const enum lttng_domain_type domain_type
=
1538 (enum lttng_domain_type
)
1539 cmd_ctx
->lsm
.domain
.type
;
1540 const enum lttng_process_attr process_attr
=
1541 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1542 .process_attr_tracker_get_tracking_policy
1545 ret
= cmd_process_attr_tracker_get_tracking_policy(
1546 cmd_ctx
->session
, domain_type
, process_attr
,
1548 if (ret
!= LTTNG_OK
) {
1552 uint32_t tracking_policy_u32
= tracking_policy
;
1553 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1554 &tracking_policy_u32
, sizeof(uint32_t));
1556 ret
= LTTNG_ERR_NOMEM
;
1562 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1564 const enum lttng_tracking_policy tracking_policy
=
1565 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1566 .process_attr_tracker_set_tracking_policy
1568 const enum lttng_domain_type domain_type
=
1569 (enum lttng_domain_type
)
1570 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
1576 ret
= cmd_process_attr_tracker_set_tracking_policy(
1577 cmd_ctx
->session
, domain_type
, process_attr
,
1579 if (ret
!= LTTNG_OK
) {
1584 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1586 struct lttng_process_attr_values
*values
;
1587 struct lttng_dynamic_buffer reply
;
1588 const enum lttng_domain_type domain_type
=
1589 (enum lttng_domain_type
)
1590 cmd_ctx
->lsm
.domain
.type
;
1591 const enum lttng_process_attr process_attr
=
1592 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1593 .process_attr_tracker_get_inclusion_set
1596 ret
= cmd_process_attr_tracker_get_inclusion_set(
1597 cmd_ctx
->session
, domain_type
, process_attr
,
1599 if (ret
!= LTTNG_OK
) {
1603 lttng_dynamic_buffer_init(&reply
);
1604 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1606 goto error_tracker_get_inclusion_set
;
1609 ret
= setup_lttng_msg_no_cmd_header(
1610 cmd_ctx
, reply
.data
, reply
.size
);
1612 ret
= LTTNG_ERR_NOMEM
;
1613 goto error_tracker_get_inclusion_set
;
1617 error_tracker_get_inclusion_set
:
1618 lttng_process_attr_values_destroy(values
);
1619 lttng_dynamic_buffer_reset(&reply
);
1622 case LTTNG_ENABLE_EVENT
:
1623 case LTTNG_DISABLE_EVENT
:
1625 struct lttng_event
*event
;
1626 char *filter_expression
;
1627 struct lttng_event_exclusion
*exclusions
;
1628 struct lttng_bytecode
*bytecode
;
1629 const enum lttng_error_code ret_code
= receive_lttng_event(
1630 cmd_ctx
, *sock
, sock_error
, &event
,
1631 &filter_expression
, &bytecode
, &exclusions
);
1633 if (ret_code
!= LTTNG_OK
) {
1634 ret
= (int) ret_code
;
1639 * Ownership of filter_expression, exclusions, and bytecode is
1640 * always transferred.
1642 ret
= cmd_ctx
->lsm
.cmd_type
== LTTNG_ENABLE_EVENT
?
1643 cmd_enable_event(cmd_ctx
, event
,
1644 filter_expression
, exclusions
,
1646 the_kernel_poll_pipe
[1]) :
1647 cmd_disable_event(cmd_ctx
, event
,
1648 filter_expression
, bytecode
,
1650 lttng_event_destroy(event
);
1653 case LTTNG_LIST_TRACEPOINTS
:
1655 enum lttng_error_code ret_code
;
1656 size_t original_payload_size
;
1657 size_t payload_size
;
1658 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1660 ret
= setup_empty_lttng_msg(cmd_ctx
);
1662 ret
= LTTNG_ERR_NOMEM
;
1666 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1668 session_lock_list();
1669 ret_code
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
,
1670 &cmd_ctx
->reply_payload
);
1671 session_unlock_list();
1672 if (ret_code
!= LTTNG_OK
) {
1673 ret
= (int) ret_code
;
1677 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1678 command_header_size
- original_payload_size
;
1679 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1684 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1686 enum lttng_error_code ret_code
;
1687 size_t original_payload_size
;
1688 size_t payload_size
;
1689 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1691 ret
= setup_empty_lttng_msg(cmd_ctx
);
1693 ret
= LTTNG_ERR_NOMEM
;
1697 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1699 session_lock_list();
1700 ret_code
= cmd_list_tracepoint_fields(
1701 cmd_ctx
->lsm
.domain
.type
, &cmd_ctx
->reply_payload
);
1702 session_unlock_list();
1703 if (ret_code
!= LTTNG_OK
) {
1704 ret
= (int) ret_code
;
1708 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1709 command_header_size
- original_payload_size
;
1710 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1715 case LTTNG_LIST_SYSCALLS
:
1717 enum lttng_error_code ret_code
;
1718 size_t original_payload_size
;
1719 size_t payload_size
;
1720 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1722 ret
= setup_empty_lttng_msg(cmd_ctx
);
1724 ret
= LTTNG_ERR_NOMEM
;
1728 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1730 ret_code
= cmd_list_syscalls(&cmd_ctx
->reply_payload
);
1731 if (ret_code
!= LTTNG_OK
) {
1732 ret
= (int) ret_code
;
1736 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1737 command_header_size
- original_payload_size
;
1738 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1743 case LTTNG_SET_CONSUMER_URI
:
1746 struct lttng_uri
*uris
;
1748 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1749 len
= nb_uri
* sizeof(struct lttng_uri
);
1752 ret
= LTTNG_ERR_INVALID
;
1756 uris
= calloc
<lttng_uri
>(nb_uri
);
1758 ret
= LTTNG_ERR_FATAL
;
1762 /* Receive variable len data */
1763 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1764 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1766 DBG("No URIs received from client... continuing");
1768 ret
= LTTNG_ERR_SESSION_FAIL
;
1773 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1775 if (ret
!= LTTNG_OK
) {
1782 case LTTNG_START_TRACE
:
1785 * On the first start, if we have a kernel session and we have
1786 * enabled time or size-based rotations, we have to make sure
1787 * the kernel tracer supports it.
1789 if (!cmd_ctx
->session
->has_been_started
&& \
1790 cmd_ctx
->session
->kernel_session
&& \
1791 (cmd_ctx
->session
->rotate_timer_period
|| \
1792 cmd_ctx
->session
->rotate_size
) && \
1793 !check_rotate_compatible()) {
1794 DBG("Kernel tracer version is not compatible with the rotation feature");
1795 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1798 ret
= cmd_start_trace(cmd_ctx
->session
);
1801 case LTTNG_STOP_TRACE
:
1803 ret
= cmd_stop_trace(cmd_ctx
->session
);
1806 case LTTNG_DESTROY_SESSION
:
1808 ret
= cmd_destroy_session(cmd_ctx
->session
,
1809 the_notification_thread_handle
, sock
);
1812 case LTTNG_LIST_DOMAINS
:
1815 struct lttng_domain
*domains
= NULL
;
1817 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1819 /* Return value is a negative lttng_error_code. */
1824 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1825 nb_dom
* sizeof(struct lttng_domain
));
1835 case LTTNG_LIST_CHANNELS
:
1837 enum lttng_error_code ret_code
;
1838 size_t original_payload_size
;
1839 size_t payload_size
;
1840 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1842 ret
= setup_empty_lttng_msg(cmd_ctx
);
1844 ret
= LTTNG_ERR_NOMEM
;
1848 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1850 ret_code
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1851 cmd_ctx
->session
, &cmd_ctx
->reply_payload
);
1852 if (ret_code
!= LTTNG_OK
) {
1853 ret
= (int) ret_code
;
1857 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1858 command_header_size
- original_payload_size
;
1859 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1864 case LTTNG_LIST_EVENTS
:
1866 enum lttng_error_code ret_code
;
1867 size_t original_payload_size
;
1868 size_t payload_size
;
1869 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1871 ret
= setup_empty_lttng_msg(cmd_ctx
);
1873 ret
= LTTNG_ERR_NOMEM
;
1877 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1879 ret_code
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1881 cmd_ctx
->lsm
.u
.list
.channel_name
, &cmd_ctx
->reply_payload
);
1882 if (ret_code
!= LTTNG_OK
) {
1883 ret
= (int) ret_code
;
1887 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1888 command_header_size
- original_payload_size
;
1889 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1894 case LTTNG_LIST_SESSIONS
:
1896 unsigned int nr_sessions
;
1897 lttng_session
*sessions_payload
= nullptr;
1898 size_t payload_len
= 0;
1900 session_lock_list();
1901 nr_sessions
= lttng_sessions_count(
1902 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1903 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1905 if (nr_sessions
> 0) {
1906 payload_len
= (sizeof(struct lttng_session
) *
1908 (sizeof(struct lttng_session_extended
) *
1910 sessions_payload
= zmalloc
<lttng_session
>(payload_len
);
1911 if (!sessions_payload
) {
1912 session_unlock_list();
1917 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1918 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1919 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1922 session_unlock_list();
1924 ret
= setup_lttng_msg_no_cmd_header(
1925 cmd_ctx
, sessions_payload
, payload_len
);
1926 free(sessions_payload
);
1935 case LTTNG_REGISTER_CONSUMER
:
1937 struct consumer_data
*cdata
;
1939 switch (cmd_ctx
->lsm
.domain
.type
) {
1940 case LTTNG_DOMAIN_KERNEL
:
1941 cdata
= &the_kconsumer_data
;
1944 ret
= LTTNG_ERR_UND
;
1948 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1949 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1952 case LTTNG_DATA_PENDING
:
1955 uint8_t pending_ret_byte
;
1957 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1962 * This function may returns 0 or 1 to indicate whether or not
1963 * there is data pending. In case of error, it should return an
1964 * LTTNG_ERR code. However, some code paths may still return
1965 * a nondescript error code, which we handle by returning an
1968 if (pending_ret
== 0 || pending_ret
== 1) {
1970 * ret will be set to LTTNG_OK at the end of
1973 } else if (pending_ret
< 0) {
1974 ret
= LTTNG_ERR_UNK
;
1981 pending_ret_byte
= (uint8_t) pending_ret
;
1983 /* 1 byte to return whether or not data is pending */
1984 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1985 &pending_ret_byte
, 1);
1994 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1996 uint32_t snapshot_id
;
1997 struct lttcomm_lttng_output_id reply
;
1998 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
2000 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
2003 if (ret
!= LTTNG_OK
) {
2006 reply
.id
= snapshot_id
;
2008 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
2014 /* Copy output list into message payload */
2018 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2020 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
2021 ret
= cmd_snapshot_del_output(cmd_ctx
->session
, &output
);
2024 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2027 struct lttng_snapshot_output
*outputs
= NULL
;
2029 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
2030 if (nb_output
< 0) {
2035 LTTNG_ASSERT((nb_output
> 0 && outputs
) || nb_output
== 0);
2036 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
2037 nb_output
* sizeof(struct lttng_snapshot_output
));
2047 case LTTNG_SNAPSHOT_RECORD
:
2049 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_record
.output
;
2050 ret
= cmd_snapshot_record(cmd_ctx
->session
,
2051 &output
, 0); // RFC: set to zero since it's ignored by cmd_snapshot_record
2054 case LTTNG_CREATE_SESSION_EXT
:
2056 struct lttng_dynamic_buffer payload
;
2057 struct lttng_session_descriptor
*return_descriptor
= NULL
;
2059 lttng_dynamic_buffer_init(&payload
);
2060 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2061 if (ret
!= LTTNG_OK
) {
2065 ret
= lttng_session_descriptor_serialize(return_descriptor
,
2068 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2069 lttng_session_descriptor_destroy(return_descriptor
);
2070 ret
= LTTNG_ERR_NOMEM
;
2073 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
2076 lttng_session_descriptor_destroy(return_descriptor
);
2077 ret
= LTTNG_ERR_NOMEM
;
2080 lttng_dynamic_buffer_reset(&payload
);
2081 lttng_session_descriptor_destroy(return_descriptor
);
2085 case LTTNG_SAVE_SESSION
:
2087 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
2091 case LTTNG_SET_SESSION_SHM_PATH
:
2093 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2094 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2097 case LTTNG_REGENERATE_METADATA
:
2099 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2102 case LTTNG_REGENERATE_STATEDUMP
:
2104 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2107 case LTTNG_REGISTER_TRIGGER
:
2109 struct lttng_trigger
*payload_trigger
;
2110 struct lttng_trigger
*return_trigger
;
2111 size_t original_reply_payload_size
;
2112 size_t reply_payload_size
;
2113 const struct lttng_credentials cmd_creds
= {
2114 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2115 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2118 ret
= setup_empty_lttng_msg(cmd_ctx
);
2120 ret
= LTTNG_ERR_NOMEM
;
2124 ret
= receive_lttng_trigger(
2125 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2126 if (ret
!= LTTNG_OK
) {
2130 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2132 ret
= cmd_register_trigger(&cmd_creds
, payload_trigger
,
2133 cmd_ctx
->lsm
.u
.trigger
.is_trigger_anonymous
,
2134 the_notification_thread_handle
,
2136 if (ret
!= LTTNG_OK
) {
2137 lttng_trigger_put(payload_trigger
);
2141 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2142 lttng_trigger_put(payload_trigger
);
2143 lttng_trigger_put(return_trigger
);
2145 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2146 ret
= LTTNG_ERR_NOMEM
;
2150 reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2151 original_reply_payload_size
;
2153 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2158 case LTTNG_UNREGISTER_TRIGGER
:
2160 struct lttng_trigger
*payload_trigger
;
2161 const struct lttng_credentials cmd_creds
= {
2162 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2163 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2166 ret
= receive_lttng_trigger(
2167 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2168 if (ret
!= LTTNG_OK
) {
2172 ret
= cmd_unregister_trigger(&cmd_creds
, payload_trigger
,
2173 the_notification_thread_handle
);
2174 lttng_trigger_put(payload_trigger
);
2177 case LTTNG_ROTATE_SESSION
:
2179 struct lttng_rotate_session_return rotate_return
;
2181 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2183 memset(&rotate_return
, 0, sizeof(rotate_return
));
2184 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2185 DBG("Kernel tracer version is not compatible with the rotation feature");
2186 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2190 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2192 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2198 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2199 sizeof(rotate_return
));
2208 case LTTNG_ROTATION_GET_INFO
:
2210 struct lttng_rotation_get_info_return get_info_return
;
2212 memset(&get_info_return
, 0, sizeof(get_info_return
));
2213 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2214 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2220 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2221 sizeof(get_info_return
));
2230 case LTTNG_ROTATION_SET_SCHEDULE
:
2233 enum lttng_rotation_schedule_type schedule_type
;
2236 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2237 DBG("Kernel tracer version does not support session rotations");
2238 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2242 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2243 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2244 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2246 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
, set_schedule
,
2247 schedule_type
, value
,
2248 the_notification_thread_handle
);
2249 if (ret
!= LTTNG_OK
) {
2255 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2257 lttng_session_list_schedules_return schedules
;
2259 schedules
.periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
;
2260 schedules
.periodic
.value
= cmd_ctx
->session
->rotate_timer_period
;
2261 schedules
.size
.set
= !!cmd_ctx
->session
->rotate_size
;
2262 schedules
.size
.value
= cmd_ctx
->session
->rotate_size
;
2264 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2274 case LTTNG_CLEAR_SESSION
:
2276 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2279 case LTTNG_LIST_TRIGGERS
:
2281 struct lttng_triggers
*return_triggers
= NULL
;
2282 size_t original_payload_size
;
2283 size_t payload_size
;
2285 ret
= setup_empty_lttng_msg(cmd_ctx
);
2287 ret
= LTTNG_ERR_NOMEM
;
2291 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2293 ret
= cmd_list_triggers(cmd_ctx
, the_notification_thread_handle
,
2295 if (ret
!= LTTNG_OK
) {
2299 LTTNG_ASSERT(return_triggers
);
2300 ret
= lttng_triggers_serialize(
2301 return_triggers
, &cmd_ctx
->reply_payload
);
2302 lttng_triggers_destroy(return_triggers
);
2304 ERR("Failed to serialize triggers in reply to `list triggers` command");
2305 ret
= LTTNG_ERR_NOMEM
;
2309 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2310 original_payload_size
;
2312 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2317 case LTTNG_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
= NULL
;
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(
2337 cmd_ctx
, *sock
, sock_error
, &query
);
2338 if (ret
!= LTTNG_OK
) {
2342 ret
= cmd_execute_error_query(&cmd_creds
, query
, &results
,
2343 the_notification_thread_handle
);
2344 lttng_error_query_destroy(query
);
2345 if (ret
!= LTTNG_OK
) {
2349 LTTNG_ASSERT(results
);
2350 ret
= lttng_error_query_results_serialize(
2351 results
, &cmd_ctx
->reply_payload
);
2352 lttng_error_query_results_destroy(results
);
2354 ERR("Failed to serialize error query result set in reply to `execute error query` command");
2355 ret
= LTTNG_ERR_NOMEM
;
2359 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2360 original_payload_size
;
2362 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2369 ret
= LTTNG_ERR_UND
;
2374 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2375 DBG("Missing llm header, creating one.");
2376 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2380 /* Set return code */
2381 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2383 if (cmd_ctx
->session
) {
2384 session_unlock(cmd_ctx
->session
);
2385 session_put(cmd_ctx
->session
);
2386 cmd_ctx
->session
= NULL
;
2388 if (need_tracing_session
) {
2389 session_unlock_list();
2392 LTTNG_ASSERT(!rcu_read_ongoing());
2396 static int create_client_sock(void)
2398 int ret
, client_sock
;
2399 const mode_t old_umask
= umask(0);
2401 /* Create client tool unix socket */
2402 client_sock
= lttcomm_create_unix_sock(
2403 the_config
.client_unix_sock_path
.value
);
2404 if (client_sock
< 0) {
2405 ERR("Create unix sock failed: %s",
2406 the_config
.client_unix_sock_path
.value
);
2411 /* Set the cloexec flag */
2412 ret
= utils_set_fd_cloexec(client_sock
);
2414 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2415 "Continuing but note that the consumer daemon will have a "
2416 "reference to this socket on exec()", client_sock
);
2419 /* File permission MUST be 660 */
2420 ret
= chmod(the_config
.client_unix_sock_path
.value
,
2421 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2423 ERR("Set file permissions failed: %s",
2424 the_config
.client_unix_sock_path
.value
);
2426 (void) lttcomm_close_unix_sock(client_sock
);
2430 DBG("Created client socket (fd = %i)", client_sock
);
2437 static void cleanup_client_thread(void *data
)
2439 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2441 lttng_pipe_destroy(quit_pipe
);
2444 static void thread_init_cleanup(void *data
__attribute__((unused
)))
2446 set_thread_status(false);
2450 * This thread manage all clients request using the unix client socket for
2453 static void *thread_manage_clients(void *data
)
2455 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2457 uint32_t revents
, nb_fd
;
2458 struct lttng_poll_event events
;
2459 const int client_sock
= thread_state
.client_sock
;
2460 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2461 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2462 struct command_ctx cmd_ctx
= {};
2464 DBG("[thread] Manage client started");
2466 lttng_payload_init(&cmd_ctx
.reply_payload
);
2468 is_root
= (getuid() == 0);
2470 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2472 rcu_register_thread();
2474 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2476 health_code_update();
2478 ret
= lttcomm_listen_unix_sock(client_sock
);
2484 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2485 * more will be added to this poll set.
2487 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2489 goto error_create_poll
;
2492 /* Add the application registration socket */
2493 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2498 /* Add thread quit pipe */
2499 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2504 /* Set state as running. */
2505 set_thread_status(true);
2506 pthread_cleanup_pop(0);
2508 /* This testpoint is after we signal readiness to the parent. */
2509 if (testpoint(sessiond_thread_manage_clients
)) {
2513 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2517 health_code_update();
2520 const struct cmd_completion_handler
*cmd_completion_handler
;
2522 cmd_ctx
.creds
.uid
= UINT32_MAX
;
2523 cmd_ctx
.creds
.gid
= UINT32_MAX
;
2524 cmd_ctx
.creds
.pid
= 0;
2525 cmd_ctx
.session
= NULL
;
2526 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2527 cmd_ctx
.lttng_msg_size
= 0;
2529 DBG("Accepting client command ...");
2531 /* Inifinite blocking call, waiting for transmission */
2533 health_poll_entry();
2534 ret
= lttng_poll_wait(&events
, -1);
2538 * Restart interrupted system call.
2540 if (errno
== EINTR
) {
2548 for (i
= 0; i
< nb_fd
; i
++) {
2549 revents
= LTTNG_POLL_GETEV(&events
, i
);
2550 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2552 health_code_update();
2554 if (pollfd
== thread_quit_pipe_fd
) {
2558 /* Event on the registration socket */
2559 if (revents
& LPOLLIN
) {
2561 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2562 ERR("Client socket poll error");
2565 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2571 DBG("Wait for client response");
2573 health_code_update();
2575 sock
= lttcomm_accept_unix_sock(client_sock
);
2581 * Set the CLOEXEC flag. Return code is useless because either way, the
2584 (void) utils_set_fd_cloexec(sock
);
2586 /* Set socket option for credentials retrieval */
2587 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2592 health_code_update();
2595 * Data is received from the lttng client. The struct
2596 * lttcomm_session_msg (lsm) contains the command and data request of
2599 DBG("Receiving data from client ...");
2600 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2601 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2602 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2603 DBG("Incomplete recv() from client... continuing");
2612 health_code_update();
2614 // TODO: Validate cmd_ctx including sanity check for
2615 // security purpose.
2617 rcu_thread_online();
2619 * This function dispatch the work to the kernel or userspace tracer
2620 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2621 * informations for the client. The command context struct contains
2622 * everything this function may needs.
2624 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2625 rcu_thread_offline();
2635 * TODO: Inform client somehow of the fatal error. At
2636 * this point, ret < 0 means that a zmalloc failed
2637 * (ENOMEM). Error detected but still accept
2638 * command, unless a socket error has been
2644 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2645 WARN("Command returned an invalid status code, returning unknown error: "
2646 "command type = %s (%d), ret = %d",
2647 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) cmd_ctx
.lsm
.cmd_type
),
2648 cmd_ctx
.lsm
.cmd_type
, ret
);
2649 ret
= LTTNG_ERR_UNK
;
2652 cmd_completion_handler
= cmd_pop_completion_handler();
2653 if (cmd_completion_handler
) {
2654 enum lttng_error_code completion_code
;
2656 completion_code
= cmd_completion_handler
->run(
2657 cmd_completion_handler
->data
);
2658 if (completion_code
!= LTTNG_OK
) {
2663 health_code_update();
2666 struct lttng_payload_view view
=
2667 lttng_payload_view_from_payload(
2668 &cmd_ctx
.reply_payload
,
2670 struct lttcomm_lttng_msg
*llm
= (typeof(
2671 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2673 LTTNG_ASSERT(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2674 LTTNG_ASSERT(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2676 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2678 DBG("Sending response (size: %d, retcode: %s (%d))",
2679 cmd_ctx
.lttng_msg_size
,
2680 lttng_strerror(-llm
->ret_code
),
2682 ret
= send_unix_sock(sock
, &view
);
2684 ERR("Failed to send data back to client");
2687 /* End of transmission */
2695 health_code_update();
2707 lttng_poll_clean(&events
);
2711 unlink(the_config
.client_unix_sock_path
.value
);
2712 ret
= close(client_sock
);
2719 ERR("Health error occurred in %s", __func__
);
2722 health_unregister(the_health_sessiond
);
2724 DBG("Client thread dying");
2725 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2726 rcu_unregister_thread();
2731 bool shutdown_client_thread(void *thread_data
)
2733 struct lttng_pipe
*client_quit_pipe
= (lttng_pipe
*) thread_data
;
2734 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2736 return notify_thread_pipe(write_fd
) == 1;
2739 struct lttng_thread
*launch_client_thread(void)
2741 bool thread_running
;
2742 struct lttng_pipe
*client_quit_pipe
;
2743 struct lttng_thread
*thread
= NULL
;
2744 int client_sock_fd
= -1;
2746 sem_init(&thread_state
.ready
, 0, 0);
2747 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2748 if (!client_quit_pipe
) {
2752 client_sock_fd
= create_client_sock();
2753 if (client_sock_fd
< 0) {
2757 thread_state
.client_sock
= client_sock_fd
;
2758 thread
= lttng_thread_create("Client management",
2759 thread_manage_clients
,
2760 shutdown_client_thread
,
2761 cleanup_client_thread
,
2766 /* The client thread now owns the client sock fd and the quit pipe. */
2767 client_sock_fd
= -1;
2768 client_quit_pipe
= NULL
;
2771 * This thread is part of the threads that need to be fully
2772 * initialized before the session daemon is marked as "ready".
2774 thread_running
= wait_thread_status();
2775 if (!thread_running
) {
2780 if (client_sock_fd
>= 0) {
2781 if (close(client_sock_fd
)) {
2782 PERROR("Failed to close client socket");
2785 lttng_thread_put(thread
);
2786 cleanup_client_thread(client_quit_pipe
);