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>
29 #include <lttng/session-internal.h>
30 #include <lttng/session-descriptor-internal.h>
31 #include <lttng/tracker-internal.h>
34 #include "lttng-sessiond.h"
38 #include "health-sessiond.h"
39 #include "testpoint.h"
41 #include "manage-consumer.h"
46 static struct thread_state
{
52 static void set_thread_status(bool running
)
54 DBG("Marking client thread's state as %s", running
? "running" : "error");
55 thread_state
.running
= running
;
56 sem_post(&thread_state
.ready
);
59 static bool wait_thread_status(void)
61 DBG("Waiting for client thread to be ready");
62 sem_wait(&thread_state
.ready
);
63 if (thread_state
.running
) {
64 DBG("Client thread is ready");
66 ERR("Initialization of client thread failed");
69 return thread_state
.running
;
73 * Setup the outgoing data buffer for the response (llm) by allocating the
74 * right amount of memory and copying the original information from the lsm
77 * Return 0 on success, negative value on error.
79 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
80 const void *payload_buf
, size_t payload_len
,
81 const void *cmd_header_buf
, size_t cmd_header_len
)
84 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
85 const size_t cmd_header_offset
= header_len
;
86 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
87 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
90 cmd_ctx
->llm
= zmalloc(total_msg_size
);
92 if (cmd_ctx
->llm
== NULL
) {
98 /* Copy common data */
99 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
100 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
101 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
102 cmd_ctx
->llm
->data_size
= payload_len
;
103 cmd_ctx
->lttng_msg_size
= total_msg_size
;
105 /* Copy command header */
106 if (cmd_header_len
) {
107 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
113 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
122 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
123 * exec or it will fail.
125 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
127 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
131 * Fork and exec a consumer daemon (consumerd).
133 * Return pid if successful else -1.
135 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
139 const char *consumer_to_use
;
140 const char *verbosity
;
143 DBG("Spawning consumerd");
150 if (config
.verbose_consumer
) {
151 verbosity
= "--verbose";
152 } else if (lttng_opt_quiet
) {
153 verbosity
= "--quiet";
158 switch (consumer_data
->type
) {
159 case LTTNG_CONSUMER_KERNEL
:
161 * Find out which consumerd to execute. We will first try the
162 * 64-bit path, then the sessiond's installation directory, and
163 * fallback on the 32-bit one,
165 DBG3("Looking for a kernel consumer at these locations:");
166 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
167 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
168 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
169 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
170 DBG3("Found location #1");
171 consumer_to_use
= config
.consumerd64_bin_path
.value
;
172 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
173 DBG3("Found location #2");
174 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
175 } else if (config
.consumerd32_bin_path
.value
&&
176 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
177 DBG3("Found location #3");
178 consumer_to_use
= config
.consumerd32_bin_path
.value
;
180 DBG("Could not find any valid consumerd executable");
184 DBG("Using kernel consumer at: %s", consumer_to_use
);
185 (void) execl(consumer_to_use
,
186 "lttng-consumerd", verbosity
, "-k",
187 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
188 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
189 "--group", config
.tracing_group_name
.value
,
192 case LTTNG_CONSUMER64_UST
:
194 if (config
.consumerd64_lib_dir
.value
) {
199 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
203 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
204 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
209 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
210 if (tmp
[0] != '\0') {
214 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
221 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
222 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
223 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
224 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
225 "--group", config
.tracing_group_name
.value
,
229 case LTTNG_CONSUMER32_UST
:
231 if (config
.consumerd32_lib_dir
.value
) {
236 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
240 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
241 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
246 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
247 if (tmp
[0] != '\0') {
251 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
258 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
259 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
260 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
261 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
262 "--group", config
.tracing_group_name
.value
,
267 ERR("unknown consumer type");
271 PERROR("Consumer execl()");
273 /* Reaching this point, we got a failure on our execl(). */
275 } else if (pid
> 0) {
278 PERROR("start consumer fork");
286 * Spawn the consumerd daemon and session daemon thread.
288 static int start_consumerd(struct consumer_data
*consumer_data
)
293 * Set the listen() state on the socket since there is a possible race
294 * between the exec() of the consumer daemon and this call if place in the
295 * consumer thread. See bug #366 for more details.
297 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
302 pthread_mutex_lock(&consumer_data
->pid_mutex
);
303 if (consumer_data
->pid
!= 0) {
304 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
308 ret
= spawn_consumerd(consumer_data
);
310 ERR("Spawning consumerd failed");
311 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
315 /* Setting up the consumer_data pid */
316 consumer_data
->pid
= ret
;
317 DBG2("Consumer pid %d", consumer_data
->pid
);
318 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
320 DBG2("Spawning consumer control thread");
321 ret
= spawn_consumer_thread(consumer_data
);
323 ERR("Fatal error spawning consumer control thread");
331 /* Cleanup already created sockets on error. */
332 if (consumer_data
->err_sock
>= 0) {
335 err
= close(consumer_data
->err_sock
);
337 PERROR("close consumer data error socket");
344 * Copy consumer output from the tracing session to the domain session. The
345 * function also applies the right modification on a per domain basis for the
346 * trace files destination directory.
348 * Should *NOT* be called with RCU read-side lock held.
350 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
353 const char *dir_name
;
354 struct consumer_output
*consumer
;
357 assert(session
->consumer
);
360 case LTTNG_DOMAIN_KERNEL
:
361 DBG3("Copying tracing session consumer output in kernel session");
363 * XXX: We should audit the session creation and what this function
364 * does "extra" in order to avoid a destroy since this function is used
365 * in the domain session creation (kernel and ust) only. Same for UST
368 if (session
->kernel_session
->consumer
) {
369 consumer_output_put(session
->kernel_session
->consumer
);
371 session
->kernel_session
->consumer
=
372 consumer_copy_output(session
->consumer
);
373 /* Ease our life a bit for the next part */
374 consumer
= session
->kernel_session
->consumer
;
375 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
377 case LTTNG_DOMAIN_JUL
:
378 case LTTNG_DOMAIN_LOG4J
:
379 case LTTNG_DOMAIN_PYTHON
:
380 case LTTNG_DOMAIN_UST
:
381 DBG3("Copying tracing session consumer output in UST session");
382 if (session
->ust_session
->consumer
) {
383 consumer_output_put(session
->ust_session
->consumer
);
385 session
->ust_session
->consumer
=
386 consumer_copy_output(session
->consumer
);
387 /* Ease our life a bit for the next part */
388 consumer
= session
->ust_session
->consumer
;
389 dir_name
= DEFAULT_UST_TRACE_DIR
;
392 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
396 /* Append correct directory to subdir */
397 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
398 sizeof(consumer
->domain_subdir
));
403 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
411 * Create an UST session and add it to the session ust list.
413 * Should *NOT* be called with RCU read-side lock held.
415 static int create_ust_session(struct ltt_session
*session
,
416 const struct lttng_domain
*domain
)
419 struct ltt_ust_session
*lus
= NULL
;
423 assert(session
->consumer
);
425 switch (domain
->type
) {
426 case LTTNG_DOMAIN_JUL
:
427 case LTTNG_DOMAIN_LOG4J
:
428 case LTTNG_DOMAIN_PYTHON
:
429 case LTTNG_DOMAIN_UST
:
432 ERR("Unknown UST domain on create session %d", domain
->type
);
433 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
437 DBG("Creating UST session");
439 lus
= trace_ust_create_session(session
->id
);
441 ret
= LTTNG_ERR_UST_SESS_FAIL
;
445 lus
->uid
= session
->uid
;
446 lus
->gid
= session
->gid
;
447 lus
->output_traces
= session
->output_traces
;
448 lus
->snapshot_mode
= session
->snapshot_mode
;
449 lus
->live_timer_interval
= session
->live_timer
;
450 session
->ust_session
= lus
;
451 if (session
->shm_path
[0]) {
452 strncpy(lus
->root_shm_path
, session
->shm_path
,
453 sizeof(lus
->root_shm_path
));
454 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
455 strncpy(lus
->shm_path
, session
->shm_path
,
456 sizeof(lus
->shm_path
));
457 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
458 strncat(lus
->shm_path
, "/ust",
459 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
461 /* Copy session output to the newly created UST session */
462 ret
= copy_session_consumer(domain
->type
, session
);
463 if (ret
!= LTTNG_OK
) {
471 session
->ust_session
= NULL
;
476 * Create a kernel tracer session then create the default channel.
478 static int create_kernel_session(struct ltt_session
*session
)
482 DBG("Creating kernel session");
484 ret
= kernel_create_session(session
);
486 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
490 /* Code flow safety */
491 assert(session
->kernel_session
);
493 /* Copy session output to the newly created Kernel session */
494 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
495 if (ret
!= LTTNG_OK
) {
499 session
->kernel_session
->uid
= session
->uid
;
500 session
->kernel_session
->gid
= session
->gid
;
501 session
->kernel_session
->output_traces
= session
->output_traces
;
502 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
507 trace_kernel_destroy_session(session
->kernel_session
);
508 session
->kernel_session
= NULL
;
514 * Count number of session permitted by uid/gid.
516 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
519 struct ltt_session
*session
;
520 const struct ltt_session_list
*session_list
= session_get_list();
522 DBG("Counting number of available session for UID %d GID %d",
524 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
525 if (!session_get(session
)) {
528 session_lock(session
);
529 /* Only count the sessions the user can control. */
530 if (session_access_ok(session
, uid
, gid
) &&
531 !session
->destroyed
) {
534 session_unlock(session
);
535 session_put(session
);
540 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
541 int *sock_error
, struct lttng_event
*event
)
544 struct lttng_userspace_probe_location
*probe_location
;
545 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
546 struct lttng_dynamic_buffer probe_location_buffer
;
547 struct lttng_buffer_view buffer_view
;
550 * Create a buffer to store the serialized version of the probe
553 lttng_dynamic_buffer_init(&probe_location_buffer
);
554 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
555 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
557 ret
= LTTNG_ERR_NOMEM
;
562 * Receive the probe location.
564 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
565 probe_location_buffer
.size
);
567 DBG("Nothing recv() from client var len data... continuing");
569 lttng_dynamic_buffer_reset(&probe_location_buffer
);
570 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
574 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
575 &probe_location_buffer
, 0, probe_location_buffer
.size
);
578 * Extract the probe location from the serialized version.
580 ret
= lttng_userspace_probe_location_create_from_buffer(
581 &buffer_view
, &probe_location
);
583 WARN("Failed to create a userspace probe location from the received buffer");
584 lttng_dynamic_buffer_reset( &probe_location_buffer
);
585 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
590 * Receive the file descriptor to the target binary from the client.
592 DBG("Receiving userspace probe target FD from client ...");
593 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
595 DBG("Nothing recv() from client userspace probe fd... continuing");
597 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
602 * Set the file descriptor received from the client through the unix
603 * socket in the probe location.
605 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
607 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
612 * From the kernel tracer's perspective, all userspace probe event types
613 * are all the same: a file and an offset.
615 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
616 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
617 ret
= lttng_userspace_probe_location_function_set_binary_fd(
620 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
621 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
625 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
630 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
634 /* Attach the probe location to the event. */
635 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
637 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
641 lttng_dynamic_buffer_reset(&probe_location_buffer
);
647 * Version of setup_lttng_msg() without command header.
649 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
650 void *payload_buf
, size_t payload_len
)
652 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
656 * Free memory of a command context structure.
658 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
660 DBG("Clean command context structure");
662 if ((*cmd_ctx
)->llm
) {
663 free((*cmd_ctx
)->llm
);
665 if ((*cmd_ctx
)->lsm
) {
666 free((*cmd_ctx
)->lsm
);
674 * Check if the current kernel tracer supports the session rotation feature.
675 * Return 1 if it does, 0 otherwise.
677 static int check_rotate_compatible(void)
681 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
682 DBG("Kernel tracer version is not compatible with the rotation feature");
690 * Send data on a unix socket using the liblttsessiondcomm API.
692 * Return lttcomm error code.
694 static int send_unix_sock(int sock
, void *buf
, size_t len
)
696 /* Check valid length */
701 return lttcomm_send_unix_sock(sock
, buf
, len
);
705 * Process the command requested by the lttng client within the command
706 * context structure. This function make sure that the return structure (llm)
707 * is set and ready for transmission before returning.
709 * Return any error encountered or 0 for success.
711 * "sock" is only used for special-case var. len data.
712 * A command may assume the ownership of the socket, in which case its value
713 * should be set to -1.
715 * Should *NOT* be called with RCU read-side lock held.
717 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
721 int need_tracing_session
= 1;
724 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
726 assert(!rcu_read_ongoing());
730 switch (cmd_ctx
->lsm
->cmd_type
) {
731 case LTTNG_CREATE_SESSION_EXT
:
732 case LTTNG_DESTROY_SESSION
:
733 case LTTNG_LIST_SESSIONS
:
734 case LTTNG_LIST_DOMAINS
:
735 case LTTNG_START_TRACE
:
736 case LTTNG_STOP_TRACE
:
737 case LTTNG_DATA_PENDING
:
738 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
739 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
740 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
741 case LTTNG_SNAPSHOT_RECORD
:
742 case LTTNG_SAVE_SESSION
:
743 case LTTNG_SET_SESSION_SHM_PATH
:
744 case LTTNG_REGENERATE_METADATA
:
745 case LTTNG_REGENERATE_STATEDUMP
:
746 case LTTNG_REGISTER_TRIGGER
:
747 case LTTNG_UNREGISTER_TRIGGER
:
748 case LTTNG_ROTATE_SESSION
:
749 case LTTNG_ROTATION_GET_INFO
:
750 case LTTNG_ROTATION_SET_SCHEDULE
:
751 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
752 case LTTNG_CLEAR_SESSION
:
759 if (config
.no_kernel
&& need_domain
760 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
762 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
764 ret
= LTTNG_ERR_KERN_NA
;
769 /* Deny register consumer if we already have a spawned consumer. */
770 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
771 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
772 if (kconsumer_data
.pid
> 0) {
773 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
774 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
777 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
781 * Check for command that don't needs to allocate a returned payload. We do
782 * this here so we don't have to make the call for no payload at each
785 switch(cmd_ctx
->lsm
->cmd_type
) {
786 case LTTNG_LIST_SESSIONS
:
787 case LTTNG_LIST_TRACEPOINTS
:
788 case LTTNG_LIST_TRACEPOINT_FIELDS
:
789 case LTTNG_LIST_DOMAINS
:
790 case LTTNG_LIST_CHANNELS
:
791 case LTTNG_LIST_EVENTS
:
792 case LTTNG_LIST_SYSCALLS
:
793 case LTTNG_LIST_TRACKER_IDS
:
794 case LTTNG_DATA_PENDING
:
795 case LTTNG_ROTATE_SESSION
:
796 case LTTNG_ROTATION_GET_INFO
:
797 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
800 /* Setup lttng message with no payload */
801 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
803 /* This label does not try to unlock the session */
804 goto init_setup_error
;
808 /* Commands that DO NOT need a session. */
809 switch (cmd_ctx
->lsm
->cmd_type
) {
810 case LTTNG_CREATE_SESSION_EXT
:
811 case LTTNG_LIST_SESSIONS
:
812 case LTTNG_LIST_TRACEPOINTS
:
813 case LTTNG_LIST_SYSCALLS
:
814 case LTTNG_LIST_TRACEPOINT_FIELDS
:
815 case LTTNG_SAVE_SESSION
:
816 case LTTNG_REGISTER_TRIGGER
:
817 case LTTNG_UNREGISTER_TRIGGER
:
818 need_tracing_session
= 0;
821 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
823 * We keep the session list lock across _all_ commands
824 * for now, because the per-session lock does not
825 * handle teardown properly.
828 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
829 if (cmd_ctx
->session
== NULL
) {
830 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
833 /* Acquire lock for the session */
834 session_lock(cmd_ctx
->session
);
840 * Commands that need a valid session but should NOT create one if none
841 * exists. Instead of creating one and destroying it when the command is
842 * handled, process that right before so we save some round trip in useless
845 switch (cmd_ctx
->lsm
->cmd_type
) {
846 case LTTNG_DISABLE_CHANNEL
:
847 case LTTNG_DISABLE_EVENT
:
848 switch (cmd_ctx
->lsm
->domain
.type
) {
849 case LTTNG_DOMAIN_KERNEL
:
850 if (!cmd_ctx
->session
->kernel_session
) {
851 ret
= LTTNG_ERR_NO_CHANNEL
;
855 case LTTNG_DOMAIN_JUL
:
856 case LTTNG_DOMAIN_LOG4J
:
857 case LTTNG_DOMAIN_PYTHON
:
858 case LTTNG_DOMAIN_UST
:
859 if (!cmd_ctx
->session
->ust_session
) {
860 ret
= LTTNG_ERR_NO_CHANNEL
;
865 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
877 * Check domain type for specific "pre-action".
879 switch (cmd_ctx
->lsm
->domain
.type
) {
880 case LTTNG_DOMAIN_KERNEL
:
882 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
886 /* Kernel tracer check */
887 if (!kernel_tracer_is_initialized()) {
888 /* Basically, load kernel tracer modules */
889 ret
= init_kernel_tracer();
895 /* Consumer is in an ERROR state. Report back to client */
896 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
897 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
901 /* Need a session for kernel command */
902 if (need_tracing_session
) {
903 if (cmd_ctx
->session
->kernel_session
== NULL
) {
904 ret
= create_kernel_session(cmd_ctx
->session
);
905 if (ret
!= LTTNG_OK
) {
906 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
911 /* Start the kernel consumer daemon */
912 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
913 if (kconsumer_data
.pid
== 0 &&
914 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
915 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
916 ret
= start_consumerd(&kconsumer_data
);
918 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
921 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
923 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
927 * The consumer was just spawned so we need to add the socket to
928 * the consumer output of the session if exist.
930 ret
= consumer_create_socket(&kconsumer_data
,
931 cmd_ctx
->session
->kernel_session
->consumer
);
938 case LTTNG_DOMAIN_JUL
:
939 case LTTNG_DOMAIN_LOG4J
:
940 case LTTNG_DOMAIN_PYTHON
:
941 case LTTNG_DOMAIN_UST
:
943 if (!ust_app_supported()) {
944 ret
= LTTNG_ERR_NO_UST
;
947 /* Consumer is in an ERROR state. Report back to client */
948 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
949 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
953 if (need_tracing_session
) {
954 /* Create UST session if none exist. */
955 if (cmd_ctx
->session
->ust_session
== NULL
) {
956 ret
= create_ust_session(cmd_ctx
->session
,
957 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
));
958 if (ret
!= LTTNG_OK
) {
963 /* Start the UST consumer daemons */
965 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
966 if (config
.consumerd64_bin_path
.value
&&
967 ustconsumer64_data
.pid
== 0 &&
968 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
969 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
970 ret
= start_consumerd(&ustconsumer64_data
);
972 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
973 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
977 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
978 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
980 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
984 * Setup socket for consumer 64 bit. No need for atomic access
985 * since it was set above and can ONLY be set in this thread.
987 ret
= consumer_create_socket(&ustconsumer64_data
,
988 cmd_ctx
->session
->ust_session
->consumer
);
994 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
995 if (config
.consumerd32_bin_path
.value
&&
996 ustconsumer32_data
.pid
== 0 &&
997 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
998 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
999 ret
= start_consumerd(&ustconsumer32_data
);
1001 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1002 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
1006 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
1007 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1009 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1013 * Setup socket for consumer 32 bit. No need for atomic access
1014 * since it was set above and can ONLY be set in this thread.
1016 ret
= consumer_create_socket(&ustconsumer32_data
,
1017 cmd_ctx
->session
->ust_session
->consumer
);
1029 /* Validate consumer daemon state when start/stop trace command */
1030 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1031 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1032 switch (cmd_ctx
->lsm
->domain
.type
) {
1033 case LTTNG_DOMAIN_NONE
:
1035 case LTTNG_DOMAIN_JUL
:
1036 case LTTNG_DOMAIN_LOG4J
:
1037 case LTTNG_DOMAIN_PYTHON
:
1038 case LTTNG_DOMAIN_UST
:
1039 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1040 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1044 case LTTNG_DOMAIN_KERNEL
:
1045 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1046 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1051 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1057 * Check that the UID or GID match that of the tracing session.
1058 * The root user can interact with all sessions.
1060 if (need_tracing_session
) {
1061 if (!session_access_ok(cmd_ctx
->session
,
1062 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1063 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1064 cmd_ctx
->session
->destroyed
) {
1065 ret
= LTTNG_ERR_EPERM
;
1071 * Send relayd information to consumer as soon as we have a domain and a
1074 if (cmd_ctx
->session
&& need_domain
) {
1076 * Setup relayd if not done yet. If the relayd information was already
1077 * sent to the consumer, this call will gracefully return.
1079 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1080 if (ret
!= LTTNG_OK
) {
1085 /* Process by command type */
1086 switch (cmd_ctx
->lsm
->cmd_type
) {
1087 case LTTNG_ADD_CONTEXT
:
1090 * An LTTNG_ADD_CONTEXT command might have a supplementary
1091 * payload if the context being added is an application context.
1093 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1094 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1095 char *provider_name
= NULL
, *context_name
= NULL
;
1096 size_t provider_name_len
=
1097 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1098 size_t context_name_len
=
1099 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1101 if (provider_name_len
== 0 || context_name_len
== 0) {
1103 * Application provider and context names MUST
1106 ret
= -LTTNG_ERR_INVALID
;
1110 provider_name
= zmalloc(provider_name_len
+ 1);
1111 if (!provider_name
) {
1112 ret
= -LTTNG_ERR_NOMEM
;
1115 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1118 context_name
= zmalloc(context_name_len
+ 1);
1119 if (!context_name
) {
1120 ret
= -LTTNG_ERR_NOMEM
;
1121 goto error_add_context
;
1123 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1126 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1129 goto error_add_context
;
1132 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1135 goto error_add_context
;
1140 * cmd_add_context assumes ownership of the provider and context
1143 ret
= cmd_add_context(cmd_ctx
->session
,
1144 cmd_ctx
->lsm
->domain
.type
,
1145 cmd_ctx
->lsm
->u
.context
.channel_name
,
1146 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.context
.ctx
),
1147 kernel_poll_pipe
[1]);
1149 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1150 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1152 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1153 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1159 case LTTNG_DISABLE_CHANNEL
:
1161 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1162 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1165 case LTTNG_DISABLE_EVENT
:
1169 * FIXME: handle filter; for now we just receive the filter's
1170 * bytecode along with the filter expression which are sent by
1171 * liblttng-ctl and discard them.
1173 * This fixes an issue where the client may block while sending
1174 * the filter payload and encounter an error because the session
1175 * daemon closes the socket without ever handling this data.
1177 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1178 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1181 char data
[LTTNG_FILTER_MAX_LEN
];
1183 DBG("Discarding disable event command payload of size %zu", count
);
1185 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1186 count
> sizeof(data
) ? sizeof(data
) : count
);
1191 count
-= (size_t) ret
;
1194 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1195 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1196 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.disable
.event
));
1199 case LTTNG_ENABLE_CHANNEL
:
1201 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1202 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1203 ret
= cmd_enable_channel(cmd_ctx
->session
,
1204 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1205 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.channel
.chan
),
1206 kernel_poll_pipe
[1]);
1209 case LTTNG_TRACK_ID
:
1211 struct lttng_tracker_id
*id
= NULL
;
1212 enum lttng_tracker_id_status status
;
1214 id
= lttng_tracker_id_create();
1216 ret
= LTTNG_ERR_NOMEM
;
1220 switch (cmd_ctx
->lsm
->u
.id_tracker
.id_type
) {
1222 status
= lttng_tracker_id_set_all(id
);
1224 case LTTNG_ID_VALUE
:
1225 status
= lttng_tracker_id_set_value(
1226 id
, cmd_ctx
->lsm
->u
.id_tracker
.u
.value
);
1228 case LTTNG_ID_STRING
:
1230 const size_t var_len
= cmd_ctx
->lsm
->u
.id_tracker
.u
.var_len
;
1231 char *string
= NULL
;
1233 string
= zmalloc(var_len
);
1235 lttng_tracker_id_destroy(id
);
1236 ret
= LTTNG_ERR_NOMEM
;
1239 DBG("Receiving var len tracker id string from client");
1240 ret
= lttcomm_recv_unix_sock(*sock
, string
, var_len
);
1242 DBG("Nothing received");
1245 lttng_tracker_id_destroy(id
);
1246 ret
= LTTNG_ERR_INVALID
;
1249 if (strnlen(string
, var_len
) != var_len
- 1) {
1250 DBG("String received as tracker ID is not NULL-terminated");
1252 lttng_tracker_id_destroy(id
);
1253 ret
= LTTNG_ERR_INVALID
;
1257 status
= lttng_tracker_id_set_string(id
, string
);
1262 lttng_tracker_id_destroy(id
);
1263 ret
= LTTNG_ERR_INVALID
;
1267 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
1268 ERR("Invalid value for tracker id");
1269 ret
= LTTNG_ERR_INVALID
;
1270 lttng_tracker_id_destroy(id
);
1274 ret
= cmd_track_id(cmd_ctx
->session
,
1275 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
1276 cmd_ctx
->lsm
->domain
.type
, id
);
1277 lttng_tracker_id_destroy(id
);
1280 case LTTNG_UNTRACK_ID
:
1282 struct lttng_tracker_id
*id
= NULL
;
1283 enum lttng_tracker_id_status status
;
1285 id
= lttng_tracker_id_create();
1287 switch (cmd_ctx
->lsm
->u
.id_tracker
.id_type
) {
1289 status
= lttng_tracker_id_set_all(id
);
1291 case LTTNG_ID_VALUE
:
1292 status
= lttng_tracker_id_set_value(
1293 id
, cmd_ctx
->lsm
->u
.id_tracker
.u
.value
);
1295 case LTTNG_ID_STRING
:
1297 const size_t var_len
= cmd_ctx
->lsm
->u
.id_tracker
.u
.var_len
;
1298 char *string
= NULL
;
1300 string
= zmalloc(var_len
);
1302 ret
= LTTNG_ERR_NOMEM
;
1303 lttng_tracker_id_destroy(id
);
1306 DBG("Receiving var len tracker id string from client");
1307 ret
= lttcomm_recv_unix_sock(*sock
, string
, var_len
);
1309 DBG("Nothing received");
1311 lttng_tracker_id_destroy(id
);
1313 ret
= LTTNG_ERR_INVALID
;
1316 if (strnlen(string
, var_len
) != var_len
- 1) {
1317 DBG("String received as tracker ID is not NULL-terminated");
1318 lttng_tracker_id_destroy(id
);
1320 ret
= LTTNG_ERR_INVALID
;
1323 status
= lttng_tracker_id_set_string(id
, string
);
1328 lttng_tracker_id_destroy(id
);
1329 ret
= LTTNG_ERR_INVALID
;
1333 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
1334 ERR("Invalid tracker id");
1335 lttng_tracker_id_destroy(id
);
1336 ret
= LTTNG_ERR_INVALID
;
1340 ret
= cmd_untrack_id(cmd_ctx
->session
,
1341 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
1342 cmd_ctx
->lsm
->domain
.type
, id
);
1343 lttng_tracker_id_destroy(id
);
1346 case LTTNG_ENABLE_EVENT
:
1348 struct lttng_event
*ev
= NULL
;
1349 struct lttng_event_exclusion
*exclusion
= NULL
;
1350 struct lttng_filter_bytecode
*bytecode
= NULL
;
1351 char *filter_expression
= NULL
;
1353 /* Handle exclusion events and receive it from the client. */
1354 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1355 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1357 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1358 (count
* LTTNG_SYMBOL_NAME_LEN
));
1360 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1364 DBG("Receiving var len exclusion event list from client ...");
1365 exclusion
->count
= count
;
1366 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1367 count
* LTTNG_SYMBOL_NAME_LEN
);
1369 DBG("Nothing recv() from client var len data... continuing");
1372 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1377 /* Get filter expression from client. */
1378 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1379 size_t expression_len
=
1380 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1382 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1383 ret
= LTTNG_ERR_FILTER_INVAL
;
1388 filter_expression
= zmalloc(expression_len
);
1389 if (!filter_expression
) {
1391 ret
= LTTNG_ERR_FILTER_NOMEM
;
1395 /* Receive var. len. data */
1396 DBG("Receiving var len filter's expression from client ...");
1397 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1400 DBG("Nothing recv() from client var len data... continuing");
1402 free(filter_expression
);
1404 ret
= LTTNG_ERR_FILTER_INVAL
;
1409 /* Handle filter and get bytecode from client. */
1410 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1411 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1413 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1414 ret
= LTTNG_ERR_FILTER_INVAL
;
1415 free(filter_expression
);
1420 bytecode
= zmalloc(bytecode_len
);
1422 free(filter_expression
);
1424 ret
= LTTNG_ERR_FILTER_NOMEM
;
1428 /* Receive var. len. data */
1429 DBG("Receiving var len filter's bytecode from client ...");
1430 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1432 DBG("Nothing recv() from client var len data... continuing");
1434 free(filter_expression
);
1437 ret
= LTTNG_ERR_FILTER_INVAL
;
1441 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1442 free(filter_expression
);
1445 ret
= LTTNG_ERR_FILTER_INVAL
;
1450 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.enable
.event
));
1452 DBG("Failed to copy event: %s",
1453 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1454 free(filter_expression
);
1457 ret
= LTTNG_ERR_NOMEM
;
1462 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1463 /* Expect a userspace probe description. */
1464 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1466 free(filter_expression
);
1469 lttng_event_destroy(ev
);
1474 ret
= cmd_enable_event(cmd_ctx
->session
,
1475 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1476 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1478 filter_expression
, bytecode
, exclusion
,
1479 kernel_poll_pipe
[1]);
1480 lttng_event_destroy(ev
);
1483 case LTTNG_LIST_TRACEPOINTS
:
1485 struct lttng_event
*events
;
1488 session_lock_list();
1489 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1490 session_unlock_list();
1491 if (nb_events
< 0) {
1492 /* Return value is a negative lttng_error_code. */
1498 * Setup lttng message with payload size set to the event list size in
1499 * bytes and then copy list into the llm payload.
1501 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1502 sizeof(struct lttng_event
) * nb_events
);
1512 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1514 struct lttng_event_field
*fields
;
1517 session_lock_list();
1518 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1520 session_unlock_list();
1521 if (nb_fields
< 0) {
1522 /* Return value is a negative lttng_error_code. */
1528 * Setup lttng message with payload size set to the event list size in
1529 * bytes and then copy list into the llm payload.
1531 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1532 sizeof(struct lttng_event_field
) * nb_fields
);
1542 case LTTNG_LIST_SYSCALLS
:
1544 struct lttng_event
*events
;
1547 nb_events
= cmd_list_syscalls(&events
);
1548 if (nb_events
< 0) {
1549 /* Return value is a negative lttng_error_code. */
1555 * Setup lttng message with payload size set to the event list size in
1556 * bytes and then copy list into the llm payload.
1558 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1559 sizeof(struct lttng_event
) * nb_events
);
1569 case LTTNG_LIST_TRACKER_IDS
:
1571 struct lttcomm_tracker_command_header cmd_header
;
1572 struct lttng_tracker_ids
*ids
= NULL
;
1573 enum lttng_tracker_id_status status
;
1574 unsigned int nr_ids
;
1575 struct lttng_dynamic_buffer buf
;
1577 ret
= cmd_list_tracker_ids(
1578 cmd_ctx
->lsm
->u
.id_tracker
.tracker_type
,
1579 cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1581 if (ret
!= LTTNG_OK
) {
1585 lttng_dynamic_buffer_init(&buf
);
1587 status
= lttng_tracker_ids_get_count(ids
, &nr_ids
);
1588 if (status
!= LTTNG_TRACKER_ID_STATUS_OK
) {
1589 ret
= -LTTNG_ERR_INVALID
;
1590 goto error_list_tracker
;
1593 cmd_header
.nb_tracker_id
= nr_ids
;
1595 ret
= lttng_tracker_ids_serialize(ids
, &buf
);
1597 goto error_list_tracker
;
1600 ret
= setup_lttng_msg(cmd_ctx
, buf
.data
, buf
.size
, &cmd_header
,
1601 sizeof(cmd_header
));
1603 lttng_tracker_ids_destroy(ids
);
1604 lttng_dynamic_buffer_reset(&buf
);
1612 case LTTNG_SET_CONSUMER_URI
:
1615 struct lttng_uri
*uris
;
1617 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1618 len
= nb_uri
* sizeof(struct lttng_uri
);
1621 ret
= LTTNG_ERR_INVALID
;
1625 uris
= zmalloc(len
);
1627 ret
= LTTNG_ERR_FATAL
;
1631 /* Receive variable len data */
1632 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1633 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1635 DBG("No URIs received from client... continuing");
1637 ret
= LTTNG_ERR_SESSION_FAIL
;
1642 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1644 if (ret
!= LTTNG_OK
) {
1651 case LTTNG_START_TRACE
:
1654 * On the first start, if we have a kernel session and we have
1655 * enabled time or size-based rotations, we have to make sure
1656 * the kernel tracer supports it.
1658 if (!cmd_ctx
->session
->has_been_started
&& \
1659 cmd_ctx
->session
->kernel_session
&& \
1660 (cmd_ctx
->session
->rotate_timer_period
|| \
1661 cmd_ctx
->session
->rotate_size
) && \
1662 !check_rotate_compatible()) {
1663 DBG("Kernel tracer version is not compatible with the rotation feature");
1664 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1667 ret
= cmd_start_trace(cmd_ctx
->session
);
1670 case LTTNG_STOP_TRACE
:
1672 ret
= cmd_stop_trace(cmd_ctx
->session
);
1675 case LTTNG_DESTROY_SESSION
:
1677 ret
= cmd_destroy_session(cmd_ctx
->session
,
1678 notification_thread_handle
,
1682 case LTTNG_LIST_DOMAINS
:
1685 struct lttng_domain
*domains
= NULL
;
1687 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1689 /* Return value is a negative lttng_error_code. */
1694 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1695 nb_dom
* sizeof(struct lttng_domain
));
1705 case LTTNG_LIST_CHANNELS
:
1707 ssize_t payload_size
;
1708 struct lttng_channel
*channels
= NULL
;
1710 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1711 cmd_ctx
->session
, &channels
);
1712 if (payload_size
< 0) {
1713 /* Return value is a negative lttng_error_code. */
1714 ret
= -payload_size
;
1718 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1729 case LTTNG_LIST_EVENTS
:
1732 struct lttng_event
*events
= NULL
;
1733 struct lttcomm_event_command_header cmd_header
;
1736 memset(&cmd_header
, 0, sizeof(cmd_header
));
1737 /* Extended infos are included at the end of events */
1738 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1739 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1740 &events
, &total_size
);
1743 /* Return value is a negative lttng_error_code. */
1748 cmd_header
.nb_events
= nb_event
;
1749 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1750 &cmd_header
, sizeof(cmd_header
));
1760 case LTTNG_LIST_SESSIONS
:
1762 unsigned int nr_sessions
;
1763 void *sessions_payload
;
1766 session_lock_list();
1767 nr_sessions
= lttng_sessions_count(
1768 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1769 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1771 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1772 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1773 sessions_payload
= zmalloc(payload_len
);
1775 if (!sessions_payload
) {
1776 session_unlock_list();
1781 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1782 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1783 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1784 session_unlock_list();
1786 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1788 free(sessions_payload
);
1797 case LTTNG_REGISTER_CONSUMER
:
1799 struct consumer_data
*cdata
;
1801 switch (cmd_ctx
->lsm
->domain
.type
) {
1802 case LTTNG_DOMAIN_KERNEL
:
1803 cdata
= &kconsumer_data
;
1806 ret
= LTTNG_ERR_UND
;
1810 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1811 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1814 case LTTNG_DATA_PENDING
:
1817 uint8_t pending_ret_byte
;
1819 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1824 * This function may returns 0 or 1 to indicate whether or not
1825 * there is data pending. In case of error, it should return an
1826 * LTTNG_ERR code. However, some code paths may still return
1827 * a nondescript error code, which we handle by returning an
1830 if (pending_ret
== 0 || pending_ret
== 1) {
1832 * ret will be set to LTTNG_OK at the end of
1835 } else if (pending_ret
< 0) {
1836 ret
= LTTNG_ERR_UNK
;
1843 pending_ret_byte
= (uint8_t) pending_ret
;
1845 /* 1 byte to return whether or not data is pending */
1846 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1847 &pending_ret_byte
, 1);
1856 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1858 uint32_t snapshot_id
;
1859 struct lttcomm_lttng_output_id reply
;
1861 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1862 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
),
1864 if (ret
!= LTTNG_OK
) {
1867 reply
.id
= snapshot_id
;
1869 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1875 /* Copy output list into message payload */
1879 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1881 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1882 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
));
1885 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1888 struct lttng_snapshot_output
*outputs
= NULL
;
1890 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1891 if (nb_output
< 0) {
1896 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1897 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1898 nb_output
* sizeof(struct lttng_snapshot_output
));
1908 case LTTNG_SNAPSHOT_RECORD
:
1910 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1911 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_record
.output
),
1912 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1915 case LTTNG_CREATE_SESSION_EXT
:
1917 struct lttng_dynamic_buffer payload
;
1918 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1920 lttng_dynamic_buffer_init(&payload
);
1921 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1922 if (ret
!= LTTNG_OK
) {
1926 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1929 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1930 lttng_session_descriptor_destroy(return_descriptor
);
1931 ret
= LTTNG_ERR_NOMEM
;
1934 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1937 lttng_session_descriptor_destroy(return_descriptor
);
1938 ret
= LTTNG_ERR_NOMEM
;
1941 lttng_dynamic_buffer_reset(&payload
);
1942 lttng_session_descriptor_destroy(return_descriptor
);
1946 case LTTNG_SAVE_SESSION
:
1948 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1952 case LTTNG_SET_SESSION_SHM_PATH
:
1954 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1955 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1958 case LTTNG_REGENERATE_METADATA
:
1960 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1963 case LTTNG_REGENERATE_STATEDUMP
:
1965 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1968 case LTTNG_REGISTER_TRIGGER
:
1970 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
1971 notification_thread_handle
);
1974 case LTTNG_UNREGISTER_TRIGGER
:
1976 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
1977 notification_thread_handle
);
1980 case LTTNG_ROTATE_SESSION
:
1982 struct lttng_rotate_session_return rotate_return
;
1984 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1986 memset(&rotate_return
, 0, sizeof(rotate_return
));
1987 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1988 DBG("Kernel tracer version is not compatible with the rotation feature");
1989 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1993 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
1995 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2001 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2002 sizeof(rotate_return
));
2011 case LTTNG_ROTATION_GET_INFO
:
2013 struct lttng_rotation_get_info_return get_info_return
;
2015 memset(&get_info_return
, 0, sizeof(get_info_return
));
2016 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2017 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
2023 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2024 sizeof(get_info_return
));
2033 case LTTNG_ROTATION_SET_SCHEDULE
:
2036 enum lttng_rotation_schedule_type schedule_type
;
2039 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2040 DBG("Kernel tracer version does not support session rotations");
2041 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2045 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
2046 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
2047 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
2049 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
2053 notification_thread_handle
);
2054 if (ret
!= LTTNG_OK
) {
2060 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2062 struct lttng_session_list_schedules_return schedules
= {
2063 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2064 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2065 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2066 .size
.value
= cmd_ctx
->session
->rotate_size
,
2069 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2079 case LTTNG_CLEAR_SESSION
:
2081 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2085 ret
= LTTNG_ERR_UND
;
2090 if (cmd_ctx
->llm
== NULL
) {
2091 DBG("Missing llm structure. Allocating one.");
2092 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2096 /* Set return code */
2097 cmd_ctx
->llm
->ret_code
= ret
;
2099 if (cmd_ctx
->session
) {
2100 session_unlock(cmd_ctx
->session
);
2101 session_put(cmd_ctx
->session
);
2102 cmd_ctx
->session
= NULL
;
2104 if (need_tracing_session
) {
2105 session_unlock_list();
2108 assert(!rcu_read_ongoing());
2112 static int create_client_sock(void)
2114 int ret
, client_sock
;
2115 const mode_t old_umask
= umask(0);
2117 /* Create client tool unix socket */
2118 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2119 if (client_sock
< 0) {
2120 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2125 /* Set the cloexec flag */
2126 ret
= utils_set_fd_cloexec(client_sock
);
2128 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2129 "Continuing but note that the consumer daemon will have a "
2130 "reference to this socket on exec()", client_sock
);
2133 /* File permission MUST be 660 */
2134 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2136 ERR("Set file permissions failed: %s",
2137 config
.client_unix_sock_path
.value
);
2139 (void) lttcomm_close_unix_sock(client_sock
);
2143 DBG("Created client socket (fd = %i)", client_sock
);
2150 static void cleanup_client_thread(void *data
)
2152 struct lttng_pipe
*quit_pipe
= data
;
2154 lttng_pipe_destroy(quit_pipe
);
2157 static void thread_init_cleanup(void *data
)
2159 set_thread_status(false);
2163 * This thread manage all clients request using the unix client socket for
2166 static void *thread_manage_clients(void *data
)
2168 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2170 uint32_t revents
, nb_fd
;
2171 struct command_ctx
*cmd_ctx
= NULL
;
2172 struct lttng_poll_event events
;
2173 const int client_sock
= thread_state
.client_sock
;
2174 struct lttng_pipe
*quit_pipe
= data
;
2175 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2177 DBG("[thread] Manage client started");
2179 is_root
= (getuid() == 0);
2181 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2183 rcu_register_thread();
2185 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2187 health_code_update();
2189 ret
= lttcomm_listen_unix_sock(client_sock
);
2195 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2196 * more will be added to this poll set.
2198 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2200 goto error_create_poll
;
2203 /* Add the application registration socket */
2204 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2209 /* Add thread quit pipe */
2210 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2215 /* Set state as running. */
2216 set_thread_status(true);
2217 pthread_cleanup_pop(0);
2219 /* This testpoint is after we signal readiness to the parent. */
2220 if (testpoint(sessiond_thread_manage_clients
)) {
2224 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2228 health_code_update();
2231 const struct cmd_completion_handler
*cmd_completion_handler
;
2233 DBG("Accepting client command ...");
2235 /* Inifinite blocking call, waiting for transmission */
2237 health_poll_entry();
2238 ret
= lttng_poll_wait(&events
, -1);
2242 * Restart interrupted system call.
2244 if (errno
== EINTR
) {
2252 for (i
= 0; i
< nb_fd
; i
++) {
2253 revents
= LTTNG_POLL_GETEV(&events
, i
);
2254 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2256 health_code_update();
2258 if (pollfd
== thread_quit_pipe_fd
) {
2262 /* Event on the registration socket */
2263 if (revents
& LPOLLIN
) {
2265 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2266 ERR("Client socket poll error");
2269 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2275 DBG("Wait for client response");
2277 health_code_update();
2279 sock
= lttcomm_accept_unix_sock(client_sock
);
2285 * Set the CLOEXEC flag. Return code is useless because either way, the
2288 (void) utils_set_fd_cloexec(sock
);
2290 /* Set socket option for credentials retrieval */
2291 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2296 /* Allocate context command to process the client request */
2297 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2298 if (cmd_ctx
== NULL
) {
2299 PERROR("zmalloc cmd_ctx");
2303 /* Allocate data buffer for reception */
2304 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2305 if (cmd_ctx
->lsm
== NULL
) {
2306 PERROR("zmalloc cmd_ctx->lsm");
2310 cmd_ctx
->llm
= NULL
;
2311 cmd_ctx
->session
= NULL
;
2313 health_code_update();
2316 * Data is received from the lttng client. The struct
2317 * lttcomm_session_msg (lsm) contains the command and data request of
2320 DBG("Receiving data from client ...");
2321 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2322 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2324 DBG("Nothing recv() from client... continuing");
2330 clean_command_ctx(&cmd_ctx
);
2334 health_code_update();
2336 // TODO: Validate cmd_ctx including sanity check for
2337 // security purpose.
2339 rcu_thread_online();
2341 * This function dispatch the work to the kernel or userspace tracer
2342 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2343 * informations for the client. The command context struct contains
2344 * everything this function may needs.
2346 ret
= process_client_msg(cmd_ctx
, &sock
, &sock_error
);
2347 rcu_thread_offline();
2357 * TODO: Inform client somehow of the fatal error. At
2358 * this point, ret < 0 means that a zmalloc failed
2359 * (ENOMEM). Error detected but still accept
2360 * command, unless a socket error has been
2363 clean_command_ctx(&cmd_ctx
);
2367 cmd_completion_handler
= cmd_pop_completion_handler();
2368 if (cmd_completion_handler
) {
2369 enum lttng_error_code completion_code
;
2371 completion_code
= cmd_completion_handler
->run(
2372 cmd_completion_handler
->data
);
2373 if (completion_code
!= LTTNG_OK
) {
2374 clean_command_ctx(&cmd_ctx
);
2379 health_code_update();
2382 DBG("Sending response (size: %d, retcode: %s (%d))",
2383 cmd_ctx
->lttng_msg_size
,
2384 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2385 cmd_ctx
->llm
->ret_code
);
2386 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2387 cmd_ctx
->lttng_msg_size
);
2389 ERR("Failed to send data back to client");
2392 /* End of transmission */
2400 clean_command_ctx(&cmd_ctx
);
2402 health_code_update();
2414 lttng_poll_clean(&events
);
2415 clean_command_ctx(&cmd_ctx
);
2419 unlink(config
.client_unix_sock_path
.value
);
2420 ret
= close(client_sock
);
2427 ERR("Health error occurred in %s", __func__
);
2430 health_unregister(health_sessiond
);
2432 DBG("Client thread dying");
2434 rcu_unregister_thread();
2439 bool shutdown_client_thread(void *thread_data
)
2441 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2442 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2444 return notify_thread_pipe(write_fd
) == 1;
2447 struct lttng_thread
*launch_client_thread(void)
2449 bool thread_running
;
2450 struct lttng_pipe
*client_quit_pipe
;
2451 struct lttng_thread
*thread
= NULL
;
2452 int client_sock_fd
= -1;
2454 sem_init(&thread_state
.ready
, 0, 0);
2455 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2456 if (!client_quit_pipe
) {
2460 client_sock_fd
= create_client_sock();
2461 if (client_sock_fd
< 0) {
2465 thread_state
.client_sock
= client_sock_fd
;
2466 thread
= lttng_thread_create("Client management",
2467 thread_manage_clients
,
2468 shutdown_client_thread
,
2469 cleanup_client_thread
,
2474 /* The client thread now owns the client sock fd and the quit pipe. */
2475 client_sock_fd
= -1;
2476 client_quit_pipe
= NULL
;
2479 * This thread is part of the threads that need to be fully
2480 * initialized before the session daemon is marked as "ready".
2482 thread_running
= wait_thread_status();
2483 if (!thread_running
) {
2488 if (client_sock_fd
>= 0) {
2489 if (close(client_sock_fd
)) {
2490 PERROR("Failed to close client socket");
2493 lttng_thread_put(thread
);
2494 cleanup_client_thread(client_quit_pipe
);