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.h"
11 #include "common/compat/socket.h"
12 #include "common/dynamic-array.h"
13 #include "common/dynamic-buffer.h"
14 #include "common/fd-handle.h"
15 #include "common/payload-view.h"
16 #include "common/payload.h"
17 #include "common/sessiond-comm/sessiond-comm.h"
18 #include "lttng/lttng-error.h"
19 #include "lttng/tracker.h"
20 #include <common/compat/getenv.h>
21 #include <common/tracker.h>
22 #include <common/unix.h>
23 #include <common/utils.h>
24 #include <lttng/error-query-internal.h>
25 #include <lttng/event-internal.h>
26 #include <lttng/session-descriptor-internal.h>
27 #include <lttng/session-internal.h>
28 #include <lttng/userspace-probe-internal.h>
36 #include "agent-thread.h"
40 #include "health-sessiond.h"
42 #include "lttng-sessiond.h"
43 #include "manage-consumer.h"
45 #include "testpoint.h"
50 static struct thread_state
{
56 static void set_thread_status(bool running
)
58 DBG("Marking client thread's state as %s", running
? "running" : "error");
59 thread_state
.running
= running
;
60 sem_post(&thread_state
.ready
);
63 static bool wait_thread_status(void)
65 DBG("Waiting for client thread to be ready");
66 sem_wait(&thread_state
.ready
);
67 if (thread_state
.running
) {
68 DBG("Client thread is ready");
70 ERR("Initialization of client thread failed");
73 return thread_state
.running
;
77 * Setup the outgoing data buffer for the response (llm) by allocating the
78 * right amount of memory and copying the original information from the lsm
81 * Return 0 on success, negative value on error.
83 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
84 const void *payload_buf
, size_t payload_len
,
85 const void *cmd_header_buf
, size_t cmd_header_len
)
88 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
89 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
90 lttcomm_lttng_msg llm
{};
92 llm
.cmd_type
= cmd_ctx
->lsm
.cmd_type
;
93 llm
.pid
= (uint32_t) cmd_ctx
->lsm
.domain
.attr
.pid
;
94 llm
.cmd_header_size
= (uint32_t) cmd_header_len
;
95 llm
.data_size
= (uint32_t) payload_len
;
97 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
102 lttng_dynamic_pointer_array_clear(&cmd_ctx
->reply_payload
._fd_handles
);
104 cmd_ctx
->lttng_msg_size
= total_msg_size
;
106 /* Append reply header. */
107 ret
= lttng_dynamic_buffer_append(
108 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
113 /* Append command header. */
114 if (cmd_header_len
) {
115 ret
= lttng_dynamic_buffer_append(
116 &cmd_ctx
->reply_payload
.buffer
, cmd_header_buf
,
123 /* Append payload. */
125 ret
= lttng_dynamic_buffer_append(
126 &cmd_ctx
->reply_payload
.buffer
, payload_buf
,
137 static int setup_empty_lttng_msg(struct command_ctx
*cmd_ctx
)
140 const struct lttcomm_lttng_msg llm
= {};
142 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
147 /* Append place-holder reply header. */
148 ret
= lttng_dynamic_buffer_append(
149 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
154 cmd_ctx
->lttng_msg_size
= sizeof(llm
);
159 static void update_lttng_msg(struct command_ctx
*cmd_ctx
, size_t cmd_header_len
,
162 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
163 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
164 struct lttcomm_lttng_msg
*p_llm
;
165 lttcomm_lttng_msg llm
{};
167 llm
.cmd_type
= cmd_ctx
->lsm
.cmd_type
;
168 llm
.pid
= (uint32_t) cmd_ctx
->lsm
.domain
.attr
.pid
;
169 llm
.cmd_header_size
= (uint32_t) cmd_header_len
;
170 llm
.data_size
= (uint32_t) payload_len
;
172 LTTNG_ASSERT(cmd_ctx
->reply_payload
.buffer
.size
>= sizeof(llm
));
174 p_llm
= (typeof(p_llm
)) cmd_ctx
->reply_payload
.buffer
.data
;
176 /* Update existing header. */
177 memcpy(p_llm
, &llm
, sizeof(llm
));
179 cmd_ctx
->lttng_msg_size
= total_msg_size
;
183 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
184 * exec or it will fail.
186 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
188 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
192 * Fork and exec a consumer daemon (consumerd).
194 * Return pid if successful else -1.
196 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
200 const char *consumer_to_use
;
201 const char *verbosity
;
204 DBG("Spawning consumerd");
211 if (the_config
.verbose_consumer
) {
212 verbosity
= "--verbose";
213 } else if (lttng_opt_quiet
) {
214 verbosity
= "--quiet";
219 switch (consumer_data
->type
) {
220 case LTTNG_CONSUMER_KERNEL
:
222 * Find out which consumerd to execute. We will first try the
223 * 64-bit path, then the sessiond's installation directory, and
224 * fallback on the 32-bit one,
226 DBG3("Looking for a kernel consumer at these locations:");
227 DBG3(" 1) %s", the_config
.consumerd64_bin_path
.value
? : "NULL");
228 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
229 DBG3(" 3) %s", the_config
.consumerd32_bin_path
.value
? : "NULL");
230 if (stat(the_config
.consumerd64_bin_path
.value
, &st
) == 0) {
231 DBG3("Found location #1");
232 consumer_to_use
= the_config
.consumerd64_bin_path
.value
;
233 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
234 DBG3("Found location #2");
235 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
236 } else if (the_config
.consumerd32_bin_path
.value
&&
237 stat(the_config
.consumerd32_bin_path
.value
, &st
) == 0) {
238 DBG3("Found location #3");
239 consumer_to_use
= the_config
.consumerd32_bin_path
.value
;
241 DBG("Could not find any valid consumerd executable");
245 DBG("Using kernel consumer at: %s", consumer_to_use
);
246 (void) execl(consumer_to_use
, "lttng-consumerd",
247 verbosity
, "-k", "--consumerd-cmd-sock",
248 consumer_data
->cmd_unix_sock_path
,
249 "--consumerd-err-sock",
250 consumer_data
->err_unix_sock_path
,
252 the_config
.tracing_group_name
.value
,
255 case LTTNG_CONSUMER64_UST
:
257 if (the_config
.consumerd64_lib_dir
.value
) {
262 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
266 tmplen
= strlen(the_config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
267 tmpnew
= (char *) zmalloc(tmplen
+ 1 /* \0 */);
272 strcat(tmpnew
, the_config
.consumerd64_lib_dir
.value
);
273 if (tmp
[0] != '\0') {
277 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
284 DBG("Using 64-bit UST consumer at: %s",
285 the_config
.consumerd64_bin_path
.value
);
286 (void) execl(the_config
.consumerd64_bin_path
.value
,
287 "lttng-consumerd", verbosity
, "-u",
288 "--consumerd-cmd-sock",
289 consumer_data
->cmd_unix_sock_path
,
290 "--consumerd-err-sock",
291 consumer_data
->err_unix_sock_path
,
293 the_config
.tracing_group_name
.value
,
297 case LTTNG_CONSUMER32_UST
:
299 if (the_config
.consumerd32_lib_dir
.value
) {
304 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
308 tmplen
= strlen(the_config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
309 tmpnew
= (char *) zmalloc(tmplen
+ 1 /* \0 */);
314 strcat(tmpnew
, the_config
.consumerd32_lib_dir
.value
);
315 if (tmp
[0] != '\0') {
319 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
326 DBG("Using 32-bit UST consumer at: %s",
327 the_config
.consumerd32_bin_path
.value
);
328 (void) execl(the_config
.consumerd32_bin_path
.value
,
329 "lttng-consumerd", verbosity
, "-u",
330 "--consumerd-cmd-sock",
331 consumer_data
->cmd_unix_sock_path
,
332 "--consumerd-err-sock",
333 consumer_data
->err_unix_sock_path
,
335 the_config
.tracing_group_name
.value
,
340 ERR("unknown consumer type");
344 PERROR("Consumer execl()");
346 /* Reaching this point, we got a failure on our execl(). */
348 } else if (pid
> 0) {
351 PERROR("start consumer fork");
359 * Spawn the consumerd daemon and session daemon thread.
361 static int start_consumerd(struct consumer_data
*consumer_data
)
366 * Set the listen() state on the socket since there is a possible race
367 * between the exec() of the consumer daemon and this call if place in the
368 * consumer thread. See bug #366 for more details.
370 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
375 pthread_mutex_lock(&consumer_data
->pid_mutex
);
376 if (consumer_data
->pid
!= 0) {
377 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
381 ret
= spawn_consumerd(consumer_data
);
383 ERR("Spawning consumerd failed");
384 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
388 /* Setting up the consumer_data pid */
389 consumer_data
->pid
= ret
;
390 DBG2("Consumer pid %d", consumer_data
->pid
);
391 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
393 DBG2("Spawning consumer control thread");
394 ret
= spawn_consumer_thread(consumer_data
);
396 ERR("Fatal error spawning consumer control thread");
404 /* Cleanup already created sockets on error. */
405 if (consumer_data
->err_sock
>= 0) {
408 err
= close(consumer_data
->err_sock
);
410 PERROR("close consumer data error socket");
417 * Copy consumer output from the tracing session to the domain session. The
418 * function also applies the right modification on a per domain basis for the
419 * trace files destination directory.
421 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
424 const char *dir_name
;
425 struct consumer_output
*consumer
;
427 LTTNG_ASSERT(session
);
428 LTTNG_ASSERT(session
->consumer
);
431 case LTTNG_DOMAIN_KERNEL
:
432 DBG3("Copying tracing session consumer output in kernel session");
434 * XXX: We should audit the session creation and what this function
435 * does "extra" in order to avoid a destroy since this function is used
436 * in the domain session creation (kernel and ust) only. Same for UST
439 if (session
->kernel_session
->consumer
) {
440 consumer_output_put(session
->kernel_session
->consumer
);
442 session
->kernel_session
->consumer
=
443 consumer_copy_output(session
->consumer
);
444 /* Ease our life a bit for the next part */
445 consumer
= session
->kernel_session
->consumer
;
446 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
448 case LTTNG_DOMAIN_JUL
:
449 case LTTNG_DOMAIN_LOG4J
:
450 case LTTNG_DOMAIN_PYTHON
:
451 case LTTNG_DOMAIN_UST
:
452 DBG3("Copying tracing session consumer output in UST session");
453 if (session
->ust_session
->consumer
) {
454 consumer_output_put(session
->ust_session
->consumer
);
456 session
->ust_session
->consumer
=
457 consumer_copy_output(session
->consumer
);
458 /* Ease our life a bit for the next part */
459 consumer
= session
->ust_session
->consumer
;
460 dir_name
= DEFAULT_UST_TRACE_DIR
;
463 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
467 /* Append correct directory to subdir */
468 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
469 sizeof(consumer
->domain_subdir
));
474 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
482 * Create an UST session and add it to the session ust list.
484 static int create_ust_session(struct ltt_session
*session
,
485 const struct lttng_domain
*domain
)
488 struct ltt_ust_session
*lus
= NULL
;
490 LTTNG_ASSERT(session
);
491 LTTNG_ASSERT(domain
);
492 LTTNG_ASSERT(session
->consumer
);
494 switch (domain
->type
) {
495 case LTTNG_DOMAIN_JUL
:
496 case LTTNG_DOMAIN_LOG4J
:
497 case LTTNG_DOMAIN_PYTHON
:
498 case LTTNG_DOMAIN_UST
:
501 ERR("Unknown UST domain on create session %d", domain
->type
);
502 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
506 DBG("Creating UST session");
508 lus
= trace_ust_create_session(session
->id
);
510 ret
= LTTNG_ERR_UST_SESS_FAIL
;
514 lus
->uid
= session
->uid
;
515 lus
->gid
= session
->gid
;
516 lus
->output_traces
= session
->output_traces
;
517 lus
->snapshot_mode
= session
->snapshot_mode
;
518 lus
->live_timer_interval
= session
->live_timer
;
519 session
->ust_session
= lus
;
520 if (session
->shm_path
[0]) {
521 strncpy(lus
->root_shm_path
, session
->shm_path
,
522 sizeof(lus
->root_shm_path
));
523 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
524 strncpy(lus
->shm_path
, session
->shm_path
,
525 sizeof(lus
->shm_path
));
526 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
527 strncat(lus
->shm_path
, "/ust",
528 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
530 /* Copy session output to the newly created UST session */
531 ret
= copy_session_consumer(domain
->type
, session
);
532 if (ret
!= LTTNG_OK
) {
540 session
->ust_session
= NULL
;
545 * Create a kernel tracer session then create the default channel.
547 static int create_kernel_session(struct ltt_session
*session
)
551 DBG("Creating kernel session");
553 ret
= kernel_create_session(session
);
555 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
559 /* Code flow safety */
560 LTTNG_ASSERT(session
->kernel_session
);
562 /* Copy session output to the newly created Kernel session */
563 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
564 if (ret
!= LTTNG_OK
) {
568 session
->kernel_session
->uid
= session
->uid
;
569 session
->kernel_session
->gid
= session
->gid
;
570 session
->kernel_session
->output_traces
= session
->output_traces
;
571 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
572 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
577 trace_kernel_destroy_session(session
->kernel_session
);
578 session
->kernel_session
= NULL
;
584 * Count number of session permitted by uid/gid.
586 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
589 struct ltt_session
*session
;
590 const struct ltt_session_list
*session_list
= session_get_list();
592 DBG("Counting number of available session for UID %d", uid
);
593 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
594 if (!session_get(session
)) {
597 session_lock(session
);
598 /* Only count the sessions the user can control. */
599 if (session_access_ok(session
, uid
) &&
600 !session
->destroyed
) {
603 session_unlock(session
);
604 session_put(session
);
609 static enum lttng_error_code
receive_lttng_trigger(struct command_ctx
*cmd_ctx
,
612 struct lttng_trigger
**_trigger
)
616 ssize_t sock_recv_len
;
617 enum lttng_error_code ret_code
;
618 struct lttng_payload trigger_payload
;
619 struct lttng_trigger
*trigger
= NULL
;
621 lttng_payload_init(&trigger_payload
);
622 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
623 ret
= lttng_dynamic_buffer_set_size(
624 &trigger_payload
.buffer
, trigger_len
);
626 ret_code
= LTTNG_ERR_NOMEM
;
630 sock_recv_len
= lttcomm_recv_unix_sock(
631 sock
, trigger_payload
.buffer
.data
, trigger_len
);
632 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
633 ERR("Failed to receive trigger in command payload");
635 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
639 /* Receive fds, if any. */
640 if (cmd_ctx
->lsm
.fd_count
> 0) {
641 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
642 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
643 if (sock_recv_len
> 0 &&
644 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
645 ERR("Failed to receive all file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
646 cmd_ctx
->lsm
.fd_count
, (int) ret
);
647 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
650 } else if (sock_recv_len
<= 0) {
651 ERR("Failed to receive file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
652 cmd_ctx
->lsm
.fd_count
, (int) ret
);
653 ret_code
= LTTNG_ERR_FATAL
;
659 /* Deserialize trigger. */
661 struct lttng_payload_view view
=
662 lttng_payload_view_from_payload(
663 &trigger_payload
, 0, -1);
665 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
667 ERR("Invalid trigger received as part of command payload");
668 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
669 lttng_trigger_put(trigger
);
678 lttng_payload_reset(&trigger_payload
);
682 static enum lttng_error_code
receive_lttng_error_query(struct command_ctx
*cmd_ctx
,
685 struct lttng_error_query
**_query
)
689 ssize_t sock_recv_len
;
690 enum lttng_error_code ret_code
;
691 struct lttng_payload query_payload
;
692 struct lttng_error_query
*query
= NULL
;
694 lttng_payload_init(&query_payload
);
695 query_len
= (size_t) cmd_ctx
->lsm
.u
.error_query
.length
;
696 ret
= lttng_dynamic_buffer_set_size(&query_payload
.buffer
, query_len
);
698 ret_code
= LTTNG_ERR_NOMEM
;
702 sock_recv_len
= lttcomm_recv_unix_sock(
703 sock
, query_payload
.buffer
.data
, query_len
);
704 if (sock_recv_len
< 0 || sock_recv_len
!= query_len
) {
705 ERR("Failed to receive error query in command payload");
707 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
711 /* Receive fds, if any. */
712 if (cmd_ctx
->lsm
.fd_count
> 0) {
713 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
714 sock
, cmd_ctx
->lsm
.fd_count
, &query_payload
);
715 if (sock_recv_len
> 0 &&
716 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
717 ERR("Failed to receive all file descriptors for error query in command payload: expected fd count = %u, ret = %d",
718 cmd_ctx
->lsm
.fd_count
, (int) ret
);
719 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
722 } else if (sock_recv_len
<= 0) {
723 ERR("Failed to receive file descriptors for error query in command payload: expected fd count = %u, ret = %d",
724 cmd_ctx
->lsm
.fd_count
, (int) ret
);
725 ret_code
= LTTNG_ERR_FATAL
;
731 /* Deserialize error query. */
733 struct lttng_payload_view view
=
734 lttng_payload_view_from_payload(
735 &query_payload
, 0, -1);
737 if (lttng_error_query_create_from_payload(&view
, &query
) !=
739 ERR("Invalid error query received as part of command payload");
740 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
749 lttng_payload_reset(&query_payload
);
753 static enum lttng_error_code
receive_lttng_event(struct command_ctx
*cmd_ctx
,
756 struct lttng_event
**out_event
,
757 char **out_filter_expression
,
758 struct lttng_bytecode
**out_bytecode
,
759 struct lttng_event_exclusion
**out_exclusion
)
763 ssize_t sock_recv_len
;
764 enum lttng_error_code ret_code
;
765 struct lttng_payload event_payload
;
767 lttng_payload_init(&event_payload
);
768 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_ENABLE_EVENT
) {
769 event_len
= (size_t) cmd_ctx
->lsm
.u
.enable
.length
;
770 } else if (cmd_ctx
->lsm
.cmd_type
== LTTNG_DISABLE_EVENT
) {
771 event_len
= (size_t) cmd_ctx
->lsm
.u
.disable
.length
;
776 ret
= lttng_dynamic_buffer_set_size(&event_payload
.buffer
, event_len
);
778 ret_code
= LTTNG_ERR_NOMEM
;
782 sock_recv_len
= lttcomm_recv_unix_sock(
783 sock
, event_payload
.buffer
.data
, event_len
);
784 if (sock_recv_len
< 0 || sock_recv_len
!= event_len
) {
785 ERR("Failed to receive event in command payload");
787 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
791 /* Receive fds, if any. */
792 if (cmd_ctx
->lsm
.fd_count
> 0) {
793 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
794 sock
, cmd_ctx
->lsm
.fd_count
, &event_payload
);
795 if (sock_recv_len
> 0 &&
796 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
797 ERR("Failed to receive all file descriptors for event in command payload: expected fd count = %u, ret = %d",
798 cmd_ctx
->lsm
.fd_count
, (int) ret
);
799 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
802 } else if (sock_recv_len
<= 0) {
803 ERR("Failed to receive file descriptors for event in command payload: expected fd count = %u, ret = %d",
804 cmd_ctx
->lsm
.fd_count
, (int) ret
);
805 ret_code
= LTTNG_ERR_FATAL
;
811 /* Deserialize event. */
813 struct lttng_payload_view event_view
=
814 lttng_payload_view_from_payload(
815 &event_payload
, 0, -1);
817 if (lttng_event_create_from_payload(&event_view
, out_event
,
818 out_exclusion
, out_filter_expression
,
819 out_bytecode
) != event_len
) {
820 ERR("Invalid event received as part of command payload");
821 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
829 lttng_payload_reset(&event_payload
);
833 static enum lttng_error_code
receive_lttng_event_context(
834 const struct command_ctx
*cmd_ctx
,
837 struct lttng_event_context
**out_event_context
)
840 const size_t event_context_len
=
841 (size_t) cmd_ctx
->lsm
.u
.context
.length
;
842 ssize_t sock_recv_len
;
843 enum lttng_error_code ret_code
;
844 struct lttng_payload event_context_payload
;
845 struct lttng_event_context
*context
= NULL
;
847 lttng_payload_init(&event_context_payload
);
849 ret
= lttng_dynamic_buffer_set_size(&event_context_payload
.buffer
,
852 ret_code
= LTTNG_ERR_NOMEM
;
856 sock_recv_len
= lttcomm_recv_unix_sock(
857 sock
, event_context_payload
.buffer
.data
,
859 if (sock_recv_len
< 0 || sock_recv_len
!= event_context_len
) {
860 ERR("Failed to receive event context in command payload");
862 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
866 /* Deserialize event. */
869 struct lttng_payload_view event_context_view
=
870 lttng_payload_view_from_payload(
871 &event_context_payload
, 0, -1);
873 len
= lttng_event_context_create_from_payload(
874 &event_context_view
, &context
);
877 ERR("Failed to create a event context from the received buffer");
878 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
882 if (len
!= event_context_len
) {
883 ERR("Event context from the received buffer is not the advertised length: expected length = %zu, payload length = %zd", event_context_len
, len
);
884 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
889 *out_event_context
= context
;
894 lttng_event_context_destroy(context
);
895 lttng_payload_reset(&event_context_payload
);
900 * Version of setup_lttng_msg() without command header.
902 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
903 void *payload_buf
, size_t payload_len
)
905 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
909 * Check if the current kernel tracer supports the session rotation feature.
910 * Return 1 if it does, 0 otherwise.
912 static int check_rotate_compatible(void)
916 if (the_kernel_tracer_version
.major
!= 2 ||
917 the_kernel_tracer_version
.minor
< 11) {
918 DBG("Kernel tracer version is not compatible with the rotation feature");
926 * Send data on a unix socket using the liblttsessiondcomm API.
928 * Return lttcomm error code.
930 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
933 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
935 /* Check valid length */
936 if (view
->buffer
.size
== 0) {
941 ret
= lttcomm_send_unix_sock(
942 sock
, view
->buffer
.data
, view
->buffer
.size
);
948 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
959 * Process the command requested by the lttng client within the command
960 * context structure. This function make sure that the return structure (llm)
961 * is set and ready for transmission before returning.
963 * Return any error encountered or 0 for success.
965 * "sock" is only used for special-case var. len data.
966 * A command may assume the ownership of the socket, in which case its value
967 * should be set to -1.
969 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
973 bool need_tracing_session
= true;
977 DBG("Processing client command '%s\' (%d)",
978 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) cmd_ctx
->lsm
.cmd_type
),
979 cmd_ctx
->lsm
.cmd_type
);
983 switch (cmd_ctx
->lsm
.cmd_type
) {
984 case LTTNG_CREATE_SESSION_EXT
:
985 case LTTNG_DESTROY_SESSION
:
986 case LTTNG_LIST_SESSIONS
:
987 case LTTNG_LIST_DOMAINS
:
988 case LTTNG_START_TRACE
:
989 case LTTNG_STOP_TRACE
:
990 case LTTNG_DATA_PENDING
:
991 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
992 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
993 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
994 case LTTNG_SNAPSHOT_RECORD
:
995 case LTTNG_SAVE_SESSION
:
996 case LTTNG_SET_SESSION_SHM_PATH
:
997 case LTTNG_REGENERATE_METADATA
:
998 case LTTNG_REGENERATE_STATEDUMP
:
999 case LTTNG_ROTATE_SESSION
:
1000 case LTTNG_ROTATION_GET_INFO
:
1001 case LTTNG_ROTATION_SET_SCHEDULE
:
1002 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1003 case LTTNG_CLEAR_SESSION
:
1004 case LTTNG_LIST_TRIGGERS
:
1005 case LTTNG_EXECUTE_ERROR_QUERY
:
1006 need_domain
= false;
1012 /* Needs a functioning consumerd? */
1013 switch (cmd_ctx
->lsm
.cmd_type
) {
1014 case LTTNG_REGISTER_TRIGGER
:
1015 case LTTNG_UNREGISTER_TRIGGER
:
1016 case LTTNG_EXECUTE_ERROR_QUERY
:
1017 need_consumerd
= false;
1020 need_consumerd
= true;
1024 if (the_config
.no_kernel
&& need_domain
&&
1025 cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
1027 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1029 ret
= LTTNG_ERR_KERN_NA
;
1034 /* Deny register consumer if we already have a spawned consumer. */
1035 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
1036 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1037 if (the_kconsumer_data
.pid
> 0) {
1038 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1039 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1042 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1046 * Check for command that don't needs to allocate a returned payload. We do
1047 * this here so we don't have to make the call for no payload at each
1050 switch(cmd_ctx
->lsm
.cmd_type
) {
1051 case LTTNG_LIST_SESSIONS
:
1052 case LTTNG_LIST_TRACEPOINTS
:
1053 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1054 case LTTNG_LIST_DOMAINS
:
1055 case LTTNG_LIST_CHANNELS
:
1056 case LTTNG_LIST_EVENTS
:
1057 case LTTNG_LIST_SYSCALLS
:
1058 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1059 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1060 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1061 case LTTNG_DATA_PENDING
:
1062 case LTTNG_ROTATE_SESSION
:
1063 case LTTNG_ROTATION_GET_INFO
:
1064 case LTTNG_REGISTER_TRIGGER
:
1065 case LTTNG_LIST_TRIGGERS
:
1066 case LTTNG_EXECUTE_ERROR_QUERY
:
1069 /* Setup lttng message with no payload */
1070 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
1072 /* This label does not try to unlock the session */
1073 goto init_setup_error
;
1077 /* Commands that DO NOT need a session. */
1078 switch (cmd_ctx
->lsm
.cmd_type
) {
1079 case LTTNG_CREATE_SESSION_EXT
:
1080 case LTTNG_LIST_SESSIONS
:
1081 case LTTNG_LIST_TRACEPOINTS
:
1082 case LTTNG_LIST_SYSCALLS
:
1083 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1084 case LTTNG_SAVE_SESSION
:
1085 case LTTNG_REGISTER_TRIGGER
:
1086 case LTTNG_UNREGISTER_TRIGGER
:
1087 case LTTNG_LIST_TRIGGERS
:
1088 case LTTNG_EXECUTE_ERROR_QUERY
:
1089 need_tracing_session
= false;
1092 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
1094 * We keep the session list lock across _all_ commands
1095 * for now, because the per-session lock does not
1096 * handle teardown properly.
1098 session_lock_list();
1099 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
1100 if (cmd_ctx
->session
== NULL
) {
1101 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1104 /* Acquire lock for the session */
1105 session_lock(cmd_ctx
->session
);
1111 * Commands that need a valid session but should NOT create one if none
1112 * exists. Instead of creating one and destroying it when the command is
1113 * handled, process that right before so we save some round trip in useless
1116 switch (cmd_ctx
->lsm
.cmd_type
) {
1117 case LTTNG_DISABLE_CHANNEL
:
1118 case LTTNG_DISABLE_EVENT
:
1119 switch (cmd_ctx
->lsm
.domain
.type
) {
1120 case LTTNG_DOMAIN_KERNEL
:
1121 if (!cmd_ctx
->session
->kernel_session
) {
1122 ret
= LTTNG_ERR_NO_CHANNEL
;
1126 case LTTNG_DOMAIN_JUL
:
1127 case LTTNG_DOMAIN_LOG4J
:
1128 case LTTNG_DOMAIN_PYTHON
:
1129 case LTTNG_DOMAIN_UST
:
1130 if (!cmd_ctx
->session
->ust_session
) {
1131 ret
= LTTNG_ERR_NO_CHANNEL
;
1136 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1148 * Check domain type for specific "pre-action".
1150 switch (cmd_ctx
->lsm
.domain
.type
) {
1151 case LTTNG_DOMAIN_KERNEL
:
1153 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1157 /* Kernel tracer check */
1158 if (!kernel_tracer_is_initialized()) {
1159 /* Basically, load kernel tracer modules */
1160 ret
= init_kernel_tracer();
1166 /* Consumer is in an ERROR state. Report back to client */
1167 if (need_consumerd
&& uatomic_read(&the_kernel_consumerd_state
) ==
1169 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1173 /* Need a session for kernel command */
1174 if (need_tracing_session
) {
1175 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1176 ret
= create_kernel_session(cmd_ctx
->session
);
1177 if (ret
!= LTTNG_OK
) {
1178 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1183 /* Start the kernel consumer daemon */
1184 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1185 if (the_kconsumer_data
.pid
== 0 &&
1186 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1187 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1188 ret
= start_consumerd(&the_kconsumer_data
);
1190 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1193 uatomic_set(&the_kernel_consumerd_state
, CONSUMER_STARTED
);
1195 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1199 * The consumer was just spawned so we need to add the socket to
1200 * the consumer output of the session if exist.
1202 ret
= consumer_create_socket(&the_kconsumer_data
,
1203 cmd_ctx
->session
->kernel_session
->consumer
);
1210 case LTTNG_DOMAIN_JUL
:
1211 case LTTNG_DOMAIN_LOG4J
:
1212 case LTTNG_DOMAIN_PYTHON
:
1213 if (!agent_tracing_is_enabled()) {
1214 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1218 case LTTNG_DOMAIN_UST
:
1220 if (!ust_app_supported()) {
1221 ret
= LTTNG_ERR_NO_UST
;
1225 /* Consumer is in an ERROR state. Report back to client */
1226 if (need_consumerd
&&
1227 uatomic_read(&the_ust_consumerd_state
) ==
1229 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1233 if (need_tracing_session
) {
1234 /* Create UST session if none exist. */
1235 if (cmd_ctx
->session
->ust_session
== NULL
) {
1236 lttng_domain domain
= cmd_ctx
->lsm
.domain
;
1237 ret
= create_ust_session(cmd_ctx
->session
, &domain
);
1238 if (ret
!= LTTNG_OK
) {
1243 /* Start the UST consumer daemons */
1245 pthread_mutex_lock(&the_ustconsumer64_data
.pid_mutex
);
1246 if (the_config
.consumerd64_bin_path
.value
&&
1247 the_ustconsumer64_data
.pid
== 0 &&
1248 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1249 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1250 ret
= start_consumerd(&the_ustconsumer64_data
);
1252 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1253 uatomic_set(&the_ust_consumerd64_fd
, -EINVAL
);
1257 uatomic_set(&the_ust_consumerd64_fd
, the_ustconsumer64_data
.cmd_sock
);
1258 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1260 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1264 * Setup socket for consumer 64 bit. No need for atomic access
1265 * since it was set above and can ONLY be set in this thread.
1267 ret
= consumer_create_socket(&the_ustconsumer64_data
,
1268 cmd_ctx
->session
->ust_session
->consumer
);
1274 pthread_mutex_lock(&the_ustconsumer32_data
.pid_mutex
);
1275 if (the_config
.consumerd32_bin_path
.value
&&
1276 the_ustconsumer32_data
.pid
== 0 &&
1277 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1278 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1279 ret
= start_consumerd(&the_ustconsumer32_data
);
1281 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1282 uatomic_set(&the_ust_consumerd32_fd
, -EINVAL
);
1286 uatomic_set(&the_ust_consumerd32_fd
, the_ustconsumer32_data
.cmd_sock
);
1287 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1289 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1293 * Setup socket for consumer 32 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_ustconsumer32_data
,
1297 cmd_ctx
->session
->ust_session
->consumer
);
1309 /* Validate consumer daemon state when start/stop trace command */
1310 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1311 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1312 switch (cmd_ctx
->lsm
.domain
.type
) {
1313 case LTTNG_DOMAIN_NONE
:
1315 case LTTNG_DOMAIN_JUL
:
1316 case LTTNG_DOMAIN_LOG4J
:
1317 case LTTNG_DOMAIN_PYTHON
:
1318 case LTTNG_DOMAIN_UST
:
1319 if (uatomic_read(&the_ust_consumerd_state
) != CONSUMER_STARTED
) {
1320 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1324 case LTTNG_DOMAIN_KERNEL
:
1325 if (uatomic_read(&the_kernel_consumerd_state
) != CONSUMER_STARTED
) {
1326 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1331 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1337 * Check that the UID matches that of the tracing session.
1338 * The root user can interact with all sessions.
1340 if (need_tracing_session
) {
1341 if (!session_access_ok(cmd_ctx
->session
,
1342 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1343 cmd_ctx
->session
->destroyed
) {
1344 ret
= LTTNG_ERR_EPERM
;
1350 * Send relayd information to consumer as soon as we have a domain and a
1353 if (cmd_ctx
->session
&& need_domain
) {
1355 * Setup relayd if not done yet. If the relayd information was already
1356 * sent to the consumer, this call will gracefully return.
1358 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1359 if (ret
!= LTTNG_OK
) {
1364 /* Process by command type */
1365 switch (cmd_ctx
->lsm
.cmd_type
) {
1366 case LTTNG_ADD_CONTEXT
:
1368 struct lttng_event_context
*event_context
= NULL
;
1369 const enum lttng_error_code ret_code
=
1370 receive_lttng_event_context(
1371 cmd_ctx
, *sock
, sock_error
, &event_context
);
1373 if (ret_code
!= LTTNG_OK
) {
1374 ret
= (int) ret_code
;
1378 ret
= cmd_add_context(cmd_ctx
, event_context
, the_kernel_poll_pipe
[1]);
1379 lttng_event_context_destroy(event_context
);
1382 case LTTNG_DISABLE_CHANNEL
:
1384 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1385 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1388 case LTTNG_ENABLE_CHANNEL
:
1390 ret
= cmd_enable_channel(
1391 cmd_ctx
, *sock
, the_kernel_poll_pipe
[1]);
1394 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1395 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1397 struct lttng_dynamic_buffer payload
;
1398 struct lttng_buffer_view payload_view
;
1399 const bool add_value
=
1400 cmd_ctx
->lsm
.cmd_type
==
1401 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1402 const size_t name_len
=
1403 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1405 const enum lttng_domain_type domain_type
=
1406 (enum lttng_domain_type
)
1407 cmd_ctx
->lsm
.domain
.type
;
1408 const enum lttng_process_attr process_attr
=
1409 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1410 .process_attr_tracker_add_remove_include_value
1412 const enum lttng_process_attr_value_type value_type
=
1413 (enum lttng_process_attr_value_type
) cmd_ctx
1415 .process_attr_tracker_add_remove_include_value
1417 struct process_attr_value
*value
;
1418 enum lttng_error_code ret_code
;
1419 long login_name_max
;
1421 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1422 if (login_name_max
< 0) {
1423 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1424 ret
= LTTNG_ERR_INVALID
;
1428 /* Receive remaining variable length payload if applicable. */
1429 if (name_len
> login_name_max
) {
1431 * POSIX mandates user and group names that are at least
1432 * 8 characters long. Note that although shadow-utils
1433 * (useradd, groupaadd, etc.) use 32 chars as their
1434 * limit (from bits/utmp.h, UT_NAMESIZE),
1435 * LOGIN_NAME_MAX is defined to 256.
1437 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1438 add_value
? "addition" : "removal",
1439 name_len
, login_name_max
);
1440 ret
= LTTNG_ERR_INVALID
;
1444 lttng_dynamic_buffer_init(&payload
);
1445 if (name_len
!= 0) {
1447 * Receive variable payload for user/group name
1450 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1452 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1453 add_value
? "add" : "remove");
1454 ret
= LTTNG_ERR_NOMEM
;
1455 goto error_add_remove_tracker_value
;
1458 ret
= lttcomm_recv_unix_sock(
1459 *sock
, payload
.data
, name_len
);
1461 ERR("Failed to receive payload of %s process attribute tracker value argument",
1462 add_value
? "add" : "remove");
1464 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1465 goto error_add_remove_tracker_value
;
1469 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1470 &payload
, 0, name_len
);
1471 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1472 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1473 goto error_add_remove_tracker_value
;
1477 * Validate the value type and domains are legal for the process
1478 * attribute tracker that is specified and convert the value to
1479 * add/remove to the internal sessiond representation.
1481 ret_code
= process_attr_value_from_comm(domain_type
,
1482 process_attr
, value_type
,
1483 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1485 &payload_view
, &value
);
1486 if (ret_code
!= LTTNG_OK
) {
1488 goto error_add_remove_tracker_value
;
1492 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1493 cmd_ctx
->session
, domain_type
,
1494 process_attr
, value
);
1496 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1497 cmd_ctx
->session
, domain_type
,
1498 process_attr
, value
);
1500 process_attr_value_destroy(value
);
1501 error_add_remove_tracker_value
:
1502 lttng_dynamic_buffer_reset(&payload
);
1505 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1507 enum lttng_tracking_policy tracking_policy
;
1508 const enum lttng_domain_type domain_type
=
1509 (enum lttng_domain_type
)
1510 cmd_ctx
->lsm
.domain
.type
;
1511 const enum lttng_process_attr process_attr
=
1512 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1513 .process_attr_tracker_get_tracking_policy
1516 ret
= cmd_process_attr_tracker_get_tracking_policy(
1517 cmd_ctx
->session
, domain_type
, process_attr
,
1519 if (ret
!= LTTNG_OK
) {
1523 uint32_t tracking_policy_u32
= tracking_policy
;
1524 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1525 &tracking_policy_u32
, sizeof(uint32_t));
1527 ret
= LTTNG_ERR_NOMEM
;
1533 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1535 const enum lttng_tracking_policy tracking_policy
=
1536 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1537 .process_attr_tracker_set_tracking_policy
1539 const enum lttng_domain_type domain_type
=
1540 (enum lttng_domain_type
)
1541 cmd_ctx
->lsm
.domain
.type
;
1542 const enum lttng_process_attr process_attr
=
1543 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1544 .process_attr_tracker_set_tracking_policy
1547 ret
= cmd_process_attr_tracker_set_tracking_policy(
1548 cmd_ctx
->session
, domain_type
, process_attr
,
1550 if (ret
!= LTTNG_OK
) {
1555 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1557 struct lttng_process_attr_values
*values
;
1558 struct lttng_dynamic_buffer reply
;
1559 const enum lttng_domain_type domain_type
=
1560 (enum lttng_domain_type
)
1561 cmd_ctx
->lsm
.domain
.type
;
1562 const enum lttng_process_attr process_attr
=
1563 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1564 .process_attr_tracker_get_inclusion_set
1567 ret
= cmd_process_attr_tracker_get_inclusion_set(
1568 cmd_ctx
->session
, domain_type
, process_attr
,
1570 if (ret
!= LTTNG_OK
) {
1574 lttng_dynamic_buffer_init(&reply
);
1575 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1577 goto error_tracker_get_inclusion_set
;
1580 ret
= setup_lttng_msg_no_cmd_header(
1581 cmd_ctx
, reply
.data
, reply
.size
);
1583 ret
= LTTNG_ERR_NOMEM
;
1584 goto error_tracker_get_inclusion_set
;
1588 error_tracker_get_inclusion_set
:
1589 lttng_process_attr_values_destroy(values
);
1590 lttng_dynamic_buffer_reset(&reply
);
1593 case LTTNG_ENABLE_EVENT
:
1594 case LTTNG_DISABLE_EVENT
:
1596 struct lttng_event
*event
;
1597 char *filter_expression
;
1598 struct lttng_event_exclusion
*exclusions
;
1599 struct lttng_bytecode
*bytecode
;
1600 const enum lttng_error_code ret_code
= receive_lttng_event(
1601 cmd_ctx
, *sock
, sock_error
, &event
,
1602 &filter_expression
, &bytecode
, &exclusions
);
1604 if (ret_code
!= LTTNG_OK
) {
1605 ret
= (int) ret_code
;
1610 * Ownership of filter_expression, exclusions, and bytecode is
1611 * always transferred.
1613 ret
= cmd_ctx
->lsm
.cmd_type
== LTTNG_ENABLE_EVENT
?
1614 cmd_enable_event(cmd_ctx
, event
,
1615 filter_expression
, exclusions
,
1617 the_kernel_poll_pipe
[1]) :
1618 cmd_disable_event(cmd_ctx
, event
,
1619 filter_expression
, bytecode
,
1621 lttng_event_destroy(event
);
1624 case LTTNG_LIST_TRACEPOINTS
:
1626 enum lttng_error_code ret_code
;
1627 size_t original_payload_size
;
1628 size_t payload_size
;
1629 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1631 ret
= setup_empty_lttng_msg(cmd_ctx
);
1633 ret
= LTTNG_ERR_NOMEM
;
1637 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1639 session_lock_list();
1640 ret_code
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
,
1641 &cmd_ctx
->reply_payload
);
1642 session_unlock_list();
1643 if (ret_code
!= LTTNG_OK
) {
1644 ret
= (int) ret_code
;
1648 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1649 command_header_size
- original_payload_size
;
1650 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1655 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1657 enum lttng_error_code ret_code
;
1658 size_t original_payload_size
;
1659 size_t payload_size
;
1660 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1662 ret
= setup_empty_lttng_msg(cmd_ctx
);
1664 ret
= LTTNG_ERR_NOMEM
;
1668 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1670 session_lock_list();
1671 ret_code
= cmd_list_tracepoint_fields(
1672 cmd_ctx
->lsm
.domain
.type
, &cmd_ctx
->reply_payload
);
1673 session_unlock_list();
1674 if (ret_code
!= LTTNG_OK
) {
1675 ret
= (int) ret_code
;
1679 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1680 command_header_size
- original_payload_size
;
1681 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1686 case LTTNG_LIST_SYSCALLS
:
1688 enum lttng_error_code ret_code
;
1689 size_t original_payload_size
;
1690 size_t payload_size
;
1691 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1693 ret
= setup_empty_lttng_msg(cmd_ctx
);
1695 ret
= LTTNG_ERR_NOMEM
;
1699 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1701 ret_code
= cmd_list_syscalls(&cmd_ctx
->reply_payload
);
1702 if (ret_code
!= LTTNG_OK
) {
1703 ret
= (int) ret_code
;
1707 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1708 command_header_size
- original_payload_size
;
1709 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1714 case LTTNG_SET_CONSUMER_URI
:
1717 struct lttng_uri
*uris
;
1719 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1720 len
= nb_uri
* sizeof(struct lttng_uri
);
1723 ret
= LTTNG_ERR_INVALID
;
1727 uris
= (lttng_uri
*) zmalloc(len
);
1729 ret
= LTTNG_ERR_FATAL
;
1733 /* Receive variable len data */
1734 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1735 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1737 DBG("No URIs received from client... continuing");
1739 ret
= LTTNG_ERR_SESSION_FAIL
;
1744 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1746 if (ret
!= LTTNG_OK
) {
1753 case LTTNG_START_TRACE
:
1756 * On the first start, if we have a kernel session and we have
1757 * enabled time or size-based rotations, we have to make sure
1758 * the kernel tracer supports it.
1760 if (!cmd_ctx
->session
->has_been_started
&& \
1761 cmd_ctx
->session
->kernel_session
&& \
1762 (cmd_ctx
->session
->rotate_timer_period
|| \
1763 cmd_ctx
->session
->rotate_size
) && \
1764 !check_rotate_compatible()) {
1765 DBG("Kernel tracer version is not compatible with the rotation feature");
1766 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1769 ret
= cmd_start_trace(cmd_ctx
->session
);
1772 case LTTNG_STOP_TRACE
:
1774 ret
= cmd_stop_trace(cmd_ctx
->session
);
1777 case LTTNG_DESTROY_SESSION
:
1779 ret
= cmd_destroy_session(cmd_ctx
->session
,
1780 the_notification_thread_handle
, sock
);
1783 case LTTNG_LIST_DOMAINS
:
1786 struct lttng_domain
*domains
= NULL
;
1788 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1790 /* Return value is a negative lttng_error_code. */
1795 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1796 nb_dom
* sizeof(struct lttng_domain
));
1806 case LTTNG_LIST_CHANNELS
:
1808 enum lttng_error_code ret_code
;
1809 size_t original_payload_size
;
1810 size_t payload_size
;
1811 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1813 ret
= setup_empty_lttng_msg(cmd_ctx
);
1815 ret
= LTTNG_ERR_NOMEM
;
1819 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1821 ret_code
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1822 cmd_ctx
->session
, &cmd_ctx
->reply_payload
);
1823 if (ret_code
!= LTTNG_OK
) {
1824 ret
= (int) ret_code
;
1828 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1829 command_header_size
- original_payload_size
;
1830 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1835 case LTTNG_LIST_EVENTS
:
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_events(cmd_ctx
->lsm
.domain
.type
,
1852 cmd_ctx
->lsm
.u
.list
.channel_name
, &cmd_ctx
->reply_payload
);
1853 if (ret_code
!= LTTNG_OK
) {
1854 ret
= (int) ret_code
;
1858 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1859 command_header_size
- original_payload_size
;
1860 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1865 case LTTNG_LIST_SESSIONS
:
1867 unsigned int nr_sessions
;
1868 lttng_session
*sessions_payload
;
1871 session_lock_list();
1872 nr_sessions
= lttng_sessions_count(
1873 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1874 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1876 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1877 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1878 sessions_payload
= (lttng_session
*) zmalloc(payload_len
);
1880 if (!sessions_payload
) {
1881 session_unlock_list();
1886 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1887 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1888 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1889 session_unlock_list();
1891 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1893 free(sessions_payload
);
1902 case LTTNG_REGISTER_CONSUMER
:
1904 struct consumer_data
*cdata
;
1906 switch (cmd_ctx
->lsm
.domain
.type
) {
1907 case LTTNG_DOMAIN_KERNEL
:
1908 cdata
= &the_kconsumer_data
;
1911 ret
= LTTNG_ERR_UND
;
1915 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1916 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1919 case LTTNG_DATA_PENDING
:
1922 uint8_t pending_ret_byte
;
1924 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1929 * This function may returns 0 or 1 to indicate whether or not
1930 * there is data pending. In case of error, it should return an
1931 * LTTNG_ERR code. However, some code paths may still return
1932 * a nondescript error code, which we handle by returning an
1935 if (pending_ret
== 0 || pending_ret
== 1) {
1937 * ret will be set to LTTNG_OK at the end of
1940 } else if (pending_ret
< 0) {
1941 ret
= LTTNG_ERR_UNK
;
1948 pending_ret_byte
= (uint8_t) pending_ret
;
1950 /* 1 byte to return whether or not data is pending */
1951 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1952 &pending_ret_byte
, 1);
1961 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1963 uint32_t snapshot_id
;
1964 struct lttcomm_lttng_output_id reply
;
1965 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
1967 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1970 if (ret
!= LTTNG_OK
) {
1973 reply
.id
= snapshot_id
;
1975 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1981 /* Copy output list into message payload */
1985 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1987 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_output
.output
;
1988 ret
= cmd_snapshot_del_output(cmd_ctx
->session
, &output
);
1991 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1994 struct lttng_snapshot_output
*outputs
= NULL
;
1996 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1997 if (nb_output
< 0) {
2002 LTTNG_ASSERT((nb_output
> 0 && outputs
) || nb_output
== 0);
2003 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
2004 nb_output
* sizeof(struct lttng_snapshot_output
));
2014 case LTTNG_SNAPSHOT_RECORD
:
2016 lttng_snapshot_output output
= cmd_ctx
->lsm
.u
.snapshot_record
.output
;
2017 ret
= cmd_snapshot_record(cmd_ctx
->session
,
2019 cmd_ctx
->lsm
.u
.snapshot_record
.wait
);
2022 case LTTNG_CREATE_SESSION_EXT
:
2024 struct lttng_dynamic_buffer payload
;
2025 struct lttng_session_descriptor
*return_descriptor
= NULL
;
2027 lttng_dynamic_buffer_init(&payload
);
2028 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2029 if (ret
!= LTTNG_OK
) {
2033 ret
= lttng_session_descriptor_serialize(return_descriptor
,
2036 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2037 lttng_session_descriptor_destroy(return_descriptor
);
2038 ret
= LTTNG_ERR_NOMEM
;
2041 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
2044 lttng_session_descriptor_destroy(return_descriptor
);
2045 ret
= LTTNG_ERR_NOMEM
;
2048 lttng_dynamic_buffer_reset(&payload
);
2049 lttng_session_descriptor_destroy(return_descriptor
);
2053 case LTTNG_SAVE_SESSION
:
2055 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
2059 case LTTNG_SET_SESSION_SHM_PATH
:
2061 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2062 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2065 case LTTNG_REGENERATE_METADATA
:
2067 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2070 case LTTNG_REGENERATE_STATEDUMP
:
2072 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2075 case LTTNG_REGISTER_TRIGGER
:
2077 struct lttng_trigger
*payload_trigger
;
2078 struct lttng_trigger
*return_trigger
;
2079 size_t original_reply_payload_size
;
2080 size_t reply_payload_size
;
2081 const struct lttng_credentials cmd_creds
= {
2082 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2083 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2086 ret
= setup_empty_lttng_msg(cmd_ctx
);
2088 ret
= LTTNG_ERR_NOMEM
;
2092 ret
= receive_lttng_trigger(
2093 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2094 if (ret
!= LTTNG_OK
) {
2098 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2100 ret
= cmd_register_trigger(&cmd_creds
, payload_trigger
,
2101 cmd_ctx
->lsm
.u
.trigger
.is_trigger_anonymous
,
2102 the_notification_thread_handle
,
2104 if (ret
!= LTTNG_OK
) {
2105 lttng_trigger_put(payload_trigger
);
2109 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2110 lttng_trigger_put(payload_trigger
);
2111 lttng_trigger_put(return_trigger
);
2113 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2114 ret
= LTTNG_ERR_NOMEM
;
2118 reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2119 original_reply_payload_size
;
2121 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2126 case LTTNG_UNREGISTER_TRIGGER
:
2128 struct lttng_trigger
*payload_trigger
;
2129 const struct lttng_credentials cmd_creds
= {
2130 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2131 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2134 ret
= receive_lttng_trigger(
2135 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2136 if (ret
!= LTTNG_OK
) {
2140 ret
= cmd_unregister_trigger(&cmd_creds
, payload_trigger
,
2141 the_notification_thread_handle
);
2142 lttng_trigger_put(payload_trigger
);
2145 case LTTNG_ROTATE_SESSION
:
2147 struct lttng_rotate_session_return rotate_return
;
2149 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2151 memset(&rotate_return
, 0, sizeof(rotate_return
));
2152 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2153 DBG("Kernel tracer version is not compatible with the rotation feature");
2154 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2158 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2160 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2166 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2167 sizeof(rotate_return
));
2176 case LTTNG_ROTATION_GET_INFO
:
2178 struct lttng_rotation_get_info_return get_info_return
;
2180 memset(&get_info_return
, 0, sizeof(get_info_return
));
2181 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2182 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2188 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2189 sizeof(get_info_return
));
2198 case LTTNG_ROTATION_SET_SCHEDULE
:
2201 enum lttng_rotation_schedule_type schedule_type
;
2204 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2205 DBG("Kernel tracer version does not support session rotations");
2206 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2210 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2211 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2212 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2214 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
, set_schedule
,
2215 schedule_type
, value
,
2216 the_notification_thread_handle
);
2217 if (ret
!= LTTNG_OK
) {
2223 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2225 lttng_session_list_schedules_return schedules
;
2227 schedules
.periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
;
2228 schedules
.periodic
.value
= cmd_ctx
->session
->rotate_timer_period
;
2229 schedules
.size
.set
= !!cmd_ctx
->session
->rotate_size
;
2230 schedules
.size
.value
= cmd_ctx
->session
->rotate_size
;
2232 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2242 case LTTNG_CLEAR_SESSION
:
2244 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2247 case LTTNG_LIST_TRIGGERS
:
2249 struct lttng_triggers
*return_triggers
= NULL
;
2250 size_t original_payload_size
;
2251 size_t payload_size
;
2253 ret
= setup_empty_lttng_msg(cmd_ctx
);
2255 ret
= LTTNG_ERR_NOMEM
;
2259 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2261 ret
= cmd_list_triggers(cmd_ctx
, the_notification_thread_handle
,
2263 if (ret
!= LTTNG_OK
) {
2267 LTTNG_ASSERT(return_triggers
);
2268 ret
= lttng_triggers_serialize(
2269 return_triggers
, &cmd_ctx
->reply_payload
);
2270 lttng_triggers_destroy(return_triggers
);
2272 ERR("Failed to serialize triggers in reply to `list triggers` command");
2273 ret
= LTTNG_ERR_NOMEM
;
2277 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2278 original_payload_size
;
2280 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2285 case LTTNG_EXECUTE_ERROR_QUERY
:
2287 struct lttng_error_query
*query
;
2288 const struct lttng_credentials cmd_creds
= {
2289 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2290 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2292 struct lttng_error_query_results
*results
= NULL
;
2293 size_t original_payload_size
;
2294 size_t payload_size
;
2296 ret
= setup_empty_lttng_msg(cmd_ctx
);
2298 ret
= LTTNG_ERR_NOMEM
;
2302 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2304 ret
= receive_lttng_error_query(
2305 cmd_ctx
, *sock
, sock_error
, &query
);
2306 if (ret
!= LTTNG_OK
) {
2310 ret
= cmd_execute_error_query(&cmd_creds
, query
, &results
,
2311 the_notification_thread_handle
);
2312 lttng_error_query_destroy(query
);
2313 if (ret
!= LTTNG_OK
) {
2317 LTTNG_ASSERT(results
);
2318 ret
= lttng_error_query_results_serialize(
2319 results
, &cmd_ctx
->reply_payload
);
2320 lttng_error_query_results_destroy(results
);
2322 ERR("Failed to serialize error query result set in reply to `execute error query` command");
2323 ret
= LTTNG_ERR_NOMEM
;
2327 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2328 original_payload_size
;
2330 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2337 ret
= LTTNG_ERR_UND
;
2342 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2343 DBG("Missing llm header, creating one.");
2344 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2348 /* Set return code */
2349 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2351 if (cmd_ctx
->session
) {
2352 session_unlock(cmd_ctx
->session
);
2353 session_put(cmd_ctx
->session
);
2354 cmd_ctx
->session
= NULL
;
2356 if (need_tracing_session
) {
2357 session_unlock_list();
2360 LTTNG_ASSERT(!rcu_read_ongoing());
2364 static int create_client_sock(void)
2366 int ret
, client_sock
;
2367 const mode_t old_umask
= umask(0);
2369 /* Create client tool unix socket */
2370 client_sock
= lttcomm_create_unix_sock(
2371 the_config
.client_unix_sock_path
.value
);
2372 if (client_sock
< 0) {
2373 ERR("Create unix sock failed: %s",
2374 the_config
.client_unix_sock_path
.value
);
2379 /* Set the cloexec flag */
2380 ret
= utils_set_fd_cloexec(client_sock
);
2382 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2383 "Continuing but note that the consumer daemon will have a "
2384 "reference to this socket on exec()", client_sock
);
2387 /* File permission MUST be 660 */
2388 ret
= chmod(the_config
.client_unix_sock_path
.value
,
2389 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2391 ERR("Set file permissions failed: %s",
2392 the_config
.client_unix_sock_path
.value
);
2394 (void) lttcomm_close_unix_sock(client_sock
);
2398 DBG("Created client socket (fd = %i)", client_sock
);
2405 static void cleanup_client_thread(void *data
)
2407 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2409 lttng_pipe_destroy(quit_pipe
);
2412 static void thread_init_cleanup(void *data
)
2414 set_thread_status(false);
2418 * This thread manage all clients request using the unix client socket for
2421 static void *thread_manage_clients(void *data
)
2423 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2425 uint32_t revents
, nb_fd
;
2426 struct lttng_poll_event events
;
2427 const int client_sock
= thread_state
.client_sock
;
2428 struct lttng_pipe
*quit_pipe
= (lttng_pipe
*) data
;
2429 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2430 struct command_ctx cmd_ctx
= {};
2432 DBG("[thread] Manage client started");
2434 lttng_payload_init(&cmd_ctx
.reply_payload
);
2436 is_root
= (getuid() == 0);
2438 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2440 rcu_register_thread();
2442 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2444 health_code_update();
2446 ret
= lttcomm_listen_unix_sock(client_sock
);
2452 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2453 * more will be added to this poll set.
2455 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2457 goto error_create_poll
;
2460 /* Add the application registration socket */
2461 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2466 /* Add thread quit pipe */
2467 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2472 /* Set state as running. */
2473 set_thread_status(true);
2474 pthread_cleanup_pop(0);
2476 /* This testpoint is after we signal readiness to the parent. */
2477 if (testpoint(sessiond_thread_manage_clients
)) {
2481 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2485 health_code_update();
2488 const struct cmd_completion_handler
*cmd_completion_handler
;
2490 cmd_ctx
.creds
.uid
= UINT32_MAX
;
2491 cmd_ctx
.creds
.gid
= UINT32_MAX
;
2492 cmd_ctx
.creds
.pid
= 0;
2493 cmd_ctx
.session
= NULL
;
2494 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2495 cmd_ctx
.lttng_msg_size
= 0;
2497 DBG("Accepting client command ...");
2499 /* Inifinite blocking call, waiting for transmission */
2501 health_poll_entry();
2502 ret
= lttng_poll_wait(&events
, -1);
2506 * Restart interrupted system call.
2508 if (errno
== EINTR
) {
2516 for (i
= 0; i
< nb_fd
; i
++) {
2517 revents
= LTTNG_POLL_GETEV(&events
, i
);
2518 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2520 health_code_update();
2522 if (pollfd
== thread_quit_pipe_fd
) {
2526 /* Event on the registration socket */
2527 if (revents
& LPOLLIN
) {
2529 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2530 ERR("Client socket poll error");
2533 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2539 DBG("Wait for client response");
2541 health_code_update();
2543 sock
= lttcomm_accept_unix_sock(client_sock
);
2549 * Set the CLOEXEC flag. Return code is useless because either way, the
2552 (void) utils_set_fd_cloexec(sock
);
2554 /* Set socket option for credentials retrieval */
2555 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2560 health_code_update();
2563 * Data is received from the lttng client. The struct
2564 * lttcomm_session_msg (lsm) contains the command and data request of
2567 DBG("Receiving data from client ...");
2568 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2569 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2570 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2571 DBG("Incomplete recv() from client... continuing");
2580 health_code_update();
2582 // TODO: Validate cmd_ctx including sanity check for
2583 // security purpose.
2585 rcu_thread_online();
2587 * This function dispatch the work to the kernel or userspace tracer
2588 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2589 * informations for the client. The command context struct contains
2590 * everything this function may needs.
2592 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2593 rcu_thread_offline();
2603 * TODO: Inform client somehow of the fatal error. At
2604 * this point, ret < 0 means that a zmalloc failed
2605 * (ENOMEM). Error detected but still accept
2606 * command, unless a socket error has been
2612 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2613 WARN("Command returned an invalid status code, returning unknown error: "
2614 "command type = %s (%d), ret = %d",
2615 lttcomm_sessiond_command_str((lttcomm_sessiond_command
) cmd_ctx
.lsm
.cmd_type
),
2616 cmd_ctx
.lsm
.cmd_type
, ret
);
2617 ret
= LTTNG_ERR_UNK
;
2620 cmd_completion_handler
= cmd_pop_completion_handler();
2621 if (cmd_completion_handler
) {
2622 enum lttng_error_code completion_code
;
2624 completion_code
= cmd_completion_handler
->run(
2625 cmd_completion_handler
->data
);
2626 if (completion_code
!= LTTNG_OK
) {
2631 health_code_update();
2634 struct lttng_payload_view view
=
2635 lttng_payload_view_from_payload(
2636 &cmd_ctx
.reply_payload
,
2638 struct lttcomm_lttng_msg
*llm
= (typeof(
2639 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2641 LTTNG_ASSERT(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2642 LTTNG_ASSERT(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2644 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2646 DBG("Sending response (size: %d, retcode: %s (%d))",
2647 cmd_ctx
.lttng_msg_size
,
2648 lttng_strerror(-llm
->ret_code
),
2650 ret
= send_unix_sock(sock
, &view
);
2652 ERR("Failed to send data back to client");
2655 /* End of transmission */
2663 health_code_update();
2675 lttng_poll_clean(&events
);
2679 unlink(the_config
.client_unix_sock_path
.value
);
2680 ret
= close(client_sock
);
2687 ERR("Health error occurred in %s", __func__
);
2690 health_unregister(the_health_sessiond
);
2692 DBG("Client thread dying");
2693 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2694 rcu_unregister_thread();
2699 bool shutdown_client_thread(void *thread_data
)
2701 struct lttng_pipe
*client_quit_pipe
= (lttng_pipe
*) thread_data
;
2702 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2704 return notify_thread_pipe(write_fd
) == 1;
2707 struct lttng_thread
*launch_client_thread(void)
2709 bool thread_running
;
2710 struct lttng_pipe
*client_quit_pipe
;
2711 struct lttng_thread
*thread
= NULL
;
2712 int client_sock_fd
= -1;
2714 sem_init(&thread_state
.ready
, 0, 0);
2715 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2716 if (!client_quit_pipe
) {
2720 client_sock_fd
= create_client_sock();
2721 if (client_sock_fd
< 0) {
2725 thread_state
.client_sock
= client_sock_fd
;
2726 thread
= lttng_thread_create("Client management",
2727 thread_manage_clients
,
2728 shutdown_client_thread
,
2729 cleanup_client_thread
,
2734 /* The client thread now owns the client sock fd and the quit pipe. */
2735 client_sock_fd
= -1;
2736 client_quit_pipe
= NULL
;
2739 * This thread is part of the threads that need to be fully
2740 * initialized before the session daemon is marked as "ready".
2742 thread_running
= wait_thread_status();
2743 if (!thread_running
) {
2748 if (client_sock_fd
>= 0) {
2749 if (close(client_sock_fd
)) {
2750 PERROR("Failed to close client socket");
2753 lttng_thread_put(thread
);
2754 cleanup_client_thread(client_quit_pipe
);