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"
46 #include "agent-thread.h"
50 static struct thread_state
{
56 static void set_thread_status(bool running
)
58 DBG("Marking client thread's state as %s", running
? "running" : "error");
59 thread_state
.running
= running
;
60 sem_post(&thread_state
.ready
);
63 static bool wait_thread_status(void)
65 DBG("Waiting for client thread to be ready");
66 sem_wait(&thread_state
.ready
);
67 if (thread_state
.running
) {
68 DBG("Client thread is ready");
70 ERR("Initialization of client thread failed");
73 return thread_state
.running
;
77 * Setup the outgoing data buffer for the response (llm) by allocating the
78 * right amount of memory and copying the original information from the lsm
81 * Return 0 on success, negative value on error.
83 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
84 const void *payload_buf
, size_t payload_len
,
85 const void *cmd_header_buf
, size_t cmd_header_len
)
88 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
89 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
90 const struct lttcomm_lttng_msg llm
= {
91 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
92 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
93 .cmd_header_size
= cmd_header_len
,
94 .data_size
= payload_len
,
97 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
102 lttng_dynamic_pointer_array_clear(&cmd_ctx
->reply_payload
._fd_handles
);
104 cmd_ctx
->lttng_msg_size
= total_msg_size
;
106 /* Append reply header. */
107 ret
= lttng_dynamic_buffer_append(
108 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
113 /* Append command header. */
114 if (cmd_header_len
) {
115 ret
= lttng_dynamic_buffer_append(
116 &cmd_ctx
->reply_payload
.buffer
, cmd_header_buf
,
123 /* Append payload. */
125 ret
= lttng_dynamic_buffer_append(
126 &cmd_ctx
->reply_payload
.buffer
, payload_buf
,
137 static int setup_empty_lttng_msg(struct command_ctx
*cmd_ctx
)
140 const struct lttcomm_lttng_msg llm
= {};
142 ret
= lttng_dynamic_buffer_set_size(&cmd_ctx
->reply_payload
.buffer
, 0);
147 /* Append place-holder reply header. */
148 ret
= lttng_dynamic_buffer_append(
149 &cmd_ctx
->reply_payload
.buffer
, &llm
, sizeof(llm
));
154 cmd_ctx
->lttng_msg_size
= sizeof(llm
);
159 static void update_lttng_msg(struct command_ctx
*cmd_ctx
, size_t cmd_header_len
,
162 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
163 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
164 const struct lttcomm_lttng_msg llm
= {
165 .cmd_type
= cmd_ctx
->lsm
.cmd_type
,
166 .pid
= cmd_ctx
->lsm
.domain
.attr
.pid
,
167 .cmd_header_size
= cmd_header_len
,
168 .data_size
= payload_len
,
170 struct lttcomm_lttng_msg
*p_llm
;
172 assert(cmd_ctx
->reply_payload
.buffer
.size
>= sizeof(llm
));
174 p_llm
= (typeof(p_llm
)) cmd_ctx
->reply_payload
.buffer
.data
;
176 /* Update existing header. */
177 memcpy(p_llm
, &llm
, sizeof(llm
));
179 cmd_ctx
->lttng_msg_size
= total_msg_size
;
183 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
184 * exec or it will fail.
186 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
188 return launch_consumer_management_thread(consumer_data
) ? 0 : -1;
192 * Fork and exec a consumer daemon (consumerd).
194 * Return pid if successful else -1.
196 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
200 const char *consumer_to_use
;
201 const char *verbosity
;
204 DBG("Spawning consumerd");
211 if (the_config
.verbose_consumer
) {
212 verbosity
= "--verbose";
213 } else if (lttng_opt_quiet
) {
214 verbosity
= "--quiet";
219 switch (consumer_data
->type
) {
220 case LTTNG_CONSUMER_KERNEL
:
222 * Find out which consumerd to execute. We will first try the
223 * 64-bit path, then the sessiond's installation directory, and
224 * fallback on the 32-bit one,
226 DBG3("Looking for a kernel consumer at these locations:");
227 DBG3(" 1) %s", the_config
.consumerd64_bin_path
.value
? : "NULL");
228 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
229 DBG3(" 3) %s", the_config
.consumerd32_bin_path
.value
? : "NULL");
230 if (stat(the_config
.consumerd64_bin_path
.value
, &st
) == 0) {
231 DBG3("Found location #1");
232 consumer_to_use
= the_config
.consumerd64_bin_path
.value
;
233 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
234 DBG3("Found location #2");
235 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
236 } else if (the_config
.consumerd32_bin_path
.value
&&
237 stat(the_config
.consumerd32_bin_path
.value
, &st
) == 0) {
238 DBG3("Found location #3");
239 consumer_to_use
= the_config
.consumerd32_bin_path
.value
;
241 DBG("Could not find any valid consumerd executable");
245 DBG("Using kernel consumer at: %s", consumer_to_use
);
246 (void) execl(consumer_to_use
, "lttng-consumerd",
247 verbosity
, "-k", "--consumerd-cmd-sock",
248 consumer_data
->cmd_unix_sock_path
,
249 "--consumerd-err-sock",
250 consumer_data
->err_unix_sock_path
,
252 the_config
.tracing_group_name
.value
,
255 case LTTNG_CONSUMER64_UST
:
257 if (the_config
.consumerd64_lib_dir
.value
) {
262 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
266 tmplen
= strlen(the_config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
267 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
272 strcat(tmpnew
, the_config
.consumerd64_lib_dir
.value
);
273 if (tmp
[0] != '\0') {
277 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
284 DBG("Using 64-bit UST consumer at: %s",
285 the_config
.consumerd64_bin_path
.value
);
286 (void) execl(the_config
.consumerd64_bin_path
.value
,
287 "lttng-consumerd", verbosity
, "-u",
288 "--consumerd-cmd-sock",
289 consumer_data
->cmd_unix_sock_path
,
290 "--consumerd-err-sock",
291 consumer_data
->err_unix_sock_path
,
293 the_config
.tracing_group_name
.value
,
297 case LTTNG_CONSUMER32_UST
:
299 if (the_config
.consumerd32_lib_dir
.value
) {
304 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
308 tmplen
= strlen(the_config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
309 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
314 strcat(tmpnew
, the_config
.consumerd32_lib_dir
.value
);
315 if (tmp
[0] != '\0') {
319 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
326 DBG("Using 32-bit UST consumer at: %s",
327 the_config
.consumerd32_bin_path
.value
);
328 (void) execl(the_config
.consumerd32_bin_path
.value
,
329 "lttng-consumerd", verbosity
, "-u",
330 "--consumerd-cmd-sock",
331 consumer_data
->cmd_unix_sock_path
,
332 "--consumerd-err-sock",
333 consumer_data
->err_unix_sock_path
,
335 the_config
.tracing_group_name
.value
,
340 ERR("unknown consumer type");
344 PERROR("Consumer execl()");
346 /* Reaching this point, we got a failure on our execl(). */
348 } else if (pid
> 0) {
351 PERROR("start consumer fork");
359 * Spawn the consumerd daemon and session daemon thread.
361 static int start_consumerd(struct consumer_data
*consumer_data
)
366 * Set the listen() state on the socket since there is a possible race
367 * between the exec() of the consumer daemon and this call if place in the
368 * consumer thread. See bug #366 for more details.
370 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
375 pthread_mutex_lock(&consumer_data
->pid_mutex
);
376 if (consumer_data
->pid
!= 0) {
377 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
381 ret
= spawn_consumerd(consumer_data
);
383 ERR("Spawning consumerd failed");
384 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
388 /* Setting up the consumer_data pid */
389 consumer_data
->pid
= ret
;
390 DBG2("Consumer pid %d", consumer_data
->pid
);
391 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
393 DBG2("Spawning consumer control thread");
394 ret
= spawn_consumer_thread(consumer_data
);
396 ERR("Fatal error spawning consumer control thread");
404 /* Cleanup already created sockets on error. */
405 if (consumer_data
->err_sock
>= 0) {
408 err
= close(consumer_data
->err_sock
);
410 PERROR("close consumer data error socket");
417 * Copy consumer output from the tracing session to the domain session. The
418 * function also applies the right modification on a per domain basis for the
419 * trace files destination directory.
421 * Should *NOT* be called with RCU read-side lock held.
423 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
426 const char *dir_name
;
427 struct consumer_output
*consumer
;
430 assert(session
->consumer
);
433 case LTTNG_DOMAIN_KERNEL
:
434 DBG3("Copying tracing session consumer output in kernel session");
436 * XXX: We should audit the session creation and what this function
437 * does "extra" in order to avoid a destroy since this function is used
438 * in the domain session creation (kernel and ust) only. Same for UST
441 if (session
->kernel_session
->consumer
) {
442 consumer_output_put(session
->kernel_session
->consumer
);
444 session
->kernel_session
->consumer
=
445 consumer_copy_output(session
->consumer
);
446 /* Ease our life a bit for the next part */
447 consumer
= session
->kernel_session
->consumer
;
448 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
450 case LTTNG_DOMAIN_JUL
:
451 case LTTNG_DOMAIN_LOG4J
:
452 case LTTNG_DOMAIN_PYTHON
:
453 case LTTNG_DOMAIN_UST
:
454 DBG3("Copying tracing session consumer output in UST session");
455 if (session
->ust_session
->consumer
) {
456 consumer_output_put(session
->ust_session
->consumer
);
458 session
->ust_session
->consumer
=
459 consumer_copy_output(session
->consumer
);
460 /* Ease our life a bit for the next part */
461 consumer
= session
->ust_session
->consumer
;
462 dir_name
= DEFAULT_UST_TRACE_DIR
;
465 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
469 /* Append correct directory to subdir */
470 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
471 sizeof(consumer
->domain_subdir
));
476 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
484 * Create an UST session and add it to the session ust list.
486 * Should *NOT* be called with RCU read-side lock held.
488 static int create_ust_session(struct ltt_session
*session
,
489 const struct lttng_domain
*domain
)
492 struct ltt_ust_session
*lus
= NULL
;
496 assert(session
->consumer
);
498 switch (domain
->type
) {
499 case LTTNG_DOMAIN_JUL
:
500 case LTTNG_DOMAIN_LOG4J
:
501 case LTTNG_DOMAIN_PYTHON
:
502 case LTTNG_DOMAIN_UST
:
505 ERR("Unknown UST domain on create session %d", domain
->type
);
506 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
510 DBG("Creating UST session");
512 lus
= trace_ust_create_session(session
->id
);
514 ret
= LTTNG_ERR_UST_SESS_FAIL
;
518 lus
->uid
= session
->uid
;
519 lus
->gid
= session
->gid
;
520 lus
->output_traces
= session
->output_traces
;
521 lus
->snapshot_mode
= session
->snapshot_mode
;
522 lus
->live_timer_interval
= session
->live_timer
;
523 session
->ust_session
= lus
;
524 if (session
->shm_path
[0]) {
525 strncpy(lus
->root_shm_path
, session
->shm_path
,
526 sizeof(lus
->root_shm_path
));
527 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
528 strncpy(lus
->shm_path
, session
->shm_path
,
529 sizeof(lus
->shm_path
));
530 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
531 strncat(lus
->shm_path
, "/ust",
532 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
534 /* Copy session output to the newly created UST session */
535 ret
= copy_session_consumer(domain
->type
, session
);
536 if (ret
!= LTTNG_OK
) {
544 session
->ust_session
= NULL
;
549 * Create a kernel tracer session then create the default channel.
551 static int create_kernel_session(struct ltt_session
*session
)
555 DBG("Creating kernel session");
557 ret
= kernel_create_session(session
);
559 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
563 /* Code flow safety */
564 assert(session
->kernel_session
);
566 /* Copy session output to the newly created Kernel session */
567 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
568 if (ret
!= LTTNG_OK
) {
572 session
->kernel_session
->uid
= session
->uid
;
573 session
->kernel_session
->gid
= session
->gid
;
574 session
->kernel_session
->output_traces
= session
->output_traces
;
575 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
576 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
581 trace_kernel_destroy_session(session
->kernel_session
);
582 session
->kernel_session
= NULL
;
588 * Count number of session permitted by uid/gid.
590 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
593 struct ltt_session
*session
;
594 const struct ltt_session_list
*session_list
= session_get_list();
596 DBG("Counting number of available session for UID %d", uid
);
597 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
598 if (!session_get(session
)) {
601 session_lock(session
);
602 /* Only count the sessions the user can control. */
603 if (session_access_ok(session
, uid
) &&
604 !session
->destroyed
) {
607 session_unlock(session
);
608 session_put(session
);
613 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
614 int *sock_error
, struct lttng_event
*event
)
617 struct lttng_userspace_probe_location
*probe_location
;
618 struct lttng_payload probe_location_payload
;
619 struct fd_handle
*handle
= NULL
;
622 * Create a payload to store the serialized version of the probe
625 lttng_payload_init(&probe_location_payload
);
627 ret
= lttng_dynamic_buffer_set_size(&probe_location_payload
.buffer
,
628 cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
);
630 ret
= LTTNG_ERR_NOMEM
;
635 * Receive the probe location.
637 ret
= lttcomm_recv_unix_sock(sock
, probe_location_payload
.buffer
.data
,
638 probe_location_payload
.buffer
.size
);
640 DBG("Nothing recv() from client var len data... continuing");
642 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
647 * Receive the file descriptor to the target binary from the client.
649 DBG("Receiving userspace probe target FD from client ...");
650 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
652 DBG("Nothing recv() from client userspace probe fd... continuing");
654 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
658 handle
= fd_handle_create(fd
);
660 ret
= LTTNG_ERR_NOMEM
;
664 /* Transferred to the handle. */
667 ret
= lttng_payload_push_fd_handle(&probe_location_payload
, handle
);
669 ERR("Failed to add userspace probe file descriptor to payload");
670 ret
= LTTNG_ERR_NOMEM
;
674 fd_handle_put(handle
);
678 struct lttng_payload_view view
= lttng_payload_view_from_payload(
679 &probe_location_payload
, 0, -1);
681 /* Extract the probe location from the serialized version. */
682 ret
= lttng_userspace_probe_location_create_from_payload(
683 &view
, &probe_location
);
686 WARN("Failed to create a userspace probe location from the received buffer");
687 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
691 /* Attach the probe location to the event. */
692 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
694 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
701 PERROR("Failed to close userspace probe location binary fd");
705 fd_handle_put(handle
);
706 lttng_payload_reset(&probe_location_payload
);
710 static enum lttng_error_code
receive_lttng_trigger(struct command_ctx
*cmd_ctx
,
713 struct lttng_trigger
**_trigger
)
717 ssize_t sock_recv_len
;
718 enum lttng_error_code ret_code
;
719 struct lttng_payload trigger_payload
;
720 struct lttng_trigger
*trigger
= NULL
;
722 lttng_payload_init(&trigger_payload
);
723 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
724 ret
= lttng_dynamic_buffer_set_size(
725 &trigger_payload
.buffer
, trigger_len
);
727 ret_code
= LTTNG_ERR_NOMEM
;
731 sock_recv_len
= lttcomm_recv_unix_sock(
732 sock
, trigger_payload
.buffer
.data
, trigger_len
);
733 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
734 ERR("Failed to receive trigger in command payload");
736 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
740 /* Receive fds, if any. */
741 if (cmd_ctx
->lsm
.fd_count
> 0) {
742 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
743 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
744 if (sock_recv_len
> 0 &&
745 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
746 ERR("Failed to receive all file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
747 cmd_ctx
->lsm
.fd_count
, (int) ret
);
748 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
751 } else if (sock_recv_len
<= 0) {
752 ERR("Failed to receive file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
753 cmd_ctx
->lsm
.fd_count
, (int) ret
);
754 ret_code
= LTTNG_ERR_FATAL
;
760 /* Deserialize trigger. */
762 struct lttng_payload_view view
=
763 lttng_payload_view_from_payload(
764 &trigger_payload
, 0, -1);
766 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
768 ERR("Invalid trigger received as part of command payload");
769 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
770 lttng_trigger_put(trigger
);
783 * Version of setup_lttng_msg() without command header.
785 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
786 void *payload_buf
, size_t payload_len
)
788 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
792 * Check if the current kernel tracer supports the session rotation feature.
793 * Return 1 if it does, 0 otherwise.
795 static int check_rotate_compatible(void)
799 if (the_kernel_tracer_version
.major
!= 2 ||
800 the_kernel_tracer_version
.minor
< 11) {
801 DBG("Kernel tracer version is not compatible with the rotation feature");
809 * Send data on a unix socket using the liblttsessiondcomm API.
811 * Return lttcomm error code.
813 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
816 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
818 /* Check valid length */
819 if (view
->buffer
.size
== 0) {
824 ret
= lttcomm_send_unix_sock(
825 sock
, view
->buffer
.data
, view
->buffer
.size
);
831 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
842 * Process the command requested by the lttng client within the command
843 * context structure. This function make sure that the return structure (llm)
844 * is set and ready for transmission before returning.
846 * Return any error encountered or 0 for success.
848 * "sock" is only used for special-case var. len data.
849 * A command may assume the ownership of the socket, in which case its value
850 * should be set to -1.
852 * Should *NOT* be called with RCU read-side lock held.
854 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
858 bool need_tracing_session
= true;
862 DBG("Processing client command '%s\' (%d)",
863 lttcomm_sessiond_command_str(cmd_ctx
->lsm
.cmd_type
),
864 cmd_ctx
->lsm
.cmd_type
);
866 assert(!rcu_read_ongoing());
870 switch (cmd_ctx
->lsm
.cmd_type
) {
871 case LTTNG_CREATE_SESSION_EXT
:
872 case LTTNG_DESTROY_SESSION
:
873 case LTTNG_LIST_SESSIONS
:
874 case LTTNG_LIST_DOMAINS
:
875 case LTTNG_START_TRACE
:
876 case LTTNG_STOP_TRACE
:
877 case LTTNG_DATA_PENDING
:
878 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
879 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
880 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
881 case LTTNG_SNAPSHOT_RECORD
:
882 case LTTNG_SAVE_SESSION
:
883 case LTTNG_SET_SESSION_SHM_PATH
:
884 case LTTNG_REGENERATE_METADATA
:
885 case LTTNG_REGENERATE_STATEDUMP
:
886 case LTTNG_ROTATE_SESSION
:
887 case LTTNG_ROTATION_GET_INFO
:
888 case LTTNG_ROTATION_SET_SCHEDULE
:
889 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
890 case LTTNG_CLEAR_SESSION
:
891 case LTTNG_LIST_TRIGGERS
:
898 /* Needs a functioning consumerd? */
899 switch (cmd_ctx
->lsm
.cmd_type
) {
900 case LTTNG_REGISTER_TRIGGER
:
901 case LTTNG_UNREGISTER_TRIGGER
:
902 need_consumerd
= false;
905 need_consumerd
= true;
909 if (the_config
.no_kernel
&& need_domain
&&
910 cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
912 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
914 ret
= LTTNG_ERR_KERN_NA
;
919 /* Deny register consumer if we already have a spawned consumer. */
920 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
921 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
922 if (the_kconsumer_data
.pid
> 0) {
923 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
924 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
927 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
931 * Check for command that don't needs to allocate a returned payload. We do
932 * this here so we don't have to make the call for no payload at each
935 switch(cmd_ctx
->lsm
.cmd_type
) {
936 case LTTNG_LIST_SESSIONS
:
937 case LTTNG_LIST_TRACEPOINTS
:
938 case LTTNG_LIST_TRACEPOINT_FIELDS
:
939 case LTTNG_LIST_DOMAINS
:
940 case LTTNG_LIST_CHANNELS
:
941 case LTTNG_LIST_EVENTS
:
942 case LTTNG_LIST_SYSCALLS
:
943 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
944 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
945 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
946 case LTTNG_DATA_PENDING
:
947 case LTTNG_ROTATE_SESSION
:
948 case LTTNG_ROTATION_GET_INFO
:
949 case LTTNG_REGISTER_TRIGGER
:
950 case LTTNG_LIST_TRIGGERS
:
953 /* Setup lttng message with no payload */
954 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
956 /* This label does not try to unlock the session */
957 goto init_setup_error
;
961 /* Commands that DO NOT need a session. */
962 switch (cmd_ctx
->lsm
.cmd_type
) {
963 case LTTNG_CREATE_SESSION_EXT
:
964 case LTTNG_LIST_SESSIONS
:
965 case LTTNG_LIST_TRACEPOINTS
:
966 case LTTNG_LIST_SYSCALLS
:
967 case LTTNG_LIST_TRACEPOINT_FIELDS
:
968 case LTTNG_SAVE_SESSION
:
969 case LTTNG_REGISTER_TRIGGER
:
970 case LTTNG_UNREGISTER_TRIGGER
:
971 case LTTNG_LIST_TRIGGERS
:
972 need_tracing_session
= false;
975 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
977 * We keep the session list lock across _all_ commands
978 * for now, because the per-session lock does not
979 * handle teardown properly.
982 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
983 if (cmd_ctx
->session
== NULL
) {
984 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
987 /* Acquire lock for the session */
988 session_lock(cmd_ctx
->session
);
994 * Commands that need a valid session but should NOT create one if none
995 * exists. Instead of creating one and destroying it when the command is
996 * handled, process that right before so we save some round trip in useless
999 switch (cmd_ctx
->lsm
.cmd_type
) {
1000 case LTTNG_DISABLE_CHANNEL
:
1001 case LTTNG_DISABLE_EVENT
:
1002 switch (cmd_ctx
->lsm
.domain
.type
) {
1003 case LTTNG_DOMAIN_KERNEL
:
1004 if (!cmd_ctx
->session
->kernel_session
) {
1005 ret
= LTTNG_ERR_NO_CHANNEL
;
1009 case LTTNG_DOMAIN_JUL
:
1010 case LTTNG_DOMAIN_LOG4J
:
1011 case LTTNG_DOMAIN_PYTHON
:
1012 case LTTNG_DOMAIN_UST
:
1013 if (!cmd_ctx
->session
->ust_session
) {
1014 ret
= LTTNG_ERR_NO_CHANNEL
;
1019 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1031 * Check domain type for specific "pre-action".
1033 switch (cmd_ctx
->lsm
.domain
.type
) {
1034 case LTTNG_DOMAIN_KERNEL
:
1036 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1040 /* Kernel tracer check */
1041 if (!kernel_tracer_is_initialized()) {
1042 /* Basically, load kernel tracer modules */
1043 ret
= init_kernel_tracer();
1049 /* Consumer is in an ERROR state. Report back to client */
1050 if (need_consumerd
&& uatomic_read(&the_kernel_consumerd_state
) ==
1052 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1056 /* Need a session for kernel command */
1057 if (need_tracing_session
) {
1058 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1059 ret
= create_kernel_session(cmd_ctx
->session
);
1060 if (ret
!= LTTNG_OK
) {
1061 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1066 /* Start the kernel consumer daemon */
1067 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1068 if (the_kconsumer_data
.pid
== 0 &&
1069 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1070 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1071 ret
= start_consumerd(&the_kconsumer_data
);
1073 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1076 uatomic_set(&the_kernel_consumerd_state
, CONSUMER_STARTED
);
1078 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1082 * The consumer was just spawned so we need to add the socket to
1083 * the consumer output of the session if exist.
1085 ret
= consumer_create_socket(&the_kconsumer_data
,
1086 cmd_ctx
->session
->kernel_session
->consumer
);
1093 case LTTNG_DOMAIN_JUL
:
1094 case LTTNG_DOMAIN_LOG4J
:
1095 case LTTNG_DOMAIN_PYTHON
:
1096 if (!agent_tracing_is_enabled()) {
1097 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1101 case LTTNG_DOMAIN_UST
:
1103 if (!ust_app_supported()) {
1104 ret
= LTTNG_ERR_NO_UST
;
1108 /* Consumer is in an ERROR state. Report back to client */
1109 if (need_consumerd
&&
1110 uatomic_read(&the_ust_consumerd_state
) ==
1112 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1116 if (need_tracing_session
) {
1117 /* Create UST session if none exist. */
1118 if (cmd_ctx
->session
->ust_session
== NULL
) {
1119 ret
= create_ust_session(cmd_ctx
->session
,
1120 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
));
1121 if (ret
!= LTTNG_OK
) {
1126 /* Start the UST consumer daemons */
1128 pthread_mutex_lock(&the_ustconsumer64_data
.pid_mutex
);
1129 if (the_config
.consumerd64_bin_path
.value
&&
1130 the_ustconsumer64_data
.pid
== 0 &&
1131 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1132 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1133 ret
= start_consumerd(&the_ustconsumer64_data
);
1135 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1136 uatomic_set(&the_ust_consumerd64_fd
, -EINVAL
);
1140 uatomic_set(&the_ust_consumerd64_fd
, the_ustconsumer64_data
.cmd_sock
);
1141 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1143 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1147 * Setup socket for consumer 64 bit. No need for atomic access
1148 * since it was set above and can ONLY be set in this thread.
1150 ret
= consumer_create_socket(&the_ustconsumer64_data
,
1151 cmd_ctx
->session
->ust_session
->consumer
);
1157 pthread_mutex_lock(&the_ustconsumer32_data
.pid_mutex
);
1158 if (the_config
.consumerd32_bin_path
.value
&&
1159 the_ustconsumer32_data
.pid
== 0 &&
1160 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1161 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1162 ret
= start_consumerd(&the_ustconsumer32_data
);
1164 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1165 uatomic_set(&the_ust_consumerd32_fd
, -EINVAL
);
1169 uatomic_set(&the_ust_consumerd32_fd
, the_ustconsumer32_data
.cmd_sock
);
1170 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1172 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1176 * Setup socket for consumer 32 bit. No need for atomic access
1177 * since it was set above and can ONLY be set in this thread.
1179 ret
= consumer_create_socket(&the_ustconsumer32_data
,
1180 cmd_ctx
->session
->ust_session
->consumer
);
1192 /* Validate consumer daemon state when start/stop trace command */
1193 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1194 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1195 switch (cmd_ctx
->lsm
.domain
.type
) {
1196 case LTTNG_DOMAIN_NONE
:
1198 case LTTNG_DOMAIN_JUL
:
1199 case LTTNG_DOMAIN_LOG4J
:
1200 case LTTNG_DOMAIN_PYTHON
:
1201 case LTTNG_DOMAIN_UST
:
1202 if (uatomic_read(&the_ust_consumerd_state
) != CONSUMER_STARTED
) {
1203 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1207 case LTTNG_DOMAIN_KERNEL
:
1208 if (uatomic_read(&the_kernel_consumerd_state
) != CONSUMER_STARTED
) {
1209 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1214 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1220 * Check that the UID matches that of the tracing session.
1221 * The root user can interact with all sessions.
1223 if (need_tracing_session
) {
1224 if (!session_access_ok(cmd_ctx
->session
,
1225 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1226 cmd_ctx
->session
->destroyed
) {
1227 ret
= LTTNG_ERR_EPERM
;
1233 * Send relayd information to consumer as soon as we have a domain and a
1236 if (cmd_ctx
->session
&& need_domain
) {
1238 * Setup relayd if not done yet. If the relayd information was already
1239 * sent to the consumer, this call will gracefully return.
1241 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1242 if (ret
!= LTTNG_OK
) {
1247 /* Process by command type */
1248 switch (cmd_ctx
->lsm
.cmd_type
) {
1249 case LTTNG_ADD_CONTEXT
:
1252 * An LTTNG_ADD_CONTEXT command might have a supplementary
1253 * payload if the context being added is an application context.
1255 if (cmd_ctx
->lsm
.u
.context
.ctx
.ctx
==
1256 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1257 char *provider_name
= NULL
, *context_name
= NULL
;
1258 size_t provider_name_len
=
1259 cmd_ctx
->lsm
.u
.context
.provider_name_len
;
1260 size_t context_name_len
=
1261 cmd_ctx
->lsm
.u
.context
.context_name_len
;
1263 if (provider_name_len
== 0 || context_name_len
== 0) {
1265 * Application provider and context names MUST
1268 ret
= -LTTNG_ERR_INVALID
;
1272 provider_name
= zmalloc(provider_name_len
+ 1);
1273 if (!provider_name
) {
1274 ret
= -LTTNG_ERR_NOMEM
;
1277 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
=
1280 context_name
= zmalloc(context_name_len
+ 1);
1281 if (!context_name
) {
1282 ret
= -LTTNG_ERR_NOMEM
;
1283 goto error_add_context
;
1285 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1288 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1291 goto error_add_context
;
1294 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1297 goto error_add_context
;
1302 * cmd_add_context assumes ownership of the provider and context
1305 ret
= cmd_add_context(cmd_ctx
->session
,
1306 cmd_ctx
->lsm
.domain
.type
,
1307 cmd_ctx
->lsm
.u
.context
.channel_name
,
1308 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.context
.ctx
),
1309 the_kernel_poll_pipe
[1]);
1311 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1312 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1314 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
);
1315 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1321 case LTTNG_DISABLE_CHANNEL
:
1323 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1324 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1327 case LTTNG_DISABLE_EVENT
:
1331 * FIXME: handle filter; for now we just receive the filter's
1332 * bytecode along with the filter expression which are sent by
1333 * liblttng-ctl and discard them.
1335 * This fixes an issue where the client may block while sending
1336 * the filter payload and encounter an error because the session
1337 * daemon closes the socket without ever handling this data.
1339 size_t count
= cmd_ctx
->lsm
.u
.disable
.expression_len
+
1340 cmd_ctx
->lsm
.u
.disable
.bytecode_len
;
1343 char data
[LTTNG_FILTER_MAX_LEN
];
1345 DBG("Discarding disable event command payload of size %zu", count
);
1347 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1348 count
> sizeof(data
) ? sizeof(data
) : count
);
1353 count
-= (size_t) ret
;
1356 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1357 cmd_ctx
->lsm
.u
.disable
.channel_name
,
1358 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.disable
.event
));
1361 case LTTNG_ENABLE_CHANNEL
:
1363 cmd_ctx
->lsm
.u
.channel
.chan
.attr
.extended
.ptr
=
1364 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
.u
.channel
.extended
;
1365 ret
= cmd_enable_channel(cmd_ctx
->session
,
1366 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1367 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.channel
.chan
),
1368 the_kernel_poll_pipe
[1]);
1371 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1372 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1374 struct lttng_dynamic_buffer payload
;
1375 struct lttng_buffer_view payload_view
;
1376 const bool add_value
=
1377 cmd_ctx
->lsm
.cmd_type
==
1378 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1379 const size_t name_len
=
1380 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1382 const enum lttng_domain_type domain_type
=
1383 (enum lttng_domain_type
)
1384 cmd_ctx
->lsm
.domain
.type
;
1385 const enum lttng_process_attr process_attr
=
1386 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1387 .process_attr_tracker_add_remove_include_value
1389 const enum lttng_process_attr_value_type value_type
=
1390 (enum lttng_process_attr_value_type
) cmd_ctx
1392 .process_attr_tracker_add_remove_include_value
1394 struct process_attr_value
*value
;
1395 enum lttng_error_code ret_code
;
1396 long login_name_max
;
1398 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1399 if (login_name_max
< 0) {
1400 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1401 ret
= LTTNG_ERR_INVALID
;
1405 /* Receive remaining variable length payload if applicable. */
1406 if (name_len
> login_name_max
) {
1408 * POSIX mandates user and group names that are at least
1409 * 8 characters long. Note that although shadow-utils
1410 * (useradd, groupaadd, etc.) use 32 chars as their
1411 * limit (from bits/utmp.h, UT_NAMESIZE),
1412 * LOGIN_NAME_MAX is defined to 256.
1414 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1415 add_value
? "addition" : "removal",
1416 name_len
, login_name_max
);
1417 ret
= LTTNG_ERR_INVALID
;
1421 lttng_dynamic_buffer_init(&payload
);
1422 if (name_len
!= 0) {
1424 * Receive variable payload for user/group name
1427 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1429 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1430 add_value
? "add" : "remove");
1431 ret
= LTTNG_ERR_NOMEM
;
1432 goto error_add_remove_tracker_value
;
1435 ret
= lttcomm_recv_unix_sock(
1436 *sock
, payload
.data
, name_len
);
1438 ERR("Failed to receive payload of %s process attribute tracker value argument",
1439 add_value
? "add" : "remove");
1441 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1442 goto error_add_remove_tracker_value
;
1446 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1447 &payload
, 0, name_len
);
1448 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1449 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1450 goto error_add_remove_tracker_value
;
1454 * Validate the value type and domains are legal for the process
1455 * attribute tracker that is specified and convert the value to
1456 * add/remove to the internal sessiond representation.
1458 ret_code
= process_attr_value_from_comm(domain_type
,
1459 process_attr
, value_type
,
1460 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1462 &payload_view
, &value
);
1463 if (ret_code
!= LTTNG_OK
) {
1465 goto error_add_remove_tracker_value
;
1469 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1470 cmd_ctx
->session
, domain_type
,
1471 process_attr
, value
);
1473 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1474 cmd_ctx
->session
, domain_type
,
1475 process_attr
, value
);
1477 process_attr_value_destroy(value
);
1478 error_add_remove_tracker_value
:
1479 lttng_dynamic_buffer_reset(&payload
);
1482 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1484 enum lttng_tracking_policy tracking_policy
;
1485 const enum lttng_domain_type domain_type
=
1486 (enum lttng_domain_type
)
1487 cmd_ctx
->lsm
.domain
.type
;
1488 const enum lttng_process_attr process_attr
=
1489 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1490 .process_attr_tracker_get_tracking_policy
1493 ret
= cmd_process_attr_tracker_get_tracking_policy(
1494 cmd_ctx
->session
, domain_type
, process_attr
,
1496 if (ret
!= LTTNG_OK
) {
1500 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1501 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1503 ret
= LTTNG_ERR_NOMEM
;
1509 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1511 const enum lttng_tracking_policy tracking_policy
=
1512 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1513 .process_attr_tracker_set_tracking_policy
1515 const enum lttng_domain_type domain_type
=
1516 (enum lttng_domain_type
)
1517 cmd_ctx
->lsm
.domain
.type
;
1518 const enum lttng_process_attr process_attr
=
1519 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1520 .process_attr_tracker_set_tracking_policy
1523 ret
= cmd_process_attr_tracker_set_tracking_policy(
1524 cmd_ctx
->session
, domain_type
, process_attr
,
1526 if (ret
!= LTTNG_OK
) {
1531 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1533 struct lttng_process_attr_values
*values
;
1534 struct lttng_dynamic_buffer reply
;
1535 const enum lttng_domain_type domain_type
=
1536 (enum lttng_domain_type
)
1537 cmd_ctx
->lsm
.domain
.type
;
1538 const enum lttng_process_attr process_attr
=
1539 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1540 .process_attr_tracker_get_inclusion_set
1543 ret
= cmd_process_attr_tracker_get_inclusion_set(
1544 cmd_ctx
->session
, domain_type
, process_attr
,
1546 if (ret
!= LTTNG_OK
) {
1550 lttng_dynamic_buffer_init(&reply
);
1551 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1553 goto error_tracker_get_inclusion_set
;
1556 ret
= setup_lttng_msg_no_cmd_header(
1557 cmd_ctx
, reply
.data
, reply
.size
);
1559 ret
= LTTNG_ERR_NOMEM
;
1560 goto error_tracker_get_inclusion_set
;
1564 error_tracker_get_inclusion_set
:
1565 lttng_process_attr_values_destroy(values
);
1566 lttng_dynamic_buffer_reset(&reply
);
1569 case LTTNG_ENABLE_EVENT
:
1571 struct lttng_event
*ev
= NULL
;
1572 struct lttng_event_exclusion
*exclusion
= NULL
;
1573 struct lttng_bytecode
*bytecode
= NULL
;
1574 char *filter_expression
= NULL
;
1576 /* Handle exclusion events and receive it from the client. */
1577 if (cmd_ctx
->lsm
.u
.enable
.exclusion_count
> 0) {
1578 size_t count
= cmd_ctx
->lsm
.u
.enable
.exclusion_count
;
1580 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1581 (count
* LTTNG_SYMBOL_NAME_LEN
));
1583 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1587 DBG("Receiving var len exclusion event list from client ...");
1588 exclusion
->count
= count
;
1589 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1590 count
* LTTNG_SYMBOL_NAME_LEN
);
1592 DBG("Nothing recv() from client var len data... continuing");
1595 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1600 /* Get filter expression from client. */
1601 if (cmd_ctx
->lsm
.u
.enable
.expression_len
> 0) {
1602 size_t expression_len
=
1603 cmd_ctx
->lsm
.u
.enable
.expression_len
;
1605 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1606 ret
= LTTNG_ERR_FILTER_INVAL
;
1611 filter_expression
= zmalloc(expression_len
);
1612 if (!filter_expression
) {
1614 ret
= LTTNG_ERR_FILTER_NOMEM
;
1618 /* Receive var. len. data */
1619 DBG("Receiving var len filter's expression from client ...");
1620 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1623 DBG("Nothing recv() from client var len data... continuing");
1625 free(filter_expression
);
1627 ret
= LTTNG_ERR_FILTER_INVAL
;
1632 /* Handle filter and get bytecode from client. */
1633 if (cmd_ctx
->lsm
.u
.enable
.bytecode_len
> 0) {
1634 size_t bytecode_len
= cmd_ctx
->lsm
.u
.enable
.bytecode_len
;
1636 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1637 ret
= LTTNG_ERR_FILTER_INVAL
;
1638 free(filter_expression
);
1643 bytecode
= zmalloc(bytecode_len
);
1645 free(filter_expression
);
1647 ret
= LTTNG_ERR_FILTER_NOMEM
;
1651 /* Receive var. len. data */
1652 DBG("Receiving var len filter's bytecode from client ...");
1653 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1655 DBG("Nothing recv() from client var len data... continuing");
1657 free(filter_expression
);
1660 ret
= LTTNG_ERR_FILTER_INVAL
;
1664 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1665 free(filter_expression
);
1668 ret
= LTTNG_ERR_FILTER_INVAL
;
1673 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.enable
.event
));
1675 DBG("Failed to copy event: %s",
1676 cmd_ctx
->lsm
.u
.enable
.event
.name
);
1677 free(filter_expression
);
1680 ret
= LTTNG_ERR_NOMEM
;
1685 if (cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
> 0) {
1686 /* Expect a userspace probe description. */
1687 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1689 free(filter_expression
);
1692 lttng_event_destroy(ev
);
1697 ret
= cmd_enable_event(cmd_ctx
->session
,
1698 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1699 cmd_ctx
->lsm
.u
.enable
.channel_name
,
1701 filter_expression
, bytecode
, exclusion
,
1702 the_kernel_poll_pipe
[1]);
1703 lttng_event_destroy(ev
);
1706 case LTTNG_LIST_TRACEPOINTS
:
1708 struct lttng_event
*events
;
1711 session_lock_list();
1712 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &events
);
1713 session_unlock_list();
1714 if (nb_events
< 0) {
1715 /* Return value is a negative lttng_error_code. */
1721 * Setup lttng message with payload size set to the event list size in
1722 * bytes and then copy list into the llm payload.
1724 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1725 sizeof(struct lttng_event
) * nb_events
);
1735 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1737 struct lttng_event_field
*fields
;
1740 session_lock_list();
1741 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1743 session_unlock_list();
1744 if (nb_fields
< 0) {
1745 /* Return value is a negative lttng_error_code. */
1751 * Setup lttng message with payload size set to the event list size in
1752 * bytes and then copy list into the llm payload.
1754 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1755 sizeof(struct lttng_event_field
) * nb_fields
);
1765 case LTTNG_LIST_SYSCALLS
:
1767 struct lttng_event
*events
;
1770 nb_events
= cmd_list_syscalls(&events
);
1771 if (nb_events
< 0) {
1772 /* Return value is a negative lttng_error_code. */
1778 * Setup lttng message with payload size set to the event list size in
1779 * bytes and then copy list into the llm payload.
1781 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1782 sizeof(struct lttng_event
) * nb_events
);
1792 case LTTNG_SET_CONSUMER_URI
:
1795 struct lttng_uri
*uris
;
1797 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1798 len
= nb_uri
* sizeof(struct lttng_uri
);
1801 ret
= LTTNG_ERR_INVALID
;
1805 uris
= zmalloc(len
);
1807 ret
= LTTNG_ERR_FATAL
;
1811 /* Receive variable len data */
1812 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1813 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1815 DBG("No URIs received from client... continuing");
1817 ret
= LTTNG_ERR_SESSION_FAIL
;
1822 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1824 if (ret
!= LTTNG_OK
) {
1831 case LTTNG_START_TRACE
:
1834 * On the first start, if we have a kernel session and we have
1835 * enabled time or size-based rotations, we have to make sure
1836 * the kernel tracer supports it.
1838 if (!cmd_ctx
->session
->has_been_started
&& \
1839 cmd_ctx
->session
->kernel_session
&& \
1840 (cmd_ctx
->session
->rotate_timer_period
|| \
1841 cmd_ctx
->session
->rotate_size
) && \
1842 !check_rotate_compatible()) {
1843 DBG("Kernel tracer version is not compatible with the rotation feature");
1844 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1847 ret
= cmd_start_trace(cmd_ctx
->session
);
1850 case LTTNG_STOP_TRACE
:
1852 ret
= cmd_stop_trace(cmd_ctx
->session
);
1855 case LTTNG_DESTROY_SESSION
:
1857 ret
= cmd_destroy_session(cmd_ctx
->session
,
1858 the_notification_thread_handle
, sock
);
1861 case LTTNG_LIST_DOMAINS
:
1864 struct lttng_domain
*domains
= NULL
;
1866 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1868 /* Return value is a negative lttng_error_code. */
1873 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1874 nb_dom
* sizeof(struct lttng_domain
));
1884 case LTTNG_LIST_CHANNELS
:
1886 ssize_t payload_size
;
1887 struct lttng_channel
*channels
= NULL
;
1889 payload_size
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1890 cmd_ctx
->session
, &channels
);
1891 if (payload_size
< 0) {
1892 /* Return value is a negative lttng_error_code. */
1893 ret
= -payload_size
;
1897 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1908 case LTTNG_LIST_EVENTS
:
1911 struct lttcomm_event_command_header cmd_header
= {};
1912 size_t original_payload_size
;
1913 size_t payload_size
;
1915 ret
= setup_empty_lttng_msg(cmd_ctx
);
1917 ret
= LTTNG_ERR_NOMEM
;
1921 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1923 /* Extended infos are included at the end of the payload. */
1924 list_ret
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1926 cmd_ctx
->lsm
.u
.list
.channel_name
,
1927 &cmd_ctx
->reply_payload
);
1929 /* Return value is a negative lttng_error_code. */
1934 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1935 sizeof(cmd_header
) - original_payload_size
;
1936 update_lttng_msg(cmd_ctx
, sizeof(cmd_header
), payload_size
);
1941 case LTTNG_LIST_SESSIONS
:
1943 unsigned int nr_sessions
;
1944 void *sessions_payload
;
1947 session_lock_list();
1948 nr_sessions
= lttng_sessions_count(
1949 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1950 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1952 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1953 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1954 sessions_payload
= zmalloc(payload_len
);
1956 if (!sessions_payload
) {
1957 session_unlock_list();
1962 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1963 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1964 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1965 session_unlock_list();
1967 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1969 free(sessions_payload
);
1978 case LTTNG_REGISTER_CONSUMER
:
1980 struct consumer_data
*cdata
;
1982 switch (cmd_ctx
->lsm
.domain
.type
) {
1983 case LTTNG_DOMAIN_KERNEL
:
1984 cdata
= &the_kconsumer_data
;
1987 ret
= LTTNG_ERR_UND
;
1991 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1992 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1995 case LTTNG_DATA_PENDING
:
1998 uint8_t pending_ret_byte
;
2000 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
2005 * This function may returns 0 or 1 to indicate whether or not
2006 * there is data pending. In case of error, it should return an
2007 * LTTNG_ERR code. However, some code paths may still return
2008 * a nondescript error code, which we handle by returning an
2011 if (pending_ret
== 0 || pending_ret
== 1) {
2013 * ret will be set to LTTNG_OK at the end of
2016 } else if (pending_ret
< 0) {
2017 ret
= LTTNG_ERR_UNK
;
2024 pending_ret_byte
= (uint8_t) pending_ret
;
2026 /* 1 byte to return whether or not data is pending */
2027 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
2028 &pending_ret_byte
, 1);
2037 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2039 uint32_t snapshot_id
;
2040 struct lttcomm_lttng_output_id reply
;
2042 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
2043 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
),
2045 if (ret
!= LTTNG_OK
) {
2048 reply
.id
= snapshot_id
;
2050 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
2056 /* Copy output list into message payload */
2060 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2062 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
2063 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
));
2066 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2069 struct lttng_snapshot_output
*outputs
= NULL
;
2071 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
2072 if (nb_output
< 0) {
2077 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
2078 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
2079 nb_output
* sizeof(struct lttng_snapshot_output
));
2089 case LTTNG_SNAPSHOT_RECORD
:
2091 ret
= cmd_snapshot_record(cmd_ctx
->session
,
2092 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_record
.output
),
2093 cmd_ctx
->lsm
.u
.snapshot_record
.wait
);
2096 case LTTNG_CREATE_SESSION_EXT
:
2098 struct lttng_dynamic_buffer payload
;
2099 struct lttng_session_descriptor
*return_descriptor
= NULL
;
2101 lttng_dynamic_buffer_init(&payload
);
2102 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2103 if (ret
!= LTTNG_OK
) {
2107 ret
= lttng_session_descriptor_serialize(return_descriptor
,
2110 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2111 lttng_session_descriptor_destroy(return_descriptor
);
2112 ret
= LTTNG_ERR_NOMEM
;
2115 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
2118 lttng_session_descriptor_destroy(return_descriptor
);
2119 ret
= LTTNG_ERR_NOMEM
;
2122 lttng_dynamic_buffer_reset(&payload
);
2123 lttng_session_descriptor_destroy(return_descriptor
);
2127 case LTTNG_SAVE_SESSION
:
2129 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
2133 case LTTNG_SET_SESSION_SHM_PATH
:
2135 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2136 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2139 case LTTNG_REGENERATE_METADATA
:
2141 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2144 case LTTNG_REGENERATE_STATEDUMP
:
2146 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2149 case LTTNG_REGISTER_TRIGGER
:
2151 struct lttng_trigger
*payload_trigger
;
2152 struct lttng_trigger
*return_trigger
;
2153 size_t original_reply_payload_size
;
2154 size_t reply_payload_size
;
2155 const struct lttng_credentials cmd_creds
= {
2156 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2157 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2160 ret
= setup_empty_lttng_msg(cmd_ctx
);
2162 ret
= LTTNG_ERR_NOMEM
;
2166 ret
= receive_lttng_trigger(
2167 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2168 if (ret
!= LTTNG_OK
) {
2172 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2174 ret
= cmd_register_trigger(&cmd_creds
, payload_trigger
,
2175 the_notification_thread_handle
,
2177 if (ret
!= LTTNG_OK
) {
2178 lttng_trigger_put(payload_trigger
);
2182 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2183 lttng_trigger_put(payload_trigger
);
2184 lttng_trigger_put(return_trigger
);
2186 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2187 ret
= LTTNG_ERR_NOMEM
;
2191 reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2192 original_reply_payload_size
;
2194 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2199 case LTTNG_UNREGISTER_TRIGGER
:
2201 struct lttng_trigger
*payload_trigger
;
2202 const struct lttng_credentials cmd_creds
= {
2203 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2204 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2207 ret
= receive_lttng_trigger(
2208 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2209 if (ret
!= LTTNG_OK
) {
2213 ret
= cmd_unregister_trigger(&cmd_creds
, payload_trigger
,
2214 the_notification_thread_handle
);
2215 lttng_trigger_put(payload_trigger
);
2218 case LTTNG_ROTATE_SESSION
:
2220 struct lttng_rotate_session_return rotate_return
;
2222 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2224 memset(&rotate_return
, 0, sizeof(rotate_return
));
2225 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2226 DBG("Kernel tracer version is not compatible with the rotation feature");
2227 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2231 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2233 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2239 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2240 sizeof(rotate_return
));
2249 case LTTNG_ROTATION_GET_INFO
:
2251 struct lttng_rotation_get_info_return get_info_return
;
2253 memset(&get_info_return
, 0, sizeof(get_info_return
));
2254 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2255 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2261 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2262 sizeof(get_info_return
));
2271 case LTTNG_ROTATION_SET_SCHEDULE
:
2274 enum lttng_rotation_schedule_type schedule_type
;
2277 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2278 DBG("Kernel tracer version does not support session rotations");
2279 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2283 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2284 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2285 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2287 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
, set_schedule
,
2288 schedule_type
, value
,
2289 the_notification_thread_handle
);
2290 if (ret
!= LTTNG_OK
) {
2296 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2298 struct lttng_session_list_schedules_return schedules
= {
2299 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2300 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2301 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2302 .size
.value
= cmd_ctx
->session
->rotate_size
,
2305 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2315 case LTTNG_CLEAR_SESSION
:
2317 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2320 case LTTNG_LIST_TRIGGERS
:
2322 struct lttng_triggers
*return_triggers
= NULL
;
2323 size_t original_payload_size
;
2324 size_t payload_size
;
2326 ret
= setup_empty_lttng_msg(cmd_ctx
);
2328 ret
= LTTNG_ERR_NOMEM
;
2332 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2334 ret
= cmd_list_triggers(cmd_ctx
, the_notification_thread_handle
,
2336 if (ret
!= LTTNG_OK
) {
2340 assert(return_triggers
);
2341 ret
= lttng_triggers_serialize(
2342 return_triggers
, &cmd_ctx
->reply_payload
);
2343 lttng_triggers_destroy(return_triggers
);
2345 ERR("Failed to serialize triggers in reply to `list triggers` command");
2346 ret
= LTTNG_ERR_NOMEM
;
2350 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2351 original_payload_size
;
2353 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2359 ret
= LTTNG_ERR_UND
;
2364 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2365 DBG("Missing llm header, creating one.");
2366 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2370 /* Set return code */
2371 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2373 if (cmd_ctx
->session
) {
2374 session_unlock(cmd_ctx
->session
);
2375 session_put(cmd_ctx
->session
);
2376 cmd_ctx
->session
= NULL
;
2378 if (need_tracing_session
) {
2379 session_unlock_list();
2382 assert(!rcu_read_ongoing());
2386 static int create_client_sock(void)
2388 int ret
, client_sock
;
2389 const mode_t old_umask
= umask(0);
2391 /* Create client tool unix socket */
2392 client_sock
= lttcomm_create_unix_sock(
2393 the_config
.client_unix_sock_path
.value
);
2394 if (client_sock
< 0) {
2395 ERR("Create unix sock failed: %s",
2396 the_config
.client_unix_sock_path
.value
);
2401 /* Set the cloexec flag */
2402 ret
= utils_set_fd_cloexec(client_sock
);
2404 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2405 "Continuing but note that the consumer daemon will have a "
2406 "reference to this socket on exec()", client_sock
);
2409 /* File permission MUST be 660 */
2410 ret
= chmod(the_config
.client_unix_sock_path
.value
,
2411 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2413 ERR("Set file permissions failed: %s",
2414 the_config
.client_unix_sock_path
.value
);
2416 (void) lttcomm_close_unix_sock(client_sock
);
2420 DBG("Created client socket (fd = %i)", client_sock
);
2427 static void cleanup_client_thread(void *data
)
2429 struct lttng_pipe
*quit_pipe
= data
;
2431 lttng_pipe_destroy(quit_pipe
);
2434 static void thread_init_cleanup(void *data
)
2436 set_thread_status(false);
2440 * This thread manage all clients request using the unix client socket for
2443 static void *thread_manage_clients(void *data
)
2445 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2447 uint32_t revents
, nb_fd
;
2448 struct lttng_poll_event events
;
2449 const int client_sock
= thread_state
.client_sock
;
2450 struct lttng_pipe
*quit_pipe
= data
;
2451 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2452 struct command_ctx cmd_ctx
= {};
2454 DBG("[thread] Manage client started");
2456 lttng_payload_init(&cmd_ctx
.reply_payload
);
2458 is_root
= (getuid() == 0);
2460 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2462 rcu_register_thread();
2464 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2466 health_code_update();
2468 ret
= lttcomm_listen_unix_sock(client_sock
);
2474 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2475 * more will be added to this poll set.
2477 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2479 goto error_create_poll
;
2482 /* Add the application registration socket */
2483 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2488 /* Add thread quit pipe */
2489 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2494 /* Set state as running. */
2495 set_thread_status(true);
2496 pthread_cleanup_pop(0);
2498 /* This testpoint is after we signal readiness to the parent. */
2499 if (testpoint(sessiond_thread_manage_clients
)) {
2503 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2507 health_code_update();
2510 const struct cmd_completion_handler
*cmd_completion_handler
;
2512 cmd_ctx
.creds
= (lttng_sock_cred
) {
2516 cmd_ctx
.session
= NULL
;
2517 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2518 cmd_ctx
.lttng_msg_size
= 0;
2520 DBG("Accepting client command ...");
2522 /* Inifinite blocking call, waiting for transmission */
2524 health_poll_entry();
2525 ret
= lttng_poll_wait(&events
, -1);
2529 * Restart interrupted system call.
2531 if (errno
== EINTR
) {
2539 for (i
= 0; i
< nb_fd
; i
++) {
2540 revents
= LTTNG_POLL_GETEV(&events
, i
);
2541 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2543 health_code_update();
2545 if (pollfd
== thread_quit_pipe_fd
) {
2549 /* Event on the registration socket */
2550 if (revents
& LPOLLIN
) {
2552 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2553 ERR("Client socket poll error");
2556 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2562 DBG("Wait for client response");
2564 health_code_update();
2566 sock
= lttcomm_accept_unix_sock(client_sock
);
2572 * Set the CLOEXEC flag. Return code is useless because either way, the
2575 (void) utils_set_fd_cloexec(sock
);
2577 /* Set socket option for credentials retrieval */
2578 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2583 health_code_update();
2586 * Data is received from the lttng client. The struct
2587 * lttcomm_session_msg (lsm) contains the command and data request of
2590 DBG("Receiving data from client ...");
2591 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2592 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2593 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2594 DBG("Incomplete recv() from client... continuing");
2603 health_code_update();
2605 // TODO: Validate cmd_ctx including sanity check for
2606 // security purpose.
2608 rcu_thread_online();
2610 * This function dispatch the work to the kernel or userspace tracer
2611 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2612 * informations for the client. The command context struct contains
2613 * everything this function may needs.
2615 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2616 rcu_thread_offline();
2626 * TODO: Inform client somehow of the fatal error. At
2627 * this point, ret < 0 means that a zmalloc failed
2628 * (ENOMEM). Error detected but still accept
2629 * command, unless a socket error has been
2635 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2636 WARN("Command returned an invalid status code, returning unknown error: "
2637 "command type = %s (%d), ret = %d",
2638 lttcomm_sessiond_command_str(cmd_ctx
.lsm
.cmd_type
),
2639 cmd_ctx
.lsm
.cmd_type
, ret
);
2640 ret
= LTTNG_ERR_UNK
;
2643 cmd_completion_handler
= cmd_pop_completion_handler();
2644 if (cmd_completion_handler
) {
2645 enum lttng_error_code completion_code
;
2647 completion_code
= cmd_completion_handler
->run(
2648 cmd_completion_handler
->data
);
2649 if (completion_code
!= LTTNG_OK
) {
2654 health_code_update();
2657 struct lttng_payload_view view
=
2658 lttng_payload_view_from_payload(
2659 &cmd_ctx
.reply_payload
,
2661 struct lttcomm_lttng_msg
*llm
= (typeof(
2662 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2664 assert(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2665 assert(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2667 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2669 DBG("Sending response (size: %d, retcode: %s (%d))",
2670 cmd_ctx
.lttng_msg_size
,
2671 lttng_strerror(-llm
->ret_code
),
2673 ret
= send_unix_sock(sock
, &view
);
2675 ERR("Failed to send data back to client");
2678 /* End of transmission */
2686 health_code_update();
2698 lttng_poll_clean(&events
);
2702 unlink(the_config
.client_unix_sock_path
.value
);
2703 ret
= close(client_sock
);
2710 ERR("Health error occurred in %s", __func__
);
2713 health_unregister(the_health_sessiond
);
2715 DBG("Client thread dying");
2716 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2717 rcu_unregister_thread();
2722 bool shutdown_client_thread(void *thread_data
)
2724 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2725 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2727 return notify_thread_pipe(write_fd
) == 1;
2730 struct lttng_thread
*launch_client_thread(void)
2732 bool thread_running
;
2733 struct lttng_pipe
*client_quit_pipe
;
2734 struct lttng_thread
*thread
= NULL
;
2735 int client_sock_fd
= -1;
2737 sem_init(&thread_state
.ready
, 0, 0);
2738 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2739 if (!client_quit_pipe
) {
2743 client_sock_fd
= create_client_sock();
2744 if (client_sock_fd
< 0) {
2748 thread_state
.client_sock
= client_sock_fd
;
2749 thread
= lttng_thread_create("Client management",
2750 thread_manage_clients
,
2751 shutdown_client_thread
,
2752 cleanup_client_thread
,
2757 /* The client thread now owns the client sock fd and the quit pipe. */
2758 client_sock_fd
= -1;
2759 client_quit_pipe
= NULL
;
2762 * This thread is part of the threads that need to be fully
2763 * initialized before the session daemon is marked as "ready".
2765 thread_running
= wait_thread_status();
2766 if (!thread_running
) {
2771 if (client_sock_fd
>= 0) {
2772 if (close(client_sock_fd
)) {
2773 PERROR("Failed to close client socket");
2776 lttng_thread_put(thread
);
2777 cleanup_client_thread(client_quit_pipe
);