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
14 #include <common/compat/getenv.h>
15 #include <common/unix.h>
16 #include <common/utils.h>
17 #include <lttng/userspace-probe-internal.h>
18 #include <lttng/event-internal.h>
19 #include <lttng/session-internal.h>
20 #include <lttng/session-descriptor-internal.h>
21 #include <lttng/tracker-internal.h>
24 #include "lttng-sessiond.h"
28 #include "health-sessiond.h"
29 #include "testpoint.h"
31 #include "manage-consumer.h"
33 #include "agent-thread.h"
37 static struct thread_state
{
43 static void set_thread_status(bool running
)
45 DBG("Marking client thread's state as %s", running
? "running" : "error");
46 thread_state
.running
= running
;
47 sem_post(&thread_state
.ready
);
50 static bool wait_thread_status(void)
52 DBG("Waiting for client thread to be ready");
53 sem_wait(&thread_state
.ready
);
54 if (thread_state
.running
) {
55 DBG("Client thread is ready");
57 ERR("Initialization of client thread failed");
60 return thread_state
.running
;
64 * Setup the outgoing data buffer for the response (llm) by allocating the
65 * right amount of memory and copying the original information from the lsm
68 * Return 0 on success, negative value on error.
70 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
71 const void *payload_buf
, size_t payload_len
,
72 const void *cmd_header_buf
, size_t cmd_header_len
)
75 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
76 const size_t cmd_header_offset
= header_len
;
77 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
78 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
81 cmd_ctx
->llm
= zmalloc(total_msg_size
);
83 if (cmd_ctx
->llm
== NULL
) {
89 /* Copy common data */
90 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
91 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
92 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
93 cmd_ctx
->llm
->data_size
= payload_len
;
94 cmd_ctx
->lttng_msg_size
= total_msg_size
;
96 /* Copy command header */
98 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
104 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
113 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
114 * exec or it will fail.
116 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
118 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
122 * Fork and exec a consumer daemon (consumerd).
124 * Return pid if successful else -1.
126 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
130 const char *consumer_to_use
;
131 const char *verbosity
;
134 DBG("Spawning consumerd");
141 if (config
.verbose_consumer
) {
142 verbosity
= "--verbose";
143 } else if (lttng_opt_quiet
) {
144 verbosity
= "--quiet";
149 switch (consumer_data
->type
) {
150 case LTTNG_CONSUMER_KERNEL
:
152 * Find out which consumerd to execute. We will first try the
153 * 64-bit path, then the sessiond's installation directory, and
154 * fallback on the 32-bit one,
156 DBG3("Looking for a kernel consumer at these locations:");
157 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
158 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
159 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
160 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
161 DBG3("Found location #1");
162 consumer_to_use
= config
.consumerd64_bin_path
.value
;
163 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
164 DBG3("Found location #2");
165 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
166 } else if (config
.consumerd32_bin_path
.value
&&
167 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
168 DBG3("Found location #3");
169 consumer_to_use
= config
.consumerd32_bin_path
.value
;
171 DBG("Could not find any valid consumerd executable");
175 DBG("Using kernel consumer at: %s", consumer_to_use
);
176 (void) execl(consumer_to_use
,
177 "lttng-consumerd", verbosity
, "-k",
178 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
179 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
180 "--group", config
.tracing_group_name
.value
,
183 case LTTNG_CONSUMER64_UST
:
185 if (config
.consumerd64_lib_dir
.value
) {
190 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
194 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
195 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
200 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
201 if (tmp
[0] != '\0') {
205 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
212 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
213 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
214 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
215 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
216 "--group", config
.tracing_group_name
.value
,
220 case LTTNG_CONSUMER32_UST
:
222 if (config
.consumerd32_lib_dir
.value
) {
227 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
231 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
232 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
237 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
238 if (tmp
[0] != '\0') {
242 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
249 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
250 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
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
,
258 ERR("unknown consumer type");
262 PERROR("Consumer execl()");
264 /* Reaching this point, we got a failure on our execl(). */
266 } else if (pid
> 0) {
269 PERROR("start consumer fork");
277 * Spawn the consumerd daemon and session daemon thread.
279 static int start_consumerd(struct consumer_data
*consumer_data
)
284 * Set the listen() state on the socket since there is a possible race
285 * between the exec() of the consumer daemon and this call if place in the
286 * consumer thread. See bug #366 for more details.
288 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
293 pthread_mutex_lock(&consumer_data
->pid_mutex
);
294 if (consumer_data
->pid
!= 0) {
295 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
299 ret
= spawn_consumerd(consumer_data
);
301 ERR("Spawning consumerd failed");
302 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
306 /* Setting up the consumer_data pid */
307 consumer_data
->pid
= ret
;
308 DBG2("Consumer pid %d", consumer_data
->pid
);
309 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
311 DBG2("Spawning consumer control thread");
312 ret
= spawn_consumer_thread(consumer_data
);
314 ERR("Fatal error spawning consumer control thread");
322 /* Cleanup already created sockets on error. */
323 if (consumer_data
->err_sock
>= 0) {
326 err
= close(consumer_data
->err_sock
);
328 PERROR("close consumer data error socket");
335 * Copy consumer output from the tracing session to the domain session. The
336 * function also applies the right modification on a per domain basis for the
337 * trace files destination directory.
339 * Should *NOT* be called with RCU read-side lock held.
341 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
344 const char *dir_name
;
345 struct consumer_output
*consumer
;
348 assert(session
->consumer
);
351 case LTTNG_DOMAIN_KERNEL
:
352 DBG3("Copying tracing session consumer output in kernel session");
354 * XXX: We should audit the session creation and what this function
355 * does "extra" in order to avoid a destroy since this function is used
356 * in the domain session creation (kernel and ust) only. Same for UST
359 if (session
->kernel_session
->consumer
) {
360 consumer_output_put(session
->kernel_session
->consumer
);
362 session
->kernel_session
->consumer
=
363 consumer_copy_output(session
->consumer
);
364 /* Ease our life a bit for the next part */
365 consumer
= session
->kernel_session
->consumer
;
366 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
368 case LTTNG_DOMAIN_JUL
:
369 case LTTNG_DOMAIN_LOG4J
:
370 case LTTNG_DOMAIN_PYTHON
:
371 case LTTNG_DOMAIN_UST
:
372 DBG3("Copying tracing session consumer output in UST session");
373 if (session
->ust_session
->consumer
) {
374 consumer_output_put(session
->ust_session
->consumer
);
376 session
->ust_session
->consumer
=
377 consumer_copy_output(session
->consumer
);
378 /* Ease our life a bit for the next part */
379 consumer
= session
->ust_session
->consumer
;
380 dir_name
= DEFAULT_UST_TRACE_DIR
;
383 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
387 /* Append correct directory to subdir */
388 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
389 sizeof(consumer
->domain_subdir
));
394 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
402 * Create an UST session and add it to the session ust list.
404 * Should *NOT* be called with RCU read-side lock held.
406 static int create_ust_session(struct ltt_session
*session
,
407 const struct lttng_domain
*domain
)
410 struct ltt_ust_session
*lus
= NULL
;
414 assert(session
->consumer
);
416 switch (domain
->type
) {
417 case LTTNG_DOMAIN_JUL
:
418 case LTTNG_DOMAIN_LOG4J
:
419 case LTTNG_DOMAIN_PYTHON
:
420 case LTTNG_DOMAIN_UST
:
423 ERR("Unknown UST domain on create session %d", domain
->type
);
424 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
428 DBG("Creating UST session");
430 lus
= trace_ust_create_session(session
->id
);
432 ret
= LTTNG_ERR_UST_SESS_FAIL
;
436 lus
->uid
= session
->uid
;
437 lus
->gid
= session
->gid
;
438 lus
->output_traces
= session
->output_traces
;
439 lus
->snapshot_mode
= session
->snapshot_mode
;
440 lus
->live_timer_interval
= session
->live_timer
;
441 session
->ust_session
= lus
;
442 if (session
->shm_path
[0]) {
443 strncpy(lus
->root_shm_path
, session
->shm_path
,
444 sizeof(lus
->root_shm_path
));
445 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
446 strncpy(lus
->shm_path
, session
->shm_path
,
447 sizeof(lus
->shm_path
));
448 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
449 strncat(lus
->shm_path
, "/ust",
450 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
452 /* Copy session output to the newly created UST session */
453 ret
= copy_session_consumer(domain
->type
, session
);
454 if (ret
!= LTTNG_OK
) {
462 session
->ust_session
= NULL
;
467 * Create a kernel tracer session then create the default channel.
469 static int create_kernel_session(struct ltt_session
*session
)
473 DBG("Creating kernel session");
475 ret
= kernel_create_session(session
);
477 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
481 /* Code flow safety */
482 assert(session
->kernel_session
);
484 /* Copy session output to the newly created Kernel session */
485 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
486 if (ret
!= LTTNG_OK
) {
490 session
->kernel_session
->uid
= session
->uid
;
491 session
->kernel_session
->gid
= session
->gid
;
492 session
->kernel_session
->output_traces
= session
->output_traces
;
493 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
498 trace_kernel_destroy_session(session
->kernel_session
);
499 session
->kernel_session
= NULL
;
505 * Count number of session permitted by uid/gid.
507 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
510 struct ltt_session
*session
;
511 const struct ltt_session_list
*session_list
= session_get_list();
513 DBG("Counting number of available session for UID %d GID %d",
515 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
516 if (!session_get(session
)) {
519 session_lock(session
);
520 /* Only count the sessions the user can control. */
521 if (session_access_ok(session
, uid
, gid
) &&
522 !session
->destroyed
) {
525 session_unlock(session
);
526 session_put(session
);
531 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
532 int *sock_error
, struct lttng_event
*event
)
535 struct lttng_userspace_probe_location
*probe_location
;
536 struct lttng_dynamic_buffer probe_location_buffer
;
537 struct lttng_buffer_view buffer_view
;
540 * Create a buffer to store the serialized version of the probe
543 lttng_dynamic_buffer_init(&probe_location_buffer
);
544 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
545 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
547 ret
= LTTNG_ERR_NOMEM
;
552 * Receive the probe location.
554 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
555 probe_location_buffer
.size
);
557 DBG("Nothing recv() from client var len data... continuing");
559 lttng_dynamic_buffer_reset(&probe_location_buffer
);
560 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
564 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
565 &probe_location_buffer
, 0, probe_location_buffer
.size
);
568 * Extract the probe location from the serialized version.
570 ret
= lttng_userspace_probe_location_create_from_buffer(
571 &buffer_view
, &probe_location
);
573 WARN("Failed to create a userspace probe location from the received buffer");
574 lttng_dynamic_buffer_reset( &probe_location_buffer
);
575 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
580 * Receive the file descriptor to the target binary from the client.
582 DBG("Receiving userspace probe target FD from client ...");
583 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
585 DBG("Nothing recv() from client userspace probe fd... continuing");
587 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
592 * Set the file descriptor received from the client through the unix
593 * socket in the probe location.
595 ret
= lttng_userspace_probe_location_set_binary_fd(probe_location
, fd
);
597 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
601 /* Attach the probe location to the event. */
602 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
604 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
608 lttng_dynamic_buffer_reset(&probe_location_buffer
);
614 * Version of setup_lttng_msg() without command header.
616 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
617 void *payload_buf
, size_t payload_len
)
619 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
623 * Free memory of a command context structure.
625 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
627 DBG("Clean command context structure");
629 if ((*cmd_ctx
)->llm
) {
630 free((*cmd_ctx
)->llm
);
632 if ((*cmd_ctx
)->lsm
) {
633 free((*cmd_ctx
)->lsm
);
641 * Check if the current kernel tracer supports the session rotation feature.
642 * Return 1 if it does, 0 otherwise.
644 static int check_rotate_compatible(void)
648 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
649 DBG("Kernel tracer version is not compatible with the rotation feature");
657 * Send data on a unix socket using the liblttsessiondcomm API.
659 * Return lttcomm error code.
661 static int send_unix_sock(int sock
, void *buf
, size_t len
)
663 /* Check valid length */
668 return lttcomm_send_unix_sock(sock
, buf
, len
);
672 * Process the command requested by the lttng client within the command
673 * context structure. This function make sure that the return structure (llm)
674 * is set and ready for transmission before returning.
676 * Return any error encountered or 0 for success.
678 * "sock" is only used for special-case var. len data.
679 * A command may assume the ownership of the socket, in which case its value
680 * should be set to -1.
682 * Should *NOT* be called with RCU read-side lock held.
684 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
688 int need_tracing_session
= 1;
690 int need_consumerd
= 1;
692 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
694 assert(!rcu_read_ongoing());
698 switch (cmd_ctx
->lsm
->cmd_type
) {
699 case LTTNG_CREATE_SESSION_EXT
:
700 case LTTNG_DESTROY_SESSION
:
701 case LTTNG_LIST_SESSIONS
:
702 case LTTNG_LIST_DOMAINS
:
703 case LTTNG_START_TRACE
:
704 case LTTNG_STOP_TRACE
:
705 case LTTNG_DATA_PENDING
:
706 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
707 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
708 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
709 case LTTNG_SNAPSHOT_RECORD
:
710 case LTTNG_SAVE_SESSION
:
711 case LTTNG_SET_SESSION_SHM_PATH
:
712 case LTTNG_REGENERATE_METADATA
:
713 case LTTNG_REGENERATE_STATEDUMP
:
714 case LTTNG_ROTATE_SESSION
:
715 case LTTNG_ROTATION_GET_INFO
:
716 case LTTNG_ROTATION_SET_SCHEDULE
:
717 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
718 case LTTNG_CLEAR_SESSION
:
719 case LTTNG_LIST_TRIGGERS
:
726 /* Needs a functioning consumerd */
727 switch (cmd_ctx
->lsm
->cmd_type
) {
728 case LTTNG_REGISTER_TRIGGER
:
729 case LTTNG_UNREGISTER_TRIGGER
:
737 if (config
.no_kernel
&& need_domain
738 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
740 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
742 ret
= LTTNG_ERR_KERN_NA
;
747 /* Deny register consumer if we already have a spawned consumer. */
748 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
749 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
750 if (kconsumer_data
.pid
> 0) {
751 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
752 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
755 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
759 * Check for command that don't needs to allocate a returned payload. We do
760 * this here so we don't have to make the call for no payload at each
763 switch(cmd_ctx
->lsm
->cmd_type
) {
764 case LTTNG_LIST_SESSIONS
:
765 case LTTNG_LIST_TRACEPOINTS
:
766 case LTTNG_LIST_TRACEPOINT_FIELDS
:
767 case LTTNG_LIST_DOMAINS
:
768 case LTTNG_LIST_CHANNELS
:
769 case LTTNG_LIST_EVENTS
:
770 case LTTNG_LIST_SYSCALLS
:
771 case LTTNG_LIST_TRACKER_IDS
:
772 case LTTNG_DATA_PENDING
:
773 case LTTNG_ROTATE_SESSION
:
774 case LTTNG_ROTATION_GET_INFO
:
775 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
776 case LTTNG_REGISTER_TRIGGER
:
777 case LTTNG_LIST_TRIGGERS
:
780 /* Setup lttng message with no payload */
781 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
783 /* This label does not try to unlock the session */
784 goto init_setup_error
;
788 /* Commands that DO NOT need a session. */
789 switch (cmd_ctx
->lsm
->cmd_type
) {
790 case LTTNG_CREATE_SESSION_EXT
:
791 case LTTNG_LIST_SESSIONS
:
792 case LTTNG_LIST_TRACEPOINTS
:
793 case LTTNG_LIST_SYSCALLS
:
794 case LTTNG_LIST_TRACEPOINT_FIELDS
:
795 case LTTNG_SAVE_SESSION
:
796 case LTTNG_REGISTER_TRIGGER
:
797 case LTTNG_UNREGISTER_TRIGGER
:
798 case LTTNG_LIST_TRIGGERS
:
799 need_tracing_session
= 0;
802 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
804 * We keep the session list lock across _all_ commands
805 * for now, because the per-session lock does not
806 * handle teardown properly.
809 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
810 if (cmd_ctx
->session
== NULL
) {
811 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
814 /* Acquire lock for the session */
815 session_lock(cmd_ctx
->session
);
821 * Commands that need a valid session but should NOT create one if none
822 * exists. Instead of creating one and destroying it when the command is
823 * handled, process that right before so we save some round trip in useless
826 switch (cmd_ctx
->lsm
->cmd_type
) {
827 case LTTNG_DISABLE_CHANNEL
:
828 case LTTNG_DISABLE_EVENT
:
829 switch (cmd_ctx
->lsm
->domain
.type
) {
830 case LTTNG_DOMAIN_KERNEL
:
831 if (!cmd_ctx
->session
->kernel_session
) {
832 ret
= LTTNG_ERR_NO_CHANNEL
;
836 case LTTNG_DOMAIN_JUL
:
837 case LTTNG_DOMAIN_LOG4J
:
838 case LTTNG_DOMAIN_PYTHON
:
839 case LTTNG_DOMAIN_UST
:
840 if (!cmd_ctx
->session
->ust_session
) {
841 ret
= LTTNG_ERR_NO_CHANNEL
;
846 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
858 * Check domain type for specific "pre-action".
860 switch (cmd_ctx
->lsm
->domain
.type
) {
861 case LTTNG_DOMAIN_KERNEL
:
863 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
867 /* Kernel tracer check */
868 if (!kernel_tracer_is_initialized()) {
869 /* Basically, load kernel tracer modules */
870 ret
= init_kernel_tracer();
876 /* Consumer is in an ERROR state. Report back to client */
877 if (need_consumerd
&& uatomic_read(&kernel_consumerd_state
) ==
879 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
883 /* Need a session for kernel command */
884 if (need_tracing_session
) {
885 if (cmd_ctx
->session
->kernel_session
== NULL
) {
886 ret
= create_kernel_session(cmd_ctx
->session
);
887 if (ret
!= LTTNG_OK
) {
888 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
893 /* Start the kernel consumer daemon */
894 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
895 if (kconsumer_data
.pid
== 0 &&
896 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
897 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
898 ret
= start_consumerd(&kconsumer_data
);
900 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
903 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
905 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
909 * The consumer was just spawned so we need to add the socket to
910 * the consumer output of the session if exist.
912 ret
= consumer_create_socket(&kconsumer_data
,
913 cmd_ctx
->session
->kernel_session
->consumer
);
920 case LTTNG_DOMAIN_JUL
:
921 case LTTNG_DOMAIN_LOG4J
:
922 case LTTNG_DOMAIN_PYTHON
:
923 if (!agent_tracing_is_enabled()) {
924 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
928 case LTTNG_DOMAIN_UST
:
930 if (!ust_app_supported()) {
931 ret
= LTTNG_ERR_NO_UST
;
935 /* Consumer is in an ERROR state. Report back to client */
936 if (need_consumerd
&& uatomic_read(&ust_consumerd_state
) ==
938 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
942 if (need_tracing_session
) {
943 /* Create UST session if none exist. */
944 if (cmd_ctx
->session
->ust_session
== NULL
) {
945 ret
= create_ust_session(cmd_ctx
->session
,
946 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
));
947 if (ret
!= LTTNG_OK
) {
952 /* Start the UST consumer daemons */
954 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
955 if (config
.consumerd64_bin_path
.value
&&
956 ustconsumer64_data
.pid
== 0 &&
957 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
958 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
959 ret
= start_consumerd(&ustconsumer64_data
);
961 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
962 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
966 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
967 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
969 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
973 * Setup socket for consumer 64 bit. No need for atomic access
974 * since it was set above and can ONLY be set in this thread.
976 ret
= consumer_create_socket(&ustconsumer64_data
,
977 cmd_ctx
->session
->ust_session
->consumer
);
983 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
984 if (config
.consumerd32_bin_path
.value
&&
985 ustconsumer32_data
.pid
== 0 &&
986 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
987 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
988 ret
= start_consumerd(&ustconsumer32_data
);
990 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
991 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
995 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
996 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
998 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1002 * Setup socket for consumer 32 bit. No need for atomic access
1003 * since it was set above and can ONLY be set in this thread.
1005 ret
= consumer_create_socket(&ustconsumer32_data
,
1006 cmd_ctx
->session
->ust_session
->consumer
);
1018 /* Validate consumer daemon state when start/stop trace command */
1019 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1020 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1021 switch (cmd_ctx
->lsm
->domain
.type
) {
1022 case LTTNG_DOMAIN_NONE
:
1024 case LTTNG_DOMAIN_JUL
:
1025 case LTTNG_DOMAIN_LOG4J
:
1026 case LTTNG_DOMAIN_PYTHON
:
1027 case LTTNG_DOMAIN_UST
:
1028 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1029 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1033 case LTTNG_DOMAIN_KERNEL
:
1034 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1035 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1040 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1046 * Check that the UID or GID match that of the tracing session.
1047 * The root user can interact with all sessions.
1049 if (need_tracing_session
) {
1050 if (!session_access_ok(cmd_ctx
->session
,
1051 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1052 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1053 cmd_ctx
->session
->destroyed
) {
1054 ret
= LTTNG_ERR_EPERM
;
1060 * Send relayd information to consumer as soon as we have a domain and a
1063 if (cmd_ctx
->session
&& need_domain
) {
1065 * Setup relayd if not done yet. If the relayd information was already
1066 * sent to the consumer, this call will gracefully return.
1068 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1069 if (ret
!= LTTNG_OK
) {
1074 /* Process by command type */
1075 switch (cmd_ctx
->lsm
->cmd_type
) {
1076 case LTTNG_ADD_CONTEXT
:
1079 * An LTTNG_ADD_CONTEXT command might have a supplementary
1080 * payload if the context being added is an application context.
1082 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1083 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1084 char *provider_name
= NULL
, *context_name
= NULL
;
1085 size_t provider_name_len
=
1086 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1087 size_t context_name_len
=
1088 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1090 if (provider_name_len
== 0 || context_name_len
== 0) {
1092 * Application provider and context names MUST
1095 ret
= -LTTNG_ERR_INVALID
;
1099 provider_name
= zmalloc(provider_name_len
+ 1);
1100 if (!provider_name
) {
1101 ret
= -LTTNG_ERR_NOMEM
;
1104 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1107 context_name
= zmalloc(context_name_len
+ 1);
1108 if (!context_name
) {
1109 ret
= -LTTNG_ERR_NOMEM
;
1110 goto error_add_context
;
1112 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1115 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1118 goto error_add_context
;
1121 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1124 goto error_add_context
;
1129 * cmd_add_context assumes ownership of the provider and context
1132 ret
= cmd_add_context(cmd_ctx
->session
,
1133 cmd_ctx
->lsm
->domain
.type
,
1134 cmd_ctx
->lsm
->u
.context
.channel_name
,
1135 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.context
.ctx
),
1136 kernel_poll_pipe
[1]);
1138 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1139 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1141 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1142 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1148 case LTTNG_DISABLE_CHANNEL
:
1150 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1151 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1154 case LTTNG_DISABLE_EVENT
:
1158 * FIXME: handle filter; for now we just receive the filter's
1159 * bytecode along with the filter expression which are sent by
1160 * liblttng-ctl and discard them.
1162 * This fixes an issue where the client may block while sending
1163 * the filter payload and encounter an error because the session
1164 * daemon closes the socket without ever handling this data.
1166 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1167 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1170 char data
[LTTNG_FILTER_MAX_LEN
];
1172 DBG("Discarding disable event command payload of size %zu", count
);
1174 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1175 count
> sizeof(data
) ? sizeof(data
) : count
);
1180 count
-= (size_t) ret
;
1183 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1184 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1185 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.disable
.event
));
1188 case LTTNG_ENABLE_CHANNEL
:
1190 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1191 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1192 ret
= cmd_enable_channel(cmd_ctx
->session
,
1193 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1194 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.channel
.chan
),
1195 kernel_poll_pipe
[1]);
1198 case LTTNG_TRACK_ID
:
1200 struct lttng_tracker_id
*id
= NULL
;
1201 enum lttng_tracker_id_status status
;
1203 id
= lttng_tracker_id_create();
1205 ret
= LTTNG_ERR_NOMEM
;
1209 switch (cmd_ctx
->lsm
->u
.id_tracker
.id_type
) {
1211 status
= lttng_tracker_id_set_all(id
);
1213 case LTTNG_ID_VALUE
:
1214 status
= lttng_tracker_id_set_value(
1215 id
, cmd_ctx
->lsm
->u
.id_tracker
.u
.value
);
1217 case LTTNG_ID_STRING
:
1219 const size_t var_len
= cmd_ctx
->lsm
->u
.id_tracker
.u
.var_len
;
1220 char *string
= NULL
;
1222 string
= zmalloc(var_len
);
1224 lttng_tracker_id_destroy(id
);
1225 ret
= LTTNG_ERR_NOMEM
;
1228 DBG("Receiving var len tracker id string from client");
1229 ret
= lttcomm_recv_unix_sock(*sock
, string
, var_len
);
1231 DBG("Nothing received");
1234 lttng_tracker_id_destroy(id
);
1235 ret
= LTTNG_ERR_INVALID
;
1238 if (strnlen(string
, var_len
) != var_len
- 1) {
1239 DBG("String received as tracker ID is not NULL-terminated");
1241 lttng_tracker_id_destroy(id
);
1242 ret
= LTTNG_ERR_INVALID
;
1246 status
= lttng_tracker_id_set_string(id
, string
);
1251 lttng_tracker_id_destroy(id
);
1252 ret
= LTTNG_ERR_INVALID
;
1256 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
1257 ERR("Invalid value for tracker id");
1258 ret
= LTTNG_ERR_INVALID
;
1259 lttng_tracker_id_destroy(id
);
1263 ret
= cmd_track_id(cmd_ctx
->session
,
1264 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
1265 cmd_ctx
->lsm
->domain
.type
, id
);
1266 lttng_tracker_id_destroy(id
);
1269 case LTTNG_UNTRACK_ID
:
1271 struct lttng_tracker_id
*id
= NULL
;
1272 enum lttng_tracker_id_status status
;
1274 id
= lttng_tracker_id_create();
1276 switch (cmd_ctx
->lsm
->u
.id_tracker
.id_type
) {
1278 status
= lttng_tracker_id_set_all(id
);
1280 case LTTNG_ID_VALUE
:
1281 status
= lttng_tracker_id_set_value(
1282 id
, cmd_ctx
->lsm
->u
.id_tracker
.u
.value
);
1284 case LTTNG_ID_STRING
:
1286 const size_t var_len
= cmd_ctx
->lsm
->u
.id_tracker
.u
.var_len
;
1287 char *string
= NULL
;
1289 string
= zmalloc(var_len
);
1291 ret
= LTTNG_ERR_NOMEM
;
1292 lttng_tracker_id_destroy(id
);
1295 DBG("Receiving var len tracker id string from client");
1296 ret
= lttcomm_recv_unix_sock(*sock
, string
, var_len
);
1298 DBG("Nothing received");
1300 lttng_tracker_id_destroy(id
);
1302 ret
= LTTNG_ERR_INVALID
;
1305 if (strnlen(string
, var_len
) != var_len
- 1) {
1306 DBG("String received as tracker ID is not NULL-terminated");
1307 lttng_tracker_id_destroy(id
);
1309 ret
= LTTNG_ERR_INVALID
;
1312 status
= lttng_tracker_id_set_string(id
, string
);
1317 lttng_tracker_id_destroy(id
);
1318 ret
= LTTNG_ERR_INVALID
;
1322 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
1323 ERR("Invalid tracker id");
1324 lttng_tracker_id_destroy(id
);
1325 ret
= LTTNG_ERR_INVALID
;
1329 ret
= cmd_untrack_id(cmd_ctx
->session
,
1330 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
1331 cmd_ctx
->lsm
->domain
.type
, id
);
1332 lttng_tracker_id_destroy(id
);
1335 case LTTNG_ENABLE_EVENT
:
1337 struct lttng_event
*ev
= NULL
;
1338 struct lttng_event_exclusion
*exclusion
= NULL
;
1339 struct lttng_filter_bytecode
*bytecode
= NULL
;
1340 char *filter_expression
= NULL
;
1342 /* Handle exclusion events and receive it from the client. */
1343 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1344 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1346 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1347 (count
* LTTNG_SYMBOL_NAME_LEN
));
1349 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1353 DBG("Receiving var len exclusion event list from client ...");
1354 exclusion
->count
= count
;
1355 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1356 count
* LTTNG_SYMBOL_NAME_LEN
);
1358 DBG("Nothing recv() from client var len data... continuing");
1361 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1366 /* Get filter expression from client. */
1367 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1368 size_t expression_len
=
1369 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1371 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1372 ret
= LTTNG_ERR_FILTER_INVAL
;
1377 filter_expression
= zmalloc(expression_len
);
1378 if (!filter_expression
) {
1380 ret
= LTTNG_ERR_FILTER_NOMEM
;
1384 /* Receive var. len. data */
1385 DBG("Receiving var len filter's expression from client ...");
1386 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1389 DBG("Nothing recv() from client var len data... continuing");
1391 free(filter_expression
);
1393 ret
= LTTNG_ERR_FILTER_INVAL
;
1398 /* Handle filter and get bytecode from client. */
1399 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1400 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1402 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1403 ret
= LTTNG_ERR_FILTER_INVAL
;
1404 free(filter_expression
);
1409 bytecode
= zmalloc(bytecode_len
);
1411 free(filter_expression
);
1413 ret
= LTTNG_ERR_FILTER_NOMEM
;
1417 /* Receive var. len. data */
1418 DBG("Receiving var len filter's bytecode from client ...");
1419 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1421 DBG("Nothing recv() from client var len data... continuing");
1423 free(filter_expression
);
1426 ret
= LTTNG_ERR_FILTER_INVAL
;
1430 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1431 free(filter_expression
);
1434 ret
= LTTNG_ERR_FILTER_INVAL
;
1439 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.enable
.event
));
1441 DBG("Failed to copy event: %s",
1442 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1443 free(filter_expression
);
1446 ret
= LTTNG_ERR_NOMEM
;
1451 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1452 /* Expect a userspace probe description. */
1453 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1455 free(filter_expression
);
1458 lttng_event_destroy(ev
);
1463 ret
= cmd_enable_event(cmd_ctx
->session
,
1464 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1465 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1467 filter_expression
, bytecode
, exclusion
,
1468 kernel_poll_pipe
[1]);
1469 lttng_event_destroy(ev
);
1472 case LTTNG_LIST_TRACEPOINTS
:
1474 struct lttng_event
*events
;
1477 session_lock_list();
1478 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1479 session_unlock_list();
1480 if (nb_events
< 0) {
1481 /* Return value is a negative lttng_error_code. */
1487 * Setup lttng message with payload size set to the event list size in
1488 * bytes and then copy list into the llm payload.
1490 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1491 sizeof(struct lttng_event
) * nb_events
);
1501 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1503 struct lttng_event_field
*fields
;
1506 session_lock_list();
1507 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1509 session_unlock_list();
1510 if (nb_fields
< 0) {
1511 /* Return value is a negative lttng_error_code. */
1517 * Setup lttng message with payload size set to the event list size in
1518 * bytes and then copy list into the llm payload.
1520 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1521 sizeof(struct lttng_event_field
) * nb_fields
);
1531 case LTTNG_LIST_SYSCALLS
:
1533 struct lttng_event
*events
;
1536 nb_events
= cmd_list_syscalls(&events
);
1537 if (nb_events
< 0) {
1538 /* Return value is a negative lttng_error_code. */
1544 * Setup lttng message with payload size set to the event list size in
1545 * bytes and then copy list into the llm payload.
1547 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1548 sizeof(struct lttng_event
) * nb_events
);
1558 case LTTNG_LIST_TRACKER_IDS
:
1560 struct lttcomm_tracker_command_header cmd_header
;
1561 struct lttng_tracker_ids
*ids
= NULL
;
1562 enum lttng_tracker_id_status status
;
1563 unsigned int nr_ids
;
1564 struct lttng_dynamic_buffer buf
;
1566 ret
= cmd_list_tracker_ids(
1567 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
1568 cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1570 if (ret
!= LTTNG_OK
) {
1574 lttng_dynamic_buffer_init(&buf
);
1576 status
= lttng_tracker_ids_get_count(ids
, &nr_ids
);
1577 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
1578 ret
= -LTTNG_ERR_INVALID
;
1579 goto error_list_tracker
;
1582 cmd_header
.nb_tracker_id
= nr_ids
;
1584 ret
= lttng_tracker_ids_serialize(ids
, &buf
);
1586 goto error_list_tracker
;
1589 ret
= setup_lttng_msg(cmd_ctx
, buf
.data
, buf
.size
, &cmd_header
,
1590 sizeof(cmd_header
));
1592 lttng_tracker_ids_destroy(ids
);
1593 lttng_dynamic_buffer_reset(&buf
);
1601 case LTTNG_SET_CONSUMER_URI
:
1604 struct lttng_uri
*uris
;
1606 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1607 len
= nb_uri
* sizeof(struct lttng_uri
);
1610 ret
= LTTNG_ERR_INVALID
;
1614 uris
= zmalloc(len
);
1616 ret
= LTTNG_ERR_FATAL
;
1620 /* Receive variable len data */
1621 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1622 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1624 DBG("No URIs received from client... continuing");
1626 ret
= LTTNG_ERR_SESSION_FAIL
;
1631 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1633 if (ret
!= LTTNG_OK
) {
1640 case LTTNG_START_TRACE
:
1643 * On the first start, if we have a kernel session and we have
1644 * enabled time or size-based rotations, we have to make sure
1645 * the kernel tracer supports it.
1647 if (!cmd_ctx
->session
->has_been_started
&& \
1648 cmd_ctx
->session
->kernel_session
&& \
1649 (cmd_ctx
->session
->rotate_timer_period
|| \
1650 cmd_ctx
->session
->rotate_size
) && \
1651 !check_rotate_compatible()) {
1652 DBG("Kernel tracer version is not compatible with the rotation feature");
1653 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1656 ret
= cmd_start_trace(cmd_ctx
->session
);
1659 case LTTNG_STOP_TRACE
:
1661 ret
= cmd_stop_trace(cmd_ctx
->session
);
1664 case LTTNG_DESTROY_SESSION
:
1666 ret
= cmd_destroy_session(cmd_ctx
->session
,
1667 notification_thread_handle
,
1671 case LTTNG_LIST_DOMAINS
:
1674 struct lttng_domain
*domains
= NULL
;
1676 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1678 /* Return value is a negative lttng_error_code. */
1683 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1684 nb_dom
* sizeof(struct lttng_domain
));
1694 case LTTNG_LIST_CHANNELS
:
1696 ssize_t payload_size
;
1697 struct lttng_channel
*channels
= NULL
;
1699 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1700 cmd_ctx
->session
, &channels
);
1701 if (payload_size
< 0) {
1702 /* Return value is a negative lttng_error_code. */
1703 ret
= -payload_size
;
1707 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1718 case LTTNG_LIST_EVENTS
:
1721 struct lttng_event
*events
= NULL
;
1722 struct lttcomm_event_command_header cmd_header
;
1725 memset(&cmd_header
, 0, sizeof(cmd_header
));
1726 /* Extended infos are included at the end of events */
1727 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1728 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1729 &events
, &total_size
);
1732 /* Return value is a negative lttng_error_code. */
1737 cmd_header
.nb_events
= nb_event
;
1738 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1739 &cmd_header
, sizeof(cmd_header
));
1749 case LTTNG_LIST_SESSIONS
:
1751 unsigned int nr_sessions
;
1752 void *sessions_payload
;
1755 session_lock_list();
1756 nr_sessions
= lttng_sessions_count(
1757 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1758 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1760 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1761 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1762 sessions_payload
= zmalloc(payload_len
);
1764 if (!sessions_payload
) {
1765 session_unlock_list();
1770 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1771 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1772 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1773 session_unlock_list();
1775 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1777 free(sessions_payload
);
1786 case LTTNG_REGISTER_CONSUMER
:
1788 struct consumer_data
*cdata
;
1790 switch (cmd_ctx
->lsm
->domain
.type
) {
1791 case LTTNG_DOMAIN_KERNEL
:
1792 cdata
= &kconsumer_data
;
1795 ret
= LTTNG_ERR_UND
;
1799 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1800 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1803 case LTTNG_DATA_PENDING
:
1806 uint8_t pending_ret_byte
;
1808 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1813 * This function may returns 0 or 1 to indicate whether or not
1814 * there is data pending. In case of error, it should return an
1815 * LTTNG_ERR code. However, some code paths may still return
1816 * a nondescript error code, which we handle by returning an
1819 if (pending_ret
== 0 || pending_ret
== 1) {
1821 * ret will be set to LTTNG_OK at the end of
1824 } else if (pending_ret
< 0) {
1825 ret
= LTTNG_ERR_UNK
;
1832 pending_ret_byte
= (uint8_t) pending_ret
;
1834 /* 1 byte to return whether or not data is pending */
1835 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1836 &pending_ret_byte
, 1);
1845 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1847 uint32_t snapshot_id
;
1848 struct lttcomm_lttng_output_id reply
;
1850 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1851 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
),
1853 if (ret
!= LTTNG_OK
) {
1856 reply
.id
= snapshot_id
;
1858 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1864 /* Copy output list into message payload */
1868 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1870 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1871 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
));
1874 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1877 struct lttng_snapshot_output
*outputs
= NULL
;
1879 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1880 if (nb_output
< 0) {
1885 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1886 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1887 nb_output
* sizeof(struct lttng_snapshot_output
));
1897 case LTTNG_SNAPSHOT_RECORD
:
1899 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1900 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_record
.output
),
1901 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1904 case LTTNG_CREATE_SESSION_EXT
:
1906 struct lttng_dynamic_buffer payload
;
1907 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1909 lttng_dynamic_buffer_init(&payload
);
1910 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1911 if (ret
!= LTTNG_OK
) {
1915 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1918 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1919 lttng_session_descriptor_destroy(return_descriptor
);
1920 ret
= LTTNG_ERR_NOMEM
;
1923 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1926 lttng_session_descriptor_destroy(return_descriptor
);
1927 ret
= LTTNG_ERR_NOMEM
;
1930 lttng_dynamic_buffer_reset(&payload
);
1931 lttng_session_descriptor_destroy(return_descriptor
);
1935 case LTTNG_SAVE_SESSION
:
1937 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1941 case LTTNG_SET_SESSION_SHM_PATH
:
1943 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1944 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1947 case LTTNG_REGENERATE_METADATA
:
1949 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1952 case LTTNG_REGENERATE_STATEDUMP
:
1954 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1957 case LTTNG_REGISTER_TRIGGER
:
1959 struct lttng_dynamic_buffer payload
;
1960 struct lttng_trigger
*return_trigger
;
1962 lttng_dynamic_buffer_init(&payload
);
1963 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
1964 notification_thread_handle
, &return_trigger
);
1965 if (ret
!= LTTNG_OK
) {
1969 ret
= lttng_trigger_serialize(return_trigger
, &payload
, NULL
);
1971 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
1972 ret
= LTTNG_ERR_NOMEM
;
1973 lttng_trigger_destroy(return_trigger
);
1976 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1979 lttng_trigger_destroy(return_trigger
);
1980 ret
= LTTNG_ERR_NOMEM
;
1983 lttng_trigger_destroy(return_trigger
);
1984 lttng_dynamic_buffer_reset(&payload
);
1988 case LTTNG_UNREGISTER_TRIGGER
:
1990 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
1991 notification_thread_handle
);
1994 case LTTNG_ROTATE_SESSION
:
1996 struct lttng_rotate_session_return rotate_return
;
1998 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2000 memset(&rotate_return
, 0, sizeof(rotate_return
));
2001 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2002 DBG("Kernel tracer version is not compatible with the rotation feature");
2003 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2007 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2009 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2015 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2016 sizeof(rotate_return
));
2025 case LTTNG_ROTATION_GET_INFO
:
2027 struct lttng_rotation_get_info_return get_info_return
;
2029 memset(&get_info_return
, 0, sizeof(get_info_return
));
2030 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2031 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
2037 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2038 sizeof(get_info_return
));
2047 case LTTNG_ROTATION_SET_SCHEDULE
:
2050 enum lttng_rotation_schedule_type schedule_type
;
2053 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2054 DBG("Kernel tracer version does not support session rotations");
2055 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2059 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
2060 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
2061 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
2063 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
2067 notification_thread_handle
);
2068 if (ret
!= LTTNG_OK
) {
2074 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2076 struct lttng_session_list_schedules_return schedules
= {
2077 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2078 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2079 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2080 .size
.value
= cmd_ctx
->session
->rotate_size
,
2083 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2093 case LTTNG_CLEAR_SESSION
:
2095 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2098 case LTTNG_LIST_TRIGGERS
:
2100 struct lttng_dynamic_buffer payload
;
2101 struct lttng_triggers
*return_triggers
;
2103 lttng_dynamic_buffer_init(&payload
);
2104 ret
= cmd_list_triggers(cmd_ctx
, *sock
,
2105 notification_thread_handle
, &return_triggers
);
2106 if (ret
!= LTTNG_OK
) {
2110 ret
= lttng_triggers_serialize(return_triggers
, &payload
);
2112 ERR("Failed to serialize triggers in reply to \"list triggers\" command");
2113 ret
= LTTNG_ERR_NOMEM
;
2114 lttng_triggers_destroy(return_triggers
);
2117 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
2120 ret
= LTTNG_ERR_NOMEM
;
2121 lttng_triggers_destroy(return_triggers
);
2124 lttng_dynamic_buffer_reset(&payload
);
2125 lttng_triggers_destroy(return_triggers
);
2130 ret
= LTTNG_ERR_UND
;
2135 if (cmd_ctx
->llm
== NULL
) {
2136 DBG("Missing llm structure. Allocating one.");
2137 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2141 /* Set return code */
2142 cmd_ctx
->llm
->ret_code
= ret
;
2144 if (cmd_ctx
->session
) {
2145 session_unlock(cmd_ctx
->session
);
2146 session_put(cmd_ctx
->session
);
2147 cmd_ctx
->session
= NULL
;
2149 if (need_tracing_session
) {
2150 session_unlock_list();
2153 assert(!rcu_read_ongoing());
2157 static int create_client_sock(void)
2159 int ret
, client_sock
;
2160 const mode_t old_umask
= umask(0);
2162 /* Create client tool unix socket */
2163 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2164 if (client_sock
< 0) {
2165 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2170 /* Set the cloexec flag */
2171 ret
= utils_set_fd_cloexec(client_sock
);
2173 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2174 "Continuing but note that the consumer daemon will have a "
2175 "reference to this socket on exec()", client_sock
);
2178 /* File permission MUST be 660 */
2179 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2181 ERR("Set file permissions failed: %s",
2182 config
.client_unix_sock_path
.value
);
2184 (void) lttcomm_close_unix_sock(client_sock
);
2188 DBG("Created client socket (fd = %i)", client_sock
);
2195 static void cleanup_client_thread(void *data
)
2197 struct lttng_pipe
*quit_pipe
= data
;
2199 lttng_pipe_destroy(quit_pipe
);
2202 static void thread_init_cleanup(void *data
)
2204 set_thread_status(false);
2208 * This thread manage all clients request using the unix client socket for
2211 static void *thread_manage_clients(void *data
)
2213 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2215 uint32_t revents
, nb_fd
;
2216 struct command_ctx
*cmd_ctx
= NULL
;
2217 struct lttng_poll_event events
;
2218 const int client_sock
= thread_state
.client_sock
;
2219 struct lttng_pipe
*quit_pipe
= data
;
2220 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2222 DBG("[thread] Manage client started");
2224 is_root
= (getuid() == 0);
2226 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2228 rcu_register_thread();
2230 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2232 health_code_update();
2234 ret
= lttcomm_listen_unix_sock(client_sock
);
2240 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2241 * more will be added to this poll set.
2243 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2245 goto error_create_poll
;
2248 /* Add the application registration socket */
2249 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2254 /* Add thread quit pipe */
2255 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2260 /* Set state as running. */
2261 set_thread_status(true);
2262 pthread_cleanup_pop(0);
2264 /* This testpoint is after we signal readiness to the parent. */
2265 if (testpoint(sessiond_thread_manage_clients
)) {
2269 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2273 health_code_update();
2276 const struct cmd_completion_handler
*cmd_completion_handler
;
2278 DBG("Accepting client command ...");
2280 /* Inifinite blocking call, waiting for transmission */
2282 health_poll_entry();
2283 ret
= lttng_poll_wait(&events
, -1);
2287 * Restart interrupted system call.
2289 if (errno
== EINTR
) {
2297 for (i
= 0; i
< nb_fd
; i
++) {
2298 revents
= LTTNG_POLL_GETEV(&events
, i
);
2299 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2301 health_code_update();
2303 if (pollfd
== thread_quit_pipe_fd
) {
2307 /* Event on the registration socket */
2308 if (revents
& LPOLLIN
) {
2310 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2311 ERR("Client socket poll error");
2314 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2320 DBG("Wait for client response");
2322 health_code_update();
2324 sock
= lttcomm_accept_unix_sock(client_sock
);
2330 * Set the CLOEXEC flag. Return code is useless because either way, the
2333 (void) utils_set_fd_cloexec(sock
);
2335 /* Set socket option for credentials retrieval */
2336 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2341 /* Allocate context command to process the client request */
2342 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2343 if (cmd_ctx
== NULL
) {
2344 PERROR("zmalloc cmd_ctx");
2348 /* Allocate data buffer for reception */
2349 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2350 if (cmd_ctx
->lsm
== NULL
) {
2351 PERROR("zmalloc cmd_ctx->lsm");
2355 cmd_ctx
->llm
= NULL
;
2356 cmd_ctx
->session
= NULL
;
2358 health_code_update();
2361 * Data is received from the lttng client. The struct
2362 * lttcomm_session_msg (lsm) contains the command and data request of
2365 DBG("Receiving data from client ...");
2366 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2367 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2369 DBG("Nothing recv() from client... continuing");
2375 clean_command_ctx(&cmd_ctx
);
2379 health_code_update();
2381 // TODO: Validate cmd_ctx including sanity check for
2382 // security purpose.
2384 rcu_thread_online();
2386 * This function dispatch the work to the kernel or userspace tracer
2387 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2388 * informations for the client. The command context struct contains
2389 * everything this function may needs.
2391 ret
= process_client_msg(cmd_ctx
, &sock
, &sock_error
);
2392 rcu_thread_offline();
2402 * TODO: Inform client somehow of the fatal error. At
2403 * this point, ret < 0 means that a zmalloc failed
2404 * (ENOMEM). Error detected but still accept
2405 * command, unless a socket error has been
2408 clean_command_ctx(&cmd_ctx
);
2412 cmd_completion_handler
= cmd_pop_completion_handler();
2413 if (cmd_completion_handler
) {
2414 enum lttng_error_code completion_code
;
2416 completion_code
= cmd_completion_handler
->run(
2417 cmd_completion_handler
->data
);
2418 if (completion_code
!= LTTNG_OK
) {
2419 clean_command_ctx(&cmd_ctx
);
2424 health_code_update();
2427 DBG("Sending response (size: %d, retcode: %s (%d))",
2428 cmd_ctx
->lttng_msg_size
,
2429 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2430 cmd_ctx
->llm
->ret_code
);
2431 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2432 cmd_ctx
->lttng_msg_size
);
2434 ERR("Failed to send data back to client");
2437 /* End of transmission */
2445 clean_command_ctx(&cmd_ctx
);
2447 health_code_update();
2459 lttng_poll_clean(&events
);
2460 clean_command_ctx(&cmd_ctx
);
2464 unlink(config
.client_unix_sock_path
.value
);
2465 ret
= close(client_sock
);
2472 ERR("Health error occurred in %s", __func__
);
2475 health_unregister(health_sessiond
);
2477 DBG("Client thread dying");
2479 rcu_unregister_thread();
2484 bool shutdown_client_thread(void *thread_data
)
2486 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2487 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2489 return notify_thread_pipe(write_fd
) == 1;
2492 struct lttng_thread
*launch_client_thread(void)
2494 bool thread_running
;
2495 struct lttng_pipe
*client_quit_pipe
;
2496 struct lttng_thread
*thread
= NULL
;
2497 int client_sock_fd
= -1;
2499 sem_init(&thread_state
.ready
, 0, 0);
2500 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2501 if (!client_quit_pipe
) {
2505 client_sock_fd
= create_client_sock();
2506 if (client_sock_fd
< 0) {
2510 thread_state
.client_sock
= client_sock_fd
;
2511 thread
= lttng_thread_create("Client management",
2512 thread_manage_clients
,
2513 shutdown_client_thread
,
2514 cleanup_client_thread
,
2519 /* The client thread now owns the client sock fd and the quit pipe. */
2520 client_sock_fd
= -1;
2521 client_quit_pipe
= NULL
;
2524 * This thread is part of the threads that need to be fully
2525 * initialized before the session daemon is marked as "ready".
2527 thread_running
= wait_thread_status();
2528 if (!thread_running
) {
2533 if (client_sock_fd
>= 0) {
2534 if (close(client_sock_fd
)) {
2535 PERROR("Failed to close client socket");
2538 lttng_thread_put(thread
);
2539 cleanup_client_thread(client_quit_pipe
);