2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/session-config.h>
52 #include <common/dynamic-buffer.h>
53 #include <lttng/userspace-probe-internal.h>
54 #include <lttng/event-internal.h>
56 #include "lttng-sessiond.h"
57 #include "buffer-registry.h"
64 #include "kernel-consumer.h"
68 #include "ust-consumer.h"
71 #include "health-sessiond.h"
72 #include "testpoint.h"
73 #include "ust-thread.h"
74 #include "agent-thread.h"
76 #include "load-session-thread.h"
77 #include "notification-thread.h"
78 #include "notification-thread-commands.h"
79 #include "rotation-thread.h"
80 #include "lttng-syscall.h"
82 #include "ht-cleanup.h"
83 #include "sessiond-config.h"
86 static const char *help_msg
=
87 #ifdef LTTNG_EMBED_HELP
88 #include <lttng-sessiond.8.h>
95 static int lockfile_fd
= -1;
97 /* Set to 1 when a SIGUSR1 signal is received. */
98 static int recv_child_signal
;
100 static struct lttng_kernel_tracer_version kernel_tracer_version
;
101 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
104 * Consumer daemon specific control data. Every value not initialized here is
105 * set to 0 by the static definition.
107 static struct consumer_data kconsumer_data
= {
108 .type
= LTTNG_CONSUMER_KERNEL
,
111 .channel_monitor_pipe
= -1,
112 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
113 .lock
= PTHREAD_MUTEX_INITIALIZER
,
114 .cond
= PTHREAD_COND_INITIALIZER
,
115 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
117 static struct consumer_data ustconsumer64_data
= {
118 .type
= LTTNG_CONSUMER64_UST
,
121 .channel_monitor_pipe
= -1,
122 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
123 .lock
= PTHREAD_MUTEX_INITIALIZER
,
124 .cond
= PTHREAD_COND_INITIALIZER
,
125 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
127 static struct consumer_data ustconsumer32_data
= {
128 .type
= LTTNG_CONSUMER32_UST
,
131 .channel_monitor_pipe
= -1,
132 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
133 .lock
= PTHREAD_MUTEX_INITIALIZER
,
134 .cond
= PTHREAD_COND_INITIALIZER
,
135 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
138 /* Command line options */
139 static const struct option long_options
[] = {
140 { "client-sock", required_argument
, 0, 'c' },
141 { "apps-sock", required_argument
, 0, 'a' },
142 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
143 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
144 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
145 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
146 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
147 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
148 { "consumerd32-path", required_argument
, 0, '\0' },
149 { "consumerd32-libdir", required_argument
, 0, '\0' },
150 { "consumerd64-path", required_argument
, 0, '\0' },
151 { "consumerd64-libdir", required_argument
, 0, '\0' },
152 { "daemonize", no_argument
, 0, 'd' },
153 { "background", no_argument
, 0, 'b' },
154 { "sig-parent", no_argument
, 0, 'S' },
155 { "help", no_argument
, 0, 'h' },
156 { "group", required_argument
, 0, 'g' },
157 { "version", no_argument
, 0, 'V' },
158 { "quiet", no_argument
, 0, 'q' },
159 { "verbose", no_argument
, 0, 'v' },
160 { "verbose-consumer", no_argument
, 0, '\0' },
161 { "no-kernel", no_argument
, 0, '\0' },
162 { "pidfile", required_argument
, 0, 'p' },
163 { "agent-tcp-port", required_argument
, 0, '\0' },
164 { "config", required_argument
, 0, 'f' },
165 { "load", required_argument
, 0, 'l' },
166 { "kmod-probes", required_argument
, 0, '\0' },
167 { "extra-kmod-probes", required_argument
, 0, '\0' },
171 /* Command line options to ignore from configuration file */
172 static const char *config_ignore_options
[] = { "help", "version", "config" };
174 /* Shared between threads */
175 static int dispatch_thread_exit
;
177 /* Sockets and FDs */
178 static int client_sock
= -1;
179 static int apps_sock
= -1;
182 * This pipe is used to inform the thread managing application communication
183 * that a command is queued and ready to be processed.
185 static int apps_cmd_pipe
[2] = { -1, -1 };
187 /* Pthread, Mutexes and Semaphores */
188 static pthread_t apps_thread
;
189 static pthread_t apps_notify_thread
;
190 static pthread_t reg_apps_thread
;
191 static pthread_t client_thread
;
192 static pthread_t kernel_thread
;
193 static pthread_t dispatch_thread
;
194 static pthread_t health_thread
;
195 static pthread_t ht_cleanup_thread
;
196 static pthread_t agent_reg_thread
;
197 static pthread_t load_session_thread
;
198 static pthread_t notification_thread
;
199 static pthread_t rotation_thread
;
200 static pthread_t timer_thread
;
203 * UST registration command queue. This queue is tied with a futex and uses a N
204 * wakers / 1 waiter implemented and detailed in futex.c/.h
206 * The thread_registration_apps and thread_dispatch_ust_registration uses this
207 * queue along with the wait/wake scheme. The thread_manage_apps receives down
208 * the line new application socket and monitors it for any I/O error or clean
209 * close that triggers an unregistration of the application.
211 static struct ust_cmd_queue ust_cmd_queue
;
213 static const char *module_proc_lttng
= "/proc/lttng";
216 * Consumer daemon state which is changed when spawning it, killing it or in
217 * case of a fatal error.
219 enum consumerd_state
{
220 CONSUMER_STARTED
= 1,
221 CONSUMER_STOPPED
= 2,
226 * This consumer daemon state is used to validate if a client command will be
227 * able to reach the consumer. If not, the client is informed. For instance,
228 * doing a "lttng start" when the consumer state is set to ERROR will return an
229 * error to the client.
231 * The following example shows a possible race condition of this scheme:
233 * consumer thread error happens
235 * client cmd checks state -> still OK
236 * consumer thread exit, sets error
237 * client cmd try to talk to consumer
240 * However, since the consumer is a different daemon, we have no way of making
241 * sure the command will reach it safely even with this state flag. This is why
242 * we consider that up to the state validation during command processing, the
243 * command is safe. After that, we can not guarantee the correctness of the
244 * client request vis-a-vis the consumer.
246 static enum consumerd_state ust_consumerd_state
;
247 static enum consumerd_state kernel_consumerd_state
;
249 /* Load session thread information to operate. */
250 static struct load_session_thread_data
*load_info
;
253 * Section name to look for in the daemon configuration file.
255 static const char * const config_section_name
= "sessiond";
257 /* Am I root or not. Set to 1 if the daemon is running as root */
260 /* Rotation thread handle. */
261 static struct rotation_thread_handle
*rotation_thread_handle
;
264 * Stop all threads by closing the thread quit pipe.
266 static void stop_threads(void)
270 /* Stopping all threads */
271 DBG("Terminating all threads");
272 ret
= sessiond_notify_quit_pipe();
274 ERR("write error on thread quit pipe");
277 /* Dispatch thread */
278 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
279 futex_nto1_wake(&ust_cmd_queue
.futex
);
283 * Close every consumer sockets.
285 static void close_consumer_sockets(void)
289 if (kconsumer_data
.err_sock
>= 0) {
290 ret
= close(kconsumer_data
.err_sock
);
292 PERROR("kernel consumer err_sock close");
295 if (ustconsumer32_data
.err_sock
>= 0) {
296 ret
= close(ustconsumer32_data
.err_sock
);
298 PERROR("UST consumerd32 err_sock close");
301 if (ustconsumer64_data
.err_sock
>= 0) {
302 ret
= close(ustconsumer64_data
.err_sock
);
304 PERROR("UST consumerd64 err_sock close");
307 if (kconsumer_data
.cmd_sock
>= 0) {
308 ret
= close(kconsumer_data
.cmd_sock
);
310 PERROR("kernel consumer cmd_sock close");
313 if (ustconsumer32_data
.cmd_sock
>= 0) {
314 ret
= close(ustconsumer32_data
.cmd_sock
);
316 PERROR("UST consumerd32 cmd_sock close");
319 if (ustconsumer64_data
.cmd_sock
>= 0) {
320 ret
= close(ustconsumer64_data
.cmd_sock
);
322 PERROR("UST consumerd64 cmd_sock close");
325 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
326 ret
= close(kconsumer_data
.channel_monitor_pipe
);
328 PERROR("kernel consumer channel monitor pipe close");
331 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
332 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
334 PERROR("UST consumerd32 channel monitor pipe close");
337 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
338 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
340 PERROR("UST consumerd64 channel monitor pipe close");
346 * Wait on consumer process termination.
348 * Need to be called with the consumer data lock held or from a context
349 * ensuring no concurrent access to data (e.g: cleanup).
351 static void wait_consumer(struct consumer_data
*consumer_data
)
356 if (consumer_data
->pid
<= 0) {
360 DBG("Waiting for complete teardown of consumerd (PID: %d)",
362 ret
= waitpid(consumer_data
->pid
, &status
, 0);
364 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
365 } else if (!WIFEXITED(status
)) {
366 ERR("consumerd termination with error: %d",
369 consumer_data
->pid
= 0;
373 * Cleanup the session daemon's data structures.
375 static void sessiond_cleanup(void)
378 struct ltt_session_list
*session_list
= session_get_list();
380 DBG("Cleanup sessiond");
383 * Close the thread quit pipe. It has already done its job,
384 * since we are now called.
386 sessiond_close_quit_pipe();
388 ret
= remove(config
.pid_file_path
.value
);
390 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
393 DBG("Removing sessiond and consumerd content of directory %s",
394 config
.rundir
.value
);
397 DBG("Removing %s", config
.pid_file_path
.value
);
398 (void) unlink(config
.pid_file_path
.value
);
400 DBG("Removing %s", config
.agent_port_file_path
.value
);
401 (void) unlink(config
.agent_port_file_path
.value
);
404 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
405 (void) unlink(kconsumer_data
.err_unix_sock_path
);
407 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
408 (void) rmdir(config
.kconsumerd_path
.value
);
410 /* ust consumerd 32 */
411 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
412 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
414 DBG("Removing directory %s", config
.consumerd32_path
.value
);
415 (void) rmdir(config
.consumerd32_path
.value
);
417 /* ust consumerd 64 */
418 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
419 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
421 DBG("Removing directory %s", config
.consumerd64_path
.value
);
422 (void) rmdir(config
.consumerd64_path
.value
);
424 pthread_mutex_destroy(&session_list
->lock
);
426 wait_consumer(&kconsumer_data
);
427 wait_consumer(&ustconsumer64_data
);
428 wait_consumer(&ustconsumer32_data
);
430 DBG("Cleaning up all agent apps");
431 agent_app_ht_clean();
433 DBG("Closing all UST sockets");
434 ust_app_clean_list();
435 buffer_reg_destroy_registries();
437 if (is_root
&& !config
.no_kernel
) {
438 DBG2("Closing kernel fd");
439 if (kernel_tracer_fd
>= 0) {
440 ret
= close(kernel_tracer_fd
);
445 DBG("Unloading kernel modules");
446 modprobe_remove_lttng_all();
450 close_consumer_sockets();
453 load_session_destroy_data(load_info
);
458 * We do NOT rmdir rundir because there are other processes
459 * using it, for instance lttng-relayd, which can start in
460 * parallel with this teardown.
465 * Cleanup the daemon's option data structures.
467 static void sessiond_cleanup_options(void)
469 DBG("Cleaning up options");
471 sessiond_config_fini(&config
);
473 run_as_destroy_worker();
477 * Send data on a unix socket using the liblttsessiondcomm API.
479 * Return lttcomm error code.
481 static int send_unix_sock(int sock
, void *buf
, size_t len
)
483 /* Check valid length */
488 return lttcomm_send_unix_sock(sock
, buf
, len
);
492 * Free memory of a command context structure.
494 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
496 DBG("Clean command context structure");
498 if ((*cmd_ctx
)->llm
) {
499 free((*cmd_ctx
)->llm
);
501 if ((*cmd_ctx
)->lsm
) {
502 free((*cmd_ctx
)->lsm
);
510 * Notify UST applications using the shm mmap futex.
512 static int notify_ust_apps(int active
)
516 DBG("Notifying applications of session daemon state: %d", active
);
518 /* See shm.c for this call implying mmap, shm and futex calls */
519 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
520 if (wait_shm_mmap
== NULL
) {
524 /* Wake waiting process */
525 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
527 /* Apps notified successfully */
535 * Setup the outgoing data buffer for the response (llm) by allocating the
536 * right amount of memory and copying the original information from the lsm
539 * Return 0 on success, negative value on error.
541 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
542 const void *payload_buf
, size_t payload_len
,
543 const void *cmd_header_buf
, size_t cmd_header_len
)
546 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
547 const size_t cmd_header_offset
= header_len
;
548 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
549 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
551 cmd_ctx
->llm
= zmalloc(total_msg_size
);
553 if (cmd_ctx
->llm
== NULL
) {
559 /* Copy common data */
560 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
561 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
562 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
563 cmd_ctx
->llm
->data_size
= payload_len
;
564 cmd_ctx
->lttng_msg_size
= total_msg_size
;
566 /* Copy command header */
567 if (cmd_header_len
) {
568 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
574 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
583 * Version of setup_lttng_msg() without command header.
585 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
586 void *payload_buf
, size_t payload_len
)
588 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
591 * Update the kernel poll set of all channel fd available over all tracing
592 * session. Add the wakeup pipe at the end of the set.
594 static int update_kernel_poll(struct lttng_poll_event
*events
)
597 struct ltt_kernel_channel
*channel
;
598 struct ltt_session
*session
;
599 const struct ltt_session_list
*session_list
= session_get_list();
601 DBG("Updating kernel poll set");
604 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
605 if (!session_get(session
)) {
608 session_lock(session
);
609 if (session
->kernel_session
== NULL
) {
610 session_unlock(session
);
611 session_put(session
);
615 cds_list_for_each_entry(channel
,
616 &session
->kernel_session
->channel_list
.head
, list
) {
617 /* Add channel fd to the kernel poll set */
618 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
620 session_unlock(session
);
621 session_put(session
);
624 DBG("Channel fd %d added to kernel set", channel
->fd
);
626 session_unlock(session
);
628 session_unlock_list();
633 session_unlock_list();
638 * Find the channel fd from 'fd' over all tracing session. When found, check
639 * for new channel stream and send those stream fds to the kernel consumer.
641 * Useful for CPU hotplug feature.
643 static int update_kernel_stream(int fd
)
646 struct ltt_session
*session
;
647 struct ltt_kernel_session
*ksess
;
648 struct ltt_kernel_channel
*channel
;
649 const struct ltt_session_list
*session_list
= session_get_list();
651 DBG("Updating kernel streams for channel fd %d", fd
);
654 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
655 if (!session_get(session
)) {
658 session_lock(session
);
659 if (session
->kernel_session
== NULL
) {
660 session_unlock(session
);
661 session_put(session
);
664 ksess
= session
->kernel_session
;
666 cds_list_for_each_entry(channel
,
667 &ksess
->channel_list
.head
, list
) {
668 struct lttng_ht_iter iter
;
669 struct consumer_socket
*socket
;
671 if (channel
->fd
!= fd
) {
674 DBG("Channel found, updating kernel streams");
675 ret
= kernel_open_channel_stream(channel
);
679 /* Update the stream global counter */
680 ksess
->stream_count_global
+= ret
;
683 * Have we already sent fds to the consumer? If yes, it
684 * means that tracing is started so it is safe to send
685 * our updated stream fds.
687 if (ksess
->consumer_fds_sent
!= 1
688 || ksess
->consumer
== NULL
) {
694 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
695 &iter
.iter
, socket
, node
.node
) {
696 pthread_mutex_lock(socket
->lock
);
697 ret
= kernel_consumer_send_channel_streams(socket
,
699 session
->output_traces
? 1 : 0);
700 pthread_mutex_unlock(socket
->lock
);
708 session_unlock(session
);
709 session_put(session
);
711 session_unlock_list();
715 session_unlock(session
);
716 session_put(session
);
717 session_unlock_list();
722 * For each tracing session, update newly registered apps. The session list
723 * lock MUST be acquired before calling this.
725 static void update_ust_app(int app_sock
)
727 struct ltt_session
*sess
, *stmp
;
728 const struct ltt_session_list
*session_list
= session_get_list();
730 /* Consumer is in an ERROR state. Stop any application update. */
731 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
732 /* Stop the update process since the consumer is dead. */
736 /* For all tracing session(s) */
737 cds_list_for_each_entry_safe(sess
, stmp
, &session_list
->head
, list
) {
740 if (!session_get(sess
)) {
744 if (!sess
->ust_session
) {
749 assert(app_sock
>= 0);
750 app
= ust_app_find_by_sock(app_sock
);
753 * Application can be unregistered before so
754 * this is possible hence simply stopping the
757 DBG3("UST app update failed to find app sock %d",
761 ust_app_global_update(sess
->ust_session
, app
);
765 session_unlock(sess
);
771 * This thread manage event coming from the kernel.
773 * Features supported in this thread:
776 static void *thread_manage_kernel(void *data
)
778 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
779 uint32_t revents
, nb_fd
;
781 struct lttng_poll_event events
;
783 DBG("[thread] Thread manage kernel started");
785 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
788 * This first step of the while is to clean this structure which could free
789 * non NULL pointers so initialize it before the loop.
791 lttng_poll_init(&events
);
793 if (testpoint(sessiond_thread_manage_kernel
)) {
794 goto error_testpoint
;
797 health_code_update();
799 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
800 goto error_testpoint
;
804 health_code_update();
806 if (update_poll_flag
== 1) {
807 /* Clean events object. We are about to populate it again. */
808 lttng_poll_clean(&events
);
810 ret
= sessiond_set_thread_pollset(&events
, 2);
812 goto error_poll_create
;
815 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
820 /* This will add the available kernel channel if any. */
821 ret
= update_kernel_poll(&events
);
825 update_poll_flag
= 0;
828 DBG("Thread kernel polling");
830 /* Poll infinite value of time */
833 ret
= lttng_poll_wait(&events
, -1);
834 DBG("Thread kernel return from poll on %d fds",
835 LTTNG_POLL_GETNB(&events
));
839 * Restart interrupted system call.
841 if (errno
== EINTR
) {
845 } else if (ret
== 0) {
846 /* Should not happen since timeout is infinite */
847 ERR("Return value of poll is 0 with an infinite timeout.\n"
848 "This should not have happened! Continuing...");
854 for (i
= 0; i
< nb_fd
; i
++) {
855 /* Fetch once the poll data */
856 revents
= LTTNG_POLL_GETEV(&events
, i
);
857 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
859 health_code_update();
862 /* No activity for this FD (poll implementation). */
866 /* Thread quit pipe has been closed. Killing thread. */
867 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
873 /* Check for data on kernel pipe */
874 if (revents
& LPOLLIN
) {
875 if (pollfd
== kernel_poll_pipe
[0]) {
876 (void) lttng_read(kernel_poll_pipe
[0],
879 * Ret value is useless here, if this pipe gets any actions an
880 * update is required anyway.
882 update_poll_flag
= 1;
886 * New CPU detected by the kernel. Adding kernel stream to
887 * kernel session and updating the kernel consumer
889 ret
= update_kernel_stream(pollfd
);
895 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
896 update_poll_flag
= 1;
899 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
907 lttng_poll_clean(&events
);
910 utils_close_pipe(kernel_poll_pipe
);
911 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
914 ERR("Health error occurred in %s", __func__
);
915 WARN("Kernel thread died unexpectedly. "
916 "Kernel tracing can continue but CPU hotplug is disabled.");
918 health_unregister(health_sessiond
);
919 DBG("Kernel thread dying");
924 * Signal pthread condition of the consumer data that the thread.
926 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
928 pthread_mutex_lock(&data
->cond_mutex
);
931 * The state is set before signaling. It can be any value, it's the waiter
932 * job to correctly interpret this condition variable associated to the
933 * consumer pthread_cond.
935 * A value of 0 means that the corresponding thread of the consumer data
936 * was not started. 1 indicates that the thread has started and is ready
937 * for action. A negative value means that there was an error during the
940 data
->consumer_thread_is_ready
= state
;
941 (void) pthread_cond_signal(&data
->cond
);
943 pthread_mutex_unlock(&data
->cond_mutex
);
947 * This thread manage the consumer error sent back to the session daemon.
949 static void *thread_manage_consumer(void *data
)
951 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
952 uint32_t revents
, nb_fd
;
953 enum lttcomm_return_code code
;
954 struct lttng_poll_event events
;
955 struct consumer_data
*consumer_data
= data
;
956 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
958 DBG("[thread] Manage consumer started");
960 rcu_register_thread();
963 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
965 health_code_update();
968 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
969 * metadata_sock. Nothing more will be added to this poll set.
971 ret
= sessiond_set_thread_pollset(&events
, 3);
977 * The error socket here is already in a listening state which was done
978 * just before spawning this thread to avoid a race between the consumer
979 * daemon exec trying to connect and the listen() call.
981 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
986 health_code_update();
988 /* Infinite blocking call, waiting for transmission */
992 if (testpoint(sessiond_thread_manage_consumer
)) {
996 ret
= lttng_poll_wait(&events
, -1);
1000 * Restart interrupted system call.
1002 if (errno
== EINTR
) {
1010 for (i
= 0; i
< nb_fd
; i
++) {
1011 /* Fetch once the poll data */
1012 revents
= LTTNG_POLL_GETEV(&events
, i
);
1013 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1015 health_code_update();
1018 /* No activity for this FD (poll implementation). */
1022 /* Thread quit pipe has been closed. Killing thread. */
1023 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1029 /* Event on the registration socket */
1030 if (pollfd
== consumer_data
->err_sock
) {
1031 if (revents
& LPOLLIN
) {
1033 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1034 ERR("consumer err socket poll error");
1037 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1043 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1049 * Set the CLOEXEC flag. Return code is useless because either way, the
1052 (void) utils_set_fd_cloexec(sock
);
1054 health_code_update();
1056 DBG2("Receiving code from consumer err_sock");
1058 /* Getting status code from kconsumerd */
1059 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1060 sizeof(enum lttcomm_return_code
));
1065 health_code_update();
1066 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1067 ERR("consumer error when waiting for SOCK_READY : %s",
1068 lttcomm_get_readable_code(-code
));
1072 /* Connect both command and metadata sockets. */
1073 consumer_data
->cmd_sock
=
1074 lttcomm_connect_unix_sock(
1075 consumer_data
->cmd_unix_sock_path
);
1076 consumer_data
->metadata_fd
=
1077 lttcomm_connect_unix_sock(
1078 consumer_data
->cmd_unix_sock_path
);
1079 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1080 PERROR("consumer connect cmd socket");
1081 /* On error, signal condition and quit. */
1082 signal_consumer_condition(consumer_data
, -1);
1086 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1088 /* Create metadata socket lock. */
1089 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1090 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1091 PERROR("zmalloc pthread mutex");
1094 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1096 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1097 DBG("Consumer metadata socket ready (fd: %d)",
1098 consumer_data
->metadata_fd
);
1101 * Remove the consumerd error sock since we've established a connection.
1103 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1108 /* Add new accepted error socket. */
1109 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1114 /* Add metadata socket that is successfully connected. */
1115 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1116 LPOLLIN
| LPOLLRDHUP
);
1121 health_code_update();
1124 * Transfer the write-end of the channel monitoring and rotate pipe
1125 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1127 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1128 if (!cmd_socket_wrapper
) {
1131 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1133 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1134 consumer_data
->channel_monitor_pipe
);
1139 /* Discard the socket wrapper as it is no longer needed. */
1140 consumer_destroy_socket(cmd_socket_wrapper
);
1141 cmd_socket_wrapper
= NULL
;
1143 /* The thread is completely initialized, signal that it is ready. */
1144 signal_consumer_condition(consumer_data
, 1);
1146 /* Infinite blocking call, waiting for transmission */
1149 health_code_update();
1151 /* Exit the thread because the thread quit pipe has been triggered. */
1153 /* Not a health error. */
1158 health_poll_entry();
1159 ret
= lttng_poll_wait(&events
, -1);
1163 * Restart interrupted system call.
1165 if (errno
== EINTR
) {
1173 for (i
= 0; i
< nb_fd
; i
++) {
1174 /* Fetch once the poll data */
1175 revents
= LTTNG_POLL_GETEV(&events
, i
);
1176 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1178 health_code_update();
1181 /* No activity for this FD (poll implementation). */
1186 * Thread quit pipe has been triggered, flag that we should stop
1187 * but continue the current loop to handle potential data from
1190 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1192 if (pollfd
== sock
) {
1193 /* Event on the consumerd socket */
1194 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1195 && !(revents
& LPOLLIN
)) {
1196 ERR("consumer err socket second poll error");
1199 health_code_update();
1200 /* Wait for any kconsumerd error */
1201 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1202 sizeof(enum lttcomm_return_code
));
1204 ERR("consumer closed the command socket");
1208 ERR("consumer return code : %s",
1209 lttcomm_get_readable_code(-code
));
1212 } else if (pollfd
== consumer_data
->metadata_fd
) {
1213 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1214 && !(revents
& LPOLLIN
)) {
1215 ERR("consumer err metadata socket second poll error");
1218 /* UST metadata requests */
1219 ret
= ust_consumer_metadata_request(
1220 &consumer_data
->metadata_sock
);
1222 ERR("Handling metadata request");
1226 /* No need for an else branch all FDs are tested prior. */
1228 health_code_update();
1234 * We lock here because we are about to close the sockets and some other
1235 * thread might be using them so get exclusive access which will abort all
1236 * other consumer command by other threads.
1238 pthread_mutex_lock(&consumer_data
->lock
);
1240 /* Immediately set the consumerd state to stopped */
1241 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1242 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1243 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1244 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1245 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1247 /* Code flow error... */
1251 if (consumer_data
->err_sock
>= 0) {
1252 ret
= close(consumer_data
->err_sock
);
1256 consumer_data
->err_sock
= -1;
1258 if (consumer_data
->cmd_sock
>= 0) {
1259 ret
= close(consumer_data
->cmd_sock
);
1263 consumer_data
->cmd_sock
= -1;
1265 if (consumer_data
->metadata_sock
.fd_ptr
&&
1266 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1267 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1279 unlink(consumer_data
->err_unix_sock_path
);
1280 unlink(consumer_data
->cmd_unix_sock_path
);
1281 pthread_mutex_unlock(&consumer_data
->lock
);
1283 /* Cleanup metadata socket mutex. */
1284 if (consumer_data
->metadata_sock
.lock
) {
1285 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1286 free(consumer_data
->metadata_sock
.lock
);
1288 lttng_poll_clean(&events
);
1290 if (cmd_socket_wrapper
) {
1291 consumer_destroy_socket(cmd_socket_wrapper
);
1296 ERR("Health error occurred in %s", __func__
);
1298 health_unregister(health_sessiond
);
1299 DBG("consumer thread cleanup completed");
1301 rcu_thread_offline();
1302 rcu_unregister_thread();
1308 * This thread receives application command sockets (FDs) on the
1309 * apps_cmd_pipe and waits (polls) on them until they are closed
1310 * or an error occurs.
1312 * At that point, it flushes the data (tracing and metadata) associated
1313 * with this application and tears down ust app sessions and other
1314 * associated data structures through ust_app_unregister().
1316 * Note that this thread never sends commands to the applications
1317 * through the command sockets; it merely listens for hang-ups
1318 * and errors on those sockets and cleans-up as they occur.
1320 static void *thread_manage_apps(void *data
)
1322 int i
, ret
, pollfd
, err
= -1;
1324 uint32_t revents
, nb_fd
;
1325 struct lttng_poll_event events
;
1327 DBG("[thread] Manage application started");
1329 rcu_register_thread();
1330 rcu_thread_online();
1332 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1334 if (testpoint(sessiond_thread_manage_apps
)) {
1335 goto error_testpoint
;
1338 health_code_update();
1340 ret
= sessiond_set_thread_pollset(&events
, 2);
1342 goto error_poll_create
;
1345 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1350 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1354 health_code_update();
1357 DBG("Apps thread polling");
1359 /* Inifinite blocking call, waiting for transmission */
1361 health_poll_entry();
1362 ret
= lttng_poll_wait(&events
, -1);
1363 DBG("Apps thread return from poll on %d fds",
1364 LTTNG_POLL_GETNB(&events
));
1368 * Restart interrupted system call.
1370 if (errno
== EINTR
) {
1378 for (i
= 0; i
< nb_fd
; i
++) {
1379 /* Fetch once the poll data */
1380 revents
= LTTNG_POLL_GETEV(&events
, i
);
1381 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1383 health_code_update();
1386 /* No activity for this FD (poll implementation). */
1390 /* Thread quit pipe has been closed. Killing thread. */
1391 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1397 /* Inspect the apps cmd pipe */
1398 if (pollfd
== apps_cmd_pipe
[0]) {
1399 if (revents
& LPOLLIN
) {
1403 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1404 if (size_ret
< sizeof(sock
)) {
1405 PERROR("read apps cmd pipe");
1409 health_code_update();
1412 * Since this is a command socket (write then read),
1413 * we only monitor the error events of the socket.
1415 ret
= lttng_poll_add(&events
, sock
,
1416 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1421 DBG("Apps with sock %d added to poll set", sock
);
1422 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1423 ERR("Apps command pipe error");
1426 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1431 * At this point, we know that a registered application made
1432 * the event at poll_wait.
1434 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1435 /* Removing from the poll set */
1436 ret
= lttng_poll_del(&events
, pollfd
);
1441 /* Socket closed on remote end. */
1442 ust_app_unregister(pollfd
);
1444 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1449 health_code_update();
1455 lttng_poll_clean(&events
);
1458 utils_close_pipe(apps_cmd_pipe
);
1459 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1462 * We don't clean the UST app hash table here since already registered
1463 * applications can still be controlled so let them be until the session
1464 * daemon dies or the applications stop.
1469 ERR("Health error occurred in %s", __func__
);
1471 health_unregister(health_sessiond
);
1472 DBG("Application communication apps thread cleanup complete");
1473 rcu_thread_offline();
1474 rcu_unregister_thread();
1479 * Send a socket to a thread This is called from the dispatch UST registration
1480 * thread once all sockets are set for the application.
1482 * The sock value can be invalid, we don't really care, the thread will handle
1483 * it and make the necessary cleanup if so.
1485 * On success, return 0 else a negative value being the errno message of the
1488 static int send_socket_to_thread(int fd
, int sock
)
1493 * It's possible that the FD is set as invalid with -1 concurrently just
1494 * before calling this function being a shutdown state of the thread.
1501 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1502 if (ret
< sizeof(sock
)) {
1503 PERROR("write apps pipe %d", fd
);
1510 /* All good. Don't send back the write positive ret value. */
1517 * Sanitize the wait queue of the dispatch registration thread meaning removing
1518 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1519 * notify socket is never received.
1521 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1523 int ret
, nb_fd
= 0, i
;
1524 unsigned int fd_added
= 0;
1525 struct lttng_poll_event events
;
1526 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1530 lttng_poll_init(&events
);
1532 /* Just skip everything for an empty queue. */
1533 if (!wait_queue
->count
) {
1537 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1542 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1543 &wait_queue
->head
, head
) {
1544 assert(wait_node
->app
);
1545 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1546 LPOLLHUP
| LPOLLERR
);
1559 * Poll but don't block so we can quickly identify the faulty events and
1560 * clean them afterwards from the wait queue.
1562 ret
= lttng_poll_wait(&events
, 0);
1568 for (i
= 0; i
< nb_fd
; i
++) {
1569 /* Get faulty FD. */
1570 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1571 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1574 /* No activity for this FD (poll implementation). */
1578 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1579 &wait_queue
->head
, head
) {
1580 if (pollfd
== wait_node
->app
->sock
&&
1581 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1582 cds_list_del(&wait_node
->head
);
1583 wait_queue
->count
--;
1584 ust_app_destroy(wait_node
->app
);
1587 * Silence warning of use-after-free in
1588 * cds_list_for_each_entry_safe which uses
1589 * __typeof__(*wait_node).
1594 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1601 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1605 lttng_poll_clean(&events
);
1609 lttng_poll_clean(&events
);
1611 ERR("Unable to sanitize wait queue");
1616 * Dispatch request from the registration threads to the application
1617 * communication thread.
1619 static void *thread_dispatch_ust_registration(void *data
)
1622 struct cds_wfcq_node
*node
;
1623 struct ust_command
*ust_cmd
= NULL
;
1624 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1625 struct ust_reg_wait_queue wait_queue
= {
1629 rcu_register_thread();
1631 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1633 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1634 goto error_testpoint
;
1637 health_code_update();
1639 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1641 DBG("[thread] Dispatch UST command started");
1644 health_code_update();
1646 /* Atomically prepare the queue futex */
1647 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1649 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1654 struct ust_app
*app
= NULL
;
1658 * Make sure we don't have node(s) that have hung up before receiving
1659 * the notify socket. This is to clean the list in order to avoid
1660 * memory leaks from notify socket that are never seen.
1662 sanitize_wait_queue(&wait_queue
);
1664 health_code_update();
1665 /* Dequeue command for registration */
1666 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1668 DBG("Woken up but nothing in the UST command queue");
1669 /* Continue thread execution */
1673 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1675 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1676 " gid:%d sock:%d name:%s (version %d.%d)",
1677 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1678 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1679 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1680 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1682 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1683 wait_node
= zmalloc(sizeof(*wait_node
));
1685 PERROR("zmalloc wait_node dispatch");
1686 ret
= close(ust_cmd
->sock
);
1688 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1690 lttng_fd_put(LTTNG_FD_APPS
, 1);
1694 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1696 /* Create application object if socket is CMD. */
1697 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1699 if (!wait_node
->app
) {
1700 ret
= close(ust_cmd
->sock
);
1702 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1704 lttng_fd_put(LTTNG_FD_APPS
, 1);
1710 * Add application to the wait queue so we can set the notify
1711 * socket before putting this object in the global ht.
1713 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1718 * We have to continue here since we don't have the notify
1719 * socket and the application MUST be added to the hash table
1720 * only at that moment.
1725 * Look for the application in the local wait queue and set the
1726 * notify socket if found.
1728 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1729 &wait_queue
.head
, head
) {
1730 health_code_update();
1731 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1732 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1733 cds_list_del(&wait_node
->head
);
1735 app
= wait_node
->app
;
1737 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1743 * With no application at this stage the received socket is
1744 * basically useless so close it before we free the cmd data
1745 * structure for good.
1748 ret
= close(ust_cmd
->sock
);
1750 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1752 lttng_fd_put(LTTNG_FD_APPS
, 1);
1759 * @session_lock_list
1761 * Lock the global session list so from the register up to the
1762 * registration done message, no thread can see the application
1763 * and change its state.
1765 session_lock_list();
1769 * Add application to the global hash table. This needs to be
1770 * done before the update to the UST registry can locate the
1775 /* Set app version. This call will print an error if needed. */
1776 (void) ust_app_version(app
);
1778 /* Send notify socket through the notify pipe. */
1779 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1783 session_unlock_list();
1785 * No notify thread, stop the UST tracing. However, this is
1786 * not an internal error of the this thread thus setting
1787 * the health error code to a normal exit.
1794 * Update newly registered application with the tracing
1795 * registry info already enabled information.
1797 update_ust_app(app
->sock
);
1800 * Don't care about return value. Let the manage apps threads
1801 * handle app unregistration upon socket close.
1803 (void) ust_app_register_done(app
);
1806 * Even if the application socket has been closed, send the app
1807 * to the thread and unregistration will take place at that
1810 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1813 session_unlock_list();
1815 * No apps. thread, stop the UST tracing. However, this is
1816 * not an internal error of the this thread thus setting
1817 * the health error code to a normal exit.
1824 session_unlock_list();
1826 } while (node
!= NULL
);
1828 health_poll_entry();
1829 /* Futex wait on queue. Blocking call on futex() */
1830 futex_nto1_wait(&ust_cmd_queue
.futex
);
1833 /* Normal exit, no error */
1837 /* Clean up wait queue. */
1838 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1839 &wait_queue
.head
, head
) {
1840 cds_list_del(&wait_node
->head
);
1845 /* Empty command queue. */
1847 /* Dequeue command for registration */
1848 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1852 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1853 ret
= close(ust_cmd
->sock
);
1855 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
1857 lttng_fd_put(LTTNG_FD_APPS
, 1);
1862 DBG("Dispatch thread dying");
1865 ERR("Health error occurred in %s", __func__
);
1867 health_unregister(health_sessiond
);
1868 rcu_unregister_thread();
1873 * This thread manage application registration.
1875 static void *thread_registration_apps(void *data
)
1877 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1878 uint32_t revents
, nb_fd
;
1879 struct lttng_poll_event events
;
1881 * Get allocated in this thread, enqueued to a global queue, dequeued and
1882 * freed in the manage apps thread.
1884 struct ust_command
*ust_cmd
= NULL
;
1886 DBG("[thread] Manage application registration started");
1888 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1890 if (testpoint(sessiond_thread_registration_apps
)) {
1891 goto error_testpoint
;
1894 ret
= lttcomm_listen_unix_sock(apps_sock
);
1900 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1901 * more will be added to this poll set.
1903 ret
= sessiond_set_thread_pollset(&events
, 2);
1905 goto error_create_poll
;
1908 /* Add the application registration socket */
1909 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1911 goto error_poll_add
;
1914 /* Notify all applications to register */
1915 ret
= notify_ust_apps(1);
1917 ERR("Failed to notify applications or create the wait shared memory.\n"
1918 "Execution continues but there might be problem for already\n"
1919 "running applications that wishes to register.");
1923 DBG("Accepting application registration");
1925 /* Inifinite blocking call, waiting for transmission */
1927 health_poll_entry();
1928 ret
= lttng_poll_wait(&events
, -1);
1932 * Restart interrupted system call.
1934 if (errno
== EINTR
) {
1942 for (i
= 0; i
< nb_fd
; i
++) {
1943 health_code_update();
1945 /* Fetch once the poll data */
1946 revents
= LTTNG_POLL_GETEV(&events
, i
);
1947 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1950 /* No activity for this FD (poll implementation). */
1954 /* Thread quit pipe has been closed. Killing thread. */
1955 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1961 /* Event on the registration socket */
1962 if (pollfd
== apps_sock
) {
1963 if (revents
& LPOLLIN
) {
1964 sock
= lttcomm_accept_unix_sock(apps_sock
);
1970 * Set socket timeout for both receiving and ending.
1971 * app_socket_timeout is in seconds, whereas
1972 * lttcomm_setsockopt_rcv_timeout and
1973 * lttcomm_setsockopt_snd_timeout expect msec as
1976 if (config
.app_socket_timeout
>= 0) {
1977 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1978 config
.app_socket_timeout
* 1000);
1979 (void) lttcomm_setsockopt_snd_timeout(sock
,
1980 config
.app_socket_timeout
* 1000);
1984 * Set the CLOEXEC flag. Return code is useless because
1985 * either way, the show must go on.
1987 (void) utils_set_fd_cloexec(sock
);
1989 /* Create UST registration command for enqueuing */
1990 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1991 if (ust_cmd
== NULL
) {
1992 PERROR("ust command zmalloc");
2001 * Using message-based transmissions to ensure we don't
2002 * have to deal with partially received messages.
2004 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2006 ERR("Exhausted file descriptors allowed for applications.");
2016 health_code_update();
2017 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2020 /* Close socket of the application. */
2025 lttng_fd_put(LTTNG_FD_APPS
, 1);
2029 health_code_update();
2031 ust_cmd
->sock
= sock
;
2034 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2035 " gid:%d sock:%d name:%s (version %d.%d)",
2036 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2037 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2038 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2039 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2042 * Lock free enqueue the registration request. The red pill
2043 * has been taken! This apps will be part of the *system*.
2045 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2048 * Wake the registration queue futex. Implicit memory
2049 * barrier with the exchange in cds_wfcq_enqueue.
2051 futex_nto1_wake(&ust_cmd_queue
.futex
);
2052 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2053 ERR("Register apps socket poll error");
2056 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2065 /* Notify that the registration thread is gone */
2068 if (apps_sock
>= 0) {
2069 ret
= close(apps_sock
);
2079 lttng_fd_put(LTTNG_FD_APPS
, 1);
2081 unlink(config
.apps_unix_sock_path
.value
);
2084 lttng_poll_clean(&events
);
2088 DBG("UST Registration thread cleanup complete");
2091 ERR("Health error occurred in %s", __func__
);
2093 health_unregister(health_sessiond
);
2099 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2100 * exec or it will fails.
2102 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2105 struct timespec timeout
;
2108 * Make sure we set the readiness flag to 0 because we are NOT ready.
2109 * This access to consumer_thread_is_ready does not need to be
2110 * protected by consumer_data.cond_mutex (yet) since the consumer
2111 * management thread has not been started at this point.
2113 consumer_data
->consumer_thread_is_ready
= 0;
2115 /* Setup pthread condition */
2116 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2119 PERROR("pthread_condattr_init consumer data");
2124 * Set the monotonic clock in order to make sure we DO NOT jump in time
2125 * between the clock_gettime() call and the timedwait call. See bug #324
2126 * for a more details and how we noticed it.
2128 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2131 PERROR("pthread_condattr_setclock consumer data");
2135 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2138 PERROR("pthread_cond_init consumer data");
2142 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2143 thread_manage_consumer
, consumer_data
);
2146 PERROR("pthread_create consumer");
2151 /* We are about to wait on a pthread condition */
2152 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2154 /* Get time for sem_timedwait absolute timeout */
2155 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2157 * Set the timeout for the condition timed wait even if the clock gettime
2158 * call fails since we might loop on that call and we want to avoid to
2159 * increment the timeout too many times.
2161 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2164 * The following loop COULD be skipped in some conditions so this is why we
2165 * set ret to 0 in order to make sure at least one round of the loop is
2171 * Loop until the condition is reached or when a timeout is reached. Note
2172 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2173 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2174 * possible. This loop does not take any chances and works with both of
2177 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2178 if (clock_ret
< 0) {
2179 PERROR("clock_gettime spawn consumer");
2180 /* Infinite wait for the consumerd thread to be ready */
2181 ret
= pthread_cond_wait(&consumer_data
->cond
,
2182 &consumer_data
->cond_mutex
);
2184 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2185 &consumer_data
->cond_mutex
, &timeout
);
2189 /* Release the pthread condition */
2190 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2194 if (ret
== ETIMEDOUT
) {
2198 * Call has timed out so we kill the kconsumerd_thread and return
2201 ERR("Condition timed out. The consumer thread was never ready."
2203 pth_ret
= pthread_cancel(consumer_data
->thread
);
2205 PERROR("pthread_cancel consumer thread");
2208 PERROR("pthread_cond_wait failed consumer thread");
2210 /* Caller is expecting a negative value on failure. */
2215 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2216 if (consumer_data
->pid
== 0) {
2217 ERR("Consumerd did not start");
2218 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2221 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2230 * Join consumer thread
2232 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2236 /* Consumer pid must be a real one. */
2237 if (consumer_data
->pid
> 0) {
2239 ret
= kill(consumer_data
->pid
, SIGTERM
);
2241 PERROR("Error killing consumer daemon");
2244 return pthread_join(consumer_data
->thread
, &status
);
2251 * Fork and exec a consumer daemon (consumerd).
2253 * Return pid if successful else -1.
2255 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2259 const char *consumer_to_use
;
2260 const char *verbosity
;
2263 DBG("Spawning consumerd");
2270 if (config
.verbose_consumer
) {
2271 verbosity
= "--verbose";
2272 } else if (lttng_opt_quiet
) {
2273 verbosity
= "--quiet";
2278 switch (consumer_data
->type
) {
2279 case LTTNG_CONSUMER_KERNEL
:
2281 * Find out which consumerd to execute. We will first try the
2282 * 64-bit path, then the sessiond's installation directory, and
2283 * fallback on the 32-bit one,
2285 DBG3("Looking for a kernel consumer at these locations:");
2286 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2287 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2288 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2289 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2290 DBG3("Found location #1");
2291 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2292 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2293 DBG3("Found location #2");
2294 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2295 } else if (config
.consumerd32_bin_path
.value
&&
2296 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2297 DBG3("Found location #3");
2298 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2300 DBG("Could not find any valid consumerd executable");
2304 DBG("Using kernel consumer at: %s", consumer_to_use
);
2305 (void) execl(consumer_to_use
,
2306 "lttng-consumerd", verbosity
, "-k",
2307 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2308 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2309 "--group", config
.tracing_group_name
.value
,
2312 case LTTNG_CONSUMER64_UST
:
2314 if (config
.consumerd64_lib_dir
.value
) {
2319 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2323 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2324 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2329 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2330 if (tmp
[0] != '\0') {
2331 strcat(tmpnew
, ":");
2332 strcat(tmpnew
, tmp
);
2334 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2341 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2342 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2343 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2344 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2345 "--group", config
.tracing_group_name
.value
,
2349 case LTTNG_CONSUMER32_UST
:
2351 if (config
.consumerd32_lib_dir
.value
) {
2356 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2360 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2361 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2366 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2367 if (tmp
[0] != '\0') {
2368 strcat(tmpnew
, ":");
2369 strcat(tmpnew
, tmp
);
2371 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2378 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2379 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2380 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2381 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2382 "--group", config
.tracing_group_name
.value
,
2387 ERR("unknown consumer type");
2391 PERROR("Consumer execl()");
2393 /* Reaching this point, we got a failure on our execl(). */
2395 } else if (pid
> 0) {
2398 PERROR("start consumer fork");
2406 * Spawn the consumerd daemon and session daemon thread.
2408 static int start_consumerd(struct consumer_data
*consumer_data
)
2413 * Set the listen() state on the socket since there is a possible race
2414 * between the exec() of the consumer daemon and this call if place in the
2415 * consumer thread. See bug #366 for more details.
2417 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2422 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2423 if (consumer_data
->pid
!= 0) {
2424 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2428 ret
= spawn_consumerd(consumer_data
);
2430 ERR("Spawning consumerd failed");
2431 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2435 /* Setting up the consumer_data pid */
2436 consumer_data
->pid
= ret
;
2437 DBG2("Consumer pid %d", consumer_data
->pid
);
2438 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2440 DBG2("Spawning consumer control thread");
2441 ret
= spawn_consumer_thread(consumer_data
);
2443 ERR("Fatal error spawning consumer control thread");
2451 /* Cleanup already created sockets on error. */
2452 if (consumer_data
->err_sock
>= 0) {
2455 err
= close(consumer_data
->err_sock
);
2457 PERROR("close consumer data error socket");
2464 * Setup necessary data for kernel tracer action.
2466 static int init_kernel_tracer(void)
2470 /* Modprobe lttng kernel modules */
2471 ret
= modprobe_lttng_control();
2476 /* Open debugfs lttng */
2477 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2478 if (kernel_tracer_fd
< 0) {
2479 DBG("Failed to open %s", module_proc_lttng
);
2483 /* Validate kernel version */
2484 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2485 &kernel_tracer_abi_version
);
2490 ret
= modprobe_lttng_data();
2495 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2502 WARN("Kernel tracer does not support buffer monitoring. "
2503 "The monitoring timer of channels in the kernel domain "
2504 "will be set to 0 (disabled).");
2507 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2511 modprobe_remove_lttng_control();
2512 ret
= close(kernel_tracer_fd
);
2516 kernel_tracer_fd
= -1;
2517 return LTTNG_ERR_KERN_VERSION
;
2520 ret
= close(kernel_tracer_fd
);
2526 modprobe_remove_lttng_control();
2529 WARN("No kernel tracer available");
2530 kernel_tracer_fd
= -1;
2532 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2534 return LTTNG_ERR_KERN_NA
;
2540 * Copy consumer output from the tracing session to the domain session. The
2541 * function also applies the right modification on a per domain basis for the
2542 * trace files destination directory.
2544 * Should *NOT* be called with RCU read-side lock held.
2546 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2549 const char *dir_name
;
2550 struct consumer_output
*consumer
;
2553 assert(session
->consumer
);
2556 case LTTNG_DOMAIN_KERNEL
:
2557 DBG3("Copying tracing session consumer output in kernel session");
2559 * XXX: We should audit the session creation and what this function
2560 * does "extra" in order to avoid a destroy since this function is used
2561 * in the domain session creation (kernel and ust) only. Same for UST
2564 if (session
->kernel_session
->consumer
) {
2565 consumer_output_put(session
->kernel_session
->consumer
);
2567 session
->kernel_session
->consumer
=
2568 consumer_copy_output(session
->consumer
);
2569 /* Ease our life a bit for the next part */
2570 consumer
= session
->kernel_session
->consumer
;
2571 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2573 case LTTNG_DOMAIN_JUL
:
2574 case LTTNG_DOMAIN_LOG4J
:
2575 case LTTNG_DOMAIN_PYTHON
:
2576 case LTTNG_DOMAIN_UST
:
2577 DBG3("Copying tracing session consumer output in UST session");
2578 if (session
->ust_session
->consumer
) {
2579 consumer_output_put(session
->ust_session
->consumer
);
2581 session
->ust_session
->consumer
=
2582 consumer_copy_output(session
->consumer
);
2583 /* Ease our life a bit for the next part */
2584 consumer
= session
->ust_session
->consumer
;
2585 dir_name
= DEFAULT_UST_TRACE_DIR
;
2588 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2592 /* Append correct directory to subdir */
2593 strncat(consumer
->subdir
, dir_name
,
2594 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2595 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2604 * Create an UST session and add it to the session ust list.
2606 * Should *NOT* be called with RCU read-side lock held.
2608 static int create_ust_session(struct ltt_session
*session
,
2609 struct lttng_domain
*domain
)
2612 struct ltt_ust_session
*lus
= NULL
;
2616 assert(session
->consumer
);
2618 switch (domain
->type
) {
2619 case LTTNG_DOMAIN_JUL
:
2620 case LTTNG_DOMAIN_LOG4J
:
2621 case LTTNG_DOMAIN_PYTHON
:
2622 case LTTNG_DOMAIN_UST
:
2625 ERR("Unknown UST domain on create session %d", domain
->type
);
2626 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2630 DBG("Creating UST session");
2632 lus
= trace_ust_create_session(session
->id
);
2634 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2638 lus
->uid
= session
->uid
;
2639 lus
->gid
= session
->gid
;
2640 lus
->output_traces
= session
->output_traces
;
2641 lus
->snapshot_mode
= session
->snapshot_mode
;
2642 lus
->live_timer_interval
= session
->live_timer
;
2643 session
->ust_session
= lus
;
2644 if (session
->shm_path
[0]) {
2645 strncpy(lus
->root_shm_path
, session
->shm_path
,
2646 sizeof(lus
->root_shm_path
));
2647 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2648 strncpy(lus
->shm_path
, session
->shm_path
,
2649 sizeof(lus
->shm_path
));
2650 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2651 strncat(lus
->shm_path
, "/ust",
2652 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2654 /* Copy session output to the newly created UST session */
2655 ret
= copy_session_consumer(domain
->type
, session
);
2656 if (ret
!= LTTNG_OK
) {
2664 session
->ust_session
= NULL
;
2669 * Create a kernel tracer session then create the default channel.
2671 static int create_kernel_session(struct ltt_session
*session
)
2675 DBG("Creating kernel session");
2677 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2679 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2683 /* Code flow safety */
2684 assert(session
->kernel_session
);
2686 /* Copy session output to the newly created Kernel session */
2687 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2688 if (ret
!= LTTNG_OK
) {
2692 session
->kernel_session
->uid
= session
->uid
;
2693 session
->kernel_session
->gid
= session
->gid
;
2694 session
->kernel_session
->output_traces
= session
->output_traces
;
2695 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2700 trace_kernel_destroy_session(session
->kernel_session
);
2701 session
->kernel_session
= NULL
;
2706 * Count number of session permitted by uid/gid.
2708 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2711 struct ltt_session
*session
;
2712 const struct ltt_session_list
*session_list
= session_get_list();
2714 DBG("Counting number of available session for UID %d GID %d",
2716 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
2717 if (!session_get(session
)) {
2720 session_lock(session
);
2721 /* Only count the sessions the user can control. */
2722 if (session_access_ok(session
, uid
, gid
) &&
2723 !session
->destroyed
) {
2726 session_unlock(session
);
2727 session_put(session
);
2732 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2733 int *sock_error
, struct lttng_event
*event
)
2736 struct lttng_userspace_probe_location
*probe_location
;
2737 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2738 struct lttng_dynamic_buffer probe_location_buffer
;
2739 struct lttng_buffer_view buffer_view
;
2742 * Create a buffer to store the serialized version of the probe
2745 lttng_dynamic_buffer_init(&probe_location_buffer
);
2746 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2747 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2749 ret
= LTTNG_ERR_NOMEM
;
2754 * Receive the probe location.
2756 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2757 probe_location_buffer
.size
);
2759 DBG("Nothing recv() from client var len data... continuing");
2761 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2762 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2766 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2767 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2770 * Extract the probe location from the serialized version.
2772 ret
= lttng_userspace_probe_location_create_from_buffer(
2773 &buffer_view
, &probe_location
);
2775 WARN("Failed to create a userspace probe location from the received buffer");
2776 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2777 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2782 * Receive the file descriptor to the target binary from the client.
2784 DBG("Receiving userspace probe target FD from client ...");
2785 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2787 DBG("Nothing recv() from client userspace probe fd... continuing");
2789 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2794 * Set the file descriptor received from the client through the unix
2795 * socket in the probe location.
2797 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
2799 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2804 * From the kernel tracer's perspective, all userspace probe event types
2805 * are all the same: a file and an offset.
2807 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
2808 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
2809 ret
= lttng_userspace_probe_location_function_set_binary_fd(
2810 probe_location
, fd
);
2812 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
2813 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
2814 probe_location
, fd
);
2817 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2822 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2826 /* Attach the probe location to the event. */
2827 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
2829 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2833 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2839 * Check if the current kernel tracer supports the session rotation feature.
2840 * Return 1 if it does, 0 otherwise.
2842 static int check_rotate_compatible(void)
2846 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2847 DBG("Kernel tracer version is not compatible with the rotation feature");
2855 * Process the command requested by the lttng client within the command
2856 * context structure. This function make sure that the return structure (llm)
2857 * is set and ready for transmission before returning.
2859 * Return any error encountered or 0 for success.
2861 * "sock" is only used for special-case var. len data.
2863 * Should *NOT* be called with RCU read-side lock held.
2865 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2869 int need_tracing_session
= 1;
2872 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2874 assert(!rcu_read_ongoing());
2878 switch (cmd_ctx
->lsm
->cmd_type
) {
2879 case LTTNG_CREATE_SESSION
:
2880 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2881 case LTTNG_CREATE_SESSION_LIVE
:
2882 case LTTNG_DESTROY_SESSION
:
2883 case LTTNG_LIST_SESSIONS
:
2884 case LTTNG_LIST_DOMAINS
:
2885 case LTTNG_START_TRACE
:
2886 case LTTNG_STOP_TRACE
:
2887 case LTTNG_DATA_PENDING
:
2888 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2889 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2890 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2891 case LTTNG_SNAPSHOT_RECORD
:
2892 case LTTNG_SAVE_SESSION
:
2893 case LTTNG_SET_SESSION_SHM_PATH
:
2894 case LTTNG_REGENERATE_METADATA
:
2895 case LTTNG_REGENERATE_STATEDUMP
:
2896 case LTTNG_REGISTER_TRIGGER
:
2897 case LTTNG_UNREGISTER_TRIGGER
:
2898 case LTTNG_ROTATE_SESSION
:
2899 case LTTNG_ROTATION_GET_INFO
:
2900 case LTTNG_ROTATION_SET_SCHEDULE
:
2901 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2908 if (config
.no_kernel
&& need_domain
2909 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2911 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2913 ret
= LTTNG_ERR_KERN_NA
;
2918 /* Deny register consumer if we already have a spawned consumer. */
2919 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2920 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2921 if (kconsumer_data
.pid
> 0) {
2922 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2923 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2926 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2930 * Check for command that don't needs to allocate a returned payload. We do
2931 * this here so we don't have to make the call for no payload at each
2934 switch(cmd_ctx
->lsm
->cmd_type
) {
2935 case LTTNG_LIST_SESSIONS
:
2936 case LTTNG_LIST_TRACEPOINTS
:
2937 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2938 case LTTNG_LIST_DOMAINS
:
2939 case LTTNG_LIST_CHANNELS
:
2940 case LTTNG_LIST_EVENTS
:
2941 case LTTNG_LIST_SYSCALLS
:
2942 case LTTNG_LIST_TRACKER_PIDS
:
2943 case LTTNG_DATA_PENDING
:
2944 case LTTNG_ROTATE_SESSION
:
2945 case LTTNG_ROTATION_GET_INFO
:
2946 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2949 /* Setup lttng message with no payload */
2950 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2952 /* This label does not try to unlock the session */
2953 goto init_setup_error
;
2957 /* Commands that DO NOT need a session. */
2958 switch (cmd_ctx
->lsm
->cmd_type
) {
2959 case LTTNG_CREATE_SESSION
:
2960 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2961 case LTTNG_CREATE_SESSION_LIVE
:
2962 case LTTNG_LIST_SESSIONS
:
2963 case LTTNG_LIST_TRACEPOINTS
:
2964 case LTTNG_LIST_SYSCALLS
:
2965 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2966 case LTTNG_SAVE_SESSION
:
2967 case LTTNG_REGISTER_TRIGGER
:
2968 case LTTNG_UNREGISTER_TRIGGER
:
2969 need_tracing_session
= 0;
2972 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2974 * We keep the session list lock across _all_ commands
2975 * for now, because the per-session lock does not
2976 * handle teardown properly.
2978 session_lock_list();
2979 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2980 if (cmd_ctx
->session
== NULL
) {
2981 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2984 /* Acquire lock for the session */
2985 session_lock(cmd_ctx
->session
);
2991 * Commands that need a valid session but should NOT create one if none
2992 * exists. Instead of creating one and destroying it when the command is
2993 * handled, process that right before so we save some round trip in useless
2996 switch (cmd_ctx
->lsm
->cmd_type
) {
2997 case LTTNG_DISABLE_CHANNEL
:
2998 case LTTNG_DISABLE_EVENT
:
2999 switch (cmd_ctx
->lsm
->domain
.type
) {
3000 case LTTNG_DOMAIN_KERNEL
:
3001 if (!cmd_ctx
->session
->kernel_session
) {
3002 ret
= LTTNG_ERR_NO_CHANNEL
;
3006 case LTTNG_DOMAIN_JUL
:
3007 case LTTNG_DOMAIN_LOG4J
:
3008 case LTTNG_DOMAIN_PYTHON
:
3009 case LTTNG_DOMAIN_UST
:
3010 if (!cmd_ctx
->session
->ust_session
) {
3011 ret
= LTTNG_ERR_NO_CHANNEL
;
3016 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3028 * Check domain type for specific "pre-action".
3030 switch (cmd_ctx
->lsm
->domain
.type
) {
3031 case LTTNG_DOMAIN_KERNEL
:
3033 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3037 /* Kernel tracer check */
3038 if (kernel_tracer_fd
== -1) {
3039 /* Basically, load kernel tracer modules */
3040 ret
= init_kernel_tracer();
3046 /* Consumer is in an ERROR state. Report back to client */
3047 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3048 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3052 /* Need a session for kernel command */
3053 if (need_tracing_session
) {
3054 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3055 ret
= create_kernel_session(cmd_ctx
->session
);
3057 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3062 /* Start the kernel consumer daemon */
3063 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3064 if (kconsumer_data
.pid
== 0 &&
3065 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3066 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3067 ret
= start_consumerd(&kconsumer_data
);
3069 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3072 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3074 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3078 * The consumer was just spawned so we need to add the socket to
3079 * the consumer output of the session if exist.
3081 ret
= consumer_create_socket(&kconsumer_data
,
3082 cmd_ctx
->session
->kernel_session
->consumer
);
3089 case LTTNG_DOMAIN_JUL
:
3090 case LTTNG_DOMAIN_LOG4J
:
3091 case LTTNG_DOMAIN_PYTHON
:
3092 case LTTNG_DOMAIN_UST
:
3094 if (!ust_app_supported()) {
3095 ret
= LTTNG_ERR_NO_UST
;
3098 /* Consumer is in an ERROR state. Report back to client */
3099 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3100 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3104 if (need_tracing_session
) {
3105 /* Create UST session if none exist. */
3106 if (cmd_ctx
->session
->ust_session
== NULL
) {
3107 ret
= create_ust_session(cmd_ctx
->session
,
3108 &cmd_ctx
->lsm
->domain
);
3109 if (ret
!= LTTNG_OK
) {
3114 /* Start the UST consumer daemons */
3116 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3117 if (config
.consumerd64_bin_path
.value
&&
3118 ustconsumer64_data
.pid
== 0 &&
3119 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3120 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3121 ret
= start_consumerd(&ustconsumer64_data
);
3123 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3124 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3128 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3129 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3131 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3135 * Setup socket for consumer 64 bit. No need for atomic access
3136 * since it was set above and can ONLY be set in this thread.
3138 ret
= consumer_create_socket(&ustconsumer64_data
,
3139 cmd_ctx
->session
->ust_session
->consumer
);
3145 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3146 if (config
.consumerd32_bin_path
.value
&&
3147 ustconsumer32_data
.pid
== 0 &&
3148 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3149 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3150 ret
= start_consumerd(&ustconsumer32_data
);
3152 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3153 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3157 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3158 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3160 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3164 * Setup socket for consumer 32 bit. No need for atomic access
3165 * since it was set above and can ONLY be set in this thread.
3167 ret
= consumer_create_socket(&ustconsumer32_data
,
3168 cmd_ctx
->session
->ust_session
->consumer
);
3180 /* Validate consumer daemon state when start/stop trace command */
3181 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3182 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3183 switch (cmd_ctx
->lsm
->domain
.type
) {
3184 case LTTNG_DOMAIN_NONE
:
3186 case LTTNG_DOMAIN_JUL
:
3187 case LTTNG_DOMAIN_LOG4J
:
3188 case LTTNG_DOMAIN_PYTHON
:
3189 case LTTNG_DOMAIN_UST
:
3190 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3191 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3195 case LTTNG_DOMAIN_KERNEL
:
3196 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3197 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3202 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3208 * Check that the UID or GID match that of the tracing session.
3209 * The root user can interact with all sessions.
3211 if (need_tracing_session
) {
3212 if (!session_access_ok(cmd_ctx
->session
,
3213 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3214 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||