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 (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", config
.consumerd64_bin_path
.value
? : "NULL");
228 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
229 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
230 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
231 DBG3("Found location #1");
232 consumer_to_use
= 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 (config
.consumerd32_bin_path
.value
&&
237 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
238 DBG3("Found location #3");
239 consumer_to_use
= 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
,
247 "lttng-consumerd", verbosity
, "-k",
248 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
249 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
250 "--group", config
.tracing_group_name
.value
,
253 case LTTNG_CONSUMER64_UST
:
255 if (config
.consumerd64_lib_dir
.value
) {
260 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
264 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
265 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
270 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
271 if (tmp
[0] != '\0') {
275 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
282 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
283 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
284 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
285 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
286 "--group", config
.tracing_group_name
.value
,
290 case LTTNG_CONSUMER32_UST
:
292 if (config
.consumerd32_lib_dir
.value
) {
297 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
301 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
302 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
307 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
308 if (tmp
[0] != '\0') {
312 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
319 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
320 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
321 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
322 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
323 "--group", config
.tracing_group_name
.value
,
328 ERR("unknown consumer type");
332 PERROR("Consumer execl()");
334 /* Reaching this point, we got a failure on our execl(). */
336 } else if (pid
> 0) {
339 PERROR("start consumer fork");
347 * Spawn the consumerd daemon and session daemon thread.
349 static int start_consumerd(struct consumer_data
*consumer_data
)
354 * Set the listen() state on the socket since there is a possible race
355 * between the exec() of the consumer daemon and this call if place in the
356 * consumer thread. See bug #366 for more details.
358 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
363 pthread_mutex_lock(&consumer_data
->pid_mutex
);
364 if (consumer_data
->pid
!= 0) {
365 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
369 ret
= spawn_consumerd(consumer_data
);
371 ERR("Spawning consumerd failed");
372 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
376 /* Setting up the consumer_data pid */
377 consumer_data
->pid
= ret
;
378 DBG2("Consumer pid %d", consumer_data
->pid
);
379 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
381 DBG2("Spawning consumer control thread");
382 ret
= spawn_consumer_thread(consumer_data
);
384 ERR("Fatal error spawning consumer control thread");
392 /* Cleanup already created sockets on error. */
393 if (consumer_data
->err_sock
>= 0) {
396 err
= close(consumer_data
->err_sock
);
398 PERROR("close consumer data error socket");
405 * Copy consumer output from the tracing session to the domain session. The
406 * function also applies the right modification on a per domain basis for the
407 * trace files destination directory.
409 * Should *NOT* be called with RCU read-side lock held.
411 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
414 const char *dir_name
;
415 struct consumer_output
*consumer
;
418 assert(session
->consumer
);
421 case LTTNG_DOMAIN_KERNEL
:
422 DBG3("Copying tracing session consumer output in kernel session");
424 * XXX: We should audit the session creation and what this function
425 * does "extra" in order to avoid a destroy since this function is used
426 * in the domain session creation (kernel and ust) only. Same for UST
429 if (session
->kernel_session
->consumer
) {
430 consumer_output_put(session
->kernel_session
->consumer
);
432 session
->kernel_session
->consumer
=
433 consumer_copy_output(session
->consumer
);
434 /* Ease our life a bit for the next part */
435 consumer
= session
->kernel_session
->consumer
;
436 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
438 case LTTNG_DOMAIN_JUL
:
439 case LTTNG_DOMAIN_LOG4J
:
440 case LTTNG_DOMAIN_PYTHON
:
441 case LTTNG_DOMAIN_UST
:
442 DBG3("Copying tracing session consumer output in UST session");
443 if (session
->ust_session
->consumer
) {
444 consumer_output_put(session
->ust_session
->consumer
);
446 session
->ust_session
->consumer
=
447 consumer_copy_output(session
->consumer
);
448 /* Ease our life a bit for the next part */
449 consumer
= session
->ust_session
->consumer
;
450 dir_name
= DEFAULT_UST_TRACE_DIR
;
453 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
457 /* Append correct directory to subdir */
458 ret
= lttng_strncpy(consumer
->domain_subdir
, dir_name
,
459 sizeof(consumer
->domain_subdir
));
464 DBG3("Copy session consumer subdir %s", consumer
->domain_subdir
);
472 * Create an UST session and add it to the session ust list.
474 * Should *NOT* be called with RCU read-side lock held.
476 static int create_ust_session(struct ltt_session
*session
,
477 const struct lttng_domain
*domain
)
480 struct ltt_ust_session
*lus
= NULL
;
484 assert(session
->consumer
);
486 switch (domain
->type
) {
487 case LTTNG_DOMAIN_JUL
:
488 case LTTNG_DOMAIN_LOG4J
:
489 case LTTNG_DOMAIN_PYTHON
:
490 case LTTNG_DOMAIN_UST
:
493 ERR("Unknown UST domain on create session %d", domain
->type
);
494 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
498 DBG("Creating UST session");
500 lus
= trace_ust_create_session(session
->id
);
502 ret
= LTTNG_ERR_UST_SESS_FAIL
;
506 lus
->uid
= session
->uid
;
507 lus
->gid
= session
->gid
;
508 lus
->output_traces
= session
->output_traces
;
509 lus
->snapshot_mode
= session
->snapshot_mode
;
510 lus
->live_timer_interval
= session
->live_timer
;
511 session
->ust_session
= lus
;
512 if (session
->shm_path
[0]) {
513 strncpy(lus
->root_shm_path
, session
->shm_path
,
514 sizeof(lus
->root_shm_path
));
515 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
516 strncpy(lus
->shm_path
, session
->shm_path
,
517 sizeof(lus
->shm_path
));
518 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
519 strncat(lus
->shm_path
, "/ust",
520 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
522 /* Copy session output to the newly created UST session */
523 ret
= copy_session_consumer(domain
->type
, session
);
524 if (ret
!= LTTNG_OK
) {
532 session
->ust_session
= NULL
;
537 * Create a kernel tracer session then create the default channel.
539 static int create_kernel_session(struct ltt_session
*session
)
543 DBG("Creating kernel session");
545 ret
= kernel_create_session(session
);
547 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
551 /* Code flow safety */
552 assert(session
->kernel_session
);
554 /* Copy session output to the newly created Kernel session */
555 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
556 if (ret
!= LTTNG_OK
) {
560 session
->kernel_session
->uid
= session
->uid
;
561 session
->kernel_session
->gid
= session
->gid
;
562 session
->kernel_session
->output_traces
= session
->output_traces
;
563 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
564 session
->kernel_session
->is_live_session
= session
->live_timer
!= 0;
569 trace_kernel_destroy_session(session
->kernel_session
);
570 session
->kernel_session
= NULL
;
576 * Count number of session permitted by uid/gid.
578 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
581 struct ltt_session
*session
;
582 const struct ltt_session_list
*session_list
= session_get_list();
584 DBG("Counting number of available session for UID %d", uid
);
585 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
586 if (!session_get(session
)) {
589 session_lock(session
);
590 /* Only count the sessions the user can control. */
591 if (session_access_ok(session
, uid
) &&
592 !session
->destroyed
) {
595 session_unlock(session
);
596 session_put(session
);
601 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
602 int *sock_error
, struct lttng_event
*event
)
605 struct lttng_userspace_probe_location
*probe_location
;
606 struct lttng_payload probe_location_payload
;
607 struct fd_handle
*handle
= NULL
;
610 * Create a payload to store the serialized version of the probe
613 lttng_payload_init(&probe_location_payload
);
615 ret
= lttng_dynamic_buffer_set_size(&probe_location_payload
.buffer
,
616 cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
);
618 ret
= LTTNG_ERR_NOMEM
;
623 * Receive the probe location.
625 ret
= lttcomm_recv_unix_sock(sock
, probe_location_payload
.buffer
.data
,
626 probe_location_payload
.buffer
.size
);
628 DBG("Nothing recv() from client var len data... continuing");
630 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
635 * Receive the file descriptor to the target binary from the client.
637 DBG("Receiving userspace probe target FD from client ...");
638 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
640 DBG("Nothing recv() from client userspace probe fd... continuing");
642 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
646 handle
= fd_handle_create(fd
);
648 ret
= LTTNG_ERR_NOMEM
;
652 /* Transferred to the handle. */
655 ret
= lttng_payload_push_fd_handle(&probe_location_payload
, handle
);
657 ERR("Failed to add userspace probe file descriptor to payload");
658 ret
= LTTNG_ERR_NOMEM
;
662 fd_handle_put(handle
);
666 struct lttng_payload_view view
= lttng_payload_view_from_payload(
667 &probe_location_payload
, 0, -1);
669 /* Extract the probe location from the serialized version. */
670 ret
= lttng_userspace_probe_location_create_from_payload(
671 &view
, &probe_location
);
674 WARN("Failed to create a userspace probe location from the received buffer");
675 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
679 /* Attach the probe location to the event. */
680 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
682 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
689 PERROR("Failed to close userspace probe location binary fd");
693 fd_handle_put(handle
);
694 lttng_payload_reset(&probe_location_payload
);
698 static enum lttng_error_code
receive_lttng_trigger(struct command_ctx
*cmd_ctx
,
701 struct lttng_trigger
**_trigger
)
705 ssize_t sock_recv_len
;
706 enum lttng_error_code ret_code
;
707 struct lttng_payload trigger_payload
;
708 struct lttng_trigger
*trigger
= NULL
;
710 lttng_payload_init(&trigger_payload
);
711 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
712 ret
= lttng_dynamic_buffer_set_size(
713 &trigger_payload
.buffer
, trigger_len
);
715 ret_code
= LTTNG_ERR_NOMEM
;
719 sock_recv_len
= lttcomm_recv_unix_sock(
720 sock
, trigger_payload
.buffer
.data
, trigger_len
);
721 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
722 ERR("Failed to receive trigger in command payload");
724 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
728 /* Receive fds, if any. */
729 if (cmd_ctx
->lsm
.fd_count
> 0) {
730 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
731 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
732 if (sock_recv_len
> 0 &&
733 sock_recv_len
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
734 ERR("Failed to receive all file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
735 cmd_ctx
->lsm
.fd_count
, (int) ret
);
736 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
739 } else if (sock_recv_len
<= 0) {
740 ERR("Failed to receive file descriptors for trigger in command payload: expected fd count = %u, ret = %d",
741 cmd_ctx
->lsm
.fd_count
, (int) ret
);
742 ret_code
= LTTNG_ERR_FATAL
;
748 /* Deserialize trigger. */
750 struct lttng_payload_view view
=
751 lttng_payload_view_from_payload(
752 &trigger_payload
, 0, -1);
754 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
756 ERR("Invalid trigger received as part of command payload");
757 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
758 lttng_trigger_put(trigger
);
771 * Version of setup_lttng_msg() without command header.
773 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
774 void *payload_buf
, size_t payload_len
)
776 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
780 * Check if the current kernel tracer supports the session rotation feature.
781 * Return 1 if it does, 0 otherwise.
783 static int check_rotate_compatible(void)
787 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
788 DBG("Kernel tracer version is not compatible with the rotation feature");
796 * Send data on a unix socket using the liblttsessiondcomm API.
798 * Return lttcomm error code.
800 static int send_unix_sock(int sock
, struct lttng_payload_view
*view
)
803 const int fd_count
= lttng_payload_view_get_fd_handle_count(view
);
805 /* Check valid length */
806 if (view
->buffer
.size
== 0) {
811 ret
= lttcomm_send_unix_sock(
812 sock
, view
->buffer
.data
, view
->buffer
.size
);
818 ret
= lttcomm_send_payload_view_fds_unix_sock(sock
, view
);
829 * Process the command requested by the lttng client within the command
830 * context structure. This function make sure that the return structure (llm)
831 * is set and ready for transmission before returning.
833 * Return any error encountered or 0 for success.
835 * "sock" is only used for special-case var. len data.
836 * A command may assume the ownership of the socket, in which case its value
837 * should be set to -1.
839 * Should *NOT* be called with RCU read-side lock held.
841 static int process_client_msg(struct command_ctx
*cmd_ctx
, int *sock
,
845 bool need_tracing_session
= true;
849 DBG("Processing client command '%s\' (%d)",
850 lttcomm_sessiond_command_str(cmd_ctx
->lsm
.cmd_type
),
851 cmd_ctx
->lsm
.cmd_type
);
853 assert(!rcu_read_ongoing());
857 switch (cmd_ctx
->lsm
.cmd_type
) {
858 case LTTNG_CREATE_SESSION_EXT
:
859 case LTTNG_DESTROY_SESSION
:
860 case LTTNG_LIST_SESSIONS
:
861 case LTTNG_LIST_DOMAINS
:
862 case LTTNG_START_TRACE
:
863 case LTTNG_STOP_TRACE
:
864 case LTTNG_DATA_PENDING
:
865 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
866 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
867 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
868 case LTTNG_SNAPSHOT_RECORD
:
869 case LTTNG_SAVE_SESSION
:
870 case LTTNG_SET_SESSION_SHM_PATH
:
871 case LTTNG_REGENERATE_METADATA
:
872 case LTTNG_REGENERATE_STATEDUMP
:
873 case LTTNG_ROTATE_SESSION
:
874 case LTTNG_ROTATION_GET_INFO
:
875 case LTTNG_ROTATION_SET_SCHEDULE
:
876 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
877 case LTTNG_CLEAR_SESSION
:
878 case LTTNG_LIST_TRIGGERS
:
885 /* Needs a functioning consumerd? */
886 switch (cmd_ctx
->lsm
.cmd_type
) {
887 case LTTNG_REGISTER_TRIGGER
:
888 case LTTNG_UNREGISTER_TRIGGER
:
889 need_consumerd
= false;
892 need_consumerd
= true;
896 if (config
.no_kernel
&& need_domain
897 && cmd_ctx
->lsm
.domain
.type
== LTTNG_DOMAIN_KERNEL
) {
899 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
901 ret
= LTTNG_ERR_KERN_NA
;
906 /* Deny register consumer if we already have a spawned consumer. */
907 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_REGISTER_CONSUMER
) {
908 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
909 if (kconsumer_data
.pid
> 0) {
910 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
911 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
914 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
918 * Check for command that don't needs to allocate a returned payload. We do
919 * this here so we don't have to make the call for no payload at each
922 switch(cmd_ctx
->lsm
.cmd_type
) {
923 case LTTNG_LIST_SESSIONS
:
924 case LTTNG_LIST_TRACEPOINTS
:
925 case LTTNG_LIST_TRACEPOINT_FIELDS
:
926 case LTTNG_LIST_DOMAINS
:
927 case LTTNG_LIST_CHANNELS
:
928 case LTTNG_LIST_EVENTS
:
929 case LTTNG_LIST_SYSCALLS
:
930 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
931 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
932 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
933 case LTTNG_DATA_PENDING
:
934 case LTTNG_ROTATE_SESSION
:
935 case LTTNG_ROTATION_GET_INFO
:
936 case LTTNG_REGISTER_TRIGGER
:
937 case LTTNG_LIST_TRIGGERS
:
940 /* Setup lttng message with no payload */
941 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
943 /* This label does not try to unlock the session */
944 goto init_setup_error
;
948 /* Commands that DO NOT need a session. */
949 switch (cmd_ctx
->lsm
.cmd_type
) {
950 case LTTNG_CREATE_SESSION_EXT
:
951 case LTTNG_LIST_SESSIONS
:
952 case LTTNG_LIST_TRACEPOINTS
:
953 case LTTNG_LIST_SYSCALLS
:
954 case LTTNG_LIST_TRACEPOINT_FIELDS
:
955 case LTTNG_SAVE_SESSION
:
956 case LTTNG_REGISTER_TRIGGER
:
957 case LTTNG_UNREGISTER_TRIGGER
:
958 case LTTNG_LIST_TRIGGERS
:
959 need_tracing_session
= false;
962 DBG("Getting session %s by name", cmd_ctx
->lsm
.session
.name
);
964 * We keep the session list lock across _all_ commands
965 * for now, because the per-session lock does not
966 * handle teardown properly.
969 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
.session
.name
);
970 if (cmd_ctx
->session
== NULL
) {
971 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
974 /* Acquire lock for the session */
975 session_lock(cmd_ctx
->session
);
981 * Commands that need a valid session but should NOT create one if none
982 * exists. Instead of creating one and destroying it when the command is
983 * handled, process that right before so we save some round trip in useless
986 switch (cmd_ctx
->lsm
.cmd_type
) {
987 case LTTNG_DISABLE_CHANNEL
:
988 case LTTNG_DISABLE_EVENT
:
989 switch (cmd_ctx
->lsm
.domain
.type
) {
990 case LTTNG_DOMAIN_KERNEL
:
991 if (!cmd_ctx
->session
->kernel_session
) {
992 ret
= LTTNG_ERR_NO_CHANNEL
;
996 case LTTNG_DOMAIN_JUL
:
997 case LTTNG_DOMAIN_LOG4J
:
998 case LTTNG_DOMAIN_PYTHON
:
999 case LTTNG_DOMAIN_UST
:
1000 if (!cmd_ctx
->session
->ust_session
) {
1001 ret
= LTTNG_ERR_NO_CHANNEL
;
1006 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1018 * Check domain type for specific "pre-action".
1020 switch (cmd_ctx
->lsm
.domain
.type
) {
1021 case LTTNG_DOMAIN_KERNEL
:
1023 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
1027 /* Kernel tracer check */
1028 if (!kernel_tracer_is_initialized()) {
1029 /* Basically, load kernel tracer modules */
1030 ret
= init_kernel_tracer();
1036 /* Consumer is in an ERROR state. Report back to client */
1037 if (need_consumerd
&& uatomic_read(&kernel_consumerd_state
) ==
1039 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1043 /* Need a session for kernel command */
1044 if (need_tracing_session
) {
1045 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1046 ret
= create_kernel_session(cmd_ctx
->session
);
1047 if (ret
!= LTTNG_OK
) {
1048 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
1053 /* Start the kernel consumer daemon */
1054 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
1055 if (kconsumer_data
.pid
== 0 &&
1056 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1057 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
1058 ret
= start_consumerd(&kconsumer_data
);
1060 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1063 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
1065 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
1069 * The consumer was just spawned so we need to add the socket to
1070 * the consumer output of the session if exist.
1072 ret
= consumer_create_socket(&kconsumer_data
,
1073 cmd_ctx
->session
->kernel_session
->consumer
);
1080 case LTTNG_DOMAIN_JUL
:
1081 case LTTNG_DOMAIN_LOG4J
:
1082 case LTTNG_DOMAIN_PYTHON
:
1083 if (!agent_tracing_is_enabled()) {
1084 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1088 case LTTNG_DOMAIN_UST
:
1090 if (!ust_app_supported()) {
1091 ret
= LTTNG_ERR_NO_UST
;
1095 /* Consumer is in an ERROR state. Report back to client */
1096 if (need_consumerd
&& uatomic_read(&ust_consumerd_state
) ==
1098 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1102 if (need_tracing_session
) {
1103 /* Create UST session if none exist. */
1104 if (cmd_ctx
->session
->ust_session
== NULL
) {
1105 ret
= create_ust_session(cmd_ctx
->session
,
1106 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
));
1107 if (ret
!= LTTNG_OK
) {
1112 /* Start the UST consumer daemons */
1114 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
1115 if (config
.consumerd64_bin_path
.value
&&
1116 ustconsumer64_data
.pid
== 0 &&
1117 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1118 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
1119 ret
= start_consumerd(&ustconsumer64_data
);
1121 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
1122 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
1126 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
1127 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1129 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
1133 * Setup socket for consumer 64 bit. No need for atomic access
1134 * since it was set above and can ONLY be set in this thread.
1136 ret
= consumer_create_socket(&ustconsumer64_data
,
1137 cmd_ctx
->session
->ust_session
->consumer
);
1143 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
1144 if (config
.consumerd32_bin_path
.value
&&
1145 ustconsumer32_data
.pid
== 0 &&
1146 cmd_ctx
->lsm
.cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1147 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1148 ret
= start_consumerd(&ustconsumer32_data
);
1150 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
1151 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
1155 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
1156 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
1158 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
1162 * Setup socket for consumer 32 bit. No need for atomic access
1163 * since it was set above and can ONLY be set in this thread.
1165 ret
= consumer_create_socket(&ustconsumer32_data
,
1166 cmd_ctx
->session
->ust_session
->consumer
);
1178 /* Validate consumer daemon state when start/stop trace command */
1179 if (cmd_ctx
->lsm
.cmd_type
== LTTNG_START_TRACE
||
1180 cmd_ctx
->lsm
.cmd_type
== LTTNG_STOP_TRACE
) {
1181 switch (cmd_ctx
->lsm
.domain
.type
) {
1182 case LTTNG_DOMAIN_NONE
:
1184 case LTTNG_DOMAIN_JUL
:
1185 case LTTNG_DOMAIN_LOG4J
:
1186 case LTTNG_DOMAIN_PYTHON
:
1187 case LTTNG_DOMAIN_UST
:
1188 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
1189 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
1193 case LTTNG_DOMAIN_KERNEL
:
1194 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
1195 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
1200 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1206 * Check that the UID matches that of the tracing session.
1207 * The root user can interact with all sessions.
1209 if (need_tracing_session
) {
1210 if (!session_access_ok(cmd_ctx
->session
,
1211 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
)) ||
1212 cmd_ctx
->session
->destroyed
) {
1213 ret
= LTTNG_ERR_EPERM
;
1219 * Send relayd information to consumer as soon as we have a domain and a
1222 if (cmd_ctx
->session
&& need_domain
) {
1224 * Setup relayd if not done yet. If the relayd information was already
1225 * sent to the consumer, this call will gracefully return.
1227 ret
= cmd_setup_relayd(cmd_ctx
->session
);
1228 if (ret
!= LTTNG_OK
) {
1233 /* Process by command type */
1234 switch (cmd_ctx
->lsm
.cmd_type
) {
1235 case LTTNG_ADD_CONTEXT
:
1238 * An LTTNG_ADD_CONTEXT command might have a supplementary
1239 * payload if the context being added is an application context.
1241 if (cmd_ctx
->lsm
.u
.context
.ctx
.ctx
==
1242 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1243 char *provider_name
= NULL
, *context_name
= NULL
;
1244 size_t provider_name_len
=
1245 cmd_ctx
->lsm
.u
.context
.provider_name_len
;
1246 size_t context_name_len
=
1247 cmd_ctx
->lsm
.u
.context
.context_name_len
;
1249 if (provider_name_len
== 0 || context_name_len
== 0) {
1251 * Application provider and context names MUST
1254 ret
= -LTTNG_ERR_INVALID
;
1258 provider_name
= zmalloc(provider_name_len
+ 1);
1259 if (!provider_name
) {
1260 ret
= -LTTNG_ERR_NOMEM
;
1263 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
=
1266 context_name
= zmalloc(context_name_len
+ 1);
1267 if (!context_name
) {
1268 ret
= -LTTNG_ERR_NOMEM
;
1269 goto error_add_context
;
1271 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
=
1274 ret
= lttcomm_recv_unix_sock(*sock
, provider_name
,
1277 goto error_add_context
;
1280 ret
= lttcomm_recv_unix_sock(*sock
, context_name
,
1283 goto error_add_context
;
1288 * cmd_add_context assumes ownership of the provider and context
1291 ret
= cmd_add_context(cmd_ctx
->session
,
1292 cmd_ctx
->lsm
.domain
.type
,
1293 cmd_ctx
->lsm
.u
.context
.channel_name
,
1294 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.context
.ctx
),
1295 kernel_poll_pipe
[1]);
1297 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
1298 cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
1300 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.provider_name
);
1301 free(cmd_ctx
->lsm
.u
.context
.ctx
.u
.app_ctx
.ctx_name
);
1307 case LTTNG_DISABLE_CHANNEL
:
1309 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1310 cmd_ctx
->lsm
.u
.disable
.channel_name
);
1313 case LTTNG_DISABLE_EVENT
:
1317 * FIXME: handle filter; for now we just receive the filter's
1318 * bytecode along with the filter expression which are sent by
1319 * liblttng-ctl and discard them.
1321 * This fixes an issue where the client may block while sending
1322 * the filter payload and encounter an error because the session
1323 * daemon closes the socket without ever handling this data.
1325 size_t count
= cmd_ctx
->lsm
.u
.disable
.expression_len
+
1326 cmd_ctx
->lsm
.u
.disable
.bytecode_len
;
1329 char data
[LTTNG_FILTER_MAX_LEN
];
1331 DBG("Discarding disable event command payload of size %zu", count
);
1333 ret
= lttcomm_recv_unix_sock(*sock
, data
,
1334 count
> sizeof(data
) ? sizeof(data
) : count
);
1339 count
-= (size_t) ret
;
1342 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1343 cmd_ctx
->lsm
.u
.disable
.channel_name
,
1344 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.disable
.event
));
1347 case LTTNG_ENABLE_CHANNEL
:
1349 cmd_ctx
->lsm
.u
.channel
.chan
.attr
.extended
.ptr
=
1350 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
.u
.channel
.extended
;
1351 ret
= cmd_enable_channel(cmd_ctx
->session
,
1352 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1353 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.channel
.chan
),
1354 kernel_poll_pipe
[1]);
1357 case LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
:
1358 case LTTNG_PROCESS_ATTR_TRACKER_REMOVE_INCLUDE_VALUE
:
1360 struct lttng_dynamic_buffer payload
;
1361 struct lttng_buffer_view payload_view
;
1362 const bool add_value
=
1363 cmd_ctx
->lsm
.cmd_type
==
1364 LTTNG_PROCESS_ATTR_TRACKER_ADD_INCLUDE_VALUE
;
1365 const size_t name_len
=
1366 cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1368 const enum lttng_domain_type domain_type
=
1369 (enum lttng_domain_type
)
1370 cmd_ctx
->lsm
.domain
.type
;
1371 const enum lttng_process_attr process_attr
=
1372 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1373 .process_attr_tracker_add_remove_include_value
1375 const enum lttng_process_attr_value_type value_type
=
1376 (enum lttng_process_attr_value_type
) cmd_ctx
1378 .process_attr_tracker_add_remove_include_value
1380 struct process_attr_value
*value
;
1381 enum lttng_error_code ret_code
;
1382 long login_name_max
;
1384 login_name_max
= sysconf(_SC_LOGIN_NAME_MAX
);
1385 if (login_name_max
< 0) {
1386 PERROR("Failed to get _SC_LOGIN_NAME_MAX system configuration");
1387 ret
= LTTNG_ERR_INVALID
;
1391 /* Receive remaining variable length payload if applicable. */
1392 if (name_len
> login_name_max
) {
1394 * POSIX mandates user and group names that are at least
1395 * 8 characters long. Note that although shadow-utils
1396 * (useradd, groupaadd, etc.) use 32 chars as their
1397 * limit (from bits/utmp.h, UT_NAMESIZE),
1398 * LOGIN_NAME_MAX is defined to 256.
1400 ERR("Rejecting process attribute tracker value %s as the provided exceeds the maximal allowed length: argument length = %zu, maximal length = %ld",
1401 add_value
? "addition" : "removal",
1402 name_len
, login_name_max
);
1403 ret
= LTTNG_ERR_INVALID
;
1407 lttng_dynamic_buffer_init(&payload
);
1408 if (name_len
!= 0) {
1410 * Receive variable payload for user/group name
1413 ret
= lttng_dynamic_buffer_set_size(&payload
, name_len
);
1415 ERR("Failed to allocate buffer to receive payload of %s process attribute tracker value argument",
1416 add_value
? "add" : "remove");
1417 ret
= LTTNG_ERR_NOMEM
;
1418 goto error_add_remove_tracker_value
;
1421 ret
= lttcomm_recv_unix_sock(
1422 *sock
, payload
.data
, name_len
);
1424 ERR("Failed to receive payload of %s process attribute tracker value argument",
1425 add_value
? "add" : "remove");
1427 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1428 goto error_add_remove_tracker_value
;
1432 payload_view
= lttng_buffer_view_from_dynamic_buffer(
1433 &payload
, 0, name_len
);
1434 if (name_len
> 0 && !lttng_buffer_view_is_valid(&payload_view
)) {
1435 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
1436 goto error_add_remove_tracker_value
;
1440 * Validate the value type and domains are legal for the process
1441 * attribute tracker that is specified and convert the value to
1442 * add/remove to the internal sessiond representation.
1444 ret_code
= process_attr_value_from_comm(domain_type
,
1445 process_attr
, value_type
,
1446 &cmd_ctx
->lsm
.u
.process_attr_tracker_add_remove_include_value
1448 &payload_view
, &value
);
1449 if (ret_code
!= LTTNG_OK
) {
1451 goto error_add_remove_tracker_value
;
1455 ret
= cmd_process_attr_tracker_inclusion_set_add_value(
1456 cmd_ctx
->session
, domain_type
,
1457 process_attr
, value
);
1459 ret
= cmd_process_attr_tracker_inclusion_set_remove_value(
1460 cmd_ctx
->session
, domain_type
,
1461 process_attr
, value
);
1463 process_attr_value_destroy(value
);
1464 error_add_remove_tracker_value
:
1465 lttng_dynamic_buffer_reset(&payload
);
1468 case LTTNG_PROCESS_ATTR_TRACKER_GET_POLICY
:
1470 enum lttng_tracking_policy tracking_policy
;
1471 const enum lttng_domain_type domain_type
=
1472 (enum lttng_domain_type
)
1473 cmd_ctx
->lsm
.domain
.type
;
1474 const enum lttng_process_attr process_attr
=
1475 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1476 .process_attr_tracker_get_tracking_policy
1479 ret
= cmd_process_attr_tracker_get_tracking_policy(
1480 cmd_ctx
->session
, domain_type
, process_attr
,
1482 if (ret
!= LTTNG_OK
) {
1486 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
1487 &(uint32_t){tracking_policy
}, sizeof(uint32_t));
1489 ret
= LTTNG_ERR_NOMEM
;
1495 case LTTNG_PROCESS_ATTR_TRACKER_SET_POLICY
:
1497 const enum lttng_tracking_policy tracking_policy
=
1498 (enum lttng_tracking_policy
) cmd_ctx
->lsm
.u
1499 .process_attr_tracker_set_tracking_policy
1501 const enum lttng_domain_type domain_type
=
1502 (enum lttng_domain_type
)
1503 cmd_ctx
->lsm
.domain
.type
;
1504 const enum lttng_process_attr process_attr
=
1505 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1506 .process_attr_tracker_set_tracking_policy
1509 ret
= cmd_process_attr_tracker_set_tracking_policy(
1510 cmd_ctx
->session
, domain_type
, process_attr
,
1512 if (ret
!= LTTNG_OK
) {
1517 case LTTNG_PROCESS_ATTR_TRACKER_GET_INCLUSION_SET
:
1519 struct lttng_process_attr_values
*values
;
1520 struct lttng_dynamic_buffer reply
;
1521 const enum lttng_domain_type domain_type
=
1522 (enum lttng_domain_type
)
1523 cmd_ctx
->lsm
.domain
.type
;
1524 const enum lttng_process_attr process_attr
=
1525 (enum lttng_process_attr
) cmd_ctx
->lsm
.u
1526 .process_attr_tracker_get_inclusion_set
1529 ret
= cmd_process_attr_tracker_get_inclusion_set(
1530 cmd_ctx
->session
, domain_type
, process_attr
,
1532 if (ret
!= LTTNG_OK
) {
1536 lttng_dynamic_buffer_init(&reply
);
1537 ret
= lttng_process_attr_values_serialize(values
, &reply
);
1539 goto error_tracker_get_inclusion_set
;
1542 ret
= setup_lttng_msg_no_cmd_header(
1543 cmd_ctx
, reply
.data
, reply
.size
);
1545 ret
= LTTNG_ERR_NOMEM
;
1546 goto error_tracker_get_inclusion_set
;
1550 error_tracker_get_inclusion_set
:
1551 lttng_process_attr_values_destroy(values
);
1552 lttng_dynamic_buffer_reset(&reply
);
1555 case LTTNG_ENABLE_EVENT
:
1557 struct lttng_event
*ev
= NULL
;
1558 struct lttng_event_exclusion
*exclusion
= NULL
;
1559 struct lttng_bytecode
*bytecode
= NULL
;
1560 char *filter_expression
= NULL
;
1562 /* Handle exclusion events and receive it from the client. */
1563 if (cmd_ctx
->lsm
.u
.enable
.exclusion_count
> 0) {
1564 size_t count
= cmd_ctx
->lsm
.u
.enable
.exclusion_count
;
1566 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
1567 (count
* LTTNG_SYMBOL_NAME_LEN
));
1569 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
1573 DBG("Receiving var len exclusion event list from client ...");
1574 exclusion
->count
= count
;
1575 ret
= lttcomm_recv_unix_sock(*sock
, exclusion
->names
,
1576 count
* LTTNG_SYMBOL_NAME_LEN
);
1578 DBG("Nothing recv() from client var len data... continuing");
1581 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
1586 /* Get filter expression from client. */
1587 if (cmd_ctx
->lsm
.u
.enable
.expression_len
> 0) {
1588 size_t expression_len
=
1589 cmd_ctx
->lsm
.u
.enable
.expression_len
;
1591 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
1592 ret
= LTTNG_ERR_FILTER_INVAL
;
1597 filter_expression
= zmalloc(expression_len
);
1598 if (!filter_expression
) {
1600 ret
= LTTNG_ERR_FILTER_NOMEM
;
1604 /* Receive var. len. data */
1605 DBG("Receiving var len filter's expression from client ...");
1606 ret
= lttcomm_recv_unix_sock(*sock
, filter_expression
,
1609 DBG("Nothing recv() from client var len data... continuing");
1611 free(filter_expression
);
1613 ret
= LTTNG_ERR_FILTER_INVAL
;
1618 /* Handle filter and get bytecode from client. */
1619 if (cmd_ctx
->lsm
.u
.enable
.bytecode_len
> 0) {
1620 size_t bytecode_len
= cmd_ctx
->lsm
.u
.enable
.bytecode_len
;
1622 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
1623 ret
= LTTNG_ERR_FILTER_INVAL
;
1624 free(filter_expression
);
1629 bytecode
= zmalloc(bytecode_len
);
1631 free(filter_expression
);
1633 ret
= LTTNG_ERR_FILTER_NOMEM
;
1637 /* Receive var. len. data */
1638 DBG("Receiving var len filter's bytecode from client ...");
1639 ret
= lttcomm_recv_unix_sock(*sock
, bytecode
, bytecode_len
);
1641 DBG("Nothing recv() from client var len data... continuing");
1643 free(filter_expression
);
1646 ret
= LTTNG_ERR_FILTER_INVAL
;
1650 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
1651 free(filter_expression
);
1654 ret
= LTTNG_ERR_FILTER_INVAL
;
1659 ev
= lttng_event_copy(ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.enable
.event
));
1661 DBG("Failed to copy event: %s",
1662 cmd_ctx
->lsm
.u
.enable
.event
.name
);
1663 free(filter_expression
);
1666 ret
= LTTNG_ERR_NOMEM
;
1671 if (cmd_ctx
->lsm
.u
.enable
.userspace_probe_location_len
> 0) {
1672 /* Expect a userspace probe description. */
1673 ret
= receive_userspace_probe(cmd_ctx
, *sock
, sock_error
, ev
);
1675 free(filter_expression
);
1678 lttng_event_destroy(ev
);
1683 ret
= cmd_enable_event(cmd_ctx
->session
,
1684 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.domain
),
1685 cmd_ctx
->lsm
.u
.enable
.channel_name
,
1687 filter_expression
, bytecode
, exclusion
,
1688 kernel_poll_pipe
[1]);
1689 lttng_event_destroy(ev
);
1692 case LTTNG_LIST_TRACEPOINTS
:
1694 struct lttng_event
*events
;
1697 session_lock_list();
1698 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
.domain
.type
, &events
);
1699 session_unlock_list();
1700 if (nb_events
< 0) {
1701 /* Return value is a negative lttng_error_code. */
1707 * Setup lttng message with payload size set to the event list size in
1708 * bytes and then copy list into the llm payload.
1710 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1711 sizeof(struct lttng_event
) * nb_events
);
1721 case LTTNG_LIST_TRACEPOINT_FIELDS
:
1723 struct lttng_event_field
*fields
;
1726 session_lock_list();
1727 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
.domain
.type
,
1729 session_unlock_list();
1730 if (nb_fields
< 0) {
1731 /* Return value is a negative lttng_error_code. */
1737 * Setup lttng message with payload size set to the event list size in
1738 * bytes and then copy list into the llm payload.
1740 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
1741 sizeof(struct lttng_event_field
) * nb_fields
);
1751 case LTTNG_LIST_SYSCALLS
:
1753 struct lttng_event
*events
;
1756 nb_events
= cmd_list_syscalls(&events
);
1757 if (nb_events
< 0) {
1758 /* Return value is a negative lttng_error_code. */
1764 * Setup lttng message with payload size set to the event list size in
1765 * bytes and then copy list into the llm payload.
1767 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
1768 sizeof(struct lttng_event
) * nb_events
);
1778 case LTTNG_SET_CONSUMER_URI
:
1781 struct lttng_uri
*uris
;
1783 nb_uri
= cmd_ctx
->lsm
.u
.uri
.size
;
1784 len
= nb_uri
* sizeof(struct lttng_uri
);
1787 ret
= LTTNG_ERR_INVALID
;
1791 uris
= zmalloc(len
);
1793 ret
= LTTNG_ERR_FATAL
;
1797 /* Receive variable len data */
1798 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
1799 ret
= lttcomm_recv_unix_sock(*sock
, uris
, len
);
1801 DBG("No URIs received from client... continuing");
1803 ret
= LTTNG_ERR_SESSION_FAIL
;
1808 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
1810 if (ret
!= LTTNG_OK
) {
1817 case LTTNG_START_TRACE
:
1820 * On the first start, if we have a kernel session and we have
1821 * enabled time or size-based rotations, we have to make sure
1822 * the kernel tracer supports it.
1824 if (!cmd_ctx
->session
->has_been_started
&& \
1825 cmd_ctx
->session
->kernel_session
&& \
1826 (cmd_ctx
->session
->rotate_timer_period
|| \
1827 cmd_ctx
->session
->rotate_size
) && \
1828 !check_rotate_compatible()) {
1829 DBG("Kernel tracer version is not compatible with the rotation feature");
1830 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
1833 ret
= cmd_start_trace(cmd_ctx
->session
);
1836 case LTTNG_STOP_TRACE
:
1838 ret
= cmd_stop_trace(cmd_ctx
->session
);
1841 case LTTNG_DESTROY_SESSION
:
1843 ret
= cmd_destroy_session(cmd_ctx
->session
,
1844 notification_thread_handle
,
1848 case LTTNG_LIST_DOMAINS
:
1851 struct lttng_domain
*domains
= NULL
;
1853 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
1855 /* Return value is a negative lttng_error_code. */
1860 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
1861 nb_dom
* sizeof(struct lttng_domain
));
1871 case LTTNG_LIST_CHANNELS
:
1873 ssize_t payload_size
;
1874 struct lttng_channel
*channels
= NULL
;
1876 payload_size
= cmd_list_channels(cmd_ctx
->lsm
.domain
.type
,
1877 cmd_ctx
->session
, &channels
);
1878 if (payload_size
< 0) {
1879 /* Return value is a negative lttng_error_code. */
1880 ret
= -payload_size
;
1884 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
1895 case LTTNG_LIST_EVENTS
:
1898 struct lttcomm_event_command_header cmd_header
= {};
1899 size_t original_payload_size
;
1900 size_t payload_size
;
1902 ret
= setup_empty_lttng_msg(cmd_ctx
);
1904 ret
= LTTNG_ERR_NOMEM
;
1908 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
1910 /* Extended infos are included at the end of the payload. */
1911 list_ret
= cmd_list_events(cmd_ctx
->lsm
.domain
.type
,
1913 cmd_ctx
->lsm
.u
.list
.channel_name
,
1914 &cmd_ctx
->reply_payload
);
1916 /* Return value is a negative lttng_error_code. */
1921 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
1922 sizeof(cmd_header
) - original_payload_size
;
1923 update_lttng_msg(cmd_ctx
, sizeof(cmd_header
), payload_size
);
1928 case LTTNG_LIST_SESSIONS
:
1930 unsigned int nr_sessions
;
1931 void *sessions_payload
;
1934 session_lock_list();
1935 nr_sessions
= lttng_sessions_count(
1936 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1937 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1939 payload_len
= (sizeof(struct lttng_session
) * nr_sessions
) +
1940 (sizeof(struct lttng_session_extended
) * nr_sessions
);
1941 sessions_payload
= zmalloc(payload_len
);
1943 if (!sessions_payload
) {
1944 session_unlock_list();
1949 cmd_list_lttng_sessions(sessions_payload
, nr_sessions
,
1950 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
1951 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
1952 session_unlock_list();
1954 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
1956 free(sessions_payload
);
1965 case LTTNG_REGISTER_CONSUMER
:
1967 struct consumer_data
*cdata
;
1969 switch (cmd_ctx
->lsm
.domain
.type
) {
1970 case LTTNG_DOMAIN_KERNEL
:
1971 cdata
= &kconsumer_data
;
1974 ret
= LTTNG_ERR_UND
;
1978 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
.domain
.type
,
1979 cmd_ctx
->lsm
.u
.reg
.path
, cdata
);
1982 case LTTNG_DATA_PENDING
:
1985 uint8_t pending_ret_byte
;
1987 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
1992 * This function may returns 0 or 1 to indicate whether or not
1993 * there is data pending. In case of error, it should return an
1994 * LTTNG_ERR code. However, some code paths may still return
1995 * a nondescript error code, which we handle by returning an
1998 if (pending_ret
== 0 || pending_ret
== 1) {
2000 * ret will be set to LTTNG_OK at the end of
2003 } else if (pending_ret
< 0) {
2004 ret
= LTTNG_ERR_UNK
;
2011 pending_ret_byte
= (uint8_t) pending_ret
;
2013 /* 1 byte to return whether or not data is pending */
2014 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
2015 &pending_ret_byte
, 1);
2024 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2026 uint32_t snapshot_id
;
2027 struct lttcomm_lttng_output_id reply
;
2029 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
2030 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
),
2032 if (ret
!= LTTNG_OK
) {
2035 reply
.id
= snapshot_id
;
2037 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
2043 /* Copy output list into message payload */
2047 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2049 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
2050 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_output
.output
));
2053 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2056 struct lttng_snapshot_output
*outputs
= NULL
;
2058 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
2059 if (nb_output
< 0) {
2064 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
2065 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
2066 nb_output
* sizeof(struct lttng_snapshot_output
));
2076 case LTTNG_SNAPSHOT_RECORD
:
2078 ret
= cmd_snapshot_record(cmd_ctx
->session
,
2079 ALIGNED_CONST_PTR(cmd_ctx
->lsm
.u
.snapshot_record
.output
),
2080 cmd_ctx
->lsm
.u
.snapshot_record
.wait
);
2083 case LTTNG_CREATE_SESSION_EXT
:
2085 struct lttng_dynamic_buffer payload
;
2086 struct lttng_session_descriptor
*return_descriptor
= NULL
;
2088 lttng_dynamic_buffer_init(&payload
);
2089 ret
= cmd_create_session(cmd_ctx
, *sock
, &return_descriptor
);
2090 if (ret
!= LTTNG_OK
) {
2094 ret
= lttng_session_descriptor_serialize(return_descriptor
,
2097 ERR("Failed to serialize session descriptor in reply to \"create session\" command");
2098 lttng_session_descriptor_destroy(return_descriptor
);
2099 ret
= LTTNG_ERR_NOMEM
;
2102 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, payload
.data
,
2105 lttng_session_descriptor_destroy(return_descriptor
);
2106 ret
= LTTNG_ERR_NOMEM
;
2109 lttng_dynamic_buffer_reset(&payload
);
2110 lttng_session_descriptor_destroy(return_descriptor
);
2114 case LTTNG_SAVE_SESSION
:
2116 ret
= cmd_save_sessions(&cmd_ctx
->lsm
.u
.save_session
.attr
,
2120 case LTTNG_SET_SESSION_SHM_PATH
:
2122 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
2123 cmd_ctx
->lsm
.u
.set_shm_path
.shm_path
);
2126 case LTTNG_REGENERATE_METADATA
:
2128 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
2131 case LTTNG_REGENERATE_STATEDUMP
:
2133 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
2136 case LTTNG_REGISTER_TRIGGER
:
2138 struct lttng_trigger
*payload_trigger
;
2139 struct lttng_trigger
*return_trigger
;
2140 size_t original_reply_payload_size
;
2141 size_t reply_payload_size
;
2142 const struct lttng_credentials cmd_creds
= {
2143 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2144 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2147 ret
= setup_empty_lttng_msg(cmd_ctx
);
2149 ret
= LTTNG_ERR_NOMEM
;
2153 ret
= receive_lttng_trigger(
2154 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2155 if (ret
!= LTTNG_OK
) {
2159 original_reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2161 ret
= cmd_register_trigger(&cmd_creds
, payload_trigger
,
2162 notification_thread_handle
, &return_trigger
);
2163 if (ret
!= LTTNG_OK
) {
2164 lttng_trigger_put(payload_trigger
);
2168 ret
= lttng_trigger_serialize(return_trigger
, &cmd_ctx
->reply_payload
);
2169 lttng_trigger_put(payload_trigger
);
2170 lttng_trigger_put(return_trigger
);
2172 ERR("Failed to serialize trigger in reply to \"register trigger\" command");
2173 ret
= LTTNG_ERR_NOMEM
;
2177 reply_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2178 original_reply_payload_size
;
2180 update_lttng_msg(cmd_ctx
, 0, reply_payload_size
);
2185 case LTTNG_UNREGISTER_TRIGGER
:
2187 struct lttng_trigger
*payload_trigger
;
2188 const struct lttng_credentials cmd_creds
= {
2189 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
2190 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
2193 ret
= receive_lttng_trigger(
2194 cmd_ctx
, *sock
, sock_error
, &payload_trigger
);
2195 if (ret
!= LTTNG_OK
) {
2199 ret
= cmd_unregister_trigger(&cmd_creds
, payload_trigger
,
2200 notification_thread_handle
);
2201 lttng_trigger_put(payload_trigger
);
2204 case LTTNG_ROTATE_SESSION
:
2206 struct lttng_rotate_session_return rotate_return
;
2208 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
2210 memset(&rotate_return
, 0, sizeof(rotate_return
));
2211 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2212 DBG("Kernel tracer version is not compatible with the rotation feature");
2213 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2217 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
,
2219 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
2225 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
2226 sizeof(rotate_return
));
2235 case LTTNG_ROTATION_GET_INFO
:
2237 struct lttng_rotation_get_info_return get_info_return
;
2239 memset(&get_info_return
, 0, sizeof(get_info_return
));
2240 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
2241 cmd_ctx
->lsm
.u
.get_rotation_info
.rotation_id
);
2247 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
2248 sizeof(get_info_return
));
2257 case LTTNG_ROTATION_SET_SCHEDULE
:
2260 enum lttng_rotation_schedule_type schedule_type
;
2263 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
2264 DBG("Kernel tracer version does not support session rotations");
2265 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
2269 set_schedule
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.set
== 1;
2270 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
.u
.rotation_set_schedule
.type
;
2271 value
= cmd_ctx
->lsm
.u
.rotation_set_schedule
.value
;
2273 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
2277 notification_thread_handle
);
2278 if (ret
!= LTTNG_OK
) {
2284 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2286 struct lttng_session_list_schedules_return schedules
= {
2287 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
2288 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
2289 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
2290 .size
.value
= cmd_ctx
->session
->rotate_size
,
2293 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
2303 case LTTNG_CLEAR_SESSION
:
2305 ret
= cmd_clear_session(cmd_ctx
->session
, sock
);
2308 case LTTNG_LIST_TRIGGERS
:
2310 struct lttng_triggers
*return_triggers
= NULL
;
2311 size_t original_payload_size
;
2312 size_t payload_size
;
2314 ret
= setup_empty_lttng_msg(cmd_ctx
);
2316 ret
= LTTNG_ERR_NOMEM
;
2320 original_payload_size
= cmd_ctx
->reply_payload
.buffer
.size
;
2322 ret
= cmd_list_triggers(cmd_ctx
,
2323 notification_thread_handle
, &return_triggers
);
2324 if (ret
!= LTTNG_OK
) {
2328 assert(return_triggers
);
2329 ret
= lttng_triggers_serialize(
2330 return_triggers
, &cmd_ctx
->reply_payload
);
2331 lttng_triggers_destroy(return_triggers
);
2333 ERR("Failed to serialize triggers in reply to `list triggers` command");
2334 ret
= LTTNG_ERR_NOMEM
;
2338 payload_size
= cmd_ctx
->reply_payload
.buffer
.size
-
2339 original_payload_size
;
2341 update_lttng_msg(cmd_ctx
, 0, payload_size
);
2347 ret
= LTTNG_ERR_UND
;
2352 if (cmd_ctx
->reply_payload
.buffer
.size
== 0) {
2353 DBG("Missing llm header, creating one.");
2354 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
2358 /* Set return code */
2359 ((struct lttcomm_lttng_msg
*) (cmd_ctx
->reply_payload
.buffer
.data
))->ret_code
= ret
;
2361 if (cmd_ctx
->session
) {
2362 session_unlock(cmd_ctx
->session
);
2363 session_put(cmd_ctx
->session
);
2364 cmd_ctx
->session
= NULL
;
2366 if (need_tracing_session
) {
2367 session_unlock_list();
2370 assert(!rcu_read_ongoing());
2374 static int create_client_sock(void)
2376 int ret
, client_sock
;
2377 const mode_t old_umask
= umask(0);
2379 /* Create client tool unix socket */
2380 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
2381 if (client_sock
< 0) {
2382 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
2387 /* Set the cloexec flag */
2388 ret
= utils_set_fd_cloexec(client_sock
);
2390 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
2391 "Continuing but note that the consumer daemon will have a "
2392 "reference to this socket on exec()", client_sock
);
2395 /* File permission MUST be 660 */
2396 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2398 ERR("Set file permissions failed: %s",
2399 config
.client_unix_sock_path
.value
);
2401 (void) lttcomm_close_unix_sock(client_sock
);
2405 DBG("Created client socket (fd = %i)", client_sock
);
2412 static void cleanup_client_thread(void *data
)
2414 struct lttng_pipe
*quit_pipe
= data
;
2416 lttng_pipe_destroy(quit_pipe
);
2419 static void thread_init_cleanup(void *data
)
2421 set_thread_status(false);
2425 * This thread manage all clients request using the unix client socket for
2428 static void *thread_manage_clients(void *data
)
2430 int sock
= -1, ret
, i
, pollfd
, err
= -1;
2432 uint32_t revents
, nb_fd
;
2433 struct lttng_poll_event events
;
2434 const int client_sock
= thread_state
.client_sock
;
2435 struct lttng_pipe
*quit_pipe
= data
;
2436 const int thread_quit_pipe_fd
= lttng_pipe_get_readfd(quit_pipe
);
2437 struct command_ctx cmd_ctx
= {};
2439 DBG("[thread] Manage client started");
2441 lttng_payload_init(&cmd_ctx
.reply_payload
);
2443 is_root
= (getuid() == 0);
2445 pthread_cleanup_push(thread_init_cleanup
, NULL
);
2447 rcu_register_thread();
2449 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
2451 health_code_update();
2453 ret
= lttcomm_listen_unix_sock(client_sock
);
2459 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2460 * more will be added to this poll set.
2462 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2464 goto error_create_poll
;
2467 /* Add the application registration socket */
2468 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2473 /* Add thread quit pipe */
2474 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
| LPOLLERR
);
2479 /* Set state as running. */
2480 set_thread_status(true);
2481 pthread_cleanup_pop(0);
2483 /* This testpoint is after we signal readiness to the parent. */
2484 if (testpoint(sessiond_thread_manage_clients
)) {
2488 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
2492 health_code_update();
2495 const struct cmd_completion_handler
*cmd_completion_handler
;
2497 cmd_ctx
.creds
= (lttng_sock_cred
) {
2501 cmd_ctx
.session
= NULL
;
2502 lttng_payload_clear(&cmd_ctx
.reply_payload
);
2503 cmd_ctx
.lttng_msg_size
= 0;
2505 DBG("Accepting client command ...");
2507 /* Inifinite blocking call, waiting for transmission */
2509 health_poll_entry();
2510 ret
= lttng_poll_wait(&events
, -1);
2514 * Restart interrupted system call.
2516 if (errno
== EINTR
) {
2524 for (i
= 0; i
< nb_fd
; i
++) {
2525 revents
= LTTNG_POLL_GETEV(&events
, i
);
2526 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2528 health_code_update();
2530 if (pollfd
== thread_quit_pipe_fd
) {
2534 /* Event on the registration socket */
2535 if (revents
& LPOLLIN
) {
2537 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2538 ERR("Client socket poll error");
2541 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2547 DBG("Wait for client response");
2549 health_code_update();
2551 sock
= lttcomm_accept_unix_sock(client_sock
);
2557 * Set the CLOEXEC flag. Return code is useless because either way, the
2560 (void) utils_set_fd_cloexec(sock
);
2562 /* Set socket option for credentials retrieval */
2563 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
2568 health_code_update();
2571 * Data is received from the lttng client. The struct
2572 * lttcomm_session_msg (lsm) contains the command and data request of
2575 DBG("Receiving data from client ...");
2576 ret
= lttcomm_recv_creds_unix_sock(sock
, &cmd_ctx
.lsm
,
2577 sizeof(struct lttcomm_session_msg
), &cmd_ctx
.creds
);
2578 if (ret
!= sizeof(struct lttcomm_session_msg
)) {
2579 DBG("Incomplete recv() from client... continuing");
2588 health_code_update();
2590 // TODO: Validate cmd_ctx including sanity check for
2591 // security purpose.
2593 rcu_thread_online();
2595 * This function dispatch the work to the kernel or userspace tracer
2596 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2597 * informations for the client. The command context struct contains
2598 * everything this function may needs.
2600 ret
= process_client_msg(&cmd_ctx
, &sock
, &sock_error
);
2601 rcu_thread_offline();
2611 * TODO: Inform client somehow of the fatal error. At
2612 * this point, ret < 0 means that a zmalloc failed
2613 * (ENOMEM). Error detected but still accept
2614 * command, unless a socket error has been
2620 if (ret
< LTTNG_OK
|| ret
>= LTTNG_ERR_NR
) {
2621 WARN("Command returned an invalid status code, returning unknown error: "
2622 "command type = %s (%d), ret = %d",
2623 lttcomm_sessiond_command_str(cmd_ctx
.lsm
.cmd_type
),
2624 cmd_ctx
.lsm
.cmd_type
, ret
);
2625 ret
= LTTNG_ERR_UNK
;
2628 cmd_completion_handler
= cmd_pop_completion_handler();
2629 if (cmd_completion_handler
) {
2630 enum lttng_error_code completion_code
;
2632 completion_code
= cmd_completion_handler
->run(
2633 cmd_completion_handler
->data
);
2634 if (completion_code
!= LTTNG_OK
) {
2639 health_code_update();
2642 struct lttng_payload_view view
=
2643 lttng_payload_view_from_payload(
2644 &cmd_ctx
.reply_payload
,
2646 struct lttcomm_lttng_msg
*llm
= (typeof(
2647 llm
)) cmd_ctx
.reply_payload
.buffer
.data
;
2649 assert(cmd_ctx
.reply_payload
.buffer
.size
>= sizeof(*llm
));
2650 assert(cmd_ctx
.lttng_msg_size
== cmd_ctx
.reply_payload
.buffer
.size
);
2652 llm
->fd_count
= lttng_payload_view_get_fd_handle_count(&view
);
2654 DBG("Sending response (size: %d, retcode: %s (%d))",
2655 cmd_ctx
.lttng_msg_size
,
2656 lttng_strerror(-llm
->ret_code
),
2658 ret
= send_unix_sock(sock
, &view
);
2660 ERR("Failed to send data back to client");
2663 /* End of transmission */
2671 health_code_update();
2683 lttng_poll_clean(&events
);
2687 unlink(config
.client_unix_sock_path
.value
);
2688 ret
= close(client_sock
);
2695 ERR("Health error occurred in %s", __func__
);
2698 health_unregister(health_sessiond
);
2700 DBG("Client thread dying");
2701 lttng_payload_reset(&cmd_ctx
.reply_payload
);
2702 rcu_unregister_thread();
2707 bool shutdown_client_thread(void *thread_data
)
2709 struct lttng_pipe
*client_quit_pipe
= thread_data
;
2710 const int write_fd
= lttng_pipe_get_writefd(client_quit_pipe
);
2712 return notify_thread_pipe(write_fd
) == 1;
2715 struct lttng_thread
*launch_client_thread(void)
2717 bool thread_running
;
2718 struct lttng_pipe
*client_quit_pipe
;
2719 struct lttng_thread
*thread
= NULL
;
2720 int client_sock_fd
= -1;
2722 sem_init(&thread_state
.ready
, 0, 0);
2723 client_quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
2724 if (!client_quit_pipe
) {
2728 client_sock_fd
= create_client_sock();
2729 if (client_sock_fd
< 0) {
2733 thread_state
.client_sock
= client_sock_fd
;
2734 thread
= lttng_thread_create("Client management",
2735 thread_manage_clients
,
2736 shutdown_client_thread
,
2737 cleanup_client_thread
,
2742 /* The client thread now owns the client sock fd and the quit pipe. */
2743 client_sock_fd
= -1;
2744 client_quit_pipe
= NULL
;
2747 * This thread is part of the threads that need to be fully
2748 * initialized before the session daemon is marked as "ready".
2750 thread_running
= wait_thread_status();
2751 if (!thread_running
) {
2756 if (client_sock_fd
>= 0) {
2757 if (close(client_sock_fd
)) {
2758 PERROR("Failed to close client socket");
2761 lttng_thread_put(thread
);
2762 cleanup_client_thread(client_quit_pipe
);