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
;
84 cmd_ctx
->llm
= zmalloc(total_msg_size
);
86 if (cmd_ctx
->llm
== NULL
) {
92 /* Copy common data */
93 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
94 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
95 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
96 cmd_ctx
->llm
->data_size
= payload_len
;
97 cmd_ctx
->lttng_msg_size
= total_msg_size
;
99 /* Copy command header */
100 if (cmd_header_len
) {
101 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
107 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
116 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
117 * exec or it will fail.
119 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
121 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
125 * Fork and exec a consumer daemon (consumerd).
127 * Return pid if successful else -1.
129 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
133 const char *consumer_to_use
;
134 const char *verbosity
;
137 DBG("Spawning consumerd");
144 if (config
.verbose_consumer
) {
145 verbosity
= "--verbose";
146 } else if (lttng_opt_quiet
) {
147 verbosity
= "--quiet";
152 switch (consumer_data
->type
) {
153 case LTTNG_CONSUMER_KERNEL
:
155 * Find out which consumerd to execute. We will first try the
156 * 64-bit path, then the sessiond's installation directory, and
157 * fallback on the 32-bit one,
159 DBG3("Looking for a kernel consumer at these locations:");
160 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
161 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
162 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
163 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
164 DBG3("Found location #1");
165 consumer_to_use
= config
.consumerd64_bin_path
.value
;
166 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
167 DBG3("Found location #2");
168 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
169 } else if (config
.consumerd32_bin_path
.value
&&
170 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
171 DBG3("Found location #3");
172 consumer_to_use
= config
.consumerd32_bin_path
.value
;
174 DBG("Could not find any valid consumerd executable");
178 DBG("Using kernel consumer at: %s", consumer_to_use
);
179 (void) execl(consumer_to_use
,
180 "lttng-consumerd", verbosity
, "-k",
181 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
182 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
183 "--group", config
.tracing_group_name
.value
,
186 case LTTNG_CONSUMER64_UST
:
188 if (config
.consumerd64_lib_dir
.value
) {
193 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
197 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
198 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
203 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
204 if (tmp
[0] != '\0') {
208 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
215 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
216 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
217 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
218 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
219 "--group", config
.tracing_group_name
.value
,
223 case LTTNG_CONSUMER32_UST
:
225 if (config
.consumerd32_lib_dir
.value
) {
230 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
234 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
235 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
240 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
241 if (tmp
[0] != '\0') {
245 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
252 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
253 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
254 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
255 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
256 "--group", config
.tracing_group_name
.value
,
261 ERR("unknown consumer type");
265 PERROR("Consumer execl()");
267 /* Reaching this point, we got a failure on our execl(). */
269 } else if (pid
> 0) {
272 PERROR("start consumer fork");
280 * Spawn the consumerd daemon and session daemon thread.
282 static int start_consumerd(struct consumer_data
*consumer_data
)
287 * Set the listen() state on the socket since there is a possible race
288 * between the exec() of the consumer daemon and this call if place in the
289 * consumer thread. See bug #366 for more details.
291 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
296 pthread_mutex_lock(&consumer_data
->pid_mutex
);
297 if (consumer_data
->pid
!= 0) {
298 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
302 ret
= spawn_consumerd(consumer_data
);
304 ERR("Spawning consumerd failed");
305 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
309 /* Setting up the consumer_data pid */
310 consumer_data
->pid
= ret
;
311 DBG2("Consumer pid %d", consumer_data
->pid
);
312 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
314 DBG2("Spawning consumer control thread");
315 ret
= spawn_consumer_thread(consumer_data
);
317 ERR("Fatal error spawning consumer control thread");
325 /* Cleanup already created sockets on error. */
326 if (consumer_data
->err_sock
>= 0) {
329 err
= close(consumer_data
->err_sock
);
331 PERROR("close consumer data error socket");
338 * Copy consumer output from the tracing session to the domain session. The
339 * function also applies the right modification on a per domain basis for the
340 * trace files destination directory.
342 * Should *NOT* be called with RCU read-side lock held.
344 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
347 const char *dir_name
;
348 struct consumer_output
*consumer
;
351 assert(session
->consumer
);
354 case LTTNG_DOMAIN_KERNEL
:
355 DBG3("Copying tracing session consumer output in kernel session");
357 * XXX: We should audit the session creation and what this function
358 * does "extra" in order to avoid a destroy since this function is used
359 * in the domain session creation (kernel and ust) only. Same for UST
362 if (session
->kernel_session
->consumer
) {
363 consumer_output_put(session
->kernel_session
->consumer
);
365 session
->kernel_session
->consumer
=
366 consumer_copy_output(session
->consumer
);
367 /* Ease our life a bit for the next part */
368 consumer
= session
->kernel_session
->consumer
;
369 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
371 case LTTNG_DOMAIN_JUL
:
372 case LTTNG_DOMAIN_LOG4J
:
373 case LTTNG_DOMAIN_PYTHON
:
374 case LTTNG_DOMAIN_UST
:
375 DBG3("Copying tracing session consumer output in UST session");
376 if (session
->ust_session
->consumer
) {
377 consumer_output_put(session
->ust_session
->consumer
);
379 session
->ust_session
->consumer
=
380 consumer_copy_output(session
->consumer
);
381 /* Ease our life a bit for the next part */
382 consumer
= session
->ust_session
->consumer
;
383 dir_name
= DEFAULT_UST_TRACE_DIR
;
386 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
390 /* Append correct directory to subdir */
391 strncat(consumer
->subdir
, dir_name
,
392 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
393 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
402 * Create an UST session and add it to the session ust list.
404 * Should *NOT* be called with RCU read-side lock held.
406 static int create_ust_session(struct ltt_session
*session
,
407 struct lttng_domain
*domain
)
410 struct ltt_ust_session
*lus
= NULL
;
414 assert(session
->consumer
);
416 switch (domain
->type
) {
417 case LTTNG_DOMAIN_JUL
:
418 case LTTNG_DOMAIN_LOG4J
:
419 case LTTNG_DOMAIN_PYTHON
:
420 case LTTNG_DOMAIN_UST
:
423 ERR("Unknown UST domain on create session %d", domain
->type
);
424 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
428 DBG("Creating UST session");
430 lus
= trace_ust_create_session(session
->id
);
432 ret
= LTTNG_ERR_UST_SESS_FAIL
;
436 lus
->uid
= session
->uid
;
437 lus
->gid
= session
->gid
;
438 lus
->output_traces
= session
->output_traces
;
439 lus
->snapshot_mode
= session
->snapshot_mode
;
440 lus
->live_timer_interval
= session
->live_timer
;
441 session
->ust_session
= lus
;
442 if (session
->shm_path
[0]) {
443 strncpy(lus
->root_shm_path
, session
->shm_path
,
444 sizeof(lus
->root_shm_path
));
445 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
446 strncpy(lus
->shm_path
, session
->shm_path
,
447 sizeof(lus
->shm_path
));
448 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
449 strncat(lus
->shm_path
, "/ust",
450 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
452 /* Copy session output to the newly created UST session */
453 ret
= copy_session_consumer(domain
->type
, session
);
454 if (ret
!= LTTNG_OK
) {
462 session
->ust_session
= NULL
;
467 * Create a kernel tracer session then create the default channel.
469 static int create_kernel_session(struct ltt_session
*session
)
473 DBG("Creating kernel session");
475 ret
= kernel_create_session(session
, kernel_tracer_fd
);
477 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
481 /* Code flow safety */
482 assert(session
->kernel_session
);
484 /* Copy session output to the newly created Kernel session */
485 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
486 if (ret
!= LTTNG_OK
) {
490 session
->kernel_session
->uid
= session
->uid
;
491 session
->kernel_session
->gid
= session
->gid
;
492 session
->kernel_session
->output_traces
= session
->output_traces
;
493 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
498 trace_kernel_destroy_session(session
->kernel_session
);
499 session
->kernel_session
= NULL
;
505 * Count number of session permitted by uid/gid.
507 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
510 struct ltt_session
*session
;
511 const struct ltt_session_list
*session_list
= session_get_list();
513 DBG("Counting number of available session for UID %d GID %d",
515 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
516 if (!session_get(session
)) {
519 session_lock(session
);
520 /* Only count the sessions the user can control. */
521 if (session_access_ok(session
, uid
, gid
) &&
522 !session
->destroyed
) {
525 session_unlock(session
);
526 session_put(session
);
531 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
532 int *sock_error
, struct lttng_event
*event
)
535 struct lttng_userspace_probe_location
*probe_location
;
536 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
537 struct lttng_dynamic_buffer probe_location_buffer
;
538 struct lttng_buffer_view buffer_view
;
541 * Create a buffer to store the serialized version of the probe
544 lttng_dynamic_buffer_init(&probe_location_buffer
);
545 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
546 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
548 ret
= LTTNG_ERR_NOMEM
;
553 * Receive the probe location.
555 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
556 probe_location_buffer
.size
);
558 DBG("Nothing recv() from client var len data... continuing");
560 lttng_dynamic_buffer_reset(&probe_location_buffer
);
561 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
565 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
566 &probe_location_buffer
, 0, probe_location_buffer
.size
);
569 * Extract the probe location from the serialized version.
571 ret
= lttng_userspace_probe_location_create_from_buffer(
572 &buffer_view
, &probe_location
);
574 WARN("Failed to create a userspace probe location from the received buffer");
575 lttng_dynamic_buffer_reset( &probe_location_buffer
);
576 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
581 * Receive the file descriptor to the target binary from the client.
583 DBG("Receiving userspace probe target FD from client ...");
584 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
586 DBG("Nothing recv() from client userspace probe fd... continuing");
588 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
593 * Set the file descriptor received from the client through the unix
594 * socket in the probe location.
596 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
598 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
603 * From the kernel tracer's perspective, all userspace probe event types
604 * are all the same: a file and an offset.
606 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
607 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
608 ret
= lttng_userspace_probe_location_function_set_binary_fd(
611 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
612 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
616 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
621 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
625 /* Attach the probe location to the event. */
626 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
628 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
632 lttng_dynamic_buffer_reset(&probe_location_buffer
);
638 * Version of setup_lttng_msg() without command header.
640 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
641 void *payload_buf
, size_t payload_len
)
643 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
647 * Free memory of a command context structure.
649 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
651 DBG("Clean command context structure");
653 if ((*cmd_ctx
)->llm
) {
654 free((*cmd_ctx
)->llm
);
656 if ((*cmd_ctx
)->lsm
) {
657 free((*cmd_ctx
)->lsm
);
665 * Check if the current kernel tracer supports the session rotation feature.
666 * Return 1 if it does, 0 otherwise.
668 static int check_rotate_compatible(void)
672 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
673 DBG("Kernel tracer version is not compatible with the rotation feature");
681 * Send data on a unix socket using the liblttsessiondcomm API.
683 * Return lttcomm error code.
685 static int send_unix_sock(int sock
, void *buf
, size_t len
)
687 /* Check valid length */
692 return lttcomm_send_unix_sock(sock
, buf
, len
);
696 * Process the command requested by the lttng client within the command
697 * context structure. This function make sure that the return structure (llm)
698 * is set and ready for transmission before returning.
700 * Return any error encountered or 0 for success.
702 * "sock" is only used for special-case var. len data.
704 * Should *NOT* be called with RCU read-side lock held.
706 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
710 int need_tracing_session
= 1;
713 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
715 assert(!rcu_read_ongoing());
719 switch (cmd_ctx
->lsm
->cmd_type
) {
720 case LTTNG_CREATE_SESSION
:
721 case LTTNG_CREATE_SESSION_SNAPSHOT
:
722 case LTTNG_CREATE_SESSION_LIVE
:
723 case LTTNG_DESTROY_SESSION
:
724 case LTTNG_LIST_SESSIONS
:
725 case LTTNG_LIST_DOMAINS
:
726 case LTTNG_START_TRACE
:
727 case LTTNG_STOP_TRACE
:
728 case LTTNG_DATA_PENDING
:
729 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
730 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
731 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
732 case LTTNG_SNAPSHOT_RECORD
:
733 case LTTNG_SAVE_SESSION
:
734 case LTTNG_SET_SESSION_SHM_PATH
:
735 case LTTNG_REGENERATE_METADATA
:
736 case LTTNG_REGENERATE_STATEDUMP
:
737 case LTTNG_REGISTER_TRIGGER
:
738 case LTTNG_UNREGISTER_TRIGGER
:
739 case LTTNG_ROTATE_SESSION
:
740 case LTTNG_ROTATION_GET_INFO
:
741 case LTTNG_ROTATION_SET_SCHEDULE
:
742 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
749 if (config
.no_kernel
&& need_domain
750 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
752 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
754 ret
= LTTNG_ERR_KERN_NA
;
759 /* Deny register consumer if we already have a spawned consumer. */
760 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
761 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
762 if (kconsumer_data
.pid
> 0) {
763 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
764 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
767 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
771 * Check for command that don't needs to allocate a returned payload. We do
772 * this here so we don't have to make the call for no payload at each
775 switch(cmd_ctx
->lsm
->cmd_type
) {
776 case LTTNG_LIST_SESSIONS
:
777 case LTTNG_LIST_TRACEPOINTS
:
778 case LTTNG_LIST_TRACEPOINT_FIELDS
:
779 case LTTNG_LIST_DOMAINS
:
780 case LTTNG_LIST_CHANNELS
:
781 case LTTNG_LIST_EVENTS
:
782 case LTTNG_LIST_SYSCALLS
:
783 case LTTNG_LIST_TRACKER_PIDS
:
784 case LTTNG_DATA_PENDING
:
785 case LTTNG_ROTATE_SESSION
:
786 case LTTNG_ROTATION_GET_INFO
:
787 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
790 /* Setup lttng message with no payload */
791 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
793 /* This label does not try to unlock the session */
794 goto init_setup_error
;
798 /* Commands that DO NOT need a session. */
799 switch (cmd_ctx
->lsm
->cmd_type
) {
800 case LTTNG_CREATE_SESSION
:
801 case LTTNG_CREATE_SESSION_SNAPSHOT
:
802 case LTTNG_CREATE_SESSION_LIVE
:
803 case LTTNG_LIST_SESSIONS
:
804 case LTTNG_LIST_TRACEPOINTS
:
805 case LTTNG_LIST_SYSCALLS
:
806 case LTTNG_LIST_TRACEPOINT_FIELDS
:
807 case LTTNG_SAVE_SESSION
:
808 case LTTNG_REGISTER_TRIGGER
:
809 case LTTNG_UNREGISTER_TRIGGER
:
810 need_tracing_session
= 0;
813 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
815 * We keep the session list lock across _all_ commands
816 * for now, because the per-session lock does not
817 * handle teardown properly.
820 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
821 if (cmd_ctx
->session
== NULL
) {
822 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
825 /* Acquire lock for the session */
826 session_lock(cmd_ctx
->session
);
832 * Commands that need a valid session but should NOT create one if none
833 * exists. Instead of creating one and destroying it when the command is
834 * handled, process that right before so we save some round trip in useless
837 switch (cmd_ctx
->lsm
->cmd_type
) {
838 case LTTNG_DISABLE_CHANNEL
:
839 case LTTNG_DISABLE_EVENT
:
840 switch (cmd_ctx
->lsm
->domain
.type
) {
841 case LTTNG_DOMAIN_KERNEL
:
842 if (!cmd_ctx
->session
->kernel_session
) {
843 ret
= LTTNG_ERR_NO_CHANNEL
;
847 case LTTNG_DOMAIN_JUL
:
848 case LTTNG_DOMAIN_LOG4J
:
849 case LTTNG_DOMAIN_PYTHON
:
850 case LTTNG_DOMAIN_UST
:
851 if (!cmd_ctx
->session
->ust_session
) {
852 ret
= LTTNG_ERR_NO_CHANNEL
;
857 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
869 * Check domain type for specific "pre-action".
871 switch (cmd_ctx
->lsm
->domain
.type
) {
872 case LTTNG_DOMAIN_KERNEL
:
874 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
878 /* Consumer is in an ERROR state. Report back to client */
879 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
880 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
884 /* Need a session for kernel command */
885 if (need_tracing_session
) {
886 if (cmd_ctx
->session
->kernel_session
== NULL
) {
887 ret
= create_kernel_session(cmd_ctx
->session
);
888 if (ret
!= LTTNG_OK
) {
889 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
894 /* Start the kernel consumer daemon */
895 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
896 if (kconsumer_data
.pid
== 0 &&
897 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
898 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
899 ret
= start_consumerd(&kconsumer_data
);
901 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
904 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
906 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
910 * The consumer was just spawned so we need to add the socket to
911 * the consumer output of the session if exist.
913 ret
= consumer_create_socket(&kconsumer_data
,
914 cmd_ctx
->session
->kernel_session
->consumer
);
921 case LTTNG_DOMAIN_JUL
:
922 case LTTNG_DOMAIN_LOG4J
:
923 case LTTNG_DOMAIN_PYTHON
:
924 case LTTNG_DOMAIN_UST
:
926 if (!ust_app_supported()) {
927 ret
= LTTNG_ERR_NO_UST
;
930 /* Consumer is in an ERROR state. Report back to client */
931 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
932 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
936 if (need_tracing_session
) {
937 /* Create UST session if none exist. */
938 if (cmd_ctx
->session
->ust_session
== NULL
) {
939 ret
= create_ust_session(cmd_ctx
->session
,
940 &cmd_ctx
->lsm
->domain
);
941 if (ret
!= LTTNG_OK
) {
946 /* Start the UST consumer daemons */
948 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
949 if (config
.consumerd64_bin_path
.value
&&
950 ustconsumer64_data
.pid
== 0 &&
951 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
952 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
953 ret
= start_consumerd(&ustconsumer64_data
);
955 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
956 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
960 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
961 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
963 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
967 * Setup socket for consumer 64 bit. No need for atomic access
968 * since it was set above and can ONLY be set in this thread.
970 ret
= consumer_create_socket(&ustconsumer64_data
,
971 cmd_ctx
->session
->ust_session
->consumer
);
977 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
978 if (config
.consumerd32_bin_path
.value
&&
979 ustconsumer32_data
.pid
== 0 &&
980 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
981 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
982 ret
= start_consumerd(&ustconsumer32_data
);
984 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
985 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
989 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
990 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
992 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
996 * Setup socket for consumer 32 bit. No need for atomic access
997 * since it was set above and can ONLY be set in this thread.
999 ret
= consumer_create_socket(&ustconsumer32_data
,
1000 cmd_ctx
->session
->ust_session
->consumer
);
1012 /* Validate consumer daemon state when start/stop trace command */
1013 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1014 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1015 switch (cmd_ctx
->lsm
->domain
.type
) {
1016 case LTTNG_DOMAIN_NONE
:
1018 case LTTNG_DOMAIN_JUL
:
1019 case LTTNG_DOMAIN_LOG4J
:
1020 case LTTNG_DOMAIN_PYTHON
:
1021 case LTTNG_DOMAIN_UST
:
1022 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1023 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1027 case LTTNG_DOMAIN_KERNEL
:
1028 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1029 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1034 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1040 * Check that the UID or GID match that of the tracing session.
1041 * The root user can interact with all sessions.
1043 if (need_tracing_session
) {
1044 if (!session_access_ok(cmd_ctx
->session
,
1045 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1046 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1047 cmd_ctx
->session
->destroyed
) {
1048 ret
= LTTNG_ERR_EPERM
;
1054 * Send relayd information to consumer as soon as we have a domain and a
1057 if (cmd_ctx
->session
&& need_domain
) {
1059 * Setup relayd if not done yet. If the relayd information was already
1060 * sent to the consumer, this call will gracefully return.
1062 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1063 if (ret
!= LTTNG_OK
) {
1068 /* Process by command type */
1069 switch (cmd_ctx
->lsm
->cmd_type
) {
1070 case LTTNG_ADD_CONTEXT
:
1073 * An LTTNG_ADD_CONTEXT command might have a supplementary
1074 * payload if the context being added is an application context.
1076 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1077 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1078 char *provider_name
= NULL
, *context_name
= NULL
;
1079 size_t provider_name_len
=
1080 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1081 size_t context_name_len
=
1082 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1084 if (provider_name_len
== 0 || context_name_len
== 0) {
1086 * Application provider and context names MUST
1089 ret
= -LTTNG_ERR_INVALID
;
1093 provider_name
= zmalloc(provider_name_len
+ 1);
1094 if (!provider_name
) {
1095 ret
= -LTTNG_ERR_NOMEM
;
1098 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1101 context_name
= zmalloc(context_name_len
+ 1);
1102 if (!context_name
) {
1103 ret
= -LTTNG_ERR_NOMEM
;
1104 goto error_add_context
;
1106 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1109 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
1112 goto error_add_context
;
1115 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
1118 goto error_add_context
;
1123 * cmd_add_context assumes ownership of the provider and context
1126 ret
= cmd_add_context(cmd_ctx
->session
,
1127 cmd_ctx
->lsm
->domain
.type
,
1128 cmd_ctx
->lsm
->u
.context
.channel_name
,
1129 &cmd_ctx
->lsm
->u
.context
.ctx
,
1130 kernel_poll_pipe
[1]);
1132 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1133 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1135 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1136 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1142 case LTTNG_DISABLE_CHANNEL
:
1144 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1145 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1148 case LTTNG_DISABLE_EVENT
:
1152 * FIXME: handle filter; for now we just receive the filter's
1153 * bytecode along with the filter expression which are sent by
1154 * liblttng-ctl and discard them.
1156 * This fixes an issue where the client may block while sending
1157 * the filter payload and encounter an error because the session
1158 * daemon closes the socket without ever handling this data.
1160 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1161 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1164 char data
[LTTNG_FILTER_MAX_LEN
];
1166 DBG("Discarding disable event command payload of size %zu", count
);
1168 ret
= lttcomm_recv_unix_sock(sock
, data
,
1169 count
> sizeof(data
) ? sizeof(data
) : count
);
1174 count
-= (size_t) ret
;
1177 /* FIXME: passing packed structure to non-packed pointer */
1178 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1179 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1180 &cmd_ctx
->lsm
->u
.disable
.event
);
1183 case LTTNG_ENABLE_CHANNEL
:
1185 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1186 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1187 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
1188 &cmd_ctx
->lsm
->u
.channel
.chan
,
1189 kernel_poll_pipe
[1]);
1192 case LTTNG_TRACK_PID
:
1194 ret
= cmd_track_pid(cmd_ctx
->session
,
1195 cmd_ctx
->lsm
->domain
.type
,
1196 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1199 case LTTNG_UNTRACK_PID
:
1201 ret
= cmd_untrack_pid(cmd_ctx
->session
,
1202 cmd_ctx
->lsm
->domain
.type
,
1203 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1206 case LTTNG_ENABLE_EVENT
:
1208 struct lttng_event
*ev
= NULL
;
1209 struct lttng_event_exclusion
*exclusion
= NULL
;
1210 struct lttng_filter_bytecode
*bytecode
= NULL
;
1211 char *filter_expression
= NULL
;
1213 /* Handle exclusion events and receive it from the client. */
1214 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1215 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1217 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1218 (count
* LTTNG_SYMBOL_NAME_LEN
));
1220 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1224 DBG("Receiving var len exclusion event list from client ...");
1225 exclusion
->count
= count
;
1226 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
1227 count
* LTTNG_SYMBOL_NAME_LEN
);
1229 DBG("Nothing recv() from client var len data... continuing");
1232 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1237 /* Get filter expression from client. */
1238 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1239 size_t expression_len
=
1240 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1242 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1243 ret
= LTTNG_ERR_FILTER_INVAL
;
1248 filter_expression
= zmalloc(expression_len
);
1249 if (!filter_expression
) {
1251 ret
= LTTNG_ERR_FILTER_NOMEM
;
1255 /* Receive var. len. data */
1256 DBG("Receiving var len filter's expression from client ...");
1257 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
1260 DBG("Nothing recv() from client var len data... continuing");
1262 free(filter_expression
);
1264 ret
= LTTNG_ERR_FILTER_INVAL
;
1269 /* Handle filter and get bytecode from client. */
1270 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1271 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1273 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1274 ret
= LTTNG_ERR_FILTER_INVAL
;
1275 free(filter_expression
);
1280 bytecode
= zmalloc(bytecode_len
);
1282 free(filter_expression
);
1284 ret
= LTTNG_ERR_FILTER_NOMEM
;
1288 /* Receive var. len. data */
1289 DBG("Receiving var len filter's bytecode from client ...");
1290 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
1292 DBG("Nothing recv() from client var len data... continuing");
1294 free(filter_expression
);
1297 ret
= LTTNG_ERR_FILTER_INVAL
;
1301 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1302 free(filter_expression
);
1305 ret
= LTTNG_ERR_FILTER_INVAL
;
1310 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
1312 DBG("Failed to copy event: %s",
1313 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1314 free(filter_expression
);
1317 ret
= LTTNG_ERR_NOMEM
;
1322 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1323 /* Expect a userspace probe description. */
1324 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
1326 free(filter_expression
);
1329 lttng_event_destroy(ev
);
1334 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
1335 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1337 filter_expression
, bytecode
, exclusion
,
1338 kernel_poll_pipe
[1]);
1339 lttng_event_destroy(ev
);
1342 case LTTNG_LIST_TRACEPOINTS
:
1344 struct lttng_event
*events
;
1347 session_lock_list();
1348 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1349 session_unlock_list();
1350 if (nb_events
< 0) {
1351 /* Return value is a negative lttng_error_code. */
1357 * Setup lttng message with payload size set to the event list size in
1358 * bytes and then copy list into the llm payload.
1360 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1361 sizeof(struct lttng_event
) * nb_events
);
1371 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1373 struct lttng_event_field
*fields
;
1376 session_lock_list();
1377 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1379 session_unlock_list();
1380 if (nb_fields
< 0) {
1381 /* Return value is a negative lttng_error_code. */
1387 * Setup lttng message with payload size set to the event list size in
1388 * bytes and then copy list into the llm payload.
1390 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1391 sizeof(struct lttng_event_field
) * nb_fields
);
1401 case LTTNG_LIST_SYSCALLS
:
1403 struct lttng_event
*events
;
1406 nb_events
= cmd_list_syscalls(&events
);
1407 if (nb_events
< 0) {
1408 /* Return value is a negative lttng_error_code. */
1414 * Setup lttng message with payload size set to the event list size in
1415 * bytes and then copy list into the llm payload.
1417 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1418 sizeof(struct lttng_event
) * nb_events
);
1428 case LTTNG_LIST_TRACKER_PIDS
:
1430 int32_t *pids
= NULL
;
1433 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
1434 cmd_ctx
->lsm
->domain
.type
, &pids
);
1436 /* Return value is a negative lttng_error_code. */
1442 * Setup lttng message with payload size set to the event list size in
1443 * bytes and then copy list into the llm payload.
1445 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
1446 sizeof(int32_t) * nr_pids
);
1456 case LTTNG_SET_CONSUMER_URI
:
1459 struct lttng_uri
*uris
;
1461 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1462 len
= nb_uri
* sizeof(struct lttng_uri
);
1465 ret
= LTTNG_ERR_INVALID
;
1469 uris
= zmalloc(len
);
1471 ret
= LTTNG_ERR_FATAL
;
1475 /* Receive variable len data */
1476 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1477 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1479 DBG("No URIs received from client... continuing");
1481 ret
= LTTNG_ERR_SESSION_FAIL
;
1486 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1488 if (ret
!= LTTNG_OK
) {
1495 case LTTNG_START_TRACE
:
1498 * On the first start, if we have a kernel session and we have
1499 * enabled time or size-based rotations, we have to make sure
1500 * the kernel tracer supports it.
1502 if (!cmd_ctx
->session
->has_been_started
&& \
1503 cmd_ctx
->session
->kernel_session
&& \
1504 (cmd_ctx
->session
->rotate_timer_period
|| \
1505 cmd_ctx
->session
->rotate_size
) && \
1506 !check_rotate_compatible()) {
1507 DBG("Kernel tracer version is not compatible with the rotation feature");
1508 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1511 ret
= cmd_start_trace(cmd_ctx
->session
);
1514 case LTTNG_STOP_TRACE
:
1516 ret
= cmd_stop_trace(cmd_ctx
->session
);
1519 case LTTNG_CREATE_SESSION
:
1522 struct lttng_uri
*uris
= NULL
;
1524 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1525 len
= nb_uri
* sizeof(struct lttng_uri
);
1528 uris
= zmalloc(len
);
1530 ret
= LTTNG_ERR_FATAL
;
1534 /* Receive variable len data */
1535 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1536 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1538 DBG("No URIs received from client... continuing");
1540 ret
= LTTNG_ERR_SESSION_FAIL
;
1545 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1546 DBG("Creating session with ONE network URI is a bad call");
1547 ret
= LTTNG_ERR_SESSION_FAIL
;
1553 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
1554 &cmd_ctx
->creds
, 0);
1560 case LTTNG_DESTROY_SESSION
:
1562 ret
= cmd_destroy_session(cmd_ctx
->session
,
1563 notification_thread_handle
);
1566 case LTTNG_LIST_DOMAINS
:
1569 struct lttng_domain
*domains
= NULL
;
1571 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1573 /* Return value is a negative lttng_error_code. */
1578 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1579 nb_dom
* sizeof(struct lttng_domain
));
1589 case LTTNG_LIST_CHANNELS
:
1591 ssize_t payload_size
;
1592 struct lttng_channel
*channels
= NULL
;
1594 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1595 cmd_ctx
->session
, &channels
);
1596 if (payload_size
< 0) {
1597 /* Return value is a negative lttng_error_code. */
1598 ret
= -payload_size
;
1602 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1613 case LTTNG_LIST_EVENTS
:
1616 struct lttng_event
*events
= NULL
;
1617 struct lttcomm_event_command_header cmd_header
;
1620 memset(&cmd_header
, 0, sizeof(cmd_header
));
1621 /* Extended infos are included at the end of events */
1622 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1623 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1624 &events
, &total_size
);
1627 /* Return value is a negative lttng_error_code. */
1632 cmd_header
.nb_events
= nb_event
;
1633 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1634 &cmd_header
, sizeof(cmd_header
));
1644 case LTTNG_LIST_SESSIONS
:
1646 unsigned int nr_sessions
;
1647 void *sessions_payload
;
1650 session_lock_list();
1651 nr_sessions
= lttng_sessions_count(
1652 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1653 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1654 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
1655 sessions_payload
= zmalloc(payload_len
);
1657 if (!sessions_payload
) {
1658 session_unlock_list();
1663 cmd_list_lttng_sessions(sessions_payload
,
1664 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1665 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1666 session_unlock_list();
1668 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1670 free(sessions_payload
);
1679 case LTTNG_REGISTER_CONSUMER
:
1681 struct consumer_data
*cdata
;
1683 switch (cmd_ctx
->lsm
->domain
.type
) {
1684 case LTTNG_DOMAIN_KERNEL
:
1685 cdata
= &kconsumer_data
;
1688 ret
= LTTNG_ERR_UND
;
1692 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1693 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1696 case LTTNG_DATA_PENDING
:
1699 uint8_t pending_ret_byte
;
1701 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1706 * This function may returns 0 or 1 to indicate whether or not
1707 * there is data pending. In case of error, it should return an
1708 * LTTNG_ERR code. However, some code paths may still return
1709 * a nondescript error code, which we handle by returning an
1712 if (pending_ret
== 0 || pending_ret
== 1) {
1714 * ret will be set to LTTNG_OK at the end of
1717 } else if (pending_ret
< 0) {
1718 ret
= LTTNG_ERR_UNK
;
1725 pending_ret_byte
= (uint8_t) pending_ret
;
1727 /* 1 byte to return whether or not data is pending */
1728 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1729 &pending_ret_byte
, 1);
1738 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1740 struct lttcomm_lttng_output_id reply
;
1742 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1743 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
1744 if (ret
!= LTTNG_OK
) {
1748 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1754 /* Copy output list into message payload */
1758 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1760 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1761 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
1764 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1767 struct lttng_snapshot_output
*outputs
= NULL
;
1769 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1770 if (nb_output
< 0) {
1775 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1776 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1777 nb_output
* sizeof(struct lttng_snapshot_output
));
1787 case LTTNG_SNAPSHOT_RECORD
:
1789 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1790 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
1791 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1794 case LTTNG_CREATE_SESSION_SNAPSHOT
:
1797 struct lttng_uri
*uris
= NULL
;
1799 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1800 len
= nb_uri
* sizeof(struct lttng_uri
);
1803 uris
= zmalloc(len
);
1805 ret
= LTTNG_ERR_FATAL
;
1809 /* Receive variable len data */
1810 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1811 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1813 DBG("No URIs received from client... continuing");
1815 ret
= LTTNG_ERR_SESSION_FAIL
;
1820 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1821 DBG("Creating session with ONE network URI is a bad call");
1822 ret
= LTTNG_ERR_SESSION_FAIL
;
1828 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
1829 nb_uri
, &cmd_ctx
->creds
);
1833 case LTTNG_CREATE_SESSION_LIVE
:
1836 struct lttng_uri
*uris
= NULL
;
1838 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1839 len
= nb_uri
* sizeof(struct lttng_uri
);
1842 uris
= zmalloc(len
);
1844 ret
= LTTNG_ERR_FATAL
;
1848 /* Receive variable len data */
1849 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1850 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1852 DBG("No URIs received from client... continuing");
1854 ret
= LTTNG_ERR_SESSION_FAIL
;
1859 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1860 DBG("Creating session with ONE network URI is a bad call");
1861 ret
= LTTNG_ERR_SESSION_FAIL
;
1867 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
1868 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
1872 case LTTNG_SAVE_SESSION
:
1874 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1878 case LTTNG_SET_SESSION_SHM_PATH
:
1880 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1881 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1884 case LTTNG_REGENERATE_METADATA
:
1886 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1889 case LTTNG_REGENERATE_STATEDUMP
:
1891 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1894 case LTTNG_REGISTER_TRIGGER
:
1896 ret
= cmd_register_trigger(cmd_ctx
, sock
,
1897 notification_thread_handle
);
1900 case LTTNG_UNREGISTER_TRIGGER
:
1902 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
1903 notification_thread_handle
);
1906 case LTTNG_ROTATE_SESSION
:
1908 struct lttng_rotate_session_return rotate_return
;
1910 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1912 memset(&rotate_return
, 0, sizeof(rotate_return
));
1913 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1914 DBG("Kernel tracer version is not compatible with the rotation feature");
1915 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1919 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
1925 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
1926 sizeof(rotate_return
));
1935 case LTTNG_ROTATION_GET_INFO
:
1937 struct lttng_rotation_get_info_return get_info_return
;
1939 memset(&get_info_return
, 0, sizeof(get_info_return
));
1940 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
1941 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
1947 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
1948 sizeof(get_info_return
));
1957 case LTTNG_ROTATION_SET_SCHEDULE
:
1960 enum lttng_rotation_schedule_type schedule_type
;
1963 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1964 DBG("Kernel tracer version does not support session rotations");
1965 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1969 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
1970 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
1971 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
1973 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
1977 notification_thread_handle
);
1978 if (ret
!= LTTNG_OK
) {
1984 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1986 struct lttng_session_list_schedules_return schedules
= {
1987 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
1988 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
1989 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
1990 .size
.value
= cmd_ctx
->session
->rotate_size
,
1993 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2004 ret
= LTTNG_ERR_UND
;
2009 if (cmd_ctx
->llm
== NULL
) {
2010 DBG("Missing llm structure. Allocating one.");
2011 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2015 /* Set return code */
2016 cmd_ctx
->llm
->ret_code
= ret
;
2018 if (cmd_ctx
->session
) {
2019 session_unlock(cmd_ctx
->session
);
2020 session_put(cmd_ctx
->session
);
2022 if (need_tracing_session
) {
2023 session_unlock_list();
2026 assert(!rcu_read_ongoing());
2030 static int create_client_sock(void)
2032 int ret
, client_sock
;
2033 const mode_t old_umask
= umask(0);
2035 /* Create client tool unix socket */
2036 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2037 if (client_sock
< 0) {
2038 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2043 /* Set the cloexec flag */
2044 ret
= utils_set_fd_cloexec(client_sock
);
2046 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2047 "Continuing but note that the consumer daemon will have a "
2048 "reference to this socket on exec()", client_sock
);
2051 /* File permission MUST be 660 */
2052 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2054 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
2058 DBG("Created client socket (fd = %i)", client_sock
);
2065 static void cleanup_client_thread(void *data
)
2067 struct lttng_pipe
*quit_pipe
= data
;
2069 lttng_pipe_destroy(quit_pipe
);
2072 static void thread_init_cleanup(void *data
)
2074 set_thread_status(false);
2078 * This thread manage all clients request using the unix client socket for
2081 static void *thread_manage_clients(void *data
)
2083 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2085 uint32_t revents
, nb_fd
;
2086 struct command_ctx
*cmd_ctx
= NULL
;
2087 struct lttng_poll_event events
;
2088 int client_sock
= -1;
2089 struct lttng_pipe
*quit_pipe
= data
;
2090 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2092 DBG("[thread] Manage client started");
2094 is_root
= (getuid() == 0);
2096 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2097 client_sock
= create_client_sock();
2098 if (client_sock
< 0) {
2102 rcu_register_thread();
2104 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2106 health_code_update();
2108 ret
= lttcomm_listen_unix_sock(client_sock
);
2114 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2115 * more will be added to this poll set.
2117 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2119 goto error_create_poll
;
2122 /* Add the application registration socket */
2123 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2128 /* Add thread quit pipe */
2129 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2134 /* Set state as running. */
2135 set_thread_status(true);
2136 pthread_cleanup_pop(0);
2138 /* This testpoint is after we signal readiness to the parent. */
2139 if (testpoint(sessiond_thread_manage_clients
)) {
2143 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2147 health_code_update();
2150 const struct cmd_completion_handler
*cmd_completion_handler
;
2152 DBG("Accepting client command ...");
2154 /* Inifinite blocking call, waiting for transmission */
2156 health_poll_entry();
2157 ret
= lttng_poll_wait(&events
, -1);
2161 * Restart interrupted system call.
2163 if (errno
== EINTR
) {
2171 for (i
= 0; i
< nb_fd
; i
++) {
2172 revents
= LTTNG_POLL_GETEV(&events
, i
);
2173 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2175 health_code_update();
2178 /* No activity for this FD (poll implementation). */
2182 if (pollfd
== thread_quit_pipe_fd
) {
2186 /* Event on the registration socket */
2187 if (revents
& LPOLLIN
) {
2189 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2190 ERR("Client socket poll error");
2193 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2199 DBG("Wait for client response");
2201 health_code_update();
2203 sock
= lttcomm_accept_unix_sock(client_sock
);
2209 * Set the CLOEXEC flag. Return code is useless because either way, the
2212 (void) utils_set_fd_cloexec(sock
);
2214 /* Set socket option for credentials retrieval */
2215 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2220 /* Allocate context command to process the client request */
2221 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2222 if (cmd_ctx
== NULL
) {
2223 PERROR("zmalloc cmd_ctx");
2227 /* Allocate data buffer for reception */
2228 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2229 if (cmd_ctx
->lsm
== NULL
) {
2230 PERROR("zmalloc cmd_ctx->lsm");
2234 cmd_ctx
->llm
= NULL
;
2235 cmd_ctx
->session
= NULL
;
2237 health_code_update();
2240 * Data is received from the lttng client. The struct
2241 * lttcomm_session_msg (lsm) contains the command and data request of
2244 DBG("Receiving data from client ...");
2245 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2246 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2248 DBG("Nothing recv() from client... continuing");
2254 clean_command_ctx(&cmd_ctx
);
2258 health_code_update();
2260 // TODO: Validate cmd_ctx including sanity check for
2261 // security purpose.
2263 rcu_thread_online();
2265 * This function dispatch the work to the kernel or userspace tracer
2266 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2267 * informations for the client. The command context struct contains
2268 * everything this function may needs.
2270 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
2271 rcu_thread_offline();
2279 * TODO: Inform client somehow of the fatal error. At
2280 * this point, ret < 0 means that a zmalloc failed
2281 * (ENOMEM). Error detected but still accept
2282 * command, unless a socket error has been
2285 clean_command_ctx(&cmd_ctx
);
2289 cmd_completion_handler
= cmd_pop_completion_handler();
2290 if (cmd_completion_handler
) {
2291 enum lttng_error_code completion_code
;
2293 completion_code
= cmd_completion_handler
->run(
2294 cmd_completion_handler
->data
);
2295 if (completion_code
!= LTTNG_OK
) {
2296 clean_command_ctx(&cmd_ctx
);
2301 health_code_update();
2303 DBG("Sending response (size: %d, retcode: %s (%d))",
2304 cmd_ctx
->lttng_msg_size
,
2305 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2306 cmd_ctx
->llm
->ret_code
);
2307 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2309 ERR("Failed to send data back to client");
2312 /* End of transmission */
2319 clean_command_ctx(&cmd_ctx
);
2321 health_code_update();
2333 lttng_poll_clean(&events
);
2334 clean_command_ctx(&cmd_ctx
);
2338 unlink(config
.client_unix_sock_path
.value
);
2339 if (client_sock
>= 0) {
2340 ret
= close(client_sock
);
2348 ERR("Health error occurred in %s", __func__
);
2351 health_unregister(health_sessiond
);
2353 DBG("Client thread dying");
2355 rcu_unregister_thread();
2360 bool shutdown_client_thread(void *thread_data
)
2362 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2363 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2365 return notify_thread_pipe(write_fd
) == 1;
2368 struct lttng_thread
*launch_client_thread(void)
2370 bool thread_running
;
2371 struct lttng_pipe
*client_quit_pipe
;
2372 struct lttng_thread
*thread
;
2374 sem_init(&thread_state
.ready
, 0, 0);
2375 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2376 if (!client_quit_pipe
) {
2380 thread
= lttng_thread_create("Client management",
2381 thread_manage_clients
,
2382 shutdown_client_thread
,
2383 cleanup_client_thread
,
2390 * This thread is part of the threads that need to be fully
2391 * initialized before the session daemon is marked as "ready".
2393 thread_running
= wait_thread_status();
2394 if (!thread_running
) {
2395 lttng_thread_put(thread
);
2400 cleanup_client_thread(client_quit_pipe
);