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 const struct lttcomm_lttng_msg llm
= {
91 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
92 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
93 .cmd_header_size
= cmd_header_len
,
94 .data_size
= 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 const struct lttcomm_lttng_msg llm
= {
165 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
166 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
167 .cmd_header_size
= cmd_header_len
,
168 .data_size
= payload_len
,
170 struct lttcomm_lttng_msg
*p_llm
;
172 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
= 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
= 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 * Should *NOT* be called with RCU read-side lock held.
423 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
426 const char *dir_name
;
427 struct consumer_output
*consumer
;
430 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 * Should *NOT* be called with RCU read-side lock held.
488 static int create_ust_session(struct ltt_session
*session
,
489 const struct lttng_domain
*domain
)
492 struct ltt_ust_session
*lus
= NULL
;
496 assert(session
->consumer
);
498 switch (domain
->type
) {
499 case LTTNG_DOMAIN_JUL
:
500 case LTTNG_DOMAIN_LOG4J
:
501 case LTTNG_DOMAIN_PYTHON
:
502 case LTTNG_DOMAIN_UST
:
505 ERR("Unknown UST domain on create session %d", domain
->type
);
506 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
510 DBG("Creating UST session");
512 lus
= trace_ust_create_session(session
->id
);
514 ret
= LTTNG_ERR_UST_SESS_FAIL
;
518 lus
->uid
= session
->uid
;
519 lus
->gid
= session
->gid
;
520 lus
->output_traces
= session
->output_traces
;
521 lus
->snapshot_mode
= session
->snapshot_mode
;
522 lus
->live_timer_interval
= session
->live_timer
;
523 session
->ust_session
= lus
;
524 if (session
->shm_path
[0]) {
525 strncpy(lus
->root_shm_path
, session
->shm_path
,
526 sizeof(lus
->root_shm_path
));
527 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
528 strncpy(lus
->shm_path
, session
->shm_path
,
529 sizeof(lus
->shm_path
));
530 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
531 strncat(lus
->shm_path
, "/ust",
532 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
534 /* Copy session output to the newly created UST session */
535 ret
= copy_session_consumer(domain
->type
, session
);
536 if (ret
!= LTTNG_OK
) {
544 session
->ust_session
= 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 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
;
588 * Count number of session permitted by uid/gid.
590 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
593 struct ltt_session
*session
;
594 const struct ltt_session_list
*session_list
= session_get_list();
596 DBG("Counting number of available session for UID %d", uid
);
597 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
598 if (!session_get(session
)) {
601 session_lock(session
);
602 /* Only count the sessions the user can control. */
603 if (session_access_ok(session
, uid
) &&
604 !session
->destroyed
) {
607 session_unlock(session
);
608 session_put(session
);
613 static enum lttng_error_code
receive_lttng_trigger(struct command_ctx
*cmd_ctx
,
616 struct lttng_trigger
**_trigger
)
620 ssize_t sock_recv_len
;
621 enum lttng_error_code ret_code
;
622 struct lttng_payload trigger_payload
;
623 struct lttng_trigger
*trigger
= NULL
;
625 lttng_payload_init(&trigger_payload
);
626 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
627 ret
= lttng_dynamic_buffer_set_size(
628 &trigger_payload
.buffer
, trigger_len
);
630 ret_code
= LTTNG_ERR_NOMEM
;
634 sock_recv_len
= lttcomm_recv_unix_sock(
635 sock
, trigger_payload
.buffer
.data
, trigger_len
);
636 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
637 ERR("Failed to receive trigger in command payload");
639 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
643 /* Receive fds, if any. */
644 if (cmd_ctx
->lsm
.fd_count
> 0) {
645 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
646 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
647 if (sock_recv_len
> 0 &&
648 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
649 ERR("Failed to receive all file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
650 cmd_ctx
->lsm
.fd_count
, (int) ret
);
651 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
654 } else if (sock_recv_len
<= 0) {
655 ERR("Failed to receive file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
656 cmd_ctx
->lsm
.fd_count
, (int) ret
);
657 ret_code
= LTTNG_ERR_FATAL
;
663 /* Deserialize trigger. */
665 struct lttng_payload_view view
=
666 lttng_payload_view_from_payload(
667 &trigger_payload
, 0, -1);
669 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
671 ERR("Invalid trigger received as part of command payload");
672 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
673 lttng_trigger_put(trigger
);
682 lttng_payload_reset(&trigger_payload
);
686 static enum lttng_error_code
receive_lttng_error_query(struct command_ctx
*cmd_ctx
,
689 struct lttng_error_query
**_query
)
693 ssize_t sock_recv_len
;
694 enum lttng_error_code ret_code
;
695 struct lttng_payload query_payload
;
696 struct lttng_error_query
*query
= NULL
;
698 lttng_payload_init(&query_payload
);
699 query_len
= (size_t) cmd_ctx
->lsm
.u
.error_query
.length
;
700 ret
= lttng_dynamic_buffer_set_size(&query_payload
.buffer
, query_len
);
702 ret_code
= LTTNG_ERR_NOMEM
;
706 sock_recv_len
= lttcomm_recv_unix_sock(
707 sock
, query_payload
.buffer
.data
, query_len
);
708 if (sock_recv_len
< 0 || sock_recv_len
!= query_len
) {
709 ERR("Failed to receive error query in command payload");
711 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
715 /* Receive fds, if any. */
716 if (cmd_ctx
->lsm
.fd_count
> 0) {
717 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
718 sock
, cmd_ctx
->lsm
.fd_count
, &query_payload
);
719 if (sock_recv_len
> 0 &&
720 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
721 ERR("Failed to receive all file descriptors for error query in command payload: expected fd count = %u, ret = %d",
722 cmd_ctx
->lsm
.fd_count
, (int) ret
);
723 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
726 } else if (sock_recv_len
<= 0) {
727 ERR("Failed to receive file descriptors for error query in command payload: expected fd count = %u, ret = %d",
728 cmd_ctx
->lsm
.fd_count
, (int) ret
);
729 ret_code
= LTTNG_ERR_FATAL
;
735 /* Deserialize error query. */
737 struct lttng_payload_view view
=
738 lttng_payload_view_from_payload(
739 &query_payload
, 0, -1);
741 if (lttng_error_query_create_from_payload(&view
, &query
) !=
743 ERR("Invalid error query received as part of command payload");
744 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
753 lttng_payload_reset(&query_payload
);
757 static enum lttng_error_code
receive_lttng_event(struct command_ctx
*cmd_ctx
,
760 struct lttng_event
**out_event
,
761 char **out_filter_expression
,
762 struct lttng_bytecode
**out_bytecode
,
763 struct lttng_event_exclusion
**out_exclusion
)
767 ssize_t sock_recv_len
;
768 enum lttng_error_code ret_code
;
769 struct lttng_payload event_payload
;
771 lttng_payload_init(&event_payload
);
772 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_ENABLE_EVENT
) {
773 event_len
= (size_t) cmd_ctx
->lsm
.u
.enable
.length
;
774 } else if (cmd_ctx
->lsm
.cmd_type
== LTTNG_DISABLE_EVENT
) {
775 event_len
= (size_t) cmd_ctx
->lsm
.u
.disable
.length
;
780 ret
= lttng_dynamic_buffer_set_size(&event_payload
.buffer
, event_len
);
782 ret_code
= LTTNG_ERR_NOMEM
;
786 sock_recv_len
= lttcomm_recv_unix_sock(
787 sock
, event_payload
.buffer
.data
, event_len
);
788 if (sock_recv_len
< 0 || sock_recv_len
!= event_len
) {
789 ERR("Failed to receive event in command payload");
791 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
795 /* Receive fds, if any. */
796 if (cmd_ctx
->lsm
.fd_count
> 0) {
797 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
798 sock
, cmd_ctx
->lsm
.fd_count
, &event_payload
);
799 if (sock_recv_len
> 0 &&
800 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
801 ERR("Failed to receive all file descriptors for event in command payload: expected fd count = %u, ret = %d",
802 cmd_ctx
->lsm
.fd_count
, (int) ret
);
803 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
806 } else if (sock_recv_len
<= 0) {
807 ERR("Failed to receive file descriptors for event in command payload: expected fd count = %u, ret = %d",
808 cmd_ctx
->lsm
.fd_count
, (int) ret
);
809 ret_code
= LTTNG_ERR_FATAL
;
815 /* Deserialize event. */
817 struct lttng_payload_view event_view
=
818 lttng_payload_view_from_payload(
819 &event_payload
, 0, -1);
821 if (lttng_event_create_from_payload(&event_view
, out_event
,
822 out_exclusion
, out_filter_expression
,
823 out_bytecode
) != event_len
) {
824 ERR("Invalid event received as part of command payload");
825 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
833 lttng_payload_reset(&event_payload
);
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
;
850 lttng_payload_init(&event_context_payload
);
852 ret
= lttng_dynamic_buffer_set_size(&event_context_payload
.buffer
,
855 ret_code
= LTTNG_ERR_NOMEM
;
859 sock_recv_len
= lttcomm_recv_unix_sock(
860 sock
, event_context_payload
.buffer
.data
,
862 if (sock_recv_len
< 0 || sock_recv_len
!= event_context_len
) {
863 ERR("Failed to receive event context in command payload");
865 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
869 /* Deserialize event. */
871 struct lttng_payload_view event_context_view
=
872 lttng_payload_view_from_payload(
873 &event_context_payload
, 0, -1);
875 if (lttng_event_context_create_from_payload(
876 &event_context_view
, out_event_context
) !=
878 ERR("Invalid event context received as part of command payload");
879 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
887 lttng_payload_reset(&event_context_payload
);
892 * Version of setup_lttng_msg() without command header.
894 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
895 void *payload_buf
, size_t payload_len
)
897 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
901 * Check if the current kernel tracer supports the session rotation feature.
902 * Return 1 if it does, 0 otherwise.
904 static int check_rotate_compatible(void)
908 if (the_kernel_tracer_version
.major
!= 2 ||
909 the_kernel_tracer_version
.minor
< 11) {
910 DBG("Kernel tracer version is not compatible with the rotation feature");
918 * Send data on a unix socket using the liblttsessiondcomm API.
920 * Return lttcomm error code.
922 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
925 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
927 /* Check valid length */
928 if (view
->buffer
.size
== 0) {
933 ret
= lttcomm_send_unix_sock(
934 sock
, view
->buffer
.data
, view
->buffer
.size
);
940 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
951 * Process the command requested by the lttng client within the command
952 * context structure. This function make sure that the return structure (llm)
953 * is set and ready for transmission before returning.
955 * Return any error encountered or 0 for success.
957 * "sock" is only used for special-case var. len data.
958 * A command may assume the ownership of the socket, in which case its value
959 * should be set to -1.
961 * Should *NOT* be called with RCU read-side lock held.
963 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
967 bool need_tracing_session
= true;
971 DBG("Processing client command '%s\' (%d)",
972 lttcomm_sessiond_command_str(cmd_ctx
->lsm
.cmd_type
),
973 cmd_ctx
->lsm
.cmd_type
);
975 assert(!rcu_read_ongoing());
979 switch (cmd_ctx
->lsm
.cmd_type
) {
980 case LTTNG_CREATE_SESSION_EXT
:
981 case LTTNG_DESTROY_SESSION
:
982 case LTTNG_LIST_SESSIONS
:
983 case LTTNG_LIST_DOMAINS
:
984 case LTTNG_START_TRACE
:
985 case LTTNG_STOP_TRACE
:
986 case LTTNG_DATA_PENDING
:
987 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
988 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
989 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
990 case LTTNG_SNAPSHOT_RECORD
:
991 case LTTNG_SAVE_SESSION
:
992 case LTTNG_SET_SESSION_SHM_PATH
:
993 case LTTNG_REGENERATE_METADATA
:
994 case LTTNG_REGENERATE_STATEDUMP
:
995 case LTTNG_ROTATE_SESSION
:
996 case LTTNG_ROTATION_GET_INFO
:
997 case LTTNG_ROTATION_SET_SCHEDULE
:
998 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
999 case LTTNG_CLEAR_SESSION
:
1000 case LTTNG_LIST_TRIGGERS
:
1001 case LTTNG_EXECUTE_ERROR_QUERY
:
1002 need_domain
= false;
1008 /* Needs a functioning consumerd? */
1009 switch (cmd_ctx
->lsm
.cmd_type
) {
1010 case LTTNG_REGISTER_TRIGGER
:
1011 case LTTNG_UNREGISTER_TRIGGER
:
1012 case LTTNG_EXECUTE_ERROR_QUERY
:
1013 need_consumerd
= false;
1016 need_consumerd
= true;
1020 if (the_config
.no_kernel
&& need_domain
&&
1021 cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
1023 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1025 ret
= LTTNG_ERR_KERN_NA
;
1030 /* Deny register consumer if we already have a spawned consumer. */
1031 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
1032 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1033 if (the_kconsumer_data
.pid
> 0) {
1034 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1035 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1038 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1042 * Check for command that don't needs to allocate a returned payload. We do
1043 * this here so we don't have to make the call for no payload at each
1046 switch(cmd_ctx
->lsm
.cmd_type
) {
1047 case LTTNG_LIST_SESSIONS
:
1048 case LTTNG_LIST_TRACEPOINTS
:
1049 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1050 case LTTNG_LIST_DOMAINS
:
1051 case LTTNG_LIST_CHANNELS
:
1052 case LTTNG_LIST_EVENTS
:
1053 case LTTNG_LIST_SYSCALLS
:
1054 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1055 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1056 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1057 case LTTNG_DATA_PENDING
:
1058 case LTTNG_ROTATE_SESSION
:
1059 case LTTNG_ROTATION_GET_INFO
:
1060 case LTTNG_REGISTER_TRIGGER
:
1061 case LTTNG_LIST_TRIGGERS
:
1062 case LTTNG_EXECUTE_ERROR_QUERY
:
1065 /* Setup lttng message with no payload */
1066 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
1068 /* This label does not try to unlock the session */
1069 goto init_setup_error
;
1073 /* Commands that DO NOT need a session. */
1074 switch (cmd_ctx
->lsm
.cmd_type
) {
1075 case LTTNG_CREATE_SESSION_EXT
:
1076 case LTTNG_LIST_SESSIONS
:
1077 case LTTNG_LIST_TRACEPOINTS
:
1078 case LTTNG_LIST_SYSCALLS
:
1079 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1080 case LTTNG_SAVE_SESSION
:
1081 case LTTNG_REGISTER_TRIGGER
:
1082 case LTTNG_UNREGISTER_TRIGGER
:
1083 case LTTNG_LIST_TRIGGERS
:
1084 case LTTNG_EXECUTE_ERROR_QUERY
:
1085 need_tracing_session
= false;
1088 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
1090 * We keep the session list lock across _all_ commands
1091 * for now, because the per-session lock does not
1092 * handle teardown properly.
1094 session_lock_list();
1095 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
1096 if (cmd_ctx
->session
== NULL
) {
1097 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1100 /* Acquire lock for the session */
1101 session_lock(cmd_ctx
->session
);
1107 * Commands that need a valid session but should NOT create one if none
1108 * exists. Instead of creating one and destroying it when the command is
1109 * handled, process that right before so we save some round trip in useless
1112 switch (cmd_ctx
->lsm
.cmd_type
) {
1113 case LTTNG_DISABLE_CHANNEL
:
1114 case LTTNG_DISABLE_EVENT
:
1115 switch (cmd_ctx
->lsm
.domain
.type
) {
1116 case LTTNG_DOMAIN_KERNEL
:
1117 if (!cmd_ctx
->session
->kernel_session
) {
1118 ret
= LTTNG_ERR_NO_CHANNEL
;
1122 case LTTNG_DOMAIN_JUL
:
1123 case LTTNG_DOMAIN_LOG4J
:
1124 case LTTNG_DOMAIN_PYTHON
:
1125 case LTTNG_DOMAIN_UST
:
1126 if (!cmd_ctx
->session
->ust_session
) {
1127 ret
= LTTNG_ERR_NO_CHANNEL
;
1132 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1144 * Check domain type for specific "pre-action".
1146 switch (cmd_ctx
->lsm
.domain
.type
) {
1147 case LTTNG_DOMAIN_KERNEL
:
1149 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1153 /* Kernel tracer check */
1154 if (!kernel_tracer_is_initialized()) {
1155 /* Basically, load kernel tracer modules */
1156 ret
= init_kernel_tracer();
1162 /* Consumer is in an ERROR state. Report back to client */
1163 if (need_consumerd
&& uatomic_read(&the_kernel_consumerd_state
) ==
1165 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1169 /* Need a session for kernel command */
1170 if (need_tracing_session
) {
1171 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1172 ret
= create_kernel_session(cmd_ctx
->session
);
1173 if (ret
!= LTTNG_OK
) {
1174 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1179 /* Start the kernel consumer daemon */
1180 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1181 if (the_kconsumer_data
.pid
== 0 &&
1182 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1183 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1184 ret
= start_consumerd(&the_kconsumer_data
);
1186 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1189 uatomic_set(&the_kernel_consumerd_state
, CONSUMER_STARTED
);
1191 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1195 * The consumer was just spawned so we need to add the socket to
1196 * the consumer output of the session if exist.
1198 ret
= consumer_create_socket(&the_kconsumer_data
,
1199 cmd_ctx
->session
->kernel_session
->consumer
);
1206 case LTTNG_DOMAIN_JUL
:
1207 case LTTNG_DOMAIN_LOG4J
:
1208 case LTTNG_DOMAIN_PYTHON
:
1209 if (!agent_tracing_is_enabled()) {
1210 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1214 case LTTNG_DOMAIN_UST
:
1216 if (!ust_app_supported()) {
1217 ret
= LTTNG_ERR_NO_UST
;
1221 /* Consumer is in an ERROR state. Report back to client */
1222 if (need_consumerd
&&
1223 uatomic_read(&the_ust_consumerd_state
) ==
1225 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1229 if (need_tracing_session
) {
1230 /* Create UST session if none exist. */
1231 if (cmd_ctx
->session
->ust_session
== NULL
) {
1232 ret
= create_ust_session(cmd_ctx
->session
,
1233 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
));
1234 if (ret
!= LTTNG_OK
) {
1239 /* Start the UST consumer daemons */
1241 pthread_mutex_lock(&the_ustconsumer64_data
.pid_mutex
);
1242 if (the_config
.consumerd64_bin_path
.value
&&
1243 the_ustconsumer64_data
.pid
== 0 &&
1244 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1245 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1246 ret
= start_consumerd(&the_ustconsumer64_data
);
1248 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1249 uatomic_set(&the_ust_consumerd64_fd
, -EINVAL
);
1253 uatomic_set(&the_ust_consumerd64_fd
, the_ustconsumer64_data
.cmd_sock
);
1254 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1256 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1260 * Setup socket for consumer 64 bit. No need for atomic access
1261 * since it was set above and can ONLY be set in this thread.
1263 ret
= consumer_create_socket(&the_ustconsumer64_data
,
1264 cmd_ctx
->session
->ust_session
->consumer
);
1270 pthread_mutex_lock(&the_ustconsumer32_data
.pid_mutex
);
1271 if (the_config
.consumerd32_bin_path
.value
&&
1272 the_ustconsumer32_data
.pid
== 0 &&
1273 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1274 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1275 ret
= start_consumerd(&the_ustconsumer32_data
);
1277 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1278 uatomic_set(&the_ust_consumerd32_fd
, -EINVAL
);
1282 uatomic_set(&the_ust_consumerd32_fd
, the_ustconsumer32_data
.cmd_sock
);
1283 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1285 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1289 * Setup socket for consumer 32 bit. No need for atomic access
1290 * since it was set above and can ONLY be set in this thread.
1292 ret
= consumer_create_socket(&the_ustconsumer32_data
,
1293 cmd_ctx
->session
->ust_session
->consumer
);
1305 /* Validate consumer daemon state when start/stop trace command */
1306 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1307 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1308 switch (cmd_ctx
->lsm
.domain
.type
) {
1309 case LTTNG_DOMAIN_NONE
:
1311 case LTTNG_DOMAIN_JUL
:
1312 case LTTNG_DOMAIN_LOG4J
:
1313 case LTTNG_DOMAIN_PYTHON
:
1314 case LTTNG_DOMAIN_UST
:
1315 if (uatomic_read(&the_ust_consumerd_state
) != CONSUMER_STARTED
) {
1316 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1320 case LTTNG_DOMAIN_KERNEL
:
1321 if (uatomic_read(&the_kernel_consumerd_state
) != CONSUMER_STARTED
) {
1322 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1327 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1333 * Check that the UID matches that of the tracing session.
1334 * The root user can interact with all sessions.
1336 if (need_tracing_session
) {
1337 if (!session_access_ok(cmd_ctx
->session
,
1338 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1339 cmd_ctx
->session
->destroyed
) {
1340 ret
= LTTNG_ERR_EPERM
;
1346 * Send relayd information to consumer as soon as we have a domain and a
1349 if (cmd_ctx
->session
&& need_domain
) {
1351 * Setup relayd if not done yet. If the relayd information was already
1352 * sent to the consumer, this call will gracefully return.
1354 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1355 if (ret
!= LTTNG_OK
) {
1360 /* Process by command type */
1361 switch (cmd_ctx
->lsm
.cmd_type
) {
1362 case LTTNG_ADD_CONTEXT
:
1364 struct lttng_event_context
*event_context
;
1365 const enum lttng_error_code ret_code
=
1366 receive_lttng_event_context(
1367 cmd_ctx
, *sock
, sock_error
, &event_context
);
1369 if (ret_code
!= LTTNG_OK
) {
1370 ret
= (int) ret_code
;
1374 ret
= cmd_add_context(cmd_ctx
, event_context
, the_kernel_poll_pipe
[1]);
1375 lttng_event_context_destroy(event_context
);
1378 case LTTNG_DISABLE_CHANNEL
:
1380 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1381 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1384 case LTTNG_ENABLE_CHANNEL
:
1386 ret
= cmd_enable_channel(
1387 cmd_ctx
, *sock
, the_kernel_poll_pipe
[1]);
1390 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1391 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1393 struct lttng_dynamic_buffer payload
;
1394 struct lttng_buffer_view payload_view
;
1395 const bool add_value
=
1396 cmd_ctx
->lsm
.cmd_type
==
1397 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1398 const size_t name_len
=
1399 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1401 const enum lttng_domain_type domain_type
=
1402 (enum lttng_domain_type
)
1403 cmd_ctx
->lsm
.domain
.type
;
1404 const enum lttng_process_attr process_attr
=
1405 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1406 .process_attr_tracker_add_remove_include_value
1408 const enum lttng_process_attr_value_type value_type
=
1409 (enum lttng_process_attr_value_type
) cmd_ctx
1411 .process_attr_tracker_add_remove_include_value
1413 struct process_attr_value
*value
;
1414 enum lttng_error_code ret_code
;
1415 long login_name_max
;
1417 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1418 if (login_name_max
< 0) {
1419 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1420 ret
= LTTNG_ERR_INVALID
;
1424 /* Receive remaining variable length payload if applicable. */
1425 if (name_len
> login_name_max
) {
1427 * POSIX mandates user and group names that are at least
1428 * 8 characters long. Note that although shadow-utils
1429 * (useradd, groupaadd, etc.) use 32 chars as their
1430 * limit (from bits/utmp.h, UT_NAMESIZE),
1431 * LOGIN_NAME_MAX is defined to 256.
1433 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1434 add_value
? "addition" : "removal",
1435 name_len
, login_name_max
);
1436 ret
= LTTNG_ERR_INVALID
;
1440 lttng_dynamic_buffer_init(&payload
);
1441 if (name_len
!= 0) {
1443 * Receive variable payload for user/group name
1446 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1448 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1449 add_value
? "add" : "remove");
1450 ret
= LTTNG_ERR_NOMEM
;
1451 goto error_add_remove_tracker_value
;
1454 ret
= lttcomm_recv_unix_sock(
1455 *sock
, payload
.data
, name_len
);
1457 ERR("Failed to receive payload of %s process attribute tracker value argument",
1458 add_value
? "add" : "remove");
1460 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1461 goto error_add_remove_tracker_value
;
1465 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1466 &payload
, 0, name_len
);
1467 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1468 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1469 goto error_add_remove_tracker_value
;
1473 * Validate the value type and domains are legal for the process
1474 * attribute tracker that is specified and convert the value to
1475 * add/remove to the internal sessiond representation.
1477 ret_code
= process_attr_value_from_comm(domain_type
,
1478 process_attr
, value_type
,
1479 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1481 &payload_view
, &value
);
1482 if (ret_code
!= LTTNG_OK
) {
1484 goto error_add_remove_tracker_value
;
1488 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1489 cmd_ctx
->session
, domain_type
,
1490 process_attr
, value
);
1492 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1493 cmd_ctx
->session
, domain_type
,
1494 process_attr
, value
);
1496 process_attr_value_destroy(value
);
1497 error_add_remove_tracker_value
:
1498 lttng_dynamic_buffer_reset(&payload
);
1501 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1503 enum lttng_tracking_policy tracking_policy
;
1504 const enum lttng_domain_type domain_type
=
1505 (enum lttng_domain_type
)
1506 cmd_ctx
->lsm
.domain
.type
;
1507 const enum lttng_process_attr process_attr
=
1508 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1509 .process_attr_tracker_get_tracking_policy
1512 ret
= cmd_process_attr_tracker_get_tracking_policy(
1513 cmd_ctx
->session
, domain_type
, process_attr
,
1515 if (ret
!= LTTNG_OK
) {
1519 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1520 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1522 ret
= LTTNG_ERR_NOMEM
;
1528 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1530 const enum lttng_tracking_policy tracking_policy
=
1531 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1532 .process_attr_tracker_set_tracking_policy
1534 const enum lttng_domain_type domain_type
=
1535 (enum lttng_domain_type
)
1536 cmd_ctx
->lsm
.domain
.type
;
1537 const enum lttng_process_attr process_attr
=
1538 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1539 .process_attr_tracker_set_tracking_policy
1542 ret
= cmd_process_attr_tracker_set_tracking_policy(
1543 cmd_ctx
->session
, domain_type
, process_attr
,
1545 if (ret
!= LTTNG_OK
) {
1550 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1552 struct lttng_process_attr_values
*values
;
1553 struct lttng_dynamic_buffer reply
;
1554 const enum lttng_domain_type domain_type
=
1555 (enum lttng_domain_type
)
1556 cmd_ctx
->lsm
.domain
.type
;
1557 const enum lttng_process_attr process_attr
=
1558 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1559 .process_attr_tracker_get_inclusion_set
1562 ret
= cmd_process_attr_tracker_get_inclusion_set(
1563 cmd_ctx
->session
, domain_type
, process_attr
,
1565 if (ret
!= LTTNG_OK
) {
1569 lttng_dynamic_buffer_init(&reply
);
1570 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1572 goto error_tracker_get_inclusion_set
;
1575 ret
= setup_lttng_msg_no_cmd_header(
1576 cmd_ctx
, reply
.data
, reply
.size
);
1578 ret
= LTTNG_ERR_NOMEM
;
1579 goto error_tracker_get_inclusion_set
;
1583 error_tracker_get_inclusion_set
:
1584 lttng_process_attr_values_destroy(values
);
1585 lttng_dynamic_buffer_reset(&reply
);
1588 case LTTNG_ENABLE_EVENT
:
1589 case LTTNG_DISABLE_EVENT
:
1591 struct lttng_event
*event
;
1592 char *filter_expression
;
1593 struct lttng_event_exclusion
*exclusions
;
1594 struct lttng_bytecode
*bytecode
;
1595 const enum lttng_error_code ret_code
= receive_lttng_event(
1596 cmd_ctx
, *sock
, sock_error
, &event
,
1597 &filter_expression
, &bytecode
, &exclusions
);
1599 if (ret_code
!= LTTNG_OK
) {
1600 ret
= (int) ret_code
;
1605 * Ownership of filter_expression, exclusions, and bytecode is
1606 * always transferred.
1608 ret
= cmd_ctx
->lsm
.cmd_type
== LTTNG_ENABLE_EVENT
?
1609 cmd_enable_event(cmd_ctx
, event
,
1610 filter_expression
, exclusions
,
1612 the_kernel_poll_pipe
[1]) :
1613 cmd_disable_event(cmd_ctx
, event
,
1614 filter_expression
, bytecode
,
1616 lttng_event_destroy(event
);
1619 case LTTNG_LIST_TRACEPOINTS
:
1621 enum lttng_error_code ret_code
;
1622 size_t original_payload_size
;
1623 size_t payload_size
;
1624 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1626 ret
= setup_empty_lttng_msg(cmd_ctx
);
1628 ret
= LTTNG_ERR_NOMEM
;
1632 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1634 session_lock_list();
1635 ret_code
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
,
1636 &cmd_ctx
->reply_payload
);
1637 session_unlock_list();
1638 if (ret_code
!= LTTNG_OK
) {
1639 ret
= (int) ret_code
;
1643 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1644 command_header_size
- original_payload_size
;
1645 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1650 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1652 struct lttng_event_field
*fields
;
1655 session_lock_list();
1656 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1658 session_unlock_list();
1659 if (nb_fields
< 0) {
1660 /* Return value is a negative lttng_error_code. */
1666 * Setup lttng message with payload size set to the event list size in
1667 * bytes and then copy list into the llm payload.
1669 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1670 sizeof(struct lttng_event_field
) * nb_fields
);
1680 case LTTNG_LIST_SYSCALLS
:
1682 enum lttng_error_code ret_code
;
1683 size_t original_payload_size
;
1684 size_t payload_size
;
1685 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1687 ret
= setup_empty_lttng_msg(cmd_ctx
);
1689 ret
= LTTNG_ERR_NOMEM
;
1693 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1695 ret_code
= cmd_list_syscalls(&cmd_ctx
->reply_payload
);
1696 if (ret_code
!= LTTNG_OK
) {
1697 ret
= (int) ret_code
;
1701 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1702 command_header_size
- original_payload_size
;
1703 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1708 case LTTNG_SET_CONSUMER_URI
:
1711 struct lttng_uri
*uris
;
1713 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1714 len
= nb_uri
* sizeof(struct lttng_uri
);
1717 ret
= LTTNG_ERR_INVALID
;
1721 uris
= zmalloc(len
);
1723 ret
= LTTNG_ERR_FATAL
;
1727 /* Receive variable len data */
1728 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1729 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1731 DBG("No URIs received from client... continuing");
1733 ret
= LTTNG_ERR_SESSION_FAIL
;
1738 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1740 if (ret
!= LTTNG_OK
) {
1747 case LTTNG_START_TRACE
:
1750 * On the first start, if we have a kernel session and we have
1751 * enabled time or size-based rotations, we have to make sure
1752 * the kernel tracer supports it.
1754 if (!cmd_ctx
->session
->has_been_started
&& \
1755 cmd_ctx
->session
->kernel_session
&& \
1756 (cmd_ctx
->session
->rotate_timer_period
|| \
1757 cmd_ctx
->session
->rotate_size
) && \
1758 !check_rotate_compatible()) {
1759 DBG("Kernel tracer version is not compatible with the rotation feature");
1760 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1763 ret
= cmd_start_trace(cmd_ctx
->session
);
1766 case LTTNG_STOP_TRACE
:
1768 ret
= cmd_stop_trace(cmd_ctx
->session
);
1771 case LTTNG_DESTROY_SESSION
:
1773 ret
= cmd_destroy_session(cmd_ctx
->session
,
1774 the_notification_thread_handle
, sock
);
1777 case LTTNG_LIST_DOMAINS
:
1780 struct lttng_domain
*domains
= NULL
;
1782 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1784 /* Return value is a negative lttng_error_code. */
1789 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1790 nb_dom
* sizeof(struct lttng_domain
));
1800 case LTTNG_LIST_CHANNELS
:
1802 enum lttng_error_code ret_code
;
1803 size_t original_payload_size
;
1804 size_t payload_size
;
1805 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1807 ret
= setup_empty_lttng_msg(cmd_ctx
);
1809 ret
= LTTNG_ERR_NOMEM
;
1813 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1815 ret_code
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1816 cmd_ctx
->session
, &cmd_ctx
->reply_payload
);
1817 if (ret_code
!= LTTNG_OK
) {
1818 ret
= (int) ret_code
;
1822 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1823 command_header_size
- original_payload_size
;
1824 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1829 case LTTNG_LIST_EVENTS
:
1831 enum lttng_error_code ret_code
;
1832 size_t original_payload_size
;
1833 size_t payload_size
;
1834 const size_t command_header_size
= sizeof(struct lttcomm_list_command_header
);
1836 ret
= setup_empty_lttng_msg(cmd_ctx
);
1838 ret
= LTTNG_ERR_NOMEM
;
1842 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1844 ret_code
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1846 cmd_ctx
->lsm
.u
.list
.channel_name
, &cmd_ctx
->reply_payload
);
1847 if (ret_code
!= LTTNG_OK
) {
1848 ret
= (int) ret_code
;
1852 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1853 command_header_size
- original_payload_size
;
1854 update_lttng_msg(cmd_ctx
, command_header_size
, payload_size
);
1859 case LTTNG_LIST_SESSIONS
:
1861 unsigned int nr_sessions
;
1862 void *sessions_payload
;
1865 session_lock_list();
1866 nr_sessions
= lttng_sessions_count(
1867 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1868 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1870 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1871 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1872 sessions_payload
= zmalloc(payload_len
);
1874 if (!sessions_payload
) {
1875 session_unlock_list();
1880 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1881 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1882 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1883 session_unlock_list();
1885 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1887 free(sessions_payload
);
1896 case LTTNG_REGISTER_CONSUMER
:
1898 struct consumer_data
*cdata
;
1900 switch (cmd_ctx
->lsm
.domain
.type
) {
1901 case LTTNG_DOMAIN_KERNEL
:
1902 cdata
= &the_kconsumer_data
;
1905 ret
= LTTNG_ERR_UND
;
1909 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1910 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1913 case LTTNG_DATA_PENDING
:
1916 uint8_t pending_ret_byte
;
1918 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1923 * This function may returns 0 or 1 to indicate whether or not
1924 * there is data pending. In case of error, it should return an
1925 * LTTNG_ERR code. However, some code paths may still return
1926 * a nondescript error code, which we handle by returning an
1929 if (pending_ret
== 0 || pending_ret
== 1) {
1931 * ret will be set to LTTNG_OK at the end of
1934 } else if (pending_ret
< 0) {
1935 ret
= LTTNG_ERR_UNK
;
1942 pending_ret_byte
= (uint8_t) pending_ret
;
1944 /* 1 byte to return whether or not data is pending */
1945 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1946 &pending_ret_byte
, 1);
1955 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1957 uint32_t snapshot_id
;
1958 struct lttcomm_lttng_output_id reply
;
1960 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1961 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
),
1963 if (ret
!= LTTNG_OK
) {
1966 reply
.id
= snapshot_id
;
1968 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1974 /* Copy output list into message payload */
1978 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1980 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1981 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
));
1984 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1987 struct lttng_snapshot_output
*outputs
= NULL
;
1989 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1990 if (nb_output
< 0) {
1995 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1996 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1997 nb_output
* sizeof(struct lttng_snapshot_output
));
2007 case LTTNG_SNAPSHOT_RECORD
:
2009 ret
= cmd_snapshot_record(cmd_ctx
->session
,
2010 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_record
.output
),
2011 cmd_ctx
->lsm
.u
.snapshot_record
.wait
);
2014 case LTTNG_CREATE_SESSION_EXT
:
2016 struct lttng_dynamic_buffer payload
;
2017 struct lttng_session_descriptor
*return_descriptor
= NULL
;
2019 lttng_dynamic_buffer_init(&payload
);
2020 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2021 if (ret
!= LTTNG_OK
) {
2025 ret
= lttng_session_descriptor_serialize(return_descriptor
,
2028 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2029 lttng_session_descriptor_destroy(return_descriptor
);
2030 ret
= LTTNG_ERR_NOMEM
;
2033 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
2036 lttng_session_descriptor_destroy(return_descriptor
);
2037 ret
= LTTNG_ERR_NOMEM
;
2040 lttng_dynamic_buffer_reset(&payload
);
2041 lttng_session_descriptor_destroy(return_descriptor
);
2045 case LTTNG_SAVE_SESSION
:
2047 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
2051 case LTTNG_SET_SESSION_SHM_PATH
:
2053 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2054 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2057 case LTTNG_REGENERATE_METADATA
:
2059 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2062 case LTTNG_REGENERATE_STATEDUMP
:
2064 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2067 case LTTNG_REGISTER_TRIGGER
:
2069 struct lttng_trigger
*payload_trigger
;
2070 struct lttng_trigger
*return_trigger
;
2071 size_t original_reply_payload_size
;
2072 size_t reply_payload_size
;
2073 const struct lttng_credentials cmd_creds
= {
2074 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2075 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2078 ret
= setup_empty_lttng_msg(cmd_ctx
);
2080 ret
= LTTNG_ERR_NOMEM
;
2084 ret
= receive_lttng_trigger(
2085 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2086 if (ret
!= LTTNG_OK
) {
2090 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2092 ret
= cmd_register_trigger(&cmd_creds
, payload_trigger
,
2093 cmd_ctx
->lsm
.u
.trigger
.is_trigger_anonymous
,
2094 the_notification_thread_handle
,
2096 if (ret
!= LTTNG_OK
) {
2097 lttng_trigger_put(payload_trigger
);
2101 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2102 lttng_trigger_put(payload_trigger
);
2103 lttng_trigger_put(return_trigger
);
2105 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2106 ret
= LTTNG_ERR_NOMEM
;
2110 reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2111 original_reply_payload_size
;
2113 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2118 case LTTNG_UNREGISTER_TRIGGER
:
2120 struct lttng_trigger
*payload_trigger
;
2121 const struct lttng_credentials cmd_creds
= {
2122 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2123 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2126 ret
= receive_lttng_trigger(
2127 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2128 if (ret
!= LTTNG_OK
) {
2132 ret
= cmd_unregister_trigger(&cmd_creds
, payload_trigger
,
2133 the_notification_thread_handle
);
2134 lttng_trigger_put(payload_trigger
);
2137 case LTTNG_ROTATE_SESSION
:
2139 struct lttng_rotate_session_return rotate_return
;
2141 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2143 memset(&rotate_return
, 0, sizeof(rotate_return
));
2144 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2145 DBG("Kernel tracer version is not compatible with the rotation feature");
2146 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2150 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2152 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2158 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2159 sizeof(rotate_return
));
2168 case LTTNG_ROTATION_GET_INFO
:
2170 struct lttng_rotation_get_info_return get_info_return
;
2172 memset(&get_info_return
, 0, sizeof(get_info_return
));
2173 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2174 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2180 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2181 sizeof(get_info_return
));
2190 case LTTNG_ROTATION_SET_SCHEDULE
:
2193 enum lttng_rotation_schedule_type schedule_type
;
2196 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2197 DBG("Kernel tracer version does not support session rotations");
2198 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2202 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2203 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2204 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2206 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
, set_schedule
,
2207 schedule_type
, value
,
2208 the_notification_thread_handle
);
2209 if (ret
!= LTTNG_OK
) {
2215 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2217 struct lttng_session_list_schedules_return schedules
= {
2218 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2219 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2220 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2221 .size
.value
= cmd_ctx
->session
->rotate_size
,
2224 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2234 case LTTNG_CLEAR_SESSION
:
2236 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2239 case LTTNG_LIST_TRIGGERS
:
2241 struct lttng_triggers
*return_triggers
= NULL
;
2242 size_t original_payload_size
;
2243 size_t payload_size
;
2245 ret
= setup_empty_lttng_msg(cmd_ctx
);
2247 ret
= LTTNG_ERR_NOMEM
;
2251 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2253 ret
= cmd_list_triggers(cmd_ctx
, the_notification_thread_handle
,
2255 if (ret
!= LTTNG_OK
) {
2259 assert(return_triggers
);
2260 ret
= lttng_triggers_serialize(
2261 return_triggers
, &cmd_ctx
->reply_payload
);
2262 lttng_triggers_destroy(return_triggers
);
2264 ERR("Failed to serialize triggers in reply to `list triggers` command");
2265 ret
= LTTNG_ERR_NOMEM
;
2269 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2270 original_payload_size
;
2272 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2277 case LTTNG_EXECUTE_ERROR_QUERY
:
2279 struct lttng_error_query
*query
;
2280 const struct lttng_credentials cmd_creds
= {
2281 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2282 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2284 struct lttng_error_query_results
*results
= NULL
;
2285 size_t original_payload_size
;
2286 size_t payload_size
;
2288 ret
= setup_empty_lttng_msg(cmd_ctx
);
2290 ret
= LTTNG_ERR_NOMEM
;
2294 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2296 ret
= receive_lttng_error_query(
2297 cmd_ctx
, *sock
, sock_error
, &query
);
2298 if (ret
!= LTTNG_OK
) {
2302 ret
= cmd_execute_error_query(&cmd_creds
, query
, &results
,
2303 the_notification_thread_handle
);
2304 lttng_error_query_destroy(query
);
2305 if (ret
!= LTTNG_OK
) {
2310 ret
= lttng_error_query_results_serialize(
2311 results
, &cmd_ctx
->reply_payload
);
2312 lttng_error_query_results_destroy(results
);
2314 ERR("Failed to serialize error query result set in reply to `execute error query` command");
2315 ret
= LTTNG_ERR_NOMEM
;
2319 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2320 original_payload_size
;
2322 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2329 ret
= LTTNG_ERR_UND
;
2334 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2335 DBG("Missing llm header, creating one.");
2336 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2340 /* Set return code */
2341 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2343 if (cmd_ctx
->session
) {
2344 session_unlock(cmd_ctx
->session
);
2345 session_put(cmd_ctx
->session
);
2346 cmd_ctx
->session
= NULL
;
2348 if (need_tracing_session
) {
2349 session_unlock_list();
2352 assert(!rcu_read_ongoing());
2356 static int create_client_sock(void)
2358 int ret
, client_sock
;
2359 const mode_t old_umask
= umask(0);
2361 /* Create client tool unix socket */
2362 client_sock
= lttcomm_create_unix_sock(
2363 the_config
.client_unix_sock_path
.value
);
2364 if (client_sock
< 0) {
2365 ERR("Create unix sock failed: %s",
2366 the_config
.client_unix_sock_path
.value
);
2371 /* Set the cloexec flag */
2372 ret
= utils_set_fd_cloexec(client_sock
);
2374 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2375 "Continuing but note that the consumer daemon will have a "
2376 "reference to this socket on exec()", client_sock
);
2379 /* File permission MUST be 660 */
2380 ret
= chmod(the_config
.client_unix_sock_path
.value
,
2381 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2383 ERR("Set file permissions failed: %s",
2384 the_config
.client_unix_sock_path
.value
);
2386 (void) lttcomm_close_unix_sock(client_sock
);
2390 DBG("Created client socket (fd = %i)", client_sock
);
2397 static void cleanup_client_thread(void *data
)
2399 struct lttng_pipe
*quit_pipe
= data
;
2401 lttng_pipe_destroy(quit_pipe
);
2404 static void thread_init_cleanup(void *data
)
2406 set_thread_status(false);
2410 * This thread manage all clients request using the unix client socket for
2413 static void *thread_manage_clients(void *data
)
2415 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2417 uint32_t revents
, nb_fd
;
2418 struct lttng_poll_event events
;
2419 const int client_sock
= thread_state
.client_sock
;
2420 struct lttng_pipe
*quit_pipe
= data
;
2421 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2422 struct command_ctx cmd_ctx
= {};
2424 DBG("[thread] Manage client started");
2426 lttng_payload_init(&cmd_ctx
.reply_payload
);
2428 is_root
= (getuid() == 0);
2430 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2432 rcu_register_thread();
2434 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2436 health_code_update();
2438 ret
= lttcomm_listen_unix_sock(client_sock
);
2444 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2445 * more will be added to this poll set.
2447 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2449 goto error_create_poll
;
2452 /* Add the application registration socket */
2453 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2458 /* Add thread quit pipe */
2459 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2464 /* Set state as running. */
2465 set_thread_status(true);
2466 pthread_cleanup_pop(0);
2468 /* This testpoint is after we signal readiness to the parent. */
2469 if (testpoint(sessiond_thread_manage_clients
)) {
2473 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2477 health_code_update();
2480 const struct cmd_completion_handler
*cmd_completion_handler
;
2482 cmd_ctx
.creds
= (lttng_sock_cred
) {
2486 cmd_ctx
.session
= NULL
;
2487 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2488 cmd_ctx
.lttng_msg_size
= 0;
2490 DBG("Accepting client command ...");
2492 /* Inifinite blocking call, waiting for transmission */
2494 health_poll_entry();
2495 ret
= lttng_poll_wait(&events
, -1);
2499 * Restart interrupted system call.
2501 if (errno
== EINTR
) {
2509 for (i
= 0; i
< nb_fd
; i
++) {
2510 revents
= LTTNG_POLL_GETEV(&events
, i
);
2511 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2513 health_code_update();
2515 if (pollfd
== thread_quit_pipe_fd
) {
2519 /* Event on the registration socket */
2520 if (revents
& LPOLLIN
) {
2522 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2523 ERR("Client socket poll error");
2526 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2532 DBG("Wait for client response");
2534 health_code_update();
2536 sock
= lttcomm_accept_unix_sock(client_sock
);
2542 * Set the CLOEXEC flag. Return code is useless because either way, the
2545 (void) utils_set_fd_cloexec(sock
);
2547 /* Set socket option for credentials retrieval */
2548 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2553 health_code_update();
2556 * Data is received from the lttng client. The struct
2557 * lttcomm_session_msg (lsm) contains the command and data request of
2560 DBG("Receiving data from client ...");
2561 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2562 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2563 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2564 DBG("Incomplete recv() from client... continuing");
2573 health_code_update();
2575 // TODO: Validate cmd_ctx including sanity check for
2576 // security purpose.
2578 rcu_thread_online();
2580 * This function dispatch the work to the kernel or userspace tracer
2581 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2582 * informations for the client. The command context struct contains
2583 * everything this function may needs.
2585 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2586 rcu_thread_offline();
2596 * TODO: Inform client somehow of the fatal error. At
2597 * this point, ret < 0 means that a zmalloc failed
2598 * (ENOMEM). Error detected but still accept
2599 * command, unless a socket error has been
2605 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2606 WARN("Command returned an invalid status code, returning unknown error: "
2607 "command type = %s (%d), ret = %d",
2608 lttcomm_sessiond_command_str(cmd_ctx
.lsm
.cmd_type
),
2609 cmd_ctx
.lsm
.cmd_type
, ret
);
2610 ret
= LTTNG_ERR_UNK
;
2613 cmd_completion_handler
= cmd_pop_completion_handler();
2614 if (cmd_completion_handler
) {
2615 enum lttng_error_code completion_code
;
2617 completion_code
= cmd_completion_handler
->run(
2618 cmd_completion_handler
->data
);
2619 if (completion_code
!= LTTNG_OK
) {
2624 health_code_update();
2627 struct lttng_payload_view view
=
2628 lttng_payload_view_from_payload(
2629 &cmd_ctx
.reply_payload
,
2631 struct lttcomm_lttng_msg
*llm
= (typeof(
2632 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2634 assert(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2635 assert(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2637 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2639 DBG("Sending response (size: %d, retcode: %s (%d))",
2640 cmd_ctx
.lttng_msg_size
,
2641 lttng_strerror(-llm
->ret_code
),
2643 ret
= send_unix_sock(sock
, &view
);
2645 ERR("Failed to send data back to client");
2648 /* End of transmission */
2656 health_code_update();
2668 lttng_poll_clean(&events
);
2672 unlink(the_config
.client_unix_sock_path
.value
);
2673 ret
= close(client_sock
);
2680 ERR("Health error occurred in %s", __func__
);
2683 health_unregister(the_health_sessiond
);
2685 DBG("Client thread dying");
2686 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2687 rcu_unregister_thread();
2692 bool shutdown_client_thread(void *thread_data
)
2694 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2695 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2697 return notify_thread_pipe(write_fd
) == 1;
2700 struct lttng_thread
*launch_client_thread(void)
2702 bool thread_running
;
2703 struct lttng_pipe
*client_quit_pipe
;
2704 struct lttng_thread
*thread
= NULL
;
2705 int client_sock_fd
= -1;
2707 sem_init(&thread_state
.ready
, 0, 0);
2708 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2709 if (!client_quit_pipe
) {
2713 client_sock_fd
= create_client_sock();
2714 if (client_sock_fd
< 0) {
2718 thread_state
.client_sock
= client_sock_fd
;
2719 thread
= lttng_thread_create("Client management",
2720 thread_manage_clients
,
2721 shutdown_client_thread
,
2722 cleanup_client_thread
,
2727 /* The client thread now owns the client sock fd and the quit pipe. */
2728 client_sock_fd
= -1;
2729 client_quit_pipe
= NULL
;
2732 * This thread is part of the threads that need to be fully
2733 * initialized before the session daemon is marked as "ready".
2735 thread_running
= wait_thread_status();
2736 if (!thread_running
) {
2741 if (client_sock_fd
>= 0) {
2742 if (close(client_sock_fd
)) {
2743 PERROR("Failed to close client socket");
2746 lttng_thread_put(thread
);
2747 cleanup_client_thread(client_quit_pipe
);