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
);
779 lttng_payload_reset(&trigger_payload
);
784 * Version of setup_lttng_msg() without command header.
786 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
787 void *payload_buf
, size_t payload_len
)
789 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
793 * Check if the current kernel tracer supports the session rotation feature.
794 * Return 1 if it does, 0 otherwise.
796 static int check_rotate_compatible(void)
800 if (the_kernel_tracer_version
.major
!= 2 ||
801 the_kernel_tracer_version
.minor
< 11) {
802 DBG("Kernel tracer version is not compatible with the rotation feature");
810 * Send data on a unix socket using the liblttsessiondcomm API.
812 * Return lttcomm error code.
814 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
817 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
819 /* Check valid length */
820 if (view
->buffer
.size
== 0) {
825 ret
= lttcomm_send_unix_sock(
826 sock
, view
->buffer
.data
, view
->buffer
.size
);
832 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
843 * Process the command requested by the lttng client within the command
844 * context structure. This function make sure that the return structure (llm)
845 * is set and ready for transmission before returning.
847 * Return any error encountered or 0 for success.
849 * "sock" is only used for special-case var. len data.
850 * A command may assume the ownership of the socket, in which case its value
851 * should be set to -1.
853 * Should *NOT* be called with RCU read-side lock held.
855 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
859 bool need_tracing_session
= true;
863 DBG("Processing client command '%s\' (%d)",
864 lttcomm_sessiond_command_str(cmd_ctx
->lsm
.cmd_type
),
865 cmd_ctx
->lsm
.cmd_type
);
867 assert(!rcu_read_ongoing());
871 switch (cmd_ctx
->lsm
.cmd_type
) {
872 case LTTNG_CREATE_SESSION_EXT
:
873 case LTTNG_DESTROY_SESSION
:
874 case LTTNG_LIST_SESSIONS
:
875 case LTTNG_LIST_DOMAINS
:
876 case LTTNG_START_TRACE
:
877 case LTTNG_STOP_TRACE
:
878 case LTTNG_DATA_PENDING
:
879 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
880 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
881 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
882 case LTTNG_SNAPSHOT_RECORD
:
883 case LTTNG_SAVE_SESSION
:
884 case LTTNG_SET_SESSION_SHM_PATH
:
885 case LTTNG_REGENERATE_METADATA
:
886 case LTTNG_REGENERATE_STATEDUMP
:
887 case LTTNG_ROTATE_SESSION
:
888 case LTTNG_ROTATION_GET_INFO
:
889 case LTTNG_ROTATION_SET_SCHEDULE
:
890 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
891 case LTTNG_CLEAR_SESSION
:
892 case LTTNG_LIST_TRIGGERS
:
899 /* Needs a functioning consumerd? */
900 switch (cmd_ctx
->lsm
.cmd_type
) {
901 case LTTNG_REGISTER_TRIGGER
:
902 case LTTNG_UNREGISTER_TRIGGER
:
903 need_consumerd
= false;
906 need_consumerd
= true;
910 if (the_config
.no_kernel
&& need_domain
&&
911 cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
913 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
915 ret
= LTTNG_ERR_KERN_NA
;
920 /* Deny register consumer if we already have a spawned consumer. */
921 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
922 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
923 if (the_kconsumer_data
.pid
> 0) {
924 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
925 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
928 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
932 * Check for command that don't needs to allocate a returned payload. We do
933 * this here so we don't have to make the call for no payload at each
936 switch(cmd_ctx
->lsm
.cmd_type
) {
937 case LTTNG_LIST_SESSIONS
:
938 case LTTNG_LIST_TRACEPOINTS
:
939 case LTTNG_LIST_TRACEPOINT_FIELDS
:
940 case LTTNG_LIST_DOMAINS
:
941 case LTTNG_LIST_CHANNELS
:
942 case LTTNG_LIST_EVENTS
:
943 case LTTNG_LIST_SYSCALLS
:
944 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
945 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
946 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
947 case LTTNG_DATA_PENDING
:
948 case LTTNG_ROTATE_SESSION
:
949 case LTTNG_ROTATION_GET_INFO
:
950 case LTTNG_REGISTER_TRIGGER
:
951 case LTTNG_LIST_TRIGGERS
:
954 /* Setup lttng message with no payload */
955 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
957 /* This label does not try to unlock the session */
958 goto init_setup_error
;
962 /* Commands that DO NOT need a session. */
963 switch (cmd_ctx
->lsm
.cmd_type
) {
964 case LTTNG_CREATE_SESSION_EXT
:
965 case LTTNG_LIST_SESSIONS
:
966 case LTTNG_LIST_TRACEPOINTS
:
967 case LTTNG_LIST_SYSCALLS
:
968 case LTTNG_LIST_TRACEPOINT_FIELDS
:
969 case LTTNG_SAVE_SESSION
:
970 case LTTNG_REGISTER_TRIGGER
:
971 case LTTNG_UNREGISTER_TRIGGER
:
972 case LTTNG_LIST_TRIGGERS
:
973 need_tracing_session
= false;
976 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
978 * We keep the session list lock across _all_ commands
979 * for now, because the per-session lock does not
980 * handle teardown properly.
983 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
984 if (cmd_ctx
->session
== NULL
) {
985 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
988 /* Acquire lock for the session */
989 session_lock(cmd_ctx
->session
);
995 * Commands that need a valid session but should NOT create one if none
996 * exists. Instead of creating one and destroying it when the command is
997 * handled, process that right before so we save some round trip in useless
1000 switch (cmd_ctx
->lsm
.cmd_type
) {
1001 case LTTNG_DISABLE_CHANNEL
:
1002 case LTTNG_DISABLE_EVENT
:
1003 switch (cmd_ctx
->lsm
.domain
.type
) {
1004 case LTTNG_DOMAIN_KERNEL
:
1005 if (!cmd_ctx
->session
->kernel_session
) {
1006 ret
= LTTNG_ERR_NO_CHANNEL
;
1010 case LTTNG_DOMAIN_JUL
:
1011 case LTTNG_DOMAIN_LOG4J
:
1012 case LTTNG_DOMAIN_PYTHON
:
1013 case LTTNG_DOMAIN_UST
:
1014 if (!cmd_ctx
->session
->ust_session
) {
1015 ret
= LTTNG_ERR_NO_CHANNEL
;
1020 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1032 * Check domain type for specific "pre-action".
1034 switch (cmd_ctx
->lsm
.domain
.type
) {
1035 case LTTNG_DOMAIN_KERNEL
:
1037 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1041 /* Kernel tracer check */
1042 if (!kernel_tracer_is_initialized()) {
1043 /* Basically, load kernel tracer modules */
1044 ret
= init_kernel_tracer();
1050 /* Consumer is in an ERROR state. Report back to client */
1051 if (need_consumerd
&& uatomic_read(&the_kernel_consumerd_state
) ==
1053 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1057 /* Need a session for kernel command */
1058 if (need_tracing_session
) {
1059 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1060 ret
= create_kernel_session(cmd_ctx
->session
);
1061 if (ret
!= LTTNG_OK
) {
1062 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1067 /* Start the kernel consumer daemon */
1068 pthread_mutex_lock(&the_kconsumer_data
.pid_mutex
);
1069 if (the_kconsumer_data
.pid
== 0 &&
1070 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1071 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1072 ret
= start_consumerd(&the_kconsumer_data
);
1074 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1077 uatomic_set(&the_kernel_consumerd_state
, CONSUMER_STARTED
);
1079 pthread_mutex_unlock(&the_kconsumer_data
.pid_mutex
);
1083 * The consumer was just spawned so we need to add the socket to
1084 * the consumer output of the session if exist.
1086 ret
= consumer_create_socket(&the_kconsumer_data
,
1087 cmd_ctx
->session
->kernel_session
->consumer
);
1094 case LTTNG_DOMAIN_JUL
:
1095 case LTTNG_DOMAIN_LOG4J
:
1096 case LTTNG_DOMAIN_PYTHON
:
1097 if (!agent_tracing_is_enabled()) {
1098 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1102 case LTTNG_DOMAIN_UST
:
1104 if (!ust_app_supported()) {
1105 ret
= LTTNG_ERR_NO_UST
;
1109 /* Consumer is in an ERROR state. Report back to client */
1110 if (need_consumerd
&&
1111 uatomic_read(&the_ust_consumerd_state
) ==
1113 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1117 if (need_tracing_session
) {
1118 /* Create UST session if none exist. */
1119 if (cmd_ctx
->session
->ust_session
== NULL
) {
1120 ret
= create_ust_session(cmd_ctx
->session
,
1121 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
));
1122 if (ret
!= LTTNG_OK
) {
1127 /* Start the UST consumer daemons */
1129 pthread_mutex_lock(&the_ustconsumer64_data
.pid_mutex
);
1130 if (the_config
.consumerd64_bin_path
.value
&&
1131 the_ustconsumer64_data
.pid
== 0 &&
1132 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1133 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1134 ret
= start_consumerd(&the_ustconsumer64_data
);
1136 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1137 uatomic_set(&the_ust_consumerd64_fd
, -EINVAL
);
1141 uatomic_set(&the_ust_consumerd64_fd
, the_ustconsumer64_data
.cmd_sock
);
1142 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1144 pthread_mutex_unlock(&the_ustconsumer64_data
.pid_mutex
);
1148 * Setup socket for consumer 64 bit. No need for atomic access
1149 * since it was set above and can ONLY be set in this thread.
1151 ret
= consumer_create_socket(&the_ustconsumer64_data
,
1152 cmd_ctx
->session
->ust_session
->consumer
);
1158 pthread_mutex_lock(&the_ustconsumer32_data
.pid_mutex
);
1159 if (the_config
.consumerd32_bin_path
.value
&&
1160 the_ustconsumer32_data
.pid
== 0 &&
1161 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1162 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1163 ret
= start_consumerd(&the_ustconsumer32_data
);
1165 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1166 uatomic_set(&the_ust_consumerd32_fd
, -EINVAL
);
1170 uatomic_set(&the_ust_consumerd32_fd
, the_ustconsumer32_data
.cmd_sock
);
1171 uatomic_set(&the_ust_consumerd_state
, CONSUMER_STARTED
);
1173 pthread_mutex_unlock(&the_ustconsumer32_data
.pid_mutex
);
1177 * Setup socket for consumer 32 bit. No need for atomic access
1178 * since it was set above and can ONLY be set in this thread.
1180 ret
= consumer_create_socket(&the_ustconsumer32_data
,
1181 cmd_ctx
->session
->ust_session
->consumer
);
1193 /* Validate consumer daemon state when start/stop trace command */
1194 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1195 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1196 switch (cmd_ctx
->lsm
.domain
.type
) {
1197 case LTTNG_DOMAIN_NONE
:
1199 case LTTNG_DOMAIN_JUL
:
1200 case LTTNG_DOMAIN_LOG4J
:
1201 case LTTNG_DOMAIN_PYTHON
:
1202 case LTTNG_DOMAIN_UST
:
1203 if (uatomic_read(&the_ust_consumerd_state
) != CONSUMER_STARTED
) {
1204 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1208 case LTTNG_DOMAIN_KERNEL
:
1209 if (uatomic_read(&the_kernel_consumerd_state
) != CONSUMER_STARTED
) {
1210 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1215 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1221 * Check that the UID matches that of the tracing session.
1222 * The root user can interact with all sessions.
1224 if (need_tracing_session
) {
1225 if (!session_access_ok(cmd_ctx
->session
,
1226 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1227 cmd_ctx
->session
->destroyed
) {
1228 ret
= LTTNG_ERR_EPERM
;
1234 * Send relayd information to consumer as soon as we have a domain and a
1237 if (cmd_ctx
->session
&& need_domain
) {
1239 * Setup relayd if not done yet. If the relayd information was already
1240 * sent to the consumer, this call will gracefully return.
1242 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1243 if (ret
!= LTTNG_OK
) {
1248 /* Process by command type */
1249 switch (cmd_ctx
->lsm
.cmd_type
) {
1250 case LTTNG_ADD_CONTEXT
:
1253 * An LTTNG_ADD_CONTEXT command might have a supplementary
1254 * payload if the context being added is an application context.
1256 if (cmd_ctx
->lsm
.u
.context
.ctx
.ctx
==
1257 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1258 char *provider_name
= NULL
, *context_name
= NULL
;
1259 size_t provider_name_len
=
1260 cmd_ctx
->lsm
.u
.context
.provider_name_len
;
1261 size_t context_name_len
=
1262 cmd_ctx
->lsm
.u
.context
.context_name_len
;
1264 if (provider_name_len
== 0 || context_name_len
== 0) {
1266 * Application provider and context names MUST
1269 ret
= -LTTNG_ERR_INVALID
;
1273 provider_name
= zmalloc(provider_name_len
+ 1);
1274 if (!provider_name
) {
1275 ret
= -LTTNG_ERR_NOMEM
;
1278 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
=
1281 context_name
= zmalloc(context_name_len
+ 1);
1282 if (!context_name
) {
1283 ret
= -LTTNG_ERR_NOMEM
;
1284 goto error_add_context
;
1286 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1289 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1292 goto error_add_context
;
1295 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1298 goto error_add_context
;
1303 * cmd_add_context assumes ownership of the provider and context
1306 ret
= cmd_add_context(cmd_ctx
->session
,
1307 cmd_ctx
->lsm
.domain
.type
,
1308 cmd_ctx
->lsm
.u
.context
.channel_name
,
1309 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.context
.ctx
),
1310 the_kernel_poll_pipe
[1]);
1312 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1313 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1315 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
);
1316 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1322 case LTTNG_DISABLE_CHANNEL
:
1324 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1325 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1328 case LTTNG_DISABLE_EVENT
:
1332 * FIXME: handle filter; for now we just receive the filter's
1333 * bytecode along with the filter expression which are sent by
1334 * liblttng-ctl and discard them.
1336 * This fixes an issue where the client may block while sending
1337 * the filter payload and encounter an error because the session
1338 * daemon closes the socket without ever handling this data.
1340 size_t count
= cmd_ctx
->lsm
.u
.disable
.expression_len
+
1341 cmd_ctx
->lsm
.u
.disable
.bytecode_len
;
1344 char data
[LTTNG_FILTER_MAX_LEN
];
1346 DBG("Discarding disable event command payload of size %zu", count
);
1348 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1349 count
> sizeof(data
) ? sizeof(data
) : count
);
1354 count
-= (size_t) ret
;
1357 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1358 cmd_ctx
->lsm
.u
.disable
.channel_name
,
1359 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.disable
.event
));
1362 case LTTNG_ENABLE_CHANNEL
:
1364 cmd_ctx
->lsm
.u
.channel
.chan
.attr
.extended
.ptr
=
1365 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
.u
.channel
.extended
;
1366 ret
= cmd_enable_channel(cmd_ctx
->session
,
1367 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1368 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.channel
.chan
),
1369 the_kernel_poll_pipe
[1]);
1372 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1373 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1375 struct lttng_dynamic_buffer payload
;
1376 struct lttng_buffer_view payload_view
;
1377 const bool add_value
=
1378 cmd_ctx
->lsm
.cmd_type
==
1379 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1380 const size_t name_len
=
1381 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1383 const enum lttng_domain_type domain_type
=
1384 (enum lttng_domain_type
)
1385 cmd_ctx
->lsm
.domain
.type
;
1386 const enum lttng_process_attr process_attr
=
1387 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1388 .process_attr_tracker_add_remove_include_value
1390 const enum lttng_process_attr_value_type value_type
=
1391 (enum lttng_process_attr_value_type
) cmd_ctx
1393 .process_attr_tracker_add_remove_include_value
1395 struct process_attr_value
*value
;
1396 enum lttng_error_code ret_code
;
1397 long login_name_max
;
1399 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1400 if (login_name_max
< 0) {
1401 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1402 ret
= LTTNG_ERR_INVALID
;
1406 /* Receive remaining variable length payload if applicable. */
1407 if (name_len
> login_name_max
) {
1409 * POSIX mandates user and group names that are at least
1410 * 8 characters long. Note that although shadow-utils
1411 * (useradd, groupaadd, etc.) use 32 chars as their
1412 * limit (from bits/utmp.h, UT_NAMESIZE),
1413 * LOGIN_NAME_MAX is defined to 256.
1415 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1416 add_value
? "addition" : "removal",
1417 name_len
, login_name_max
);
1418 ret
= LTTNG_ERR_INVALID
;
1422 lttng_dynamic_buffer_init(&payload
);
1423 if (name_len
!= 0) {
1425 * Receive variable payload for user/group name
1428 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1430 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1431 add_value
? "add" : "remove");
1432 ret
= LTTNG_ERR_NOMEM
;
1433 goto error_add_remove_tracker_value
;
1436 ret
= lttcomm_recv_unix_sock(
1437 *sock
, payload
.data
, name_len
);
1439 ERR("Failed to receive payload of %s process attribute tracker value argument",
1440 add_value
? "add" : "remove");
1442 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1443 goto error_add_remove_tracker_value
;
1447 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1448 &payload
, 0, name_len
);
1449 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1450 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1451 goto error_add_remove_tracker_value
;
1455 * Validate the value type and domains are legal for the process
1456 * attribute tracker that is specified and convert the value to
1457 * add/remove to the internal sessiond representation.
1459 ret_code
= process_attr_value_from_comm(domain_type
,
1460 process_attr
, value_type
,
1461 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1463 &payload_view
, &value
);
1464 if (ret_code
!= LTTNG_OK
) {
1466 goto error_add_remove_tracker_value
;
1470 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1471 cmd_ctx
->session
, domain_type
,
1472 process_attr
, value
);
1474 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1475 cmd_ctx
->session
, domain_type
,
1476 process_attr
, value
);
1478 process_attr_value_destroy(value
);
1479 error_add_remove_tracker_value
:
1480 lttng_dynamic_buffer_reset(&payload
);
1483 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1485 enum lttng_tracking_policy tracking_policy
;
1486 const enum lttng_domain_type domain_type
=
1487 (enum lttng_domain_type
)
1488 cmd_ctx
->lsm
.domain
.type
;
1489 const enum lttng_process_attr process_attr
=
1490 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1491 .process_attr_tracker_get_tracking_policy
1494 ret
= cmd_process_attr_tracker_get_tracking_policy(
1495 cmd_ctx
->session
, domain_type
, process_attr
,
1497 if (ret
!= LTTNG_OK
) {
1501 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1502 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1504 ret
= LTTNG_ERR_NOMEM
;
1510 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1512 const enum lttng_tracking_policy tracking_policy
=
1513 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1514 .process_attr_tracker_set_tracking_policy
1516 const enum lttng_domain_type domain_type
=
1517 (enum lttng_domain_type
)
1518 cmd_ctx
->lsm
.domain
.type
;
1519 const enum lttng_process_attr process_attr
=
1520 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1521 .process_attr_tracker_set_tracking_policy
1524 ret
= cmd_process_attr_tracker_set_tracking_policy(
1525 cmd_ctx
->session
, domain_type
, process_attr
,
1527 if (ret
!= LTTNG_OK
) {
1532 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1534 struct lttng_process_attr_values
*values
;
1535 struct lttng_dynamic_buffer reply
;
1536 const enum lttng_domain_type domain_type
=
1537 (enum lttng_domain_type
)
1538 cmd_ctx
->lsm
.domain
.type
;
1539 const enum lttng_process_attr process_attr
=
1540 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1541 .process_attr_tracker_get_inclusion_set
1544 ret
= cmd_process_attr_tracker_get_inclusion_set(
1545 cmd_ctx
->session
, domain_type
, process_attr
,
1547 if (ret
!= LTTNG_OK
) {
1551 lttng_dynamic_buffer_init(&reply
);
1552 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1554 goto error_tracker_get_inclusion_set
;
1557 ret
= setup_lttng_msg_no_cmd_header(
1558 cmd_ctx
, reply
.data
, reply
.size
);
1560 ret
= LTTNG_ERR_NOMEM
;
1561 goto error_tracker_get_inclusion_set
;
1565 error_tracker_get_inclusion_set
:
1566 lttng_process_attr_values_destroy(values
);
1567 lttng_dynamic_buffer_reset(&reply
);
1570 case LTTNG_ENABLE_EVENT
:
1572 struct lttng_event
*ev
= NULL
;
1573 struct lttng_event_exclusion
*exclusion
= NULL
;
1574 struct lttng_bytecode
*bytecode
= NULL
;
1575 char *filter_expression
= NULL
;
1577 /* Handle exclusion events and receive it from the client. */
1578 if (cmd_ctx
->lsm
.u
.enable
.exclusion_count
> 0) {
1579 size_t count
= cmd_ctx
->lsm
.u
.enable
.exclusion_count
;
1581 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1582 (count
* LTTNG_SYMBOL_NAME_LEN
));
1584 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1588 DBG("Receiving var len exclusion event list from client ...");
1589 exclusion
->count
= count
;
1590 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1591 count
* LTTNG_SYMBOL_NAME_LEN
);
1593 DBG("Nothing recv() from client var len data... continuing");
1596 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1601 /* Get filter expression from client. */
1602 if (cmd_ctx
->lsm
.u
.enable
.expression_len
> 0) {
1603 size_t expression_len
=
1604 cmd_ctx
->lsm
.u
.enable
.expression_len
;
1606 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1607 ret
= LTTNG_ERR_FILTER_INVAL
;
1612 filter_expression
= zmalloc(expression_len
);
1613 if (!filter_expression
) {
1615 ret
= LTTNG_ERR_FILTER_NOMEM
;
1619 /* Receive var. len. data */
1620 DBG("Receiving var len filter's expression from client ...");
1621 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1624 DBG("Nothing recv() from client var len data... continuing");
1626 free(filter_expression
);
1628 ret
= LTTNG_ERR_FILTER_INVAL
;
1633 /* Handle filter and get bytecode from client. */
1634 if (cmd_ctx
->lsm
.u
.enable
.bytecode_len
> 0) {
1635 size_t bytecode_len
= cmd_ctx
->lsm
.u
.enable
.bytecode_len
;
1637 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1638 ret
= LTTNG_ERR_FILTER_INVAL
;
1639 free(filter_expression
);
1644 bytecode
= zmalloc(bytecode_len
);
1646 free(filter_expression
);
1648 ret
= LTTNG_ERR_FILTER_NOMEM
;
1652 /* Receive var. len. data */
1653 DBG("Receiving var len filter's bytecode from client ...");
1654 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1656 DBG("Nothing recv() from client var len data... continuing");
1658 free(filter_expression
);
1661 ret
= LTTNG_ERR_FILTER_INVAL
;
1665 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1666 free(filter_expression
);
1669 ret
= LTTNG_ERR_FILTER_INVAL
;
1674 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.enable
.event
));
1676 DBG("Failed to copy event: %s",
1677 cmd_ctx
->lsm
.u
.enable
.event
.name
);
1678 free(filter_expression
);
1681 ret
= LTTNG_ERR_NOMEM
;
1686 if (cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
> 0) {
1687 /* Expect a userspace probe description. */
1688 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1690 free(filter_expression
);
1693 lttng_event_destroy(ev
);
1698 ret
= cmd_enable_event(cmd_ctx
->session
,
1699 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1700 cmd_ctx
->lsm
.u
.enable
.channel_name
,
1702 filter_expression
, bytecode
, exclusion
,
1703 the_kernel_poll_pipe
[1]);
1704 lttng_event_destroy(ev
);
1707 case LTTNG_LIST_TRACEPOINTS
:
1709 struct lttng_event
*events
;
1712 session_lock_list();
1713 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &events
);
1714 session_unlock_list();
1715 if (nb_events
< 0) {
1716 /* Return value is a negative lttng_error_code. */
1722 * Setup lttng message with payload size set to the event list size in
1723 * bytes and then copy list into the llm payload.
1725 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1726 sizeof(struct lttng_event
) * nb_events
);
1736 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1738 struct lttng_event_field
*fields
;
1741 session_lock_list();
1742 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1744 session_unlock_list();
1745 if (nb_fields
< 0) {
1746 /* Return value is a negative lttng_error_code. */
1752 * Setup lttng message with payload size set to the event list size in
1753 * bytes and then copy list into the llm payload.
1755 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1756 sizeof(struct lttng_event_field
) * nb_fields
);
1766 case LTTNG_LIST_SYSCALLS
:
1768 struct lttng_event
*events
;
1771 nb_events
= cmd_list_syscalls(&events
);
1772 if (nb_events
< 0) {
1773 /* Return value is a negative lttng_error_code. */
1779 * Setup lttng message with payload size set to the event list size in
1780 * bytes and then copy list into the llm payload.
1782 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1783 sizeof(struct lttng_event
) * nb_events
);
1793 case LTTNG_SET_CONSUMER_URI
:
1796 struct lttng_uri
*uris
;
1798 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1799 len
= nb_uri
* sizeof(struct lttng_uri
);
1802 ret
= LTTNG_ERR_INVALID
;
1806 uris
= zmalloc(len
);
1808 ret
= LTTNG_ERR_FATAL
;
1812 /* Receive variable len data */
1813 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1814 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1816 DBG("No URIs received from client... continuing");
1818 ret
= LTTNG_ERR_SESSION_FAIL
;
1823 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1825 if (ret
!= LTTNG_OK
) {
1832 case LTTNG_START_TRACE
:
1835 * On the first start, if we have a kernel session and we have
1836 * enabled time or size-based rotations, we have to make sure
1837 * the kernel tracer supports it.
1839 if (!cmd_ctx
->session
->has_been_started
&& \
1840 cmd_ctx
->session
->kernel_session
&& \
1841 (cmd_ctx
->session
->rotate_timer_period
|| \
1842 cmd_ctx
->session
->rotate_size
) && \
1843 !check_rotate_compatible()) {
1844 DBG("Kernel tracer version is not compatible with the rotation feature");
1845 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1848 ret
= cmd_start_trace(cmd_ctx
->session
);
1851 case LTTNG_STOP_TRACE
:
1853 ret
= cmd_stop_trace(cmd_ctx
->session
);
1856 case LTTNG_DESTROY_SESSION
:
1858 ret
= cmd_destroy_session(cmd_ctx
->session
,
1859 the_notification_thread_handle
, sock
);
1862 case LTTNG_LIST_DOMAINS
:
1865 struct lttng_domain
*domains
= NULL
;
1867 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1869 /* Return value is a negative lttng_error_code. */
1874 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1875 nb_dom
* sizeof(struct lttng_domain
));
1885 case LTTNG_LIST_CHANNELS
:
1887 ssize_t payload_size
;
1888 struct lttng_channel
*channels
= NULL
;
1890 payload_size
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1891 cmd_ctx
->session
, &channels
);
1892 if (payload_size
< 0) {
1893 /* Return value is a negative lttng_error_code. */
1894 ret
= -payload_size
;
1898 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1909 case LTTNG_LIST_EVENTS
:
1912 struct lttcomm_event_command_header cmd_header
= {};
1913 size_t original_payload_size
;
1914 size_t payload_size
;
1916 ret
= setup_empty_lttng_msg(cmd_ctx
);
1918 ret
= LTTNG_ERR_NOMEM
;
1922 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1924 /* Extended infos are included at the end of the payload. */
1925 list_ret
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1927 cmd_ctx
->lsm
.u
.list
.channel_name
,
1928 &cmd_ctx
->reply_payload
);
1930 /* Return value is a negative lttng_error_code. */
1935 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1936 sizeof(cmd_header
) - original_payload_size
;
1937 update_lttng_msg(cmd_ctx
, sizeof(cmd_header
), payload_size
);
1942 case LTTNG_LIST_SESSIONS
:
1944 unsigned int nr_sessions
;
1945 void *sessions_payload
;
1948 session_lock_list();
1949 nr_sessions
= lttng_sessions_count(
1950 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1951 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1953 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1954 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1955 sessions_payload
= zmalloc(payload_len
);
1957 if (!sessions_payload
) {
1958 session_unlock_list();
1963 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1964 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1965 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1966 session_unlock_list();
1968 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1970 free(sessions_payload
);
1979 case LTTNG_REGISTER_CONSUMER
:
1981 struct consumer_data
*cdata
;
1983 switch (cmd_ctx
->lsm
.domain
.type
) {
1984 case LTTNG_DOMAIN_KERNEL
:
1985 cdata
= &the_kconsumer_data
;
1988 ret
= LTTNG_ERR_UND
;
1992 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1993 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1996 case LTTNG_DATA_PENDING
:
1999 uint8_t pending_ret_byte
;
2001 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
2006 * This function may returns 0 or 1 to indicate whether or not
2007 * there is data pending. In case of error, it should return an
2008 * LTTNG_ERR code. However, some code paths may still return
2009 * a nondescript error code, which we handle by returning an
2012 if (pending_ret
== 0 || pending_ret
== 1) {
2014 * ret will be set to LTTNG_OK at the end of
2017 } else if (pending_ret
< 0) {
2018 ret
= LTTNG_ERR_UNK
;
2025 pending_ret_byte
= (uint8_t) pending_ret
;
2027 /* 1 byte to return whether or not data is pending */
2028 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
2029 &pending_ret_byte
, 1);
2038 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2040 uint32_t snapshot_id
;
2041 struct lttcomm_lttng_output_id reply
;
2043 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
2044 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
),
2046 if (ret
!= LTTNG_OK
) {
2049 reply
.id
= snapshot_id
;
2051 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
2057 /* Copy output list into message payload */
2061 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2063 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
2064 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
));
2067 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2070 struct lttng_snapshot_output
*outputs
= NULL
;
2072 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
2073 if (nb_output
< 0) {
2078 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
2079 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
2080 nb_output
* sizeof(struct lttng_snapshot_output
));
2090 case LTTNG_SNAPSHOT_RECORD
:
2092 ret
= cmd_snapshot_record(cmd_ctx
->session
,
2093 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_record
.output
),
2094 cmd_ctx
->lsm
.u
.snapshot_record
.wait
);
2097 case LTTNG_CREATE_SESSION_EXT
:
2099 struct lttng_dynamic_buffer payload
;
2100 struct lttng_session_descriptor
*return_descriptor
= NULL
;
2102 lttng_dynamic_buffer_init(&payload
);
2103 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2104 if (ret
!= LTTNG_OK
) {
2108 ret
= lttng_session_descriptor_serialize(return_descriptor
,
2111 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2112 lttng_session_descriptor_destroy(return_descriptor
);
2113 ret
= LTTNG_ERR_NOMEM
;
2116 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
2119 lttng_session_descriptor_destroy(return_descriptor
);
2120 ret
= LTTNG_ERR_NOMEM
;
2123 lttng_dynamic_buffer_reset(&payload
);
2124 lttng_session_descriptor_destroy(return_descriptor
);
2128 case LTTNG_SAVE_SESSION
:
2130 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
2134 case LTTNG_SET_SESSION_SHM_PATH
:
2136 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2137 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2140 case LTTNG_REGENERATE_METADATA
:
2142 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2145 case LTTNG_REGENERATE_STATEDUMP
:
2147 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2150 case LTTNG_REGISTER_TRIGGER
:
2152 struct lttng_trigger
*payload_trigger
;
2153 struct lttng_trigger
*return_trigger
;
2154 size_t original_reply_payload_size
;
2155 size_t reply_payload_size
;
2156 const struct lttng_credentials cmd_creds
= {
2157 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2158 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2161 ret
= setup_empty_lttng_msg(cmd_ctx
);
2163 ret
= LTTNG_ERR_NOMEM
;
2167 ret
= receive_lttng_trigger(
2168 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2169 if (ret
!= LTTNG_OK
) {
2173 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2175 ret
= cmd_register_trigger(&cmd_creds
, payload_trigger
,
2176 the_notification_thread_handle
,
2178 if (ret
!= LTTNG_OK
) {
2179 lttng_trigger_put(payload_trigger
);
2183 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2184 lttng_trigger_put(payload_trigger
);
2185 lttng_trigger_put(return_trigger
);
2187 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2188 ret
= LTTNG_ERR_NOMEM
;
2192 reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2193 original_reply_payload_size
;
2195 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2200 case LTTNG_UNREGISTER_TRIGGER
:
2202 struct lttng_trigger
*payload_trigger
;
2203 const struct lttng_credentials cmd_creds
= {
2204 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2205 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2208 ret
= receive_lttng_trigger(
2209 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2210 if (ret
!= LTTNG_OK
) {
2214 ret
= cmd_unregister_trigger(&cmd_creds
, payload_trigger
,
2215 the_notification_thread_handle
);
2216 lttng_trigger_put(payload_trigger
);
2219 case LTTNG_ROTATE_SESSION
:
2221 struct lttng_rotate_session_return rotate_return
;
2223 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2225 memset(&rotate_return
, 0, sizeof(rotate_return
));
2226 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2227 DBG("Kernel tracer version is not compatible with the rotation feature");
2228 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2232 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2234 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2240 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2241 sizeof(rotate_return
));
2250 case LTTNG_ROTATION_GET_INFO
:
2252 struct lttng_rotation_get_info_return get_info_return
;
2254 memset(&get_info_return
, 0, sizeof(get_info_return
));
2255 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2256 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2262 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2263 sizeof(get_info_return
));
2272 case LTTNG_ROTATION_SET_SCHEDULE
:
2275 enum lttng_rotation_schedule_type schedule_type
;
2278 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2279 DBG("Kernel tracer version does not support session rotations");
2280 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2284 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2285 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2286 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2288 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
, set_schedule
,
2289 schedule_type
, value
,
2290 the_notification_thread_handle
);
2291 if (ret
!= LTTNG_OK
) {
2297 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2299 struct lttng_session_list_schedules_return schedules
= {
2300 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2301 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2302 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2303 .size
.value
= cmd_ctx
->session
->rotate_size
,
2306 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2316 case LTTNG_CLEAR_SESSION
:
2318 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2321 case LTTNG_LIST_TRIGGERS
:
2323 struct lttng_triggers
*return_triggers
= NULL
;
2324 size_t original_payload_size
;
2325 size_t payload_size
;
2327 ret
= setup_empty_lttng_msg(cmd_ctx
);
2329 ret
= LTTNG_ERR_NOMEM
;
2333 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2335 ret
= cmd_list_triggers(cmd_ctx
, the_notification_thread_handle
,
2337 if (ret
!= LTTNG_OK
) {
2341 assert(return_triggers
);
2342 ret
= lttng_triggers_serialize(
2343 return_triggers
, &cmd_ctx
->reply_payload
);
2344 lttng_triggers_destroy(return_triggers
);
2346 ERR("Failed to serialize triggers in reply to `list triggers` command");
2347 ret
= LTTNG_ERR_NOMEM
;
2351 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2352 original_payload_size
;
2354 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2360 ret
= LTTNG_ERR_UND
;
2365 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2366 DBG("Missing llm header, creating one.");
2367 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2371 /* Set return code */
2372 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2374 if (cmd_ctx
->session
) {
2375 session_unlock(cmd_ctx
->session
);
2376 session_put(cmd_ctx
->session
);
2377 cmd_ctx
->session
= NULL
;
2379 if (need_tracing_session
) {
2380 session_unlock_list();
2383 assert(!rcu_read_ongoing());
2387 static int create_client_sock(void)
2389 int ret
, client_sock
;
2390 const mode_t old_umask
= umask(0);
2392 /* Create client tool unix socket */
2393 client_sock
= lttcomm_create_unix_sock(
2394 the_config
.client_unix_sock_path
.value
);
2395 if (client_sock
< 0) {
2396 ERR("Create unix sock failed: %s",
2397 the_config
.client_unix_sock_path
.value
);
2402 /* Set the cloexec flag */
2403 ret
= utils_set_fd_cloexec(client_sock
);
2405 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2406 "Continuing but note that the consumer daemon will have a "
2407 "reference to this socket on exec()", client_sock
);
2410 /* File permission MUST be 660 */
2411 ret
= chmod(the_config
.client_unix_sock_path
.value
,
2412 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2414 ERR("Set file permissions failed: %s",
2415 the_config
.client_unix_sock_path
.value
);
2417 (void) lttcomm_close_unix_sock(client_sock
);
2421 DBG("Created client socket (fd = %i)", client_sock
);
2428 static void cleanup_client_thread(void *data
)
2430 struct lttng_pipe
*quit_pipe
= data
;
2432 lttng_pipe_destroy(quit_pipe
);
2435 static void thread_init_cleanup(void *data
)
2437 set_thread_status(false);
2441 * This thread manage all clients request using the unix client socket for
2444 static void *thread_manage_clients(void *data
)
2446 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2448 uint32_t revents
, nb_fd
;
2449 struct lttng_poll_event events
;
2450 const int client_sock
= thread_state
.client_sock
;
2451 struct lttng_pipe
*quit_pipe
= data
;
2452 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2453 struct command_ctx cmd_ctx
= {};
2455 DBG("[thread] Manage client started");
2457 lttng_payload_init(&cmd_ctx
.reply_payload
);
2459 is_root
= (getuid() == 0);
2461 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2463 rcu_register_thread();
2465 health_register(the_health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2467 health_code_update();
2469 ret
= lttcomm_listen_unix_sock(client_sock
);
2475 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2476 * more will be added to this poll set.
2478 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2480 goto error_create_poll
;
2483 /* Add the application registration socket */
2484 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2489 /* Add thread quit pipe */
2490 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2495 /* Set state as running. */
2496 set_thread_status(true);
2497 pthread_cleanup_pop(0);
2499 /* This testpoint is after we signal readiness to the parent. */
2500 if (testpoint(sessiond_thread_manage_clients
)) {
2504 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2508 health_code_update();
2511 const struct cmd_completion_handler
*cmd_completion_handler
;
2513 cmd_ctx
.creds
= (lttng_sock_cred
) {
2517 cmd_ctx
.session
= NULL
;
2518 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2519 cmd_ctx
.lttng_msg_size
= 0;
2521 DBG("Accepting client command ...");
2523 /* Inifinite blocking call, waiting for transmission */
2525 health_poll_entry();
2526 ret
= lttng_poll_wait(&events
, -1);
2530 * Restart interrupted system call.
2532 if (errno
== EINTR
) {
2540 for (i
= 0; i
< nb_fd
; i
++) {
2541 revents
= LTTNG_POLL_GETEV(&events
, i
);
2542 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2544 health_code_update();
2546 if (pollfd
== thread_quit_pipe_fd
) {
2550 /* Event on the registration socket */
2551 if (revents
& LPOLLIN
) {
2553 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2554 ERR("Client socket poll error");
2557 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2563 DBG("Wait for client response");
2565 health_code_update();
2567 sock
= lttcomm_accept_unix_sock(client_sock
);
2573 * Set the CLOEXEC flag. Return code is useless because either way, the
2576 (void) utils_set_fd_cloexec(sock
);
2578 /* Set socket option for credentials retrieval */
2579 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2584 health_code_update();
2587 * Data is received from the lttng client. The struct
2588 * lttcomm_session_msg (lsm) contains the command and data request of
2591 DBG("Receiving data from client ...");
2592 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2593 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2594 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2595 DBG("Incomplete recv() from client... continuing");
2604 health_code_update();
2606 // TODO: Validate cmd_ctx including sanity check for
2607 // security purpose.
2609 rcu_thread_online();
2611 * This function dispatch the work to the kernel or userspace tracer
2612 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2613 * informations for the client. The command context struct contains
2614 * everything this function may needs.
2616 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2617 rcu_thread_offline();
2627 * TODO: Inform client somehow of the fatal error. At
2628 * this point, ret < 0 means that a zmalloc failed
2629 * (ENOMEM). Error detected but still accept
2630 * command, unless a socket error has been
2636 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2637 WARN("Command returned an invalid status code, returning unknown error: "
2638 "command type = %s (%d), ret = %d",
2639 lttcomm_sessiond_command_str(cmd_ctx
.lsm
.cmd_type
),
2640 cmd_ctx
.lsm
.cmd_type
, ret
);
2641 ret
= LTTNG_ERR_UNK
;
2644 cmd_completion_handler
= cmd_pop_completion_handler();
2645 if (cmd_completion_handler
) {
2646 enum lttng_error_code completion_code
;
2648 completion_code
= cmd_completion_handler
->run(
2649 cmd_completion_handler
->data
);
2650 if (completion_code
!= LTTNG_OK
) {
2655 health_code_update();
2658 struct lttng_payload_view view
=
2659 lttng_payload_view_from_payload(
2660 &cmd_ctx
.reply_payload
,
2662 struct lttcomm_lttng_msg
*llm
= (typeof(
2663 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2665 assert(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2666 assert(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2668 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2670 DBG("Sending response (size: %d, retcode: %s (%d))",
2671 cmd_ctx
.lttng_msg_size
,
2672 lttng_strerror(-llm
->ret_code
),
2674 ret
= send_unix_sock(sock
, &view
);
2676 ERR("Failed to send data back to client");
2679 /* End of transmission */
2687 health_code_update();
2699 lttng_poll_clean(&events
);
2703 unlink(the_config
.client_unix_sock_path
.value
);
2704 ret
= close(client_sock
);
2711 ERR("Health error occurred in %s", __func__
);
2714 health_unregister(the_health_sessiond
);
2716 DBG("Client thread dying");
2717 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2718 rcu_unregister_thread();
2723 bool shutdown_client_thread(void *thread_data
)
2725 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2726 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2728 return notify_thread_pipe(write_fd
) == 1;
2731 struct lttng_thread
*launch_client_thread(void)
2733 bool thread_running
;
2734 struct lttng_pipe
*client_quit_pipe
;
2735 struct lttng_thread
*thread
= NULL
;
2736 int client_sock_fd
= -1;
2738 sem_init(&thread_state
.ready
, 0, 0);
2739 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2740 if (!client_quit_pipe
) {
2744 client_sock_fd
= create_client_sock();
2745 if (client_sock_fd
< 0) {
2749 thread_state
.client_sock
= client_sock_fd
;
2750 thread
= lttng_thread_create("Client management",
2751 thread_manage_clients
,
2752 shutdown_client_thread
,
2753 cleanup_client_thread
,
2758 /* The client thread now owns the client sock fd and the quit pipe. */
2759 client_sock_fd
= -1;
2760 client_quit_pipe
= NULL
;
2763 * This thread is part of the threads that need to be fully
2764 * initialized before the session daemon is marked as "ready".
2766 thread_running
= wait_thread_status();
2767 if (!thread_running
) {
2772 if (client_sock_fd
>= 0) {
2773 if (close(client_sock_fd
)) {
2774 PERROR("Failed to close client socket");
2777 lttng_thread_put(thread
);
2778 cleanup_client_thread(client_quit_pipe
);