2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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>
35 #include "agent-thread.h"
39 #include "health-sessiond.h"
41 #include "lttng-sessiond.h"
42 #include "manage-consumer.h"
44 #include "testpoint.h"
49 static struct thread_state
{
55 static void set_thread_status(bool running
)
57 DBG("Marking client thread's state as %s", running
? "running" : "error");
58 thread_state
.running
= running
;
59 sem_post(&thread_state
.ready
);
62 static bool wait_thread_status(void)
64 DBG("Waiting for client thread to be ready");
65 sem_wait(&thread_state
.ready
);
66 if (thread_state
.running
) {
67 DBG("Client thread is ready");
69 ERR("Initialization of client thread failed");
72 return thread_state
.running
;
76 * Setup the outgoing data buffer for the response (llm) by allocating the
77 * right amount of memory and copying the original information from the lsm
80 * Return 0 on success, negative value on error.
82 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
83 const void *payload_buf
, size_t payload_len
,
84 const void *cmd_header_buf
, size_t cmd_header_len
)
87 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
88 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
89 const struct lttcomm_lttng_msg llm
= {
90 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
91 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
92 .cmd_header_size
= cmd_header_len
,
93 .data_size
= payload_len
,
96 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
101 lttng_dynamic_pointer_array_clear(&cmd_ctx
->reply_payload
._fd_handles
);
103 cmd_ctx
->lttng_msg_size
= total_msg_size
;
105 /* Append reply header. */
106 ret
= lttng_dynamic_buffer_append(
107 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
112 /* Append command header. */
113 if (cmd_header_len
) {
114 ret
= lttng_dynamic_buffer_append(
115 &cmd_ctx
->reply_payload
.buffer
, cmd_header_buf
,
122 /* Append payload. */
124 ret
= lttng_dynamic_buffer_append(
125 &cmd_ctx
->reply_payload
.buffer
, payload_buf
,
136 static int setup_empty_lttng_msg(struct command_ctx
*cmd_ctx
)
139 const struct lttcomm_lttng_msg llm
= {};
141 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
146 /* Append place-holder reply header. */
147 ret
= lttng_dynamic_buffer_append(
148 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
153 cmd_ctx
->lttng_msg_size
= sizeof(llm
);
158 static void update_lttng_msg(struct command_ctx
*cmd_ctx
, size_t cmd_header_len
,
161 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
162 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
163 const struct lttcomm_lttng_msg llm
= {
164 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
165 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
166 .cmd_header_size
= cmd_header_len
,
167 .data_size
= payload_len
,
169 struct lttcomm_lttng_msg
*p_llm
;
171 assert(cmd_ctx
->reply_payload
.buffer
.size
>= sizeof(llm
));
173 p_llm
= (typeof(p_llm
)) cmd_ctx
->reply_payload
.buffer
.data
;
175 /* Update existing header. */
176 memcpy(p_llm
, &llm
, sizeof(llm
));
178 cmd_ctx
->lttng_msg_size
= total_msg_size
;
182 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
183 * exec or it will fail.
185 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
187 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
191 * Fork and exec a consumer daemon (consumerd).
193 * Return pid if successful else -1.
195 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
199 const char *consumer_to_use
;
200 const char *verbosity
;
203 DBG("Spawning consumerd");
210 if (the_config
.verbose_consumer
) {
211 verbosity
= "--verbose";
212 } else if (lttng_opt_quiet
) {
213 verbosity
= "--quiet";
218 switch (consumer_data
->type
) {
219 case LTTNG_CONSUMER_KERNEL
:
221 * Find out which consumerd to execute. We will first try the
222 * 64-bit path, then the sessiond's installation directory, and
223 * fallback on the 32-bit one,
225 DBG3("Looking for a kernel consumer at these locations:");
226 DBG3(" 1) %s", the_config
.consumerd64_bin_path
.value
? : "NULL");
227 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
228 DBG3(" 3) %s", the_config
.consumerd32_bin_path
.value
? : "NULL");
229 if (stat(the_config
.consumerd64_bin_path
.value
, &st
) == 0) {
230 DBG3("Found location #1");
231 consumer_to_use
= the_config
.consumerd64_bin_path
.value
;
232 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
233 DBG3("Found location #2");
234 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
235 } else if (the_config
.consumerd32_bin_path
.value
&&
236 stat(the_config
.consumerd32_bin_path
.value
, &st
) == 0) {
237 DBG3("Found location #3");
238 consumer_to_use
= the_config
.consumerd32_bin_path
.value
;
240 DBG("Could not find any valid consumerd executable");
244 DBG("Using kernel consumer at: %s", consumer_to_use
);
245 (void) execl(consumer_to_use
, "lttng-consumerd",
246 verbosity
, "-k", "--consumerd-cmd-sock",
247 consumer_data
->cmd_unix_sock_path
,
248 "--consumerd-err-sock",
249 consumer_data
->err_unix_sock_path
,
251 the_config
.tracing_group_name
.value
,
254 case LTTNG_CONSUMER64_UST
:
256 if (the_config
.consumerd64_lib_dir
.value
) {
261 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
265 tmplen
= strlen(the_config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
266 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
271 strcat(tmpnew
, the_config
.consumerd64_lib_dir
.value
);
272 if (tmp
[0] != '\0') {
276 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
283 DBG("Using 64-bit UST consumer at: %s",
284 the_config
.consumerd64_bin_path
.value
);
285 (void) execl(the_config
.consumerd64_bin_path
.value
,
286 "lttng-consumerd", verbosity
, "-u",
287 "--consumerd-cmd-sock",
288 consumer_data
->cmd_unix_sock_path
,
289 "--consumerd-err-sock",
290 consumer_data
->err_unix_sock_path
,
292 the_config
.tracing_group_name
.value
,
296 case LTTNG_CONSUMER32_UST
:
298 if (the_config
.consumerd32_lib_dir
.value
) {
303 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
307 tmplen
= strlen(the_config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
308 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
313 strcat(tmpnew
, the_config
.consumerd32_lib_dir
.value
);
314 if (tmp
[0] != '\0') {
318 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
325 DBG("Using 32-bit UST consumer at: %s",
326 the_config
.consumerd32_bin_path
.value
);
327 (void) execl(the_config
.consumerd32_bin_path
.value
,
328 "lttng-consumerd", verbosity
, "-u",
329 "--consumerd-cmd-sock",
330 consumer_data
->cmd_unix_sock_path
,
331 "--consumerd-err-sock",
332 consumer_data
->err_unix_sock_path
,
334 the_config
.tracing_group_name
.value
,
339 ERR("unknown consumer type");
343 PERROR("Consumer execl()");
345 /* Reaching this point, we got a failure on our execl(). */
347 } else if (pid
> 0) {
350 PERROR("start consumer fork");
358 * Spawn the consumerd daemon and session daemon thread.
360 static int start_consumerd(struct consumer_data
*consumer_data
)
365 * Set the listen() state on the socket since there is a possible race
366 * between the exec() of the consumer daemon and this call if place in the
367 * consumer thread. See bug #366 for more details.
369 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
374 pthread_mutex_lock(&consumer_data
->pid_mutex
);
375 if (consumer_data
->pid
!= 0) {
376 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
380 ret
= spawn_consumerd(consumer_data
);
382 ERR("Spawning consumerd failed");
383 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
387 /* Setting up the consumer_data pid */
388 consumer_data
->pid
= ret
;
389 DBG2("Consumer pid %d", consumer_data
->pid
);
390 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
392 DBG2("Spawning consumer control thread");
393 ret
= spawn_consumer_thread(consumer_data
);
395 ERR("Fatal error spawning consumer control thread");
403 /* Cleanup already created sockets on error. */
404 if (consumer_data
->err_sock
>= 0) {
407 err
= close(consumer_data
->err_sock
);
409 PERROR("close consumer data error socket");
416 * Copy consumer output from the tracing session to the domain session. The
417 * function also applies the right modification on a per domain basis for the
418 * trace files destination directory.
420 * Should *NOT* be called with RCU read-side lock held.
422 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
425 const char *dir_name
;
426 struct consumer_output
*consumer
;
429 assert(session
->consumer
);
432 case LTTNG_DOMAIN_KERNEL
:
433 DBG3("Copying tracing session consumer output in kernel session");
435 * XXX: We should audit the session creation and what this function
436 * does "extra" in order to avoid a destroy since this function is used
437 * in the domain session creation (kernel and ust) only. Same for UST
440 if (session
->kernel_session
->consumer
) {
441 consumer_output_put(session
->kernel_session
->consumer
);
443 session
->kernel_session
->consumer
=
444 consumer_copy_output(session
->consumer
);
445 /* Ease our life a bit for the next part */
446 consumer
= session
->kernel_session
->consumer
;
447 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
449 case LTTNG_DOMAIN_JUL
:
450 case LTTNG_DOMAIN_LOG4J
:
451 case LTTNG_DOMAIN_PYTHON
:
452 case LTTNG_DOMAIN_UST
:
453 DBG3("Copying tracing session consumer output in UST session");
454 if (session
->ust_session
->consumer
) {
455 consumer_output_put(session
->ust_session
->consumer
);
457 session
->ust_session
->consumer
=
458 consumer_copy_output(session
->consumer
);
459 /* Ease our life a bit for the next part */
460 consumer
= session
->ust_session
->consumer
;
461 dir_name
= DEFAULT_UST_TRACE_DIR
;
464 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
468 /* Append correct directory to subdir */
469 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
470 sizeof(consumer
->domain_subdir
));
475 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
483 * Create an UST session and add it to the session ust list.
485 * Should *NOT* be called with RCU read-side lock held.
487 static int create_ust_session(struct ltt_session
*session
,
488 const struct lttng_domain
*domain
)
491 struct ltt_ust_session
*lus
= NULL
;
495 assert(session
->consumer
);
497 switch (domain
->type
) {
498 case LTTNG_DOMAIN_JUL
:
499 case LTTNG_DOMAIN_LOG4J
:
500 case LTTNG_DOMAIN_PYTHON
:
501 case LTTNG_DOMAIN_UST
:
504 ERR("Unknown UST domain on create session %d", domain
->type
);
505 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
509 DBG("Creating UST session");
511 lus
= trace_ust_create_session(session
->id
);
513 ret
= LTTNG_ERR_UST_SESS_FAIL
;
517 lus
->uid
= session
->uid
;
518 lus
->gid
= session
->gid
;
519 lus
->output_traces
= session
->output_traces
;
520 lus
->snapshot_mode
= session
->snapshot_mode
;
521 lus
->live_timer_interval
= session
->live_timer
;
522 session
->ust_session
= lus
;
523 if (session
->shm_path
[0]) {
524 strncpy(lus
->root_shm_path
, session
->shm_path
,
525 sizeof(lus
->root_shm_path
));
526 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
527 strncpy(lus
->shm_path
, session
->shm_path
,
528 sizeof(lus
->shm_path
));
529 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
530 strncat(lus
->shm_path
, "/ust",
531 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
533 /* Copy session output to the newly created UST session */
534 ret
= copy_session_consumer(domain
->type
, session
);
535 if (ret
!= LTTNG_OK
) {
543 session
->ust_session
= NULL
;
548 * Create a kernel tracer session then create the default channel.
550 static int create_kernel_session(struct ltt_session
*session
)
554 DBG("Creating kernel session");
556 ret
= kernel_create_session(session
);
558 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
562 /* Code flow safety */
563 assert(session
->kernel_session
);
565 /* Copy session output to the newly created Kernel session */
566 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
567 if (ret
!= LTTNG_OK
) {
571 session
->kernel_session
->uid
= session
->uid
;
572 session
->kernel_session
->gid
= session
->gid
;
573 session
->kernel_session
->output_traces
= session
->output_traces
;
574 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
575 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
580 trace_kernel_destroy_session(session
->kernel_session
);
581 session
->kernel_session
= NULL
;
587 * Count number of session permitted by uid/gid.
589 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
592 struct ltt_session
*session
;
593 const struct ltt_session_list
*session_list
= session_get_list();
595 DBG("Counting number of available session for UID %d", uid
);
596 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
597 if (!session_get(session
)) {
600 session_lock(session
);
601 /* Only count the sessions the user can control. */
602 if (session_access_ok(session
, uid
) &&
603 !session
->destroyed
) {
606 session_unlock(session
);
607 session_put(session
);
612 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
613 int *sock_error
, struct lttng_event
*event
)
616 struct lttng_userspace_probe_location
*probe_location
;
617 struct lttng_payload probe_location_payload
;
618 struct fd_handle
*handle
= NULL
;
621 * Create a payload to store the serialized version of the probe
624 lttng_payload_init(&probe_location_payload
);
626 ret
= lttng_dynamic_buffer_set_size(&probe_location_payload
.buffer
,
627 cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
);
629 ret
= LTTNG_ERR_NOMEM
;
634 * Receive the probe location.
636 ret
= lttcomm_recv_unix_sock(sock
, probe_location_payload
.buffer
.data
,
637 probe_location_payload
.buffer
.size
);
639 DBG("Nothing recv() from client var len data... continuing");
641 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
646 * Receive the file descriptor to the target binary from the client.
648 DBG("Receiving userspace probe target FD from client ...");
649 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
651 DBG("Nothing recv() from client userspace probe fd... continuing");
653 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
657 handle
= fd_handle_create(fd
);
659 ret
= LTTNG_ERR_NOMEM
;
663 /* Transferred to the handle. */
666 ret
= lttng_payload_push_fd_handle(&probe_location_payload
, handle
);
668 ERR("Failed to add userspace probe file descriptor to payload");
669 ret
= LTTNG_ERR_NOMEM
;
673 fd_handle_put(handle
);
677 struct lttng_payload_view view
= lttng_payload_view_from_payload(
678 &probe_location_payload
, 0, -1);
680 /* Extract the probe location from the serialized version. */
681 ret
= lttng_userspace_probe_location_create_from_payload(
682 &view
, &probe_location
);
685 WARN("Failed to create a userspace probe location from the received buffer");
686 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
690 /* Attach the probe location to the event. */
691 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
693 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
700 PERROR("Failed to close userspace probe location binary fd");
704 fd_handle_put(handle
);
705 lttng_payload_reset(&probe_location_payload
);
709 static enum lttng_error_code
receive_lttng_trigger(struct command_ctx
*cmd_ctx
,
712 struct lttng_trigger
**_trigger
)
716 ssize_t sock_recv_len
;
717 enum lttng_error_code ret_code
;
718 struct lttng_payload trigger_payload
;
719 struct lttng_trigger
*trigger
= NULL
;
721 lttng_payload_init(&trigger_payload
);
722 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
723 ret
= lttng_dynamic_buffer_set_size(
724 &trigger_payload
.buffer
, trigger_len
);
726 ret_code
= LTTNG_ERR_NOMEM
;
730 sock_recv_len
= lttcomm_recv_unix_sock(
731 sock
, trigger_payload
.buffer
.data
, trigger_len
);
732 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
733 ERR("Failed to receive trigger in command payload");
735 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
739 /* Receive fds, if any. */
740 if (cmd_ctx
->lsm
.fd_count
> 0) {
741 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
742 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
743 if (sock_recv_len
> 0 &&
744 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
745 ERR("Failed to receive all file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
746 cmd_ctx
->lsm
.fd_count
, (int) ret
);
747 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
750 } else if (sock_recv_len
<= 0) {
751 ERR("Failed to receive file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
752 cmd_ctx
->lsm
.fd_count
, (int) ret
);
753 ret_code
= LTTNG_ERR_FATAL
;
759 /* Deserialize trigger. */
761 struct lttng_payload_view view
=
762 lttng_payload_view_from_payload(
763 &trigger_payload
, 0, -1);
765 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
767 ERR("Invalid trigger received as part of command payload");
768 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
769 lttng_trigger_put(trigger
);
778 lttng_payload_reset(&trigger_payload
);
782 static enum lttng_error_code
receive_lttng_error_query(struct command_ctx
*cmd_ctx
,
785 struct lttng_error_query
**_query
)
789 ssize_t sock_recv_len
;
790 enum lttng_error_code ret_code
;
791 struct lttng_payload query_payload
;
792 struct lttng_error_query
*query
= NULL
;
794 lttng_payload_init(&query_payload
);
795 query_len
= (size_t) cmd_ctx
->lsm
.u
.error_query
.length
;
796 ret
= lttng_dynamic_buffer_set_size(&query_payload
.buffer
, query_len
);
798 ret_code
= LTTNG_ERR_NOMEM
;
802 sock_recv_len
= lttcomm_recv_unix_sock(
803 sock
, query_payload
.buffer
.data
, query_len
);
804 if (sock_recv_len
< 0 || sock_recv_len
!= query_len
) {
805 ERR("Failed to receive error query in command payload");
807 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
811 /* Receive fds, if any. */
812 if (cmd_ctx
->lsm
.fd_count
> 0) {
813 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
814 sock
, cmd_ctx
->lsm
.fd_count
, &query_payload
);
815 if (sock_recv_len
> 0 &&
816 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
817 ERR("Failed to receive all file descriptors for error query in command payload: expected fd count = %u, ret = %d",
818 cmd_ctx
->lsm
.fd_count
, (int) ret
);
819 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
822 } else if (sock_recv_len
<= 0) {
823 ERR("Failed to receive file descriptors for error query in command payload: expected fd count = %u, ret = %d",
824 cmd_ctx
->lsm
.fd_count
, (int) ret
);
825 ret_code
= LTTNG_ERR_FATAL
;
831 /* Deserialize error query. */
833 struct lttng_payload_view view
=
834 lttng_payload_view_from_payload(
835 &query_payload
, 0, -1);
837 if (lttng_error_query_create_from_payload(&view
, &query
) !=
839 ERR("Invalid error query received as part of command payload");
840 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
849 lttng_payload_reset(&query_payload
);
854 * Version of setup_lttng_msg() without command header.
856 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
857 void *payload_buf
, size_t payload_len
)
859 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
863 * Check if the current kernel tracer supports the session rotation feature.
864 * Return 1 if it does, 0 otherwise.
866 static int check_rotate_compatible(void)
870 if (the_kernel_tracer_version
.major
!= 2 ||
871 the_kernel_tracer_version
.minor
< 11) {
872 DBG("Kernel tracer version is not compatible with the rotation feature");
880 * Send data on a unix socket using the liblttsessiondcomm API.
882 * Return lttcomm error code.
884 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
887 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
889 /* Check valid length */
890 if (view
->buffer
.size
== 0) {
895 ret
= lttcomm_send_unix_sock(
896 sock
, view
->buffer
.data
, view
->buffer
.size
);
902 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
913 * Process the command requested by the lttng client within the command
914 * context structure. This function make sure that the return structure (llm)
915 * is set and ready for transmission before returning.
917 * Return any error encountered or 0 for success.
919 * "sock" is only used for special-case var. len data.
920 * A command may assume the ownership of the socket, in which case its value
921 * should be set to -1.
923 * Should *NOT* be called with RCU read-side lock held.
925 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
929 bool need_tracing_session
= true;
933 DBG("Processing client command '%s\' (%d)",
934 lttcomm_sessiond_command_str(cmd_ctx
->lsm
.cmd_type
),
935 cmd_ctx
->lsm
.cmd_type
);
937 assert(!rcu_read_ongoing());
941 switch (cmd_ctx
->lsm
.cmd_type
) {
942 case LTTNG_CREATE_SESSION_EXT
:
943 case LTTNG_DESTROY_SESSION
:
944 case LTTNG_LIST_SESSIONS
:
945 case LTTNG_LIST_DOMAINS
:
946 case LTTNG_START_TRACE
:
947 case LTTNG_STOP_TRACE
:
948 case LTTNG_DATA_PENDING
:
949 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
950 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
951 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
952 case LTTNG_SNAPSHOT_RECORD
:
953 case LTTNG_SAVE_SESSION
:
954 case LTTNG_SET_SESSION_SHM_PATH
:
955 case LTTNG_REGENERATE_METADATA
:
956 case LTTNG_REGENERATE_STATEDUMP
:
957 case LTTNG_ROTATE_SESSION
:
958 case LTTNG_ROTATION_GET_INFO
:
959 case LTTNG_ROTATION_SET_SCHEDULE
:
960 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
961 case LTTNG_CLEAR_SESSION
:
962 case LTTNG_LIST_TRIGGERS
:
963 case LTTNG_EXECUTE_ERROR_QUERY
:
970 /* Needs a functioning consumerd? */
971 switch (cmd_ctx
->lsm
.cmd_type
) {
972 case LTTNG_REGISTER_TRIGGER
:
973 case LTTNG_UNREGISTER_TRIGGER
:
974 case LTTNG_EXECUTE_ERROR_QUERY
:
975 need_consumerd
= false;
978 need_consumerd
= true;
982 if (the_config
.no_kernel
&& need_domain
&&
983 cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
985 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
987 ret
= LTTNG_ERR_KERN_NA
;
992 /* Deny register consumer if we already have a spawned consumer. */
993 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
994 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
995 if (the_kconsumer_data
.pid
> 0) {
996 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
997 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1000 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1004 * Check for command that don't needs to allocate a returned payload. We do
1005 * this here so we don't have to make the call for no payload at each
1008 switch(cmd_ctx
->lsm
.cmd_type
) {
1009 case LTTNG_LIST_SESSIONS
:
1010 case LTTNG_LIST_TRACEPOINTS
:
1011 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1012 case LTTNG_LIST_DOMAINS
:
1013 case LTTNG_LIST_CHANNELS
:
1014 case LTTNG_LIST_EVENTS
:
1015 case LTTNG_LIST_SYSCALLS
:
1016 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1017 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1018 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1019 case LTTNG_DATA_PENDING
:
1020 case LTTNG_ROTATE_SESSION
:
1021 case LTTNG_ROTATION_GET_INFO
:
1022 case LTTNG_REGISTER_TRIGGER
:
1023 case LTTNG_LIST_TRIGGERS
:
1024 case LTTNG_EXECUTE_ERROR_QUERY
:
1027 /* Setup lttng message with no payload */
1028 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
1030 /* This label does not try to unlock the session */
1031 goto init_setup_error
;
1035 /* Commands that DO NOT need a session. */
1036 switch (cmd_ctx
->lsm
.cmd_type
) {
1037 case LTTNG_CREATE_SESSION_EXT
:
1038 case LTTNG_LIST_SESSIONS
:
1039 case LTTNG_LIST_TRACEPOINTS
:
1040 case LTTNG_LIST_SYSCALLS
:
1041 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1042 case LTTNG_SAVE_SESSION
:
1043 case LTTNG_REGISTER_TRIGGER
:
1044 case LTTNG_UNREGISTER_TRIGGER
:
1045 case LTTNG_LIST_TRIGGERS
:
1046 case LTTNG_EXECUTE_ERROR_QUERY
:
1047 need_tracing_session
= false;
1050 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
1052 * We keep the session list lock across _all_ commands
1053 * for now, because the per-session lock does not
1054 * handle teardown properly.
1056 session_lock_list();
1057 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
1058 if (cmd_ctx
->session
== NULL
) {
1059 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1062 /* Acquire lock for the session */
1063 session_lock(cmd_ctx
->session
);
1069 * Commands that need a valid session but should NOT create one if none
1070 * exists. Instead of creating one and destroying it when the command is
1071 * handled, process that right before so we save some round trip in useless
1074 switch (cmd_ctx
->lsm
.cmd_type
) {
1075 case LTTNG_DISABLE_CHANNEL
:
1076 case LTTNG_DISABLE_EVENT
:
1077 switch (cmd_ctx
->lsm
.domain
.type
) {
1078 case LTTNG_DOMAIN_KERNEL
:
1079 if (!cmd_ctx
->session
->kernel_session
) {
1080 ret
= LTTNG_ERR_NO_CHANNEL
;
1084 case LTTNG_DOMAIN_JUL
:
1085 case LTTNG_DOMAIN_LOG4J
:
1086 case LTTNG_DOMAIN_PYTHON
:
1087 case LTTNG_DOMAIN_UST
:
1088 if (!cmd_ctx
->session
->ust_session
) {
1089 ret
= LTTNG_ERR_NO_CHANNEL
;
1094 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1106 * Check domain type for specific "pre-action".
1108 switch (cmd_ctx
->lsm
.domain
.type
) {
1109 case LTTNG_DOMAIN_KERNEL
:
1111 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1115 /* Kernel tracer check */
1116 if (!kernel_tracer_is_initialized()) {
1117 /* Basically, load kernel tracer modules */
1118 ret
= init_kernel_tracer();
1124 /* Consumer is in an ERROR state. Report back to client */
1125 if (need_consumerd
&& uatomic_read(&the_kernel_consumerd_state
) ==
1127 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1131 /* Need a session for kernel command */
1132 if (need_tracing_session
) {
1133 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1134 ret
= create_kernel_session(cmd_ctx
->session
);
1135 if (ret
!= LTTNG_OK
) {
1136 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1141 /* Start the kernel consumer daemon */
1142 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1143 if (the_kconsumer_data
.pid
== 0 &&
1144 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1145 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1146 ret
= start_consumerd(&the_kconsumer_data
);
1148 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1151 uatomic_set(&the_kernel_consumerd_state
, CONSUMER_STARTED
);
1153 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1157 * The consumer was just spawned so we need to add the socket to
1158 * the consumer output of the session if exist.
1160 ret
= consumer_create_socket(&the_kconsumer_data
,
1161 cmd_ctx
->session
->kernel_session
->consumer
);
1168 case LTTNG_DOMAIN_JUL
:
1169 case LTTNG_DOMAIN_LOG4J
:
1170 case LTTNG_DOMAIN_PYTHON
:
1171 if (!agent_tracing_is_enabled()) {
1172 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1176 case LTTNG_DOMAIN_UST
:
1178 if (!ust_app_supported()) {
1179 ret
= LTTNG_ERR_NO_UST
;
1183 /* Consumer is in an ERROR state. Report back to client */
1184 if (need_consumerd
&&
1185 uatomic_read(&the_ust_consumerd_state
) ==
1187 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1191 if (need_tracing_session
) {
1192 /* Create UST session if none exist. */
1193 if (cmd_ctx
->session
->ust_session
== NULL
) {
1194 ret
= create_ust_session(cmd_ctx
->session
,
1195 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
));
1196 if (ret
!= LTTNG_OK
) {
1201 /* Start the UST consumer daemons */
1203 pthread_mutex_lock(&the_ustconsumer64_data
.pid_mutex
);
1204 if (the_config
.consumerd64_bin_path
.value
&&
1205 the_ustconsumer64_data
.pid
== 0 &&
1206 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1207 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1208 ret
= start_consumerd(&the_ustconsumer64_data
);
1210 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1211 uatomic_set(&the_ust_consumerd64_fd
, -EINVAL
);
1215 uatomic_set(&the_ust_consumerd64_fd
, the_ustconsumer64_data
.cmd_sock
);
1216 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1218 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1222 * Setup socket for consumer 64 bit. No need for atomic access
1223 * since it was set above and can ONLY be set in this thread.
1225 ret
= consumer_create_socket(&the_ustconsumer64_data
,
1226 cmd_ctx
->session
->ust_session
->consumer
);
1232 pthread_mutex_lock(&the_ustconsumer32_data
.pid_mutex
);
1233 if (the_config
.consumerd32_bin_path
.value
&&
1234 the_ustconsumer32_data
.pid
== 0 &&
1235 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1236 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1237 ret
= start_consumerd(&the_ustconsumer32_data
);
1239 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1240 uatomic_set(&the_ust_consumerd32_fd
, -EINVAL
);
1244 uatomic_set(&the_ust_consumerd32_fd
, the_ustconsumer32_data
.cmd_sock
);
1245 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1247 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1251 * Setup socket for consumer 32 bit. No need for atomic access
1252 * since it was set above and can ONLY be set in this thread.
1254 ret
= consumer_create_socket(&the_ustconsumer32_data
,
1255 cmd_ctx
->session
->ust_session
->consumer
);
1267 /* Validate consumer daemon state when start/stop trace command */
1268 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1269 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1270 switch (cmd_ctx
->lsm
.domain
.type
) {
1271 case LTTNG_DOMAIN_NONE
:
1273 case LTTNG_DOMAIN_JUL
:
1274 case LTTNG_DOMAIN_LOG4J
:
1275 case LTTNG_DOMAIN_PYTHON
:
1276 case LTTNG_DOMAIN_UST
:
1277 if (uatomic_read(&the_ust_consumerd_state
) != CONSUMER_STARTED
) {
1278 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1282 case LTTNG_DOMAIN_KERNEL
:
1283 if (uatomic_read(&the_kernel_consumerd_state
) != CONSUMER_STARTED
) {
1284 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1289 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1295 * Check that the UID matches that of the tracing session.
1296 * The root user can interact with all sessions.
1298 if (need_tracing_session
) {
1299 if (!session_access_ok(cmd_ctx
->session
,
1300 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1301 cmd_ctx
->session
->destroyed
) {
1302 ret
= LTTNG_ERR_EPERM
;
1308 * Send relayd information to consumer as soon as we have a domain and a
1311 if (cmd_ctx
->session
&& need_domain
) {
1313 * Setup relayd if not done yet. If the relayd information was already
1314 * sent to the consumer, this call will gracefully return.
1316 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1317 if (ret
!= LTTNG_OK
) {
1322 /* Process by command type */
1323 switch (cmd_ctx
->lsm
.cmd_type
) {
1324 case LTTNG_ADD_CONTEXT
:
1327 * An LTTNG_ADD_CONTEXT command might have a supplementary
1328 * payload if the context being added is an application context.
1330 if (cmd_ctx
->lsm
.u
.context
.ctx
.ctx
==
1331 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1332 char *provider_name
= NULL
, *context_name
= NULL
;
1333 size_t provider_name_len
=
1334 cmd_ctx
->lsm
.u
.context
.provider_name_len
;
1335 size_t context_name_len
=
1336 cmd_ctx
->lsm
.u
.context
.context_name_len
;
1338 if (provider_name_len
== 0 || context_name_len
== 0) {
1340 * Application provider and context names MUST
1343 ret
= -LTTNG_ERR_INVALID
;
1347 provider_name
= zmalloc(provider_name_len
+ 1);
1348 if (!provider_name
) {
1349 ret
= -LTTNG_ERR_NOMEM
;
1352 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
=
1355 context_name
= zmalloc(context_name_len
+ 1);
1356 if (!context_name
) {
1357 ret
= -LTTNG_ERR_NOMEM
;
1358 goto error_add_context
;
1360 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1363 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1366 goto error_add_context
;
1369 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1372 goto error_add_context
;
1377 * cmd_add_context assumes ownership of the provider and context
1380 ret
= cmd_add_context(cmd_ctx
->session
,
1381 cmd_ctx
->lsm
.domain
.type
,
1382 cmd_ctx
->lsm
.u
.context
.channel_name
,
1383 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.context
.ctx
),
1384 the_kernel_poll_pipe
[1]);
1386 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1387 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1389 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
);
1390 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1396 case LTTNG_DISABLE_CHANNEL
:
1398 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1399 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1402 case LTTNG_DISABLE_EVENT
:
1406 * FIXME: handle filter; for now we just receive the filter's
1407 * bytecode along with the filter expression which are sent by
1408 * liblttng-ctl and discard them.
1410 * This fixes an issue where the client may block while sending
1411 * the filter payload and encounter an error because the session
1412 * daemon closes the socket without ever handling this data.
1414 size_t count
= cmd_ctx
->lsm
.u
.disable
.expression_len
+
1415 cmd_ctx
->lsm
.u
.disable
.bytecode_len
;
1418 char data
[LTTNG_FILTER_MAX_LEN
];
1420 DBG("Discarding disable event command payload of size %zu", count
);
1422 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1423 count
> sizeof(data
) ? sizeof(data
) : count
);
1428 count
-= (size_t) ret
;
1431 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1432 cmd_ctx
->lsm
.u
.disable
.channel_name
,
1433 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.disable
.event
));
1436 case LTTNG_ENABLE_CHANNEL
:
1438 cmd_ctx
->lsm
.u
.channel
.chan
.attr
.extended
.ptr
=
1439 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
.u
.channel
.extended
;
1440 ret
= cmd_enable_channel(cmd_ctx
->session
,
1441 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1442 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.channel
.chan
),
1443 the_kernel_poll_pipe
[1]);
1446 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1447 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1449 struct lttng_dynamic_buffer payload
;
1450 struct lttng_buffer_view payload_view
;
1451 const bool add_value
=
1452 cmd_ctx
->lsm
.cmd_type
==
1453 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1454 const size_t name_len
=
1455 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1457 const enum lttng_domain_type domain_type
=
1458 (enum lttng_domain_type
)
1459 cmd_ctx
->lsm
.domain
.type
;
1460 const enum lttng_process_attr process_attr
=
1461 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1462 .process_attr_tracker_add_remove_include_value
1464 const enum lttng_process_attr_value_type value_type
=
1465 (enum lttng_process_attr_value_type
) cmd_ctx
1467 .process_attr_tracker_add_remove_include_value
1469 struct process_attr_value
*value
;
1470 enum lttng_error_code ret_code
;
1471 long login_name_max
;
1473 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1474 if (login_name_max
< 0) {
1475 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1476 ret
= LTTNG_ERR_INVALID
;
1480 /* Receive remaining variable length payload if applicable. */
1481 if (name_len
> login_name_max
) {
1483 * POSIX mandates user and group names that are at least
1484 * 8 characters long. Note that although shadow-utils
1485 * (useradd, groupaadd, etc.) use 32 chars as their
1486 * limit (from bits/utmp.h, UT_NAMESIZE),
1487 * LOGIN_NAME_MAX is defined to 256.
1489 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1490 add_value
? "addition" : "removal",
1491 name_len
, login_name_max
);
1492 ret
= LTTNG_ERR_INVALID
;
1496 lttng_dynamic_buffer_init(&payload
);
1497 if (name_len
!= 0) {
1499 * Receive variable payload for user/group name
1502 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1504 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1505 add_value
? "add" : "remove");
1506 ret
= LTTNG_ERR_NOMEM
;
1507 goto error_add_remove_tracker_value
;
1510 ret
= lttcomm_recv_unix_sock(
1511 *sock
, payload
.data
, name_len
);
1513 ERR("Failed to receive payload of %s process attribute tracker value argument",
1514 add_value
? "add" : "remove");
1516 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1517 goto error_add_remove_tracker_value
;
1521 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1522 &payload
, 0, name_len
);
1523 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1524 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1525 goto error_add_remove_tracker_value
;
1529 * Validate the value type and domains are legal for the process
1530 * attribute tracker that is specified and convert the value to
1531 * add/remove to the internal sessiond representation.
1533 ret_code
= process_attr_value_from_comm(domain_type
,
1534 process_attr
, value_type
,
1535 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1537 &payload_view
, &value
);
1538 if (ret_code
!= LTTNG_OK
) {
1540 goto error_add_remove_tracker_value
;
1544 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1545 cmd_ctx
->session
, domain_type
,
1546 process_attr
, value
);
1548 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1549 cmd_ctx
->session
, domain_type
,
1550 process_attr
, value
);
1552 process_attr_value_destroy(value
);
1553 error_add_remove_tracker_value
:
1554 lttng_dynamic_buffer_reset(&payload
);
1557 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1559 enum lttng_tracking_policy tracking_policy
;
1560 const enum lttng_domain_type domain_type
=
1561 (enum lttng_domain_type
)
1562 cmd_ctx
->lsm
.domain
.type
;
1563 const enum lttng_process_attr process_attr
=
1564 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1565 .process_attr_tracker_get_tracking_policy
1568 ret
= cmd_process_attr_tracker_get_tracking_policy(
1569 cmd_ctx
->session
, domain_type
, process_attr
,
1571 if (ret
!= LTTNG_OK
) {
1575 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1576 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1578 ret
= LTTNG_ERR_NOMEM
;
1584 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1586 const enum lttng_tracking_policy tracking_policy
=
1587 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1588 .process_attr_tracker_set_tracking_policy
1590 const enum lttng_domain_type domain_type
=
1591 (enum lttng_domain_type
)
1592 cmd_ctx
->lsm
.domain
.type
;
1593 const enum lttng_process_attr process_attr
=
1594 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1595 .process_attr_tracker_set_tracking_policy
1598 ret
= cmd_process_attr_tracker_set_tracking_policy(
1599 cmd_ctx
->session
, domain_type
, process_attr
,
1601 if (ret
!= LTTNG_OK
) {
1606 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1608 struct lttng_process_attr_values
*values
;
1609 struct lttng_dynamic_buffer reply
;
1610 const enum lttng_domain_type domain_type
=
1611 (enum lttng_domain_type
)
1612 cmd_ctx
->lsm
.domain
.type
;
1613 const enum lttng_process_attr process_attr
=
1614 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1615 .process_attr_tracker_get_inclusion_set
1618 ret
= cmd_process_attr_tracker_get_inclusion_set(
1619 cmd_ctx
->session
, domain_type
, process_attr
,
1621 if (ret
!= LTTNG_OK
) {
1625 lttng_dynamic_buffer_init(&reply
);
1626 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1628 goto error_tracker_get_inclusion_set
;
1631 ret
= setup_lttng_msg_no_cmd_header(
1632 cmd_ctx
, reply
.data
, reply
.size
);
1634 ret
= LTTNG_ERR_NOMEM
;
1635 goto error_tracker_get_inclusion_set
;
1639 error_tracker_get_inclusion_set
:
1640 lttng_process_attr_values_destroy(values
);
1641 lttng_dynamic_buffer_reset(&reply
);
1644 case LTTNG_ENABLE_EVENT
:
1646 struct lttng_event
*ev
= NULL
;
1647 struct lttng_event_exclusion
*exclusion
= NULL
;
1648 struct lttng_bytecode
*bytecode
= NULL
;
1649 char *filter_expression
= NULL
;
1651 /* Handle exclusion events and receive it from the client. */
1652 if (cmd_ctx
->lsm
.u
.enable
.exclusion_count
> 0) {
1653 size_t count
= cmd_ctx
->lsm
.u
.enable
.exclusion_count
;
1655 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1656 (count
* LTTNG_SYMBOL_NAME_LEN
));
1658 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1662 DBG("Receiving var len exclusion event list from client ...");
1663 exclusion
->count
= count
;
1664 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1665 count
* LTTNG_SYMBOL_NAME_LEN
);
1667 DBG("Nothing recv() from client var len data... continuing");
1670 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1675 /* Get filter expression from client. */
1676 if (cmd_ctx
->lsm
.u
.enable
.expression_len
> 0) {
1677 size_t expression_len
=
1678 cmd_ctx
->lsm
.u
.enable
.expression_len
;
1680 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1681 ret
= LTTNG_ERR_FILTER_INVAL
;
1686 filter_expression
= zmalloc(expression_len
);
1687 if (!filter_expression
) {
1689 ret
= LTTNG_ERR_FILTER_NOMEM
;
1693 /* Receive var. len. data */
1694 DBG("Receiving var len filter's expression from client ...");
1695 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1698 DBG("Nothing recv() from client var len data... continuing");
1700 free(filter_expression
);
1702 ret
= LTTNG_ERR_FILTER_INVAL
;
1707 /* Handle filter and get bytecode from client. */
1708 if (cmd_ctx
->lsm
.u
.enable
.bytecode_len
> 0) {
1709 size_t bytecode_len
= cmd_ctx
->lsm
.u
.enable
.bytecode_len
;
1711 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1712 ret
= LTTNG_ERR_FILTER_INVAL
;
1713 free(filter_expression
);
1718 bytecode
= zmalloc(bytecode_len
);
1720 free(filter_expression
);
1722 ret
= LTTNG_ERR_FILTER_NOMEM
;
1726 /* Receive var. len. data */
1727 DBG("Receiving var len filter's bytecode from client ...");
1728 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1730 DBG("Nothing recv() from client var len data... continuing");
1732 free(filter_expression
);
1735 ret
= LTTNG_ERR_FILTER_INVAL
;
1739 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1740 free(filter_expression
);
1743 ret
= LTTNG_ERR_FILTER_INVAL
;
1748 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.enable
.event
));
1750 DBG("Failed to copy event: %s",
1751 cmd_ctx
->lsm
.u
.enable
.event
.name
);
1752 free(filter_expression
);
1755 ret
= LTTNG_ERR_NOMEM
;
1760 if (cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
> 0) {
1761 /* Expect a userspace probe description. */
1762 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1764 free(filter_expression
);
1767 lttng_event_destroy(ev
);
1772 ret
= cmd_enable_event(cmd_ctx
->session
,
1773 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1774 cmd_ctx
->lsm
.u
.enable
.channel_name
,
1776 filter_expression
, bytecode
, exclusion
,
1777 the_kernel_poll_pipe
[1]);
1778 lttng_event_destroy(ev
);
1781 case LTTNG_LIST_TRACEPOINTS
:
1783 struct lttng_event
*events
;
1786 session_lock_list();
1787 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &events
);
1788 session_unlock_list();
1789 if (nb_events
< 0) {
1790 /* Return value is a negative lttng_error_code. */
1796 * Setup lttng message with payload size set to the event list size in
1797 * bytes and then copy list into the llm payload.
1799 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1800 sizeof(struct lttng_event
) * nb_events
);
1810 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1812 struct lttng_event_field
*fields
;
1815 session_lock_list();
1816 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1818 session_unlock_list();
1819 if (nb_fields
< 0) {
1820 /* Return value is a negative lttng_error_code. */
1826 * Setup lttng message with payload size set to the event list size in
1827 * bytes and then copy list into the llm payload.
1829 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1830 sizeof(struct lttng_event_field
) * nb_fields
);
1840 case LTTNG_LIST_SYSCALLS
:
1842 struct lttng_event
*events
;
1845 nb_events
= cmd_list_syscalls(&events
);
1846 if (nb_events
< 0) {
1847 /* Return value is a negative lttng_error_code. */
1853 * Setup lttng message with payload size set to the event list size in
1854 * bytes and then copy list into the llm payload.
1856 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1857 sizeof(struct lttng_event
) * nb_events
);
1867 case LTTNG_SET_CONSUMER_URI
:
1870 struct lttng_uri
*uris
;
1872 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1873 len
= nb_uri
* sizeof(struct lttng_uri
);
1876 ret
= LTTNG_ERR_INVALID
;
1880 uris
= zmalloc(len
);
1882 ret
= LTTNG_ERR_FATAL
;
1886 /* Receive variable len data */
1887 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1888 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1890 DBG("No URIs received from client... continuing");
1892 ret
= LTTNG_ERR_SESSION_FAIL
;
1897 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1899 if (ret
!= LTTNG_OK
) {
1906 case LTTNG_START_TRACE
:
1909 * On the first start, if we have a kernel session and we have
1910 * enabled time or size-based rotations, we have to make sure
1911 * the kernel tracer supports it.
1913 if (!cmd_ctx
->session
->has_been_started
&& \
1914 cmd_ctx
->session
->kernel_session
&& \
1915 (cmd_ctx
->session
->rotate_timer_period
|| \
1916 cmd_ctx
->session
->rotate_size
) && \
1917 !check_rotate_compatible()) {
1918 DBG("Kernel tracer version is not compatible with the rotation feature");
1919 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1922 ret
= cmd_start_trace(cmd_ctx
->session
);
1925 case LTTNG_STOP_TRACE
:
1927 ret
= cmd_stop_trace(cmd_ctx
->session
);
1930 case LTTNG_DESTROY_SESSION
:
1932 ret
= cmd_destroy_session(cmd_ctx
->session
,
1933 the_notification_thread_handle
, sock
);
1936 case LTTNG_LIST_DOMAINS
:
1939 struct lttng_domain
*domains
= NULL
;
1941 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1943 /* Return value is a negative lttng_error_code. */
1948 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1949 nb_dom
* sizeof(struct lttng_domain
));
1959 case LTTNG_LIST_CHANNELS
:
1961 ssize_t payload_size
;
1962 struct lttng_channel
*channels
= NULL
;
1964 payload_size
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1965 cmd_ctx
->session
, &channels
);
1966 if (payload_size
< 0) {
1967 /* Return value is a negative lttng_error_code. */
1968 ret
= -payload_size
;
1972 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1983 case LTTNG_LIST_EVENTS
:
1986 struct lttcomm_event_command_header cmd_header
= {};
1987 size_t original_payload_size
;
1988 size_t payload_size
;
1990 ret
= setup_empty_lttng_msg(cmd_ctx
);
1992 ret
= LTTNG_ERR_NOMEM
;
1996 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1998 /* Extended infos are included at the end of the payload. */
1999 list_ret
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
2001 cmd_ctx
->lsm
.u
.list
.channel_name
,
2002 &cmd_ctx
->reply_payload
);
2004 /* Return value is a negative lttng_error_code. */
2009 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2010 sizeof(cmd_header
) - original_payload_size
;
2011 update_lttng_msg(cmd_ctx
, sizeof(cmd_header
), payload_size
);
2016 case LTTNG_LIST_SESSIONS
:
2018 unsigned int nr_sessions
;
2019 void *sessions_payload
;
2022 session_lock_list();
2023 nr_sessions
= lttng_sessions_count(
2024 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2025 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2027 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
2028 (sizeof(struct lttng_session_extended
) * nr_sessions
);
2029 sessions_payload
= zmalloc(payload_len
);
2031 if (!sessions_payload
) {
2032 session_unlock_list();
2037 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
2038 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2039 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2040 session_unlock_list();
2042 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
2044 free(sessions_payload
);
2053 case LTTNG_REGISTER_CONSUMER
:
2055 struct consumer_data
*cdata
;
2057 switch (cmd_ctx
->lsm
.domain
.type
) {
2058 case LTTNG_DOMAIN_KERNEL
:
2059 cdata
= &the_kconsumer_data
;
2062 ret
= LTTNG_ERR_UND
;
2066 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
2067 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
2070 case LTTNG_DATA_PENDING
:
2073 uint8_t pending_ret_byte
;
2075 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
2080 * This function may returns 0 or 1 to indicate whether or not
2081 * there is data pending. In case of error, it should return an
2082 * LTTNG_ERR code. However, some code paths may still return
2083 * a nondescript error code, which we handle by returning an
2086 if (pending_ret
== 0 || pending_ret
== 1) {
2088 * ret will be set to LTTNG_OK at the end of
2091 } else if (pending_ret
< 0) {
2092 ret
= LTTNG_ERR_UNK
;
2099 pending_ret_byte
= (uint8_t) pending_ret
;
2101 /* 1 byte to return whether or not data is pending */
2102 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
2103 &pending_ret_byte
, 1);
2112 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2114 uint32_t snapshot_id
;
2115 struct lttcomm_lttng_output_id reply
;
2117 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
2118 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
),
2120 if (ret
!= LTTNG_OK
) {
2123 reply
.id
= snapshot_id
;
2125 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
2131 /* Copy output list into message payload */
2135 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2137 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
2138 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
));
2141 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2144 struct lttng_snapshot_output
*outputs
= NULL
;
2146 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
2147 if (nb_output
< 0) {
2152 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
2153 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
2154 nb_output
* sizeof(struct lttng_snapshot_output
));
2164 case LTTNG_SNAPSHOT_RECORD
:
2166 ret
= cmd_snapshot_record(cmd_ctx
->session
,
2167 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_record
.output
),
2168 cmd_ctx
->lsm
.u
.snapshot_record
.wait
);
2171 case LTTNG_CREATE_SESSION_EXT
:
2173 struct lttng_dynamic_buffer payload
;
2174 struct lttng_session_descriptor
*return_descriptor
= NULL
;
2176 lttng_dynamic_buffer_init(&payload
);
2177 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2178 if (ret
!= LTTNG_OK
) {
2182 ret
= lttng_session_descriptor_serialize(return_descriptor
,
2185 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2186 lttng_session_descriptor_destroy(return_descriptor
);
2187 ret
= LTTNG_ERR_NOMEM
;
2190 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
2193 lttng_session_descriptor_destroy(return_descriptor
);
2194 ret
= LTTNG_ERR_NOMEM
;
2197 lttng_dynamic_buffer_reset(&payload
);
2198 lttng_session_descriptor_destroy(return_descriptor
);
2202 case LTTNG_SAVE_SESSION
:
2204 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
2208 case LTTNG_SET_SESSION_SHM_PATH
:
2210 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2211 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2214 case LTTNG_REGENERATE_METADATA
:
2216 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2219 case LTTNG_REGENERATE_STATEDUMP
:
2221 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2224 case LTTNG_REGISTER_TRIGGER
:
2226 struct lttng_trigger
*payload_trigger
;
2227 struct lttng_trigger
*return_trigger
;
2228 size_t original_reply_payload_size
;
2229 size_t reply_payload_size
;
2230 const struct lttng_credentials cmd_creds
= {
2231 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2232 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2235 ret
= setup_empty_lttng_msg(cmd_ctx
);
2237 ret
= LTTNG_ERR_NOMEM
;
2241 ret
= receive_lttng_trigger(
2242 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2243 if (ret
!= LTTNG_OK
) {
2247 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2249 ret
= cmd_register_trigger(&cmd_creds
, payload_trigger
,
2250 cmd_ctx
->lsm
.u
.trigger
.is_trigger_anonymous
,
2251 the_notification_thread_handle
,
2253 if (ret
!= LTTNG_OK
) {
2254 lttng_trigger_put(payload_trigger
);
2258 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2259 lttng_trigger_put(payload_trigger
);
2260 lttng_trigger_put(return_trigger
);
2262 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2263 ret
= LTTNG_ERR_NOMEM
;
2267 reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2268 original_reply_payload_size
;
2270 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2275 case LTTNG_UNREGISTER_TRIGGER
:
2277 struct lttng_trigger
*payload_trigger
;
2278 const struct lttng_credentials cmd_creds
= {
2279 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2280 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2283 ret
= receive_lttng_trigger(
2284 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2285 if (ret
!= LTTNG_OK
) {
2289 ret
= cmd_unregister_trigger(&cmd_creds
, payload_trigger
,
2290 the_notification_thread_handle
);
2291 lttng_trigger_put(payload_trigger
);
2294 case LTTNG_ROTATE_SESSION
:
2296 struct lttng_rotate_session_return rotate_return
;
2298 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2300 memset(&rotate_return
, 0, sizeof(rotate_return
));
2301 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2302 DBG("Kernel tracer version is not compatible with the rotation feature");
2303 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2307 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2309 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2315 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2316 sizeof(rotate_return
));
2325 case LTTNG_ROTATION_GET_INFO
:
2327 struct lttng_rotation_get_info_return get_info_return
;
2329 memset(&get_info_return
, 0, sizeof(get_info_return
));
2330 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2331 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2337 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2338 sizeof(get_info_return
));
2347 case LTTNG_ROTATION_SET_SCHEDULE
:
2350 enum lttng_rotation_schedule_type schedule_type
;
2353 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2354 DBG("Kernel tracer version does not support session rotations");
2355 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2359 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2360 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2361 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2363 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
, set_schedule
,
2364 schedule_type
, value
,
2365 the_notification_thread_handle
);
2366 if (ret
!= LTTNG_OK
) {
2372 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2374 struct lttng_session_list_schedules_return schedules
= {
2375 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2376 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2377 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2378 .size
.value
= cmd_ctx
->session
->rotate_size
,
2381 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2391 case LTTNG_CLEAR_SESSION
:
2393 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2396 case LTTNG_LIST_TRIGGERS
:
2398 struct lttng_triggers
*return_triggers
= NULL
;
2399 size_t original_payload_size
;
2400 size_t payload_size
;
2402 ret
= setup_empty_lttng_msg(cmd_ctx
);
2404 ret
= LTTNG_ERR_NOMEM
;
2408 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2410 ret
= cmd_list_triggers(cmd_ctx
, the_notification_thread_handle
,
2412 if (ret
!= LTTNG_OK
) {
2416 assert(return_triggers
);
2417 ret
= lttng_triggers_serialize(
2418 return_triggers
, &cmd_ctx
->reply_payload
);
2419 lttng_triggers_destroy(return_triggers
);
2421 ERR("Failed to serialize triggers in reply to `list triggers` command");
2422 ret
= LTTNG_ERR_NOMEM
;
2426 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2427 original_payload_size
;
2429 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2434 case LTTNG_EXECUTE_ERROR_QUERY
:
2436 struct lttng_error_query
*query
;
2437 const struct lttng_credentials cmd_creds
= {
2438 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2439 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2441 struct lttng_error_query_results
*results
= NULL
;
2442 size_t original_payload_size
;
2443 size_t payload_size
;
2445 ret
= setup_empty_lttng_msg(cmd_ctx
);
2447 ret
= LTTNG_ERR_NOMEM
;
2451 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2453 ret
= receive_lttng_error_query(
2454 cmd_ctx
, *sock
, sock_error
, &query
);
2455 if (ret
!= LTTNG_OK
) {
2459 ret
= cmd_execute_error_query(&cmd_creds
, query
, &results
,
2460 the_notification_thread_handle
);
2461 lttng_error_query_destroy(query
);
2462 if (ret
!= LTTNG_OK
) {
2467 ret
= lttng_error_query_results_serialize(
2468 results
, &cmd_ctx
->reply_payload
);
2469 lttng_error_query_results_destroy(results
);
2471 ERR("Failed to serialize error query result set in reply to `execute error query` command");
2472 ret
= LTTNG_ERR_NOMEM
;
2476 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2477 original_payload_size
;
2479 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2486 ret
= LTTNG_ERR_UND
;
2491 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2492 DBG("Missing llm header, creating one.");
2493 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2497 /* Set return code */
2498 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2500 if (cmd_ctx
->session
) {
2501 session_unlock(cmd_ctx
->session
);
2502 session_put(cmd_ctx
->session
);
2503 cmd_ctx
->session
= NULL
;
2505 if (need_tracing_session
) {
2506 session_unlock_list();
2509 assert(!rcu_read_ongoing());
2513 static int create_client_sock(void)
2515 int ret
, client_sock
;
2516 const mode_t old_umask
= umask(0);
2518 /* Create client tool unix socket */
2519 client_sock
= lttcomm_create_unix_sock(
2520 the_config
.client_unix_sock_path
.value
);
2521 if (client_sock
< 0) {
2522 ERR("Create unix sock failed: %s",
2523 the_config
.client_unix_sock_path
.value
);
2528 /* Set the cloexec flag */
2529 ret
= utils_set_fd_cloexec(client_sock
);
2531 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2532 "Continuing but note that the consumer daemon will have a "
2533 "reference to this socket on exec()", client_sock
);
2536 /* File permission MUST be 660 */
2537 ret
= chmod(the_config
.client_unix_sock_path
.value
,
2538 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2540 ERR("Set file permissions failed: %s",
2541 the_config
.client_unix_sock_path
.value
);
2543 (void) lttcomm_close_unix_sock(client_sock
);
2547 DBG("Created client socket (fd = %i)", client_sock
);
2554 static void cleanup_client_thread(void *data
)
2556 struct lttng_pipe
*quit_pipe
= data
;
2558 lttng_pipe_destroy(quit_pipe
);
2561 static void thread_init_cleanup(void *data
)
2563 set_thread_status(false);
2567 * This thread manage all clients request using the unix client socket for
2570 static void *thread_manage_clients(void *data
)
2572 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2574 uint32_t revents
, nb_fd
;
2575 struct lttng_poll_event events
;
2576 const int client_sock
= thread_state
.client_sock
;
2577 struct lttng_pipe
*quit_pipe
= data
;
2578 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2579 struct command_ctx cmd_ctx
= {};
2581 DBG("[thread] Manage client started");
2583 lttng_payload_init(&cmd_ctx
.reply_payload
);
2585 is_root
= (getuid() == 0);
2587 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2589 rcu_register_thread();
2591 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2593 health_code_update();
2595 ret
= lttcomm_listen_unix_sock(client_sock
);
2601 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2602 * more will be added to this poll set.
2604 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2606 goto error_create_poll
;
2609 /* Add the application registration socket */
2610 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2615 /* Add thread quit pipe */
2616 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2621 /* Set state as running. */
2622 set_thread_status(true);
2623 pthread_cleanup_pop(0);
2625 /* This testpoint is after we signal readiness to the parent. */
2626 if (testpoint(sessiond_thread_manage_clients
)) {
2630 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2634 health_code_update();
2637 const struct cmd_completion_handler
*cmd_completion_handler
;
2639 cmd_ctx
.creds
= (lttng_sock_cred
) {
2643 cmd_ctx
.session
= NULL
;
2644 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2645 cmd_ctx
.lttng_msg_size
= 0;
2647 DBG("Accepting client command ...");
2649 /* Inifinite blocking call, waiting for transmission */
2651 health_poll_entry();
2652 ret
= lttng_poll_wait(&events
, -1);
2656 * Restart interrupted system call.
2658 if (errno
== EINTR
) {
2666 for (i
= 0; i
< nb_fd
; i
++) {
2667 revents
= LTTNG_POLL_GETEV(&events
, i
);
2668 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2670 health_code_update();
2672 if (pollfd
== thread_quit_pipe_fd
) {
2676 /* Event on the registration socket */
2677 if (revents
& LPOLLIN
) {
2679 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2680 ERR("Client socket poll error");
2683 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2689 DBG("Wait for client response");
2691 health_code_update();
2693 sock
= lttcomm_accept_unix_sock(client_sock
);
2699 * Set the CLOEXEC flag. Return code is useless because either way, the
2702 (void) utils_set_fd_cloexec(sock
);
2704 /* Set socket option for credentials retrieval */
2705 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2710 health_code_update();
2713 * Data is received from the lttng client. The struct
2714 * lttcomm_session_msg (lsm) contains the command and data request of
2717 DBG("Receiving data from client ...");
2718 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2719 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2720 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2721 DBG("Incomplete recv() from client... continuing");
2730 health_code_update();
2732 // TODO: Validate cmd_ctx including sanity check for
2733 // security purpose.
2735 rcu_thread_online();
2737 * This function dispatch the work to the kernel or userspace tracer
2738 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2739 * informations for the client. The command context struct contains
2740 * everything this function may needs.
2742 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2743 rcu_thread_offline();
2753 * TODO: Inform client somehow of the fatal error. At
2754 * this point, ret < 0 means that a zmalloc failed
2755 * (ENOMEM). Error detected but still accept
2756 * command, unless a socket error has been
2762 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2763 WARN("Command returned an invalid status code, returning unknown error: "
2764 "command type = %s (%d), ret = %d",
2765 lttcomm_sessiond_command_str(cmd_ctx
.lsm
.cmd_type
),
2766 cmd_ctx
.lsm
.cmd_type
, ret
);
2767 ret
= LTTNG_ERR_UNK
;
2770 cmd_completion_handler
= cmd_pop_completion_handler();
2771 if (cmd_completion_handler
) {
2772 enum lttng_error_code completion_code
;
2774 completion_code
= cmd_completion_handler
->run(
2775 cmd_completion_handler
->data
);
2776 if (completion_code
!= LTTNG_OK
) {
2781 health_code_update();
2784 struct lttng_payload_view view
=
2785 lttng_payload_view_from_payload(
2786 &cmd_ctx
.reply_payload
,
2788 struct lttcomm_lttng_msg
*llm
= (typeof(
2789 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2791 assert(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2792 assert(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2794 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2796 DBG("Sending response (size: %d, retcode: %s (%d))",
2797 cmd_ctx
.lttng_msg_size
,
2798 lttng_strerror(-llm
->ret_code
),
2800 ret
= send_unix_sock(sock
, &view
);
2802 ERR("Failed to send data back to client");
2805 /* End of transmission */
2813 health_code_update();
2825 lttng_poll_clean(&events
);
2829 unlink(the_config
.client_unix_sock_path
.value
);
2830 ret
= close(client_sock
);
2837 ERR("Health error occurred in %s", __func__
);
2840 health_unregister(the_health_sessiond
);
2842 DBG("Client thread dying");
2843 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2844 rcu_unregister_thread();
2849 bool shutdown_client_thread(void *thread_data
)
2851 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2852 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2854 return notify_thread_pipe(write_fd
) == 1;
2857 struct lttng_thread
*launch_client_thread(void)
2859 bool thread_running
;
2860 struct lttng_pipe
*client_quit_pipe
;
2861 struct lttng_thread
*thread
= NULL
;
2862 int client_sock_fd
= -1;
2864 sem_init(&thread_state
.ready
, 0, 0);
2865 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2866 if (!client_quit_pipe
) {
2870 client_sock_fd
= create_client_sock();
2871 if (client_sock_fd
< 0) {
2875 thread_state
.client_sock
= client_sock_fd
;
2876 thread
= lttng_thread_create("Client management",
2877 thread_manage_clients
,
2878 shutdown_client_thread
,
2879 cleanup_client_thread
,
2884 /* The client thread now owns the client sock fd and the quit pipe. */
2885 client_sock_fd
= -1;
2886 client_quit_pipe
= NULL
;
2889 * This thread is part of the threads that need to be fully
2890 * initialized before the session daemon is marked as "ready".
2892 thread_running
= wait_thread_status();
2893 if (!thread_running
) {
2898 if (client_sock_fd
>= 0) {
2899 if (close(client_sock_fd
)) {
2900 PERROR("Failed to close client socket");
2903 lttng_thread_put(thread
);
2904 cleanup_client_thread(client_quit_pipe
);