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>
33 #include "lttng-sessiond.h"
37 #include "health-sessiond.h"
38 #include "testpoint.h"
40 #include "manage-consumer.h"
45 static struct thread_state
{
51 static void set_thread_status(bool running
)
53 DBG("Marking client thread's state as %s", running
? "running" : "error");
54 thread_state
.running
= running
;
55 sem_post(&thread_state
.ready
);
58 static bool wait_thread_status(void)
60 DBG("Waiting for client thread to be ready");
61 sem_wait(&thread_state
.ready
);
62 if (thread_state
.running
) {
63 DBG("Client thread is ready");
65 ERR("Initialization of client thread failed");
68 return thread_state
.running
;
72 * Setup the outgoing data buffer for the response (llm) by allocating the
73 * right amount of memory and copying the original information from the lsm
76 * Return 0 on success, negative value on error.
78 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
79 const void *payload_buf
, size_t payload_len
,
80 const void *cmd_header_buf
, size_t cmd_header_len
)
83 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
84 const size_t cmd_header_offset
= header_len
;
85 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
86 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
89 cmd_ctx
->llm
= zmalloc(total_msg_size
);
91 if (cmd_ctx
->llm
== NULL
) {
97 /* Copy common data */
98 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
99 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
100 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
101 cmd_ctx
->llm
->data_size
= payload_len
;
102 cmd_ctx
->lttng_msg_size
= total_msg_size
;
104 /* Copy command header */
105 if (cmd_header_len
) {
106 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
112 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
121 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
122 * exec or it will fail.
124 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
126 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
130 * Fork and exec a consumer daemon (consumerd).
132 * Return pid if successful else -1.
134 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
138 const char *consumer_to_use
;
139 const char *verbosity
;
142 DBG("Spawning consumerd");
149 if (config
.verbose_consumer
) {
150 verbosity
= "--verbose";
151 } else if (lttng_opt_quiet
) {
152 verbosity
= "--quiet";
157 switch (consumer_data
->type
) {
158 case LTTNG_CONSUMER_KERNEL
:
160 * Find out which consumerd to execute. We will first try the
161 * 64-bit path, then the sessiond's installation directory, and
162 * fallback on the 32-bit one,
164 DBG3("Looking for a kernel consumer at these locations:");
165 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
166 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
167 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
168 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
169 DBG3("Found location #1");
170 consumer_to_use
= config
.consumerd64_bin_path
.value
;
171 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
172 DBG3("Found location #2");
173 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
174 } else if (config
.consumerd32_bin_path
.value
&&
175 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
176 DBG3("Found location #3");
177 consumer_to_use
= config
.consumerd32_bin_path
.value
;
179 DBG("Could not find any valid consumerd executable");
183 DBG("Using kernel consumer at: %s", consumer_to_use
);
184 (void) execl(consumer_to_use
,
185 "lttng-consumerd", verbosity
, "-k",
186 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
187 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
188 "--group", config
.tracing_group_name
.value
,
191 case LTTNG_CONSUMER64_UST
:
193 if (config
.consumerd64_lib_dir
.value
) {
198 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
202 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
203 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
208 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
209 if (tmp
[0] != '\0') {
213 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
220 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
221 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
222 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
223 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
224 "--group", config
.tracing_group_name
.value
,
228 case LTTNG_CONSUMER32_UST
:
230 if (config
.consumerd32_lib_dir
.value
) {
235 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
239 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
240 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
245 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
246 if (tmp
[0] != '\0') {
250 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
257 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
258 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
259 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
260 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
261 "--group", config
.tracing_group_name
.value
,
266 ERR("unknown consumer type");
270 PERROR("Consumer execl()");
272 /* Reaching this point, we got a failure on our execl(). */
274 } else if (pid
> 0) {
277 PERROR("start consumer fork");
285 * Spawn the consumerd daemon and session daemon thread.
287 static int start_consumerd(struct consumer_data
*consumer_data
)
292 * Set the listen() state on the socket since there is a possible race
293 * between the exec() of the consumer daemon and this call if place in the
294 * consumer thread. See bug #366 for more details.
296 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
301 pthread_mutex_lock(&consumer_data
->pid_mutex
);
302 if (consumer_data
->pid
!= 0) {
303 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
307 ret
= spawn_consumerd(consumer_data
);
309 ERR("Spawning consumerd failed");
310 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
314 /* Setting up the consumer_data pid */
315 consumer_data
->pid
= ret
;
316 DBG2("Consumer pid %d", consumer_data
->pid
);
317 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
319 DBG2("Spawning consumer control thread");
320 ret
= spawn_consumer_thread(consumer_data
);
322 ERR("Fatal error spawning consumer control thread");
330 /* Cleanup already created sockets on error. */
331 if (consumer_data
->err_sock
>= 0) {
334 err
= close(consumer_data
->err_sock
);
336 PERROR("close consumer data error socket");
343 * Copy consumer output from the tracing session to the domain session. The
344 * function also applies the right modification on a per domain basis for the
345 * trace files destination directory.
347 * Should *NOT* be called with RCU read-side lock held.
349 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
352 const char *dir_name
;
353 struct consumer_output
*consumer
;
356 assert(session
->consumer
);
359 case LTTNG_DOMAIN_KERNEL
:
360 DBG3("Copying tracing session consumer output in kernel session");
362 * XXX: We should audit the session creation and what this function
363 * does "extra" in order to avoid a destroy since this function is used
364 * in the domain session creation (kernel and ust) only. Same for UST
367 if (session
->kernel_session
->consumer
) {
368 consumer_output_put(session
->kernel_session
->consumer
);
370 session
->kernel_session
->consumer
=
371 consumer_copy_output(session
->consumer
);
372 /* Ease our life a bit for the next part */
373 consumer
= session
->kernel_session
->consumer
;
374 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
376 case LTTNG_DOMAIN_JUL
:
377 case LTTNG_DOMAIN_LOG4J
:
378 case LTTNG_DOMAIN_PYTHON
:
379 case LTTNG_DOMAIN_UST
:
380 DBG3("Copying tracing session consumer output in UST session");
381 if (session
->ust_session
->consumer
) {
382 consumer_output_put(session
->ust_session
->consumer
);
384 session
->ust_session
->consumer
=
385 consumer_copy_output(session
->consumer
);
386 /* Ease our life a bit for the next part */
387 consumer
= session
->ust_session
->consumer
;
388 dir_name
= DEFAULT_UST_TRACE_DIR
;
391 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
395 /* Append correct directory to subdir */
396 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
397 sizeof(consumer
->domain_subdir
));
402 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
410 * Create an UST session and add it to the session ust list.
412 * Should *NOT* be called with RCU read-side lock held.
414 static int create_ust_session(struct ltt_session
*session
,
415 const struct lttng_domain
*domain
)
418 struct ltt_ust_session
*lus
= NULL
;
422 assert(session
->consumer
);
424 switch (domain
->type
) {
425 case LTTNG_DOMAIN_JUL
:
426 case LTTNG_DOMAIN_LOG4J
:
427 case LTTNG_DOMAIN_PYTHON
:
428 case LTTNG_DOMAIN_UST
:
431 ERR("Unknown UST domain on create session %d", domain
->type
);
432 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
436 DBG("Creating UST session");
438 lus
= trace_ust_create_session(session
->id
);
440 ret
= LTTNG_ERR_UST_SESS_FAIL
;
444 lus
->uid
= session
->uid
;
445 lus
->gid
= session
->gid
;
446 lus
->output_traces
= session
->output_traces
;
447 lus
->snapshot_mode
= session
->snapshot_mode
;
448 lus
->live_timer_interval
= session
->live_timer
;
449 session
->ust_session
= lus
;
450 if (session
->shm_path
[0]) {
451 strncpy(lus
->root_shm_path
, session
->shm_path
,
452 sizeof(lus
->root_shm_path
));
453 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
454 strncpy(lus
->shm_path
, session
->shm_path
,
455 sizeof(lus
->shm_path
));
456 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
457 strncat(lus
->shm_path
, "/ust",
458 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
460 /* Copy session output to the newly created UST session */
461 ret
= copy_session_consumer(domain
->type
, session
);
462 if (ret
!= LTTNG_OK
) {
470 session
->ust_session
= NULL
;
475 * Create a kernel tracer session then create the default channel.
477 static int create_kernel_session(struct ltt_session
*session
)
481 DBG("Creating kernel session");
483 ret
= kernel_create_session(session
);
485 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
489 /* Code flow safety */
490 assert(session
->kernel_session
);
492 /* Copy session output to the newly created Kernel session */
493 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
494 if (ret
!= LTTNG_OK
) {
498 session
->kernel_session
->uid
= session
->uid
;
499 session
->kernel_session
->gid
= session
->gid
;
500 session
->kernel_session
->output_traces
= session
->output_traces
;
501 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
506 trace_kernel_destroy_session(session
->kernel_session
);
507 session
->kernel_session
= NULL
;
513 * Count number of session permitted by uid/gid.
515 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
518 struct ltt_session
*session
;
519 const struct ltt_session_list
*session_list
= session_get_list();
521 DBG("Counting number of available session for UID %d GID %d",
523 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
524 if (!session_get(session
)) {
527 session_lock(session
);
528 /* Only count the sessions the user can control. */
529 if (session_access_ok(session
, uid
, gid
) &&
530 !session
->destroyed
) {
533 session_unlock(session
);
534 session_put(session
);
539 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
540 int *sock_error
, struct lttng_event
*event
)
543 struct lttng_userspace_probe_location
*probe_location
;
544 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
545 struct lttng_dynamic_buffer probe_location_buffer
;
546 struct lttng_buffer_view buffer_view
;
549 * Create a buffer to store the serialized version of the probe
552 lttng_dynamic_buffer_init(&probe_location_buffer
);
553 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
554 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
556 ret
= LTTNG_ERR_NOMEM
;
561 * Receive the probe location.
563 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
564 probe_location_buffer
.size
);
566 DBG("Nothing recv() from client var len data... continuing");
568 lttng_dynamic_buffer_reset(&probe_location_buffer
);
569 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
573 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
574 &probe_location_buffer
, 0, probe_location_buffer
.size
);
577 * Extract the probe location from the serialized version.
579 ret
= lttng_userspace_probe_location_create_from_buffer(
580 &buffer_view
, &probe_location
);
582 WARN("Failed to create a userspace probe location from the received buffer");
583 lttng_dynamic_buffer_reset( &probe_location_buffer
);
584 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
589 * Receive the file descriptor to the target binary from the client.
591 DBG("Receiving userspace probe target FD from client ...");
592 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
594 DBG("Nothing recv() from client userspace probe fd... continuing");
596 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
601 * Set the file descriptor received from the client through the unix
602 * socket in the probe location.
604 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
606 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
611 * From the kernel tracer's perspective, all userspace probe event types
612 * are all the same: a file and an offset.
614 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
615 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
616 ret
= lttng_userspace_probe_location_function_set_binary_fd(
619 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
620 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
624 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
629 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
633 /* Attach the probe location to the event. */
634 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
636 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
640 lttng_dynamic_buffer_reset(&probe_location_buffer
);
646 * Version of setup_lttng_msg() without command header.
648 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
649 void *payload_buf
, size_t payload_len
)
651 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
655 * Free memory of a command context structure.
657 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
659 DBG("Clean command context structure");
661 if ((*cmd_ctx
)->llm
) {
662 free((*cmd_ctx
)->llm
);
664 if ((*cmd_ctx
)->lsm
) {
665 free((*cmd_ctx
)->lsm
);
673 * Check if the current kernel tracer supports the session rotation feature.
674 * Return 1 if it does, 0 otherwise.
676 static int check_rotate_compatible(void)
680 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
681 DBG("Kernel tracer version is not compatible with the rotation feature");
689 * Send data on a unix socket using the liblttsessiondcomm API.
691 * Return lttcomm error code.
693 static int send_unix_sock(int sock
, void *buf
, size_t len
)
695 /* Check valid length */
700 return lttcomm_send_unix_sock(sock
, buf
, len
);
704 * Process the command requested by the lttng client within the command
705 * context structure. This function make sure that the return structure (llm)
706 * is set and ready for transmission before returning.
708 * Return any error encountered or 0 for success.
710 * "sock" is only used for special-case var. len data.
711 * A command may assume the ownership of the socket, in which case its value
712 * should be set to -1.
714 * Should *NOT* be called with RCU read-side lock held.
716 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
720 int need_tracing_session
= 1;
723 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
725 assert(!rcu_read_ongoing());
729 switch (cmd_ctx
->lsm
->cmd_type
) {
730 case LTTNG_CREATE_SESSION_EXT
:
731 case LTTNG_DESTROY_SESSION
:
732 case LTTNG_LIST_SESSIONS
:
733 case LTTNG_LIST_DOMAINS
:
734 case LTTNG_START_TRACE
:
735 case LTTNG_STOP_TRACE
:
736 case LTTNG_DATA_PENDING
:
737 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
738 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
739 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
740 case LTTNG_SNAPSHOT_RECORD
:
741 case LTTNG_SAVE_SESSION
:
742 case LTTNG_SET_SESSION_SHM_PATH
:
743 case LTTNG_REGENERATE_METADATA
:
744 case LTTNG_REGENERATE_STATEDUMP
:
745 case LTTNG_REGISTER_TRIGGER
:
746 case LTTNG_UNREGISTER_TRIGGER
:
747 case LTTNG_ROTATE_SESSION
:
748 case LTTNG_ROTATION_GET_INFO
:
749 case LTTNG_ROTATION_SET_SCHEDULE
:
750 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
751 case LTTNG_CLEAR_SESSION
:
758 if (config
.no_kernel
&& need_domain
759 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
761 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
763 ret
= LTTNG_ERR_KERN_NA
;
768 /* Deny register consumer if we already have a spawned consumer. */
769 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
770 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
771 if (kconsumer_data
.pid
> 0) {
772 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
773 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
776 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
780 * Check for command that don't needs to allocate a returned payload. We do
781 * this here so we don't have to make the call for no payload at each
784 switch(cmd_ctx
->lsm
->cmd_type
) {
785 case LTTNG_LIST_SESSIONS
:
786 case LTTNG_LIST_TRACEPOINTS
:
787 case LTTNG_LIST_TRACEPOINT_FIELDS
:
788 case LTTNG_LIST_DOMAINS
:
789 case LTTNG_LIST_CHANNELS
:
790 case LTTNG_LIST_EVENTS
:
791 case LTTNG_LIST_SYSCALLS
:
792 case LTTNG_LIST_TRACKER_PIDS
:
793 case LTTNG_DATA_PENDING
:
794 case LTTNG_ROTATE_SESSION
:
795 case LTTNG_ROTATION_GET_INFO
:
796 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
799 /* Setup lttng message with no payload */
800 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
802 /* This label does not try to unlock the session */
803 goto init_setup_error
;
807 /* Commands that DO NOT need a session. */
808 switch (cmd_ctx
->lsm
->cmd_type
) {
809 case LTTNG_CREATE_SESSION_EXT
:
810 case LTTNG_LIST_SESSIONS
:
811 case LTTNG_LIST_TRACEPOINTS
:
812 case LTTNG_LIST_SYSCALLS
:
813 case LTTNG_LIST_TRACEPOINT_FIELDS
:
814 case LTTNG_SAVE_SESSION
:
815 case LTTNG_REGISTER_TRIGGER
:
816 case LTTNG_UNREGISTER_TRIGGER
:
817 need_tracing_session
= 0;
820 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
822 * We keep the session list lock across _all_ commands
823 * for now, because the per-session lock does not
824 * handle teardown properly.
827 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
828 if (cmd_ctx
->session
== NULL
) {
829 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
832 /* Acquire lock for the session */
833 session_lock(cmd_ctx
->session
);
839 * Commands that need a valid session but should NOT create one if none
840 * exists. Instead of creating one and destroying it when the command is
841 * handled, process that right before so we save some round trip in useless
844 switch (cmd_ctx
->lsm
->cmd_type
) {
845 case LTTNG_DISABLE_CHANNEL
:
846 case LTTNG_DISABLE_EVENT
:
847 switch (cmd_ctx
->lsm
->domain
.type
) {
848 case LTTNG_DOMAIN_KERNEL
:
849 if (!cmd_ctx
->session
->kernel_session
) {
850 ret
= LTTNG_ERR_NO_CHANNEL
;
854 case LTTNG_DOMAIN_JUL
:
855 case LTTNG_DOMAIN_LOG4J
:
856 case LTTNG_DOMAIN_PYTHON
:
857 case LTTNG_DOMAIN_UST
:
858 if (!cmd_ctx
->session
->ust_session
) {
859 ret
= LTTNG_ERR_NO_CHANNEL
;
864 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
876 * Check domain type for specific "pre-action".
878 switch (cmd_ctx
->lsm
->domain
.type
) {
879 case LTTNG_DOMAIN_KERNEL
:
881 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
885 /* Kernel tracer check */
886 if (!kernel_tracer_is_initialized()) {
887 /* Basically, load kernel tracer modules */
888 ret
= init_kernel_tracer();
894 /* Consumer is in an ERROR state. Report back to client */
895 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
896 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
900 /* Need a session for kernel command */
901 if (need_tracing_session
) {
902 if (cmd_ctx
->session
->kernel_session
== NULL
) {
903 ret
= create_kernel_session(cmd_ctx
->session
);
904 if (ret
!= LTTNG_OK
) {
905 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
910 /* Start the kernel consumer daemon */
911 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
912 if (kconsumer_data
.pid
== 0 &&
913 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
914 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
915 ret
= start_consumerd(&kconsumer_data
);
917 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
920 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
922 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
926 * The consumer was just spawned so we need to add the socket to
927 * the consumer output of the session if exist.
929 ret
= consumer_create_socket(&kconsumer_data
,
930 cmd_ctx
->session
->kernel_session
->consumer
);
937 case LTTNG_DOMAIN_JUL
:
938 case LTTNG_DOMAIN_LOG4J
:
939 case LTTNG_DOMAIN_PYTHON
:
940 case LTTNG_DOMAIN_UST
:
942 if (!ust_app_supported()) {
943 ret
= LTTNG_ERR_NO_UST
;
946 /* Consumer is in an ERROR state. Report back to client */
947 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
948 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
952 if (need_tracing_session
) {
953 /* Create UST session if none exist. */
954 if (cmd_ctx
->session
->ust_session
== NULL
) {
955 ret
= create_ust_session(cmd_ctx
->session
,
956 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
));
957 if (ret
!= LTTNG_OK
) {
962 /* Start the UST consumer daemons */
964 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
965 if (config
.consumerd64_bin_path
.value
&&
966 ustconsumer64_data
.pid
== 0 &&
967 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
968 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
969 ret
= start_consumerd(&ustconsumer64_data
);
971 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
972 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
976 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
977 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
979 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
983 * Setup socket for consumer 64 bit. No need for atomic access
984 * since it was set above and can ONLY be set in this thread.
986 ret
= consumer_create_socket(&ustconsumer64_data
,
987 cmd_ctx
->session
->ust_session
->consumer
);
993 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
994 if (config
.consumerd32_bin_path
.value
&&
995 ustconsumer32_data
.pid
== 0 &&
996 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
997 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
998 ret
= start_consumerd(&ustconsumer32_data
);
1000 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1001 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
1005 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
1006 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1008 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1012 * Setup socket for consumer 32 bit. No need for atomic access
1013 * since it was set above and can ONLY be set in this thread.
1015 ret
= consumer_create_socket(&ustconsumer32_data
,
1016 cmd_ctx
->session
->ust_session
->consumer
);
1028 /* Validate consumer daemon state when start/stop trace command */
1029 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1030 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1031 switch (cmd_ctx
->lsm
->domain
.type
) {
1032 case LTTNG_DOMAIN_NONE
:
1034 case LTTNG_DOMAIN_JUL
:
1035 case LTTNG_DOMAIN_LOG4J
:
1036 case LTTNG_DOMAIN_PYTHON
:
1037 case LTTNG_DOMAIN_UST
:
1038 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1039 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1043 case LTTNG_DOMAIN_KERNEL
:
1044 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1045 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1050 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1056 * Check that the UID or GID match that of the tracing session.
1057 * The root user can interact with all sessions.
1059 if (need_tracing_session
) {
1060 if (!session_access_ok(cmd_ctx
->session
,
1061 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1062 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1063 cmd_ctx
->session
->destroyed
) {
1064 ret
= LTTNG_ERR_EPERM
;
1070 * Send relayd information to consumer as soon as we have a domain and a
1073 if (cmd_ctx
->session
&& need_domain
) {
1075 * Setup relayd if not done yet. If the relayd information was already
1076 * sent to the consumer, this call will gracefully return.
1078 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1079 if (ret
!= LTTNG_OK
) {
1084 /* Process by command type */
1085 switch (cmd_ctx
->lsm
->cmd_type
) {
1086 case LTTNG_ADD_CONTEXT
:
1089 * An LTTNG_ADD_CONTEXT command might have a supplementary
1090 * payload if the context being added is an application context.
1092 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1093 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1094 char *provider_name
= NULL
, *context_name
= NULL
;
1095 size_t provider_name_len
=
1096 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1097 size_t context_name_len
=
1098 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1100 if (provider_name_len
== 0 || context_name_len
== 0) {
1102 * Application provider and context names MUST
1105 ret
= -LTTNG_ERR_INVALID
;
1109 provider_name
= zmalloc(provider_name_len
+ 1);
1110 if (!provider_name
) {
1111 ret
= -LTTNG_ERR_NOMEM
;
1114 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1117 context_name
= zmalloc(context_name_len
+ 1);
1118 if (!context_name
) {
1119 ret
= -LTTNG_ERR_NOMEM
;
1120 goto error_add_context
;
1122 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1125 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1128 goto error_add_context
;
1131 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1134 goto error_add_context
;
1139 * cmd_add_context assumes ownership of the provider and context
1142 ret
= cmd_add_context(cmd_ctx
->session
,
1143 cmd_ctx
->lsm
->domain
.type
,
1144 cmd_ctx
->lsm
->u
.context
.channel_name
,
1145 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.context
.ctx
),
1146 kernel_poll_pipe
[1]);
1148 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1149 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1151 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1152 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1158 case LTTNG_DISABLE_CHANNEL
:
1160 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1161 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1164 case LTTNG_DISABLE_EVENT
:
1168 * FIXME: handle filter; for now we just receive the filter's
1169 * bytecode along with the filter expression which are sent by
1170 * liblttng-ctl and discard them.
1172 * This fixes an issue where the client may block while sending
1173 * the filter payload and encounter an error because the session
1174 * daemon closes the socket without ever handling this data.
1176 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1177 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1180 char data
[LTTNG_FILTER_MAX_LEN
];
1182 DBG("Discarding disable event command payload of size %zu", count
);
1184 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1185 count
> sizeof(data
) ? sizeof(data
) : count
);
1190 count
-= (size_t) ret
;
1193 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1194 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1195 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.disable
.event
));
1198 case LTTNG_ENABLE_CHANNEL
:
1200 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1201 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1202 ret
= cmd_enable_channel(cmd_ctx
->session
,
1203 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1204 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.channel
.chan
),
1205 kernel_poll_pipe
[1]);
1208 case LTTNG_TRACK_PID
:
1210 ret
= cmd_track_pid(cmd_ctx
->session
,
1211 cmd_ctx
->lsm
->domain
.type
,
1212 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1215 case LTTNG_UNTRACK_PID
:
1217 ret
= cmd_untrack_pid(cmd_ctx
->session
,
1218 cmd_ctx
->lsm
->domain
.type
,
1219 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1222 case LTTNG_ENABLE_EVENT
:
1224 struct lttng_event
*ev
= NULL
;
1225 struct lttng_event_exclusion
*exclusion
= NULL
;
1226 struct lttng_filter_bytecode
*bytecode
= NULL
;
1227 char *filter_expression
= NULL
;
1229 /* Handle exclusion events and receive it from the client. */
1230 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1231 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1233 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1234 (count
* LTTNG_SYMBOL_NAME_LEN
));
1236 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1240 DBG("Receiving var len exclusion event list from client ...");
1241 exclusion
->count
= count
;
1242 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1243 count
* LTTNG_SYMBOL_NAME_LEN
);
1245 DBG("Nothing recv() from client var len data... continuing");
1248 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1253 /* Get filter expression from client. */
1254 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1255 size_t expression_len
=
1256 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1258 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1259 ret
= LTTNG_ERR_FILTER_INVAL
;
1264 filter_expression
= zmalloc(expression_len
);
1265 if (!filter_expression
) {
1267 ret
= LTTNG_ERR_FILTER_NOMEM
;
1271 /* Receive var. len. data */
1272 DBG("Receiving var len filter's expression from client ...");
1273 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1276 DBG("Nothing recv() from client var len data... continuing");
1278 free(filter_expression
);
1280 ret
= LTTNG_ERR_FILTER_INVAL
;
1285 /* Handle filter and get bytecode from client. */
1286 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1287 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1289 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1290 ret
= LTTNG_ERR_FILTER_INVAL
;
1291 free(filter_expression
);
1296 bytecode
= zmalloc(bytecode_len
);
1298 free(filter_expression
);
1300 ret
= LTTNG_ERR_FILTER_NOMEM
;
1304 /* Receive var. len. data */
1305 DBG("Receiving var len filter's bytecode from client ...");
1306 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1308 DBG("Nothing recv() from client var len data... continuing");
1310 free(filter_expression
);
1313 ret
= LTTNG_ERR_FILTER_INVAL
;
1317 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1318 free(filter_expression
);
1321 ret
= LTTNG_ERR_FILTER_INVAL
;
1326 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.enable
.event
));
1328 DBG("Failed to copy event: %s",
1329 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1330 free(filter_expression
);
1333 ret
= LTTNG_ERR_NOMEM
;
1338 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1339 /* Expect a userspace probe description. */
1340 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1342 free(filter_expression
);
1345 lttng_event_destroy(ev
);
1350 ret
= cmd_enable_event(cmd_ctx
->session
,
1351 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1352 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1354 filter_expression
, bytecode
, exclusion
,
1355 kernel_poll_pipe
[1]);
1356 lttng_event_destroy(ev
);
1359 case LTTNG_LIST_TRACEPOINTS
:
1361 struct lttng_event
*events
;
1364 session_lock_list();
1365 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1366 session_unlock_list();
1367 if (nb_events
< 0) {
1368 /* Return value is a negative lttng_error_code. */
1374 * Setup lttng message with payload size set to the event list size in
1375 * bytes and then copy list into the llm payload.
1377 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1378 sizeof(struct lttng_event
) * nb_events
);
1388 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1390 struct lttng_event_field
*fields
;
1393 session_lock_list();
1394 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1396 session_unlock_list();
1397 if (nb_fields
< 0) {
1398 /* Return value is a negative lttng_error_code. */
1404 * Setup lttng message with payload size set to the event list size in
1405 * bytes and then copy list into the llm payload.
1407 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1408 sizeof(struct lttng_event_field
) * nb_fields
);
1418 case LTTNG_LIST_SYSCALLS
:
1420 struct lttng_event
*events
;
1423 nb_events
= cmd_list_syscalls(&events
);
1424 if (nb_events
< 0) {
1425 /* Return value is a negative lttng_error_code. */
1431 * Setup lttng message with payload size set to the event list size in
1432 * bytes and then copy list into the llm payload.
1434 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1435 sizeof(struct lttng_event
) * nb_events
);
1445 case LTTNG_LIST_TRACKER_PIDS
:
1447 int32_t *pids
= NULL
;
1450 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
1451 cmd_ctx
->lsm
->domain
.type
, &pids
);
1453 /* Return value is a negative lttng_error_code. */
1459 * Setup lttng message with payload size set to the event list size in
1460 * bytes and then copy list into the llm payload.
1462 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
1463 sizeof(int32_t) * nr_pids
);
1473 case LTTNG_SET_CONSUMER_URI
:
1476 struct lttng_uri
*uris
;
1478 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1479 len
= nb_uri
* sizeof(struct lttng_uri
);
1482 ret
= LTTNG_ERR_INVALID
;
1486 uris
= zmalloc(len
);
1488 ret
= LTTNG_ERR_FATAL
;
1492 /* Receive variable len data */
1493 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1494 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1496 DBG("No URIs received from client... continuing");
1498 ret
= LTTNG_ERR_SESSION_FAIL
;
1503 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1505 if (ret
!= LTTNG_OK
) {
1512 case LTTNG_START_TRACE
:
1515 * On the first start, if we have a kernel session and we have
1516 * enabled time or size-based rotations, we have to make sure
1517 * the kernel tracer supports it.
1519 if (!cmd_ctx
->session
->has_been_started
&& \
1520 cmd_ctx
->session
->kernel_session
&& \
1521 (cmd_ctx
->session
->rotate_timer_period
|| \
1522 cmd_ctx
->session
->rotate_size
) && \
1523 !check_rotate_compatible()) {
1524 DBG("Kernel tracer version is not compatible with the rotation feature");
1525 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1528 ret
= cmd_start_trace(cmd_ctx
->session
);
1531 case LTTNG_STOP_TRACE
:
1533 ret
= cmd_stop_trace(cmd_ctx
->session
);
1536 case LTTNG_DESTROY_SESSION
:
1538 ret
= cmd_destroy_session(cmd_ctx
->session
,
1539 notification_thread_handle
,
1543 case LTTNG_LIST_DOMAINS
:
1546 struct lttng_domain
*domains
= NULL
;
1548 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1550 /* Return value is a negative lttng_error_code. */
1555 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1556 nb_dom
* sizeof(struct lttng_domain
));
1566 case LTTNG_LIST_CHANNELS
:
1568 ssize_t payload_size
;
1569 struct lttng_channel
*channels
= NULL
;
1571 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1572 cmd_ctx
->session
, &channels
);
1573 if (payload_size
< 0) {
1574 /* Return value is a negative lttng_error_code. */
1575 ret
= -payload_size
;
1579 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1590 case LTTNG_LIST_EVENTS
:
1593 struct lttng_event
*events
= NULL
;
1594 struct lttcomm_event_command_header cmd_header
;
1597 memset(&cmd_header
, 0, sizeof(cmd_header
));
1598 /* Extended infos are included at the end of events */
1599 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1600 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1601 &events
, &total_size
);
1604 /* Return value is a negative lttng_error_code. */
1609 cmd_header
.nb_events
= nb_event
;
1610 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1611 &cmd_header
, sizeof(cmd_header
));
1621 case LTTNG_LIST_SESSIONS
:
1623 unsigned int nr_sessions
;
1624 void *sessions_payload
;
1627 session_lock_list();
1628 nr_sessions
= lttng_sessions_count(
1629 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1630 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1632 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1633 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1634 sessions_payload
= zmalloc(payload_len
);
1636 if (!sessions_payload
) {
1637 session_unlock_list();
1642 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1643 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1644 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1645 session_unlock_list();
1647 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1649 free(sessions_payload
);
1658 case LTTNG_REGISTER_CONSUMER
:
1660 struct consumer_data
*cdata
;
1662 switch (cmd_ctx
->lsm
->domain
.type
) {
1663 case LTTNG_DOMAIN_KERNEL
:
1664 cdata
= &kconsumer_data
;
1667 ret
= LTTNG_ERR_UND
;
1671 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1672 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1675 case LTTNG_DATA_PENDING
:
1678 uint8_t pending_ret_byte
;
1680 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1685 * This function may returns 0 or 1 to indicate whether or not
1686 * there is data pending. In case of error, it should return an
1687 * LTTNG_ERR code. However, some code paths may still return
1688 * a nondescript error code, which we handle by returning an
1691 if (pending_ret
== 0 || pending_ret
== 1) {
1693 * ret will be set to LTTNG_OK at the end of
1696 } else if (pending_ret
< 0) {
1697 ret
= LTTNG_ERR_UNK
;
1704 pending_ret_byte
= (uint8_t) pending_ret
;
1706 /* 1 byte to return whether or not data is pending */
1707 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1708 &pending_ret_byte
, 1);
1717 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1719 uint32_t snapshot_id
;
1720 struct lttcomm_lttng_output_id reply
;
1722 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1723 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
),
1725 if (ret
!= LTTNG_OK
) {
1728 reply
.id
= snapshot_id
;
1730 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1736 /* Copy output list into message payload */
1740 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1742 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1743 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
));
1746 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1749 struct lttng_snapshot_output
*outputs
= NULL
;
1751 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1752 if (nb_output
< 0) {
1757 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1758 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1759 nb_output
* sizeof(struct lttng_snapshot_output
));
1769 case LTTNG_SNAPSHOT_RECORD
:
1771 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1772 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_record
.output
),
1773 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1776 case LTTNG_CREATE_SESSION_EXT
:
1778 struct lttng_dynamic_buffer payload
;
1779 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1781 lttng_dynamic_buffer_init(&payload
);
1782 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1783 if (ret
!= LTTNG_OK
) {
1787 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1790 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1791 lttng_session_descriptor_destroy(return_descriptor
);
1792 ret
= LTTNG_ERR_NOMEM
;
1795 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1798 lttng_session_descriptor_destroy(return_descriptor
);
1799 ret
= LTTNG_ERR_NOMEM
;
1802 lttng_dynamic_buffer_reset(&payload
);
1803 lttng_session_descriptor_destroy(return_descriptor
);
1807 case LTTNG_SAVE_SESSION
:
1809 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1813 case LTTNG_SET_SESSION_SHM_PATH
:
1815 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1816 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1819 case LTTNG_REGENERATE_METADATA
:
1821 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1824 case LTTNG_REGENERATE_STATEDUMP
:
1826 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1829 case LTTNG_REGISTER_TRIGGER
:
1831 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
1832 notification_thread_handle
);
1835 case LTTNG_UNREGISTER_TRIGGER
:
1837 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
1838 notification_thread_handle
);
1841 case LTTNG_ROTATE_SESSION
:
1843 struct lttng_rotate_session_return rotate_return
;
1845 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1847 memset(&rotate_return
, 0, sizeof(rotate_return
));
1848 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1849 DBG("Kernel tracer version is not compatible with the rotation feature");
1850 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1854 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
1856 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
1862 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
1863 sizeof(rotate_return
));
1872 case LTTNG_ROTATION_GET_INFO
:
1874 struct lttng_rotation_get_info_return get_info_return
;
1876 memset(&get_info_return
, 0, sizeof(get_info_return
));
1877 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
1878 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
1884 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
1885 sizeof(get_info_return
));
1894 case LTTNG_ROTATION_SET_SCHEDULE
:
1897 enum lttng_rotation_schedule_type schedule_type
;
1900 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1901 DBG("Kernel tracer version does not support session rotations");
1902 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1906 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
1907 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
1908 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
1910 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
1914 notification_thread_handle
);
1915 if (ret
!= LTTNG_OK
) {
1921 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1923 struct lttng_session_list_schedules_return schedules
= {
1924 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
1925 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
1926 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
1927 .size
.value
= cmd_ctx
->session
->rotate_size
,
1930 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
1940 case LTTNG_CLEAR_SESSION
:
1942 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
1946 ret
= LTTNG_ERR_UND
;
1951 if (cmd_ctx
->llm
== NULL
) {
1952 DBG("Missing llm structure. Allocating one.");
1953 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
1957 /* Set return code */
1958 cmd_ctx
->llm
->ret_code
= ret
;
1960 if (cmd_ctx
->session
) {
1961 session_unlock(cmd_ctx
->session
);
1962 session_put(cmd_ctx
->session
);
1963 cmd_ctx
->session
= NULL
;
1965 if (need_tracing_session
) {
1966 session_unlock_list();
1969 assert(!rcu_read_ongoing());
1973 static int create_client_sock(void)
1975 int ret
, client_sock
;
1976 const mode_t old_umask
= umask(0);
1978 /* Create client tool unix socket */
1979 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
1980 if (client_sock
< 0) {
1981 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
1986 /* Set the cloexec flag */
1987 ret
= utils_set_fd_cloexec(client_sock
);
1989 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
1990 "Continuing but note that the consumer daemon will have a "
1991 "reference to this socket on exec()", client_sock
);
1994 /* File permission MUST be 660 */
1995 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1997 ERR("Set file permissions failed: %s",
1998 config
.client_unix_sock_path
.value
);
2000 (void) lttcomm_close_unix_sock(client_sock
);
2004 DBG("Created client socket (fd = %i)", client_sock
);
2011 static void cleanup_client_thread(void *data
)
2013 struct lttng_pipe
*quit_pipe
= data
;
2015 lttng_pipe_destroy(quit_pipe
);
2018 static void thread_init_cleanup(void *data
)
2020 set_thread_status(false);
2024 * This thread manage all clients request using the unix client socket for
2027 static void *thread_manage_clients(void *data
)
2029 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2031 uint32_t revents
, nb_fd
;
2032 struct command_ctx
*cmd_ctx
= NULL
;
2033 struct lttng_poll_event events
;
2034 const int client_sock
= thread_state
.client_sock
;
2035 struct lttng_pipe
*quit_pipe
= data
;
2036 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2038 DBG("[thread] Manage client started");
2040 is_root
= (getuid() == 0);
2042 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2044 rcu_register_thread();
2046 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2048 health_code_update();
2050 ret
= lttcomm_listen_unix_sock(client_sock
);
2056 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2057 * more will be added to this poll set.
2059 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2061 goto error_create_poll
;
2064 /* Add the application registration socket */
2065 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2070 /* Add thread quit pipe */
2071 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2076 /* Set state as running. */
2077 set_thread_status(true);
2078 pthread_cleanup_pop(0);
2080 /* This testpoint is after we signal readiness to the parent. */
2081 if (testpoint(sessiond_thread_manage_clients
)) {
2085 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2089 health_code_update();
2092 const struct cmd_completion_handler
*cmd_completion_handler
;
2094 DBG("Accepting client command ...");
2096 /* Inifinite blocking call, waiting for transmission */
2098 health_poll_entry();
2099 ret
= lttng_poll_wait(&events
, -1);
2103 * Restart interrupted system call.
2105 if (errno
== EINTR
) {
2113 for (i
= 0; i
< nb_fd
; i
++) {
2114 revents
= LTTNG_POLL_GETEV(&events
, i
);
2115 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2117 health_code_update();
2119 if (pollfd
== thread_quit_pipe_fd
) {
2123 /* Event on the registration socket */
2124 if (revents
& LPOLLIN
) {
2126 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2127 ERR("Client socket poll error");
2130 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2136 DBG("Wait for client response");
2138 health_code_update();
2140 sock
= lttcomm_accept_unix_sock(client_sock
);
2146 * Set the CLOEXEC flag. Return code is useless because either way, the
2149 (void) utils_set_fd_cloexec(sock
);
2151 /* Set socket option for credentials retrieval */
2152 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2157 /* Allocate context command to process the client request */
2158 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2159 if (cmd_ctx
== NULL
) {
2160 PERROR("zmalloc cmd_ctx");
2164 /* Allocate data buffer for reception */
2165 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2166 if (cmd_ctx
->lsm
== NULL
) {
2167 PERROR("zmalloc cmd_ctx->lsm");
2171 cmd_ctx
->llm
= NULL
;
2172 cmd_ctx
->session
= NULL
;
2174 health_code_update();
2177 * Data is received from the lttng client. The struct
2178 * lttcomm_session_msg (lsm) contains the command and data request of
2181 DBG("Receiving data from client ...");
2182 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2183 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2185 DBG("Nothing recv() from client... continuing");
2191 clean_command_ctx(&cmd_ctx
);
2195 health_code_update();
2197 // TODO: Validate cmd_ctx including sanity check for
2198 // security purpose.
2200 rcu_thread_online();
2202 * This function dispatch the work to the kernel or userspace tracer
2203 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2204 * informations for the client. The command context struct contains
2205 * everything this function may needs.
2207 ret
= process_client_msg(cmd_ctx
, &sock
, &sock_error
);
2208 rcu_thread_offline();
2218 * TODO: Inform client somehow of the fatal error. At
2219 * this point, ret < 0 means that a zmalloc failed
2220 * (ENOMEM). Error detected but still accept
2221 * command, unless a socket error has been
2224 clean_command_ctx(&cmd_ctx
);
2228 cmd_completion_handler
= cmd_pop_completion_handler();
2229 if (cmd_completion_handler
) {
2230 enum lttng_error_code completion_code
;
2232 completion_code
= cmd_completion_handler
->run(
2233 cmd_completion_handler
->data
);
2234 if (completion_code
!= LTTNG_OK
) {
2235 clean_command_ctx(&cmd_ctx
);
2240 health_code_update();
2243 DBG("Sending response (size: %d, retcode: %s (%d))",
2244 cmd_ctx
->lttng_msg_size
,
2245 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2246 cmd_ctx
->llm
->ret_code
);
2247 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2248 cmd_ctx
->lttng_msg_size
);
2250 ERR("Failed to send data back to client");
2253 /* End of transmission */
2261 clean_command_ctx(&cmd_ctx
);
2263 health_code_update();
2275 lttng_poll_clean(&events
);
2276 clean_command_ctx(&cmd_ctx
);
2280 unlink(config
.client_unix_sock_path
.value
);
2281 ret
= close(client_sock
);
2288 ERR("Health error occurred in %s", __func__
);
2291 health_unregister(health_sessiond
);
2293 DBG("Client thread dying");
2295 rcu_unregister_thread();
2300 bool shutdown_client_thread(void *thread_data
)
2302 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2303 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2305 return notify_thread_pipe(write_fd
) == 1;
2308 struct lttng_thread
*launch_client_thread(void)
2310 bool thread_running
;
2311 struct lttng_pipe
*client_quit_pipe
;
2312 struct lttng_thread
*thread
= NULL
;
2313 int client_sock_fd
= -1;
2315 sem_init(&thread_state
.ready
, 0, 0);
2316 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2317 if (!client_quit_pipe
) {
2321 client_sock_fd
= create_client_sock();
2322 if (client_sock_fd
< 0) {
2326 thread_state
.client_sock
= client_sock_fd
;
2327 thread
= lttng_thread_create("Client management",
2328 thread_manage_clients
,
2329 shutdown_client_thread
,
2330 cleanup_client_thread
,
2335 /* The client thread now owns the client sock fd and the quit pipe. */
2336 client_sock_fd
= -1;
2337 client_quit_pipe
= NULL
;
2340 * This thread is part of the threads that need to be fully
2341 * initialized before the session daemon is marked as "ready".
2343 thread_running
= wait_thread_status();
2344 if (!thread_running
) {
2349 if (client_sock_fd
>= 0) {
2350 if (close(client_sock_fd
)) {
2351 PERROR("Failed to close client socket");
2354 lttng_thread_put(thread
);
2355 cleanup_client_thread(client_quit_pipe
);