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-buffer.h"
13 #include "common/dynamic-array.h"
14 #include "common/payload.h"
15 #include "common/payload-view.h"
16 #include "common/fd-handle.h"
17 #include "common/sessiond-comm/sessiond-comm.h"
18 #include "common/payload.h"
19 #include "common/payload-view.h"
20 #include "lttng/lttng-error.h"
21 #include "lttng/tracker.h"
22 #include <common/compat/getenv.h>
23 #include <common/tracker.h>
24 #include <common/unix.h>
25 #include <common/utils.h>
26 #include <lttng/event-internal.h>
27 #include <lttng/map/map.h>
28 #include <lttng/map/map-internal.h>
29 #include <lttng/map/map-query-internal.h>
30 #include <lttng/session-descriptor-internal.h>
31 #include <lttng/session-internal.h>
32 #include <lttng/userspace-probe-internal.h>
40 #include "lttng-sessiond.h"
44 #include "health-sessiond.h"
45 #include "testpoint.h"
47 #include "manage-consumer.h"
49 #include "agent-thread.h"
53 static struct thread_state
{
59 static void set_thread_status(bool running
)
61 DBG("Marking client thread's state as %s", running
? "running" : "error");
62 thread_state
.running
= running
;
63 sem_post(&thread_state
.ready
);
66 static bool wait_thread_status(void)
68 DBG("Waiting for client thread to be ready");
69 sem_wait(&thread_state
.ready
);
70 if (thread_state
.running
) {
71 DBG("Client thread is ready");
73 ERR("Initialization of client thread failed");
76 return thread_state
.running
;
80 * Setup the outgoing data buffer for the response (llm) by allocating the
81 * right amount of memory and copying the original information from the lsm
84 * Return 0 on success, negative value on error.
86 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
87 const void *payload_buf
, size_t payload_len
,
88 const void *cmd_header_buf
, size_t cmd_header_len
)
91 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
92 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
93 const struct lttcomm_lttng_msg llm
= {
94 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
95 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
96 .cmd_header_size
= cmd_header_len
,
97 .data_size
= payload_len
,
100 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
105 lttng_dynamic_pointer_array_clear(&cmd_ctx
->reply_payload
._fd_handles
);
107 cmd_ctx
->lttng_msg_size
= total_msg_size
;
109 /* Append reply header. */
110 ret
= lttng_dynamic_buffer_append(
111 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
116 /* Append command header. */
117 if (cmd_header_len
) {
118 ret
= lttng_dynamic_buffer_append(
119 &cmd_ctx
->reply_payload
.buffer
, cmd_header_buf
,
126 /* Append payload. */
128 ret
= lttng_dynamic_buffer_append(
129 &cmd_ctx
->reply_payload
.buffer
, payload_buf
,
140 static int setup_empty_lttng_msg(struct command_ctx
*cmd_ctx
)
143 const struct lttcomm_lttng_msg llm
= {};
145 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
150 /* Append place-holder reply header. */
151 ret
= lttng_dynamic_buffer_append(
152 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
157 cmd_ctx
->lttng_msg_size
= sizeof(llm
);
162 static void update_lttng_msg(struct command_ctx
*cmd_ctx
, size_t cmd_header_len
,
165 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
166 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
167 const struct lttcomm_lttng_msg llm
= {
168 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
169 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
170 .cmd_header_size
= cmd_header_len
,
171 .data_size
= payload_len
,
173 struct lttcomm_lttng_msg
*p_llm
;
175 assert(cmd_ctx
->reply_payload
.buffer
.size
>= sizeof(llm
));
177 p_llm
= (typeof(p_llm
)) cmd_ctx
->reply_payload
.buffer
.data
;
179 /* Update existing header. */
180 memcpy(p_llm
, &llm
, sizeof(llm
));
182 cmd_ctx
->lttng_msg_size
= total_msg_size
;
186 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
187 * exec or it will fail.
189 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
191 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
195 * Fork and exec a consumer daemon (consumerd).
197 * Return pid if successful else -1.
199 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
203 const char *consumer_to_use
;
204 const char *verbosity
;
207 DBG("Spawning consumerd");
214 if (config
.verbose_consumer
) {
215 verbosity
= "--verbose";
216 } else if (lttng_opt_quiet
) {
217 verbosity
= "--quiet";
222 switch (consumer_data
->type
) {
223 case LTTNG_CONSUMER_KERNEL
:
225 * Find out which consumerd to execute. We will first try the
226 * 64-bit path, then the sessiond's installation directory, and
227 * fallback on the 32-bit one,
229 DBG3("Looking for a kernel consumer at these locations:");
230 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
231 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
232 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
233 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
234 DBG3("Found location #1");
235 consumer_to_use
= config
.consumerd64_bin_path
.value
;
236 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
237 DBG3("Found location #2");
238 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
239 } else if (config
.consumerd32_bin_path
.value
&&
240 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
241 DBG3("Found location #3");
242 consumer_to_use
= config
.consumerd32_bin_path
.value
;
244 DBG("Could not find any valid consumerd executable");
248 DBG("Using kernel consumer at: %s", consumer_to_use
);
249 (void) execl(consumer_to_use
,
250 "lttng-consumerd", verbosity
, "-k",
251 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
252 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
253 "--group", config
.tracing_group_name
.value
,
256 case LTTNG_CONSUMER64_UST
:
258 if (config
.consumerd64_lib_dir
.value
) {
263 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
267 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
268 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
273 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
274 if (tmp
[0] != '\0') {
278 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
285 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
286 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
287 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
288 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
289 "--group", config
.tracing_group_name
.value
,
293 case LTTNG_CONSUMER32_UST
:
295 if (config
.consumerd32_lib_dir
.value
) {
300 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
304 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
305 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
310 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
311 if (tmp
[0] != '\0') {
315 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
322 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
323 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
324 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
325 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
326 "--group", config
.tracing_group_name
.value
,
331 ERR("unknown consumer type");
335 PERROR("Consumer execl()");
337 /* Reaching this point, we got a failure on our execl(). */
339 } else if (pid
> 0) {
342 PERROR("start consumer fork");
350 * Spawn the consumerd daemon and session daemon thread.
352 static int start_consumerd(struct consumer_data
*consumer_data
)
357 * Set the listen() state on the socket since there is a possible race
358 * between the exec() of the consumer daemon and this call if place in the
359 * consumer thread. See bug #366 for more details.
361 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
366 pthread_mutex_lock(&consumer_data
->pid_mutex
);
367 if (consumer_data
->pid
!= 0) {
368 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
372 ret
= spawn_consumerd(consumer_data
);
374 ERR("Spawning consumerd failed");
375 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
379 /* Setting up the consumer_data pid */
380 consumer_data
->pid
= ret
;
381 DBG2("Consumer pid %d", consumer_data
->pid
);
382 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
384 DBG2("Spawning consumer control thread");
385 ret
= spawn_consumer_thread(consumer_data
);
387 ERR("Fatal error spawning consumer control thread");
395 /* Cleanup already created sockets on error. */
396 if (consumer_data
->err_sock
>= 0) {
399 err
= close(consumer_data
->err_sock
);
401 PERROR("close consumer data error socket");
408 * Copy consumer output from the tracing session to the domain session. The
409 * function also applies the right modification on a per domain basis for the
410 * trace files destination directory.
412 * Should *NOT* be called with RCU read-side lock held.
414 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
417 const char *dir_name
;
418 struct consumer_output
*consumer
;
421 assert(session
->consumer
);
424 case LTTNG_DOMAIN_KERNEL
:
425 DBG3("Copying tracing session consumer output in kernel session");
427 * XXX: We should audit the session creation and what this function
428 * does "extra" in order to avoid a destroy since this function is used
429 * in the domain session creation (kernel and ust) only. Same for UST
432 if (session
->kernel_session
->consumer
) {
433 consumer_output_put(session
->kernel_session
->consumer
);
435 session
->kernel_session
->consumer
=
436 consumer_copy_output(session
->consumer
);
437 /* Ease our life a bit for the next part */
438 consumer
= session
->kernel_session
->consumer
;
439 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
441 case LTTNG_DOMAIN_JUL
:
442 case LTTNG_DOMAIN_LOG4J
:
443 case LTTNG_DOMAIN_PYTHON
:
444 case LTTNG_DOMAIN_UST
:
445 DBG3("Copying tracing session consumer output in UST session");
446 if (session
->ust_session
->consumer
) {
447 consumer_output_put(session
->ust_session
->consumer
);
449 session
->ust_session
->consumer
=
450 consumer_copy_output(session
->consumer
);
451 /* Ease our life a bit for the next part */
452 consumer
= session
->ust_session
->consumer
;
453 dir_name
= DEFAULT_UST_TRACE_DIR
;
456 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
460 /* Append correct directory to subdir */
461 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
462 sizeof(consumer
->domain_subdir
));
467 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
475 * Create an UST session and add it to the session ust list.
477 * Should *NOT* be called with RCU read-side lock held.
479 static int create_ust_session(struct ltt_session
*session
,
480 const struct lttng_domain
*domain
)
483 struct ltt_ust_session
*lus
= NULL
;
487 assert(session
->consumer
);
489 switch (domain
->type
) {
490 case LTTNG_DOMAIN_JUL
:
491 case LTTNG_DOMAIN_LOG4J
:
492 case LTTNG_DOMAIN_PYTHON
:
493 case LTTNG_DOMAIN_UST
:
496 ERR("Unknown UST domain on create session %d", domain
->type
);
497 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
501 DBG("Creating UST session");
503 lus
= trace_ust_create_session(session
->id
);
505 ret
= LTTNG_ERR_UST_SESS_FAIL
;
509 lus
->uid
= session
->uid
;
510 lus
->gid
= session
->gid
;
511 lus
->output_traces
= session
->output_traces
;
512 lus
->snapshot_mode
= session
->snapshot_mode
;
513 lus
->live_timer_interval
= session
->live_timer
;
514 session
->ust_session
= lus
;
515 if (session
->shm_path
[0]) {
516 strncpy(lus
->root_shm_path
, session
->shm_path
,
517 sizeof(lus
->root_shm_path
));
518 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
519 strncpy(lus
->shm_path
, session
->shm_path
,
520 sizeof(lus
->shm_path
));
521 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
522 strncat(lus
->shm_path
, "/ust",
523 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
525 /* Copy session output to the newly created UST session */
526 ret
= copy_session_consumer(domain
->type
, session
);
527 if (ret
!= LTTNG_OK
) {
535 session
->ust_session
= NULL
;
540 * Create a kernel tracer session then create the default channel.
542 static int create_kernel_session(struct ltt_session
*session
)
546 DBG("Creating kernel session");
548 ret
= kernel_create_session(session
);
550 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
554 /* Code flow safety */
555 assert(session
->kernel_session
);
557 /* Copy session output to the newly created Kernel session */
558 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
559 if (ret
!= LTTNG_OK
) {
563 session
->kernel_session
->uid
= session
->uid
;
564 session
->kernel_session
->gid
= session
->gid
;
565 session
->kernel_session
->output_traces
= session
->output_traces
;
566 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
567 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
572 trace_kernel_destroy_session(session
->kernel_session
);
573 session
->kernel_session
= NULL
;
579 * Count number of session permitted by uid/gid.
581 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
584 struct ltt_session
*session
;
585 const struct ltt_session_list
*session_list
= session_get_list();
587 DBG("Counting number of available session for UID %d", uid
);
588 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
589 if (!session_get(session
)) {
592 session_lock(session
);
593 /* Only count the sessions the user can control. */
594 if (session_access_ok(session
, uid
) &&
595 !session
->destroyed
) {
598 session_unlock(session
);
599 session_put(session
);
604 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
605 int *sock_error
, struct lttng_event
*event
)
608 struct lttng_userspace_probe_location
*probe_location
;
609 struct lttng_payload probe_location_payload
;
610 struct fd_handle
*handle
= NULL
;
613 * Create a payload to store the serialized version of the probe
616 lttng_payload_init(&probe_location_payload
);
618 ret
= lttng_dynamic_buffer_set_size(&probe_location_payload
.buffer
,
619 cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
);
621 ret
= LTTNG_ERR_NOMEM
;
626 * Receive the probe location.
628 ret
= lttcomm_recv_unix_sock(sock
, probe_location_payload
.buffer
.data
,
629 probe_location_payload
.buffer
.size
);
631 DBG("Nothing recv() from client var len data... continuing");
633 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
638 * Receive the file descriptor to the target binary from the client.
640 DBG("Receiving userspace probe target FD from client ...");
641 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
643 DBG("Nothing recv() from client userspace probe fd... continuing");
645 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
649 handle
= fd_handle_create(fd
);
651 ret
= LTTNG_ERR_NOMEM
;
655 /* Transferred to the handle. */
658 ret
= lttng_payload_push_fd_handle(&probe_location_payload
, handle
);
660 ERR("Failed to add userspace probe file descriptor to payload");
661 ret
= LTTNG_ERR_NOMEM
;
665 fd_handle_put(handle
);
669 struct lttng_payload_view view
= lttng_payload_view_from_payload(
670 &probe_location_payload
, 0, -1);
672 /* Extract the probe location from the serialized version. */
673 ret
= lttng_userspace_probe_location_create_from_payload(
674 &view
, &probe_location
);
677 WARN("Failed to create a userspace probe location from the received buffer");
678 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
682 /* Attach the probe location to the event. */
683 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
685 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
692 PERROR("Failed to close userspace probe location binary fd");
696 fd_handle_put(handle
);
697 lttng_payload_reset(&probe_location_payload
);
701 static enum lttng_error_code
receive_lttng_trigger(struct command_ctx
*cmd_ctx
,
704 struct lttng_trigger
**_trigger
)
708 ssize_t sock_recv_len
;
709 enum lttng_error_code ret_code
;
710 struct lttng_payload trigger_payload
;
711 struct lttng_trigger
*trigger
= NULL
;
713 lttng_payload_init(&trigger_payload
);
714 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
715 ret
= lttng_dynamic_buffer_set_size(
716 &trigger_payload
.buffer
, trigger_len
);
718 ret_code
= LTTNG_ERR_NOMEM
;
722 sock_recv_len
= lttcomm_recv_unix_sock(
723 sock
, trigger_payload
.buffer
.data
, trigger_len
);
724 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
725 ERR("Failed to receive trigger in command payload");
727 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
731 /* Receive fds, if any. */
732 if (cmd_ctx
->lsm
.fd_count
> 0) {
733 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
734 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
735 if (sock_recv_len
> 0 &&
736 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
737 ERR("Failed to receive all file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
738 cmd_ctx
->lsm
.fd_count
, (int) ret
);
739 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
742 } else if (sock_recv_len
<= 0) {
743 ERR("Failed to receive file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
744 cmd_ctx
->lsm
.fd_count
, (int) ret
);
745 ret_code
= LTTNG_ERR_FATAL
;
751 /* Deserialize trigger. */
753 struct lttng_payload_view view
=
754 lttng_payload_view_from_payload(
755 &trigger_payload
, 0, -1);
757 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
759 ERR("Invalid trigger received as part of command payload");
760 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
761 lttng_trigger_put(trigger
);
773 static enum lttng_error_code
receive_lttng_map(int sock
,
776 struct lttng_map
**_map
)
779 ssize_t sock_recv_len
;
780 enum lttng_error_code ret_code
;
781 struct lttng_payload map_payload
;
782 struct lttng_map
*map
= NULL
;
784 lttng_payload_init(&map_payload
);
785 ret
= lttng_dynamic_buffer_set_size(
786 &map_payload
.buffer
, map_len
);
788 ret_code
= LTTNG_ERR_NOMEM
;
792 sock_recv_len
= lttcomm_recv_unix_sock(
793 sock
, map_payload
.buffer
.data
, map_len
);
794 if (sock_recv_len
< 0 || sock_recv_len
!= map_len
) {
795 ERR("Failed to receive map in command payload");
797 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
801 /* Deserialize map. */
803 struct lttng_payload_view view
=
804 lttng_payload_view_from_payload(
805 &map_payload
, 0, -1);
807 if (lttng_map_create_from_payload(&view
, &map
) !=
809 ERR("Invalid map received as part of command payload");
810 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
823 static enum lttng_error_code
receive_lttng_map_query(int sock
,
825 uint32_t map_query_len
,
826 struct lttng_map_query
**_map_query
)
829 ssize_t sock_recv_len
;
830 enum lttng_error_code ret_code
;
831 struct lttng_payload map_query_payload
;
832 struct lttng_map_query
*map_query
= NULL
;
834 lttng_payload_init(&map_query_payload
);
835 ret
= lttng_dynamic_buffer_set_size(
836 &map_query_payload
.buffer
, map_query_len
);
838 ret_code
= LTTNG_ERR_NOMEM
;
842 sock_recv_len
= lttcomm_recv_unix_sock(
843 sock
, map_query_payload
.buffer
.data
, map_query_len
);
844 if (sock_recv_len
< 0 || sock_recv_len
!= map_query_len
) {
845 ERR("Failed to receive map query in command payload");
847 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
851 /* Deserialize map query. */
853 struct lttng_payload_view view
=
854 lttng_payload_view_from_payload(
855 &map_query_payload
, 0, -1);
857 if (lttng_map_query_create_from_payload(&view
, &map_query
) !=
859 ERR("Invalid map query received as part of command payload");
860 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
861 lttng_map_query_destroy(map_query
);
866 *_map_query
= map_query
;
874 * Version of setup_lttng_msg() without command header.
876 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
877 void *payload_buf
, size_t payload_len
)
879 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
883 * Check if the current kernel tracer supports the session rotation feature.
884 * Return 1 if it does, 0 otherwise.
886 static int check_rotate_compatible(void)
890 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
891 DBG("Kernel tracer version is not compatible with the rotation feature");
899 * Send data on a unix socket using the liblttsessiondcomm API.
901 * Return lttcomm error code.
903 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
906 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
908 /* Check valid length */
909 if (view
->buffer
.size
== 0) {
914 ret
= lttcomm_send_unix_sock(
915 sock
, view
->buffer
.data
, view
->buffer
.size
);
921 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
932 * Process the command requested by the lttng client within the command
933 * context structure. This function make sure that the return structure (llm)
934 * is set and ready for transmission before returning.
936 * Return any error encountered or 0 for success.
938 * "sock" is only used for special-case var. len data.
939 * A command may assume the ownership of the socket, in which case its value
940 * should be set to -1.
942 * Should *NOT* be called with RCU read-side lock held.
944 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
948 bool need_tracing_session
= true;
952 DBG("Processing client command '%s\' (%d)",
953 lttcomm_sessiond_command_str(cmd_ctx
->lsm
.cmd_type
),
954 cmd_ctx
->lsm
.cmd_type
);
956 assert(!rcu_read_ongoing());
960 switch (cmd_ctx
->lsm
.cmd_type
) {
961 case LTTNG_CREATE_SESSION_EXT
:
962 case LTTNG_DESTROY_SESSION
:
963 case LTTNG_LIST_SESSIONS
:
964 case LTTNG_LIST_DOMAINS
:
965 case LTTNG_START_TRACE
:
966 case LTTNG_STOP_TRACE
:
967 case LTTNG_DATA_PENDING
:
968 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
969 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
970 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
971 case LTTNG_SNAPSHOT_RECORD
:
972 case LTTNG_SAVE_SESSION
:
973 case LTTNG_SET_SESSION_SHM_PATH
:
974 case LTTNG_REGENERATE_METADATA
:
975 case LTTNG_REGENERATE_STATEDUMP
:
976 case LTTNG_ROTATE_SESSION
:
977 case LTTNG_ROTATION_GET_INFO
:
978 case LTTNG_ROTATION_SET_SCHEDULE
:
979 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
980 case LTTNG_CLEAR_SESSION
:
981 case LTTNG_LIST_TRIGGERS
:
982 case LTTNG_LIST_MAP_VALUES
:
989 /* Needs a functioning consumerd? */
990 switch (cmd_ctx
->lsm
.cmd_type
) {
991 case LTTNG_REGISTER_TRIGGER
:
992 case LTTNG_UNREGISTER_TRIGGER
:
994 case LTTNG_ENABLE_MAP
:
995 case LTTNG_DISABLE_MAP
:
996 case LTTNG_LIST_MAP_VALUES
:
997 need_consumerd
= false;
1000 need_consumerd
= true;
1004 if (config
.no_kernel
&& need_domain
1005 && cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
1007 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1009 ret
= LTTNG_ERR_KERN_NA
;
1014 /* Deny register consumer if we already have a spawned consumer. */
1015 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
1016 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
1017 if (kconsumer_data
.pid
> 0) {
1018 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1019 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
1022 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
1026 * Check for command that don't needs to allocate a returned payload. We do
1027 * this here so we don't have to make the call for no payload at each
1030 switch(cmd_ctx
->lsm
.cmd_type
) {
1031 case LTTNG_LIST_SESSIONS
:
1032 case LTTNG_LIST_TRACEPOINTS
:
1033 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1034 case LTTNG_LIST_DOMAINS
:
1035 case LTTNG_LIST_CHANNELS
:
1036 case LTTNG_LIST_EVENTS
:
1037 case LTTNG_LIST_SYSCALLS
:
1038 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1039 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1040 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1041 case LTTNG_DATA_PENDING
:
1042 case LTTNG_ROTATE_SESSION
:
1043 case LTTNG_ROTATION_GET_INFO
:
1044 case LTTNG_REGISTER_TRIGGER
:
1045 case LTTNG_LIST_TRIGGERS
:
1048 /* Setup lttng message with no payload */
1049 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
1051 /* This label does not try to unlock the session */
1052 goto init_setup_error
;
1056 /* Commands that DO NOT need a session. */
1057 switch (cmd_ctx
->lsm
.cmd_type
) {
1058 case LTTNG_CREATE_SESSION_EXT
:
1059 case LTTNG_LIST_SESSIONS
:
1060 case LTTNG_LIST_TRACEPOINTS
:
1061 case LTTNG_LIST_SYSCALLS
:
1062 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1063 case LTTNG_SAVE_SESSION
:
1064 case LTTNG_REGISTER_TRIGGER
:
1065 case LTTNG_UNREGISTER_TRIGGER
:
1066 case LTTNG_LIST_TRIGGERS
:
1067 need_tracing_session
= false;
1070 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
1072 * We keep the session list lock across _all_ commands
1073 * for now, because the per-session lock does not
1074 * handle teardown properly.
1076 session_lock_list();
1077 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
1078 if (cmd_ctx
->session
== NULL
) {
1079 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
1082 /* Acquire lock for the session */
1083 session_lock(cmd_ctx
->session
);
1089 * Commands that need a valid session but should NOT create one if none
1090 * exists. Instead of creating one and destroying it when the command is
1091 * handled, process that right before so we save some round trip in useless
1094 switch (cmd_ctx
->lsm
.cmd_type
) {
1095 case LTTNG_DISABLE_CHANNEL
:
1096 case LTTNG_DISABLE_EVENT
:
1097 case LTTNG_DISABLE_MAP
:
1098 switch (cmd_ctx
->lsm
.domain
.type
) {
1099 case LTTNG_DOMAIN_KERNEL
:
1100 if (!cmd_ctx
->session
->kernel_session
) {
1101 ret
= LTTNG_ERR_NO_CHANNEL
;
1105 case LTTNG_DOMAIN_JUL
:
1106 case LTTNG_DOMAIN_LOG4J
:
1107 case LTTNG_DOMAIN_PYTHON
:
1108 case LTTNG_DOMAIN_UST
:
1109 if (!cmd_ctx
->session
->ust_session
) {
1110 ret
= LTTNG_ERR_NO_CHANNEL
;
1115 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1127 * Check domain type for specific "pre-action".
1129 switch (cmd_ctx
->lsm
.domain
.type
) {
1130 case LTTNG_DOMAIN_KERNEL
:
1132 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1136 /* Kernel tracer check */
1137 if (!kernel_tracer_is_initialized()) {
1138 /* Basically, load kernel tracer modules */
1139 ret
= init_kernel_tracer();
1145 /* Consumer is in an ERROR state. Report back to client */
1146 if (need_consumerd
&& uatomic_read(&kernel_consumerd_state
) ==
1148 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1152 /* Need a session for kernel command */
1153 if (need_tracing_session
) {
1154 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1155 ret
= create_kernel_session(cmd_ctx
->session
);
1156 if (ret
!= LTTNG_OK
) {
1157 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1162 /* Start the kernel consumer daemon */
1163 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
1164 if (kconsumer_data
.pid
== 0 &&
1165 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1166 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
1167 ret
= start_consumerd(&kconsumer_data
);
1169 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1172 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
1174 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
1178 * The consumer was just spawned so we need to add the socket to
1179 * the consumer output of the session if exist.
1181 ret
= consumer_create_socket(&kconsumer_data
,
1182 cmd_ctx
->session
->kernel_session
->consumer
);
1189 case LTTNG_DOMAIN_JUL
:
1190 case LTTNG_DOMAIN_LOG4J
:
1191 case LTTNG_DOMAIN_PYTHON
:
1192 if (!agent_tracing_is_enabled()) {
1193 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1197 case LTTNG_DOMAIN_UST
:
1199 if (!ust_app_supported()) {
1200 ret
= LTTNG_ERR_NO_UST
;
1204 /* Consumer is in an ERROR state. Report back to client */
1205 if (need_consumerd
&& uatomic_read(&ust_consumerd_state
) ==
1207 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1211 if (need_tracing_session
) {
1212 /* Create UST session if none exist. */
1213 if (cmd_ctx
->session
->ust_session
== NULL
) {
1214 ret
= create_ust_session(cmd_ctx
->session
,
1215 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
));
1216 if (ret
!= LTTNG_OK
) {
1221 /* Start the UST consumer daemons */
1223 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
1224 if (config
.consumerd64_bin_path
.value
&&
1225 ustconsumer64_data
.pid
== 0 &&
1226 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1227 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
1228 ret
= start_consumerd(&ustconsumer64_data
);
1230 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1231 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
1235 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
1236 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1238 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
1242 * Setup socket for consumer 64 bit. No need for atomic access
1243 * since it was set above and can ONLY be set in this thread.
1245 ret
= consumer_create_socket(&ustconsumer64_data
,
1246 cmd_ctx
->session
->ust_session
->consumer
);
1252 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
1253 if (config
.consumerd32_bin_path
.value
&&
1254 ustconsumer32_data
.pid
== 0 &&
1255 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1256 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1257 ret
= start_consumerd(&ustconsumer32_data
);
1259 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1260 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
1264 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
1265 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1267 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1271 * Setup socket for consumer 32 bit. No need for atomic access
1272 * since it was set above and can ONLY be set in this thread.
1274 ret
= consumer_create_socket(&ustconsumer32_data
,
1275 cmd_ctx
->session
->ust_session
->consumer
);
1287 /* Validate consumer daemon state when start/stop trace command */
1288 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1289 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1290 switch (cmd_ctx
->lsm
.domain
.type
) {
1291 case LTTNG_DOMAIN_NONE
:
1293 case LTTNG_DOMAIN_JUL
:
1294 case LTTNG_DOMAIN_LOG4J
:
1295 case LTTNG_DOMAIN_PYTHON
:
1296 case LTTNG_DOMAIN_UST
:
1297 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1298 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1302 case LTTNG_DOMAIN_KERNEL
:
1303 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1304 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1309 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1315 * Check that the UID matches that of the tracing session.
1316 * The root user can interact with all sessions.
1318 if (need_tracing_session
) {
1319 if (!session_access_ok(cmd_ctx
->session
,
1320 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1321 cmd_ctx
->session
->destroyed
) {
1322 ret
= LTTNG_ERR_EPERM
;
1328 * Send relayd information to consumer as soon as we have a domain and a
1331 if (cmd_ctx
->session
&& need_domain
) {
1333 * Setup relayd if not done yet. If the relayd information was already
1334 * sent to the consumer, this call will gracefully return.
1336 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1337 if (ret
!= LTTNG_OK
) {
1342 /* Process by command type */
1343 switch (cmd_ctx
->lsm
.cmd_type
) {
1344 case LTTNG_ADD_CONTEXT
:
1347 * An LTTNG_ADD_CONTEXT command might have a supplementary
1348 * payload if the context being added is an application context.
1350 if (cmd_ctx
->lsm
.u
.context
.ctx
.ctx
==
1351 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1352 char *provider_name
= NULL
, *context_name
= NULL
;
1353 size_t provider_name_len
=
1354 cmd_ctx
->lsm
.u
.context
.provider_name_len
;
1355 size_t context_name_len
=
1356 cmd_ctx
->lsm
.u
.context
.context_name_len
;
1358 if (provider_name_len
== 0 || context_name_len
== 0) {
1360 * Application provider and context names MUST
1363 ret
= -LTTNG_ERR_INVALID
;
1367 provider_name
= zmalloc(provider_name_len
+ 1);
1368 if (!provider_name
) {
1369 ret
= -LTTNG_ERR_NOMEM
;
1372 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
=
1375 context_name
= zmalloc(context_name_len
+ 1);
1376 if (!context_name
) {
1377 ret
= -LTTNG_ERR_NOMEM
;
1378 goto error_add_context
;
1380 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1383 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1386 goto error_add_context
;
1389 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1392 goto error_add_context
;
1397 * cmd_add_context assumes ownership of the provider and context
1400 ret
= cmd_add_context(cmd_ctx
->session
,
1401 cmd_ctx
->lsm
.domain
.type
,
1402 cmd_ctx
->lsm
.u
.context
.channel_name
,
1403 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.context
.ctx
),
1404 kernel_poll_pipe
[1]);
1406 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1407 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1409 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
);
1410 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1416 case LTTNG_DISABLE_CHANNEL
:
1418 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1419 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1422 case LTTNG_DISABLE_EVENT
:
1426 * FIXME: handle filter; for now we just receive the filter's
1427 * bytecode along with the filter expression which are sent by
1428 * liblttng-ctl and discard them.
1430 * This fixes an issue where the client may block while sending
1431 * the filter payload and encounter an error because the session
1432 * daemon closes the socket without ever handling this data.
1434 size_t count
= cmd_ctx
->lsm
.u
.disable
.expression_len
+
1435 cmd_ctx
->lsm
.u
.disable
.bytecode_len
;
1438 char data
[LTTNG_FILTER_MAX_LEN
];
1440 DBG("Discarding disable event command payload of size %zu", count
);
1442 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1443 count
> sizeof(data
) ? sizeof(data
) : count
);
1448 count
-= (size_t) ret
;
1451 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1452 cmd_ctx
->lsm
.u
.disable
.channel_name
,
1453 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.disable
.event
));
1456 case LTTNG_ENABLE_CHANNEL
:
1458 cmd_ctx
->lsm
.u
.channel
.chan
.attr
.extended
.ptr
=
1459 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
.u
.channel
.extended
;
1460 ret
= cmd_enable_channel(cmd_ctx
->session
,
1461 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1462 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.channel
.chan
),
1463 kernel_poll_pipe
[1]);
1468 ret
= cmd_add_map(cmd_ctx
, *sock
);
1472 case LTTNG_ENABLE_MAP
:
1473 ret
= cmd_enable_map(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1474 cmd_ctx
->lsm
.u
.enable_map
.map_name
);
1476 case LTTNG_DISABLE_MAP
:
1478 ret
= cmd_disable_map(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1479 cmd_ctx
->lsm
.u
.disable_map
.map_name
);
1483 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1484 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1486 struct lttng_dynamic_buffer payload
;
1487 struct lttng_buffer_view payload_view
;
1488 const bool add_value
=
1489 cmd_ctx
->lsm
.cmd_type
==
1490 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1491 const size_t name_len
=
1492 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1494 const enum lttng_domain_type domain_type
=
1495 (enum lttng_domain_type
)
1496 cmd_ctx
->lsm
.domain
.type
;
1497 const enum lttng_process_attr process_attr
=
1498 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1499 .process_attr_tracker_add_remove_include_value
1501 const enum lttng_process_attr_value_type value_type
=
1502 (enum lttng_process_attr_value_type
) cmd_ctx
1504 .process_attr_tracker_add_remove_include_value
1506 struct process_attr_value
*value
;
1507 enum lttng_error_code ret_code
;
1508 long login_name_max
;
1510 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1511 if (login_name_max
< 0) {
1512 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1513 ret
= LTTNG_ERR_INVALID
;
1517 /* Receive remaining variable length payload if applicable. */
1518 if (name_len
> login_name_max
) {
1520 * POSIX mandates user and group names that are at least
1521 * 8 characters long. Note that although shadow-utils
1522 * (useradd, groupaadd, etc.) use 32 chars as their
1523 * limit (from bits/utmp.h, UT_NAMESIZE),
1524 * LOGIN_NAME_MAX is defined to 256.
1526 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1527 add_value
? "addition" : "removal",
1528 name_len
, login_name_max
);
1529 ret
= LTTNG_ERR_INVALID
;
1533 lttng_dynamic_buffer_init(&payload
);
1534 if (name_len
!= 0) {
1536 * Receive variable payload for user/group name
1539 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1541 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1542 add_value
? "add" : "remove");
1543 ret
= LTTNG_ERR_NOMEM
;
1544 goto error_add_remove_tracker_value
;
1547 ret
= lttcomm_recv_unix_sock(
1548 *sock
, payload
.data
, name_len
);
1550 ERR("Failed to receive payload of %s process attribute tracker value argument",
1551 add_value
? "add" : "remove");
1553 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1554 goto error_add_remove_tracker_value
;
1558 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1559 &payload
, 0, name_len
);
1560 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1561 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1562 goto error_add_remove_tracker_value
;
1566 * Validate the value type and domains are legal for the process
1567 * attribute tracker that is specified and convert the value to
1568 * add/remove to the internal sessiond representation.
1570 ret_code
= process_attr_value_from_comm(domain_type
,
1571 process_attr
, value_type
,
1572 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1574 &payload_view
, &value
);
1575 if (ret_code
!= LTTNG_OK
) {
1577 goto error_add_remove_tracker_value
;
1581 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1582 cmd_ctx
->session
, domain_type
,
1583 process_attr
, value
);
1585 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1586 cmd_ctx
->session
, domain_type
,
1587 process_attr
, value
);
1589 process_attr_value_destroy(value
);
1590 error_add_remove_tracker_value
:
1591 lttng_dynamic_buffer_reset(&payload
);
1594 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1596 enum lttng_tracking_policy tracking_policy
;
1597 const enum lttng_domain_type domain_type
=
1598 (enum lttng_domain_type
)
1599 cmd_ctx
->lsm
.domain
.type
;
1600 const enum lttng_process_attr process_attr
=
1601 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1602 .process_attr_tracker_get_tracking_policy
1605 ret
= cmd_process_attr_tracker_get_tracking_policy(
1606 cmd_ctx
->session
, domain_type
, process_attr
,
1608 if (ret
!= LTTNG_OK
) {
1612 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1613 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1615 ret
= LTTNG_ERR_NOMEM
;
1621 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1623 const enum lttng_tracking_policy tracking_policy
=
1624 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1625 .process_attr_tracker_set_tracking_policy
1627 const enum lttng_domain_type domain_type
=
1628 (enum lttng_domain_type
)
1629 cmd_ctx
->lsm
.domain
.type
;
1630 const enum lttng_process_attr process_attr
=
1631 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1632 .process_attr_tracker_set_tracking_policy
1635 ret
= cmd_process_attr_tracker_set_tracking_policy(
1636 cmd_ctx
->session
, domain_type
, process_attr
,
1638 if (ret
!= LTTNG_OK
) {
1643 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1645 struct lttng_process_attr_values
*values
;
1646 struct lttng_dynamic_buffer reply
;
1647 const enum lttng_domain_type domain_type
=
1648 (enum lttng_domain_type
)
1649 cmd_ctx
->lsm
.domain
.type
;
1650 const enum lttng_process_attr process_attr
=
1651 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1652 .process_attr_tracker_get_inclusion_set
1655 ret
= cmd_process_attr_tracker_get_inclusion_set(
1656 cmd_ctx
->session
, domain_type
, process_attr
,
1658 if (ret
!= LTTNG_OK
) {
1662 lttng_dynamic_buffer_init(&reply
);
1663 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1665 goto error_tracker_get_inclusion_set
;
1668 ret
= setup_lttng_msg_no_cmd_header(
1669 cmd_ctx
, reply
.data
, reply
.size
);
1671 ret
= LTTNG_ERR_NOMEM
;
1672 goto error_tracker_get_inclusion_set
;
1676 error_tracker_get_inclusion_set
:
1677 lttng_process_attr_values_destroy(values
);
1678 lttng_dynamic_buffer_reset(&reply
);
1681 case LTTNG_ENABLE_EVENT
:
1683 struct lttng_event
*ev
= NULL
;
1684 struct lttng_event_exclusion
*exclusion
= NULL
;
1685 struct lttng_bytecode
*bytecode
= NULL
;
1686 char *filter_expression
= NULL
;
1688 /* Handle exclusion events and receive it from the client. */
1689 if (cmd_ctx
->lsm
.u
.enable
.exclusion_count
> 0) {
1690 size_t count
= cmd_ctx
->lsm
.u
.enable
.exclusion_count
;
1692 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1693 (count
* LTTNG_SYMBOL_NAME_LEN
));
1695 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1699 DBG("Receiving var len exclusion event list from client ...");
1700 exclusion
->count
= count
;
1701 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1702 count
* LTTNG_SYMBOL_NAME_LEN
);
1704 DBG("Nothing recv() from client var len data... continuing");
1707 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1712 /* Get filter expression from client. */
1713 if (cmd_ctx
->lsm
.u
.enable
.expression_len
> 0) {
1714 size_t expression_len
=
1715 cmd_ctx
->lsm
.u
.enable
.expression_len
;
1717 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1718 ret
= LTTNG_ERR_FILTER_INVAL
;
1723 filter_expression
= zmalloc(expression_len
);
1724 if (!filter_expression
) {
1726 ret
= LTTNG_ERR_FILTER_NOMEM
;
1730 /* Receive var. len. data */
1731 DBG("Receiving var len filter's expression from client ...");
1732 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1735 DBG("Nothing recv() from client var len data... continuing");
1737 free(filter_expression
);
1739 ret
= LTTNG_ERR_FILTER_INVAL
;
1744 /* Handle filter and get bytecode from client. */
1745 if (cmd_ctx
->lsm
.u
.enable
.bytecode_len
> 0) {
1746 size_t bytecode_len
= cmd_ctx
->lsm
.u
.enable
.bytecode_len
;
1748 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1749 ret
= LTTNG_ERR_FILTER_INVAL
;
1750 free(filter_expression
);
1755 bytecode
= zmalloc(bytecode_len
);
1757 free(filter_expression
);
1759 ret
= LTTNG_ERR_FILTER_NOMEM
;
1763 /* Receive var. len. data */
1764 DBG("Receiving var len filter's bytecode from client ...");
1765 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1767 DBG("Nothing recv() from client var len data... continuing");
1769 free(filter_expression
);
1772 ret
= LTTNG_ERR_FILTER_INVAL
;
1776 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1777 free(filter_expression
);
1780 ret
= LTTNG_ERR_FILTER_INVAL
;
1785 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.enable
.event
));
1787 DBG("Failed to copy event: %s",
1788 cmd_ctx
->lsm
.u
.enable
.event
.name
);
1789 free(filter_expression
);
1792 ret
= LTTNG_ERR_NOMEM
;
1797 if (cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
> 0) {
1798 /* Expect a userspace probe description. */
1799 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1801 free(filter_expression
);
1804 lttng_event_destroy(ev
);
1809 ret
= cmd_enable_event(cmd_ctx
->session
,
1810 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1811 cmd_ctx
->lsm
.u
.enable
.channel_name
,
1813 filter_expression
, bytecode
, exclusion
,
1814 kernel_poll_pipe
[1]);
1815 lttng_event_destroy(ev
);
1818 case LTTNG_LIST_TRACEPOINTS
:
1820 struct lttng_event
*events
;
1823 session_lock_list();
1824 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &events
);
1825 session_unlock_list();
1826 if (nb_events
< 0) {
1827 /* Return value is a negative lttng_error_code. */
1833 * Setup lttng message with payload size set to the event list size in
1834 * bytes and then copy list into the llm payload.
1836 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1837 sizeof(struct lttng_event
) * nb_events
);
1847 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1849 struct lttng_event_field
*fields
;
1852 session_lock_list();
1853 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1855 session_unlock_list();
1856 if (nb_fields
< 0) {
1857 /* Return value is a negative lttng_error_code. */
1863 * Setup lttng message with payload size set to the event list size in
1864 * bytes and then copy list into the llm payload.
1866 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1867 sizeof(struct lttng_event_field
) * nb_fields
);
1877 case LTTNG_LIST_SYSCALLS
:
1879 struct lttng_event
*events
;
1882 nb_events
= cmd_list_syscalls(&events
);
1883 if (nb_events
< 0) {
1884 /* Return value is a negative lttng_error_code. */
1890 * Setup lttng message with payload size set to the event list size in
1891 * bytes and then copy list into the llm payload.
1893 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1894 sizeof(struct lttng_event
) * nb_events
);
1904 case LTTNG_SET_CONSUMER_URI
:
1907 struct lttng_uri
*uris
;
1909 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1910 len
= nb_uri
* sizeof(struct lttng_uri
);
1913 ret
= LTTNG_ERR_INVALID
;
1917 uris
= zmalloc(len
);
1919 ret
= LTTNG_ERR_FATAL
;
1923 /* Receive variable len data */
1924 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1925 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1927 DBG("No URIs received from client... continuing");
1929 ret
= LTTNG_ERR_SESSION_FAIL
;
1934 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1936 if (ret
!= LTTNG_OK
) {
1943 case LTTNG_START_TRACE
:
1946 * On the first start, if we have a kernel session and we have
1947 * enabled time or size-based rotations, we have to make sure
1948 * the kernel tracer supports it.
1950 if (!cmd_ctx
->session
->has_been_started
&& \
1951 cmd_ctx
->session
->kernel_session
&& \
1952 (cmd_ctx
->session
->rotate_timer_period
|| \
1953 cmd_ctx
->session
->rotate_size
) && \
1954 !check_rotate_compatible()) {
1955 DBG("Kernel tracer version is not compatible with the rotation feature");
1956 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1959 ret
= cmd_start_trace(cmd_ctx
->session
);
1962 case LTTNG_STOP_TRACE
:
1964 ret
= cmd_stop_trace(cmd_ctx
->session
);
1967 case LTTNG_DESTROY_SESSION
:
1969 ret
= cmd_destroy_session(cmd_ctx
->session
,
1970 notification_thread_handle
,
1974 case LTTNG_LIST_DOMAINS
:
1977 struct lttng_domain
*domains
= NULL
;
1979 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1981 /* Return value is a negative lttng_error_code. */
1986 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1987 nb_dom
* sizeof(struct lttng_domain
));
1997 case LTTNG_LIST_CHANNELS
:
1999 ssize_t payload_size
;
2000 struct lttng_channel
*channels
= NULL
;
2002 payload_size
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
2003 cmd_ctx
->session
, &channels
);
2004 if (payload_size
< 0) {
2005 /* Return value is a negative lttng_error_code. */
2006 ret
= -payload_size
;
2010 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
2021 case LTTNG_LIST_MAPS
:
2023 struct lttng_map_list
*return_map_list
= NULL
;
2024 size_t original_payload_size
;
2025 size_t payload_size
;
2027 ret
= setup_empty_lttng_msg(cmd_ctx
);
2029 ret
= LTTNG_ERR_NOMEM
;
2033 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2035 ret
= cmd_list_maps(cmd_ctx
->lsm
.domain
.type
, cmd_ctx
->session
,
2037 if (ret
!= LTTNG_OK
) {
2041 assert(return_map_list
);
2042 ret
= lttng_map_list_serialize(return_map_list
,
2043 &cmd_ctx
->reply_payload
);
2044 lttng_map_list_destroy(return_map_list
);
2046 ERR("Failed to serialize map_list in reply to `%s` command",
2047 lttcomm_sessiond_command_str(cmd_ctx
->lsm
.cmd_type
));
2048 ret
= LTTNG_ERR_NOMEM
;
2052 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2053 original_payload_size
;
2055 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2060 case LTTNG_LIST_EVENTS
:
2063 struct lttcomm_event_command_header cmd_header
= {};
2064 size_t original_payload_size
;
2065 size_t payload_size
;
2067 ret
= setup_empty_lttng_msg(cmd_ctx
);
2069 ret
= LTTNG_ERR_NOMEM
;
2073 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2075 /* Extended infos are included at the end of the payload. */
2076 list_ret
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
2078 cmd_ctx
->lsm
.u
.list
.channel_name
,
2079 &cmd_ctx
->reply_payload
);
2081 /* Return value is a negative lttng_error_code. */
2086 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2087 sizeof(cmd_header
) - original_payload_size
;
2088 update_lttng_msg(cmd_ctx
, sizeof(cmd_header
), payload_size
);
2093 case LTTNG_LIST_SESSIONS
:
2095 unsigned int nr_sessions
;
2096 void *sessions_payload
;
2099 session_lock_list();
2100 nr_sessions
= lttng_sessions_count(
2101 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2102 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2104 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
2105 (sizeof(struct lttng_session_extended
) * nr_sessions
);
2106 sessions_payload
= zmalloc(payload_len
);
2108 if (!sessions_payload
) {
2109 session_unlock_list();
2114 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
2115 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2116 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
2117 session_unlock_list();
2119 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
2121 free(sessions_payload
);
2130 case LTTNG_REGISTER_CONSUMER
:
2132 struct consumer_data
*cdata
;
2134 switch (cmd_ctx
->lsm
.domain
.type
) {
2135 case LTTNG_DOMAIN_KERNEL
:
2136 cdata
= &kconsumer_data
;
2139 ret
= LTTNG_ERR_UND
;
2143 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
2144 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
2147 case LTTNG_DATA_PENDING
:
2150 uint8_t pending_ret_byte
;
2152 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
2157 * This function may returns 0 or 1 to indicate whether or not
2158 * there is data pending. In case of error, it should return an
2159 * LTTNG_ERR code. However, some code paths may still return
2160 * a nondescript error code, which we handle by returning an
2163 if (pending_ret
== 0 || pending_ret
== 1) {
2165 * ret will be set to LTTNG_OK at the end of
2168 } else if (pending_ret
< 0) {
2169 ret
= LTTNG_ERR_UNK
;
2176 pending_ret_byte
= (uint8_t) pending_ret
;
2178 /* 1 byte to return whether or not data is pending */
2179 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
2180 &pending_ret_byte
, 1);
2189 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2191 uint32_t snapshot_id
;
2192 struct lttcomm_lttng_output_id reply
;
2194 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
2195 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
),
2197 if (ret
!= LTTNG_OK
) {
2200 reply
.id
= snapshot_id
;
2202 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
2208 /* Copy output list into message payload */
2212 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2214 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
2215 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
));
2218 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2221 struct lttng_snapshot_output
*outputs
= NULL
;
2223 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
2224 if (nb_output
< 0) {
2229 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
2230 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
2231 nb_output
* sizeof(struct lttng_snapshot_output
));
2241 case LTTNG_SNAPSHOT_RECORD
:
2243 ret
= cmd_snapshot_record(cmd_ctx
->session
,
2244 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_record
.output
),
2245 cmd_ctx
->lsm
.u
.snapshot_record
.wait
);
2248 case LTTNG_CREATE_SESSION_EXT
:
2250 struct lttng_dynamic_buffer payload
;
2251 struct lttng_session_descriptor
*return_descriptor
= NULL
;
2253 lttng_dynamic_buffer_init(&payload
);
2254 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2255 if (ret
!= LTTNG_OK
) {
2259 ret
= lttng_session_descriptor_serialize(return_descriptor
,
2262 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2263 lttng_session_descriptor_destroy(return_descriptor
);
2264 ret
= LTTNG_ERR_NOMEM
;
2267 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
2270 lttng_session_descriptor_destroy(return_descriptor
);
2271 ret
= LTTNG_ERR_NOMEM
;
2274 lttng_dynamic_buffer_reset(&payload
);
2275 lttng_session_descriptor_destroy(return_descriptor
);
2279 case LTTNG_SAVE_SESSION
:
2281 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
2285 case LTTNG_SET_SESSION_SHM_PATH
:
2287 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2288 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2291 case LTTNG_REGENERATE_METADATA
:
2293 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2296 case LTTNG_REGENERATE_STATEDUMP
:
2298 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2301 case LTTNG_REGISTER_TRIGGER
:
2303 struct lttng_trigger
*payload_trigger
;
2304 struct lttng_trigger
*return_trigger
;
2305 size_t original_reply_payload_size
;
2306 size_t reply_payload_size
;
2307 const struct lttng_credentials cmd_creds
= {
2308 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2309 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2312 ret
= setup_empty_lttng_msg(cmd_ctx
);
2314 ret
= LTTNG_ERR_NOMEM
;
2318 ret
= receive_lttng_trigger(
2319 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2320 if (ret
!= LTTNG_OK
) {
2324 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2326 ret
= cmd_register_trigger(&cmd_creds
, payload_trigger
,
2327 notification_thread_handle
, &return_trigger
);
2328 if (ret
!= LTTNG_OK
) {
2329 lttng_trigger_put(payload_trigger
);
2333 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2334 lttng_trigger_put(payload_trigger
);
2335 lttng_trigger_put(return_trigger
);
2337 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2338 ret
= LTTNG_ERR_NOMEM
;
2342 reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2343 original_reply_payload_size
;
2345 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2350 case LTTNG_UNREGISTER_TRIGGER
:
2352 struct lttng_trigger
*payload_trigger
;
2353 const struct lttng_credentials cmd_creds
= {
2354 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2355 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2358 ret
= receive_lttng_trigger(
2359 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2360 if (ret
!= LTTNG_OK
) {
2364 ret
= cmd_unregister_trigger(&cmd_creds
, payload_trigger
,
2365 notification_thread_handle
);
2366 lttng_trigger_put(payload_trigger
);
2369 case LTTNG_ROTATE_SESSION
:
2371 struct lttng_rotate_session_return rotate_return
;
2373 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2375 memset(&rotate_return
, 0, sizeof(rotate_return
));
2376 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2377 DBG("Kernel tracer version is not compatible with the rotation feature");
2378 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2382 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2384 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2390 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2391 sizeof(rotate_return
));
2400 case LTTNG_ROTATION_GET_INFO
:
2402 struct lttng_rotation_get_info_return get_info_return
;
2404 memset(&get_info_return
, 0, sizeof(get_info_return
));
2405 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2406 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2412 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2413 sizeof(get_info_return
));
2422 case LTTNG_ROTATION_SET_SCHEDULE
:
2425 enum lttng_rotation_schedule_type schedule_type
;
2428 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2429 DBG("Kernel tracer version does not support session rotations");
2430 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2434 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2435 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2436 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2438 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
2442 notification_thread_handle
);
2443 if (ret
!= LTTNG_OK
) {
2449 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2451 struct lttng_session_list_schedules_return schedules
= {
2452 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2453 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2454 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2455 .size
.value
= cmd_ctx
->session
->rotate_size
,
2458 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2468 case LTTNG_CLEAR_SESSION
:
2470 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2473 case LTTNG_LIST_TRIGGERS
:
2475 struct lttng_triggers
*return_triggers
= NULL
;
2476 size_t original_payload_size
;
2477 size_t payload_size
;
2479 ret
= setup_empty_lttng_msg(cmd_ctx
);
2481 ret
= LTTNG_ERR_NOMEM
;
2485 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2487 ret
= cmd_list_triggers(cmd_ctx
,
2488 notification_thread_handle
, &return_triggers
);
2489 if (ret
!= LTTNG_OK
) {
2493 assert(return_triggers
);
2494 ret
= lttng_triggers_serialize(
2495 return_triggers
, &cmd_ctx
->reply_payload
);
2496 lttng_triggers_destroy(return_triggers
);
2498 ERR("Failed to serialize triggers in reply to `list triggers` command");
2499 ret
= LTTNG_ERR_NOMEM
;
2503 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2504 original_payload_size
;
2506 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2511 case LTTNG_LIST_MAP_VALUES
:
2513 struct lttng_map_content
*return_map_content
= NULL
;
2514 struct lttng_map
*payload_map
= NULL
;
2515 struct lttng_map_query
*payload_map_query
= NULL
;
2516 size_t original_reply_payload_size
;
2517 size_t reply_payload_size
;
2519 ret
= setup_empty_lttng_msg(cmd_ctx
);
2521 ret
= LTTNG_ERR_NOMEM
;
2525 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2527 ret
= receive_lttng_map(*sock
, sock_error
,
2528 cmd_ctx
->lsm
.u
.list_map_values
.map_length
,
2530 if (ret
!= LTTNG_OK
) {
2534 ret
= receive_lttng_map_query(*sock
, sock_error
,
2535 cmd_ctx
->lsm
.u
.list_map_values
.query_length
,
2536 &payload_map_query
);
2537 if (ret
!= LTTNG_OK
) {
2541 ret
= cmd_list_map_values(cmd_ctx
->lsm
.session
.name
,
2542 payload_map
, payload_map_query
,
2543 &return_map_content
);
2544 if (ret
!= LTTNG_OK
) {
2547 assert(return_map_content
);
2548 ret
= lttng_map_content_serialize(return_map_content
,
2549 &cmd_ctx
->reply_payload
);
2550 lttng_map_content_destroy(return_map_content
);
2552 ERR("Failed to serialize key-value pair list in reply to `list map values` command");
2553 ret
= LTTNG_ERR_NOMEM
;
2557 reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2558 original_reply_payload_size
;
2560 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2566 ret
= LTTNG_ERR_UND
;
2571 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2572 DBG("Missing llm header, creating one.");
2573 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2577 /* Set return code */
2578 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2580 if (cmd_ctx
->session
) {
2581 session_unlock(cmd_ctx
->session
);
2582 session_put(cmd_ctx
->session
);
2583 cmd_ctx
->session
= NULL
;
2585 if (need_tracing_session
) {
2586 session_unlock_list();
2589 assert(!rcu_read_ongoing());
2593 static int create_client_sock(void)
2595 int ret
, client_sock
;
2596 const mode_t old_umask
= umask(0);
2598 /* Create client tool unix socket */
2599 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2600 if (client_sock
< 0) {
2601 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2606 /* Set the cloexec flag */
2607 ret
= utils_set_fd_cloexec(client_sock
);
2609 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2610 "Continuing but note that the consumer daemon will have a "
2611 "reference to this socket on exec()", client_sock
);
2614 /* File permission MUST be 660 */
2615 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2617 ERR("Set file permissions failed: %s",
2618 config
.client_unix_sock_path
.value
);
2620 (void) lttcomm_close_unix_sock(client_sock
);
2624 DBG("Created client socket (fd = %i)", client_sock
);
2631 static void cleanup_client_thread(void *data
)
2633 struct lttng_pipe
*quit_pipe
= data
;
2635 lttng_pipe_destroy(quit_pipe
);
2638 static void thread_init_cleanup(void *data
)
2640 set_thread_status(false);
2644 * This thread manage all clients request using the unix client socket for
2647 static void *thread_manage_clients(void *data
)
2649 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2651 uint32_t revents
, nb_fd
;
2652 struct lttng_poll_event events
;
2653 const int client_sock
= thread_state
.client_sock
;
2654 struct lttng_pipe
*quit_pipe
= data
;
2655 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2656 struct command_ctx cmd_ctx
= {};
2658 DBG("[thread] Manage client started");
2660 lttng_payload_init(&cmd_ctx
.reply_payload
);
2662 is_root
= (getuid() == 0);
2664 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2666 rcu_register_thread();
2668 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2670 health_code_update();
2672 ret
= lttcomm_listen_unix_sock(client_sock
);
2678 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2679 * more will be added to this poll set.
2681 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2683 goto error_create_poll
;
2686 /* Add the application registration socket */
2687 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2692 /* Add thread quit pipe */
2693 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2698 /* Set state as running. */
2699 set_thread_status(true);
2700 pthread_cleanup_pop(0);
2702 /* This testpoint is after we signal readiness to the parent. */
2703 if (testpoint(sessiond_thread_manage_clients
)) {
2707 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2711 health_code_update();
2714 const struct cmd_completion_handler
*cmd_completion_handler
;
2716 cmd_ctx
.creds
= (lttng_sock_cred
) {
2720 cmd_ctx
.session
= NULL
;
2721 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2722 cmd_ctx
.lttng_msg_size
= 0;
2724 DBG("Accepting client command ...");
2726 /* Inifinite blocking call, waiting for transmission */
2728 health_poll_entry();
2729 ret
= lttng_poll_wait(&events
, -1);
2733 * Restart interrupted system call.
2735 if (errno
== EINTR
) {
2743 for (i
= 0; i
< nb_fd
; i
++) {
2744 revents
= LTTNG_POLL_GETEV(&events
, i
);
2745 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2747 health_code_update();
2749 if (pollfd
== thread_quit_pipe_fd
) {
2753 /* Event on the registration socket */
2754 if (revents
& LPOLLIN
) {
2756 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2757 ERR("Client socket poll error");
2760 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2766 DBG("Wait for client response");
2768 health_code_update();
2770 sock
= lttcomm_accept_unix_sock(client_sock
);
2776 * Set the CLOEXEC flag. Return code is useless because either way, the
2779 (void) utils_set_fd_cloexec(sock
);
2781 /* Set socket option for credentials retrieval */
2782 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2787 health_code_update();
2790 * Data is received from the lttng client. The struct
2791 * lttcomm_session_msg (lsm) contains the command and data request of
2794 DBG("Receiving data from client ...");
2795 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2796 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2797 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2798 DBG("Incomplete recv() from client... continuing");
2807 health_code_update();
2809 // TODO: Validate cmd_ctx including sanity check for
2810 // security purpose.
2812 rcu_thread_online();
2814 * This function dispatch the work to the kernel or userspace tracer
2815 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2816 * informations for the client. The command context struct contains
2817 * everything this function may needs.
2819 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2820 rcu_thread_offline();
2830 * TODO: Inform client somehow of the fatal error. At
2831 * this point, ret < 0 means that a zmalloc failed
2832 * (ENOMEM). Error detected but still accept
2833 * command, unless a socket error has been
2839 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2840 WARN("Command returned an invalid status code, returning unknown error: "
2841 "command type = %s (%d), ret = %d",
2842 lttcomm_sessiond_command_str(cmd_ctx
.lsm
.cmd_type
),
2843 cmd_ctx
.lsm
.cmd_type
, ret
);
2844 ret
= LTTNG_ERR_UNK
;
2847 cmd_completion_handler
= cmd_pop_completion_handler();
2848 if (cmd_completion_handler
) {
2849 enum lttng_error_code completion_code
;
2851 completion_code
= cmd_completion_handler
->run(
2852 cmd_completion_handler
->data
);
2853 if (completion_code
!= LTTNG_OK
) {
2858 health_code_update();
2861 struct lttng_payload_view view
=
2862 lttng_payload_view_from_payload(
2863 &cmd_ctx
.reply_payload
,
2865 struct lttcomm_lttng_msg
*llm
= (typeof(
2866 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2868 assert(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2869 assert(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2871 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2873 DBG("Sending response (size: %d, retcode: %s (%d))",
2874 cmd_ctx
.lttng_msg_size
,
2875 lttng_strerror(-llm
->ret_code
),
2877 ret
= send_unix_sock(sock
, &view
);
2879 ERR("Failed to send data back to client");
2882 /* End of transmission */
2890 health_code_update();
2902 lttng_poll_clean(&events
);
2906 unlink(config
.client_unix_sock_path
.value
);
2907 ret
= close(client_sock
);
2914 ERR("Health error occurred in %s", __func__
);
2917 health_unregister(health_sessiond
);
2919 DBG("Client thread dying");
2920 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2921 rcu_unregister_thread();
2926 bool shutdown_client_thread(void *thread_data
)
2928 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2929 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2931 return notify_thread_pipe(write_fd
) == 1;
2934 struct lttng_thread
*launch_client_thread(void)
2936 bool thread_running
;
2937 struct lttng_pipe
*client_quit_pipe
;
2938 struct lttng_thread
*thread
= NULL
;
2939 int client_sock_fd
= -1;
2941 sem_init(&thread_state
.ready
, 0, 0);
2942 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2943 if (!client_quit_pipe
) {
2947 client_sock_fd
= create_client_sock();
2948 if (client_sock_fd
< 0) {
2952 thread_state
.client_sock
= client_sock_fd
;
2953 thread
= lttng_thread_create("Client management",
2954 thread_manage_clients
,
2955 shutdown_client_thread
,
2956 cleanup_client_thread
,
2961 /* The client thread now owns the client sock fd and the quit pipe. */
2962 client_sock_fd
= -1;
2963 client_quit_pipe
= NULL
;
2966 * This thread is part of the threads that need to be fully
2967 * initialized before the session daemon is marked as "ready".
2969 thread_running
= wait_thread_status();
2970 if (!thread_running
) {
2975 if (client_sock_fd
>= 0) {
2976 if (close(client_sock_fd
)) {
2977 PERROR("Failed to close client socket");
2980 lttng_thread_put(thread
);
2981 cleanup_client_thread(client_quit_pipe
);