2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <common/compat/getenv.h>
25 #include <common/unix.h>
26 #include <common/utils.h>
27 #include <lttng/userspace-probe-internal.h>
28 #include <lttng/event-internal.h>
31 #include "lttng-sessiond.h"
35 #include "health-sessiond.h"
36 #include "testpoint.h"
38 #include "manage-consumer.h"
42 static struct thread_state
{
47 static void set_thread_status(bool running
)
49 DBG("Marking client thread's state as %s", running
? "running" : "error");
50 thread_state
.running
= running
;
51 sem_post(&thread_state
.ready
);
54 static bool wait_thread_status(void)
56 DBG("Waiting for client thread to be ready");
57 sem_wait(&thread_state
.ready
);
58 if (thread_state
.running
) {
59 DBG("Client thread is ready");
61 ERR("Initialization of client thread failed");
64 return thread_state
.running
;
68 * Setup the outgoing data buffer for the response (llm) by allocating the
69 * right amount of memory and copying the original information from the lsm
72 * Return 0 on success, negative value on error.
74 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
75 const void *payload_buf
, size_t payload_len
,
76 const void *cmd_header_buf
, size_t cmd_header_len
)
79 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
80 const size_t cmd_header_offset
= header_len
;
81 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
82 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
85 cmd_ctx
->llm
= zmalloc(total_msg_size
);
87 if (cmd_ctx
->llm
== NULL
) {
93 /* Copy common data */
94 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
95 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
96 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
97 cmd_ctx
->llm
->data_size
= payload_len
;
98 cmd_ctx
->lttng_msg_size
= total_msg_size
;
100 /* Copy command header */
101 if (cmd_header_len
) {
102 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
108 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
117 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
118 * exec or it will fail.
120 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
122 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
126 * Fork and exec a consumer daemon (consumerd).
128 * Return pid if successful else -1.
130 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
134 const char *consumer_to_use
;
135 const char *verbosity
;
138 DBG("Spawning consumerd");
145 if (config
.verbose_consumer
) {
146 verbosity
= "--verbose";
147 } else if (lttng_opt_quiet
) {
148 verbosity
= "--quiet";
153 switch (consumer_data
->type
) {
154 case LTTNG_CONSUMER_KERNEL
:
156 * Find out which consumerd to execute. We will first try the
157 * 64-bit path, then the sessiond's installation directory, and
158 * fallback on the 32-bit one,
160 DBG3("Looking for a kernel consumer at these locations:");
161 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
162 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
163 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
164 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
165 DBG3("Found location #1");
166 consumer_to_use
= config
.consumerd64_bin_path
.value
;
167 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
168 DBG3("Found location #2");
169 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
170 } else if (config
.consumerd32_bin_path
.value
&&
171 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
172 DBG3("Found location #3");
173 consumer_to_use
= config
.consumerd32_bin_path
.value
;
175 DBG("Could not find any valid consumerd executable");
179 DBG("Using kernel consumer at: %s", consumer_to_use
);
180 (void) execl(consumer_to_use
,
181 "lttng-consumerd", verbosity
, "-k",
182 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
183 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
184 "--group", config
.tracing_group_name
.value
,
187 case LTTNG_CONSUMER64_UST
:
189 if (config
.consumerd64_lib_dir
.value
) {
194 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
198 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
199 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
204 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
205 if (tmp
[0] != '\0') {
209 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
216 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
217 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
218 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
219 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
220 "--group", config
.tracing_group_name
.value
,
224 case LTTNG_CONSUMER32_UST
:
226 if (config
.consumerd32_lib_dir
.value
) {
231 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
235 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
236 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
241 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
242 if (tmp
[0] != '\0') {
246 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
253 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
254 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
255 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
256 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
257 "--group", config
.tracing_group_name
.value
,
262 ERR("unknown consumer type");
266 PERROR("Consumer execl()");
268 /* Reaching this point, we got a failure on our execl(). */
270 } else if (pid
> 0) {
273 PERROR("start consumer fork");
281 * Spawn the consumerd daemon and session daemon thread.
283 static int start_consumerd(struct consumer_data
*consumer_data
)
288 * Set the listen() state on the socket since there is a possible race
289 * between the exec() of the consumer daemon and this call if place in the
290 * consumer thread. See bug #366 for more details.
292 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
297 pthread_mutex_lock(&consumer_data
->pid_mutex
);
298 if (consumer_data
->pid
!= 0) {
299 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
303 ret
= spawn_consumerd(consumer_data
);
305 ERR("Spawning consumerd failed");
306 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
310 /* Setting up the consumer_data pid */
311 consumer_data
->pid
= ret
;
312 DBG2("Consumer pid %d", consumer_data
->pid
);
313 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
315 DBG2("Spawning consumer control thread");
316 ret
= spawn_consumer_thread(consumer_data
);
318 ERR("Fatal error spawning consumer control thread");
326 /* Cleanup already created sockets on error. */
327 if (consumer_data
->err_sock
>= 0) {
330 err
= close(consumer_data
->err_sock
);
332 PERROR("close consumer data error socket");
339 * Copy consumer output from the tracing session to the domain session. The
340 * function also applies the right modification on a per domain basis for the
341 * trace files destination directory.
343 * Should *NOT* be called with RCU read-side lock held.
345 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
348 const char *dir_name
;
349 struct consumer_output
*consumer
;
352 assert(session
->consumer
);
355 case LTTNG_DOMAIN_KERNEL
:
356 DBG3("Copying tracing session consumer output in kernel session");
358 * XXX: We should audit the session creation and what this function
359 * does "extra" in order to avoid a destroy since this function is used
360 * in the domain session creation (kernel and ust) only. Same for UST
363 if (session
->kernel_session
->consumer
) {
364 consumer_output_put(session
->kernel_session
->consumer
);
366 session
->kernel_session
->consumer
=
367 consumer_copy_output(session
->consumer
);
368 /* Ease our life a bit for the next part */
369 consumer
= session
->kernel_session
->consumer
;
370 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
372 case LTTNG_DOMAIN_JUL
:
373 case LTTNG_DOMAIN_LOG4J
:
374 case LTTNG_DOMAIN_PYTHON
:
375 case LTTNG_DOMAIN_UST
:
376 DBG3("Copying tracing session consumer output in UST session");
377 if (session
->ust_session
->consumer
) {
378 consumer_output_put(session
->ust_session
->consumer
);
380 session
->ust_session
->consumer
=
381 consumer_copy_output(session
->consumer
);
382 /* Ease our life a bit for the next part */
383 consumer
= session
->ust_session
->consumer
;
384 dir_name
= DEFAULT_UST_TRACE_DIR
;
387 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
391 /* Append correct directory to subdir */
392 strncat(consumer
->subdir
, dir_name
,
393 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
394 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
403 * Create an UST session and add it to the session ust list.
405 * Should *NOT* be called with RCU read-side lock held.
407 static int create_ust_session(struct ltt_session
*session
,
408 struct lttng_domain
*domain
)
411 struct ltt_ust_session
*lus
= NULL
;
415 assert(session
->consumer
);
417 switch (domain
->type
) {
418 case LTTNG_DOMAIN_JUL
:
419 case LTTNG_DOMAIN_LOG4J
:
420 case LTTNG_DOMAIN_PYTHON
:
421 case LTTNG_DOMAIN_UST
:
424 ERR("Unknown UST domain on create session %d", domain
->type
);
425 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
429 DBG("Creating UST session");
431 lus
= trace_ust_create_session(session
->id
);
433 ret
= LTTNG_ERR_UST_SESS_FAIL
;
437 lus
->uid
= session
->uid
;
438 lus
->gid
= session
->gid
;
439 lus
->output_traces
= session
->output_traces
;
440 lus
->snapshot_mode
= session
->snapshot_mode
;
441 lus
->live_timer_interval
= session
->live_timer
;
442 session
->ust_session
= lus
;
443 if (session
->shm_path
[0]) {
444 strncpy(lus
->root_shm_path
, session
->shm_path
,
445 sizeof(lus
->root_shm_path
));
446 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
447 strncpy(lus
->shm_path
, session
->shm_path
,
448 sizeof(lus
->shm_path
));
449 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
450 strncat(lus
->shm_path
, "/ust",
451 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
453 /* Copy session output to the newly created UST session */
454 ret
= copy_session_consumer(domain
->type
, session
);
455 if (ret
!= LTTNG_OK
) {
463 session
->ust_session
= NULL
;
468 * Create a kernel tracer session then create the default channel.
470 static int create_kernel_session(struct ltt_session
*session
)
474 DBG("Creating kernel session");
476 ret
= kernel_create_session(session
, kernel_tracer_fd
);
478 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
482 /* Code flow safety */
483 assert(session
->kernel_session
);
485 /* Copy session output to the newly created Kernel session */
486 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
487 if (ret
!= LTTNG_OK
) {
491 session
->kernel_session
->uid
= session
->uid
;
492 session
->kernel_session
->gid
= session
->gid
;
493 session
->kernel_session
->output_traces
= session
->output_traces
;
494 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
499 trace_kernel_destroy_session(session
->kernel_session
);
500 session
->kernel_session
= NULL
;
506 * Count number of session permitted by uid/gid.
508 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
511 struct ltt_session
*session
;
512 const struct ltt_session_list
*session_list
= session_get_list();
514 DBG("Counting number of available session for UID %d GID %d",
516 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
517 if (!session_get(session
)) {
520 session_lock(session
);
521 /* Only count the sessions the user can control. */
522 if (session_access_ok(session
, uid
, gid
) &&
523 !session
->destroyed
) {
526 session_unlock(session
);
527 session_put(session
);
532 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
533 int *sock_error
, struct lttng_event
*event
)
536 struct lttng_userspace_probe_location
*probe_location
;
537 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
538 struct lttng_dynamic_buffer probe_location_buffer
;
539 struct lttng_buffer_view buffer_view
;
542 * Create a buffer to store the serialized version of the probe
545 lttng_dynamic_buffer_init(&probe_location_buffer
);
546 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
547 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
549 ret
= LTTNG_ERR_NOMEM
;
554 * Receive the probe location.
556 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
557 probe_location_buffer
.size
);
559 DBG("Nothing recv() from client var len data... continuing");
561 lttng_dynamic_buffer_reset(&probe_location_buffer
);
562 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
566 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
567 &probe_location_buffer
, 0, probe_location_buffer
.size
);
570 * Extract the probe location from the serialized version.
572 ret
= lttng_userspace_probe_location_create_from_buffer(
573 &buffer_view
, &probe_location
);
575 WARN("Failed to create a userspace probe location from the received buffer");
576 lttng_dynamic_buffer_reset( &probe_location_buffer
);
577 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
582 * Receive the file descriptor to the target binary from the client.
584 DBG("Receiving userspace probe target FD from client ...");
585 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
587 DBG("Nothing recv() from client userspace probe fd... continuing");
589 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
594 * Set the file descriptor received from the client through the unix
595 * socket in the probe location.
597 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
599 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
604 * From the kernel tracer's perspective, all userspace probe event types
605 * are all the same: a file and an offset.
607 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
608 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
609 ret
= lttng_userspace_probe_location_function_set_binary_fd(
612 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
613 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
617 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
622 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
626 /* Attach the probe location to the event. */
627 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
629 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
633 lttng_dynamic_buffer_reset(&probe_location_buffer
);
639 * Version of setup_lttng_msg() without command header.
641 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
642 void *payload_buf
, size_t payload_len
)
644 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
648 * Free memory of a command context structure.
650 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
652 DBG("Clean command context structure");
654 if ((*cmd_ctx
)->llm
) {
655 free((*cmd_ctx
)->llm
);
657 if ((*cmd_ctx
)->lsm
) {
658 free((*cmd_ctx
)->lsm
);
666 * Check if the current kernel tracer supports the session rotation feature.
667 * Return 1 if it does, 0 otherwise.
669 static int check_rotate_compatible(void)
673 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
674 DBG("Kernel tracer version is not compatible with the rotation feature");
682 * Send data on a unix socket using the liblttsessiondcomm API.
684 * Return lttcomm error code.
686 static int send_unix_sock(int sock
, void *buf
, size_t len
)
688 /* Check valid length */
693 return lttcomm_send_unix_sock(sock
, buf
, len
);
697 * Process the command requested by the lttng client within the command
698 * context structure. This function make sure that the return structure (llm)
699 * is set and ready for transmission before returning.
701 * Return any error encountered or 0 for success.
703 * "sock" is only used for special-case var. len data.
705 * Should *NOT* be called with RCU read-side lock held.
707 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
711 int need_tracing_session
= 1;
714 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
716 assert(!rcu_read_ongoing());
720 switch (cmd_ctx
->lsm
->cmd_type
) {
721 case LTTNG_CREATE_SESSION
:
722 case LTTNG_CREATE_SESSION_SNAPSHOT
:
723 case LTTNG_CREATE_SESSION_LIVE
:
724 case LTTNG_DESTROY_SESSION
:
725 case LTTNG_LIST_SESSIONS
:
726 case LTTNG_LIST_DOMAINS
:
727 case LTTNG_START_TRACE
:
728 case LTTNG_STOP_TRACE
:
729 case LTTNG_DATA_PENDING
:
730 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
731 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
732 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
733 case LTTNG_SNAPSHOT_RECORD
:
734 case LTTNG_SAVE_SESSION
:
735 case LTTNG_SET_SESSION_SHM_PATH
:
736 case LTTNG_REGENERATE_METADATA
:
737 case LTTNG_REGENERATE_STATEDUMP
:
738 case LTTNG_REGISTER_TRIGGER
:
739 case LTTNG_UNREGISTER_TRIGGER
:
740 case LTTNG_ROTATE_SESSION
:
741 case LTTNG_ROTATION_GET_INFO
:
742 case LTTNG_ROTATION_SET_SCHEDULE
:
743 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
750 if (config
.no_kernel
&& need_domain
751 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
753 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
755 ret
= LTTNG_ERR_KERN_NA
;
760 /* Deny register consumer if we already have a spawned consumer. */
761 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
762 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
763 if (kconsumer_data
.pid
> 0) {
764 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
765 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
768 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
772 * Check for command that don't needs to allocate a returned payload. We do
773 * this here so we don't have to make the call for no payload at each
776 switch(cmd_ctx
->lsm
->cmd_type
) {
777 case LTTNG_LIST_SESSIONS
:
778 case LTTNG_LIST_TRACEPOINTS
:
779 case LTTNG_LIST_TRACEPOINT_FIELDS
:
780 case LTTNG_LIST_DOMAINS
:
781 case LTTNG_LIST_CHANNELS
:
782 case LTTNG_LIST_EVENTS
:
783 case LTTNG_LIST_SYSCALLS
:
784 case LTTNG_LIST_TRACKER_PIDS
:
785 case LTTNG_DATA_PENDING
:
786 case LTTNG_ROTATE_SESSION
:
787 case LTTNG_ROTATION_GET_INFO
:
788 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
791 /* Setup lttng message with no payload */
792 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
794 /* This label does not try to unlock the session */
795 goto init_setup_error
;
799 /* Commands that DO NOT need a session. */
800 switch (cmd_ctx
->lsm
->cmd_type
) {
801 case LTTNG_CREATE_SESSION
:
802 case LTTNG_CREATE_SESSION_SNAPSHOT
:
803 case LTTNG_CREATE_SESSION_LIVE
:
804 case LTTNG_LIST_SESSIONS
:
805 case LTTNG_LIST_TRACEPOINTS
:
806 case LTTNG_LIST_SYSCALLS
:
807 case LTTNG_LIST_TRACEPOINT_FIELDS
:
808 case LTTNG_SAVE_SESSION
:
809 case LTTNG_REGISTER_TRIGGER
:
810 case LTTNG_UNREGISTER_TRIGGER
:
811 need_tracing_session
= 0;
814 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
816 * We keep the session list lock across _all_ commands
817 * for now, because the per-session lock does not
818 * handle teardown properly.
821 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
822 if (cmd_ctx
->session
== NULL
) {
823 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
826 /* Acquire lock for the session */
827 session_lock(cmd_ctx
->session
);
833 * Commands that need a valid session but should NOT create one if none
834 * exists. Instead of creating one and destroying it when the command is
835 * handled, process that right before so we save some round trip in useless
838 switch (cmd_ctx
->lsm
->cmd_type
) {
839 case LTTNG_DISABLE_CHANNEL
:
840 case LTTNG_DISABLE_EVENT
:
841 switch (cmd_ctx
->lsm
->domain
.type
) {
842 case LTTNG_DOMAIN_KERNEL
:
843 if (!cmd_ctx
->session
->kernel_session
) {
844 ret
= LTTNG_ERR_NO_CHANNEL
;
848 case LTTNG_DOMAIN_JUL
:
849 case LTTNG_DOMAIN_LOG4J
:
850 case LTTNG_DOMAIN_PYTHON
:
851 case LTTNG_DOMAIN_UST
:
852 if (!cmd_ctx
->session
->ust_session
) {
853 ret
= LTTNG_ERR_NO_CHANNEL
;
858 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
870 * Check domain type for specific "pre-action".
872 switch (cmd_ctx
->lsm
->domain
.type
) {
873 case LTTNG_DOMAIN_KERNEL
:
875 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
879 /* Consumer is in an ERROR state. Report back to client */
880 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
881 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
885 /* Need a session for kernel command */
886 if (need_tracing_session
) {
887 if (cmd_ctx
->session
->kernel_session
== NULL
) {
888 ret
= create_kernel_session(cmd_ctx
->session
);
889 if (ret
!= LTTNG_OK
) {
890 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
895 /* Start the kernel consumer daemon */
896 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
897 if (kconsumer_data
.pid
== 0 &&
898 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
899 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
900 ret
= start_consumerd(&kconsumer_data
);
902 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
905 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
907 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
911 * The consumer was just spawned so we need to add the socket to
912 * the consumer output of the session if exist.
914 ret
= consumer_create_socket(&kconsumer_data
,
915 cmd_ctx
->session
->kernel_session
->consumer
);
922 case LTTNG_DOMAIN_JUL
:
923 case LTTNG_DOMAIN_LOG4J
:
924 case LTTNG_DOMAIN_PYTHON
:
925 case LTTNG_DOMAIN_UST
:
927 if (!ust_app_supported()) {
928 ret
= LTTNG_ERR_NO_UST
;
931 /* Consumer is in an ERROR state. Report back to client */
932 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
933 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
937 if (need_tracing_session
) {
938 /* Create UST session if none exist. */
939 if (cmd_ctx
->session
->ust_session
== NULL
) {
940 ret
= create_ust_session(cmd_ctx
->session
,
941 &cmd_ctx
->lsm
->domain
);
942 if (ret
!= LTTNG_OK
) {
947 /* Start the UST consumer daemons */
949 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
950 if (config
.consumerd64_bin_path
.value
&&
951 ustconsumer64_data
.pid
== 0 &&
952 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
953 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
954 ret
= start_consumerd(&ustconsumer64_data
);
956 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
957 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
961 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
962 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
964 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
968 * Setup socket for consumer 64 bit. No need for atomic access
969 * since it was set above and can ONLY be set in this thread.
971 ret
= consumer_create_socket(&ustconsumer64_data
,
972 cmd_ctx
->session
->ust_session
->consumer
);
978 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
979 if (config
.consumerd32_bin_path
.value
&&
980 ustconsumer32_data
.pid
== 0 &&
981 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
982 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
983 ret
= start_consumerd(&ustconsumer32_data
);
985 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
986 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
990 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
991 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
993 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
997 * Setup socket for consumer 32 bit. No need for atomic access
998 * since it was set above and can ONLY be set in this thread.
1000 ret
= consumer_create_socket(&ustconsumer32_data
,
1001 cmd_ctx
->session
->ust_session
->consumer
);
1013 /* Validate consumer daemon state when start/stop trace command */
1014 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1015 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1016 switch (cmd_ctx
->lsm
->domain
.type
) {
1017 case LTTNG_DOMAIN_NONE
:
1019 case LTTNG_DOMAIN_JUL
:
1020 case LTTNG_DOMAIN_LOG4J
:
1021 case LTTNG_DOMAIN_PYTHON
:
1022 case LTTNG_DOMAIN_UST
:
1023 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1024 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1028 case LTTNG_DOMAIN_KERNEL
:
1029 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1030 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1035 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1041 * Check that the UID or GID match that of the tracing session.
1042 * The root user can interact with all sessions.
1044 if (need_tracing_session
) {
1045 if (!session_access_ok(cmd_ctx
->session
,
1046 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1047 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1048 cmd_ctx
->session
->destroyed
) {
1049 ret
= LTTNG_ERR_EPERM
;
1055 * Send relayd information to consumer as soon as we have a domain and a
1058 if (cmd_ctx
->session
&& need_domain
) {
1060 * Setup relayd if not done yet. If the relayd information was already
1061 * sent to the consumer, this call will gracefully return.
1063 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1064 if (ret
!= LTTNG_OK
) {
1069 /* Process by command type */
1070 switch (cmd_ctx
->lsm
->cmd_type
) {
1071 case LTTNG_ADD_CONTEXT
:
1074 * An LTTNG_ADD_CONTEXT command might have a supplementary
1075 * payload if the context being added is an application context.
1077 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1078 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1079 char *provider_name
= NULL
, *context_name
= NULL
;
1080 size_t provider_name_len
=
1081 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1082 size_t context_name_len
=
1083 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1085 if (provider_name_len
== 0 || context_name_len
== 0) {
1087 * Application provider and context names MUST
1090 ret
= -LTTNG_ERR_INVALID
;
1094 provider_name
= zmalloc(provider_name_len
+ 1);
1095 if (!provider_name
) {
1096 ret
= -LTTNG_ERR_NOMEM
;
1099 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1102 context_name
= zmalloc(context_name_len
+ 1);
1103 if (!context_name
) {
1104 ret
= -LTTNG_ERR_NOMEM
;
1105 goto error_add_context
;
1107 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1110 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
1113 goto error_add_context
;
1116 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
1119 goto error_add_context
;
1124 * cmd_add_context assumes ownership of the provider and context
1127 ret
= cmd_add_context(cmd_ctx
->session
,
1128 cmd_ctx
->lsm
->domain
.type
,
1129 cmd_ctx
->lsm
->u
.context
.channel_name
,
1130 &cmd_ctx
->lsm
->u
.context
.ctx
,
1131 kernel_poll_pipe
[1]);
1133 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1134 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1136 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1137 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1143 case LTTNG_DISABLE_CHANNEL
:
1145 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1146 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1149 case LTTNG_DISABLE_EVENT
:
1153 * FIXME: handle filter; for now we just receive the filter's
1154 * bytecode along with the filter expression which are sent by
1155 * liblttng-ctl and discard them.
1157 * This fixes an issue where the client may block while sending
1158 * the filter payload and encounter an error because the session
1159 * daemon closes the socket without ever handling this data.
1161 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1162 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1165 char data
[LTTNG_FILTER_MAX_LEN
];
1167 DBG("Discarding disable event command payload of size %zu", count
);
1169 ret
= lttcomm_recv_unix_sock(sock
, data
,
1170 count
> sizeof(data
) ? sizeof(data
) : count
);
1175 count
-= (size_t) ret
;
1178 /* FIXME: passing packed structure to non-packed pointer */
1179 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1180 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1181 &cmd_ctx
->lsm
->u
.disable
.event
);
1184 case LTTNG_ENABLE_CHANNEL
:
1186 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1187 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1188 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
1189 &cmd_ctx
->lsm
->u
.channel
.chan
,
1190 kernel_poll_pipe
[1]);
1193 case LTTNG_TRACK_PID
:
1195 ret
= cmd_track_pid(cmd_ctx
->session
,
1196 cmd_ctx
->lsm
->domain
.type
,
1197 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1200 case LTTNG_UNTRACK_PID
:
1202 ret
= cmd_untrack_pid(cmd_ctx
->session
,
1203 cmd_ctx
->lsm
->domain
.type
,
1204 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1207 case LTTNG_ENABLE_EVENT
:
1209 struct lttng_event
*ev
= NULL
;
1210 struct lttng_event_exclusion
*exclusion
= NULL
;
1211 struct lttng_filter_bytecode
*bytecode
= NULL
;
1212 char *filter_expression
= NULL
;
1214 /* Handle exclusion events and receive it from the client. */
1215 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1216 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1218 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1219 (count
* LTTNG_SYMBOL_NAME_LEN
));
1221 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1225 DBG("Receiving var len exclusion event list from client ...");
1226 exclusion
->count
= count
;
1227 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
1228 count
* LTTNG_SYMBOL_NAME_LEN
);
1230 DBG("Nothing recv() from client var len data... continuing");
1233 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1238 /* Get filter expression from client. */
1239 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1240 size_t expression_len
=
1241 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1243 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1244 ret
= LTTNG_ERR_FILTER_INVAL
;
1249 filter_expression
= zmalloc(expression_len
);
1250 if (!filter_expression
) {
1252 ret
= LTTNG_ERR_FILTER_NOMEM
;
1256 /* Receive var. len. data */
1257 DBG("Receiving var len filter's expression from client ...");
1258 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
1261 DBG("Nothing recv() from client var len data... continuing");
1263 free(filter_expression
);
1265 ret
= LTTNG_ERR_FILTER_INVAL
;
1270 /* Handle filter and get bytecode from client. */
1271 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1272 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1274 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1275 ret
= LTTNG_ERR_FILTER_INVAL
;
1276 free(filter_expression
);
1281 bytecode
= zmalloc(bytecode_len
);
1283 free(filter_expression
);
1285 ret
= LTTNG_ERR_FILTER_NOMEM
;
1289 /* Receive var. len. data */
1290 DBG("Receiving var len filter's bytecode from client ...");
1291 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
1293 DBG("Nothing recv() from client var len data... continuing");
1295 free(filter_expression
);
1298 ret
= LTTNG_ERR_FILTER_INVAL
;
1302 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1303 free(filter_expression
);
1306 ret
= LTTNG_ERR_FILTER_INVAL
;
1311 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
1313 DBG("Failed to copy event: %s",
1314 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1315 free(filter_expression
);
1318 ret
= LTTNG_ERR_NOMEM
;
1323 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1324 /* Expect a userspace probe description. */
1325 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
1327 free(filter_expression
);
1330 lttng_event_destroy(ev
);
1335 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
1336 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1338 filter_expression
, bytecode
, exclusion
,
1339 kernel_poll_pipe
[1]);
1340 lttng_event_destroy(ev
);
1343 case LTTNG_LIST_TRACEPOINTS
:
1345 struct lttng_event
*events
;
1348 session_lock_list();
1349 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1350 session_unlock_list();
1351 if (nb_events
< 0) {
1352 /* Return value is a negative lttng_error_code. */
1358 * Setup lttng message with payload size set to the event list size in
1359 * bytes and then copy list into the llm payload.
1361 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1362 sizeof(struct lttng_event
) * nb_events
);
1372 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1374 struct lttng_event_field
*fields
;
1377 session_lock_list();
1378 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1380 session_unlock_list();
1381 if (nb_fields
< 0) {
1382 /* Return value is a negative lttng_error_code. */
1388 * Setup lttng message with payload size set to the event list size in
1389 * bytes and then copy list into the llm payload.
1391 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1392 sizeof(struct lttng_event_field
) * nb_fields
);
1402 case LTTNG_LIST_SYSCALLS
:
1404 struct lttng_event
*events
;
1407 nb_events
= cmd_list_syscalls(&events
);
1408 if (nb_events
< 0) {
1409 /* Return value is a negative lttng_error_code. */
1415 * Setup lttng message with payload size set to the event list size in
1416 * bytes and then copy list into the llm payload.
1418 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1419 sizeof(struct lttng_event
) * nb_events
);
1429 case LTTNG_LIST_TRACKER_PIDS
:
1431 int32_t *pids
= NULL
;
1434 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
1435 cmd_ctx
->lsm
->domain
.type
, &pids
);
1437 /* Return value is a negative lttng_error_code. */
1443 * Setup lttng message with payload size set to the event list size in
1444 * bytes and then copy list into the llm payload.
1446 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
1447 sizeof(int32_t) * nr_pids
);
1457 case LTTNG_SET_CONSUMER_URI
:
1460 struct lttng_uri
*uris
;
1462 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1463 len
= nb_uri
* sizeof(struct lttng_uri
);
1466 ret
= LTTNG_ERR_INVALID
;
1470 uris
= zmalloc(len
);
1472 ret
= LTTNG_ERR_FATAL
;
1476 /* Receive variable len data */
1477 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1478 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1480 DBG("No URIs received from client... continuing");
1482 ret
= LTTNG_ERR_SESSION_FAIL
;
1487 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1489 if (ret
!= LTTNG_OK
) {
1496 case LTTNG_START_TRACE
:
1499 * On the first start, if we have a kernel session and we have
1500 * enabled time or size-based rotations, we have to make sure
1501 * the kernel tracer supports it.
1503 if (!cmd_ctx
->session
->has_been_started
&& \
1504 cmd_ctx
->session
->kernel_session
&& \
1505 (cmd_ctx
->session
->rotate_timer_period
|| \
1506 cmd_ctx
->session
->rotate_size
) && \
1507 !check_rotate_compatible()) {
1508 DBG("Kernel tracer version is not compatible with the rotation feature");
1509 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1512 ret
= cmd_start_trace(cmd_ctx
->session
);
1515 case LTTNG_STOP_TRACE
:
1517 ret
= cmd_stop_trace(cmd_ctx
->session
);
1520 case LTTNG_CREATE_SESSION
:
1523 struct lttng_uri
*uris
= NULL
;
1525 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1526 len
= nb_uri
* sizeof(struct lttng_uri
);
1529 uris
= zmalloc(len
);
1531 ret
= LTTNG_ERR_FATAL
;
1535 /* Receive variable len data */
1536 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1537 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1539 DBG("No URIs received from client... continuing");
1541 ret
= LTTNG_ERR_SESSION_FAIL
;
1546 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1547 DBG("Creating session with ONE network URI is a bad call");
1548 ret
= LTTNG_ERR_SESSION_FAIL
;
1554 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
1555 &cmd_ctx
->creds
, 0);
1561 case LTTNG_DESTROY_SESSION
:
1563 ret
= cmd_destroy_session(cmd_ctx
->session
,
1564 notification_thread_handle
);
1567 case LTTNG_LIST_DOMAINS
:
1570 struct lttng_domain
*domains
= NULL
;
1572 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1574 /* Return value is a negative lttng_error_code. */
1579 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1580 nb_dom
* sizeof(struct lttng_domain
));
1590 case LTTNG_LIST_CHANNELS
:
1592 ssize_t payload_size
;
1593 struct lttng_channel
*channels
= NULL
;
1595 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1596 cmd_ctx
->session
, &channels
);
1597 if (payload_size
< 0) {
1598 /* Return value is a negative lttng_error_code. */
1599 ret
= -payload_size
;
1603 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1614 case LTTNG_LIST_EVENTS
:
1617 struct lttng_event
*events
= NULL
;
1618 struct lttcomm_event_command_header cmd_header
;
1621 memset(&cmd_header
, 0, sizeof(cmd_header
));
1622 /* Extended infos are included at the end of events */
1623 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1624 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1625 &events
, &total_size
);
1628 /* Return value is a negative lttng_error_code. */
1633 cmd_header
.nb_events
= nb_event
;
1634 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1635 &cmd_header
, sizeof(cmd_header
));
1645 case LTTNG_LIST_SESSIONS
:
1647 unsigned int nr_sessions
;
1648 void *sessions_payload
;
1651 session_lock_list();
1652 nr_sessions
= lttng_sessions_count(
1653 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1654 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1655 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
1656 sessions_payload
= zmalloc(payload_len
);
1658 if (!sessions_payload
) {
1659 session_unlock_list();
1664 cmd_list_lttng_sessions(sessions_payload
,
1665 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1666 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1667 session_unlock_list();
1669 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1671 free(sessions_payload
);
1680 case LTTNG_REGISTER_CONSUMER
:
1682 struct consumer_data
*cdata
;
1684 switch (cmd_ctx
->lsm
->domain
.type
) {
1685 case LTTNG_DOMAIN_KERNEL
:
1686 cdata
= &kconsumer_data
;
1689 ret
= LTTNG_ERR_UND
;
1693 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1694 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1697 case LTTNG_DATA_PENDING
:
1700 uint8_t pending_ret_byte
;
1702 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1707 * This function may returns 0 or 1 to indicate whether or not
1708 * there is data pending. In case of error, it should return an
1709 * LTTNG_ERR code. However, some code paths may still return
1710 * a nondescript error code, which we handle by returning an
1713 if (pending_ret
== 0 || pending_ret
== 1) {
1715 * ret will be set to LTTNG_OK at the end of
1718 } else if (pending_ret
< 0) {
1719 ret
= LTTNG_ERR_UNK
;
1726 pending_ret_byte
= (uint8_t) pending_ret
;
1728 /* 1 byte to return whether or not data is pending */
1729 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1730 &pending_ret_byte
, 1);
1739 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1741 struct lttcomm_lttng_output_id reply
;
1743 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1744 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
1745 if (ret
!= LTTNG_OK
) {
1749 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1755 /* Copy output list into message payload */
1759 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1761 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1762 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
1765 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1768 struct lttng_snapshot_output
*outputs
= NULL
;
1770 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1771 if (nb_output
< 0) {
1776 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1777 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1778 nb_output
* sizeof(struct lttng_snapshot_output
));
1788 case LTTNG_SNAPSHOT_RECORD
:
1790 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1791 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
1792 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1795 case LTTNG_CREATE_SESSION_SNAPSHOT
:
1798 struct lttng_uri
*uris
= NULL
;
1800 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1801 len
= nb_uri
* sizeof(struct lttng_uri
);
1804 uris
= zmalloc(len
);
1806 ret
= LTTNG_ERR_FATAL
;
1810 /* Receive variable len data */
1811 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1812 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1814 DBG("No URIs received from client... continuing");
1816 ret
= LTTNG_ERR_SESSION_FAIL
;
1821 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1822 DBG("Creating session with ONE network URI is a bad call");
1823 ret
= LTTNG_ERR_SESSION_FAIL
;
1829 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
1830 nb_uri
, &cmd_ctx
->creds
);
1834 case LTTNG_CREATE_SESSION_LIVE
:
1837 struct lttng_uri
*uris
= NULL
;
1839 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1840 len
= nb_uri
* sizeof(struct lttng_uri
);
1843 uris
= zmalloc(len
);
1845 ret
= LTTNG_ERR_FATAL
;
1849 /* Receive variable len data */
1850 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1851 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1853 DBG("No URIs received from client... continuing");
1855 ret
= LTTNG_ERR_SESSION_FAIL
;
1860 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1861 DBG("Creating session with ONE network URI is a bad call");
1862 ret
= LTTNG_ERR_SESSION_FAIL
;
1868 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
1869 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
1873 case LTTNG_SAVE_SESSION
:
1875 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1879 case LTTNG_SET_SESSION_SHM_PATH
:
1881 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1882 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1885 case LTTNG_REGENERATE_METADATA
:
1887 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1890 case LTTNG_REGENERATE_STATEDUMP
:
1892 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1895 case LTTNG_REGISTER_TRIGGER
:
1897 ret
= cmd_register_trigger(cmd_ctx
, sock
,
1898 notification_thread_handle
);
1901 case LTTNG_UNREGISTER_TRIGGER
:
1903 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
1904 notification_thread_handle
);
1907 case LTTNG_ROTATE_SESSION
:
1909 struct lttng_rotate_session_return rotate_return
;
1911 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1913 memset(&rotate_return
, 0, sizeof(rotate_return
));
1914 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1915 DBG("Kernel tracer version is not compatible with the rotation feature");
1916 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1920 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
1926 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
1927 sizeof(rotate_return
));
1936 case LTTNG_ROTATION_GET_INFO
:
1938 struct lttng_rotation_get_info_return get_info_return
;
1940 memset(&get_info_return
, 0, sizeof(get_info_return
));
1941 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
1942 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
1948 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
1949 sizeof(get_info_return
));
1958 case LTTNG_ROTATION_SET_SCHEDULE
:
1961 enum lttng_rotation_schedule_type schedule_type
;
1964 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1965 DBG("Kernel tracer version does not support session rotations");
1966 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1970 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
1971 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
1972 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
1974 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
1978 notification_thread_handle
);
1979 if (ret
!= LTTNG_OK
) {
1985 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1987 struct lttng_session_list_schedules_return schedules
= {
1988 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
1989 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
1990 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
1991 .size
.value
= cmd_ctx
->session
->rotate_size
,
1994 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2005 ret
= LTTNG_ERR_UND
;
2010 if (cmd_ctx
->llm
== NULL
) {
2011 DBG("Missing llm structure. Allocating one.");
2012 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2016 /* Set return code */
2017 cmd_ctx
->llm
->ret_code
= ret
;
2019 if (cmd_ctx
->session
) {
2020 session_unlock(cmd_ctx
->session
);
2021 session_put(cmd_ctx
->session
);
2023 if (need_tracing_session
) {
2024 session_unlock_list();
2027 assert(!rcu_read_ongoing());
2031 static int create_client_sock(void)
2033 int ret
, client_sock
;
2034 const mode_t old_umask
= umask(0);
2036 /* Create client tool unix socket */
2037 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2038 if (client_sock
< 0) {
2039 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2044 /* Set the cloexec flag */
2045 ret
= utils_set_fd_cloexec(client_sock
);
2047 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2048 "Continuing but note that the consumer daemon will have a "
2049 "reference to this socket on exec()", client_sock
);
2052 /* File permission MUST be 660 */
2053 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2055 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
2059 DBG("Created client socket (fd = %i)", client_sock
);
2066 static void cleanup_client_thread(void *data
)
2068 struct lttng_pipe
*quit_pipe
= data
;
2070 lttng_pipe_destroy(quit_pipe
);
2073 static void thread_init_cleanup(void *data
)
2075 set_thread_status(false);
2079 * This thread manage all clients request using the unix client socket for
2082 static void *thread_manage_clients(void *data
)
2084 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2086 uint32_t revents
, nb_fd
;
2087 struct command_ctx
*cmd_ctx
= NULL
;
2088 struct lttng_poll_event events
;
2089 int client_sock
= -1;
2090 struct lttng_pipe
*quit_pipe
= data
;
2091 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2093 DBG("[thread] Manage client started");
2095 is_root
= (getuid() == 0);
2097 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2098 client_sock
= create_client_sock();
2099 if (client_sock
< 0) {
2103 rcu_register_thread();
2105 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2107 health_code_update();
2109 ret
= lttcomm_listen_unix_sock(client_sock
);
2115 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2116 * more will be added to this poll set.
2118 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2120 goto error_create_poll
;
2123 /* Add the application registration socket */
2124 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2129 /* Add thread quit pipe */
2130 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2135 /* Set state as running. */
2136 set_thread_status(true);
2137 pthread_cleanup_pop(0);
2139 /* This testpoint is after we signal readiness to the parent. */
2140 if (testpoint(sessiond_thread_manage_clients
)) {
2144 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2148 health_code_update();
2151 const struct cmd_completion_handler
*cmd_completion_handler
;
2153 DBG("Accepting client command ...");
2155 /* Inifinite blocking call, waiting for transmission */
2157 health_poll_entry();
2158 ret
= lttng_poll_wait(&events
, -1);
2162 * Restart interrupted system call.
2164 if (errno
== EINTR
) {
2172 for (i
= 0; i
< nb_fd
; i
++) {
2173 revents
= LTTNG_POLL_GETEV(&events
, i
);
2174 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2176 health_code_update();
2179 /* No activity for this FD (poll implementation). */
2183 if (pollfd
== thread_quit_pipe_fd
) {
2187 /* Event on the registration socket */
2188 if (revents
& LPOLLIN
) {
2190 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2191 ERR("Client socket poll error");
2194 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2200 DBG("Wait for client response");
2202 health_code_update();
2204 sock
= lttcomm_accept_unix_sock(client_sock
);
2210 * Set the CLOEXEC flag. Return code is useless because either way, the
2213 (void) utils_set_fd_cloexec(sock
);
2215 /* Set socket option for credentials retrieval */
2216 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2221 /* Allocate context command to process the client request */
2222 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2223 if (cmd_ctx
== NULL
) {
2224 PERROR("zmalloc cmd_ctx");
2228 /* Allocate data buffer for reception */
2229 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2230 if (cmd_ctx
->lsm
== NULL
) {
2231 PERROR("zmalloc cmd_ctx->lsm");
2235 cmd_ctx
->llm
= NULL
;
2236 cmd_ctx
->session
= NULL
;
2238 health_code_update();
2241 * Data is received from the lttng client. The struct
2242 * lttcomm_session_msg (lsm) contains the command and data request of
2245 DBG("Receiving data from client ...");
2246 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2247 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2249 DBG("Nothing recv() from client... continuing");
2255 clean_command_ctx(&cmd_ctx
);
2259 health_code_update();
2261 // TODO: Validate cmd_ctx including sanity check for
2262 // security purpose.
2264 rcu_thread_online();
2266 * This function dispatch the work to the kernel or userspace tracer
2267 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2268 * informations for the client. The command context struct contains
2269 * everything this function may needs.
2271 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
2272 rcu_thread_offline();
2280 * TODO: Inform client somehow of the fatal error. At
2281 * this point, ret < 0 means that a zmalloc failed
2282 * (ENOMEM). Error detected but still accept
2283 * command, unless a socket error has been
2286 clean_command_ctx(&cmd_ctx
);
2290 cmd_completion_handler
= cmd_pop_completion_handler();
2291 if (cmd_completion_handler
) {
2292 enum lttng_error_code completion_code
;
2294 completion_code
= cmd_completion_handler
->run(
2295 cmd_completion_handler
->data
);
2296 if (completion_code
!= LTTNG_OK
) {
2297 clean_command_ctx(&cmd_ctx
);
2302 health_code_update();
2304 DBG("Sending response (size: %d, retcode: %s (%d))",
2305 cmd_ctx
->lttng_msg_size
,
2306 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2307 cmd_ctx
->llm
->ret_code
);
2308 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2310 ERR("Failed to send data back to client");
2313 /* End of transmission */
2320 clean_command_ctx(&cmd_ctx
);
2322 health_code_update();
2334 lttng_poll_clean(&events
);
2335 clean_command_ctx(&cmd_ctx
);
2339 unlink(config
.client_unix_sock_path
.value
);
2340 if (client_sock
>= 0) {
2341 ret
= close(client_sock
);
2349 ERR("Health error occurred in %s", __func__
);
2352 health_unregister(health_sessiond
);
2354 DBG("Client thread dying");
2356 rcu_unregister_thread();
2361 bool shutdown_client_thread(void *thread_data
)
2363 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2364 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2366 return notify_thread_pipe(write_fd
) == 1;
2369 struct lttng_thread
*launch_client_thread(void)
2371 bool thread_running
;
2372 struct lttng_pipe
*client_quit_pipe
;
2373 struct lttng_thread
*thread
;
2375 sem_init(&thread_state
.ready
, 0, 0);
2376 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2377 if (!client_quit_pipe
) {
2381 thread
= lttng_thread_create("Client management",
2382 thread_manage_clients
,
2383 shutdown_client_thread
,
2384 cleanup_client_thread
,
2391 * This thread is part of the threads that need to be fully
2392 * initialized before the session daemon is marked as "ready".
2394 thread_running
= wait_thread_status();
2395 if (!thread_running
) {
2396 lttng_thread_put(thread
);
2401 cleanup_client_thread(client_quit_pipe
);