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
;
504 * Count number of session permitted by uid/gid.
506 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
509 struct ltt_session
*session
;
510 const struct ltt_session_list
*session_list
= session_get_list();
512 DBG("Counting number of available session for UID %d GID %d",
514 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
515 if (!session_get(session
)) {
518 session_lock(session
);
519 /* Only count the sessions the user can control. */
520 if (session_access_ok(session
, uid
, gid
) &&
521 !session
->destroyed
) {
524 session_unlock(session
);
525 session_put(session
);
530 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
531 int *sock_error
, struct lttng_event
*event
)
534 struct lttng_userspace_probe_location
*probe_location
;
535 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
536 struct lttng_dynamic_buffer probe_location_buffer
;
537 struct lttng_buffer_view buffer_view
;
540 * Create a buffer to store the serialized version of the probe
543 lttng_dynamic_buffer_init(&probe_location_buffer
);
544 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
545 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
547 ret
= LTTNG_ERR_NOMEM
;
552 * Receive the probe location.
554 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
555 probe_location_buffer
.size
);
557 DBG("Nothing recv() from client var len data... continuing");
559 lttng_dynamic_buffer_reset(&probe_location_buffer
);
560 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
564 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
565 &probe_location_buffer
, 0, probe_location_buffer
.size
);
568 * Extract the probe location from the serialized version.
570 ret
= lttng_userspace_probe_location_create_from_buffer(
571 &buffer_view
, &probe_location
);
573 WARN("Failed to create a userspace probe location from the received buffer");
574 lttng_dynamic_buffer_reset( &probe_location_buffer
);
575 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
580 * Receive the file descriptor to the target binary from the client.
582 DBG("Receiving userspace probe target FD from client ...");
583 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
585 DBG("Nothing recv() from client userspace probe fd... continuing");
587 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
592 * Set the file descriptor received from the client through the unix
593 * socket in the probe location.
595 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
597 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
602 * From the kernel tracer's perspective, all userspace probe event types
603 * are all the same: a file and an offset.
605 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
606 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
607 ret
= lttng_userspace_probe_location_function_set_binary_fd(
610 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
611 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
615 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
620 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
624 /* Attach the probe location to the event. */
625 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
627 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
631 lttng_dynamic_buffer_reset(&probe_location_buffer
);
637 * Version of setup_lttng_msg() without command header.
639 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
640 void *payload_buf
, size_t payload_len
)
642 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
646 * Free memory of a command context structure.
648 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
650 DBG("Clean command context structure");
652 if ((*cmd_ctx
)->llm
) {
653 free((*cmd_ctx
)->llm
);
655 if ((*cmd_ctx
)->lsm
) {
656 free((*cmd_ctx
)->lsm
);
664 * Check if the current kernel tracer supports the session rotation feature.
665 * Return 1 if it does, 0 otherwise.
667 static int check_rotate_compatible(void)
671 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
672 DBG("Kernel tracer version is not compatible with the rotation feature");
680 * Send data on a unix socket using the liblttsessiondcomm API.
682 * Return lttcomm error code.
684 static int send_unix_sock(int sock
, void *buf
, size_t len
)
686 /* Check valid length */
691 return lttcomm_send_unix_sock(sock
, buf
, len
);
695 * Process the command requested by the lttng client within the command
696 * context structure. This function make sure that the return structure (llm)
697 * is set and ready for transmission before returning.
699 * Return any error encountered or 0 for success.
701 * "sock" is only used for special-case var. len data.
703 * Should *NOT* be called with RCU read-side lock held.
705 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
709 int need_tracing_session
= 1;
712 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
714 assert(!rcu_read_ongoing());
718 switch (cmd_ctx
->lsm
->cmd_type
) {
719 case LTTNG_CREATE_SESSION
:
720 case LTTNG_CREATE_SESSION_SNAPSHOT
:
721 case LTTNG_CREATE_SESSION_LIVE
:
722 case LTTNG_DESTROY_SESSION
:
723 case LTTNG_LIST_SESSIONS
:
724 case LTTNG_LIST_DOMAINS
:
725 case LTTNG_START_TRACE
:
726 case LTTNG_STOP_TRACE
:
727 case LTTNG_DATA_PENDING
:
728 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
729 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
730 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
731 case LTTNG_SNAPSHOT_RECORD
:
732 case LTTNG_SAVE_SESSION
:
733 case LTTNG_SET_SESSION_SHM_PATH
:
734 case LTTNG_REGENERATE_METADATA
:
735 case LTTNG_REGENERATE_STATEDUMP
:
736 case LTTNG_REGISTER_TRIGGER
:
737 case LTTNG_UNREGISTER_TRIGGER
:
738 case LTTNG_ROTATE_SESSION
:
739 case LTTNG_ROTATION_GET_INFO
:
740 case LTTNG_ROTATION_SET_SCHEDULE
:
741 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
748 if (config
.no_kernel
&& need_domain
749 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
751 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
753 ret
= LTTNG_ERR_KERN_NA
;
758 /* Deny register consumer if we already have a spawned consumer. */
759 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
760 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
761 if (kconsumer_data
.pid
> 0) {
762 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
763 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
766 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
770 * Check for command that don't needs to allocate a returned payload. We do
771 * this here so we don't have to make the call for no payload at each
774 switch(cmd_ctx
->lsm
->cmd_type
) {
775 case LTTNG_LIST_SESSIONS
:
776 case LTTNG_LIST_TRACEPOINTS
:
777 case LTTNG_LIST_TRACEPOINT_FIELDS
:
778 case LTTNG_LIST_DOMAINS
:
779 case LTTNG_LIST_CHANNELS
:
780 case LTTNG_LIST_EVENTS
:
781 case LTTNG_LIST_SYSCALLS
:
782 case LTTNG_LIST_TRACKER_PIDS
:
783 case LTTNG_DATA_PENDING
:
784 case LTTNG_ROTATE_SESSION
:
785 case LTTNG_ROTATION_GET_INFO
:
786 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
789 /* Setup lttng message with no payload */
790 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
792 /* This label does not try to unlock the session */
793 goto init_setup_error
;
797 /* Commands that DO NOT need a session. */
798 switch (cmd_ctx
->lsm
->cmd_type
) {
799 case LTTNG_CREATE_SESSION
:
800 case LTTNG_CREATE_SESSION_SNAPSHOT
:
801 case LTTNG_CREATE_SESSION_LIVE
:
802 case LTTNG_LIST_SESSIONS
:
803 case LTTNG_LIST_TRACEPOINTS
:
804 case LTTNG_LIST_SYSCALLS
:
805 case LTTNG_LIST_TRACEPOINT_FIELDS
:
806 case LTTNG_SAVE_SESSION
:
807 case LTTNG_REGISTER_TRIGGER
:
808 case LTTNG_UNREGISTER_TRIGGER
:
809 need_tracing_session
= 0;
812 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
814 * We keep the session list lock across _all_ commands
815 * for now, because the per-session lock does not
816 * handle teardown properly.
819 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
820 if (cmd_ctx
->session
== NULL
) {
821 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
824 /* Acquire lock for the session */
825 session_lock(cmd_ctx
->session
);
831 * Commands that need a valid session but should NOT create one if none
832 * exists. Instead of creating one and destroying it when the command is
833 * handled, process that right before so we save some round trip in useless
836 switch (cmd_ctx
->lsm
->cmd_type
) {
837 case LTTNG_DISABLE_CHANNEL
:
838 case LTTNG_DISABLE_EVENT
:
839 switch (cmd_ctx
->lsm
->domain
.type
) {
840 case LTTNG_DOMAIN_KERNEL
:
841 if (!cmd_ctx
->session
->kernel_session
) {
842 ret
= LTTNG_ERR_NO_CHANNEL
;
846 case LTTNG_DOMAIN_JUL
:
847 case LTTNG_DOMAIN_LOG4J
:
848 case LTTNG_DOMAIN_PYTHON
:
849 case LTTNG_DOMAIN_UST
:
850 if (!cmd_ctx
->session
->ust_session
) {
851 ret
= LTTNG_ERR_NO_CHANNEL
;
856 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
868 * Check domain type for specific "pre-action".
870 switch (cmd_ctx
->lsm
->domain
.type
) {
871 case LTTNG_DOMAIN_KERNEL
:
873 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
877 /* Consumer is in an ERROR state. Report back to client */
878 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
879 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
883 /* Need a session for kernel command */
884 if (need_tracing_session
) {
885 if (cmd_ctx
->session
->kernel_session
== NULL
) {
886 ret
= create_kernel_session(cmd_ctx
->session
);
888 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
893 /* Start the kernel consumer daemon */
894 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
895 if (kconsumer_data
.pid
== 0 &&
896 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
897 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
898 ret
= start_consumerd(&kconsumer_data
);
900 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
903 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
905 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
909 * The consumer was just spawned so we need to add the socket to
910 * the consumer output of the session if exist.
912 ret
= consumer_create_socket(&kconsumer_data
,
913 cmd_ctx
->session
->kernel_session
->consumer
);
920 case LTTNG_DOMAIN_JUL
:
921 case LTTNG_DOMAIN_LOG4J
:
922 case LTTNG_DOMAIN_PYTHON
:
923 case LTTNG_DOMAIN_UST
:
925 if (!ust_app_supported()) {
926 ret
= LTTNG_ERR_NO_UST
;
929 /* Consumer is in an ERROR state. Report back to client */
930 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
931 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
935 if (need_tracing_session
) {
936 /* Create UST session if none exist. */
937 if (cmd_ctx
->session
->ust_session
== NULL
) {
938 ret
= create_ust_session(cmd_ctx
->session
,
939 &cmd_ctx
->lsm
->domain
);
940 if (ret
!= LTTNG_OK
) {
945 /* Start the UST consumer daemons */
947 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
948 if (config
.consumerd64_bin_path
.value
&&
949 ustconsumer64_data
.pid
== 0 &&
950 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
951 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
952 ret
= start_consumerd(&ustconsumer64_data
);
954 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
955 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
959 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
960 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
962 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
966 * Setup socket for consumer 64 bit. No need for atomic access
967 * since it was set above and can ONLY be set in this thread.
969 ret
= consumer_create_socket(&ustconsumer64_data
,
970 cmd_ctx
->session
->ust_session
->consumer
);
976 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
977 if (config
.consumerd32_bin_path
.value
&&
978 ustconsumer32_data
.pid
== 0 &&
979 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
980 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
981 ret
= start_consumerd(&ustconsumer32_data
);
983 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
984 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
988 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
989 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
991 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
995 * Setup socket for consumer 32 bit. No need for atomic access
996 * since it was set above and can ONLY be set in this thread.
998 ret
= consumer_create_socket(&ustconsumer32_data
,
999 cmd_ctx
->session
->ust_session
->consumer
);
1011 /* Validate consumer daemon state when start/stop trace command */
1012 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1013 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1014 switch (cmd_ctx
->lsm
->domain
.type
) {
1015 case LTTNG_DOMAIN_NONE
:
1017 case LTTNG_DOMAIN_JUL
:
1018 case LTTNG_DOMAIN_LOG4J
:
1019 case LTTNG_DOMAIN_PYTHON
:
1020 case LTTNG_DOMAIN_UST
:
1021 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1022 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1026 case LTTNG_DOMAIN_KERNEL
:
1027 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1028 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1033 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1039 * Check that the UID or GID match that of the tracing session.
1040 * The root user can interact with all sessions.
1042 if (need_tracing_session
) {
1043 if (!session_access_ok(cmd_ctx
->session
,
1044 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1045 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1046 cmd_ctx
->session
->destroyed
) {
1047 ret
= LTTNG_ERR_EPERM
;
1053 * Send relayd information to consumer as soon as we have a domain and a
1056 if (cmd_ctx
->session
&& need_domain
) {
1058 * Setup relayd if not done yet. If the relayd information was already
1059 * sent to the consumer, this call will gracefully return.
1061 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1062 if (ret
!= LTTNG_OK
) {
1067 /* Process by command type */
1068 switch (cmd_ctx
->lsm
->cmd_type
) {
1069 case LTTNG_ADD_CONTEXT
:
1072 * An LTTNG_ADD_CONTEXT command might have a supplementary
1073 * payload if the context being added is an application context.
1075 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1076 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1077 char *provider_name
= NULL
, *context_name
= NULL
;
1078 size_t provider_name_len
=
1079 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1080 size_t context_name_len
=
1081 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1083 if (provider_name_len
== 0 || context_name_len
== 0) {
1085 * Application provider and context names MUST
1088 ret
= -LTTNG_ERR_INVALID
;
1092 provider_name
= zmalloc(provider_name_len
+ 1);
1093 if (!provider_name
) {
1094 ret
= -LTTNG_ERR_NOMEM
;
1097 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1100 context_name
= zmalloc(context_name_len
+ 1);
1101 if (!context_name
) {
1102 ret
= -LTTNG_ERR_NOMEM
;
1103 goto error_add_context
;
1105 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1108 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
1111 goto error_add_context
;
1114 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
1117 goto error_add_context
;
1122 * cmd_add_context assumes ownership of the provider and context
1125 ret
= cmd_add_context(cmd_ctx
->session
,
1126 cmd_ctx
->lsm
->domain
.type
,
1127 cmd_ctx
->lsm
->u
.context
.channel_name
,
1128 &cmd_ctx
->lsm
->u
.context
.ctx
,
1129 kernel_poll_pipe
[1]);
1131 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1132 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1134 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1135 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1141 case LTTNG_DISABLE_CHANNEL
:
1143 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1144 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1147 case LTTNG_DISABLE_EVENT
:
1151 * FIXME: handle filter; for now we just receive the filter's
1152 * bytecode along with the filter expression which are sent by
1153 * liblttng-ctl and discard them.
1155 * This fixes an issue where the client may block while sending
1156 * the filter payload and encounter an error because the session
1157 * daemon closes the socket without ever handling this data.
1159 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1160 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1163 char data
[LTTNG_FILTER_MAX_LEN
];
1165 DBG("Discarding disable event command payload of size %zu", count
);
1167 ret
= lttcomm_recv_unix_sock(sock
, data
,
1168 count
> sizeof(data
) ? sizeof(data
) : count
);
1173 count
-= (size_t) ret
;
1176 /* FIXME: passing packed structure to non-packed pointer */
1177 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1178 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1179 &cmd_ctx
->lsm
->u
.disable
.event
);
1182 case LTTNG_ENABLE_CHANNEL
:
1184 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1185 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1186 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
1187 &cmd_ctx
->lsm
->u
.channel
.chan
,
1188 kernel_poll_pipe
[1]);
1191 case LTTNG_TRACK_PID
:
1193 ret
= cmd_track_pid(cmd_ctx
->session
,
1194 cmd_ctx
->lsm
->domain
.type
,
1195 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1198 case LTTNG_UNTRACK_PID
:
1200 ret
= cmd_untrack_pid(cmd_ctx
->session
,
1201 cmd_ctx
->lsm
->domain
.type
,
1202 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1205 case LTTNG_ENABLE_EVENT
:
1207 struct lttng_event
*ev
= NULL
;
1208 struct lttng_event_exclusion
*exclusion
= NULL
;
1209 struct lttng_filter_bytecode
*bytecode
= NULL
;
1210 char *filter_expression
= NULL
;
1212 /* Handle exclusion events and receive it from the client. */
1213 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1214 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1216 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1217 (count
* LTTNG_SYMBOL_NAME_LEN
));
1219 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1223 DBG("Receiving var len exclusion event list from client ...");
1224 exclusion
->count
= count
;
1225 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
1226 count
* LTTNG_SYMBOL_NAME_LEN
);
1228 DBG("Nothing recv() from client var len data... continuing");
1231 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1236 /* Get filter expression from client. */
1237 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1238 size_t expression_len
=
1239 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1241 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1242 ret
= LTTNG_ERR_FILTER_INVAL
;
1247 filter_expression
= zmalloc(expression_len
);
1248 if (!filter_expression
) {
1250 ret
= LTTNG_ERR_FILTER_NOMEM
;
1254 /* Receive var. len. data */
1255 DBG("Receiving var len filter's expression from client ...");
1256 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
1259 DBG("Nothing recv() from client var len data... continuing");
1261 free(filter_expression
);
1263 ret
= LTTNG_ERR_FILTER_INVAL
;
1268 /* Handle filter and get bytecode from client. */
1269 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1270 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1272 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1273 ret
= LTTNG_ERR_FILTER_INVAL
;
1274 free(filter_expression
);
1279 bytecode
= zmalloc(bytecode_len
);
1281 free(filter_expression
);
1283 ret
= LTTNG_ERR_FILTER_NOMEM
;
1287 /* Receive var. len. data */
1288 DBG("Receiving var len filter's bytecode from client ...");
1289 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
1291 DBG("Nothing recv() from client var len data... continuing");
1293 free(filter_expression
);
1296 ret
= LTTNG_ERR_FILTER_INVAL
;
1300 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1301 free(filter_expression
);
1304 ret
= LTTNG_ERR_FILTER_INVAL
;
1309 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
1311 DBG("Failed to copy event: %s",
1312 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1313 free(filter_expression
);
1316 ret
= LTTNG_ERR_NOMEM
;
1321 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1322 /* Expect a userspace probe description. */
1323 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
1325 free(filter_expression
);
1328 lttng_event_destroy(ev
);
1333 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
1334 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1336 filter_expression
, bytecode
, exclusion
,
1337 kernel_poll_pipe
[1]);
1338 lttng_event_destroy(ev
);
1341 case LTTNG_LIST_TRACEPOINTS
:
1343 struct lttng_event
*events
;
1346 session_lock_list();
1347 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1348 session_unlock_list();
1349 if (nb_events
< 0) {
1350 /* Return value is a negative lttng_error_code. */
1356 * Setup lttng message with payload size set to the event list size in
1357 * bytes and then copy list into the llm payload.
1359 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1360 sizeof(struct lttng_event
) * nb_events
);
1370 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1372 struct lttng_event_field
*fields
;
1375 session_lock_list();
1376 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1378 session_unlock_list();
1379 if (nb_fields
< 0) {
1380 /* Return value is a negative lttng_error_code. */
1386 * Setup lttng message with payload size set to the event list size in
1387 * bytes and then copy list into the llm payload.
1389 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1390 sizeof(struct lttng_event_field
) * nb_fields
);
1400 case LTTNG_LIST_SYSCALLS
:
1402 struct lttng_event
*events
;
1405 nb_events
= cmd_list_syscalls(&events
);
1406 if (nb_events
< 0) {
1407 /* Return value is a negative lttng_error_code. */
1413 * Setup lttng message with payload size set to the event list size in
1414 * bytes and then copy list into the llm payload.
1416 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1417 sizeof(struct lttng_event
) * nb_events
);
1427 case LTTNG_LIST_TRACKER_PIDS
:
1429 int32_t *pids
= NULL
;
1432 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
1433 cmd_ctx
->lsm
->domain
.type
, &pids
);
1435 /* Return value is a negative lttng_error_code. */
1441 * Setup lttng message with payload size set to the event list size in
1442 * bytes and then copy list into the llm payload.
1444 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
1445 sizeof(int32_t) * nr_pids
);
1455 case LTTNG_SET_CONSUMER_URI
:
1458 struct lttng_uri
*uris
;
1460 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1461 len
= nb_uri
* sizeof(struct lttng_uri
);
1464 ret
= LTTNG_ERR_INVALID
;
1468 uris
= zmalloc(len
);
1470 ret
= LTTNG_ERR_FATAL
;
1474 /* Receive variable len data */
1475 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1476 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1478 DBG("No URIs received from client... continuing");
1480 ret
= LTTNG_ERR_SESSION_FAIL
;
1485 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1487 if (ret
!= LTTNG_OK
) {
1494 case LTTNG_START_TRACE
:
1497 * On the first start, if we have a kernel session and we have
1498 * enabled time or size-based rotations, we have to make sure
1499 * the kernel tracer supports it.
1501 if (!cmd_ctx
->session
->has_been_started
&& \
1502 cmd_ctx
->session
->kernel_session
&& \
1503 (cmd_ctx
->session
->rotate_timer_period
|| \
1504 cmd_ctx
->session
->rotate_size
) && \
1505 !check_rotate_compatible()) {
1506 DBG("Kernel tracer version is not compatible with the rotation feature");
1507 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1510 ret
= cmd_start_trace(cmd_ctx
->session
);
1513 case LTTNG_STOP_TRACE
:
1515 ret
= cmd_stop_trace(cmd_ctx
->session
);
1518 case LTTNG_CREATE_SESSION
:
1521 struct lttng_uri
*uris
= NULL
;
1523 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1524 len
= nb_uri
* sizeof(struct lttng_uri
);
1527 uris
= zmalloc(len
);
1529 ret
= LTTNG_ERR_FATAL
;
1533 /* Receive variable len data */
1534 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1535 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1537 DBG("No URIs received from client... continuing");
1539 ret
= LTTNG_ERR_SESSION_FAIL
;
1544 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1545 DBG("Creating session with ONE network URI is a bad call");
1546 ret
= LTTNG_ERR_SESSION_FAIL
;
1552 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
1553 &cmd_ctx
->creds
, 0);
1559 case LTTNG_DESTROY_SESSION
:
1561 ret
= cmd_destroy_session(cmd_ctx
->session
,
1562 notification_thread_handle
);
1565 case LTTNG_LIST_DOMAINS
:
1568 struct lttng_domain
*domains
= NULL
;
1570 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1572 /* Return value is a negative lttng_error_code. */
1577 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1578 nb_dom
* sizeof(struct lttng_domain
));
1588 case LTTNG_LIST_CHANNELS
:
1590 ssize_t payload_size
;
1591 struct lttng_channel
*channels
= NULL
;
1593 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1594 cmd_ctx
->session
, &channels
);
1595 if (payload_size
< 0) {
1596 /* Return value is a negative lttng_error_code. */
1597 ret
= -payload_size
;
1601 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1612 case LTTNG_LIST_EVENTS
:
1615 struct lttng_event
*events
= NULL
;
1616 struct lttcomm_event_command_header cmd_header
;
1619 memset(&cmd_header
, 0, sizeof(cmd_header
));
1620 /* Extended infos are included at the end of events */
1621 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1622 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1623 &events
, &total_size
);
1626 /* Return value is a negative lttng_error_code. */
1631 cmd_header
.nb_events
= nb_event
;
1632 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1633 &cmd_header
, sizeof(cmd_header
));
1643 case LTTNG_LIST_SESSIONS
:
1645 unsigned int nr_sessions
;
1646 void *sessions_payload
;
1649 session_lock_list();
1650 nr_sessions
= lttng_sessions_count(
1651 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1652 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1653 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
1654 sessions_payload
= zmalloc(payload_len
);
1656 if (!sessions_payload
) {
1657 session_unlock_list();
1662 cmd_list_lttng_sessions(sessions_payload
,
1663 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1664 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1665 session_unlock_list();
1667 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1669 free(sessions_payload
);
1678 case LTTNG_REGISTER_CONSUMER
:
1680 struct consumer_data
*cdata
;
1682 switch (cmd_ctx
->lsm
->domain
.type
) {
1683 case LTTNG_DOMAIN_KERNEL
:
1684 cdata
= &kconsumer_data
;
1687 ret
= LTTNG_ERR_UND
;
1691 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1692 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1695 case LTTNG_DATA_PENDING
:
1698 uint8_t pending_ret_byte
;
1700 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1705 * This function may returns 0 or 1 to indicate whether or not
1706 * there is data pending. In case of error, it should return an
1707 * LTTNG_ERR code. However, some code paths may still return
1708 * a nondescript error code, which we handle by returning an
1711 if (pending_ret
== 0 || pending_ret
== 1) {
1713 * ret will be set to LTTNG_OK at the end of
1716 } else if (pending_ret
< 0) {
1717 ret
= LTTNG_ERR_UNK
;
1724 pending_ret_byte
= (uint8_t) pending_ret
;
1726 /* 1 byte to return whether or not data is pending */
1727 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1728 &pending_ret_byte
, 1);
1737 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1739 struct lttcomm_lttng_output_id reply
;
1741 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1742 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
1743 if (ret
!= LTTNG_OK
) {
1747 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1753 /* Copy output list into message payload */
1757 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1759 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1760 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
1763 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1766 struct lttng_snapshot_output
*outputs
= NULL
;
1768 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1769 if (nb_output
< 0) {
1774 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1775 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1776 nb_output
* sizeof(struct lttng_snapshot_output
));
1786 case LTTNG_SNAPSHOT_RECORD
:
1788 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1789 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
1790 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1793 case LTTNG_CREATE_SESSION_SNAPSHOT
:
1796 struct lttng_uri
*uris
= NULL
;
1798 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1799 len
= nb_uri
* sizeof(struct lttng_uri
);
1802 uris
= zmalloc(len
);
1804 ret
= LTTNG_ERR_FATAL
;
1808 /* Receive variable len data */
1809 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1810 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1812 DBG("No URIs received from client... continuing");
1814 ret
= LTTNG_ERR_SESSION_FAIL
;
1819 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1820 DBG("Creating session with ONE network URI is a bad call");
1821 ret
= LTTNG_ERR_SESSION_FAIL
;
1827 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
1828 nb_uri
, &cmd_ctx
->creds
);
1832 case LTTNG_CREATE_SESSION_LIVE
:
1835 struct lttng_uri
*uris
= NULL
;
1837 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1838 len
= nb_uri
* sizeof(struct lttng_uri
);
1841 uris
= zmalloc(len
);
1843 ret
= LTTNG_ERR_FATAL
;
1847 /* Receive variable len data */
1848 DBG("Waiting for %zu URIs from client ...", nb_uri
);
1849 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
1851 DBG("No URIs received from client... continuing");
1853 ret
= LTTNG_ERR_SESSION_FAIL
;
1858 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
1859 DBG("Creating session with ONE network URI is a bad call");
1860 ret
= LTTNG_ERR_SESSION_FAIL
;
1866 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
1867 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
1871 case LTTNG_SAVE_SESSION
:
1873 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1877 case LTTNG_SET_SESSION_SHM_PATH
:
1879 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1880 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1883 case LTTNG_REGENERATE_METADATA
:
1885 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1888 case LTTNG_REGENERATE_STATEDUMP
:
1890 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1893 case LTTNG_REGISTER_TRIGGER
:
1895 ret
= cmd_register_trigger(cmd_ctx
, sock
,
1896 notification_thread_handle
);
1899 case LTTNG_UNREGISTER_TRIGGER
:
1901 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
1902 notification_thread_handle
);
1905 case LTTNG_ROTATE_SESSION
:
1907 struct lttng_rotate_session_return rotate_return
;
1909 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1911 memset(&rotate_return
, 0, sizeof(rotate_return
));
1912 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1913 DBG("Kernel tracer version is not compatible with the rotation feature");
1914 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1918 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
1924 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
1925 sizeof(rotate_return
));
1934 case LTTNG_ROTATION_GET_INFO
:
1936 struct lttng_rotation_get_info_return get_info_return
;
1938 memset(&get_info_return
, 0, sizeof(get_info_return
));
1939 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
1940 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
1946 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
1947 sizeof(get_info_return
));
1956 case LTTNG_ROTATION_SET_SCHEDULE
:
1959 enum lttng_rotation_schedule_type schedule_type
;
1962 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1963 DBG("Kernel tracer version does not support session rotations");
1964 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1968 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
1969 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
1970 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
1972 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
1976 notification_thread_handle
);
1977 if (ret
!= LTTNG_OK
) {
1983 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1985 struct lttng_session_list_schedules_return schedules
= {
1986 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
1987 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
1988 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
1989 .size
.value
= cmd_ctx
->session
->rotate_size
,
1992 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2003 ret
= LTTNG_ERR_UND
;
2008 if (cmd_ctx
->llm
== NULL
) {
2009 DBG("Missing llm structure. Allocating one.");
2010 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2014 /* Set return code */
2015 cmd_ctx
->llm
->ret_code
= ret
;
2017 if (cmd_ctx
->session
) {
2018 session_unlock(cmd_ctx
->session
);
2019 session_put(cmd_ctx
->session
);
2021 if (need_tracing_session
) {
2022 session_unlock_list();
2025 assert(!rcu_read_ongoing());
2029 static int create_client_sock(void)
2031 int ret
, client_sock
;
2032 const mode_t old_umask
= umask(0);
2034 /* Create client tool unix socket */
2035 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2036 if (client_sock
< 0) {
2037 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2042 /* Set the cloexec flag */
2043 ret
= utils_set_fd_cloexec(client_sock
);
2045 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2046 "Continuing but note that the consumer daemon will have a "
2047 "reference to this socket on exec()", client_sock
);
2050 /* File permission MUST be 660 */
2051 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2053 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
2057 DBG("Created client socket (fd = %i)", client_sock
);
2064 static void cleanup_client_thread(void *data
)
2066 struct lttng_pipe
*quit_pipe
= data
;
2068 lttng_pipe_destroy(quit_pipe
);
2071 static void thread_init_cleanup(void *data
)
2073 set_thread_status(false);
2077 * This thread manage all clients request using the unix client socket for
2080 static void *thread_manage_clients(void *data
)
2082 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2084 uint32_t revents
, nb_fd
;
2085 struct command_ctx
*cmd_ctx
= NULL
;
2086 struct lttng_poll_event events
;
2087 int client_sock
= -1;
2088 struct lttng_pipe
*quit_pipe
= data
;
2089 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2091 DBG("[thread] Manage client started");
2093 is_root
= (getuid() == 0);
2095 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2096 client_sock
= create_client_sock();
2097 if (client_sock
< 0) {
2101 rcu_register_thread();
2103 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2105 health_code_update();
2107 ret
= lttcomm_listen_unix_sock(client_sock
);
2113 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2114 * more will be added to this poll set.
2116 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2118 goto error_create_poll
;
2121 /* Add the application registration socket */
2122 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2127 /* Add thread quit pipe */
2128 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2133 /* Set state as running. */
2134 set_thread_status(true);
2135 pthread_cleanup_pop(0);
2137 /* This testpoint is after we signal readiness to the parent. */
2138 if (testpoint(sessiond_thread_manage_clients
)) {
2142 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2146 health_code_update();
2149 const struct cmd_completion_handler
*cmd_completion_handler
;
2151 DBG("Accepting client command ...");
2153 /* Inifinite blocking call, waiting for transmission */
2155 health_poll_entry();
2156 ret
= lttng_poll_wait(&events
, -1);
2160 * Restart interrupted system call.
2162 if (errno
== EINTR
) {
2170 for (i
= 0; i
< nb_fd
; i
++) {
2171 revents
= LTTNG_POLL_GETEV(&events
, i
);
2172 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2174 health_code_update();
2177 /* No activity for this FD (poll implementation). */
2181 if (pollfd
== thread_quit_pipe_fd
) {
2185 /* Event on the registration socket */
2186 if (revents
& LPOLLIN
) {
2188 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2189 ERR("Client socket poll error");
2192 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2198 DBG("Wait for client response");
2200 health_code_update();
2202 sock
= lttcomm_accept_unix_sock(client_sock
);
2208 * Set the CLOEXEC flag. Return code is useless because either way, the
2211 (void) utils_set_fd_cloexec(sock
);
2213 /* Set socket option for credentials retrieval */
2214 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2219 /* Allocate context command to process the client request */
2220 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2221 if (cmd_ctx
== NULL
) {
2222 PERROR("zmalloc cmd_ctx");
2226 /* Allocate data buffer for reception */
2227 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2228 if (cmd_ctx
->lsm
== NULL
) {
2229 PERROR("zmalloc cmd_ctx->lsm");
2233 cmd_ctx
->llm
= NULL
;
2234 cmd_ctx
->session
= NULL
;
2236 health_code_update();
2239 * Data is received from the lttng client. The struct
2240 * lttcomm_session_msg (lsm) contains the command and data request of
2243 DBG("Receiving data from client ...");
2244 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2245 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2247 DBG("Nothing recv() from client... continuing");
2253 clean_command_ctx(&cmd_ctx
);
2257 health_code_update();
2259 // TODO: Validate cmd_ctx including sanity check for
2260 // security purpose.
2262 rcu_thread_online();
2264 * This function dispatch the work to the kernel or userspace tracer
2265 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2266 * informations for the client. The command context struct contains
2267 * everything this function may needs.
2269 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
2270 rcu_thread_offline();
2278 * TODO: Inform client somehow of the fatal error. At
2279 * this point, ret < 0 means that a zmalloc failed
2280 * (ENOMEM). Error detected but still accept
2281 * command, unless a socket error has been
2284 clean_command_ctx(&cmd_ctx
);
2288 cmd_completion_handler
= cmd_pop_completion_handler();
2289 if (cmd_completion_handler
) {
2290 enum lttng_error_code completion_code
;
2292 completion_code
= cmd_completion_handler
->run(
2293 cmd_completion_handler
->data
);
2294 if (completion_code
!= LTTNG_OK
) {
2295 clean_command_ctx(&cmd_ctx
);
2300 health_code_update();
2302 DBG("Sending response (size: %d, retcode: %s (%d))",
2303 cmd_ctx
->lttng_msg_size
,
2304 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2305 cmd_ctx
->llm
->ret_code
);
2306 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2308 ERR("Failed to send data back to client");
2311 /* End of transmission */
2318 clean_command_ctx(&cmd_ctx
);
2320 health_code_update();
2332 lttng_poll_clean(&events
);
2333 clean_command_ctx(&cmd_ctx
);
2337 unlink(config
.client_unix_sock_path
.value
);
2338 if (client_sock
>= 0) {
2339 ret
= close(client_sock
);
2347 ERR("Health error occurred in %s", __func__
);
2350 health_unregister(health_sessiond
);
2352 DBG("Client thread dying");
2354 rcu_unregister_thread();
2359 bool shutdown_client_thread(void *thread_data
)
2361 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2362 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2364 return notify_thread_pipe(write_fd
) == 1;
2367 struct lttng_thread
*launch_client_thread(void)
2369 bool thread_running
;
2370 struct lttng_pipe
*client_quit_pipe
;
2371 struct lttng_thread
*thread
;
2373 sem_init(&thread_state
.ready
, 0, 0);
2374 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2375 if (!client_quit_pipe
) {
2379 thread
= lttng_thread_create("Client management",
2380 thread_manage_clients
,
2381 shutdown_client_thread
,
2382 cleanup_client_thread
,
2389 * This thread is part of the threads that need to be fully
2390 * initialized before the session daemon is marked as "ready".
2392 thread_running
= wait_thread_status();
2393 if (!thread_running
) {
2394 lttng_thread_put(thread
);
2399 cleanup_client_thread(client_quit_pipe
);