2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "common/buffer-view.h"
11 #include "common/compat/socket.h"
12 #include "common/dynamic-buffer.h"
13 #include "common/dynamic-array.h"
14 #include "common/payload.h"
15 #include "common/payload-view.h"
16 #include "common/fd-handle.h"
17 #include "common/sessiond-comm/sessiond-comm.h"
18 #include "common/payload.h"
19 #include "common/payload-view.h"
20 #include "lttng/lttng-error.h"
21 #include "lttng/tracker.h"
22 #include <common/compat/getenv.h>
23 #include <common/tracker.h>
24 #include <common/unix.h>
25 #include <common/utils.h>
26 #include <lttng/event-internal.h>
27 #include <lttng/session-descriptor-internal.h>
28 #include <lttng/session-internal.h>
29 #include <lttng/userspace-probe-internal.h>
37 #include "lttng-sessiond.h"
41 #include "health-sessiond.h"
42 #include "testpoint.h"
44 #include "manage-consumer.h"
49 static struct thread_state
{
55 static void set_thread_status(bool running
)
57 DBG("Marking client thread's state as %s", running
? "running" : "error");
58 thread_state
.running
= running
;
59 sem_post(&thread_state
.ready
);
62 static bool wait_thread_status(void)
64 DBG("Waiting for client thread to be ready");
65 sem_wait(&thread_state
.ready
);
66 if (thread_state
.running
) {
67 DBG("Client thread is ready");
69 ERR("Initialization of client thread failed");
72 return thread_state
.running
;
76 * Setup the outgoing data buffer for the response (llm) by allocating the
77 * right amount of memory and copying the original information from the lsm
80 * Return 0 on success, negative value on error.
82 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
83 const void *payload_buf
, size_t payload_len
,
84 const void *cmd_header_buf
, size_t cmd_header_len
)
87 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
88 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
89 const struct lttcomm_lttng_msg llm
= {
90 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
91 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
92 .cmd_header_size
= cmd_header_len
,
93 .data_size
= payload_len
,
96 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
101 lttng_dynamic_pointer_array_clear(&cmd_ctx
->reply_payload
._fd_handles
);
103 cmd_ctx
->lttng_msg_size
= total_msg_size
;
105 /* Append reply header. */
106 ret
= lttng_dynamic_buffer_append(
107 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
112 /* Append command header. */
113 if (cmd_header_len
) {
114 ret
= lttng_dynamic_buffer_append(
115 &cmd_ctx
->reply_payload
.buffer
, cmd_header_buf
,
122 /* Append payload. */
124 ret
= lttng_dynamic_buffer_append(
125 &cmd_ctx
->reply_payload
.buffer
, payload_buf
,
136 static int setup_empty_lttng_msg(struct command_ctx
*cmd_ctx
)
139 const struct lttcomm_lttng_msg llm
= {};
141 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
146 /* Append place-holder reply header. */
147 ret
= lttng_dynamic_buffer_append(
148 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
153 cmd_ctx
->lttng_msg_size
= sizeof(llm
);
158 static void update_lttng_msg(struct command_ctx
*cmd_ctx
, size_t cmd_header_len
,
161 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
162 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
163 const struct lttcomm_lttng_msg llm
= {
164 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
165 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
166 .cmd_header_size
= cmd_header_len
,
167 .data_size
= payload_len
,
169 struct lttcomm_lttng_msg
*p_llm
;
171 assert(cmd_ctx
->reply_payload
.buffer
.size
>= sizeof(llm
));
173 p_llm
= (typeof(p_llm
)) cmd_ctx
->reply_payload
.buffer
.data
;
175 /* Update existing header. */
176 memcpy(p_llm
, &llm
, sizeof(llm
));
178 cmd_ctx
->lttng_msg_size
= total_msg_size
;
182 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
183 * exec or it will fail.
185 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
187 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
191 * Fork and exec a consumer daemon (consumerd).
193 * Return pid if successful else -1.
195 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
199 const char *consumer_to_use
;
200 const char *verbosity
;
203 DBG("Spawning consumerd");
210 if (config
.verbose_consumer
) {
211 verbosity
= "--verbose";
212 } else if (lttng_opt_quiet
) {
213 verbosity
= "--quiet";
218 switch (consumer_data
->type
) {
219 case LTTNG_CONSUMER_KERNEL
:
221 * Find out which consumerd to execute. We will first try the
222 * 64-bit path, then the sessiond's installation directory, and
223 * fallback on the 32-bit one,
225 DBG3("Looking for a kernel consumer at these locations:");
226 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
227 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
228 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
229 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
230 DBG3("Found location #1");
231 consumer_to_use
= config
.consumerd64_bin_path
.value
;
232 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
233 DBG3("Found location #2");
234 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
235 } else if (config
.consumerd32_bin_path
.value
&&
236 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
237 DBG3("Found location #3");
238 consumer_to_use
= config
.consumerd32_bin_path
.value
;
240 DBG("Could not find any valid consumerd executable");
244 DBG("Using kernel consumer at: %s", consumer_to_use
);
245 (void) execl(consumer_to_use
,
246 "lttng-consumerd", verbosity
, "-k",
247 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
248 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
249 "--group", config
.tracing_group_name
.value
,
252 case LTTNG_CONSUMER64_UST
:
254 if (config
.consumerd64_lib_dir
.value
) {
259 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
263 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
264 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
269 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
270 if (tmp
[0] != '\0') {
274 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
281 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
282 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
283 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
284 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
285 "--group", config
.tracing_group_name
.value
,
289 case LTTNG_CONSUMER32_UST
:
291 if (config
.consumerd32_lib_dir
.value
) {
296 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
300 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
301 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
306 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
307 if (tmp
[0] != '\0') {
311 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
318 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
319 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
320 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
321 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
322 "--group", config
.tracing_group_name
.value
,
327 ERR("unknown consumer type");
331 PERROR("Consumer execl()");
333 /* Reaching this point, we got a failure on our execl(). */
335 } else if (pid
> 0) {
338 PERROR("start consumer fork");
346 * Spawn the consumerd daemon and session daemon thread.
348 static int start_consumerd(struct consumer_data
*consumer_data
)
353 * Set the listen() state on the socket since there is a possible race
354 * between the exec() of the consumer daemon and this call if place in the
355 * consumer thread. See bug #366 for more details.
357 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
362 pthread_mutex_lock(&consumer_data
->pid_mutex
);
363 if (consumer_data
->pid
!= 0) {
364 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
368 ret
= spawn_consumerd(consumer_data
);
370 ERR("Spawning consumerd failed");
371 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
375 /* Setting up the consumer_data pid */
376 consumer_data
->pid
= ret
;
377 DBG2("Consumer pid %d", consumer_data
->pid
);
378 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
380 DBG2("Spawning consumer control thread");
381 ret
= spawn_consumer_thread(consumer_data
);
383 ERR("Fatal error spawning consumer control thread");
391 /* Cleanup already created sockets on error. */
392 if (consumer_data
->err_sock
>= 0) {
395 err
= close(consumer_data
->err_sock
);
397 PERROR("close consumer data error socket");
404 * Copy consumer output from the tracing session to the domain session. The
405 * function also applies the right modification on a per domain basis for the
406 * trace files destination directory.
408 * Should *NOT* be called with RCU read-side lock held.
410 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
413 const char *dir_name
;
414 struct consumer_output
*consumer
;
417 assert(session
->consumer
);
420 case LTTNG_DOMAIN_KERNEL
:
421 DBG3("Copying tracing session consumer output in kernel session");
423 * XXX: We should audit the session creation and what this function
424 * does "extra" in order to avoid a destroy since this function is used
425 * in the domain session creation (kernel and ust) only. Same for UST
428 if (session
->kernel_session
->consumer
) {
429 consumer_output_put(session
->kernel_session
->consumer
);
431 session
->kernel_session
->consumer
=
432 consumer_copy_output(session
->consumer
);
433 /* Ease our life a bit for the next part */
434 consumer
= session
->kernel_session
->consumer
;
435 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
437 case LTTNG_DOMAIN_JUL
:
438 case LTTNG_DOMAIN_LOG4J
:
439 case LTTNG_DOMAIN_PYTHON
:
440 case LTTNG_DOMAIN_UST
:
441 DBG3("Copying tracing session consumer output in UST session");
442 if (session
->ust_session
->consumer
) {
443 consumer_output_put(session
->ust_session
->consumer
);
445 session
->ust_session
->consumer
=
446 consumer_copy_output(session
->consumer
);
447 /* Ease our life a bit for the next part */
448 consumer
= session
->ust_session
->consumer
;
449 dir_name
= DEFAULT_UST_TRACE_DIR
;
452 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
456 /* Append correct directory to subdir */
457 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
458 sizeof(consumer
->domain_subdir
));
463 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
471 * Create an UST session and add it to the session ust list.
473 * Should *NOT* be called with RCU read-side lock held.
475 static int create_ust_session(struct ltt_session
*session
,
476 const struct lttng_domain
*domain
)
479 struct ltt_ust_session
*lus
= NULL
;
483 assert(session
->consumer
);
485 switch (domain
->type
) {
486 case LTTNG_DOMAIN_JUL
:
487 case LTTNG_DOMAIN_LOG4J
:
488 case LTTNG_DOMAIN_PYTHON
:
489 case LTTNG_DOMAIN_UST
:
492 ERR("Unknown UST domain on create session %d", domain
->type
);
493 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
497 DBG("Creating UST session");
499 lus
= trace_ust_create_session(session
->id
);
501 ret
= LTTNG_ERR_UST_SESS_FAIL
;
505 lus
->uid
= session
->uid
;
506 lus
->gid
= session
->gid
;
507 lus
->output_traces
= session
->output_traces
;
508 lus
->snapshot_mode
= session
->snapshot_mode
;
509 lus
->live_timer_interval
= session
->live_timer
;
510 session
->ust_session
= lus
;
511 if (session
->shm_path
[0]) {
512 strncpy(lus
->root_shm_path
, session
->shm_path
,
513 sizeof(lus
->root_shm_path
));
514 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
515 strncpy(lus
->shm_path
, session
->shm_path
,
516 sizeof(lus
->shm_path
));
517 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
518 strncat(lus
->shm_path
, "/ust",
519 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
521 /* Copy session output to the newly created UST session */
522 ret
= copy_session_consumer(domain
->type
, session
);
523 if (ret
!= LTTNG_OK
) {
531 session
->ust_session
= NULL
;
536 * Create a kernel tracer session then create the default channel.
538 static int create_kernel_session(struct ltt_session
*session
)
542 DBG("Creating kernel session");
544 ret
= kernel_create_session(session
);
546 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
550 /* Code flow safety */
551 assert(session
->kernel_session
);
553 /* Copy session output to the newly created Kernel session */
554 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
555 if (ret
!= LTTNG_OK
) {
559 session
->kernel_session
->uid
= session
->uid
;
560 session
->kernel_session
->gid
= session
->gid
;
561 session
->kernel_session
->output_traces
= session
->output_traces
;
562 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
563 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
568 trace_kernel_destroy_session(session
->kernel_session
);
569 session
->kernel_session
= NULL
;
575 * Count number of session permitted by uid/gid.
577 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
580 struct ltt_session
*session
;
581 const struct ltt_session_list
*session_list
= session_get_list();
583 DBG("Counting number of available session for UID %d", uid
);
584 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
585 if (!session_get(session
)) {
588 session_lock(session
);
589 /* Only count the sessions the user can control. */
590 if (session_access_ok(session
, uid
) &&
591 !session
->destroyed
) {
594 session_unlock(session
);
595 session_put(session
);
600 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
601 int *sock_error
, struct lttng_event
*event
)
604 struct lttng_userspace_probe_location
*probe_location
;
605 struct lttng_payload probe_location_payload
;
606 struct fd_handle
*handle
= NULL
;
609 * Create a payload to store the serialized version of the probe
612 lttng_payload_init(&probe_location_payload
);
614 ret
= lttng_dynamic_buffer_set_size(&probe_location_payload
.buffer
,
615 cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
);
617 ret
= LTTNG_ERR_NOMEM
;
622 * Receive the probe location.
624 ret
= lttcomm_recv_unix_sock(sock
, probe_location_payload
.buffer
.data
,
625 probe_location_payload
.buffer
.size
);
627 DBG("Nothing recv() from client var len data... continuing");
629 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
634 * Receive the file descriptor to the target binary from the client.
636 DBG("Receiving userspace probe target FD from client ...");
637 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
639 DBG("Nothing recv() from client userspace probe fd... continuing");
641 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
645 handle
= fd_handle_create(fd
);
647 ret
= LTTNG_ERR_NOMEM
;
651 /* Transferred to the handle. */
654 ret
= lttng_payload_push_fd_handle(&probe_location_payload
, handle
);
656 ERR("Failed to add userspace probe file descriptor to payload");
657 ret
= LTTNG_ERR_NOMEM
;
661 fd_handle_put(handle
);
665 struct lttng_payload_view view
= lttng_payload_view_from_payload(
666 &probe_location_payload
, 0, -1);
668 /* Extract the probe location from the serialized version. */
669 ret
= lttng_userspace_probe_location_create_from_payload(
670 &view
, &probe_location
);
673 WARN("Failed to create a userspace probe location from the received buffer");
674 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
678 /* Attach the probe location to the event. */
679 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
681 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
688 PERROR("Failed to close userspace probe location binary fd");
692 fd_handle_put(handle
);
693 lttng_payload_reset(&probe_location_payload
);
698 * Version of setup_lttng_msg() without command header.
700 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
701 void *payload_buf
, size_t payload_len
)
703 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
707 * Check if the current kernel tracer supports the session rotation feature.
708 * Return 1 if it does, 0 otherwise.
710 static int check_rotate_compatible(void)
714 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
715 DBG("Kernel tracer version is not compatible with the rotation feature");
723 * Send data on a unix socket using the liblttsessiondcomm API.
725 * Return lttcomm error code.
727 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
730 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
732 /* Check valid length */
733 if (view
->buffer
.size
== 0) {
738 ret
= lttcomm_send_unix_sock(
739 sock
, view
->buffer
.data
, view
->buffer
.size
);
745 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
756 * Process the command requested by the lttng client within the command
757 * context structure. This function make sure that the return structure (llm)
758 * is set and ready for transmission before returning.
760 * Return any error encountered or 0 for success.
762 * "sock" is only used for special-case var. len data.
763 * A command may assume the ownership of the socket, in which case its value
764 * should be set to -1.
766 * Should *NOT* be called with RCU read-side lock held.
768 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
772 bool need_tracing_session
= true;
776 DBG("Processing client command %d", cmd_ctx
->lsm
.cmd_type
);
778 assert(!rcu_read_ongoing());
782 switch (cmd_ctx
->lsm
.cmd_type
) {
783 case LTTNG_CREATE_SESSION_EXT
:
784 case LTTNG_DESTROY_SESSION
:
785 case LTTNG_LIST_SESSIONS
:
786 case LTTNG_LIST_DOMAINS
:
787 case LTTNG_START_TRACE
:
788 case LTTNG_STOP_TRACE
:
789 case LTTNG_DATA_PENDING
:
790 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
791 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
792 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
793 case LTTNG_SNAPSHOT_RECORD
:
794 case LTTNG_SAVE_SESSION
:
795 case LTTNG_SET_SESSION_SHM_PATH
:
796 case LTTNG_REGENERATE_METADATA
:
797 case LTTNG_REGENERATE_STATEDUMP
:
798 case LTTNG_ROTATE_SESSION
:
799 case LTTNG_ROTATION_GET_INFO
:
800 case LTTNG_ROTATION_SET_SCHEDULE
:
801 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
802 case LTTNG_CLEAR_SESSION
:
803 case LTTNG_LIST_TRIGGERS
:
810 /* Needs a functioning consumerd? */
811 switch (cmd_ctx
->lsm
.cmd_type
) {
812 case LTTNG_REGISTER_TRIGGER
:
813 case LTTNG_UNREGISTER_TRIGGER
:
814 need_consumerd
= false;
817 need_consumerd
= true;
821 if (config
.no_kernel
&& need_domain
822 && cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
824 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
826 ret
= LTTNG_ERR_KERN_NA
;
831 /* Deny register consumer if we already have a spawned consumer. */
832 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
833 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
834 if (kconsumer_data
.pid
> 0) {
835 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
836 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
839 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
843 * Check for command that don't needs to allocate a returned payload. We do
844 * this here so we don't have to make the call for no payload at each
847 switch(cmd_ctx
->lsm
.cmd_type
) {
848 case LTTNG_LIST_SESSIONS
:
849 case LTTNG_LIST_TRACEPOINTS
:
850 case LTTNG_LIST_TRACEPOINT_FIELDS
:
851 case LTTNG_LIST_DOMAINS
:
852 case LTTNG_LIST_CHANNELS
:
853 case LTTNG_LIST_EVENTS
:
854 case LTTNG_LIST_SYSCALLS
:
855 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
856 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
857 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
858 case LTTNG_DATA_PENDING
:
859 case LTTNG_ROTATE_SESSION
:
860 case LTTNG_ROTATION_GET_INFO
:
861 case LTTNG_REGISTER_TRIGGER
:
862 case LTTNG_LIST_TRIGGERS
:
865 /* Setup lttng message with no payload */
866 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
868 /* This label does not try to unlock the session */
869 goto init_setup_error
;
873 /* Commands that DO NOT need a session. */
874 switch (cmd_ctx
->lsm
.cmd_type
) {
875 case LTTNG_CREATE_SESSION_EXT
:
876 case LTTNG_LIST_SESSIONS
:
877 case LTTNG_LIST_TRACEPOINTS
:
878 case LTTNG_LIST_SYSCALLS
:
879 case LTTNG_LIST_TRACEPOINT_FIELDS
:
880 case LTTNG_SAVE_SESSION
:
881 case LTTNG_REGISTER_TRIGGER
:
882 case LTTNG_UNREGISTER_TRIGGER
:
883 case LTTNG_LIST_TRIGGERS
:
884 need_tracing_session
= false;
887 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
889 * We keep the session list lock across _all_ commands
890 * for now, because the per-session lock does not
891 * handle teardown properly.
894 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
895 if (cmd_ctx
->session
== NULL
) {
896 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
899 /* Acquire lock for the session */
900 session_lock(cmd_ctx
->session
);
906 * Commands that need a valid session but should NOT create one if none
907 * exists. Instead of creating one and destroying it when the command is
908 * handled, process that right before so we save some round trip in useless
911 switch (cmd_ctx
->lsm
.cmd_type
) {
912 case LTTNG_DISABLE_CHANNEL
:
913 case LTTNG_DISABLE_EVENT
:
914 switch (cmd_ctx
->lsm
.domain
.type
) {
915 case LTTNG_DOMAIN_KERNEL
:
916 if (!cmd_ctx
->session
->kernel_session
) {
917 ret
= LTTNG_ERR_NO_CHANNEL
;
921 case LTTNG_DOMAIN_JUL
:
922 case LTTNG_DOMAIN_LOG4J
:
923 case LTTNG_DOMAIN_PYTHON
:
924 case LTTNG_DOMAIN_UST
:
925 if (!cmd_ctx
->session
->ust_session
) {
926 ret
= LTTNG_ERR_NO_CHANNEL
;
931 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
943 * Check domain type for specific "pre-action".
945 switch (cmd_ctx
->lsm
.domain
.type
) {
946 case LTTNG_DOMAIN_KERNEL
:
948 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
952 /* Kernel tracer check */
953 if (!kernel_tracer_is_initialized()) {
954 /* Basically, load kernel tracer modules */
955 ret
= init_kernel_tracer();
961 /* Consumer is in an ERROR state. Report back to client */
962 if (need_consumerd
&& uatomic_read(&kernel_consumerd_state
) ==
964 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
968 /* Need a session for kernel command */
969 if (need_tracing_session
) {
970 if (cmd_ctx
->session
->kernel_session
== NULL
) {
971 ret
= create_kernel_session(cmd_ctx
->session
);
972 if (ret
!= LTTNG_OK
) {
973 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
978 /* Start the kernel consumer daemon */
979 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
980 if (kconsumer_data
.pid
== 0 &&
981 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
982 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
983 ret
= start_consumerd(&kconsumer_data
);
985 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
988 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
990 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
994 * The consumer was just spawned so we need to add the socket to
995 * the consumer output of the session if exist.
997 ret
= consumer_create_socket(&kconsumer_data
,
998 cmd_ctx
->session
->kernel_session
->consumer
);
1005 case LTTNG_DOMAIN_JUL
:
1006 case LTTNG_DOMAIN_LOG4J
:
1007 case LTTNG_DOMAIN_PYTHON
:
1008 case LTTNG_DOMAIN_UST
:
1010 if (!ust_app_supported()) {
1011 ret
= LTTNG_ERR_NO_UST
;
1015 /* Consumer is in an ERROR state. Report back to client */
1016 if (need_consumerd
&& uatomic_read(&ust_consumerd_state
) ==
1018 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1022 if (need_tracing_session
) {
1023 /* Create UST session if none exist. */
1024 if (cmd_ctx
->session
->ust_session
== NULL
) {
1025 ret
= create_ust_session(cmd_ctx
->session
,
1026 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
));
1027 if (ret
!= LTTNG_OK
) {
1032 /* Start the UST consumer daemons */
1034 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
1035 if (config
.consumerd64_bin_path
.value
&&
1036 ustconsumer64_data
.pid
== 0 &&
1037 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1038 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
1039 ret
= start_consumerd(&ustconsumer64_data
);
1041 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1042 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
1046 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
1047 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1049 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
1053 * Setup socket for consumer 64 bit. No need for atomic access
1054 * since it was set above and can ONLY be set in this thread.
1056 ret
= consumer_create_socket(&ustconsumer64_data
,
1057 cmd_ctx
->session
->ust_session
->consumer
);
1063 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
1064 if (config
.consumerd32_bin_path
.value
&&
1065 ustconsumer32_data
.pid
== 0 &&
1066 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1067 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1068 ret
= start_consumerd(&ustconsumer32_data
);
1070 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1071 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
1075 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
1076 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1078 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1082 * Setup socket for consumer 32 bit. No need for atomic access
1083 * since it was set above and can ONLY be set in this thread.
1085 ret
= consumer_create_socket(&ustconsumer32_data
,
1086 cmd_ctx
->session
->ust_session
->consumer
);
1098 /* Validate consumer daemon state when start/stop trace command */
1099 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1100 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1101 switch (cmd_ctx
->lsm
.domain
.type
) {
1102 case LTTNG_DOMAIN_NONE
:
1104 case LTTNG_DOMAIN_JUL
:
1105 case LTTNG_DOMAIN_LOG4J
:
1106 case LTTNG_DOMAIN_PYTHON
:
1107 case LTTNG_DOMAIN_UST
:
1108 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1109 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1113 case LTTNG_DOMAIN_KERNEL
:
1114 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1115 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1120 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1126 * Check that the UID matches that of the tracing session.
1127 * The root user can interact with all sessions.
1129 if (need_tracing_session
) {
1130 if (!session_access_ok(cmd_ctx
->session
,
1131 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1132 cmd_ctx
->session
->destroyed
) {
1133 ret
= LTTNG_ERR_EPERM
;
1139 * Send relayd information to consumer as soon as we have a domain and a
1142 if (cmd_ctx
->session
&& need_domain
) {
1144 * Setup relayd if not done yet. If the relayd information was already
1145 * sent to the consumer, this call will gracefully return.
1147 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1148 if (ret
!= LTTNG_OK
) {
1153 /* Process by command type */
1154 switch (cmd_ctx
->lsm
.cmd_type
) {
1155 case LTTNG_ADD_CONTEXT
:
1158 * An LTTNG_ADD_CONTEXT command might have a supplementary
1159 * payload if the context being added is an application context.
1161 if (cmd_ctx
->lsm
.u
.context
.ctx
.ctx
==
1162 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1163 char *provider_name
= NULL
, *context_name
= NULL
;
1164 size_t provider_name_len
=
1165 cmd_ctx
->lsm
.u
.context
.provider_name_len
;
1166 size_t context_name_len
=
1167 cmd_ctx
->lsm
.u
.context
.context_name_len
;
1169 if (provider_name_len
== 0 || context_name_len
== 0) {
1171 * Application provider and context names MUST
1174 ret
= -LTTNG_ERR_INVALID
;
1178 provider_name
= zmalloc(provider_name_len
+ 1);
1179 if (!provider_name
) {
1180 ret
= -LTTNG_ERR_NOMEM
;
1183 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
=
1186 context_name
= zmalloc(context_name_len
+ 1);
1187 if (!context_name
) {
1188 ret
= -LTTNG_ERR_NOMEM
;
1189 goto error_add_context
;
1191 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1194 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1197 goto error_add_context
;
1200 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1203 goto error_add_context
;
1208 * cmd_add_context assumes ownership of the provider and context
1211 ret
= cmd_add_context(cmd_ctx
->session
,
1212 cmd_ctx
->lsm
.domain
.type
,
1213 cmd_ctx
->lsm
.u
.context
.channel_name
,
1214 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.context
.ctx
),
1215 kernel_poll_pipe
[1]);
1217 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1218 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1220 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
);
1221 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1227 case LTTNG_DISABLE_CHANNEL
:
1229 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1230 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1233 case LTTNG_DISABLE_EVENT
:
1237 * FIXME: handle filter; for now we just receive the filter's
1238 * bytecode along with the filter expression which are sent by
1239 * liblttng-ctl and discard them.
1241 * This fixes an issue where the client may block while sending
1242 * the filter payload and encounter an error because the session
1243 * daemon closes the socket without ever handling this data.
1245 size_t count
= cmd_ctx
->lsm
.u
.disable
.expression_len
+
1246 cmd_ctx
->lsm
.u
.disable
.bytecode_len
;
1249 char data
[LTTNG_FILTER_MAX_LEN
];
1251 DBG("Discarding disable event command payload of size %zu", count
);
1253 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1254 count
> sizeof(data
) ? sizeof(data
) : count
);
1259 count
-= (size_t) ret
;
1262 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1263 cmd_ctx
->lsm
.u
.disable
.channel_name
,
1264 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.disable
.event
));
1267 case LTTNG_ENABLE_CHANNEL
:
1269 cmd_ctx
->lsm
.u
.channel
.chan
.attr
.extended
.ptr
=
1270 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
.u
.channel
.extended
;
1271 ret
= cmd_enable_channel(cmd_ctx
->session
,
1272 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1273 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.channel
.chan
),
1274 kernel_poll_pipe
[1]);
1277 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1278 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1280 struct lttng_dynamic_buffer payload
;
1281 struct lttng_buffer_view payload_view
;
1282 const bool add_value
=
1283 cmd_ctx
->lsm
.cmd_type
==
1284 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1285 const size_t name_len
=
1286 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1288 const enum lttng_domain_type domain_type
=
1289 (enum lttng_domain_type
)
1290 cmd_ctx
->lsm
.domain
.type
;
1291 const enum lttng_process_attr process_attr
=
1292 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1293 .process_attr_tracker_add_remove_include_value
1295 const enum lttng_process_attr_value_type value_type
=
1296 (enum lttng_process_attr_value_type
) cmd_ctx
1298 .process_attr_tracker_add_remove_include_value
1300 struct process_attr_value
*value
;
1301 enum lttng_error_code ret_code
;
1302 long login_name_max
;
1304 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1305 if (login_name_max
< 0) {
1306 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1307 ret
= LTTNG_ERR_INVALID
;
1311 /* Receive remaining variable length payload if applicable. */
1312 if (name_len
> login_name_max
) {
1314 * POSIX mandates user and group names that are at least
1315 * 8 characters long. Note that although shadow-utils
1316 * (useradd, groupaadd, etc.) use 32 chars as their
1317 * limit (from bits/utmp.h, UT_NAMESIZE),
1318 * LOGIN_NAME_MAX is defined to 256.
1320 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1321 add_value
? "addition" : "removal",
1322 name_len
, login_name_max
);
1323 ret
= LTTNG_ERR_INVALID
;
1327 lttng_dynamic_buffer_init(&payload
);
1328 if (name_len
!= 0) {
1330 * Receive variable payload for user/group name
1333 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1335 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1336 add_value
? "add" : "remove");
1337 ret
= LTTNG_ERR_NOMEM
;
1338 goto error_add_remove_tracker_value
;
1341 ret
= lttcomm_recv_unix_sock(
1342 *sock
, payload
.data
, name_len
);
1344 ERR("Failed to receive payload of %s process attribute tracker value argument",
1345 add_value
? "add" : "remove");
1347 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1348 goto error_add_remove_tracker_value
;
1352 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1353 &payload
, 0, name_len
);
1354 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1355 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1356 goto error_add_remove_tracker_value
;
1360 * Validate the value type and domains are legal for the process
1361 * attribute tracker that is specified and convert the value to
1362 * add/remove to the internal sessiond representation.
1364 ret_code
= process_attr_value_from_comm(domain_type
,
1365 process_attr
, value_type
,
1366 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1368 &payload_view
, &value
);
1369 if (ret_code
!= LTTNG_OK
) {
1371 goto error_add_remove_tracker_value
;
1375 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1376 cmd_ctx
->session
, domain_type
,
1377 process_attr
, value
);
1379 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1380 cmd_ctx
->session
, domain_type
,
1381 process_attr
, value
);
1383 process_attr_value_destroy(value
);
1384 error_add_remove_tracker_value
:
1385 lttng_dynamic_buffer_reset(&payload
);
1388 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1390 enum lttng_tracking_policy tracking_policy
;
1391 const enum lttng_domain_type domain_type
=
1392 (enum lttng_domain_type
)
1393 cmd_ctx
->lsm
.domain
.type
;
1394 const enum lttng_process_attr process_attr
=
1395 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1396 .process_attr_tracker_get_tracking_policy
1399 ret
= cmd_process_attr_tracker_get_tracking_policy(
1400 cmd_ctx
->session
, domain_type
, process_attr
,
1402 if (ret
!= LTTNG_OK
) {
1406 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1407 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1409 ret
= LTTNG_ERR_NOMEM
;
1415 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1417 const enum lttng_tracking_policy tracking_policy
=
1418 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1419 .process_attr_tracker_set_tracking_policy
1421 const enum lttng_domain_type domain_type
=
1422 (enum lttng_domain_type
)
1423 cmd_ctx
->lsm
.domain
.type
;
1424 const enum lttng_process_attr process_attr
=
1425 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1426 .process_attr_tracker_set_tracking_policy
1429 ret
= cmd_process_attr_tracker_set_tracking_policy(
1430 cmd_ctx
->session
, domain_type
, process_attr
,
1432 if (ret
!= LTTNG_OK
) {
1437 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1439 struct lttng_process_attr_values
*values
;
1440 struct lttng_dynamic_buffer reply
;
1441 const enum lttng_domain_type domain_type
=
1442 (enum lttng_domain_type
)
1443 cmd_ctx
->lsm
.domain
.type
;
1444 const enum lttng_process_attr process_attr
=
1445 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1446 .process_attr_tracker_get_inclusion_set
1449 ret
= cmd_process_attr_tracker_get_inclusion_set(
1450 cmd_ctx
->session
, domain_type
, process_attr
,
1452 if (ret
!= LTTNG_OK
) {
1456 lttng_dynamic_buffer_init(&reply
);
1457 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1459 goto error_tracker_get_inclusion_set
;
1462 ret
= setup_lttng_msg_no_cmd_header(
1463 cmd_ctx
, reply
.data
, reply
.size
);
1465 ret
= LTTNG_ERR_NOMEM
;
1466 goto error_tracker_get_inclusion_set
;
1470 error_tracker_get_inclusion_set
:
1471 lttng_process_attr_values_destroy(values
);
1472 lttng_dynamic_buffer_reset(&reply
);
1475 case LTTNG_ENABLE_EVENT
:
1477 struct lttng_event
*ev
= NULL
;
1478 struct lttng_event_exclusion
*exclusion
= NULL
;
1479 struct lttng_filter_bytecode
*bytecode
= NULL
;
1480 char *filter_expression
= NULL
;
1482 /* Handle exclusion events and receive it from the client. */
1483 if (cmd_ctx
->lsm
.u
.enable
.exclusion_count
> 0) {
1484 size_t count
= cmd_ctx
->lsm
.u
.enable
.exclusion_count
;
1486 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1487 (count
* LTTNG_SYMBOL_NAME_LEN
));
1489 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1493 DBG("Receiving var len exclusion event list from client ...");
1494 exclusion
->count
= count
;
1495 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1496 count
* LTTNG_SYMBOL_NAME_LEN
);
1498 DBG("Nothing recv() from client var len data... continuing");
1501 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1506 /* Get filter expression from client. */
1507 if (cmd_ctx
->lsm
.u
.enable
.expression_len
> 0) {
1508 size_t expression_len
=
1509 cmd_ctx
->lsm
.u
.enable
.expression_len
;
1511 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1512 ret
= LTTNG_ERR_FILTER_INVAL
;
1517 filter_expression
= zmalloc(expression_len
);
1518 if (!filter_expression
) {
1520 ret
= LTTNG_ERR_FILTER_NOMEM
;
1524 /* Receive var. len. data */
1525 DBG("Receiving var len filter's expression from client ...");
1526 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1529 DBG("Nothing recv() from client var len data... continuing");
1531 free(filter_expression
);
1533 ret
= LTTNG_ERR_FILTER_INVAL
;
1538 /* Handle filter and get bytecode from client. */
1539 if (cmd_ctx
->lsm
.u
.enable
.bytecode_len
> 0) {
1540 size_t bytecode_len
= cmd_ctx
->lsm
.u
.enable
.bytecode_len
;
1542 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1543 ret
= LTTNG_ERR_FILTER_INVAL
;
1544 free(filter_expression
);
1549 bytecode
= zmalloc(bytecode_len
);
1551 free(filter_expression
);
1553 ret
= LTTNG_ERR_FILTER_NOMEM
;
1557 /* Receive var. len. data */
1558 DBG("Receiving var len filter's bytecode from client ...");
1559 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1561 DBG("Nothing recv() from client var len data... continuing");
1563 free(filter_expression
);
1566 ret
= LTTNG_ERR_FILTER_INVAL
;
1570 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1571 free(filter_expression
);
1574 ret
= LTTNG_ERR_FILTER_INVAL
;
1579 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.enable
.event
));
1581 DBG("Failed to copy event: %s",
1582 cmd_ctx
->lsm
.u
.enable
.event
.name
);
1583 free(filter_expression
);
1586 ret
= LTTNG_ERR_NOMEM
;
1591 if (cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
> 0) {
1592 /* Expect a userspace probe description. */
1593 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1595 free(filter_expression
);
1598 lttng_event_destroy(ev
);
1603 ret
= cmd_enable_event(cmd_ctx
->session
,
1604 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1605 cmd_ctx
->lsm
.u
.enable
.channel_name
,
1607 filter_expression
, bytecode
, exclusion
,
1608 kernel_poll_pipe
[1]);
1609 lttng_event_destroy(ev
);
1612 case LTTNG_LIST_TRACEPOINTS
:
1614 struct lttng_event
*events
;
1617 session_lock_list();
1618 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &events
);
1619 session_unlock_list();
1620 if (nb_events
< 0) {
1621 /* Return value is a negative lttng_error_code. */
1627 * Setup lttng message with payload size set to the event list size in
1628 * bytes and then copy list into the llm payload.
1630 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1631 sizeof(struct lttng_event
) * nb_events
);
1641 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1643 struct lttng_event_field
*fields
;
1646 session_lock_list();
1647 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1649 session_unlock_list();
1650 if (nb_fields
< 0) {
1651 /* Return value is a negative lttng_error_code. */
1657 * Setup lttng message with payload size set to the event list size in
1658 * bytes and then copy list into the llm payload.
1660 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1661 sizeof(struct lttng_event_field
) * nb_fields
);
1671 case LTTNG_LIST_SYSCALLS
:
1673 struct lttng_event
*events
;
1676 nb_events
= cmd_list_syscalls(&events
);
1677 if (nb_events
< 0) {
1678 /* Return value is a negative lttng_error_code. */
1684 * Setup lttng message with payload size set to the event list size in
1685 * bytes and then copy list into the llm payload.
1687 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1688 sizeof(struct lttng_event
) * nb_events
);
1698 case LTTNG_SET_CONSUMER_URI
:
1701 struct lttng_uri
*uris
;
1703 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1704 len
= nb_uri
* sizeof(struct lttng_uri
);
1707 ret
= LTTNG_ERR_INVALID
;
1711 uris
= zmalloc(len
);
1713 ret
= LTTNG_ERR_FATAL
;
1717 /* Receive variable len data */
1718 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1719 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1721 DBG("No URIs received from client... continuing");
1723 ret
= LTTNG_ERR_SESSION_FAIL
;
1728 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1730 if (ret
!= LTTNG_OK
) {
1737 case LTTNG_START_TRACE
:
1740 * On the first start, if we have a kernel session and we have
1741 * enabled time or size-based rotations, we have to make sure
1742 * the kernel tracer supports it.
1744 if (!cmd_ctx
->session
->has_been_started
&& \
1745 cmd_ctx
->session
->kernel_session
&& \
1746 (cmd_ctx
->session
->rotate_timer_period
|| \
1747 cmd_ctx
->session
->rotate_size
) && \
1748 !check_rotate_compatible()) {
1749 DBG("Kernel tracer version is not compatible with the rotation feature");
1750 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1753 ret
= cmd_start_trace(cmd_ctx
->session
);
1756 case LTTNG_STOP_TRACE
:
1758 ret
= cmd_stop_trace(cmd_ctx
->session
);
1761 case LTTNG_DESTROY_SESSION
:
1763 ret
= cmd_destroy_session(cmd_ctx
->session
,
1764 notification_thread_handle
,
1768 case LTTNG_LIST_DOMAINS
:
1771 struct lttng_domain
*domains
= NULL
;
1773 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1775 /* Return value is a negative lttng_error_code. */
1780 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1781 nb_dom
* sizeof(struct lttng_domain
));
1791 case LTTNG_LIST_CHANNELS
:
1793 ssize_t payload_size
;
1794 struct lttng_channel
*channels
= NULL
;
1796 payload_size
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1797 cmd_ctx
->session
, &channels
);
1798 if (payload_size
< 0) {
1799 /* Return value is a negative lttng_error_code. */
1800 ret
= -payload_size
;
1804 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1815 case LTTNG_LIST_EVENTS
:
1818 struct lttcomm_event_command_header cmd_header
= {};
1819 size_t original_payload_size
;
1820 size_t payload_size
;
1822 ret
= setup_empty_lttng_msg(cmd_ctx
);
1824 ret
= LTTNG_ERR_NOMEM
;
1828 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1830 /* Extended infos are included at the end of the payload. */
1831 list_ret
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1833 cmd_ctx
->lsm
.u
.list
.channel_name
,
1834 &cmd_ctx
->reply_payload
);
1836 /* Return value is a negative lttng_error_code. */
1841 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1842 sizeof(cmd_header
) - original_payload_size
;
1843 update_lttng_msg(cmd_ctx
, sizeof(cmd_header
), payload_size
);
1848 case LTTNG_LIST_SESSIONS
:
1850 unsigned int nr_sessions
;
1851 void *sessions_payload
;
1854 session_lock_list();
1855 nr_sessions
= lttng_sessions_count(
1856 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1857 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1859 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1860 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1861 sessions_payload
= zmalloc(payload_len
);
1863 if (!sessions_payload
) {
1864 session_unlock_list();
1869 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1870 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1871 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1872 session_unlock_list();
1874 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1876 free(sessions_payload
);
1885 case LTTNG_REGISTER_CONSUMER
:
1887 struct consumer_data
*cdata
;
1889 switch (cmd_ctx
->lsm
.domain
.type
) {
1890 case LTTNG_DOMAIN_KERNEL
:
1891 cdata
= &kconsumer_data
;
1894 ret
= LTTNG_ERR_UND
;
1898 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1899 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1902 case LTTNG_DATA_PENDING
:
1905 uint8_t pending_ret_byte
;
1907 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1912 * This function may returns 0 or 1 to indicate whether or not
1913 * there is data pending. In case of error, it should return an
1914 * LTTNG_ERR code. However, some code paths may still return
1915 * a nondescript error code, which we handle by returning an
1918 if (pending_ret
== 0 || pending_ret
== 1) {
1920 * ret will be set to LTTNG_OK at the end of
1923 } else if (pending_ret
< 0) {
1924 ret
= LTTNG_ERR_UNK
;
1931 pending_ret_byte
= (uint8_t) pending_ret
;
1933 /* 1 byte to return whether or not data is pending */
1934 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1935 &pending_ret_byte
, 1);
1944 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
1946 uint32_t snapshot_id
;
1947 struct lttcomm_lttng_output_id reply
;
1949 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
1950 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
),
1952 if (ret
!= LTTNG_OK
) {
1955 reply
.id
= snapshot_id
;
1957 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
1963 /* Copy output list into message payload */
1967 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
1969 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
1970 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
));
1973 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
1976 struct lttng_snapshot_output
*outputs
= NULL
;
1978 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
1979 if (nb_output
< 0) {
1984 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
1985 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
1986 nb_output
* sizeof(struct lttng_snapshot_output
));
1996 case LTTNG_SNAPSHOT_RECORD
:
1998 ret
= cmd_snapshot_record(cmd_ctx
->session
,
1999 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_record
.output
),
2000 cmd_ctx
->lsm
.u
.snapshot_record
.wait
);
2003 case LTTNG_CREATE_SESSION_EXT
:
2005 struct lttng_dynamic_buffer payload
;
2006 struct lttng_session_descriptor
*return_descriptor
= NULL
;
2008 lttng_dynamic_buffer_init(&payload
);
2009 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2010 if (ret
!= LTTNG_OK
) {
2014 ret
= lttng_session_descriptor_serialize(return_descriptor
,
2017 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2018 lttng_session_descriptor_destroy(return_descriptor
);
2019 ret
= LTTNG_ERR_NOMEM
;
2022 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
2025 lttng_session_descriptor_destroy(return_descriptor
);
2026 ret
= LTTNG_ERR_NOMEM
;
2029 lttng_dynamic_buffer_reset(&payload
);
2030 lttng_session_descriptor_destroy(return_descriptor
);
2034 case LTTNG_SAVE_SESSION
:
2036 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
2040 case LTTNG_SET_SESSION_SHM_PATH
:
2042 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2043 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2046 case LTTNG_REGENERATE_METADATA
:
2048 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2051 case LTTNG_REGENERATE_STATEDUMP
:
2053 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2056 case LTTNG_REGISTER_TRIGGER
:
2058 struct lttng_trigger
*return_trigger
;
2059 size_t original_payload_size
;
2060 size_t payload_size
;
2062 ret
= setup_empty_lttng_msg(cmd_ctx
);
2064 ret
= LTTNG_ERR_NOMEM
;
2068 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2070 ret
= cmd_register_trigger(cmd_ctx
, *sock
,
2071 notification_thread_handle
, &return_trigger
);
2072 if (ret
!= LTTNG_OK
) {
2076 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2078 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2079 ret
= LTTNG_ERR_NOMEM
;
2080 lttng_trigger_destroy(return_trigger
);
2084 lttng_trigger_destroy(return_trigger
);
2085 return_trigger
= NULL
;
2087 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2088 original_payload_size
;
2090 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2095 case LTTNG_UNREGISTER_TRIGGER
:
2097 ret
= cmd_unregister_trigger(cmd_ctx
, *sock
,
2098 notification_thread_handle
);
2101 case LTTNG_ROTATE_SESSION
:
2103 struct lttng_rotate_session_return rotate_return
;
2105 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2107 memset(&rotate_return
, 0, sizeof(rotate_return
));
2108 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2109 DBG("Kernel tracer version is not compatible with the rotation feature");
2110 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2114 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2116 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2122 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2123 sizeof(rotate_return
));
2132 case LTTNG_ROTATION_GET_INFO
:
2134 struct lttng_rotation_get_info_return get_info_return
;
2136 memset(&get_info_return
, 0, sizeof(get_info_return
));
2137 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2138 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2144 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2145 sizeof(get_info_return
));
2154 case LTTNG_ROTATION_SET_SCHEDULE
:
2157 enum lttng_rotation_schedule_type schedule_type
;
2160 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2161 DBG("Kernel tracer version does not support session rotations");
2162 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2166 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2167 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2168 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2170 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
2174 notification_thread_handle
);
2175 if (ret
!= LTTNG_OK
) {
2181 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2183 struct lttng_session_list_schedules_return schedules
= {
2184 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2185 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2186 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2187 .size
.value
= cmd_ctx
->session
->rotate_size
,
2190 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2200 case LTTNG_CLEAR_SESSION
:
2202 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2205 case LTTNG_LIST_TRIGGERS
:
2207 struct lttng_triggers
*return_triggers
= NULL
;
2208 size_t original_payload_size
;
2209 size_t payload_size
;
2211 ret
= setup_empty_lttng_msg(cmd_ctx
);
2213 ret
= LTTNG_ERR_NOMEM
;
2217 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2219 ret
= cmd_list_triggers(cmd_ctx
,
2220 notification_thread_handle
, &return_triggers
);
2221 if (ret
!= LTTNG_OK
) {
2225 assert(return_triggers
);
2226 ret
= lttng_triggers_serialize(
2227 return_triggers
, &cmd_ctx
->reply_payload
);
2228 lttng_triggers_destroy(return_triggers
);
2230 ERR("Failed to serialize triggers in reply to `list triggers` command");
2231 ret
= LTTNG_ERR_NOMEM
;
2235 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2236 original_payload_size
;
2238 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2244 ret
= LTTNG_ERR_UND
;
2249 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2250 DBG("Missing llm header, creating one.");
2251 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2255 /* Set return code */
2256 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2258 if (cmd_ctx
->session
) {
2259 session_unlock(cmd_ctx
->session
);
2260 session_put(cmd_ctx
->session
);
2261 cmd_ctx
->session
= NULL
;
2263 if (need_tracing_session
) {
2264 session_unlock_list();
2267 assert(!rcu_read_ongoing());
2271 static int create_client_sock(void)
2273 int ret
, client_sock
;
2274 const mode_t old_umask
= umask(0);
2276 /* Create client tool unix socket */
2277 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2278 if (client_sock
< 0) {
2279 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2284 /* Set the cloexec flag */
2285 ret
= utils_set_fd_cloexec(client_sock
);
2287 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2288 "Continuing but note that the consumer daemon will have a "
2289 "reference to this socket on exec()", client_sock
);
2292 /* File permission MUST be 660 */
2293 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2295 ERR("Set file permissions failed: %s",
2296 config
.client_unix_sock_path
.value
);
2298 (void) lttcomm_close_unix_sock(client_sock
);
2302 DBG("Created client socket (fd = %i)", client_sock
);
2309 static void cleanup_client_thread(void *data
)
2311 struct lttng_pipe
*quit_pipe
= data
;
2313 lttng_pipe_destroy(quit_pipe
);
2316 static void thread_init_cleanup(void *data
)
2318 set_thread_status(false);
2322 * This thread manage all clients request using the unix client socket for
2325 static void *thread_manage_clients(void *data
)
2327 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2329 uint32_t revents
, nb_fd
;
2330 struct lttng_poll_event events
;
2331 const int client_sock
= thread_state
.client_sock
;
2332 struct lttng_pipe
*quit_pipe
= data
;
2333 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2334 struct command_ctx cmd_ctx
= {};
2336 DBG("[thread] Manage client started");
2338 lttng_payload_init(&cmd_ctx
.reply_payload
);
2340 is_root
= (getuid() == 0);
2342 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2344 rcu_register_thread();
2346 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2348 health_code_update();
2350 ret
= lttcomm_listen_unix_sock(client_sock
);
2356 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2357 * more will be added to this poll set.
2359 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2361 goto error_create_poll
;
2364 /* Add the application registration socket */
2365 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2370 /* Add thread quit pipe */
2371 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2376 /* Set state as running. */
2377 set_thread_status(true);
2378 pthread_cleanup_pop(0);
2380 /* This testpoint is after we signal readiness to the parent. */
2381 if (testpoint(sessiond_thread_manage_clients
)) {
2385 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2389 health_code_update();
2392 const struct cmd_completion_handler
*cmd_completion_handler
;
2394 cmd_ctx
.creds
= (lttng_sock_cred
) {
2398 cmd_ctx
.session
= NULL
;
2399 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2400 cmd_ctx
.lttng_msg_size
= 0;
2402 DBG("Accepting client command ...");
2404 /* Inifinite blocking call, waiting for transmission */
2406 health_poll_entry();
2407 ret
= lttng_poll_wait(&events
, -1);
2411 * Restart interrupted system call.
2413 if (errno
== EINTR
) {
2421 for (i
= 0; i
< nb_fd
; i
++) {
2422 revents
= LTTNG_POLL_GETEV(&events
, i
);
2423 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2425 health_code_update();
2427 if (pollfd
== thread_quit_pipe_fd
) {
2431 /* Event on the registration socket */
2432 if (revents
& LPOLLIN
) {
2434 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2435 ERR("Client socket poll error");
2438 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2444 DBG("Wait for client response");
2446 health_code_update();
2448 sock
= lttcomm_accept_unix_sock(client_sock
);
2454 * Set the CLOEXEC flag. Return code is useless because either way, the
2457 (void) utils_set_fd_cloexec(sock
);
2459 /* Set socket option for credentials retrieval */
2460 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2465 health_code_update();
2468 * Data is received from the lttng client. The struct
2469 * lttcomm_session_msg (lsm) contains the command and data request of
2472 DBG("Receiving data from client ...");
2473 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2474 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2475 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2476 DBG("Incomplete recv() from client... continuing");
2485 health_code_update();
2487 // TODO: Validate cmd_ctx including sanity check for
2488 // security purpose.
2490 rcu_thread_online();
2492 * This function dispatch the work to the kernel or userspace tracer
2493 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2494 * informations for the client. The command context struct contains
2495 * everything this function may needs.
2497 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2498 rcu_thread_offline();
2508 * TODO: Inform client somehow of the fatal error. At
2509 * this point, ret < 0 means that a zmalloc failed
2510 * (ENOMEM). Error detected but still accept
2511 * command, unless a socket error has been
2517 cmd_completion_handler
= cmd_pop_completion_handler();
2518 if (cmd_completion_handler
) {
2519 enum lttng_error_code completion_code
;
2521 completion_code
= cmd_completion_handler
->run(
2522 cmd_completion_handler
->data
);
2523 if (completion_code
!= LTTNG_OK
) {
2528 health_code_update();
2531 struct lttng_payload_view view
=
2532 lttng_payload_view_from_payload(
2533 &cmd_ctx
.reply_payload
,
2535 struct lttcomm_lttng_msg
*llm
= (typeof(
2536 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2538 assert(cmd_ctx
.reply_payload
.buffer
.size
>=
2540 assert(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2542 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2544 DBG("Sending response (size: %d, retcode: %s (%d))",
2545 cmd_ctx
.lttng_msg_size
,
2546 lttng_strerror(-llm
->ret_code
),
2548 ret
= send_unix_sock(sock
, &view
);
2550 ERR("Failed to send data back to client");
2553 /* End of transmission */
2561 health_code_update();
2573 lttng_poll_clean(&events
);
2577 unlink(config
.client_unix_sock_path
.value
);
2578 ret
= close(client_sock
);
2585 ERR("Health error occurred in %s", __func__
);
2588 health_unregister(health_sessiond
);
2590 DBG("Client thread dying");
2591 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2592 rcu_unregister_thread();
2597 bool shutdown_client_thread(void *thread_data
)
2599 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2600 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2602 return notify_thread_pipe(write_fd
) == 1;
2605 struct lttng_thread
*launch_client_thread(void)
2607 bool thread_running
;
2608 struct lttng_pipe
*client_quit_pipe
;
2609 struct lttng_thread
*thread
= NULL
;
2610 int client_sock_fd
= -1;
2612 sem_init(&thread_state
.ready
, 0, 0);
2613 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2614 if (!client_quit_pipe
) {
2618 client_sock_fd
= create_client_sock();
2619 if (client_sock_fd
< 0) {
2623 thread_state
.client_sock
= client_sock_fd
;
2624 thread
= lttng_thread_create("Client management",
2625 thread_manage_clients
,
2626 shutdown_client_thread
,
2627 cleanup_client_thread
,
2632 /* The client thread now owns the client sock fd and the quit pipe. */
2633 client_sock_fd
= -1;
2634 client_quit_pipe
= NULL
;
2637 * This thread is part of the threads that need to be fully
2638 * initialized before the session daemon is marked as "ready".
2640 thread_running
= wait_thread_status();
2641 if (!thread_running
) {
2646 if (client_sock_fd
>= 0) {
2647 if (close(client_sock_fd
)) {
2648 PERROR("Failed to close client socket");
2651 lttng_thread_put(thread
);
2652 cleanup_client_thread(client_quit_pipe
);