2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <common/compat/getenv.h>
25 #include <common/unix.h>
26 #include <common/utils.h>
27 #include <lttng/userspace-probe-internal.h>
28 #include <lttng/event-internal.h>
29 #include <lttng/session-internal.h>
30 #include <lttng/session-descriptor-internal.h>
33 #include "lttng-sessiond.h"
37 #include "health-sessiond.h"
38 #include "testpoint.h"
40 #include "manage-consumer.h"
44 static struct thread_state
{
50 static void set_thread_status(bool running
)
52 DBG("Marking client thread's state as %s", running
? "running" : "error");
53 thread_state
.running
= running
;
54 sem_post(&thread_state
.ready
);
57 static bool wait_thread_status(void)
59 DBG("Waiting for client thread to be ready");
60 sem_wait(&thread_state
.ready
);
61 if (thread_state
.running
) {
62 DBG("Client thread is ready");
64 ERR("Initialization of client thread failed");
67 return thread_state
.running
;
71 * Setup the outgoing data buffer for the response (llm) by allocating the
72 * right amount of memory and copying the original information from the lsm
75 * Return 0 on success, negative value on error.
77 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
78 const void *payload_buf
, size_t payload_len
,
79 const void *cmd_header_buf
, size_t cmd_header_len
)
82 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
83 const size_t cmd_header_offset
= header_len
;
84 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
85 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
88 cmd_ctx
->llm
= zmalloc(total_msg_size
);
90 if (cmd_ctx
->llm
== NULL
) {
96 /* Copy common data */
97 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
98 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
99 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
100 cmd_ctx
->llm
->data_size
= payload_len
;
101 cmd_ctx
->lttng_msg_size
= total_msg_size
;
103 /* Copy command header */
104 if (cmd_header_len
) {
105 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
111 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
120 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
121 * exec or it will fail.
123 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
125 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
129 * Fork and exec a consumer daemon (consumerd).
131 * Return pid if successful else -1.
133 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
137 const char *consumer_to_use
;
138 const char *verbosity
;
141 DBG("Spawning consumerd");
148 if (config
.verbose_consumer
) {
149 verbosity
= "--verbose";
150 } else if (lttng_opt_quiet
) {
151 verbosity
= "--quiet";
156 switch (consumer_data
->type
) {
157 case LTTNG_CONSUMER_KERNEL
:
159 * Find out which consumerd to execute. We will first try the
160 * 64-bit path, then the sessiond's installation directory, and
161 * fallback on the 32-bit one,
163 DBG3("Looking for a kernel consumer at these locations:");
164 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
165 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
166 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
167 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
168 DBG3("Found location #1");
169 consumer_to_use
= config
.consumerd64_bin_path
.value
;
170 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
171 DBG3("Found location #2");
172 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
173 } else if (config
.consumerd32_bin_path
.value
&&
174 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
175 DBG3("Found location #3");
176 consumer_to_use
= config
.consumerd32_bin_path
.value
;
178 DBG("Could not find any valid consumerd executable");
182 DBG("Using kernel consumer at: %s", consumer_to_use
);
183 (void) execl(consumer_to_use
,
184 "lttng-consumerd", verbosity
, "-k",
185 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
186 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
187 "--group", config
.tracing_group_name
.value
,
190 case LTTNG_CONSUMER64_UST
:
192 if (config
.consumerd64_lib_dir
.value
) {
197 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
201 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
202 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
207 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
208 if (tmp
[0] != '\0') {
212 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
219 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
220 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
221 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
222 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
223 "--group", config
.tracing_group_name
.value
,
227 case LTTNG_CONSUMER32_UST
:
229 if (config
.consumerd32_lib_dir
.value
) {
234 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
238 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
239 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
244 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
245 if (tmp
[0] != '\0') {
249 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
256 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
257 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
258 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
259 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
260 "--group", config
.tracing_group_name
.value
,
265 ERR("unknown consumer type");
269 PERROR("Consumer execl()");
271 /* Reaching this point, we got a failure on our execl(). */
273 } else if (pid
> 0) {
276 PERROR("start consumer fork");
284 * Spawn the consumerd daemon and session daemon thread.
286 static int start_consumerd(struct consumer_data
*consumer_data
)
291 * Set the listen() state on the socket since there is a possible race
292 * between the exec() of the consumer daemon and this call if place in the
293 * consumer thread. See bug #366 for more details.
295 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
300 pthread_mutex_lock(&consumer_data
->pid_mutex
);
301 if (consumer_data
->pid
!= 0) {
302 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
306 ret
= spawn_consumerd(consumer_data
);
308 ERR("Spawning consumerd failed");
309 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
313 /* Setting up the consumer_data pid */
314 consumer_data
->pid
= ret
;
315 DBG2("Consumer pid %d", consumer_data
->pid
);
316 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
318 DBG2("Spawning consumer control thread");
319 ret
= spawn_consumer_thread(consumer_data
);
321 ERR("Fatal error spawning consumer control thread");
329 /* Cleanup already created sockets on error. */
330 if (consumer_data
->err_sock
>= 0) {
333 err
= close(consumer_data
->err_sock
);
335 PERROR("close consumer data error socket");
342 * Copy consumer output from the tracing session to the domain session. The
343 * function also applies the right modification on a per domain basis for the
344 * trace files destination directory.
346 * Should *NOT* be called with RCU read-side lock held.
348 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
351 const char *dir_name
;
352 struct consumer_output
*consumer
;
355 assert(session
->consumer
);
358 case LTTNG_DOMAIN_KERNEL
:
359 DBG3("Copying tracing session consumer output in kernel session");
361 * XXX: We should audit the session creation and what this function
362 * does "extra" in order to avoid a destroy since this function is used
363 * in the domain session creation (kernel and ust) only. Same for UST
366 if (session
->kernel_session
->consumer
) {
367 consumer_output_put(session
->kernel_session
->consumer
);
369 session
->kernel_session
->consumer
=
370 consumer_copy_output(session
->consumer
);
371 /* Ease our life a bit for the next part */
372 consumer
= session
->kernel_session
->consumer
;
373 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
375 case LTTNG_DOMAIN_JUL
:
376 case LTTNG_DOMAIN_LOG4J
:
377 case LTTNG_DOMAIN_PYTHON
:
378 case LTTNG_DOMAIN_UST
:
379 DBG3("Copying tracing session consumer output in UST session");
380 if (session
->ust_session
->consumer
) {
381 consumer_output_put(session
->ust_session
->consumer
);
383 session
->ust_session
->consumer
=
384 consumer_copy_output(session
->consumer
);
385 /* Ease our life a bit for the next part */
386 consumer
= session
->ust_session
->consumer
;
387 dir_name
= DEFAULT_UST_TRACE_DIR
;
390 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
394 /* Append correct directory to subdir */
395 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
396 sizeof(consumer
->domain_subdir
));
401 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
409 * Create an UST session and add it to the session ust list.
411 * Should *NOT* be called with RCU read-side lock held.
413 static int create_ust_session(struct ltt_session
*session
,
414 const struct lttng_domain
*domain
)
417 struct ltt_ust_session
*lus
= NULL
;
421 assert(session
->consumer
);
423 switch (domain
->type
) {
424 case LTTNG_DOMAIN_JUL
:
425 case LTTNG_DOMAIN_LOG4J
:
426 case LTTNG_DOMAIN_PYTHON
:
427 case LTTNG_DOMAIN_UST
:
430 ERR("Unknown UST domain on create session %d", domain
->type
);
431 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
435 DBG("Creating UST session");
437 lus
= trace_ust_create_session(session
->id
);
439 ret
= LTTNG_ERR_UST_SESS_FAIL
;
443 lus
->uid
= session
->uid
;
444 lus
->gid
= session
->gid
;
445 lus
->output_traces
= session
->output_traces
;
446 lus
->snapshot_mode
= session
->snapshot_mode
;
447 lus
->live_timer_interval
= session
->live_timer
;
448 session
->ust_session
= lus
;
449 if (session
->shm_path
[0]) {
450 strncpy(lus
->root_shm_path
, session
->shm_path
,
451 sizeof(lus
->root_shm_path
));
452 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
453 strncpy(lus
->shm_path
, session
->shm_path
,
454 sizeof(lus
->shm_path
));
455 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
456 strncat(lus
->shm_path
, "/ust",
457 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
459 /* Copy session output to the newly created UST session */
460 ret
= copy_session_consumer(domain
->type
, session
);
461 if (ret
!= LTTNG_OK
) {
469 session
->ust_session
= NULL
;
474 * Create a kernel tracer session then create the default channel.
476 static int create_kernel_session(struct ltt_session
*session
)
480 DBG("Creating kernel session");
482 ret
= kernel_create_session(session
);
484 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
488 /* Code flow safety */
489 assert(session
->kernel_session
);
491 /* Copy session output to the newly created Kernel session */
492 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
493 if (ret
!= LTTNG_OK
) {
497 session
->kernel_session
->uid
= session
->uid
;
498 session
->kernel_session
->gid
= session
->gid
;
499 session
->kernel_session
->output_traces
= session
->output_traces
;
500 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
505 trace_kernel_destroy_session(session
->kernel_session
);
506 session
->kernel_session
= NULL
;
512 * Count number of session permitted by uid/gid.
514 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
517 struct ltt_session
*session
;
518 const struct ltt_session_list
*session_list
= session_get_list();
520 DBG("Counting number of available session for UID %d GID %d",
522 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
523 if (!session_get(session
)) {
526 session_lock(session
);
527 /* Only count the sessions the user can control. */
528 if (session_access_ok(session
, uid
, gid
) &&
529 !session
->destroyed
) {
532 session_unlock(session
);
533 session_put(session
);
538 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
539 int *sock_error
, struct lttng_event
*event
)
542 struct lttng_userspace_probe_location
*probe_location
;
543 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
544 struct lttng_dynamic_buffer probe_location_buffer
;
545 struct lttng_buffer_view buffer_view
;
548 * Create a buffer to store the serialized version of the probe
551 lttng_dynamic_buffer_init(&probe_location_buffer
);
552 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
553 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
555 ret
= LTTNG_ERR_NOMEM
;
560 * Receive the probe location.
562 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
563 probe_location_buffer
.size
);
565 DBG("Nothing recv() from client var len data... continuing");
567 lttng_dynamic_buffer_reset(&probe_location_buffer
);
568 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
572 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
573 &probe_location_buffer
, 0, probe_location_buffer
.size
);
576 * Extract the probe location from the serialized version.
578 ret
= lttng_userspace_probe_location_create_from_buffer(
579 &buffer_view
, &probe_location
);
581 WARN("Failed to create a userspace probe location from the received buffer");
582 lttng_dynamic_buffer_reset( &probe_location_buffer
);
583 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
588 * Receive the file descriptor to the target binary from the client.
590 DBG("Receiving userspace probe target FD from client ...");
591 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
593 DBG("Nothing recv() from client userspace probe fd... continuing");
595 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
600 * Set the file descriptor received from the client through the unix
601 * socket in the probe location.
603 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
605 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
610 * From the kernel tracer's perspective, all userspace probe event types
611 * are all the same: a file and an offset.
613 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
614 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
615 ret
= lttng_userspace_probe_location_function_set_binary_fd(
618 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
619 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
623 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
628 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
632 /* Attach the probe location to the event. */
633 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
635 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
639 lttng_dynamic_buffer_reset(&probe_location_buffer
);
645 * Version of setup_lttng_msg() without command header.
647 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
648 void *payload_buf
, size_t payload_len
)
650 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
654 * Free memory of a command context structure.
656 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
658 DBG("Clean command context structure");
660 if ((*cmd_ctx
)->llm
) {
661 free((*cmd_ctx
)->llm
);
663 if ((*cmd_ctx
)->lsm
) {
664 free((*cmd_ctx
)->lsm
);
672 * Check if the current kernel tracer supports the session rotation feature.
673 * Return 1 if it does, 0 otherwise.
675 static int check_rotate_compatible(void)
679 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
680 DBG("Kernel tracer version is not compatible with the rotation feature");
688 * Send data on a unix socket using the liblttsessiondcomm API.
690 * Return lttcomm error code.
692 static int send_unix_sock(int sock
, void *buf
, size_t len
)
694 /* Check valid length */
699 return lttcomm_send_unix_sock(sock
, buf
, len
);
703 * Process the command requested by the lttng client within the command
704 * context structure. This function make sure that the return structure (llm)
705 * is set and ready for transmission before returning.
707 * Return any error encountered or 0 for success.
709 * "sock" is only used for special-case var. len data.
710 * A command may assume the ownership of the socket, in which case its value
711 * should be set to -1.
713 * Should *NOT* be called with RCU read-side lock held.
715 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
719 int need_tracing_session
= 1;
722 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
724 assert(!rcu_read_ongoing());
728 switch (cmd_ctx
->lsm
->cmd_type
) {
729 case LTTNG_CREATE_SESSION_EXT
:
730 case LTTNG_DESTROY_SESSION
:
731 case LTTNG_LIST_SESSIONS
:
732 case LTTNG_LIST_DOMAINS
:
733 case LTTNG_START_TRACE
:
734 case LTTNG_STOP_TRACE
:
735 case LTTNG_DATA_PENDING
:
736 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
737 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
738 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
739 case LTTNG_SNAPSHOT_RECORD
:
740 case LTTNG_SAVE_SESSION
:
741 case LTTNG_SET_SESSION_SHM_PATH
:
742 case LTTNG_REGENERATE_METADATA
:
743 case LTTNG_REGENERATE_STATEDUMP
:
744 case LTTNG_REGISTER_TRIGGER
:
745 case LTTNG_UNREGISTER_TRIGGER
:
746 case LTTNG_ROTATE_SESSION
:
747 case LTTNG_ROTATION_GET_INFO
:
748 case LTTNG_ROTATION_SET_SCHEDULE
:
749 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
756 if (config
.no_kernel
&& need_domain
757 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
759 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
761 ret
= LTTNG_ERR_KERN_NA
;
766 /* Deny register consumer if we already have a spawned consumer. */
767 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
768 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
769 if (kconsumer_data
.pid
> 0) {
770 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
771 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
774 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
778 * Check for command that don't needs to allocate a returned payload. We do
779 * this here so we don't have to make the call for no payload at each
782 switch(cmd_ctx
->lsm
->cmd_type
) {
783 case LTTNG_LIST_SESSIONS
:
784 case LTTNG_LIST_TRACEPOINTS
:
785 case LTTNG_LIST_TRACEPOINT_FIELDS
:
786 case LTTNG_LIST_DOMAINS
:
787 case LTTNG_LIST_CHANNELS
:
788 case LTTNG_LIST_EVENTS
:
789 case LTTNG_LIST_SYSCALLS
:
790 case LTTNG_LIST_TRACKER_PIDS
:
791 case LTTNG_DATA_PENDING
:
792 case LTTNG_ROTATE_SESSION
:
793 case LTTNG_ROTATION_GET_INFO
:
794 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
797 /* Setup lttng message with no payload */
798 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
800 /* This label does not try to unlock the session */
801 goto init_setup_error
;
805 /* Commands that DO NOT need a session. */
806 switch (cmd_ctx
->lsm
->cmd_type
) {
807 case LTTNG_CREATE_SESSION_EXT
:
808 case LTTNG_LIST_SESSIONS
:
809 case LTTNG_LIST_TRACEPOINTS
:
810 case LTTNG_LIST_SYSCALLS
:
811 case LTTNG_LIST_TRACEPOINT_FIELDS
:
812 case LTTNG_SAVE_SESSION
:
813 case LTTNG_REGISTER_TRIGGER
:
814 case LTTNG_UNREGISTER_TRIGGER
:
815 need_tracing_session
= 0;
818 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
820 * We keep the session list lock across _all_ commands
821 * for now, because the per-session lock does not
822 * handle teardown properly.
825 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
826 if (cmd_ctx
->session
== NULL
) {
827 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
830 /* Acquire lock for the session */
831 session_lock(cmd_ctx
->session
);
837 * Commands that need a valid session but should NOT create one if none
838 * exists. Instead of creating one and destroying it when the command is
839 * handled, process that right before so we save some round trip in useless
842 switch (cmd_ctx
->lsm
->cmd_type
) {
843 case LTTNG_DISABLE_CHANNEL
:
844 case LTTNG_DISABLE_EVENT
:
845 switch (cmd_ctx
->lsm
->domain
.type
) {
846 case LTTNG_DOMAIN_KERNEL
:
847 if (!cmd_ctx
->session
->kernel_session
) {
848 ret
= LTTNG_ERR_NO_CHANNEL
;
852 case LTTNG_DOMAIN_JUL
:
853 case LTTNG_DOMAIN_LOG4J
:
854 case LTTNG_DOMAIN_PYTHON
:
855 case LTTNG_DOMAIN_UST
:
856 if (!cmd_ctx
->session
->ust_session
) {
857 ret
= LTTNG_ERR_NO_CHANNEL
;
862 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
874 * Check domain type for specific "pre-action".
876 switch (cmd_ctx
->lsm
->domain
.type
) {
877 case LTTNG_DOMAIN_KERNEL
:
879 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
883 /* Kernel tracer check */
884 if (!kernel_tracer_is_initialized()) {
885 /* Basically, load kernel tracer modules */
886 ret
= init_kernel_tracer();
892 /* Consumer is in an ERROR state. Report back to client */
893 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
894 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
898 /* Need a session for kernel command */
899 if (need_tracing_session
) {
900 if (cmd_ctx
->session
->kernel_session
== NULL
) {
901 ret
= create_kernel_session(cmd_ctx
->session
);
902 if (ret
!= LTTNG_OK
) {
903 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
908 /* Start the kernel consumer daemon */
909 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
910 if (kconsumer_data
.pid
== 0 &&
911 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
912 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
913 ret
= start_consumerd(&kconsumer_data
);
915 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
918 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
920 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
924 * The consumer was just spawned so we need to add the socket to
925 * the consumer output of the session if exist.
927 ret
= consumer_create_socket(&kconsumer_data
,
928 cmd_ctx
->session
->kernel_session
->consumer
);
935 case LTTNG_DOMAIN_JUL
:
936 case LTTNG_DOMAIN_LOG4J
:
937 case LTTNG_DOMAIN_PYTHON
:
938 case LTTNG_DOMAIN_UST
:
940 if (!ust_app_supported()) {
941 ret
= LTTNG_ERR_NO_UST
;
944 /* Consumer is in an ERROR state. Report back to client */
945 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
946 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
950 if (need_tracing_session
) {
951 /* Create UST session if none exist. */
952 if (cmd_ctx
->session
->ust_session
== NULL
) {
953 ret
= create_ust_session(cmd_ctx
->session
,
954 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
));
955 if (ret
!= LTTNG_OK
) {
960 /* Start the UST consumer daemons */
962 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
963 if (config
.consumerd64_bin_path
.value
&&
964 ustconsumer64_data
.pid
== 0 &&
965 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
966 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
967 ret
= start_consumerd(&ustconsumer64_data
);
969 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
970 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
974 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
975 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
977 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
981 * Setup socket for consumer 64 bit. No need for atomic access
982 * since it was set above and can ONLY be set in this thread.
984 ret
= consumer_create_socket(&ustconsumer64_data
,
985 cmd_ctx
->session
->ust_session
->consumer
);
991 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
992 if (config
.consumerd32_bin_path
.value
&&
993 ustconsumer32_data
.pid
== 0 &&
994 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
995 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
996 ret
= start_consumerd(&ustconsumer32_data
);
998 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
999 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
1003 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
1004 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1006 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1010 * Setup socket for consumer 32 bit. No need for atomic access
1011 * since it was set above and can ONLY be set in this thread.
1013 ret
= consumer_create_socket(&ustconsumer32_data
,
1014 cmd_ctx
->session
->ust_session
->consumer
);
1026 /* Validate consumer daemon state when start/stop trace command */
1027 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
1028 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
1029 switch (cmd_ctx
->lsm
->domain
.type
) {
1030 case LTTNG_DOMAIN_NONE
:
1032 case LTTNG_DOMAIN_JUL
:
1033 case LTTNG_DOMAIN_LOG4J
:
1034 case LTTNG_DOMAIN_PYTHON
:
1035 case LTTNG_DOMAIN_UST
:
1036 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1037 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1041 case LTTNG_DOMAIN_KERNEL
:
1042 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1043 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1048 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1054 * Check that the UID or GID match that of the tracing session.
1055 * The root user can interact with all sessions.
1057 if (need_tracing_session
) {
1058 if (!session_access_ok(cmd_ctx
->session
,
1059 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1060 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
1061 cmd_ctx
->session
->destroyed
) {
1062 ret
= LTTNG_ERR_EPERM
;
1068 * Send relayd information to consumer as soon as we have a domain and a
1071 if (cmd_ctx
->session
&& need_domain
) {
1073 * Setup relayd if not done yet. If the relayd information was already
1074 * sent to the consumer, this call will gracefully return.
1076 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1077 if (ret
!= LTTNG_OK
) {
1082 /* Process by command type */
1083 switch (cmd_ctx
->lsm
->cmd_type
) {
1084 case LTTNG_ADD_CONTEXT
:
1087 * An LTTNG_ADD_CONTEXT command might have a supplementary
1088 * payload if the context being added is an application context.
1090 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
1091 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1092 char *provider_name
= NULL
, *context_name
= NULL
;
1093 size_t provider_name_len
=
1094 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
1095 size_t context_name_len
=
1096 cmd_ctx
->lsm
->u
.context
.context_name_len
;
1098 if (provider_name_len
== 0 || context_name_len
== 0) {
1100 * Application provider and context names MUST
1103 ret
= -LTTNG_ERR_INVALID
;
1107 provider_name
= zmalloc(provider_name_len
+ 1);
1108 if (!provider_name
) {
1109 ret
= -LTTNG_ERR_NOMEM
;
1112 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
1115 context_name
= zmalloc(context_name_len
+ 1);
1116 if (!context_name
) {
1117 ret
= -LTTNG_ERR_NOMEM
;
1118 goto error_add_context
;
1120 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1123 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1126 goto error_add_context
;
1129 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1132 goto error_add_context
;
1137 * cmd_add_context assumes ownership of the provider and context
1140 ret
= cmd_add_context(cmd_ctx
->session
,
1141 cmd_ctx
->lsm
->domain
.type
,
1142 cmd_ctx
->lsm
->u
.context
.channel_name
,
1143 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.context
.ctx
),
1144 kernel_poll_pipe
[1]);
1146 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1147 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1149 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
1150 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1156 case LTTNG_DISABLE_CHANNEL
:
1158 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1159 cmd_ctx
->lsm
->u
.disable
.channel_name
);
1162 case LTTNG_DISABLE_EVENT
:
1166 * FIXME: handle filter; for now we just receive the filter's
1167 * bytecode along with the filter expression which are sent by
1168 * liblttng-ctl and discard them.
1170 * This fixes an issue where the client may block while sending
1171 * the filter payload and encounter an error because the session
1172 * daemon closes the socket without ever handling this data.
1174 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
1175 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
1178 char data
[LTTNG_FILTER_MAX_LEN
];
1180 DBG("Discarding disable event command payload of size %zu", count
);
1182 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1183 count
> sizeof(data
) ? sizeof(data
) : count
);
1188 count
-= (size_t) ret
;
1191 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1192 cmd_ctx
->lsm
->u
.disable
.channel_name
,
1193 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.disable
.event
));
1196 case LTTNG_ENABLE_CHANNEL
:
1198 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
1199 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
1200 ret
= cmd_enable_channel(cmd_ctx
->session
,
1201 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1202 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.channel
.chan
),
1203 kernel_poll_pipe
[1]);
1206 case LTTNG_TRACK_PID
:
1208 ret
= cmd_track_pid(cmd_ctx
->session
,
1209 cmd_ctx
->lsm
->domain
.type
,
1210 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1213 case LTTNG_UNTRACK_PID
:
1215 ret
= cmd_untrack_pid(cmd_ctx
->session
,
1216 cmd_ctx
->lsm
->domain
.type
,
1217 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
1220 case LTTNG_ENABLE_EVENT
:
1222 struct lttng_event
*ev
= NULL
;
1223 struct lttng_event_exclusion
*exclusion
= NULL
;
1224 struct lttng_filter_bytecode
*bytecode
= NULL
;
1225 char *filter_expression
= NULL
;
1227 /* Handle exclusion events and receive it from the client. */
1228 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
1229 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
1231 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1232 (count
* LTTNG_SYMBOL_NAME_LEN
));
1234 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1238 DBG("Receiving var len exclusion event list from client ...");
1239 exclusion
->count
= count
;
1240 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1241 count
* LTTNG_SYMBOL_NAME_LEN
);
1243 DBG("Nothing recv() from client var len data... continuing");
1246 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1251 /* Get filter expression from client. */
1252 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
1253 size_t expression_len
=
1254 cmd_ctx
->lsm
->u
.enable
.expression_len
;
1256 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1257 ret
= LTTNG_ERR_FILTER_INVAL
;
1262 filter_expression
= zmalloc(expression_len
);
1263 if (!filter_expression
) {
1265 ret
= LTTNG_ERR_FILTER_NOMEM
;
1269 /* Receive var. len. data */
1270 DBG("Receiving var len filter's expression from client ...");
1271 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1274 DBG("Nothing recv() from client var len data... continuing");
1276 free(filter_expression
);
1278 ret
= LTTNG_ERR_FILTER_INVAL
;
1283 /* Handle filter and get bytecode from client. */
1284 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
1285 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
1287 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1288 ret
= LTTNG_ERR_FILTER_INVAL
;
1289 free(filter_expression
);
1294 bytecode
= zmalloc(bytecode_len
);
1296 free(filter_expression
);
1298 ret
= LTTNG_ERR_FILTER_NOMEM
;
1302 /* Receive var. len. data */
1303 DBG("Receiving var len filter's bytecode from client ...");
1304 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1306 DBG("Nothing recv() from client var len data... continuing");
1308 free(filter_expression
);
1311 ret
= LTTNG_ERR_FILTER_INVAL
;
1315 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1316 free(filter_expression
);
1319 ret
= LTTNG_ERR_FILTER_INVAL
;
1324 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.enable
.event
));
1326 DBG("Failed to copy event: %s",
1327 cmd_ctx
->lsm
->u
.enable
.event
.name
);
1328 free(filter_expression
);
1331 ret
= LTTNG_ERR_NOMEM
;
1336 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
1337 /* Expect a userspace probe description. */
1338 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1340 free(filter_expression
);
1343 lttng_event_destroy(ev
);
1348 ret
= cmd_enable_event(cmd_ctx
->session
,
1349 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
1350 cmd_ctx
->lsm
->u
.enable
.channel_name
,
1352 filter_expression
, bytecode
, exclusion
,
1353 kernel_poll_pipe
[1]);
1354 lttng_event_destroy(ev
);
1357 case LTTNG_LIST_TRACEPOINTS
:
1359 struct lttng_event
*events
;
1362 session_lock_list();
1363 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
1364 session_unlock_list();
1365 if (nb_events
< 0) {
1366 /* Return value is a negative lttng_error_code. */
1372 * Setup lttng message with payload size set to the event list size in
1373 * bytes and then copy list into the llm payload.
1375 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1376 sizeof(struct lttng_event
) * nb_events
);
1386 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1388 struct lttng_event_field
*fields
;
1391 session_lock_list();
1392 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
1394 session_unlock_list();
1395 if (nb_fields
< 0) {
1396 /* Return value is a negative lttng_error_code. */
1402 * Setup lttng message with payload size set to the event list size in
1403 * bytes and then copy list into the llm payload.
1405 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1406 sizeof(struct lttng_event_field
) * nb_fields
);
1416 case LTTNG_LIST_SYSCALLS
:
1418 struct lttng_event
*events
;
1421 nb_events
= cmd_list_syscalls(&events
);
1422 if (nb_events
< 0) {
1423 /* Return value is a negative lttng_error_code. */
1429 * Setup lttng message with payload size set to the event list size in
1430 * bytes and then copy list into the llm payload.
1432 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1433 sizeof(struct lttng_event
) * nb_events
);
1443 case LTTNG_LIST_TRACKER_PIDS
:
1445 int32_t *pids
= NULL
;
1448 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
1449 cmd_ctx
->lsm
->domain
.type
, &pids
);
1451 /* Return value is a negative lttng_error_code. */
1457 * Setup lttng message with payload size set to the event list size in
1458 * bytes and then copy list into the llm payload.
1460 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
1461 sizeof(int32_t) * nr_pids
);
1471 case LTTNG_SET_CONSUMER_URI
:
1474 struct lttng_uri
*uris
;
1476 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
1477 len
= nb_uri
* sizeof(struct lttng_uri
);
1480 ret
= LTTNG_ERR_INVALID
;
1484 uris
= zmalloc(len
);
1486 ret
= LTTNG_ERR_FATAL
;
1490 /* Receive variable len data */
1491 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1492 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1494 DBG("No URIs received from client... continuing");
1496 ret
= LTTNG_ERR_SESSION_FAIL
;
1501 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1503 if (ret
!= LTTNG_OK
) {
1510 case LTTNG_START_TRACE
:
1513 * On the first start, if we have a kernel session and we have
1514 * enabled time or size-based rotations, we have to make sure
1515 * the kernel tracer supports it.
1517 if (!cmd_ctx
->session
->has_been_started
&& \
1518 cmd_ctx
->session
->kernel_session
&& \
1519 (cmd_ctx
->session
->rotate_timer_period
|| \
1520 cmd_ctx
->session
->rotate_size
) && \
1521 !check_rotate_compatible()) {
1522 DBG("Kernel tracer version is not compatible with the rotation feature");
1523 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1526 ret
= cmd_start_trace(cmd_ctx
->session
);
1529 case LTTNG_STOP_TRACE
:
1531 ret
= cmd_stop_trace(cmd_ctx
->session
);
1534 case LTTNG_DESTROY_SESSION
:
1536 ret
= cmd_destroy_session(cmd_ctx
->session
,
1537 notification_thread_handle
,
1541 case LTTNG_LIST_DOMAINS
:
1544 struct lttng_domain
*domains
= NULL
;
1546 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1548 /* Return value is a negative lttng_error_code. */
1553 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1554 nb_dom
* sizeof(struct lttng_domain
));
1564 case LTTNG_LIST_CHANNELS
:
1566 ssize_t payload_size
;
1567 struct lttng_channel
*channels
= NULL
;
1569 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
1570 cmd_ctx
->session
, &channels
);
1571 if (payload_size
< 0) {
1572 /* Return value is a negative lttng_error_code. */
1573 ret
= -payload_size
;
1577 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1588 case LTTNG_LIST_EVENTS
:
1591 struct lttng_event
*events
= NULL
;
1592 struct lttcomm_event_command_header cmd_header
;
1595 memset(&cmd_header
, 0, sizeof(cmd_header
));
1596 /* Extended infos are included at the end of events */
1597 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
1598 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
1599 &events
, &total_size
);
1602 /* Return value is a negative lttng_error_code. */
1607 cmd_header
.nb_events
= nb_event
;
1608 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
1609 &cmd_header
, sizeof(cmd_header
));
1619 case LTTNG_LIST_SESSIONS
:
1621 unsigned int nr_sessions
;
1622 void *sessions_payload
;
1625 session_lock_list();
1626 nr_sessions
= lttng_sessions_count(
1627 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1628 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1630 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1631 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1632 sessions_payload
= zmalloc(payload_len
);
1634 if (!sessions_payload
) {
1635 session_unlock_list();
1640 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1641 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1642 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1643 session_unlock_list();
1645 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1647 free(sessions_payload
);
1656 case LTTNG_REGISTER_CONSUMER
:
1658 struct consumer_data
*cdata
;
1660 switch (cmd_ctx
->lsm
->domain
.type
) {
1661 case LTTNG_DOMAIN_KERNEL
:
1662 cdata
= &kconsumer_data
;
1665 ret
= LTTNG_ERR_UND
;
1669 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
1670 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
1673 case LTTNG_DATA_PENDING
:
1676 uint8_t pending_ret_byte
;
1678 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1683 * This function may returns 0 or 1 to indicate whether or not
1684 * there is data pending. In case of error, it should return an
1685 * LTTNG_ERR code. However, some code paths may still return
1686 * a nondescript error code, which we handle by returning an
1689 if (pending_ret
== 0 || pending_ret
== 1) {
1691 * ret will be set to LTTNG_OK at the end of
1694 } else if (pending_ret
< 0) {
1695 ret
= LTTNG_ERR_UNK
;
1702 pending_ret_byte
= (uint8_t) pending_ret
;
1704 /* 1 byte to return whether or not data is pending */
1705 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1706 &pending_ret_byte
, 1);
1715 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1717 uint32_t snapshot_id
;
1718 struct lttcomm_lttng_output_id reply
;
1720 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1721 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
),
1723 if (ret
!= LTTNG_OK
) {
1726 reply
.id
= snapshot_id
;
1728 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1734 /* Copy output list into message payload */
1738 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1740 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1741 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_output
.output
));
1744 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1747 struct lttng_snapshot_output
*outputs
= NULL
;
1749 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1750 if (nb_output
< 0) {
1755 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1756 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1757 nb_output
* sizeof(struct lttng_snapshot_output
));
1767 case LTTNG_SNAPSHOT_RECORD
:
1769 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1770 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->u
.snapshot_record
.output
),
1771 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
1774 case LTTNG_CREATE_SESSION_EXT
:
1776 struct lttng_dynamic_buffer payload
;
1777 struct lttng_session_descriptor
*return_descriptor
= NULL
;
1779 lttng_dynamic_buffer_init(&payload
);
1780 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
1781 if (ret
!= LTTNG_OK
) {
1785 ret
= lttng_session_descriptor_serialize(return_descriptor
,
1788 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
1789 lttng_session_descriptor_destroy(return_descriptor
);
1790 ret
= LTTNG_ERR_NOMEM
;
1793 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
1796 lttng_session_descriptor_destroy(return_descriptor
);
1797 ret
= LTTNG_ERR_NOMEM
;
1800 lttng_dynamic_buffer_reset(&payload
);
1801 lttng_session_descriptor_destroy(return_descriptor
);
1805 case LTTNG_SAVE_SESSION
:
1807 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
1811 case LTTNG_SET_SESSION_SHM_PATH
:
1813 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
1814 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
1817 case LTTNG_REGENERATE_METADATA
:
1819 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
1822 case LTTNG_REGENERATE_STATEDUMP
:
1824 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
1827 case LTTNG_REGISTER_TRIGGER
:
1829 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
1830 notification_thread_handle
);
1833 case LTTNG_UNREGISTER_TRIGGER
:
1835 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
1836 notification_thread_handle
);
1839 case LTTNG_ROTATE_SESSION
:
1841 struct lttng_rotate_session_return rotate_return
;
1843 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
1845 memset(&rotate_return
, 0, sizeof(rotate_return
));
1846 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1847 DBG("Kernel tracer version is not compatible with the rotation feature");
1848 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1852 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
1859 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
1860 sizeof(rotate_return
));
1869 case LTTNG_ROTATION_GET_INFO
:
1871 struct lttng_rotation_get_info_return get_info_return
;
1873 memset(&get_info_return
, 0, sizeof(get_info_return
));
1874 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
1875 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
1881 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
1882 sizeof(get_info_return
));
1891 case LTTNG_ROTATION_SET_SCHEDULE
:
1894 enum lttng_rotation_schedule_type schedule_type
;
1897 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
1898 DBG("Kernel tracer version does not support session rotations");
1899 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1903 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
1904 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
1905 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
1907 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
1911 notification_thread_handle
);
1912 if (ret
!= LTTNG_OK
) {
1918 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
1920 struct lttng_session_list_schedules_return schedules
= {
1921 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
1922 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
1923 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
1924 .size
.value
= cmd_ctx
->session
->rotate_size
,
1927 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
1938 ret
= LTTNG_ERR_UND
;
1943 if (cmd_ctx
->llm
== NULL
) {
1944 DBG("Missing llm structure. Allocating one.");
1945 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
1949 /* Set return code */
1950 cmd_ctx
->llm
->ret_code
= ret
;
1952 if (cmd_ctx
->session
) {
1953 session_unlock(cmd_ctx
->session
);
1954 session_put(cmd_ctx
->session
);
1955 cmd_ctx
->session
= NULL
;
1957 if (need_tracing_session
) {
1958 session_unlock_list();
1961 assert(!rcu_read_ongoing());
1965 static int create_client_sock(void)
1967 int ret
, client_sock
;
1968 const mode_t old_umask
= umask(0);
1970 /* Create client tool unix socket */
1971 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
1972 if (client_sock
< 0) {
1973 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
1978 /* Set the cloexec flag */
1979 ret
= utils_set_fd_cloexec(client_sock
);
1981 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
1982 "Continuing but note that the consumer daemon will have a "
1983 "reference to this socket on exec()", client_sock
);
1986 /* File permission MUST be 660 */
1987 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1989 ERR("Set file permissions failed: %s",
1990 config
.client_unix_sock_path
.value
);
1992 (void) lttcomm_close_unix_sock(client_sock
);
1996 DBG("Created client socket (fd = %i)", client_sock
);
2003 static void cleanup_client_thread(void *data
)
2005 struct lttng_pipe
*quit_pipe
= data
;
2007 lttng_pipe_destroy(quit_pipe
);
2010 static void thread_init_cleanup(void *data
)
2012 set_thread_status(false);
2016 * This thread manage all clients request using the unix client socket for
2019 static void *thread_manage_clients(void *data
)
2021 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2023 uint32_t revents
, nb_fd
;
2024 struct command_ctx
*cmd_ctx
= NULL
;
2025 struct lttng_poll_event events
;
2026 const int client_sock
= thread_state
.client_sock
;
2027 struct lttng_pipe
*quit_pipe
= data
;
2028 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2030 DBG("[thread] Manage client started");
2032 is_root
= (getuid() == 0);
2034 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2036 rcu_register_thread();
2038 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2040 health_code_update();
2042 ret
= lttcomm_listen_unix_sock(client_sock
);
2048 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2049 * more will be added to this poll set.
2051 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2053 goto error_create_poll
;
2056 /* Add the application registration socket */
2057 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2062 /* Add thread quit pipe */
2063 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2068 /* Set state as running. */
2069 set_thread_status(true);
2070 pthread_cleanup_pop(0);
2072 /* This testpoint is after we signal readiness to the parent. */
2073 if (testpoint(sessiond_thread_manage_clients
)) {
2077 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2081 health_code_update();
2084 const struct cmd_completion_handler
*cmd_completion_handler
;
2086 DBG("Accepting client command ...");
2088 /* Inifinite blocking call, waiting for transmission */
2090 health_poll_entry();
2091 ret
= lttng_poll_wait(&events
, -1);
2095 * Restart interrupted system call.
2097 if (errno
== EINTR
) {
2105 for (i
= 0; i
< nb_fd
; i
++) {
2106 revents
= LTTNG_POLL_GETEV(&events
, i
);
2107 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2109 health_code_update();
2111 if (pollfd
== thread_quit_pipe_fd
) {
2115 /* Event on the registration socket */
2116 if (revents
& LPOLLIN
) {
2118 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2119 ERR("Client socket poll error");
2122 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2128 DBG("Wait for client response");
2130 health_code_update();
2132 sock
= lttcomm_accept_unix_sock(client_sock
);
2138 * Set the CLOEXEC flag. Return code is useless because either way, the
2141 (void) utils_set_fd_cloexec(sock
);
2143 /* Set socket option for credentials retrieval */
2144 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2149 /* Allocate context command to process the client request */
2150 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
2151 if (cmd_ctx
== NULL
) {
2152 PERROR("zmalloc cmd_ctx");
2156 /* Allocate data buffer for reception */
2157 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
2158 if (cmd_ctx
->lsm
== NULL
) {
2159 PERROR("zmalloc cmd_ctx->lsm");
2163 cmd_ctx
->llm
= NULL
;
2164 cmd_ctx
->session
= NULL
;
2166 health_code_update();
2169 * Data is received from the lttng client. The struct
2170 * lttcomm_session_msg (lsm) contains the command and data request of
2173 DBG("Receiving data from client ...");
2174 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
2175 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
2177 DBG("Nothing recv() from client... continuing");
2183 clean_command_ctx(&cmd_ctx
);
2187 health_code_update();
2189 // TODO: Validate cmd_ctx including sanity check for
2190 // security purpose.
2192 rcu_thread_online();
2194 * This function dispatch the work to the kernel or userspace tracer
2195 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2196 * informations for the client. The command context struct contains
2197 * everything this function may needs.
2199 ret
= process_client_msg(cmd_ctx
, &sock
, &sock_error
);
2200 rcu_thread_offline();
2210 * TODO: Inform client somehow of the fatal error. At
2211 * this point, ret < 0 means that a zmalloc failed
2212 * (ENOMEM). Error detected but still accept
2213 * command, unless a socket error has been
2216 clean_command_ctx(&cmd_ctx
);
2220 cmd_completion_handler
= cmd_pop_completion_handler();
2221 if (cmd_completion_handler
) {
2222 enum lttng_error_code completion_code
;
2224 completion_code
= cmd_completion_handler
->run(
2225 cmd_completion_handler
->data
);
2226 if (completion_code
!= LTTNG_OK
) {
2227 clean_command_ctx(&cmd_ctx
);
2232 health_code_update();
2235 DBG("Sending response (size: %d, retcode: %s (%d))",
2236 cmd_ctx
->lttng_msg_size
,
2237 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
2238 cmd_ctx
->llm
->ret_code
);
2239 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2240 cmd_ctx
->lttng_msg_size
);
2242 ERR("Failed to send data back to client");
2245 /* End of transmission */
2253 clean_command_ctx(&cmd_ctx
);
2255 health_code_update();
2267 lttng_poll_clean(&events
);
2268 clean_command_ctx(&cmd_ctx
);
2272 unlink(config
.client_unix_sock_path
.value
);
2273 ret
= close(client_sock
);
2280 ERR("Health error occurred in %s", __func__
);
2283 health_unregister(health_sessiond
);
2285 DBG("Client thread dying");
2287 rcu_unregister_thread();
2292 bool shutdown_client_thread(void *thread_data
)
2294 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2295 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2297 return notify_thread_pipe(write_fd
) == 1;
2300 struct lttng_thread
*launch_client_thread(void)
2302 bool thread_running
;
2303 struct lttng_pipe
*client_quit_pipe
;
2304 struct lttng_thread
*thread
= NULL
;
2305 int client_sock_fd
= -1;
2307 sem_init(&thread_state
.ready
, 0, 0);
2308 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2309 if (!client_quit_pipe
) {
2313 client_sock_fd
= create_client_sock();
2314 if (client_sock_fd
< 0) {
2318 thread_state
.client_sock
= client_sock_fd
;
2319 thread
= lttng_thread_create("Client management",
2320 thread_manage_clients
,
2321 shutdown_client_thread
,
2322 cleanup_client_thread
,
2327 /* The client thread now owns the client sock fd and the quit pipe. */
2328 client_sock_fd
= -1;
2329 client_quit_pipe
= NULL
;
2332 * This thread is part of the threads that need to be fully
2333 * initialized before the session daemon is marked as "ready".
2335 thread_running
= wait_thread_status();
2336 if (!thread_running
) {
2341 if (client_sock_fd
>= 0) {
2342 if (close(client_sock_fd
)) {
2343 PERROR("Failed to close client socket");
2346 lttng_thread_put(thread
);
2347 cleanup_client_thread(client_quit_pipe
);