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
) {
538 lus
->trace_format
= session
->trace_format
;
544 session
->ust_session
= NULL
;
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
= NULL
;
587 static enum lttng_error_code
receive_lttng_trigger(struct command_ctx
*cmd_ctx
,
590 struct lttng_trigger
**_trigger
)
594 ssize_t sock_recv_len
;
595 enum lttng_error_code ret_code
;
596 struct lttng_payload trigger_payload
;
597 struct lttng_trigger
*trigger
= NULL
;
599 lttng_payload_init(&trigger_payload
);
600 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
601 ret
= lttng_dynamic_buffer_set_size(
602 &trigger_payload
.buffer
, trigger_len
);
604 ret_code
= LTTNG_ERR_NOMEM
;
608 sock_recv_len
= lttcomm_recv_unix_sock(
609 sock
, trigger_payload
.buffer
.data
, trigger_len
);
610 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
611 ERR("Failed to receive trigger in command payload");
613 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
617 /* Receive fds, if any. */
618 if (cmd_ctx
->lsm
.fd_count
> 0) {
619 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
620 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
621 if (sock_recv_len
> 0 &&
622 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
623 ERR("Failed to receive all file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
624 cmd_ctx
->lsm
.fd_count
, (int) ret
);
625 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
628 } else if (sock_recv_len
<= 0) {
629 ERR("Failed to receive file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
630 cmd_ctx
->lsm
.fd_count
, (int) ret
);
631 ret_code
= LTTNG_ERR_FATAL
;
637 /* Deserialize trigger. */
639 struct lttng_payload_view view
=
640 lttng_payload_view_from_payload(
641 &trigger_payload
, 0, -1);
643 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
645 ERR("Invalid trigger received as part of command payload");
646 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
647 lttng_trigger_put(trigger
);
656 lttng_payload_reset(&trigger_payload
);
660 static enum lttng_error_code
receive_lttng_error_query(struct command_ctx
*cmd_ctx
,
663 struct lttng_error_query
**_query
)
667 ssize_t sock_recv_len
;
668 enum lttng_error_code ret_code
;
669 struct lttng_payload query_payload
;
670 struct lttng_error_query
*query
= NULL
;
672 lttng_payload_init(&query_payload
);
673 query_len
= (size_t) cmd_ctx
->lsm
.u
.error_query
.length
;
674 ret
= lttng_dynamic_buffer_set_size(&query_payload
.buffer
, query_len
);
676 ret_code
= LTTNG_ERR_NOMEM
;
680 sock_recv_len
= lttcomm_recv_unix_sock(
681 sock
, query_payload
.buffer
.data
, query_len
);
682 if (sock_recv_len
< 0 || sock_recv_len
!= query_len
) {
683 ERR("Failed to receive error query in command payload");
685 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
689 /* Receive fds, if any. */
690 if (cmd_ctx
->lsm
.fd_count
> 0) {
691 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
692 sock
, cmd_ctx
->lsm
.fd_count
, &query_payload
);
693 if (sock_recv_len
> 0 &&
694 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
695 ERR("Failed to receive all file descriptors for error query in command payload: expected fd count = %u, ret = %d",
696 cmd_ctx
->lsm
.fd_count
, (int) ret
);
697 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
700 } else if (sock_recv_len
<= 0) {
701 ERR("Failed to receive file descriptors for error query in command payload: expected fd count = %u, ret = %d",
702 cmd_ctx
->lsm
.fd_count
, (int) ret
);
703 ret_code
= LTTNG_ERR_FATAL
;
709 /* Deserialize error query. */
711 struct lttng_payload_view view
=
712 lttng_payload_view_from_payload(
713 &query_payload
, 0, -1);
715 if (lttng_error_query_create_from_payload(&view
, &query
) !=
717 ERR("Invalid error query received as part of command payload");
718 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
727 lttng_payload_reset(&query_payload
);
731 static enum lttng_error_code
receive_lttng_event(struct command_ctx
*cmd_ctx
,
734 struct lttng_event
**out_event
,
735 char **out_filter_expression
,
736 struct lttng_bytecode
**out_bytecode
,
737 struct lttng_event_exclusion
**out_exclusion
)
741 ssize_t sock_recv_len
;
742 enum lttng_error_code ret_code
;
743 struct lttng_payload event_payload
;
744 struct lttng_event
*local_event
= NULL
;
745 char *local_filter_expression
= NULL
;
746 struct lttng_bytecode
*local_bytecode
= NULL
;
747 struct lttng_event_exclusion
*local_exclusion
= NULL
;
749 lttng_payload_init(&event_payload
);
750 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_ENABLE_EVENT
) {
751 event_len
= (size_t) cmd_ctx
->lsm
.u
.enable
.length
;
752 } else if (cmd_ctx
->lsm
.cmd_type
== LTTNG_DISABLE_EVENT
) {
753 event_len
= (size_t) cmd_ctx
->lsm
.u
.disable
.length
;
758 ret
= lttng_dynamic_buffer_set_size(&event_payload
.buffer
, event_len
);
760 ret_code
= LTTNG_ERR_NOMEM
;
764 sock_recv_len
= lttcomm_recv_unix_sock(
765 sock
, event_payload
.buffer
.data
, event_len
);
766 if (sock_recv_len
< 0 || sock_recv_len
!= event_len
) {
767 ERR("Failed to receive event in command payload");
769 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
773 /* Receive fds, if any. */
774 if (cmd_ctx
->lsm
.fd_count
> 0) {
775 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
776 sock
, cmd_ctx
->lsm
.fd_count
, &event_payload
);
777 if (sock_recv_len
> 0 &&
778 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
779 ERR("Failed to receive all file descriptors for event in command payload: expected fd count = %u, ret = %d",
780 cmd_ctx
->lsm
.fd_count
, (int) ret
);
781 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
784 } else if (sock_recv_len
<= 0) {
785 ERR("Failed to receive file descriptors for event in command payload: expected fd count = %u, ret = %d",
786 cmd_ctx
->lsm
.fd_count
, (int) ret
);
787 ret_code
= LTTNG_ERR_FATAL
;
793 /* Deserialize event. */
796 struct lttng_payload_view event_view
=
797 lttng_payload_view_from_payload(
798 &event_payload
, 0, -1);
800 len
= lttng_event_create_from_payload(&event_view
, &local_event
,
801 &local_exclusion
, &local_filter_expression
,
805 ERR("Failed to create an event from the received buffer");
806 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
810 if (len
!= event_len
) {
811 ERR("Userspace probe location from the received buffer is not the advertised length: header length = %zu" PRIu32
", payload length = %zd", event_len
, len
);
812 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
817 *out_event
= local_event
;
818 *out_exclusion
= local_exclusion
;
819 *out_filter_expression
= local_filter_expression
;
820 *out_bytecode
= local_bytecode
;
822 local_exclusion
= NULL
;
823 local_filter_expression
= NULL
;
824 local_bytecode
= NULL
;
829 lttng_payload_reset(&event_payload
);
830 lttng_event_destroy(local_event
);
831 free(local_filter_expression
);
832 free(local_bytecode
);
833 free(local_exclusion
);
837 static enum lttng_error_code
receive_lttng_event_context(
838 const struct command_ctx
*cmd_ctx
,
841 struct lttng_event_context
**out_event_context
)
844 const size_t event_context_len
=
845 (size_t) cmd_ctx
->lsm
.u
.context
.length
;
846 ssize_t sock_recv_len
;
847 enum lttng_error_code ret_code
;
848 struct lttng_payload event_context_payload
;
849 struct lttng_event_context
*context
= NULL
;
851 lttng_payload_init(&event_context_payload
);
853 ret
= lttng_dynamic_buffer_set_size(&event_context_payload
.buffer
,
856 ret_code
= LTTNG_ERR_NOMEM
;
860 sock_recv_len
= lttcomm_recv_unix_sock(
861 sock
, event_context_payload
.buffer
.data
,
863 if (sock_recv_len
< 0 || sock_recv_len
!= event_context_len
) {
864 ERR("Failed to receive event context in command payload");
866 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
870 /* Deserialize event. */
873 struct lttng_payload_view event_context_view
=
874 lttng_payload_view_from_payload(
875 &event_context_payload
, 0, -1);
877 len
= lttng_event_context_create_from_payload(
878 &event_context_view
, &context
);
881 ERR("Failed to create a event context from the received buffer");
882 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
886 if (len
!= event_context_len
) {
887 ERR("Event context from the received buffer is not the advertised length: expected length = %zu, payload length = %zd", event_context_len
, len
);
888 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
893 *out_event_context
= context
;
898 lttng_event_context_destroy(context
);
899 lttng_payload_reset(&event_context_payload
);
904 * Version of setup_lttng_msg() without command header.
906 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
907 void *payload_buf
, size_t payload_len
)
909 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
913 * Check if the current kernel tracer supports the session rotation feature.
914 * Return 1 if it does, 0 otherwise.
916 static int check_rotate_compatible(void)
920 if (the_kernel_tracer_version
.major
!= 2 ||
921 the_kernel_tracer_version
.minor
< 11) {
922 DBG("Kernel tracer version is not compatible with the rotation feature");
930 * Send data on a unix socket using the liblttsessiondcomm API.
932 * Return lttcomm error code.
934 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
937 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
939 /* Check valid length */
940 if (view
->buffer
.size
== 0) {
945 ret
= lttcomm_send_unix_sock(
946 sock
, view
->buffer
.data
, view
->buffer
.size
);
952 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
963 * Process the command requested by the lttng client within the command
964 * context structure. This function make sure that the return structure (llm)
965 * is set and ready for transmission before returning.
967 * Return any error encountered or 0 for success.
969 * "sock" is only used for special-case var. len data.
970 * A command may assume the ownership of the socket, in which case its value
971 * should be set to -1.
973 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
977 bool need_tracing_session
= true;
981 DBG("Processing client command '%s\' (%d)",
982 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) cmd_ctx
->lsm
.cmd_type
),
983 cmd_ctx
->lsm
.cmd_type
);
987 switch (cmd_ctx
->lsm
.cmd_type
) {
988 case LTTNG_CREATE_SESSION_EXT
:
989 case LTTNG_DESTROY_SESSION
:
990 case LTTNG_LIST_SESSIONS
:
991 case LTTNG_LIST_DOMAINS
:
992 case LTTNG_START_TRACE
:
993 case LTTNG_STOP_TRACE
:
994 case LTTNG_DATA_PENDING
:
995 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
996 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
997 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
998 case LTTNG_SNAPSHOT_RECORD
:
999 case LTTNG_SAVE_SESSION
:
1000 case LTTNG_SET_SESSION_SHM_PATH
:
1001 case LTTNG_REGENERATE_METADATA
:
1002 case LTTNG_REGENERATE_STATEDUMP
:
1003 case LTTNG_ROTATE_SESSION
:
1004 case LTTNG_ROTATION_GET_INFO
:
1005 case LTTNG_ROTATION_SET_SCHEDULE
:
1006 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1007 case LTTNG_CLEAR_SESSION
:
1008 case LTTNG_LIST_TRIGGERS
:
1009 case LTTNG_EXECUTE_ERROR_QUERY
:
1010 need_domain
= false;
1016 /* Needs a functioning consumerd? */
1017 switch (cmd_ctx
->lsm
.cmd_type
) {
1018 case LTTNG_REGISTER_TRIGGER
:
1019 case LTTNG_UNREGISTER_TRIGGER
:
1020 case LTTNG_EXECUTE_ERROR_QUERY
:
1021 need_consumerd
= false;
1024 need_consumerd
= true;
1028 if (the_config
.no_kernel
&& need_domain
&&
1029 cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
1031 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1033 ret
= LTTNG_ERR_KERN_NA
;
1038 /* Deny register consumer if we already have a spawned consumer. */
1039 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
1040 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1041 if (the_kconsumer_data
.pid
> 0) {
1042 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1043 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1046 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1050 * Check for command that don't needs to allocate a returned payload. We do
1051 * this here so we don't have to make the call for no payload at each
1054 switch(cmd_ctx
->lsm
.cmd_type
) {
1055 case LTTNG_LIST_SESSIONS
:
1056 case LTTNG_LIST_TRACEPOINTS
:
1057 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1058 case LTTNG_LIST_DOMAINS
:
1059 case LTTNG_LIST_CHANNELS
:
1060 case LTTNG_LIST_EVENTS
:
1061 case LTTNG_LIST_SYSCALLS
:
1062 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1063 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1064 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1065 case LTTNG_DATA_PENDING
:
1066 case LTTNG_ROTATE_SESSION
:
1067 case LTTNG_ROTATION_GET_INFO
:
1068 case LTTNG_REGISTER_TRIGGER
:
1069 case LTTNG_LIST_TRIGGERS
:
1070 case LTTNG_EXECUTE_ERROR_QUERY
:
1073 /* Setup lttng message with no payload */
1074 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
1076 /* This label does not try to unlock the session */
1077 goto init_setup_error
;
1081 /* Commands that DO NOT need a session. */
1082 switch (cmd_ctx
->lsm
.cmd_type
) {
1083 case LTTNG_CREATE_SESSION_EXT
:
1084 case LTTNG_LIST_SESSIONS
:
1085 case LTTNG_LIST_TRACEPOINTS
:
1086 case LTTNG_LIST_SYSCALLS
:
1087 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1088 case LTTNG_SAVE_SESSION
:
1089 case LTTNG_REGISTER_TRIGGER
:
1090 case LTTNG_UNREGISTER_TRIGGER
:
1091 case LTTNG_LIST_TRIGGERS
:
1092 case LTTNG_EXECUTE_ERROR_QUERY
:
1093 need_tracing_session
= false;
1096 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
1098 * We keep the session list lock across _all_ commands
1099 * for now, because the per-session lock does not
1100 * handle teardown properly.
1102 session_lock_list();
1103 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
1104 if (cmd_ctx
->session
== NULL
) {
1105 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1108 /* Acquire lock for the session */
1109 session_lock(cmd_ctx
->session
);
1115 * Commands that need a valid session but should NOT create one if none
1116 * exists. Instead of creating one and destroying it when the command is
1117 * handled, process that right before so we save some round trip in useless
1120 switch (cmd_ctx
->lsm
.cmd_type
) {
1121 case LTTNG_DISABLE_CHANNEL
:
1122 case LTTNG_DISABLE_EVENT
:
1123 switch (cmd_ctx
->lsm
.domain
.type
) {
1124 case LTTNG_DOMAIN_KERNEL
:
1125 if (!cmd_ctx
->session
->kernel_session
) {
1126 ret
= LTTNG_ERR_NO_CHANNEL
;
1130 case LTTNG_DOMAIN_JUL
:
1131 case LTTNG_DOMAIN_LOG4J
:
1132 case LTTNG_DOMAIN_PYTHON
:
1133 case LTTNG_DOMAIN_UST
:
1134 if (!cmd_ctx
->session
->ust_session
) {
1135 ret
= LTTNG_ERR_NO_CHANNEL
;
1140 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1152 * Check domain type for specific "pre-action".
1154 switch (cmd_ctx
->lsm
.domain
.type
) {
1155 case LTTNG_DOMAIN_KERNEL
:
1157 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1161 /* Kernel tracer check */
1162 if (!kernel_tracer_is_initialized()) {
1163 /* Basically, load kernel tracer modules */
1164 ret
= init_kernel_tracer();
1170 /* Consumer is in an ERROR state. Report back to client */
1171 if (need_consumerd
&& uatomic_read(&the_kernel_consumerd_state
) ==
1173 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1177 /* Need a session for kernel command */
1178 if (need_tracing_session
) {
1179 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1180 ret
= create_kernel_session(cmd_ctx
->session
);
1181 if (ret
!= LTTNG_OK
) {
1182 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1187 /* Start the kernel consumer daemon */
1188 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1189 if (the_kconsumer_data
.pid
== 0 &&
1190 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1191 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1192 ret
= start_consumerd(&the_kconsumer_data
);
1194 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1197 uatomic_set(&the_kernel_consumerd_state
, CONSUMER_STARTED
);
1199 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1203 * The consumer was just spawned so we need to add the socket to
1204 * the consumer output of the session if exist.
1206 ret
= consumer_create_socket(&the_kconsumer_data
,
1207 cmd_ctx
->session
->kernel_session
->consumer
);
1214 case LTTNG_DOMAIN_JUL
:
1215 case LTTNG_DOMAIN_LOG4J
:
1216 case LTTNG_DOMAIN_PYTHON
:
1217 if (!agent_tracing_is_enabled()) {
1218 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1222 case LTTNG_DOMAIN_UST
:
1224 if (!ust_app_supported()) {
1225 ret
= LTTNG_ERR_NO_UST
;
1229 /* Consumer is in an ERROR state. Report back to client */
1230 if (need_consumerd
&&
1231 uatomic_read(&the_ust_consumerd_state
) ==
1233 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1237 if (need_tracing_session
) {
1238 /* Create UST session if none exist. */
1239 if (cmd_ctx
->session
->ust_session
== NULL
) {
1240 lttng_domain domain
= cmd_ctx
->lsm
.domain
;
1241 ret
= create_ust_session(cmd_ctx
->session
, &domain
);
1242 if (ret
!= LTTNG_OK
) {
1247 /* Start the UST consumer daemons */
1249 pthread_mutex_lock(&the_ustconsumer64_data
.pid_mutex
);
1250 if (the_config
.consumerd64_bin_path
.value
&&
1251 the_ustconsumer64_data
.pid
== 0 &&
1252 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1253 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1254 ret
= start_consumerd(&the_ustconsumer64_data
);
1256 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1257 uatomic_set(&the_ust_consumerd64_fd
, -EINVAL
);
1261 uatomic_set(&the_ust_consumerd64_fd
, the_ustconsumer64_data
.cmd_sock
);
1262 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1264 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1268 * Setup socket for consumer 64 bit. No need for atomic access
1269 * since it was set above and can ONLY be set in this thread.
1271 ret
= consumer_create_socket(&the_ustconsumer64_data
,
1272 cmd_ctx
->session
->ust_session
->consumer
);
1278 pthread_mutex_lock(&the_ustconsumer32_data
.pid_mutex
);
1279 if (the_config
.consumerd32_bin_path
.value
&&
1280 the_ustconsumer32_data
.pid
== 0 &&
1281 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1282 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1283 ret
= start_consumerd(&the_ustconsumer32_data
);
1285 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1286 uatomic_set(&the_ust_consumerd32_fd
, -EINVAL
);
1290 uatomic_set(&the_ust_consumerd32_fd
, the_ustconsumer32_data
.cmd_sock
);
1291 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1293 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1297 * Setup socket for consumer 32 bit. No need for atomic access
1298 * since it was set above and can ONLY be set in this thread.
1300 ret
= consumer_create_socket(&the_ustconsumer32_data
,
1301 cmd_ctx
->session
->ust_session
->consumer
);
1313 /* Validate consumer daemon state when start/stop trace command */
1314 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1315 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1316 switch (cmd_ctx
->lsm
.domain
.type
) {
1317 case LTTNG_DOMAIN_NONE
:
1319 case LTTNG_DOMAIN_JUL
:
1320 case LTTNG_DOMAIN_LOG4J
:
1321 case LTTNG_DOMAIN_PYTHON
:
1322 case LTTNG_DOMAIN_UST
:
1323 if (uatomic_read(&the_ust_consumerd_state
) != CONSUMER_STARTED
) {
1324 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1328 case LTTNG_DOMAIN_KERNEL
:
1329 if (uatomic_read(&the_kernel_consumerd_state
) != CONSUMER_STARTED
) {
1330 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1335 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1341 * Check that the UID matches that of the tracing session.
1342 * The root user can interact with all sessions.
1344 if (need_tracing_session
) {
1345 if (!session_access_ok(cmd_ctx
->session
,
1346 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1347 cmd_ctx
->session
->destroyed
) {
1348 ret
= LTTNG_ERR_EPERM
;
1354 * Send relayd information to consumer as soon as we have a domain and a
1357 if (cmd_ctx
->session
&& need_domain
) {
1359 * Setup relayd if not done yet. If the relayd information was already
1360 * sent to the consumer, this call will gracefully return.
1362 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1363 if (ret
!= LTTNG_OK
) {
1368 /* Process by command type */
1369 switch (cmd_ctx
->lsm
.cmd_type
) {
1370 case LTTNG_ADD_CONTEXT
:
1372 struct lttng_event_context
*event_context
= NULL
;
1373 const enum lttng_error_code ret_code
=
1374 receive_lttng_event_context(
1375 cmd_ctx
, *sock
, sock_error
, &event_context
);
1377 if (ret_code
!= LTTNG_OK
) {
1378 ret
= (int) ret_code
;
1382 ret
= cmd_add_context(cmd_ctx
, event_context
, the_kernel_poll_pipe
[1]);
1383 lttng_event_context_destroy(event_context
);
1386 case LTTNG_DISABLE_CHANNEL
:
1388 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1389 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1392 case LTTNG_ENABLE_CHANNEL
:
1394 ret
= cmd_enable_channel(
1395 cmd_ctx
, *sock
, the_kernel_poll_pipe
[1]);
1398 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1399 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1401 struct lttng_dynamic_buffer payload
;
1402 struct lttng_buffer_view payload_view
;
1403 const bool add_value
=
1404 cmd_ctx
->lsm
.cmd_type
==
1405 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1406 const size_t name_len
=
1407 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1409 const enum lttng_domain_type domain_type
=
1410 (enum lttng_domain_type
)
1411 cmd_ctx
->lsm
.domain
.type
;
1412 const enum lttng_process_attr process_attr
=
1413 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1414 .process_attr_tracker_add_remove_include_value
1416 const enum lttng_process_attr_value_type value_type
=
1417 (enum lttng_process_attr_value_type
) cmd_ctx
1419 .process_attr_tracker_add_remove_include_value
1421 struct process_attr_value
*value
;
1422 enum lttng_error_code ret_code
;
1423 long login_name_max
;
1425 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1426 if (login_name_max
< 0) {
1427 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1428 ret
= LTTNG_ERR_INVALID
;
1432 /* Receive remaining variable length payload if applicable. */
1433 if (name_len
> login_name_max
) {
1435 * POSIX mandates user and group names that are at least
1436 * 8 characters long. Note that although shadow-utils
1437 * (useradd, groupaadd, etc.) use 32 chars as their
1438 * limit (from bits/utmp.h, UT_NAMESIZE),
1439 * LOGIN_NAME_MAX is defined to 256.
1441 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1442 add_value
? "addition" : "removal",
1443 name_len
, login_name_max
);
1444 ret
= LTTNG_ERR_INVALID
;
1448 lttng_dynamic_buffer_init(&payload
);
1449 if (name_len
!= 0) {
1451 * Receive variable payload for user/group name
1454 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1456 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1457 add_value
? "add" : "remove");
1458 ret
= LTTNG_ERR_NOMEM
;
1459 goto error_add_remove_tracker_value
;
1462 ret
= lttcomm_recv_unix_sock(
1463 *sock
, payload
.data
, name_len
);
1465 ERR("Failed to receive payload of %s process attribute tracker value argument",
1466 add_value
? "add" : "remove");
1468 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1469 goto error_add_remove_tracker_value
;
1473 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1474 &payload
, 0, name_len
);
1475 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1476 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1477 goto error_add_remove_tracker_value
;
1481 * Validate the value type and domains are legal for the process
1482 * attribute tracker that is specified and convert the value to
1483 * add/remove to the internal sessiond representation.
1485 ret_code
= process_attr_value_from_comm(domain_type
,
1486 process_attr
, value_type
,
1487 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1489 &payload_view
, &value
);
1490 if (ret_code
!= LTTNG_OK
) {
1492 goto error_add_remove_tracker_value
;
1496 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1497 cmd_ctx
->session
, domain_type
,
1498 process_attr
, value
);
1500 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1501 cmd_ctx
->session
, domain_type
,
1502 process_attr
, value
);
1504 process_attr_value_destroy(value
);
1505 error_add_remove_tracker_value
:
1506 lttng_dynamic_buffer_reset(&payload
);
1509 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1511 enum lttng_tracking_policy tracking_policy
;
1512 const enum lttng_domain_type domain_type
=
1513 (enum lttng_domain_type
)
1514 cmd_ctx
->lsm
.domain
.type
;
1515 const enum lttng_process_attr process_attr
=
1516 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1517 .process_attr_tracker_get_tracking_policy
1520 ret
= cmd_process_attr_tracker_get_tracking_policy(
1521 cmd_ctx
->session
, domain_type
, process_attr
,
1523 if (ret
!= LTTNG_OK
) {
1527 uint32_t tracking_policy_u32
= tracking_policy
;
1528 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1529 &tracking_policy_u32
, sizeof(uint32_t));
1531 ret
= LTTNG_ERR_NOMEM
;
1537 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1539 const enum lttng_tracking_policy tracking_policy
=
1540 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1541 .process_attr_tracker_set_tracking_policy
1543 const enum lttng_domain_type domain_type
=
1544 (enum lttng_domain_type
)
1545 cmd_ctx
->lsm
.domain
.type
;
1546 const enum lttng_process_attr process_attr
=
1547 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1548 .process_attr_tracker_set_tracking_policy
1551 ret
= cmd_process_attr_tracker_set_tracking_policy(
1552 cmd_ctx
->session
, domain_type
, process_attr
,
1554 if (ret
!= LTTNG_OK
) {
1559 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1561 struct lttng_process_attr_values
*values
;
1562 struct lttng_dynamic_buffer reply
;
1563 const enum lttng_domain_type domain_type
=
1564 (enum lttng_domain_type
)
1565 cmd_ctx
->lsm
.domain
.type
;
1566 const enum lttng_process_attr process_attr
=
1567 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1568 .process_attr_tracker_get_inclusion_set
1571 ret
= cmd_process_attr_tracker_get_inclusion_set(
1572 cmd_ctx
->session
, domain_type
, process_attr
,
1574 if (ret
!= LTTNG_OK
) {
1578 lttng_dynamic_buffer_init(&reply
);
1579 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1581 goto error_tracker_get_inclusion_set
;
1584 ret
= setup_lttng_msg_no_cmd_header(
1585 cmd_ctx
, reply
.data
, reply
.size
);
1587 ret
= LTTNG_ERR_NOMEM
;
1588 goto error_tracker_get_inclusion_set
;
1592 error_tracker_get_inclusion_set
:
1593 lttng_process_attr_values_destroy(values
);
1594 lttng_dynamic_buffer_reset(&reply
);
1597 case LTTNG_ENABLE_EVENT
:
1598 case LTTNG_DISABLE_EVENT
:
1600 struct lttng_event
*event
;
1601 char *filter_expression
;
1602 struct lttng_event_exclusion
*exclusions
;
1603 struct lttng_bytecode
*bytecode
;
1604 const enum lttng_error_code ret_code
= receive_lttng_event(
1605 cmd_ctx
, *sock
, sock_error
, &event
,
1606 &filter_expression
, &bytecode
, &exclusions
);
1608 if (ret_code
!= LTTNG_OK
) {
1609 ret
= (int) ret_code
;
1614 * Ownership of filter_expression, exclusions, and bytecode is
1615 * always transferred.
1617 ret
= cmd_ctx
->lsm
.cmd_type
== LTTNG_ENABLE_EVENT
?
1618 cmd_enable_event(cmd_ctx
, event
,
1619 filter_expression
, exclusions
,
1621 the_kernel_poll_pipe
[1]) :
1622 cmd_disable_event(cmd_ctx
, event
,
1623 filter_expression
, bytecode
,
1625 lttng_event_destroy(event
);
1628 case LTTNG_LIST_TRACEPOINTS
:
1630 enum lttng_error_code ret_code
;
1631 size_t original_payload_size
;
1632 size_t payload_size
;
1633 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1635 ret
= setup_empty_lttng_msg(cmd_ctx
);
1637 ret
= LTTNG_ERR_NOMEM
;
1641 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1643 session_lock_list();
1644 ret_code
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
,
1645 &cmd_ctx
->reply_payload
);
1646 session_unlock_list();
1647 if (ret_code
!= LTTNG_OK
) {
1648 ret
= (int) ret_code
;
1652 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1653 command_header_size
- original_payload_size
;
1654 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1659 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1661 enum lttng_error_code ret_code
;
1662 size_t original_payload_size
;
1663 size_t payload_size
;
1664 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1666 ret
= setup_empty_lttng_msg(cmd_ctx
);
1668 ret
= LTTNG_ERR_NOMEM
;
1672 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1674 session_lock_list();
1675 ret_code
= cmd_list_tracepoint_fields(
1676 cmd_ctx
->lsm
.domain
.type
, &cmd_ctx
->reply_payload
);
1677 session_unlock_list();
1678 if (ret_code
!= LTTNG_OK
) {
1679 ret
= (int) ret_code
;
1683 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1684 command_header_size
- original_payload_size
;
1685 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1690 case LTTNG_LIST_SYSCALLS
:
1692 enum lttng_error_code ret_code
;
1693 size_t original_payload_size
;
1694 size_t payload_size
;
1695 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1697 ret
= setup_empty_lttng_msg(cmd_ctx
);
1699 ret
= LTTNG_ERR_NOMEM
;
1703 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1705 ret_code
= cmd_list_syscalls(&cmd_ctx
->reply_payload
);
1706 if (ret_code
!= LTTNG_OK
) {
1707 ret
= (int) ret_code
;
1711 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1712 command_header_size
- original_payload_size
;
1713 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1718 case LTTNG_SET_CONSUMER_URI
:
1721 struct lttng_uri
*uris
;
1723 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1724 len
= nb_uri
* sizeof(struct lttng_uri
);
1727 ret
= LTTNG_ERR_INVALID
;
1731 uris
= calloc
<lttng_uri
>(nb_uri
);
1733 ret
= LTTNG_ERR_FATAL
;
1737 /* Receive variable len data */
1738 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1739 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1741 DBG("No URIs received from client... continuing");
1743 ret
= LTTNG_ERR_SESSION_FAIL
;
1748 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1750 if (ret
!= LTTNG_OK
) {
1757 case LTTNG_START_TRACE
:
1760 * On the first start, if we have a kernel session and we have
1761 * enabled time or size-based rotations, we have to make sure
1762 * the kernel tracer supports it.
1764 if (!cmd_ctx
->session
->has_been_started
&& \
1765 cmd_ctx
->session
->kernel_session
&& \
1766 (cmd_ctx
->session
->rotate_timer_period
|| \
1767 cmd_ctx
->session
->rotate_size
) && \
1768 !check_rotate_compatible()) {
1769 DBG("Kernel tracer version is not compatible with the rotation feature");
1770 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1773 ret
= cmd_start_trace(cmd_ctx
->session
);
1776 case LTTNG_STOP_TRACE
:
1778 ret
= cmd_stop_trace(cmd_ctx
->session
);
1781 case LTTNG_DESTROY_SESSION
:
1783 ret
= cmd_destroy_session(cmd_ctx
->session
,
1784 the_notification_thread_handle
, sock
);
1787 case LTTNG_LIST_DOMAINS
:
1790 struct lttng_domain
*domains
= NULL
;
1792 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1794 /* Return value is a negative lttng_error_code. */
1799 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1800 nb_dom
* sizeof(struct lttng_domain
));
1810 case LTTNG_LIST_CHANNELS
:
1812 enum lttng_error_code ret_code
;
1813 size_t original_payload_size
;
1814 size_t payload_size
;
1815 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1817 ret
= setup_empty_lttng_msg(cmd_ctx
);
1819 ret
= LTTNG_ERR_NOMEM
;
1823 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1825 ret_code
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1826 cmd_ctx
->session
, &cmd_ctx
->reply_payload
);
1827 if (ret_code
!= LTTNG_OK
) {
1828 ret
= (int) ret_code
;
1832 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1833 command_header_size
- original_payload_size
;
1834 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1839 case LTTNG_LIST_EVENTS
:
1841 enum lttng_error_code ret_code
;
1842 size_t original_payload_size
;
1843 size_t payload_size
;
1844 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1846 ret
= setup_empty_lttng_msg(cmd_ctx
);
1848 ret
= LTTNG_ERR_NOMEM
;
1852 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1854 ret_code
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1856 cmd_ctx
->lsm
.u
.list
.channel_name
, &cmd_ctx
->reply_payload
);
1857 if (ret_code
!= LTTNG_OK
) {
1858 ret
= (int) ret_code
;
1862 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1863 command_header_size
- original_payload_size
;
1864 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1869 case LTTNG_LIST_SESSIONS
:
1871 enum lttng_error_code ret_code
;
1872 size_t original_payload_size
;
1873 size_t payload_size
;
1874 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1876 ret
= setup_empty_lttng_msg(cmd_ctx
);
1878 ret
= LTTNG_ERR_NOMEM
;
1882 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1884 session_lock_list();
1885 ret_code
= cmd_list_lttng_sessions(&cmd_ctx
->reply_payload
,
1886 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1887 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1888 session_unlock_list();
1889 if (ret_code
!= LTTNG_OK
) {
1890 ret
= (int) ret_code
;
1894 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
- command_header_size
-
1895 original_payload_size
;
1896 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1901 case LTTNG_REGISTER_CONSUMER
:
1903 struct consumer_data
*cdata
;
1905 switch (cmd_ctx
->lsm
.domain
.type
) {
1906 case LTTNG_DOMAIN_KERNEL
:
1907 cdata
= &the_kconsumer_data
;
1910 ret
= LTTNG_ERR_UND
;
1914 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1915 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1918 case LTTNG_DATA_PENDING
:
1921 uint8_t pending_ret_byte
;
1923 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1928 * This function may returns 0 or 1 to indicate whether or not
1929 * there is data pending. In case of error, it should return an
1930 * LTTNG_ERR code. However, some code paths may still return
1931 * a nondescript error code, which we handle by returning an
1934 if (pending_ret
== 0 || pending_ret
== 1) {
1936 * ret will be set to LTTNG_OK at the end of
1939 } else if (pending_ret
< 0) {
1940 ret
= LTTNG_ERR_UNK
;
1947 pending_ret_byte
= (uint8_t) pending_ret
;
1949 /* 1 byte to return whether or not data is pending */
1950 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1951 &pending_ret_byte
, 1);
1960 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1962 uint32_t snapshot_id
;
1963 struct lttcomm_lttng_output_id reply
;
1964 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
1966 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1969 if (ret
!= LTTNG_OK
) {
1972 reply
.id
= snapshot_id
;
1974 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1980 /* Copy output list into message payload */
1984 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1986 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
1987 ret
= cmd_snapshot_del_output(cmd_ctx
->session
, &output
);
1990 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1993 struct lttng_snapshot_output
*outputs
= NULL
;
1995 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1996 if (nb_output
< 0) {
2001 LTTNG_ASSERT((nb_output
> 0 && outputs
) || nb_output
== 0);
2002 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
2003 nb_output
* sizeof(struct lttng_snapshot_output
));
2013 case LTTNG_SNAPSHOT_RECORD
:
2015 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_record
.output
;
2016 ret
= cmd_snapshot_record(cmd_ctx
->session
,
2017 &output
, 0); // RFC: set to zero since it's ignored by cmd_snapshot_record
2020 case LTTNG_CREATE_SESSION_EXT
:
2022 struct lttng_payload payload
;
2023 struct lttng_session_descriptor
*return_descriptor
= NULL
;
2025 lttng_payload_init(&payload
);
2026 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2027 if (ret
!= LTTNG_OK
) {
2031 ret
= lttng_session_descriptor_serialize(return_descriptor
,
2034 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2035 lttng_session_descriptor_destroy(return_descriptor
);
2036 ret
= LTTNG_ERR_NOMEM
;
2039 ret
= setup_lttng_msg_no_cmd_header(
2040 cmd_ctx
, payload
.buffer
.data
, payload
.buffer
.size
);
2042 lttng_session_descriptor_destroy(return_descriptor
);
2043 ret
= LTTNG_ERR_NOMEM
;
2046 lttng_payload_reset(&payload
);
2047 lttng_session_descriptor_destroy(return_descriptor
);
2051 case LTTNG_SAVE_SESSION
:
2053 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
2057 case LTTNG_SET_SESSION_SHM_PATH
:
2059 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2060 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2063 case LTTNG_REGENERATE_METADATA
:
2065 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2068 case LTTNG_REGENERATE_STATEDUMP
:
2070 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2073 case LTTNG_REGISTER_TRIGGER
:
2075 struct lttng_trigger
*payload_trigger
;
2076 struct lttng_trigger
*return_trigger
;
2077 size_t original_reply_payload_size
;
2078 size_t reply_payload_size
;
2079 const struct lttng_credentials cmd_creds
= {
2080 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2081 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2084 ret
= setup_empty_lttng_msg(cmd_ctx
);
2086 ret
= LTTNG_ERR_NOMEM
;
2090 ret
= receive_lttng_trigger(
2091 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2092 if (ret
!= LTTNG_OK
) {
2096 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2098 ret
= cmd_register_trigger(&cmd_creds
, payload_trigger
,
2099 cmd_ctx
->lsm
.u
.trigger
.is_trigger_anonymous
,
2100 the_notification_thread_handle
,
2102 if (ret
!= LTTNG_OK
) {
2103 lttng_trigger_put(payload_trigger
);
2107 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2108 lttng_trigger_put(payload_trigger
);
2109 lttng_trigger_put(return_trigger
);
2111 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2112 ret
= LTTNG_ERR_NOMEM
;
2116 reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2117 original_reply_payload_size
;
2119 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2124 case LTTNG_UNREGISTER_TRIGGER
:
2126 struct lttng_trigger
*payload_trigger
;
2127 const struct lttng_credentials cmd_creds
= {
2128 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2129 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2132 ret
= receive_lttng_trigger(
2133 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2134 if (ret
!= LTTNG_OK
) {
2138 ret
= cmd_unregister_trigger(&cmd_creds
, payload_trigger
,
2139 the_notification_thread_handle
);
2140 lttng_trigger_put(payload_trigger
);
2143 case LTTNG_ROTATE_SESSION
:
2145 struct lttng_rotate_session_return rotate_return
;
2147 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2149 memset(&rotate_return
, 0, sizeof(rotate_return
));
2150 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2151 DBG("Kernel tracer version is not compatible with the rotation feature");
2152 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2156 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2158 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2164 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2165 sizeof(rotate_return
));
2174 case LTTNG_ROTATION_GET_INFO
:
2176 struct lttng_rotation_get_info_return get_info_return
;
2178 memset(&get_info_return
, 0, sizeof(get_info_return
));
2179 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2180 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2186 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2187 sizeof(get_info_return
));
2196 case LTTNG_ROTATION_SET_SCHEDULE
:
2199 enum lttng_rotation_schedule_type schedule_type
;
2202 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2203 DBG("Kernel tracer version does not support session rotations");
2204 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2208 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2209 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2210 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2212 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
, set_schedule
,
2213 schedule_type
, value
,
2214 the_notification_thread_handle
);
2215 if (ret
!= LTTNG_OK
) {
2221 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2223 lttng_session_list_schedules_return schedules
;
2225 schedules
.periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
;
2226 schedules
.periodic
.value
= cmd_ctx
->session
->rotate_timer_period
;
2227 schedules
.size
.set
= !!cmd_ctx
->session
->rotate_size
;
2228 schedules
.size
.value
= cmd_ctx
->session
->rotate_size
;
2230 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2240 case LTTNG_CLEAR_SESSION
:
2242 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2245 case LTTNG_LIST_TRIGGERS
:
2247 struct lttng_triggers
*return_triggers
= NULL
;
2248 size_t original_payload_size
;
2249 size_t payload_size
;
2251 ret
= setup_empty_lttng_msg(cmd_ctx
);
2253 ret
= LTTNG_ERR_NOMEM
;
2257 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2259 ret
= cmd_list_triggers(cmd_ctx
, the_notification_thread_handle
,
2261 if (ret
!= LTTNG_OK
) {
2265 LTTNG_ASSERT(return_triggers
);
2266 ret
= lttng_triggers_serialize(
2267 return_triggers
, &cmd_ctx
->reply_payload
);
2268 lttng_triggers_destroy(return_triggers
);
2270 ERR("Failed to serialize triggers in reply to `list triggers` command");
2271 ret
= LTTNG_ERR_NOMEM
;
2275 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2276 original_payload_size
;
2278 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2283 case LTTNG_EXECUTE_ERROR_QUERY
:
2285 struct lttng_error_query
*query
;
2286 const struct lttng_credentials cmd_creds
= {
2287 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2288 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2290 struct lttng_error_query_results
*results
= NULL
;
2291 size_t original_payload_size
;
2292 size_t payload_size
;
2294 ret
= setup_empty_lttng_msg(cmd_ctx
);
2296 ret
= LTTNG_ERR_NOMEM
;
2300 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2302 ret
= receive_lttng_error_query(
2303 cmd_ctx
, *sock
, sock_error
, &query
);
2304 if (ret
!= LTTNG_OK
) {
2308 ret
= cmd_execute_error_query(&cmd_creds
, query
, &results
,
2309 the_notification_thread_handle
);
2310 lttng_error_query_destroy(query
);
2311 if (ret
!= LTTNG_OK
) {
2315 LTTNG_ASSERT(results
);
2316 ret
= lttng_error_query_results_serialize(
2317 results
, &cmd_ctx
->reply_payload
);
2318 lttng_error_query_results_destroy(results
);
2320 ERR("Failed to serialize error query result set in reply to `execute error query` command");
2321 ret
= LTTNG_ERR_NOMEM
;
2325 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2326 original_payload_size
;
2328 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2335 ret
= LTTNG_ERR_UND
;
2340 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2341 DBG("Missing llm header, creating one.");
2342 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2346 /* Set return code */
2347 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2349 if (cmd_ctx
->session
) {
2350 session_unlock(cmd_ctx
->session
);
2351 session_put(cmd_ctx
->session
);
2352 cmd_ctx
->session
= NULL
;
2354 if (need_tracing_session
) {
2355 session_unlock_list();
2358 LTTNG_ASSERT(!rcu_read_ongoing());
2362 static int create_client_sock(void)
2364 int ret
, client_sock
;
2365 const mode_t old_umask
= umask(0);
2367 /* Create client tool unix socket */
2368 client_sock
= lttcomm_create_unix_sock(
2369 the_config
.client_unix_sock_path
.value
);
2370 if (client_sock
< 0) {
2371 ERR("Create unix sock failed: %s",
2372 the_config
.client_unix_sock_path
.value
);
2377 /* Set the cloexec flag */
2378 ret
= utils_set_fd_cloexec(client_sock
);
2380 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2381 "Continuing but note that the consumer daemon will have a "
2382 "reference to this socket on exec()", client_sock
);
2385 /* File permission MUST be 660 */
2386 ret
= chmod(the_config
.client_unix_sock_path
.value
,
2387 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2389 ERR("Set file permissions failed: %s",
2390 the_config
.client_unix_sock_path
.value
);
2392 (void) lttcomm_close_unix_sock(client_sock
);
2396 DBG("Created client socket (fd = %i)", client_sock
);
2403 static void cleanup_client_thread(void *data
)
2405 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2407 lttng_pipe_destroy(quit_pipe
);
2410 static void thread_init_cleanup(void *data
__attribute__((unused
)))
2412 set_thread_status(false);
2416 * This thread manage all clients request using the unix client socket for
2419 static void *thread_manage_clients(void *data
)
2421 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2423 uint32_t revents
, nb_fd
;
2424 struct lttng_poll_event events
;
2425 const int client_sock
= thread_state
.client_sock
;
2426 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2427 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2428 struct command_ctx cmd_ctx
= {};
2430 DBG("[thread] Manage client started");
2432 lttng_payload_init(&cmd_ctx
.reply_payload
);
2434 is_root
= (getuid() == 0);
2436 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2438 rcu_register_thread();
2440 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2442 health_code_update();
2444 ret
= lttcomm_listen_unix_sock(client_sock
);
2450 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2451 * more will be added to this poll set.
2453 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2455 goto error_create_poll
;
2458 /* Add the application registration socket */
2459 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2464 /* Add thread quit pipe */
2465 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2470 /* Set state as running. */
2471 set_thread_status(true);
2472 pthread_cleanup_pop(0);
2474 /* This testpoint is after we signal readiness to the parent. */
2475 if (testpoint(sessiond_thread_manage_clients
)) {
2479 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2483 health_code_update();
2486 const struct cmd_completion_handler
*cmd_completion_handler
;
2488 cmd_ctx
.creds
.uid
= UINT32_MAX
;
2489 cmd_ctx
.creds
.gid
= UINT32_MAX
;
2490 cmd_ctx
.creds
.pid
= 0;
2491 cmd_ctx
.session
= NULL
;
2492 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2493 cmd_ctx
.lttng_msg_size
= 0;
2495 DBG("Accepting client command ...");
2497 /* Inifinite blocking call, waiting for transmission */
2499 health_poll_entry();
2500 ret
= lttng_poll_wait(&events
, -1);
2504 * Restart interrupted system call.
2506 if (errno
== EINTR
) {
2514 for (i
= 0; i
< nb_fd
; i
++) {
2515 revents
= LTTNG_POLL_GETEV(&events
, i
);
2516 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2518 health_code_update();
2520 if (pollfd
== thread_quit_pipe_fd
) {
2524 /* Event on the registration socket */
2525 if (revents
& LPOLLIN
) {
2527 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2528 ERR("Client socket poll error");
2531 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2537 DBG("Wait for client response");
2539 health_code_update();
2541 sock
= lttcomm_accept_unix_sock(client_sock
);
2547 * Set the CLOEXEC flag. Return code is useless because either way, the
2550 (void) utils_set_fd_cloexec(sock
);
2552 /* Set socket option for credentials retrieval */
2553 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2558 health_code_update();
2561 * Data is received from the lttng client. The struct
2562 * lttcomm_session_msg (lsm) contains the command and data request of
2565 DBG("Receiving data from client ...");
2566 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2567 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2568 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2569 DBG("Incomplete recv() from client... continuing");
2578 health_code_update();
2580 // TODO: Validate cmd_ctx including sanity check for
2581 // security purpose.
2583 rcu_thread_online();
2585 * This function dispatch the work to the kernel or userspace tracer
2586 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2587 * informations for the client. The command context struct contains
2588 * everything this function may needs.
2590 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2591 rcu_thread_offline();
2601 * TODO: Inform client somehow of the fatal error. At
2602 * this point, ret < 0 means that a zmalloc failed
2603 * (ENOMEM). Error detected but still accept
2604 * command, unless a socket error has been
2610 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2611 WARN("Command returned an invalid status code, returning unknown error: "
2612 "command type = %s (%d), ret = %d",
2613 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) cmd_ctx
.lsm
.cmd_type
),
2614 cmd_ctx
.lsm
.cmd_type
, ret
);
2615 ret
= LTTNG_ERR_UNK
;
2618 cmd_completion_handler
= cmd_pop_completion_handler();
2619 if (cmd_completion_handler
) {
2620 enum lttng_error_code completion_code
;
2622 completion_code
= cmd_completion_handler
->run(
2623 cmd_completion_handler
->data
);
2624 if (completion_code
!= LTTNG_OK
) {
2629 health_code_update();
2632 struct lttng_payload_view view
=
2633 lttng_payload_view_from_payload(
2634 &cmd_ctx
.reply_payload
,
2636 struct lttcomm_lttng_msg
*llm
= (typeof(
2637 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2639 LTTNG_ASSERT(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2640 LTTNG_ASSERT(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2642 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2644 DBG("Sending response (size: %d, retcode: %s (%d))",
2645 cmd_ctx
.lttng_msg_size
,
2646 lttng_strerror(-llm
->ret_code
),
2648 ret
= send_unix_sock(sock
, &view
);
2650 ERR("Failed to send data back to client");
2653 /* End of transmission */
2661 health_code_update();
2673 lttng_poll_clean(&events
);
2677 unlink(the_config
.client_unix_sock_path
.value
);
2678 ret
= close(client_sock
);
2685 ERR("Health error occurred in %s", __func__
);
2688 health_unregister(the_health_sessiond
);
2690 DBG("Client thread dying");
2691 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2692 rcu_unregister_thread();
2697 bool shutdown_client_thread(void *thread_data
)
2699 struct lttng_pipe
*client_quit_pipe
= (lttng_pipe
*) thread_data
;
2700 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2702 return notify_thread_pipe(write_fd
) == 1;
2705 struct lttng_thread
*launch_client_thread(void)
2707 bool thread_running
;
2708 struct lttng_pipe
*client_quit_pipe
;
2709 struct lttng_thread
*thread
= NULL
;
2710 int client_sock_fd
= -1;
2712 sem_init(&thread_state
.ready
, 0, 0);
2713 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2714 if (!client_quit_pipe
) {
2718 client_sock_fd
= create_client_sock();
2719 if (client_sock_fd
< 0) {
2723 thread_state
.client_sock
= client_sock_fd
;
2724 thread
= lttng_thread_create("Client management",
2725 thread_manage_clients
,
2726 shutdown_client_thread
,
2727 cleanup_client_thread
,
2732 /* The client thread now owns the client sock fd and the quit pipe. */
2733 client_sock_fd
= -1;
2734 client_quit_pipe
= NULL
;
2737 * This thread is part of the threads that need to be fully
2738 * initialized before the session daemon is marked as "ready".
2740 thread_running
= wait_thread_status();
2741 if (!thread_running
) {
2746 if (client_sock_fd
>= 0) {
2747 if (close(client_sock_fd
)) {
2748 PERROR("Failed to close client socket");
2751 lttng_thread_put(thread
);
2752 cleanup_client_thread(client_quit_pipe
);