2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <common/compat/getenv.h>
25 #include <common/unix.h>
26 #include <common/utils.h>
27 #include <lttng/userspace-probe-internal.h>
28 #include <lttng/event-internal.h>
31 #include "lttng-sessiond.h"
35 #include "health-sessiond.h"
36 #include "testpoint.h"
38 #include "manage-consumer.h"
42 static struct thread_state
{
47 .cond
= PTHREAD_COND_INITIALIZER
,
48 .lock
= PTHREAD_MUTEX_INITIALIZER
,
52 void set_thread_state_running(void)
54 pthread_mutex_lock(&thread_state
.lock
);
55 thread_state
.is_running
= true;
56 pthread_cond_broadcast(&thread_state
.cond
);
57 pthread_mutex_unlock(&thread_state
.lock
);
60 static void wait_thread_state_running(void)
62 pthread_mutex_lock(&thread_state
.lock
);
63 while (!thread_state
.is_running
) {
64 pthread_cond_wait(&thread_state
.cond
,
67 pthread_mutex_unlock(&thread_state
.lock
);
71 * Setup the outgoing data buffer for the response (llm) by allocating the
72 * right amount of memory and copying the original information from the lsm
75 * Return 0 on success, negative value on error.
77 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
78 const void *payload_buf
, size_t payload_len
,
79 const void *cmd_header_buf
, size_t cmd_header_len
)
82 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
83 const size_t cmd_header_offset
= header_len
;
84 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
85 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
87 cmd_ctx
->llm
= zmalloc(total_msg_size
);
89 if (cmd_ctx
->llm
== NULL
) {
95 /* Copy common data */
96 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
97 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
98 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
99 cmd_ctx
->llm
->data_size
= payload_len
;
100 cmd_ctx
->lttng_msg_size
= total_msg_size
;
102 /* Copy command header */
103 if (cmd_header_len
) {
104 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
110 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
119 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
120 * exec or it will fail.
122 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
124 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
128 * Fork and exec a consumer daemon (consumerd).
130 * Return pid if successful else -1.
132 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
136 const char *consumer_to_use
;
137 const char *verbosity
;
140 DBG("Spawning consumerd");
147 if (config
.verbose_consumer
) {
148 verbosity
= "--verbose";
149 } else if (lttng_opt_quiet
) {
150 verbosity
= "--quiet";
155 switch (consumer_data
->type
) {
156 case LTTNG_CONSUMER_KERNEL
:
158 * Find out which consumerd to execute. We will first try the
159 * 64-bit path, then the sessiond's installation directory, and
160 * fallback on the 32-bit one,
162 DBG3("Looking for a kernel consumer at these locations:");
163 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
164 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
165 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
166 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
167 DBG3("Found location #1");
168 consumer_to_use
= config
.consumerd64_bin_path
.value
;
169 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
170 DBG3("Found location #2");
171 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
172 } else if (config
.consumerd32_bin_path
.value
&&
173 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
174 DBG3("Found location #3");
175 consumer_to_use
= config
.consumerd32_bin_path
.value
;
177 DBG("Could not find any valid consumerd executable");
181 DBG("Using kernel consumer at: %s", consumer_to_use
);
182 (void) execl(consumer_to_use
,
183 "lttng-consumerd", verbosity
, "-k",
184 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
185 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
186 "--group", config
.tracing_group_name
.value
,
189 case LTTNG_CONSUMER64_UST
:
191 if (config
.consumerd64_lib_dir
.value
) {
196 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
200 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
201 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
206 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
207 if (tmp
[0] != '\0') {
211 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
218 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
219 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
220 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
221 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
222 "--group", config
.tracing_group_name
.value
,
226 case LTTNG_CONSUMER32_UST
:
228 if (config
.consumerd32_lib_dir
.value
) {
233 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
237 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
238 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
243 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
244 if (tmp
[0] != '\0') {
248 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
255 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
256 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
257 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
258 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
259 "--group", config
.tracing_group_name
.value
,
264 ERR("unknown consumer type");
268 PERROR("Consumer execl()");
270 /* Reaching this point, we got a failure on our execl(). */
272 } else if (pid
> 0) {
275 PERROR("start consumer fork");
283 * Spawn the consumerd daemon and session daemon thread.
285 static int start_consumerd(struct consumer_data
*consumer_data
)
290 * Set the listen() state on the socket since there is a possible race
291 * between the exec() of the consumer daemon and this call if place in the
292 * consumer thread. See bug #366 for more details.
294 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
299 pthread_mutex_lock(&consumer_data
->pid_mutex
);
300 if (consumer_data
->pid
!= 0) {
301 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
305 ret
= spawn_consumerd(consumer_data
);
307 ERR("Spawning consumerd failed");
308 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
312 /* Setting up the consumer_data pid */
313 consumer_data
->pid
= ret
;
314 DBG2("Consumer pid %d", consumer_data
->pid
);
315 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
317 DBG2("Spawning consumer control thread");
318 ret
= spawn_consumer_thread(consumer_data
);
320 ERR("Fatal error spawning consumer control thread");
328 /* Cleanup already created sockets on error. */
329 if (consumer_data
->err_sock
>= 0) {
332 err
= close(consumer_data
->err_sock
);
334 PERROR("close consumer data error socket");
341 * Copy consumer output from the tracing session to the domain session. The
342 * function also applies the right modification on a per domain basis for the
343 * trace files destination directory.
345 * Should *NOT* be called with RCU read-side lock held.
347 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
350 const char *dir_name
;
351 struct consumer_output
*consumer
;
354 assert(session
->consumer
);
357 case LTTNG_DOMAIN_KERNEL
:
358 DBG3("Copying tracing session consumer output in kernel session");
360 * XXX: We should audit the session creation and what this function
361 * does "extra" in order to avoid a destroy since this function is used
362 * in the domain session creation (kernel and ust) only. Same for UST
365 if (session
->kernel_session
->consumer
) {
366 consumer_output_put(session
->kernel_session
->consumer
);
368 session
->kernel_session
->consumer
=
369 consumer_copy_output(session
->consumer
);
370 /* Ease our life a bit for the next part */
371 consumer
= session
->kernel_session
->consumer
;
372 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
374 case LTTNG_DOMAIN_JUL
:
375 case LTTNG_DOMAIN_LOG4J
:
376 case LTTNG_DOMAIN_PYTHON
:
377 case LTTNG_DOMAIN_UST
:
378 DBG3("Copying tracing session consumer output in UST session");
379 if (session
->ust_session
->consumer
) {
380 consumer_output_put(session
->ust_session
->consumer
);
382 session
->ust_session
->consumer
=
383 consumer_copy_output(session
->consumer
);
384 /* Ease our life a bit for the next part */
385 consumer
= session
->ust_session
->consumer
;
386 dir_name
= DEFAULT_UST_TRACE_DIR
;
389 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
393 /* Append correct directory to subdir */
394 strncat(consumer
->subdir
, dir_name
,
395 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
396 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
405 * Create an UST session and add it to the session ust list.
407 * Should *NOT* be called with RCU read-side lock held.
409 static int create_ust_session(struct ltt_session
*session
,
410 struct lttng_domain
*domain
)
413 struct ltt_ust_session
*lus
= NULL
;
417 assert(session
->consumer
);
419 switch (domain
->type
) {
420 case LTTNG_DOMAIN_JUL
:
421 case LTTNG_DOMAIN_LOG4J
:
422 case LTTNG_DOMAIN_PYTHON
:
423 case LTTNG_DOMAIN_UST
:
426 ERR("Unknown UST domain on create session %d", domain
->type
);
427 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
431 DBG("Creating UST session");
433 lus
= trace_ust_create_session(session
->id
);
435 ret
= LTTNG_ERR_UST_SESS_FAIL
;
439 lus
->uid
= session
->uid
;
440 lus
->gid
= session
->gid
;
441 lus
->output_traces
= session
->output_traces
;
442 lus
->snapshot_mode
= session
->snapshot_mode
;
443 lus
->live_timer_interval
= session
->live_timer
;
444 session
->ust_session
= lus
;
445 if (session
->shm_path
[0]) {
446 strncpy(lus
->root_shm_path
, session
->shm_path
,
447 sizeof(lus
->root_shm_path
));
448 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
449 strncpy(lus
->shm_path
, session
->shm_path
,
450 sizeof(lus
->shm_path
));
451 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
452 strncat(lus
->shm_path
, "/ust",
453 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
455 /* Copy session output to the newly created UST session */
456 ret
= copy_session_consumer(domain
->type
, session
);
457 if (ret
!= LTTNG_OK
) {
465 session
->ust_session
= NULL
;
470 * Create a kernel tracer session then create the default channel.
472 static int create_kernel_session(struct ltt_session
*session
)
476 DBG("Creating kernel session");
478 ret
= kernel_create_session(session
, kernel_tracer_fd
);
480 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
484 /* Code flow safety */
485 assert(session
->kernel_session
);
487 /* Copy session output to the newly created Kernel session */
488 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
489 if (ret
!= LTTNG_OK
) {
493 session
->kernel_session
->uid
= session
->uid
;
494 session
->kernel_session
->gid
= session
->gid
;
495 session
->kernel_session
->output_traces
= session
->output_traces
;
496 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
501 trace_kernel_destroy_session(session
->kernel_session
);
502 session
->kernel_session
= NULL
;
507 * Count number of session permitted by uid/gid.
509 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
512 struct ltt_session
*session
;
513 const struct ltt_session_list
*session_list
= session_get_list();
515 DBG("Counting number of available session for UID %d GID %d",
517 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
518 if (!session_get(session
)) {
521 session_lock(session
);
522 /* Only count the sessions the user can control. */
523 if (session_access_ok(session
, uid
, gid
) &&
524 !session
->destroyed
) {
527 session_unlock(session
);
528 session_put(session
);
533 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
534 int *sock_error
, struct lttng_event
*event
)
537 struct lttng_userspace_probe_location
*probe_location
;
538 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
539 struct lttng_dynamic_buffer probe_location_buffer
;
540 struct lttng_buffer_view buffer_view
;
543 * Create a buffer to store the serialized version of the probe
546 lttng_dynamic_buffer_init(&probe_location_buffer
);
547 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
548 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
550 ret
= LTTNG_ERR_NOMEM
;
555 * Receive the probe location.
557 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
558 probe_location_buffer
.size
);
560 DBG("Nothing recv() from client var len data... continuing");
562 lttng_dynamic_buffer_reset(&probe_location_buffer
);
563 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
567 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
568 &probe_location_buffer
, 0, probe_location_buffer
.size
);
571 * Extract the probe location from the serialized version.
573 ret
= lttng_userspace_probe_location_create_from_buffer(
574 &buffer_view
, &probe_location
);
576 WARN("Failed to create a userspace probe location from the received buffer");
577 lttng_dynamic_buffer_reset( &probe_location_buffer
);
578 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
583 * Receive the file descriptor to the target binary from the client.
585 DBG("Receiving userspace probe target FD from client ...");
586 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
588 DBG("Nothing recv() from client userspace probe fd... continuing");
590 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
595 * Set the file descriptor received from the client through the unix
596 * socket in the probe location.
598 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
600 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
605 * From the kernel tracer's perspective, all userspace probe event types
606 * are all the same: a file and an offset.
608 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
609 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
610 ret
= lttng_userspace_probe_location_function_set_binary_fd(
613 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
614 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
618 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
623 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
627 /* Attach the probe location to the event. */
628 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
630 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
634 lttng_dynamic_buffer_reset(&probe_location_buffer
);
640 * Version of setup_lttng_msg() without command header.
642 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
643 void *payload_buf
, size_t payload_len
)
645 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
649 * Free memory of a command context structure.
651 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
653 DBG("Clean command context structure");
655 if ((*cmd_ctx
)->llm
) {
656 free((*cmd_ctx
)->llm
);
658 if ((*cmd_ctx
)->lsm
) {
659 free((*cmd_ctx
)->lsm
);
667 * Check if the current kernel tracer supports the session rotation feature.
668 * Return 1 if it does, 0 otherwise.
670 static int check_rotate_compatible(void)
674 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
675 DBG("Kernel tracer version is not compatible with the rotation feature");
683 * Send data on a unix socket using the liblttsessiondcomm API.
685 * Return lttcomm error code.
687 static int send_unix_sock(int sock
, void *buf
, size_t len
)
689 /* Check valid length */
694 return lttcomm_send_unix_sock(sock
, buf
, len
);
698 * Process the command requested by the lttng client within the command
699 * context structure. This function make sure that the return structure (llm)
700 * is set and ready for transmission before returning.
702 * Return any error encountered or 0 for success.
704 * "sock" is only used for special-case var. len data.
706 * Should *NOT* be called with RCU read-side lock held.
708 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
712 int need_tracing_session
= 1;
715 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
717 assert(!rcu_read_ongoing());
721 switch (cmd_ctx
->lsm
->cmd_type
) {
722 case LTTNG_CREATE_SESSION
:
723 case LTTNG_CREATE_SESSION_SNAPSHOT
:
724 case LTTNG_CREATE_SESSION_LIVE
:
725 case LTTNG_DESTROY_SESSION
:
726 case LTTNG_LIST_SESSIONS
:
727 case LTTNG_LIST_DOMAINS
:
728 case LTTNG_START_TRACE
:
729 case LTTNG_STOP_TRACE
:
730 case LTTNG_DATA_PENDING
:
731 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
732 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
733 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
734 case LTTNG_SNAPSHOT_RECORD
:
735 case LTTNG_SAVE_SESSION
:
736 case LTTNG_SET_SESSION_SHM_PATH
:
737 case LTTNG_REGENERATE_METADATA
:
738 case LTTNG_REGENERATE_STATEDUMP
:
739 case LTTNG_REGISTER_TRIGGER
:
740 case LTTNG_UNREGISTER_TRIGGER
:
741 case LTTNG_ROTATE_SESSION
:
742 case LTTNG_ROTATION_GET_INFO
:
743 case LTTNG_ROTATION_SET_SCHEDULE
:
744 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
751 if (config
.no_kernel
&& need_domain
752 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
754 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
756 ret
= LTTNG_ERR_KERN_NA
;
761 /* Deny register consumer if we already have a spawned consumer. */
762 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
763 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
764 if (kconsumer_data
.pid
> 0) {
765 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
766 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
769 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
773 * Check for command that don't needs to allocate a returned payload. We do
774 * this here so we don't have to make the call for no payload at each
777 switch(cmd_ctx
->lsm
->cmd_type
) {
778 case LTTNG_LIST_SESSIONS
:
779 case LTTNG_LIST_TRACEPOINTS
:
780 case LTTNG_LIST_TRACEPOINT_FIELDS
:
781 case LTTNG_LIST_DOMAINS
:
782 case LTTNG_LIST_CHANNELS
:
783 case LTTNG_LIST_EVENTS
:
784 case LTTNG_LIST_SYSCALLS
:
785 case LTTNG_LIST_TRACKER_PIDS
:
786 case LTTNG_DATA_PENDING
:
787 case LTTNG_ROTATE_SESSION
:
788 case LTTNG_ROTATION_GET_INFO
:
789 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
792 /* Setup lttng message with no payload */
793 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
795 /* This label does not try to unlock the session */
796 goto init_setup_error
;
800 /* Commands that DO NOT need a session. */
801 switch (cmd_ctx
->lsm
->cmd_type
) {
802 case LTTNG_CREATE_SESSION
:
803 case LTTNG_CREATE_SESSION_SNAPSHOT
:
804 case LTTNG_CREATE_SESSION_LIVE
:
805 case LTTNG_LIST_SESSIONS
:
806 case LTTNG_LIST_TRACEPOINTS
:
807 case LTTNG_LIST_SYSCALLS
:
808 case LTTNG_LIST_TRACEPOINT_FIELDS
:
809 case LTTNG_SAVE_SESSION
:
810 case LTTNG_REGISTER_TRIGGER
:
811 case LTTNG_UNREGISTER_TRIGGER
:
812 need_tracing_session
= 0;
815 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
817 * We keep the session list lock across _all_ commands
818 * for now, because the per-session lock does not
819 * handle teardown properly.
822 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
823 if (cmd_ctx
->session
== NULL
) {
824 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
827 /* Acquire lock for the session */
828 session_lock(cmd_ctx
->session
);
834 * Commands that need a valid session but should NOT create one if none
835 * exists. Instead of creating one and destroying it when the command is
836 * handled, process that right before so we save some round trip in useless
839 switch (cmd_ctx
->lsm
->cmd_type
) {
840 case LTTNG_DISABLE_CHANNEL
:
841 case LTTNG_DISABLE_EVENT
:
842 switch (cmd_ctx
->lsm
->domain
.type
) {
843 case LTTNG_DOMAIN_KERNEL
:
844 if (!cmd_ctx
->session
->kernel_session
) {
845 ret
= LTTNG_ERR_NO_CHANNEL
;
849 case LTTNG_DOMAIN_JUL
:
850 case LTTNG_DOMAIN_LOG4J
:
851 case LTTNG_DOMAIN_PYTHON
:
852 case LTTNG_DOMAIN_UST
:
853 if (!cmd_ctx
->session
->ust_session
) {
854 ret
= LTTNG_ERR_NO_CHANNEL
;
859 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
871 * Check domain type for specific "pre-action".
873 switch (cmd_ctx
->lsm
->domain
.type
) {
874 case LTTNG_DOMAIN_KERNEL
:
876 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
880 /* Consumer is in an ERROR state. Report back to client */
881 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
882 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
886 /* Need a session for kernel command */
887 if (need_tracing_session
) {
888 if (cmd_ctx
->session
->kernel_session
== NULL
) {
889 ret
= create_kernel_session(cmd_ctx
->session
);
891 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
896 /* Start the kernel consumer daemon */
897 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
898 if (kconsumer_data
.pid
== 0 &&
899 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
900 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
901 ret
= start_consumerd(&kconsumer_data
);
903 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
906 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
908 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
912 * The consumer was just spawned so we need to add the socket to
913 * the consumer output of the session if exist.
915 ret
= consumer_create_socket(&kconsumer_data
,
916 cmd_ctx
->session
->kernel_session
->consumer
);
923 case LTTNG_DOMAIN_JUL
:
924 case LTTNG_DOMAIN_LOG4J
:
925 case LTTNG_DOMAIN_PYTHON
:
926 case LTTNG_DOMAIN_UST
:
928 if (!ust_app_supported()) {
929 ret
= LTTNG_ERR_NO_UST
;
932 /* Consumer is in an ERROR state. Report back to client */
933 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
934 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
938 if (need_tracing_session
) {
939 /* Create UST session if none exist. */
940 if (cmd_ctx
->session
->ust_session
== NULL
) {
941 ret
= create_ust_session(cmd_ctx
->session
,
942 &cmd_ctx
->lsm
->domain
);
943 if (ret
!= LTTNG_OK
) {
948 /* Start the UST consumer daemons */
950 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
951 if (config
.consumerd64_bin_path
.value
&&
952 ustconsumer64_data
.pid
== 0 &&
953 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
954 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
955 ret
= start_consumerd(&ustconsumer64_data
);
957 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
958 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
962 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
963 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
965 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
969 * Setup socket for consumer 64 bit. No need for atomic access
970 * since it was set above and can ONLY be set in this thread.
972 ret
= consumer_create_socket(&ustconsumer64_data
,
973 cmd_ctx
->session
->ust_session
->consumer
);
979 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
980 if (config
.consumerd32_bin_path
.value
&&
981 ustconsumer32_data
.pid
== 0 &&
982 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
983 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
984 ret
= start_consumerd(&ustconsumer32_data
);
986 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
987 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
991 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
992 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
994 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
998 * Setup socket for consumer 32 bit. No need for atomic access
999 * since it was set above and can ONLY be set in this thread.
1001 ret
= consumer_create_socket(&ustconsumer32_data
,
1002 cmd_ctx
->session
->ust_session
->consumer
);
1014 /* Validate consumer daemon state when start/stop trace command */
1015 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1016 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1017 switch (cmd_ctx
->lsm
->domain
.type
) {
1018 case LTTNG_DOMAIN_NONE
:
1020 case LTTNG_DOMAIN_JUL
:
1021 case LTTNG_DOMAIN_LOG4J
:
1022 case LTTNG_DOMAIN_PYTHON
:
1023 case LTTNG_DOMAIN_UST
:
1024 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1025 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1029 case LTTNG_DOMAIN_KERNEL
:
1030 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1031 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1036 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1042 * Check that the UID or GID match that of the tracing session.
1043 * The root user can interact with all sessions.
1045 if (need_tracing_session
) {
1046 if (!session_access_ok(cmd_ctx
->session
,
1047 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1048 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1049 cmd_ctx
->session
->destroyed
) {
1050 ret
= LTTNG_ERR_EPERM
;
1056 * Send relayd information to consumer as soon as we have a domain and a
1059 if (cmd_ctx
->session
&& need_domain
) {
1061 * Setup relayd if not done yet. If the relayd information was already
1062 * sent to the consumer, this call will gracefully return.
1064 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1065 if (ret
!= LTTNG_OK
) {
1070 /* Process by command type */
1071 switch (cmd_ctx
->lsm
->cmd_type
) {
1072 case LTTNG_ADD_CONTEXT
:
1075 * An LTTNG_ADD_CONTEXT command might have a supplementary
1076 * payload if the context being added is an application context.
1078 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1079 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1080 char *provider_name
= NULL
, *context_name
= NULL
;
1081 size_t provider_name_len
=
1082 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1083 size_t context_name_len
=
1084 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1086 if (provider_name_len
== 0 || context_name_len
== 0) {
1088 * Application provider and context names MUST
1091 ret
= -LTTNG_ERR_INVALID
;
1095 provider_name
= zmalloc(provider_name_len
+ 1);
1096 if (!provider_name
) {
1097 ret
= -LTTNG_ERR_NOMEM
;
1100 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1103 context_name
= zmalloc(context_name_len
+ 1);
1104 if (!context_name
) {
1105 ret
= -LTTNG_ERR_NOMEM
;
1106 goto error_add_context
;
1108 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1111 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
1114 goto error_add_context
;
1117 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
1120 goto error_add_context
;
1125 * cmd_add_context assumes ownership of the provider and context
1128 ret
= cmd_add_context(cmd_ctx
->session
,
1129 cmd_ctx
->lsm
->domain
.type
,
1130 cmd_ctx
->lsm
->u
.context
.channel_name
,
1131 &cmd_ctx
->lsm
->u
.context
.ctx
,
1132 kernel_poll_pipe
[1]);
1134 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1135 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1137 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1138 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1144 case LTTNG_DISABLE_CHANNEL
:
1146 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1147 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1150 case LTTNG_DISABLE_EVENT
:
1154 * FIXME: handle filter; for now we just receive the filter's
1155 * bytecode along with the filter expression which are sent by
1156 * liblttng-ctl and discard them.
1158 * This fixes an issue where the client may block while sending
1159 * the filter payload and encounter an error because the session
1160 * daemon closes the socket without ever handling this data.
1162 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1163 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1166 char data
[LTTNG_FILTER_MAX_LEN
];
1168 DBG("Discarding disable event command payload of size %zu", count
);
1170 ret
= lttcomm_recv_unix_sock(sock
, data
,
1171 count
> sizeof(data
) ? sizeof(data
) : count
);
1176 count
-= (size_t) ret
;
1179 /* FIXME: passing packed structure to non-packed pointer */
1180 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1181 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1182 &cmd_ctx
->lsm
->u
.disable
.event
);
1185 case LTTNG_ENABLE_CHANNEL
:
1187 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1188 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1189 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
1190 &cmd_ctx
->lsm
->u
.channel
.chan
,
1191 kernel_poll_pipe
[1]);
1194 case LTTNG_TRACK_PID
:
1196 ret
= cmd_track_pid(cmd_ctx
->session
,
1197 cmd_ctx
->lsm
->domain
.type
,
1198 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1201 case LTTNG_UNTRACK_PID
:
1203 ret
= cmd_untrack_pid(cmd_ctx
->session
,
1204 cmd_ctx
->lsm
->domain
.type
,
1205 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1208 case LTTNG_ENABLE_EVENT
:
1210 struct lttng_event
*ev
= NULL
;
1211 struct lttng_event_exclusion
*exclusion
= NULL
;
1212 struct lttng_filter_bytecode
*bytecode
= NULL
;
1213 char *filter_expression
= NULL
;
1215 /* Handle exclusion events and receive it from the client. */
1216 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1217 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1219 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1220 (count
* LTTNG_SYMBOL_NAME_LEN
));
1222 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1226 DBG("Receiving var len exclusion event list from client ...");
1227 exclusion
->count
= count
;
1228 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
1229 count
* LTTNG_SYMBOL_NAME_LEN
);
1231 DBG("Nothing recv() from client var len data... continuing");
1234 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1239 /* Get filter expression from client. */
1240 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1241 size_t expression_len
=
1242 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1244 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1245 ret
= LTTNG_ERR_FILTER_INVAL
;
1250 filter_expression
= zmalloc(expression_len
);
1251 if (!filter_expression
) {
1253 ret
= LTTNG_ERR_FILTER_NOMEM
;
1257 /* Receive var. len. data */
1258 DBG("Receiving var len filter's expression from client ...");
1259 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
1262 DBG("Nothing recv() from client var len data... continuing");
1264 free(filter_expression
);
1266 ret
= LTTNG_ERR_FILTER_INVAL
;
1271 /* Handle filter and get bytecode from client. */
1272 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1273 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1275 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1276 ret
= LTTNG_ERR_FILTER_INVAL
;
1277 free(filter_expression
);
1282 bytecode
= zmalloc(bytecode_len
);
1284 free(filter_expression
);
1286 ret
= LTTNG_ERR_FILTER_NOMEM
;
1290 /* Receive var. len. data */
1291 DBG("Receiving var len filter's bytecode from client ...");
1292 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
1294 DBG("Nothing recv() from client var len data... continuing");
1296 free(filter_expression
);
1299 ret
= LTTNG_ERR_FILTER_INVAL
;
1303 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1304 free(filter_expression
);
1307 ret
= LTTNG_ERR_FILTER_INVAL
;
1312 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
1314 DBG("Failed to copy event: %s",
1315 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1316 free(filter_expression
);
1319 ret
= LTTNG_ERR_NOMEM
;
1324 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1325 /* Expect a userspace probe description. */
1326 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
1328 free(filter_expression
);
1331 lttng_event_destroy(ev
);
1336 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
1337 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1339 filter_expression
, bytecode
, exclusion
,
1340 kernel_poll_pipe
[1]);
1341 lttng_event_destroy(ev
);
1344 case LTTNG_LIST_TRACEPOINTS
:
1346 struct lttng_event
*events
;
1349 session_lock_list();
1350 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1351 session_unlock_list();
1352 if (nb_events
< 0) {
1353 /* Return value is a negative lttng_error_code. */
1359 * Setup lttng message with payload size set to the event list size in
1360 * bytes and then copy list into the llm payload.
1362 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1363 sizeof(struct lttng_event
) * nb_events
);
1373 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1375 struct lttng_event_field
*fields
;
1378 session_lock_list();
1379 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1381 session_unlock_list();
1382 if (nb_fields
< 0) {
1383 /* Return value is a negative lttng_error_code. */
1389 * Setup lttng message with payload size set to the event list size in
1390 * bytes and then copy list into the llm payload.
1392 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1393 sizeof(struct lttng_event_field
) * nb_fields
);
1403 case LTTNG_LIST_SYSCALLS
:
1405 struct lttng_event
*events
;
1408 nb_events
= cmd_list_syscalls(&events
);
1409 if (nb_events
< 0) {
1410 /* Return value is a negative lttng_error_code. */
1416 * Setup lttng message with payload size set to the event list size in
1417 * bytes and then copy list into the llm payload.
1419 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1420 sizeof(struct lttng_event
) * nb_events
);
1430 case LTTNG_LIST_TRACKER_PIDS
:
1432 int32_t *pids
= NULL
;
1435 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
1436 cmd_ctx
->lsm
->domain
.type
, &pids
);
1438 /* Return value is a negative lttng_error_code. */
1444 * Setup lttng message with payload size set to the event list size in
1445 * bytes and then copy list into the llm payload.
1447 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
1448 sizeof(int32_t) * nr_pids
);
1458 case LTTNG_SET_CONSUMER_URI
:
1461 struct lttng_uri
*uris
;
1463 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1464 len
= nb_uri
* sizeof(struct lttng_uri
);
1467 ret
= LTTNG_ERR_INVALID
;
1471 uris
= zmalloc(len
);
1473 ret
= LTTNG_ERR_FATAL
;
1477 /* Receive variable len data */
1478 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1479 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1481 DBG("No URIs received from client... continuing");
1483 ret
= LTTNG_ERR_SESSION_FAIL
;
1488 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1490 if (ret
!= LTTNG_OK
) {
1497 case LTTNG_START_TRACE
:
1500 * On the first start, if we have a kernel session and we have
1501 * enabled time or size-based rotations, we have to make sure
1502 * the kernel tracer supports it.
1504 if (!cmd_ctx
->session
->has_been_started
&& \
1505 cmd_ctx
->session
->kernel_session
&& \
1506 (cmd_ctx
->session
->rotate_timer_period
|| \
1507 cmd_ctx
->session
->rotate_size
) && \
1508 !check_rotate_compatible()) {
1509 DBG("Kernel tracer version is not compatible with the rotation feature");
1510 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1513 ret
= cmd_start_trace(cmd_ctx
->session
);
1516 case LTTNG_STOP_TRACE
:
1518 ret
= cmd_stop_trace(cmd_ctx
->session
);
1521 case LTTNG_CREATE_SESSION
:
1524 struct lttng_uri
*uris
= NULL
;
1526 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1527 len
= nb_uri
* sizeof(struct lttng_uri
);
1530 uris
= zmalloc(len
);
1532 ret
= LTTNG_ERR_FATAL
;
1536 /* Receive variable len data */
1537 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1538 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1540 DBG("No URIs received from client... continuing");
1542 ret
= LTTNG_ERR_SESSION_FAIL
;
1547 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1548 DBG("Creating session with ONE network URI is a bad call");
1549 ret
= LTTNG_ERR_SESSION_FAIL
;
1555 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
1556 &cmd_ctx
->creds
, 0);
1562 case LTTNG_DESTROY_SESSION
:
1564 ret
= cmd_destroy_session(cmd_ctx
->session
,
1565 notification_thread_handle
);
1568 case LTTNG_LIST_DOMAINS
:
1571 struct lttng_domain
*domains
= NULL
;
1573 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1575 /* Return value is a negative lttng_error_code. */
1580 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1581 nb_dom
* sizeof(struct lttng_domain
));
1591 case LTTNG_LIST_CHANNELS
:
1593 ssize_t payload_size
;
1594 struct lttng_channel
*channels
= NULL
;
1596 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1597 cmd_ctx
->session
, &channels
);
1598 if (payload_size
< 0) {
1599 /* Return value is a negative lttng_error_code. */
1600 ret
= -payload_size
;
1604 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1615 case LTTNG_LIST_EVENTS
:
1618 struct lttng_event
*events
= NULL
;
1619 struct lttcomm_event_command_header cmd_header
;
1622 memset(&cmd_header
, 0, sizeof(cmd_header
));
1623 /* Extended infos are included at the end of events */
1624 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1625 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1626 &events
, &total_size
);
1629 /* Return value is a negative lttng_error_code. */
1634 cmd_header
.nb_events
= nb_event
;
1635 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1636 &cmd_header
, sizeof(cmd_header
));
1646 case LTTNG_LIST_SESSIONS
:
1648 unsigned int nr_sessions
;
1649 void *sessions_payload
;
1652 session_lock_list();
1653 nr_sessions
= lttng_sessions_count(
1654 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1655 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1656 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
1657 sessions_payload
= zmalloc(payload_len
);
1659 if (!sessions_payload
) {
1660 session_unlock_list();
1665 cmd_list_lttng_sessions(sessions_payload
,
1666 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1667 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1668 session_unlock_list();
1670 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1672 free(sessions_payload
);
1681 case LTTNG_REGISTER_CONSUMER
:
1683 struct consumer_data
*cdata
;
1685 switch (cmd_ctx
->lsm
->domain
.type
) {
1686 case LTTNG_DOMAIN_KERNEL
:
1687 cdata
= &kconsumer_data
;
1690 ret
= LTTNG_ERR_UND
;
1694 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1695 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1698 case LTTNG_DATA_PENDING
:
1701 uint8_t pending_ret_byte
;
1703 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1708 * This function may returns 0 or 1 to indicate whether or not
1709 * there is data pending. In case of error, it should return an
1710 * LTTNG_ERR code. However, some code paths may still return
1711 * a nondescript error code, which we handle by returning an
1714 if (pending_ret
== 0 || pending_ret
== 1) {
1716 * ret will be set to LTTNG_OK at the end of
1719 } else if (pending_ret
< 0) {
1720 ret
= LTTNG_ERR_UNK
;
1727 pending_ret_byte
= (uint8_t) pending_ret
;
1729 /* 1 byte to return whether or not data is pending */
1730 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1731 &pending_ret_byte
, 1);
1740 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1742 struct lttcomm_lttng_output_id reply
;
1744 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1745 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
1746 if (ret
!= LTTNG_OK
) {
1750 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1756 /* Copy output list into message payload */
1760 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1762 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1763 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
1766 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1769 struct lttng_snapshot_output
*outputs
= NULL
;
1771 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1772 if (nb_output
< 0) {
1777 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1778 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1779 nb_output
* sizeof(struct lttng_snapshot_output
));
1789 case LTTNG_SNAPSHOT_RECORD
:
1791 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1792 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
1793 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1796 case LTTNG_CREATE_SESSION_SNAPSHOT
:
1799 struct lttng_uri
*uris
= NULL
;
1801 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1802 len
= nb_uri
* sizeof(struct lttng_uri
);
1805 uris
= zmalloc(len
);
1807 ret
= LTTNG_ERR_FATAL
;
1811 /* Receive variable len data */
1812 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1813 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1815 DBG("No URIs received from client... continuing");
1817 ret
= LTTNG_ERR_SESSION_FAIL
;
1822 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1823 DBG("Creating session with ONE network URI is a bad call");
1824 ret
= LTTNG_ERR_SESSION_FAIL
;
1830 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
1831 nb_uri
, &cmd_ctx
->creds
);
1835 case LTTNG_CREATE_SESSION_LIVE
:
1838 struct lttng_uri
*uris
= NULL
;
1840 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1841 len
= nb_uri
* sizeof(struct lttng_uri
);
1844 uris
= zmalloc(len
);
1846 ret
= LTTNG_ERR_FATAL
;
1850 /* Receive variable len data */
1851 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1852 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1854 DBG("No URIs received from client... continuing");
1856 ret
= LTTNG_ERR_SESSION_FAIL
;
1861 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1862 DBG("Creating session with ONE network URI is a bad call");
1863 ret
= LTTNG_ERR_SESSION_FAIL
;
1869 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
1870 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
1874 case LTTNG_SAVE_SESSION
:
1876 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1880 case LTTNG_SET_SESSION_SHM_PATH
:
1882 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1883 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1886 case LTTNG_REGENERATE_METADATA
:
1888 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1891 case LTTNG_REGENERATE_STATEDUMP
:
1893 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1896 case LTTNG_REGISTER_TRIGGER
:
1898 ret
= cmd_register_trigger(cmd_ctx
, sock
,
1899 notification_thread_handle
);
1902 case LTTNG_UNREGISTER_TRIGGER
:
1904 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
1905 notification_thread_handle
);
1908 case LTTNG_ROTATE_SESSION
:
1910 struct lttng_rotate_session_return rotate_return
;
1912 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1914 memset(&rotate_return
, 0, sizeof(rotate_return
));
1915 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1916 DBG("Kernel tracer version is not compatible with the rotation feature");
1917 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1921 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
1927 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
1928 sizeof(rotate_return
));
1937 case LTTNG_ROTATION_GET_INFO
:
1939 struct lttng_rotation_get_info_return get_info_return
;
1941 memset(&get_info_return
, 0, sizeof(get_info_return
));
1942 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
1943 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
1949 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
1950 sizeof(get_info_return
));
1959 case LTTNG_ROTATION_SET_SCHEDULE
:
1962 enum lttng_rotation_schedule_type schedule_type
;
1965 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1966 DBG("Kernel tracer version does not support session rotations");
1967 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1971 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
1972 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
1973 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
1975 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
1979 notification_thread_handle
);
1980 if (ret
!= LTTNG_OK
) {
1986 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1988 struct lttng_session_list_schedules_return schedules
= {
1989 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
1990 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
1991 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
1992 .size
.value
= cmd_ctx
->session
->rotate_size
,
1995 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2006 ret
= LTTNG_ERR_UND
;
2011 if (cmd_ctx
->llm
== NULL
) {
2012 DBG("Missing llm structure. Allocating one.");
2013 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2017 /* Set return code */
2018 cmd_ctx
->llm
->ret_code
= ret
;
2020 if (cmd_ctx
->session
) {
2021 session_unlock(cmd_ctx
->session
);
2022 session_put(cmd_ctx
->session
);
2024 if (need_tracing_session
) {
2025 session_unlock_list();
2028 assert(!rcu_read_ongoing());
2032 static int create_client_sock(void)
2034 int ret
, client_sock
;
2035 const mode_t old_umask
= umask(0);
2037 /* Create client tool unix socket */
2038 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2039 if (client_sock
< 0) {
2040 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2045 /* Set the cloexec flag */
2046 ret
= utils_set_fd_cloexec(client_sock
);
2048 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2049 "Continuing but note that the consumer daemon will have a "
2050 "reference to this socket on exec()", client_sock
);
2053 /* File permission MUST be 660 */
2054 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2056 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
2060 DBG("Created client socket (fd = %i)", client_sock
);
2067 static void cleanup_client_thread(void *data
)
2069 struct lttng_pipe
*quit_pipe
= data
;
2071 lttng_pipe_destroy(quit_pipe
);
2075 * This thread manage all clients request using the unix client socket for
2078 static void *thread_manage_clients(void *data
)
2080 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2082 uint32_t revents
, nb_fd
;
2083 struct command_ctx
*cmd_ctx
= NULL
;
2084 struct lttng_poll_event events
;
2085 int client_sock
= -1;
2086 struct lttng_pipe
*quit_pipe
= data
;
2087 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2089 DBG("[thread] Manage client started");
2091 is_root
= (getuid() == 0);
2093 client_sock
= create_client_sock();
2094 if (client_sock
< 0) {
2098 rcu_register_thread();
2100 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2102 health_code_update();
2104 ret
= lttcomm_listen_unix_sock(client_sock
);
2110 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2111 * more will be added to this poll set.
2113 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2115 goto error_create_poll
;
2118 /* Add the application registration socket */
2119 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2124 /* Add thread quit pipe */
2125 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2130 /* This testpoint is after we signal readiness to the parent. */
2131 if (testpoint(sessiond_thread_manage_clients
)) {
2135 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2139 health_code_update();
2141 /* Set state as running. */
2142 set_thread_state_running();
2145 const struct cmd_completion_handler
*cmd_completion_handler
;
2147 DBG("Accepting client command ...");
2149 /* Inifinite blocking call, waiting for transmission */
2151 health_poll_entry();
2152 ret
= lttng_poll_wait(&events
, -1);
2156 * Restart interrupted system call.
2158 if (errno
== EINTR
) {
2166 for (i
= 0; i
< nb_fd
; i
++) {
2167 revents
= LTTNG_POLL_GETEV(&events
, i
);
2168 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2170 health_code_update();
2173 /* No activity for this FD (poll implementation). */
2177 if (pollfd
== thread_quit_pipe_fd
) {
2181 /* Event on the registration socket */
2182 if (revents
& LPOLLIN
) {
2184 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2185 ERR("Client socket poll error");
2188 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2194 DBG("Wait for client response");
2196 health_code_update();
2198 sock
= lttcomm_accept_unix_sock(client_sock
);
2204 * Set the CLOEXEC flag. Return code is useless because either way, the
2207 (void) utils_set_fd_cloexec(sock
);
2209 /* Set socket option for credentials retrieval */
2210 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2215 /* Allocate context command to process the client request */
2216 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2217 if (cmd_ctx
== NULL
) {
2218 PERROR("zmalloc cmd_ctx");
2222 /* Allocate data buffer for reception */
2223 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2224 if (cmd_ctx
->lsm
== NULL
) {
2225 PERROR("zmalloc cmd_ctx->lsm");
2229 cmd_ctx
->llm
= NULL
;
2230 cmd_ctx
->session
= NULL
;
2232 health_code_update();
2235 * Data is received from the lttng client. The struct
2236 * lttcomm_session_msg (lsm) contains the command and data request of
2239 DBG("Receiving data from client ...");
2240 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2241 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2243 DBG("Nothing recv() from client... continuing");
2249 clean_command_ctx(&cmd_ctx
);
2253 health_code_update();
2255 // TODO: Validate cmd_ctx including sanity check for
2256 // security purpose.
2258 rcu_thread_online();
2260 * This function dispatch the work to the kernel or userspace tracer
2261 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2262 * informations for the client. The command context struct contains
2263 * everything this function may needs.
2265 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
2266 rcu_thread_offline();
2274 * TODO: Inform client somehow of the fatal error. At
2275 * this point, ret < 0 means that a zmalloc failed
2276 * (ENOMEM). Error detected but still accept
2277 * command, unless a socket error has been
2280 clean_command_ctx(&cmd_ctx
);
2284 cmd_completion_handler
= cmd_pop_completion_handler();
2285 if (cmd_completion_handler
) {
2286 enum lttng_error_code completion_code
;
2288 completion_code
= cmd_completion_handler
->run(
2289 cmd_completion_handler
->data
);
2290 if (completion_code
!= LTTNG_OK
) {
2291 clean_command_ctx(&cmd_ctx
);
2296 health_code_update();
2298 DBG("Sending response (size: %d, retcode: %s (%d))",
2299 cmd_ctx
->lttng_msg_size
,
2300 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2301 cmd_ctx
->llm
->ret_code
);
2302 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2304 ERR("Failed to send data back to client");
2307 /* End of transmission */
2314 clean_command_ctx(&cmd_ctx
);
2316 health_code_update();
2328 lttng_poll_clean(&events
);
2329 clean_command_ctx(&cmd_ctx
);
2333 unlink(config
.client_unix_sock_path
.value
);
2334 if (client_sock
>= 0) {
2335 ret
= close(client_sock
);
2343 ERR("Health error occurred in %s", __func__
);
2346 health_unregister(health_sessiond
);
2348 DBG("Client thread dying");
2350 rcu_unregister_thread();
2355 bool shutdown_client_thread(void *thread_data
)
2357 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2358 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2360 return notify_thread_pipe(write_fd
) == 1;
2363 struct lttng_thread
*launch_client_thread(void)
2365 struct lttng_pipe
*client_quit_pipe
;
2366 struct lttng_thread
*thread
;
2368 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2369 if (!client_quit_pipe
) {
2373 thread
= lttng_thread_create("Client management",
2374 thread_manage_clients
,
2375 shutdown_client_thread
,
2376 cleanup_client_thread
,
2383 * This thread is part of the threads that need to be fully
2384 * initialized before the session daemon is marked as "ready".
2386 wait_thread_state_running();
2390 cleanup_client_thread(client_quit_pipe
);