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"
87 static const char *help_msg
=
88 #ifdef LTTNG_EMBED_HELP
89 #include <lttng-sessiond.8.h>
96 static int lockfile_fd
= -1;
98 /* Set to 1 when a SIGUSR1 signal is received. */
99 static int recv_child_signal
;
101 static struct lttng_kernel_tracer_version kernel_tracer_version
;
102 static struct lttng_kernel_tracer_abi_version kernel_tracer_abi_version
;
105 * Consumer daemon specific control data. Every value not initialized here is
106 * set to 0 by the static definition.
108 static struct consumer_data kconsumer_data
= {
109 .type
= LTTNG_CONSUMER_KERNEL
,
112 .channel_monitor_pipe
= -1,
113 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
114 .lock
= PTHREAD_MUTEX_INITIALIZER
,
115 .cond
= PTHREAD_COND_INITIALIZER
,
116 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
118 static struct consumer_data ustconsumer64_data
= {
119 .type
= LTTNG_CONSUMER64_UST
,
122 .channel_monitor_pipe
= -1,
123 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
124 .lock
= PTHREAD_MUTEX_INITIALIZER
,
125 .cond
= PTHREAD_COND_INITIALIZER
,
126 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
128 static struct consumer_data ustconsumer32_data
= {
129 .type
= LTTNG_CONSUMER32_UST
,
132 .channel_monitor_pipe
= -1,
133 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
134 .lock
= PTHREAD_MUTEX_INITIALIZER
,
135 .cond
= PTHREAD_COND_INITIALIZER
,
136 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
139 /* Command line options */
140 static const struct option long_options
[] = {
141 { "client-sock", required_argument
, 0, 'c' },
142 { "apps-sock", required_argument
, 0, 'a' },
143 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
144 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
145 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
146 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
147 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
148 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
149 { "consumerd32-path", required_argument
, 0, '\0' },
150 { "consumerd32-libdir", required_argument
, 0, '\0' },
151 { "consumerd64-path", required_argument
, 0, '\0' },
152 { "consumerd64-libdir", required_argument
, 0, '\0' },
153 { "daemonize", no_argument
, 0, 'd' },
154 { "background", no_argument
, 0, 'b' },
155 { "sig-parent", no_argument
, 0, 'S' },
156 { "help", no_argument
, 0, 'h' },
157 { "group", required_argument
, 0, 'g' },
158 { "version", no_argument
, 0, 'V' },
159 { "quiet", no_argument
, 0, 'q' },
160 { "verbose", no_argument
, 0, 'v' },
161 { "verbose-consumer", no_argument
, 0, '\0' },
162 { "no-kernel", no_argument
, 0, '\0' },
163 { "pidfile", required_argument
, 0, 'p' },
164 { "agent-tcp-port", required_argument
, 0, '\0' },
165 { "config", required_argument
, 0, 'f' },
166 { "load", required_argument
, 0, 'l' },
167 { "kmod-probes", required_argument
, 0, '\0' },
168 { "extra-kmod-probes", required_argument
, 0, '\0' },
172 /* Command line options to ignore from configuration file */
173 static const char *config_ignore_options
[] = { "help", "version", "config" };
175 /* Shared between threads */
176 static int dispatch_thread_exit
;
178 /* Sockets and FDs */
179 static int client_sock
= -1;
180 static int apps_sock
= -1;
183 * This pipe is used to inform the thread managing application communication
184 * that a command is queued and ready to be processed.
186 static int apps_cmd_pipe
[2] = { -1, -1 };
188 /* Pthread, Mutexes and Semaphores */
189 static pthread_t apps_thread
;
190 static pthread_t apps_notify_thread
;
191 static pthread_t reg_apps_thread
;
192 static pthread_t client_thread
;
193 static pthread_t kernel_thread
;
194 static pthread_t dispatch_thread
;
195 static pthread_t agent_reg_thread
;
196 static pthread_t load_session_thread
;
199 * UST registration command queue. This queue is tied with a futex and uses a N
200 * wakers / 1 waiter implemented and detailed in futex.c/.h
202 * The thread_registration_apps and thread_dispatch_ust_registration uses this
203 * queue along with the wait/wake scheme. The thread_manage_apps receives down
204 * the line new application socket and monitors it for any I/O error or clean
205 * close that triggers an unregistration of the application.
207 static struct ust_cmd_queue ust_cmd_queue
;
209 static const char *module_proc_lttng
= "/proc/lttng";
212 * Consumer daemon state which is changed when spawning it, killing it or in
213 * case of a fatal error.
215 enum consumerd_state
{
216 CONSUMER_STARTED
= 1,
217 CONSUMER_STOPPED
= 2,
222 * This consumer daemon state is used to validate if a client command will be
223 * able to reach the consumer. If not, the client is informed. For instance,
224 * doing a "lttng start" when the consumer state is set to ERROR will return an
225 * error to the client.
227 * The following example shows a possible race condition of this scheme:
229 * consumer thread error happens
231 * client cmd checks state -> still OK
232 * consumer thread exit, sets error
233 * client cmd try to talk to consumer
236 * However, since the consumer is a different daemon, we have no way of making
237 * sure the command will reach it safely even with this state flag. This is why
238 * we consider that up to the state validation during command processing, the
239 * command is safe. After that, we can not guarantee the correctness of the
240 * client request vis-a-vis the consumer.
242 static enum consumerd_state ust_consumerd_state
;
243 static enum consumerd_state kernel_consumerd_state
;
245 /* Load session thread information to operate. */
246 static struct load_session_thread_data
*load_info
;
249 * Section name to look for in the daemon configuration file.
251 static const char * const config_section_name
= "sessiond";
253 /* Am I root or not. Set to 1 if the daemon is running as root */
257 * Stop all threads by closing the thread quit pipe.
259 static void stop_threads(void)
263 /* Stopping all threads */
264 DBG("Terminating all threads");
265 ret
= sessiond_notify_quit_pipe();
267 ERR("write error on thread quit pipe");
270 /* Dispatch thread */
271 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
272 futex_nto1_wake(&ust_cmd_queue
.futex
);
276 * Close every consumer sockets.
278 static void close_consumer_sockets(void)
282 if (kconsumer_data
.err_sock
>= 0) {
283 ret
= close(kconsumer_data
.err_sock
);
285 PERROR("kernel consumer err_sock close");
288 if (ustconsumer32_data
.err_sock
>= 0) {
289 ret
= close(ustconsumer32_data
.err_sock
);
291 PERROR("UST consumerd32 err_sock close");
294 if (ustconsumer64_data
.err_sock
>= 0) {
295 ret
= close(ustconsumer64_data
.err_sock
);
297 PERROR("UST consumerd64 err_sock close");
300 if (kconsumer_data
.cmd_sock
>= 0) {
301 ret
= close(kconsumer_data
.cmd_sock
);
303 PERROR("kernel consumer cmd_sock close");
306 if (ustconsumer32_data
.cmd_sock
>= 0) {
307 ret
= close(ustconsumer32_data
.cmd_sock
);
309 PERROR("UST consumerd32 cmd_sock close");
312 if (ustconsumer64_data
.cmd_sock
>= 0) {
313 ret
= close(ustconsumer64_data
.cmd_sock
);
315 PERROR("UST consumerd64 cmd_sock close");
318 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
319 ret
= close(kconsumer_data
.channel_monitor_pipe
);
321 PERROR("kernel consumer channel monitor pipe close");
324 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
325 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
327 PERROR("UST consumerd32 channel monitor pipe close");
330 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
331 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
333 PERROR("UST consumerd64 channel monitor pipe close");
339 * Wait on consumer process termination.
341 * Need to be called with the consumer data lock held or from a context
342 * ensuring no concurrent access to data (e.g: cleanup).
344 static void wait_consumer(struct consumer_data
*consumer_data
)
349 if (consumer_data
->pid
<= 0) {
353 DBG("Waiting for complete teardown of consumerd (PID: %d)",
355 ret
= waitpid(consumer_data
->pid
, &status
, 0);
357 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
358 } else if (!WIFEXITED(status
)) {
359 ERR("consumerd termination with error: %d",
362 consumer_data
->pid
= 0;
366 * Cleanup the session daemon's data structures.
368 static void sessiond_cleanup(void)
371 struct ltt_session_list
*session_list
= session_get_list();
373 DBG("Cleanup sessiond");
376 * Close the thread quit pipe. It has already done its job,
377 * since we are now called.
379 sessiond_close_quit_pipe();
381 ret
= remove(config
.pid_file_path
.value
);
383 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
386 DBG("Removing sessiond and consumerd content of directory %s",
387 config
.rundir
.value
);
390 DBG("Removing %s", config
.pid_file_path
.value
);
391 (void) unlink(config
.pid_file_path
.value
);
393 DBG("Removing %s", config
.agent_port_file_path
.value
);
394 (void) unlink(config
.agent_port_file_path
.value
);
397 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
398 (void) unlink(kconsumer_data
.err_unix_sock_path
);
400 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
401 (void) rmdir(config
.kconsumerd_path
.value
);
403 /* ust consumerd 32 */
404 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
405 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
407 DBG("Removing directory %s", config
.consumerd32_path
.value
);
408 (void) rmdir(config
.consumerd32_path
.value
);
410 /* ust consumerd 64 */
411 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
412 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
414 DBG("Removing directory %s", config
.consumerd64_path
.value
);
415 (void) rmdir(config
.consumerd64_path
.value
);
417 pthread_mutex_destroy(&session_list
->lock
);
419 wait_consumer(&kconsumer_data
);
420 wait_consumer(&ustconsumer64_data
);
421 wait_consumer(&ustconsumer32_data
);
423 DBG("Cleaning up all agent apps");
424 agent_app_ht_clean();
426 DBG("Closing all UST sockets");
427 ust_app_clean_list();
428 buffer_reg_destroy_registries();
430 if (is_root
&& !config
.no_kernel
) {
431 DBG2("Closing kernel fd");
432 if (kernel_tracer_fd
>= 0) {
433 ret
= close(kernel_tracer_fd
);
438 DBG("Unloading kernel modules");
439 modprobe_remove_lttng_all();
443 close_consumer_sockets();
446 load_session_destroy_data(load_info
);
451 * We do NOT rmdir rundir because there are other processes
452 * using it, for instance lttng-relayd, which can start in
453 * parallel with this teardown.
458 * Cleanup the daemon's option data structures.
460 static void sessiond_cleanup_options(void)
462 DBG("Cleaning up options");
464 sessiond_config_fini(&config
);
466 run_as_destroy_worker();
470 * Send data on a unix socket using the liblttsessiondcomm API.
472 * Return lttcomm error code.
474 static int send_unix_sock(int sock
, void *buf
, size_t len
)
476 /* Check valid length */
481 return lttcomm_send_unix_sock(sock
, buf
, len
);
485 * Free memory of a command context structure.
487 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
489 DBG("Clean command context structure");
491 if ((*cmd_ctx
)->llm
) {
492 free((*cmd_ctx
)->llm
);
494 if ((*cmd_ctx
)->lsm
) {
495 free((*cmd_ctx
)->lsm
);
503 * Notify UST applications using the shm mmap futex.
505 static int notify_ust_apps(int active
)
509 DBG("Notifying applications of session daemon state: %d", active
);
511 /* See shm.c for this call implying mmap, shm and futex calls */
512 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
513 if (wait_shm_mmap
== NULL
) {
517 /* Wake waiting process */
518 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
520 /* Apps notified successfully */
528 * Setup the outgoing data buffer for the response (llm) by allocating the
529 * right amount of memory and copying the original information from the lsm
532 * Return 0 on success, negative value on error.
534 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
535 const void *payload_buf
, size_t payload_len
,
536 const void *cmd_header_buf
, size_t cmd_header_len
)
539 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
540 const size_t cmd_header_offset
= header_len
;
541 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
542 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
544 cmd_ctx
->llm
= zmalloc(total_msg_size
);
546 if (cmd_ctx
->llm
== NULL
) {
552 /* Copy common data */
553 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
554 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
555 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
556 cmd_ctx
->llm
->data_size
= payload_len
;
557 cmd_ctx
->lttng_msg_size
= total_msg_size
;
559 /* Copy command header */
560 if (cmd_header_len
) {
561 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
567 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
576 * Version of setup_lttng_msg() without command header.
578 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
579 void *payload_buf
, size_t payload_len
)
581 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
584 * Update the kernel poll set of all channel fd available over all tracing
585 * session. Add the wakeup pipe at the end of the set.
587 static int update_kernel_poll(struct lttng_poll_event
*events
)
590 struct ltt_kernel_channel
*channel
;
591 struct ltt_session
*session
;
592 const struct ltt_session_list
*session_list
= session_get_list();
594 DBG("Updating kernel poll set");
597 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
598 if (!session_get(session
)) {
601 session_lock(session
);
602 if (session
->kernel_session
== NULL
) {
603 session_unlock(session
);
604 session_put(session
);
608 cds_list_for_each_entry(channel
,
609 &session
->kernel_session
->channel_list
.head
, list
) {
610 /* Add channel fd to the kernel poll set */
611 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
613 session_unlock(session
);
614 session_put(session
);
617 DBG("Channel fd %d added to kernel set", channel
->fd
);
619 session_unlock(session
);
621 session_unlock_list();
626 session_unlock_list();
631 * Find the channel fd from 'fd' over all tracing session. When found, check
632 * for new channel stream and send those stream fds to the kernel consumer.
634 * Useful for CPU hotplug feature.
636 static int update_kernel_stream(int fd
)
639 struct ltt_session
*session
;
640 struct ltt_kernel_session
*ksess
;
641 struct ltt_kernel_channel
*channel
;
642 const struct ltt_session_list
*session_list
= session_get_list();
644 DBG("Updating kernel streams for channel fd %d", fd
);
647 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
648 if (!session_get(session
)) {
651 session_lock(session
);
652 if (session
->kernel_session
== NULL
) {
653 session_unlock(session
);
654 session_put(session
);
657 ksess
= session
->kernel_session
;
659 cds_list_for_each_entry(channel
,
660 &ksess
->channel_list
.head
, list
) {
661 struct lttng_ht_iter iter
;
662 struct consumer_socket
*socket
;
664 if (channel
->fd
!= fd
) {
667 DBG("Channel found, updating kernel streams");
668 ret
= kernel_open_channel_stream(channel
);
672 /* Update the stream global counter */
673 ksess
->stream_count_global
+= ret
;
676 * Have we already sent fds to the consumer? If yes, it
677 * means that tracing is started so it is safe to send
678 * our updated stream fds.
680 if (ksess
->consumer_fds_sent
!= 1
681 || ksess
->consumer
== NULL
) {
687 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
688 &iter
.iter
, socket
, node
.node
) {
689 pthread_mutex_lock(socket
->lock
);
690 ret
= kernel_consumer_send_channel_streams(socket
,
692 session
->output_traces
? 1 : 0);
693 pthread_mutex_unlock(socket
->lock
);
701 session_unlock(session
);
702 session_put(session
);
704 session_unlock_list();
708 session_unlock(session
);
709 session_put(session
);
710 session_unlock_list();
715 * For each tracing session, update newly registered apps. The session list
716 * lock MUST be acquired before calling this.
718 static void update_ust_app(int app_sock
)
720 struct ltt_session
*sess
, *stmp
;
721 const struct ltt_session_list
*session_list
= session_get_list();
723 /* Consumer is in an ERROR state. Stop any application update. */
724 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
725 /* Stop the update process since the consumer is dead. */
729 /* For all tracing session(s) */
730 cds_list_for_each_entry_safe(sess
, stmp
, &session_list
->head
, list
) {
733 if (!session_get(sess
)) {
737 if (!sess
->ust_session
) {
742 assert(app_sock
>= 0);
743 app
= ust_app_find_by_sock(app_sock
);
746 * Application can be unregistered before so
747 * this is possible hence simply stopping the
750 DBG3("UST app update failed to find app sock %d",
754 ust_app_global_update(sess
->ust_session
, app
);
758 session_unlock(sess
);
764 * This thread manage event coming from the kernel.
766 * Features supported in this thread:
769 static void *thread_manage_kernel(void *data
)
771 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
772 uint32_t revents
, nb_fd
;
774 struct lttng_poll_event events
;
776 DBG("[thread] Thread manage kernel started");
778 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
781 * This first step of the while is to clean this structure which could free
782 * non NULL pointers so initialize it before the loop.
784 lttng_poll_init(&events
);
786 if (testpoint(sessiond_thread_manage_kernel
)) {
787 goto error_testpoint
;
790 health_code_update();
792 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
793 goto error_testpoint
;
797 health_code_update();
799 if (update_poll_flag
== 1) {
800 /* Clean events object. We are about to populate it again. */
801 lttng_poll_clean(&events
);
803 ret
= sessiond_set_thread_pollset(&events
, 2);
805 goto error_poll_create
;
808 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
813 /* This will add the available kernel channel if any. */
814 ret
= update_kernel_poll(&events
);
818 update_poll_flag
= 0;
821 DBG("Thread kernel polling");
823 /* Poll infinite value of time */
826 ret
= lttng_poll_wait(&events
, -1);
827 DBG("Thread kernel return from poll on %d fds",
828 LTTNG_POLL_GETNB(&events
));
832 * Restart interrupted system call.
834 if (errno
== EINTR
) {
838 } else if (ret
== 0) {
839 /* Should not happen since timeout is infinite */
840 ERR("Return value of poll is 0 with an infinite timeout.\n"
841 "This should not have happened! Continuing...");
847 for (i
= 0; i
< nb_fd
; i
++) {
848 /* Fetch once the poll data */
849 revents
= LTTNG_POLL_GETEV(&events
, i
);
850 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
852 health_code_update();
855 /* No activity for this FD (poll implementation). */
859 /* Thread quit pipe has been closed. Killing thread. */
860 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
866 /* Check for data on kernel pipe */
867 if (revents
& LPOLLIN
) {
868 if (pollfd
== kernel_poll_pipe
[0]) {
869 (void) lttng_read(kernel_poll_pipe
[0],
872 * Ret value is useless here, if this pipe gets any actions an
873 * update is required anyway.
875 update_poll_flag
= 1;
879 * New CPU detected by the kernel. Adding kernel stream to
880 * kernel session and updating the kernel consumer
882 ret
= update_kernel_stream(pollfd
);
888 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
889 update_poll_flag
= 1;
892 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
900 lttng_poll_clean(&events
);
903 utils_close_pipe(kernel_poll_pipe
);
904 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
907 ERR("Health error occurred in %s", __func__
);
908 WARN("Kernel thread died unexpectedly. "
909 "Kernel tracing can continue but CPU hotplug is disabled.");
911 health_unregister(health_sessiond
);
912 DBG("Kernel thread dying");
917 * Signal pthread condition of the consumer data that the thread.
919 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
921 pthread_mutex_lock(&data
->cond_mutex
);
924 * The state is set before signaling. It can be any value, it's the waiter
925 * job to correctly interpret this condition variable associated to the
926 * consumer pthread_cond.
928 * A value of 0 means that the corresponding thread of the consumer data
929 * was not started. 1 indicates that the thread has started and is ready
930 * for action. A negative value means that there was an error during the
933 data
->consumer_thread_is_ready
= state
;
934 (void) pthread_cond_signal(&data
->cond
);
936 pthread_mutex_unlock(&data
->cond_mutex
);
940 * This thread manage the consumer error sent back to the session daemon.
942 static void *thread_manage_consumer(void *data
)
944 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
945 uint32_t revents
, nb_fd
;
946 enum lttcomm_return_code code
;
947 struct lttng_poll_event events
;
948 struct consumer_data
*consumer_data
= data
;
949 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
951 DBG("[thread] Manage consumer started");
953 rcu_register_thread();
956 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
958 health_code_update();
961 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
962 * metadata_sock. Nothing more will be added to this poll set.
964 ret
= sessiond_set_thread_pollset(&events
, 3);
970 * The error socket here is already in a listening state which was done
971 * just before spawning this thread to avoid a race between the consumer
972 * daemon exec trying to connect and the listen() call.
974 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
979 health_code_update();
981 /* Infinite blocking call, waiting for transmission */
985 if (testpoint(sessiond_thread_manage_consumer
)) {
989 ret
= lttng_poll_wait(&events
, -1);
993 * Restart interrupted system call.
995 if (errno
== EINTR
) {
1003 for (i
= 0; i
< nb_fd
; i
++) {
1004 /* Fetch once the poll data */
1005 revents
= LTTNG_POLL_GETEV(&events
, i
);
1006 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1008 health_code_update();
1011 /* No activity for this FD (poll implementation). */
1015 /* Thread quit pipe has been closed. Killing thread. */
1016 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1022 /* Event on the registration socket */
1023 if (pollfd
== consumer_data
->err_sock
) {
1024 if (revents
& LPOLLIN
) {
1026 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1027 ERR("consumer err socket poll error");
1030 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1036 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1042 * Set the CLOEXEC flag. Return code is useless because either way, the
1045 (void) utils_set_fd_cloexec(sock
);
1047 health_code_update();
1049 DBG2("Receiving code from consumer err_sock");
1051 /* Getting status code from kconsumerd */
1052 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1053 sizeof(enum lttcomm_return_code
));
1058 health_code_update();
1059 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1060 ERR("consumer error when waiting for SOCK_READY : %s",
1061 lttcomm_get_readable_code(-code
));
1065 /* Connect both command and metadata sockets. */
1066 consumer_data
->cmd_sock
=
1067 lttcomm_connect_unix_sock(
1068 consumer_data
->cmd_unix_sock_path
);
1069 consumer_data
->metadata_fd
=
1070 lttcomm_connect_unix_sock(
1071 consumer_data
->cmd_unix_sock_path
);
1072 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1073 PERROR("consumer connect cmd socket");
1074 /* On error, signal condition and quit. */
1075 signal_consumer_condition(consumer_data
, -1);
1079 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1081 /* Create metadata socket lock. */
1082 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1083 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1084 PERROR("zmalloc pthread mutex");
1087 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1089 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1090 DBG("Consumer metadata socket ready (fd: %d)",
1091 consumer_data
->metadata_fd
);
1094 * Remove the consumerd error sock since we've established a connection.
1096 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1101 /* Add new accepted error socket. */
1102 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1107 /* Add metadata socket that is successfully connected. */
1108 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1109 LPOLLIN
| LPOLLRDHUP
);
1114 health_code_update();
1117 * Transfer the write-end of the channel monitoring and rotate pipe
1118 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1120 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1121 if (!cmd_socket_wrapper
) {
1124 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1126 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1127 consumer_data
->channel_monitor_pipe
);
1132 /* Discard the socket wrapper as it is no longer needed. */
1133 consumer_destroy_socket(cmd_socket_wrapper
);
1134 cmd_socket_wrapper
= NULL
;
1136 /* The thread is completely initialized, signal that it is ready. */
1137 signal_consumer_condition(consumer_data
, 1);
1139 /* Infinite blocking call, waiting for transmission */
1142 health_code_update();
1144 /* Exit the thread because the thread quit pipe has been triggered. */
1146 /* Not a health error. */
1151 health_poll_entry();
1152 ret
= lttng_poll_wait(&events
, -1);
1156 * Restart interrupted system call.
1158 if (errno
== EINTR
) {
1166 for (i
= 0; i
< nb_fd
; i
++) {
1167 /* Fetch once the poll data */
1168 revents
= LTTNG_POLL_GETEV(&events
, i
);
1169 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1171 health_code_update();
1174 /* No activity for this FD (poll implementation). */
1179 * Thread quit pipe has been triggered, flag that we should stop
1180 * but continue the current loop to handle potential data from
1183 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1185 if (pollfd
== sock
) {
1186 /* Event on the consumerd socket */
1187 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1188 && !(revents
& LPOLLIN
)) {
1189 ERR("consumer err socket second poll error");
1192 health_code_update();
1193 /* Wait for any kconsumerd error */
1194 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1195 sizeof(enum lttcomm_return_code
));
1197 ERR("consumer closed the command socket");
1201 ERR("consumer return code : %s",
1202 lttcomm_get_readable_code(-code
));
1205 } else if (pollfd
== consumer_data
->metadata_fd
) {
1206 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1207 && !(revents
& LPOLLIN
)) {
1208 ERR("consumer err metadata socket second poll error");
1211 /* UST metadata requests */
1212 ret
= ust_consumer_metadata_request(
1213 &consumer_data
->metadata_sock
);
1215 ERR("Handling metadata request");
1219 /* No need for an else branch all FDs are tested prior. */
1221 health_code_update();
1227 * We lock here because we are about to close the sockets and some other
1228 * thread might be using them so get exclusive access which will abort all
1229 * other consumer command by other threads.
1231 pthread_mutex_lock(&consumer_data
->lock
);
1233 /* Immediately set the consumerd state to stopped */
1234 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1235 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1236 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1237 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1238 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1240 /* Code flow error... */
1244 if (consumer_data
->err_sock
>= 0) {
1245 ret
= close(consumer_data
->err_sock
);
1249 consumer_data
->err_sock
= -1;
1251 if (consumer_data
->cmd_sock
>= 0) {
1252 ret
= close(consumer_data
->cmd_sock
);
1256 consumer_data
->cmd_sock
= -1;
1258 if (consumer_data
->metadata_sock
.fd_ptr
&&
1259 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1260 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1272 unlink(consumer_data
->err_unix_sock_path
);
1273 unlink(consumer_data
->cmd_unix_sock_path
);
1274 pthread_mutex_unlock(&consumer_data
->lock
);
1276 /* Cleanup metadata socket mutex. */
1277 if (consumer_data
->metadata_sock
.lock
) {
1278 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1279 free(consumer_data
->metadata_sock
.lock
);
1281 lttng_poll_clean(&events
);
1283 if (cmd_socket_wrapper
) {
1284 consumer_destroy_socket(cmd_socket_wrapper
);
1289 ERR("Health error occurred in %s", __func__
);
1291 health_unregister(health_sessiond
);
1292 DBG("consumer thread cleanup completed");
1294 rcu_thread_offline();
1295 rcu_unregister_thread();
1301 * This thread receives application command sockets (FDs) on the
1302 * apps_cmd_pipe and waits (polls) on them until they are closed
1303 * or an error occurs.
1305 * At that point, it flushes the data (tracing and metadata) associated
1306 * with this application and tears down ust app sessions and other
1307 * associated data structures through ust_app_unregister().
1309 * Note that this thread never sends commands to the applications
1310 * through the command sockets; it merely listens for hang-ups
1311 * and errors on those sockets and cleans-up as they occur.
1313 static void *thread_manage_apps(void *data
)
1315 int i
, ret
, pollfd
, err
= -1;
1317 uint32_t revents
, nb_fd
;
1318 struct lttng_poll_event events
;
1320 DBG("[thread] Manage application started");
1322 rcu_register_thread();
1323 rcu_thread_online();
1325 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1327 if (testpoint(sessiond_thread_manage_apps
)) {
1328 goto error_testpoint
;
1331 health_code_update();
1333 ret
= sessiond_set_thread_pollset(&events
, 2);
1335 goto error_poll_create
;
1338 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1343 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1347 health_code_update();
1350 DBG("Apps thread polling");
1352 /* Inifinite blocking call, waiting for transmission */
1354 health_poll_entry();
1355 ret
= lttng_poll_wait(&events
, -1);
1356 DBG("Apps thread return from poll on %d fds",
1357 LTTNG_POLL_GETNB(&events
));
1361 * Restart interrupted system call.
1363 if (errno
== EINTR
) {
1371 for (i
= 0; i
< nb_fd
; i
++) {
1372 /* Fetch once the poll data */
1373 revents
= LTTNG_POLL_GETEV(&events
, i
);
1374 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1376 health_code_update();
1379 /* No activity for this FD (poll implementation). */
1383 /* Thread quit pipe has been closed. Killing thread. */
1384 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1390 /* Inspect the apps cmd pipe */
1391 if (pollfd
== apps_cmd_pipe
[0]) {
1392 if (revents
& LPOLLIN
) {
1396 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1397 if (size_ret
< sizeof(sock
)) {
1398 PERROR("read apps cmd pipe");
1402 health_code_update();
1405 * Since this is a command socket (write then read),
1406 * we only monitor the error events of the socket.
1408 ret
= lttng_poll_add(&events
, sock
,
1409 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1414 DBG("Apps with sock %d added to poll set", sock
);
1415 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1416 ERR("Apps command pipe error");
1419 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1424 * At this point, we know that a registered application made
1425 * the event at poll_wait.
1427 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1428 /* Removing from the poll set */
1429 ret
= lttng_poll_del(&events
, pollfd
);
1434 /* Socket closed on remote end. */
1435 ust_app_unregister(pollfd
);
1437 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1442 health_code_update();
1448 lttng_poll_clean(&events
);
1451 utils_close_pipe(apps_cmd_pipe
);
1452 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1455 * We don't clean the UST app hash table here since already registered
1456 * applications can still be controlled so let them be until the session
1457 * daemon dies or the applications stop.
1462 ERR("Health error occurred in %s", __func__
);
1464 health_unregister(health_sessiond
);
1465 DBG("Application communication apps thread cleanup complete");
1466 rcu_thread_offline();
1467 rcu_unregister_thread();
1472 * Send a socket to a thread This is called from the dispatch UST registration
1473 * thread once all sockets are set for the application.
1475 * The sock value can be invalid, we don't really care, the thread will handle
1476 * it and make the necessary cleanup if so.
1478 * On success, return 0 else a negative value being the errno message of the
1481 static int send_socket_to_thread(int fd
, int sock
)
1486 * It's possible that the FD is set as invalid with -1 concurrently just
1487 * before calling this function being a shutdown state of the thread.
1494 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1495 if (ret
< sizeof(sock
)) {
1496 PERROR("write apps pipe %d", fd
);
1503 /* All good. Don't send back the write positive ret value. */
1510 * Sanitize the wait queue of the dispatch registration thread meaning removing
1511 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1512 * notify socket is never received.
1514 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1516 int ret
, nb_fd
= 0, i
;
1517 unsigned int fd_added
= 0;
1518 struct lttng_poll_event events
;
1519 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1523 lttng_poll_init(&events
);
1525 /* Just skip everything for an empty queue. */
1526 if (!wait_queue
->count
) {
1530 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1535 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1536 &wait_queue
->head
, head
) {
1537 assert(wait_node
->app
);
1538 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1539 LPOLLHUP
| LPOLLERR
);
1552 * Poll but don't block so we can quickly identify the faulty events and
1553 * clean them afterwards from the wait queue.
1555 ret
= lttng_poll_wait(&events
, 0);
1561 for (i
= 0; i
< nb_fd
; i
++) {
1562 /* Get faulty FD. */
1563 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1564 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1567 /* No activity for this FD (poll implementation). */
1571 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1572 &wait_queue
->head
, head
) {
1573 if (pollfd
== wait_node
->app
->sock
&&
1574 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1575 cds_list_del(&wait_node
->head
);
1576 wait_queue
->count
--;
1577 ust_app_destroy(wait_node
->app
);
1580 * Silence warning of use-after-free in
1581 * cds_list_for_each_entry_safe which uses
1582 * __typeof__(*wait_node).
1587 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1594 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1598 lttng_poll_clean(&events
);
1602 lttng_poll_clean(&events
);
1604 ERR("Unable to sanitize wait queue");
1609 * Dispatch request from the registration threads to the application
1610 * communication thread.
1612 static void *thread_dispatch_ust_registration(void *data
)
1615 struct cds_wfcq_node
*node
;
1616 struct ust_command
*ust_cmd
= NULL
;
1617 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1618 struct ust_reg_wait_queue wait_queue
= {
1622 rcu_register_thread();
1624 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1626 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1627 goto error_testpoint
;
1630 health_code_update();
1632 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1634 DBG("[thread] Dispatch UST command started");
1637 health_code_update();
1639 /* Atomically prepare the queue futex */
1640 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1642 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1647 struct ust_app
*app
= NULL
;
1651 * Make sure we don't have node(s) that have hung up before receiving
1652 * the notify socket. This is to clean the list in order to avoid
1653 * memory leaks from notify socket that are never seen.
1655 sanitize_wait_queue(&wait_queue
);
1657 health_code_update();
1658 /* Dequeue command for registration */
1659 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1661 DBG("Woken up but nothing in the UST command queue");
1662 /* Continue thread execution */
1666 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1668 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1669 " gid:%d sock:%d name:%s (version %d.%d)",
1670 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1671 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1672 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1673 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1675 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1676 wait_node
= zmalloc(sizeof(*wait_node
));
1678 PERROR("zmalloc wait_node dispatch");
1679 ret
= close(ust_cmd
->sock
);
1681 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1683 lttng_fd_put(LTTNG_FD_APPS
, 1);
1687 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1689 /* Create application object if socket is CMD. */
1690 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1692 if (!wait_node
->app
) {
1693 ret
= close(ust_cmd
->sock
);
1695 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1697 lttng_fd_put(LTTNG_FD_APPS
, 1);
1703 * Add application to the wait queue so we can set the notify
1704 * socket before putting this object in the global ht.
1706 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1711 * We have to continue here since we don't have the notify
1712 * socket and the application MUST be added to the hash table
1713 * only at that moment.
1718 * Look for the application in the local wait queue and set the
1719 * notify socket if found.
1721 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1722 &wait_queue
.head
, head
) {
1723 health_code_update();
1724 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1725 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1726 cds_list_del(&wait_node
->head
);
1728 app
= wait_node
->app
;
1730 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1736 * With no application at this stage the received socket is
1737 * basically useless so close it before we free the cmd data
1738 * structure for good.
1741 ret
= close(ust_cmd
->sock
);
1743 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1745 lttng_fd_put(LTTNG_FD_APPS
, 1);
1752 * @session_lock_list
1754 * Lock the global session list so from the register up to the
1755 * registration done message, no thread can see the application
1756 * and change its state.
1758 session_lock_list();
1762 * Add application to the global hash table. This needs to be
1763 * done before the update to the UST registry can locate the
1768 /* Set app version. This call will print an error if needed. */
1769 (void) ust_app_version(app
);
1771 /* Send notify socket through the notify pipe. */
1772 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1776 session_unlock_list();
1778 * No notify thread, stop the UST tracing. However, this is
1779 * not an internal error of the this thread thus setting
1780 * the health error code to a normal exit.
1787 * Update newly registered application with the tracing
1788 * registry info already enabled information.
1790 update_ust_app(app
->sock
);
1793 * Don't care about return value. Let the manage apps threads
1794 * handle app unregistration upon socket close.
1796 (void) ust_app_register_done(app
);
1799 * Even if the application socket has been closed, send the app
1800 * to the thread and unregistration will take place at that
1803 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1806 session_unlock_list();
1808 * No apps. thread, stop the UST tracing. However, this is
1809 * not an internal error of the this thread thus setting
1810 * the health error code to a normal exit.
1817 session_unlock_list();
1819 } while (node
!= NULL
);
1821 health_poll_entry();
1822 /* Futex wait on queue. Blocking call on futex() */
1823 futex_nto1_wait(&ust_cmd_queue
.futex
);
1826 /* Normal exit, no error */
1830 /* Clean up wait queue. */
1831 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1832 &wait_queue
.head
, head
) {
1833 cds_list_del(&wait_node
->head
);
1838 /* Empty command queue. */
1840 /* Dequeue command for registration */
1841 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1845 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1846 ret
= close(ust_cmd
->sock
);
1848 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
1850 lttng_fd_put(LTTNG_FD_APPS
, 1);
1855 DBG("Dispatch thread dying");
1858 ERR("Health error occurred in %s", __func__
);
1860 health_unregister(health_sessiond
);
1861 rcu_unregister_thread();
1866 * This thread manage application registration.
1868 static void *thread_registration_apps(void *data
)
1870 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1871 uint32_t revents
, nb_fd
;
1872 struct lttng_poll_event events
;
1874 * Get allocated in this thread, enqueued to a global queue, dequeued and
1875 * freed in the manage apps thread.
1877 struct ust_command
*ust_cmd
= NULL
;
1879 DBG("[thread] Manage application registration started");
1881 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1883 if (testpoint(sessiond_thread_registration_apps
)) {
1884 goto error_testpoint
;
1887 ret
= lttcomm_listen_unix_sock(apps_sock
);
1893 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1894 * more will be added to this poll set.
1896 ret
= sessiond_set_thread_pollset(&events
, 2);
1898 goto error_create_poll
;
1901 /* Add the application registration socket */
1902 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1904 goto error_poll_add
;
1907 /* Notify all applications to register */
1908 ret
= notify_ust_apps(1);
1910 ERR("Failed to notify applications or create the wait shared memory.\n"
1911 "Execution continues but there might be problem for already\n"
1912 "running applications that wishes to register.");
1916 DBG("Accepting application registration");
1918 /* Inifinite blocking call, waiting for transmission */
1920 health_poll_entry();
1921 ret
= lttng_poll_wait(&events
, -1);
1925 * Restart interrupted system call.
1927 if (errno
== EINTR
) {
1935 for (i
= 0; i
< nb_fd
; i
++) {
1936 health_code_update();
1938 /* Fetch once the poll data */
1939 revents
= LTTNG_POLL_GETEV(&events
, i
);
1940 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1943 /* No activity for this FD (poll implementation). */
1947 /* Thread quit pipe has been closed. Killing thread. */
1948 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1954 /* Event on the registration socket */
1955 if (pollfd
== apps_sock
) {
1956 if (revents
& LPOLLIN
) {
1957 sock
= lttcomm_accept_unix_sock(apps_sock
);
1963 * Set socket timeout for both receiving and ending.
1964 * app_socket_timeout is in seconds, whereas
1965 * lttcomm_setsockopt_rcv_timeout and
1966 * lttcomm_setsockopt_snd_timeout expect msec as
1969 if (config
.app_socket_timeout
>= 0) {
1970 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1971 config
.app_socket_timeout
* 1000);
1972 (void) lttcomm_setsockopt_snd_timeout(sock
,
1973 config
.app_socket_timeout
* 1000);
1977 * Set the CLOEXEC flag. Return code is useless because
1978 * either way, the show must go on.
1980 (void) utils_set_fd_cloexec(sock
);
1982 /* Create UST registration command for enqueuing */
1983 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1984 if (ust_cmd
== NULL
) {
1985 PERROR("ust command zmalloc");
1994 * Using message-based transmissions to ensure we don't
1995 * have to deal with partially received messages.
1997 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1999 ERR("Exhausted file descriptors allowed for applications.");
2009 health_code_update();
2010 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2013 /* Close socket of the application. */
2018 lttng_fd_put(LTTNG_FD_APPS
, 1);
2022 health_code_update();
2024 ust_cmd
->sock
= sock
;
2027 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2028 " gid:%d sock:%d name:%s (version %d.%d)",
2029 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2030 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2031 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2032 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2035 * Lock free enqueue the registration request. The red pill
2036 * has been taken! This apps will be part of the *system*.
2038 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2041 * Wake the registration queue futex. Implicit memory
2042 * barrier with the exchange in cds_wfcq_enqueue.
2044 futex_nto1_wake(&ust_cmd_queue
.futex
);
2045 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2046 ERR("Register apps socket poll error");
2049 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2058 /* Notify that the registration thread is gone */
2061 if (apps_sock
>= 0) {
2062 ret
= close(apps_sock
);
2072 lttng_fd_put(LTTNG_FD_APPS
, 1);
2074 unlink(config
.apps_unix_sock_path
.value
);
2077 lttng_poll_clean(&events
);
2081 DBG("UST Registration thread cleanup complete");
2084 ERR("Health error occurred in %s", __func__
);
2086 health_unregister(health_sessiond
);
2092 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2093 * exec or it will fails.
2095 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2098 struct timespec timeout
;
2101 * Make sure we set the readiness flag to 0 because we are NOT ready.
2102 * This access to consumer_thread_is_ready does not need to be
2103 * protected by consumer_data.cond_mutex (yet) since the consumer
2104 * management thread has not been started at this point.
2106 consumer_data
->consumer_thread_is_ready
= 0;
2108 /* Setup pthread condition */
2109 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2112 PERROR("pthread_condattr_init consumer data");
2117 * Set the monotonic clock in order to make sure we DO NOT jump in time
2118 * between the clock_gettime() call and the timedwait call. See bug #324
2119 * for a more details and how we noticed it.
2121 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2124 PERROR("pthread_condattr_setclock consumer data");
2128 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2131 PERROR("pthread_cond_init consumer data");
2135 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2136 thread_manage_consumer
, consumer_data
);
2139 PERROR("pthread_create consumer");
2144 /* We are about to wait on a pthread condition */
2145 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2147 /* Get time for sem_timedwait absolute timeout */
2148 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2150 * Set the timeout for the condition timed wait even if the clock gettime
2151 * call fails since we might loop on that call and we want to avoid to
2152 * increment the timeout too many times.
2154 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2157 * The following loop COULD be skipped in some conditions so this is why we
2158 * set ret to 0 in order to make sure at least one round of the loop is
2164 * Loop until the condition is reached or when a timeout is reached. Note
2165 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2166 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2167 * possible. This loop does not take any chances and works with both of
2170 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2171 if (clock_ret
< 0) {
2172 PERROR("clock_gettime spawn consumer");
2173 /* Infinite wait for the consumerd thread to be ready */
2174 ret
= pthread_cond_wait(&consumer_data
->cond
,
2175 &consumer_data
->cond_mutex
);
2177 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2178 &consumer_data
->cond_mutex
, &timeout
);
2182 /* Release the pthread condition */
2183 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2187 if (ret
== ETIMEDOUT
) {
2191 * Call has timed out so we kill the kconsumerd_thread and return
2194 ERR("Condition timed out. The consumer thread was never ready."
2196 pth_ret
= pthread_cancel(consumer_data
->thread
);
2198 PERROR("pthread_cancel consumer thread");
2201 PERROR("pthread_cond_wait failed consumer thread");
2203 /* Caller is expecting a negative value on failure. */
2208 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2209 if (consumer_data
->pid
== 0) {
2210 ERR("Consumerd did not start");
2211 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2214 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2223 * Join consumer thread
2225 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2229 /* Consumer pid must be a real one. */
2230 if (consumer_data
->pid
> 0) {
2232 ret
= kill(consumer_data
->pid
, SIGTERM
);
2234 PERROR("Error killing consumer daemon");
2237 return pthread_join(consumer_data
->thread
, &status
);
2244 * Fork and exec a consumer daemon (consumerd).
2246 * Return pid if successful else -1.
2248 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2252 const char *consumer_to_use
;
2253 const char *verbosity
;
2256 DBG("Spawning consumerd");
2263 if (config
.verbose_consumer
) {
2264 verbosity
= "--verbose";
2265 } else if (lttng_opt_quiet
) {
2266 verbosity
= "--quiet";
2271 switch (consumer_data
->type
) {
2272 case LTTNG_CONSUMER_KERNEL
:
2274 * Find out which consumerd to execute. We will first try the
2275 * 64-bit path, then the sessiond's installation directory, and
2276 * fallback on the 32-bit one,
2278 DBG3("Looking for a kernel consumer at these locations:");
2279 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2280 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2281 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2282 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2283 DBG3("Found location #1");
2284 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2285 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2286 DBG3("Found location #2");
2287 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2288 } else if (config
.consumerd32_bin_path
.value
&&
2289 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2290 DBG3("Found location #3");
2291 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2293 DBG("Could not find any valid consumerd executable");
2297 DBG("Using kernel consumer at: %s", consumer_to_use
);
2298 (void) execl(consumer_to_use
,
2299 "lttng-consumerd", verbosity
, "-k",
2300 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2301 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2302 "--group", config
.tracing_group_name
.value
,
2305 case LTTNG_CONSUMER64_UST
:
2307 if (config
.consumerd64_lib_dir
.value
) {
2312 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2316 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2317 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2322 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2323 if (tmp
[0] != '\0') {
2324 strcat(tmpnew
, ":");
2325 strcat(tmpnew
, tmp
);
2327 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2334 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2335 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2336 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2337 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2338 "--group", config
.tracing_group_name
.value
,
2342 case LTTNG_CONSUMER32_UST
:
2344 if (config
.consumerd32_lib_dir
.value
) {
2349 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2353 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2354 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2359 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2360 if (tmp
[0] != '\0') {
2361 strcat(tmpnew
, ":");
2362 strcat(tmpnew
, tmp
);
2364 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2371 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2372 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2373 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2374 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2375 "--group", config
.tracing_group_name
.value
,
2380 ERR("unknown consumer type");
2384 PERROR("Consumer execl()");
2386 /* Reaching this point, we got a failure on our execl(). */
2388 } else if (pid
> 0) {
2391 PERROR("start consumer fork");
2399 * Spawn the consumerd daemon and session daemon thread.
2401 static int start_consumerd(struct consumer_data
*consumer_data
)
2406 * Set the listen() state on the socket since there is a possible race
2407 * between the exec() of the consumer daemon and this call if place in the
2408 * consumer thread. See bug #366 for more details.
2410 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2415 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2416 if (consumer_data
->pid
!= 0) {
2417 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2421 ret
= spawn_consumerd(consumer_data
);
2423 ERR("Spawning consumerd failed");
2424 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2428 /* Setting up the consumer_data pid */
2429 consumer_data
->pid
= ret
;
2430 DBG2("Consumer pid %d", consumer_data
->pid
);
2431 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2433 DBG2("Spawning consumer control thread");
2434 ret
= spawn_consumer_thread(consumer_data
);
2436 ERR("Fatal error spawning consumer control thread");
2444 /* Cleanup already created sockets on error. */
2445 if (consumer_data
->err_sock
>= 0) {
2448 err
= close(consumer_data
->err_sock
);
2450 PERROR("close consumer data error socket");
2457 * Setup necessary data for kernel tracer action.
2459 static int init_kernel_tracer(void)
2463 /* Modprobe lttng kernel modules */
2464 ret
= modprobe_lttng_control();
2469 /* Open debugfs lttng */
2470 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2471 if (kernel_tracer_fd
< 0) {
2472 DBG("Failed to open %s", module_proc_lttng
);
2476 /* Validate kernel version */
2477 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2478 &kernel_tracer_abi_version
);
2483 ret
= modprobe_lttng_data();
2488 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2495 WARN("Kernel tracer does not support buffer monitoring. "
2496 "The monitoring timer of channels in the kernel domain "
2497 "will be set to 0 (disabled).");
2500 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2504 modprobe_remove_lttng_control();
2505 ret
= close(kernel_tracer_fd
);
2509 kernel_tracer_fd
= -1;
2510 return LTTNG_ERR_KERN_VERSION
;
2513 ret
= close(kernel_tracer_fd
);
2519 modprobe_remove_lttng_control();
2522 WARN("No kernel tracer available");
2523 kernel_tracer_fd
= -1;
2525 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2527 return LTTNG_ERR_KERN_NA
;
2533 * Copy consumer output from the tracing session to the domain session. The
2534 * function also applies the right modification on a per domain basis for the
2535 * trace files destination directory.
2537 * Should *NOT* be called with RCU read-side lock held.
2539 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2542 const char *dir_name
;
2543 struct consumer_output
*consumer
;
2546 assert(session
->consumer
);
2549 case LTTNG_DOMAIN_KERNEL
:
2550 DBG3("Copying tracing session consumer output in kernel session");
2552 * XXX: We should audit the session creation and what this function
2553 * does "extra" in order to avoid a destroy since this function is used
2554 * in the domain session creation (kernel and ust) only. Same for UST
2557 if (session
->kernel_session
->consumer
) {
2558 consumer_output_put(session
->kernel_session
->consumer
);
2560 session
->kernel_session
->consumer
=
2561 consumer_copy_output(session
->consumer
);
2562 /* Ease our life a bit for the next part */
2563 consumer
= session
->kernel_session
->consumer
;
2564 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2566 case LTTNG_DOMAIN_JUL
:
2567 case LTTNG_DOMAIN_LOG4J
:
2568 case LTTNG_DOMAIN_PYTHON
:
2569 case LTTNG_DOMAIN_UST
:
2570 DBG3("Copying tracing session consumer output in UST session");
2571 if (session
->ust_session
->consumer
) {
2572 consumer_output_put(session
->ust_session
->consumer
);
2574 session
->ust_session
->consumer
=
2575 consumer_copy_output(session
->consumer
);
2576 /* Ease our life a bit for the next part */
2577 consumer
= session
->ust_session
->consumer
;
2578 dir_name
= DEFAULT_UST_TRACE_DIR
;
2581 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2585 /* Append correct directory to subdir */
2586 strncat(consumer
->subdir
, dir_name
,
2587 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2588 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2597 * Create an UST session and add it to the session ust list.
2599 * Should *NOT* be called with RCU read-side lock held.
2601 static int create_ust_session(struct ltt_session
*session
,
2602 struct lttng_domain
*domain
)
2605 struct ltt_ust_session
*lus
= NULL
;
2609 assert(session
->consumer
);
2611 switch (domain
->type
) {
2612 case LTTNG_DOMAIN_JUL
:
2613 case LTTNG_DOMAIN_LOG4J
:
2614 case LTTNG_DOMAIN_PYTHON
:
2615 case LTTNG_DOMAIN_UST
:
2618 ERR("Unknown UST domain on create session %d", domain
->type
);
2619 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2623 DBG("Creating UST session");
2625 lus
= trace_ust_create_session(session
->id
);
2627 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2631 lus
->uid
= session
->uid
;
2632 lus
->gid
= session
->gid
;
2633 lus
->output_traces
= session
->output_traces
;
2634 lus
->snapshot_mode
= session
->snapshot_mode
;
2635 lus
->live_timer_interval
= session
->live_timer
;
2636 session
->ust_session
= lus
;
2637 if (session
->shm_path
[0]) {
2638 strncpy(lus
->root_shm_path
, session
->shm_path
,
2639 sizeof(lus
->root_shm_path
));
2640 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2641 strncpy(lus
->shm_path
, session
->shm_path
,
2642 sizeof(lus
->shm_path
));
2643 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2644 strncat(lus
->shm_path
, "/ust",
2645 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2647 /* Copy session output to the newly created UST session */
2648 ret
= copy_session_consumer(domain
->type
, session
);
2649 if (ret
!= LTTNG_OK
) {
2657 session
->ust_session
= NULL
;
2662 * Create a kernel tracer session then create the default channel.
2664 static int create_kernel_session(struct ltt_session
*session
)
2668 DBG("Creating kernel session");
2670 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2672 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2676 /* Code flow safety */
2677 assert(session
->kernel_session
);
2679 /* Copy session output to the newly created Kernel session */
2680 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2681 if (ret
!= LTTNG_OK
) {
2685 session
->kernel_session
->uid
= session
->uid
;
2686 session
->kernel_session
->gid
= session
->gid
;
2687 session
->kernel_session
->output_traces
= session
->output_traces
;
2688 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2693 trace_kernel_destroy_session(session
->kernel_session
);
2694 session
->kernel_session
= NULL
;
2699 * Count number of session permitted by uid/gid.
2701 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2704 struct ltt_session
*session
;
2705 const struct ltt_session_list
*session_list
= session_get_list();
2707 DBG("Counting number of available session for UID %d GID %d",
2709 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
2710 if (!session_get(session
)) {
2713 session_lock(session
);
2714 /* Only count the sessions the user can control. */
2715 if (session_access_ok(session
, uid
, gid
) &&
2716 !session
->destroyed
) {
2719 session_unlock(session
);
2720 session_put(session
);
2725 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2726 int *sock_error
, struct lttng_event
*event
)
2729 struct lttng_userspace_probe_location
*probe_location
;
2730 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2731 struct lttng_dynamic_buffer probe_location_buffer
;
2732 struct lttng_buffer_view buffer_view
;
2735 * Create a buffer to store the serialized version of the probe
2738 lttng_dynamic_buffer_init(&probe_location_buffer
);
2739 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2740 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2742 ret
= LTTNG_ERR_NOMEM
;
2747 * Receive the probe location.
2749 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2750 probe_location_buffer
.size
);
2752 DBG("Nothing recv() from client var len data... continuing");
2754 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2755 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2759 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2760 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2763 * Extract the probe location from the serialized version.
2765 ret
= lttng_userspace_probe_location_create_from_buffer(
2766 &buffer_view
, &probe_location
);
2768 WARN("Failed to create a userspace probe location from the received buffer");
2769 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2770 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2775 * Receive the file descriptor to the target binary from the client.
2777 DBG("Receiving userspace probe target FD from client ...");
2778 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2780 DBG("Nothing recv() from client userspace probe fd... continuing");
2782 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2787 * Set the file descriptor received from the client through the unix
2788 * socket in the probe location.
2790 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
2792 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2797 * From the kernel tracer's perspective, all userspace probe event types
2798 * are all the same: a file and an offset.
2800 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
2801 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
2802 ret
= lttng_userspace_probe_location_function_set_binary_fd(
2803 probe_location
, fd
);
2805 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
2806 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
2807 probe_location
, fd
);
2810 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2815 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2819 /* Attach the probe location to the event. */
2820 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
2822 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2826 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2832 * Check if the current kernel tracer supports the session rotation feature.
2833 * Return 1 if it does, 0 otherwise.
2835 static int check_rotate_compatible(void)
2839 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2840 DBG("Kernel tracer version is not compatible with the rotation feature");
2848 * Process the command requested by the lttng client within the command
2849 * context structure. This function make sure that the return structure (llm)
2850 * is set and ready for transmission before returning.
2852 * Return any error encountered or 0 for success.
2854 * "sock" is only used for special-case var. len data.
2856 * Should *NOT* be called with RCU read-side lock held.
2858 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2862 int need_tracing_session
= 1;
2865 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2867 assert(!rcu_read_ongoing());
2871 switch (cmd_ctx
->lsm
->cmd_type
) {
2872 case LTTNG_CREATE_SESSION
:
2873 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2874 case LTTNG_CREATE_SESSION_LIVE
:
2875 case LTTNG_DESTROY_SESSION
:
2876 case LTTNG_LIST_SESSIONS
:
2877 case LTTNG_LIST_DOMAINS
:
2878 case LTTNG_START_TRACE
:
2879 case LTTNG_STOP_TRACE
:
2880 case LTTNG_DATA_PENDING
:
2881 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2882 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2883 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2884 case LTTNG_SNAPSHOT_RECORD
:
2885 case LTTNG_SAVE_SESSION
:
2886 case LTTNG_SET_SESSION_SHM_PATH
:
2887 case LTTNG_REGENERATE_METADATA
:
2888 case LTTNG_REGENERATE_STATEDUMP
:
2889 case LTTNG_REGISTER_TRIGGER
:
2890 case LTTNG_UNREGISTER_TRIGGER
:
2891 case LTTNG_ROTATE_SESSION
:
2892 case LTTNG_ROTATION_GET_INFO
:
2893 case LTTNG_ROTATION_SET_SCHEDULE
:
2894 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2901 if (config
.no_kernel
&& need_domain
2902 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2904 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2906 ret
= LTTNG_ERR_KERN_NA
;
2911 /* Deny register consumer if we already have a spawned consumer. */
2912 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2913 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2914 if (kconsumer_data
.pid
> 0) {
2915 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2916 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2919 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2923 * Check for command that don't needs to allocate a returned payload. We do
2924 * this here so we don't have to make the call for no payload at each
2927 switch(cmd_ctx
->lsm
->cmd_type
) {
2928 case LTTNG_LIST_SESSIONS
:
2929 case LTTNG_LIST_TRACEPOINTS
:
2930 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2931 case LTTNG_LIST_DOMAINS
:
2932 case LTTNG_LIST_CHANNELS
:
2933 case LTTNG_LIST_EVENTS
:
2934 case LTTNG_LIST_SYSCALLS
:
2935 case LTTNG_LIST_TRACKER_PIDS
:
2936 case LTTNG_DATA_PENDING
:
2937 case LTTNG_ROTATE_SESSION
:
2938 case LTTNG_ROTATION_GET_INFO
:
2939 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2942 /* Setup lttng message with no payload */
2943 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2945 /* This label does not try to unlock the session */
2946 goto init_setup_error
;
2950 /* Commands that DO NOT need a session. */
2951 switch (cmd_ctx
->lsm
->cmd_type
) {
2952 case LTTNG_CREATE_SESSION
:
2953 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2954 case LTTNG_CREATE_SESSION_LIVE
:
2955 case LTTNG_LIST_SESSIONS
:
2956 case LTTNG_LIST_TRACEPOINTS
:
2957 case LTTNG_LIST_SYSCALLS
:
2958 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2959 case LTTNG_SAVE_SESSION
:
2960 case LTTNG_REGISTER_TRIGGER
:
2961 case LTTNG_UNREGISTER_TRIGGER
:
2962 need_tracing_session
= 0;
2965 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2967 * We keep the session list lock across _all_ commands
2968 * for now, because the per-session lock does not
2969 * handle teardown properly.
2971 session_lock_list();
2972 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2973 if (cmd_ctx
->session
== NULL
) {
2974 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2977 /* Acquire lock for the session */
2978 session_lock(cmd_ctx
->session
);
2984 * Commands that need a valid session but should NOT create one if none
2985 * exists. Instead of creating one and destroying it when the command is
2986 * handled, process that right before so we save some round trip in useless
2989 switch (cmd_ctx
->lsm
->cmd_type
) {
2990 case LTTNG_DISABLE_CHANNEL
:
2991 case LTTNG_DISABLE_EVENT
:
2992 switch (cmd_ctx
->lsm
->domain
.type
) {
2993 case LTTNG_DOMAIN_KERNEL
:
2994 if (!cmd_ctx
->session
->kernel_session
) {
2995 ret
= LTTNG_ERR_NO_CHANNEL
;
2999 case LTTNG_DOMAIN_JUL
:
3000 case LTTNG_DOMAIN_LOG4J
:
3001 case LTTNG_DOMAIN_PYTHON
:
3002 case LTTNG_DOMAIN_UST
:
3003 if (!cmd_ctx
->session
->ust_session
) {
3004 ret
= LTTNG_ERR_NO_CHANNEL
;
3009 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3021 * Check domain type for specific "pre-action".
3023 switch (cmd_ctx
->lsm
->domain
.type
) {
3024 case LTTNG_DOMAIN_KERNEL
:
3026 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3030 /* Kernel tracer check */
3031 if (kernel_tracer_fd
== -1) {
3032 /* Basically, load kernel tracer modules */
3033 ret
= init_kernel_tracer();
3039 /* Consumer is in an ERROR state. Report back to client */
3040 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3041 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3045 /* Need a session for kernel command */
3046 if (need_tracing_session
) {
3047 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3048 ret
= create_kernel_session(cmd_ctx
->session
);
3050 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3055 /* Start the kernel consumer daemon */
3056 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3057 if (kconsumer_data
.pid
== 0 &&
3058 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3059 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3060 ret
= start_consumerd(&kconsumer_data
);
3062 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3065 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3067 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3071 * The consumer was just spawned so we need to add the socket to
3072 * the consumer output of the session if exist.
3074 ret
= consumer_create_socket(&kconsumer_data
,
3075 cmd_ctx
->session
->kernel_session
->consumer
);
3082 case LTTNG_DOMAIN_JUL
:
3083 case LTTNG_DOMAIN_LOG4J
:
3084 case LTTNG_DOMAIN_PYTHON
:
3085 case LTTNG_DOMAIN_UST
:
3087 if (!ust_app_supported()) {
3088 ret
= LTTNG_ERR_NO_UST
;
3091 /* Consumer is in an ERROR state. Report back to client */
3092 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3093 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3097 if (need_tracing_session
) {
3098 /* Create UST session if none exist. */
3099 if (cmd_ctx
->session
->ust_session
== NULL
) {
3100 ret
= create_ust_session(cmd_ctx
->session
,
3101 &cmd_ctx
->lsm
->domain
);
3102 if (ret
!= LTTNG_OK
) {
3107 /* Start the UST consumer daemons */
3109 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3110 if (config
.consumerd64_bin_path
.value
&&
3111 ustconsumer64_data
.pid
== 0 &&
3112 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3113 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3114 ret
= start_consumerd(&ustconsumer64_data
);
3116 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3117 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3121 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3122 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3124 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3128 * Setup socket for consumer 64 bit. No need for atomic access
3129 * since it was set above and can ONLY be set in this thread.
3131 ret
= consumer_create_socket(&ustconsumer64_data
,
3132 cmd_ctx
->session
->ust_session
->consumer
);
3138 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3139 if (config
.consumerd32_bin_path
.value
&&
3140 ustconsumer32_data
.pid
== 0 &&
3141 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3142 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3143 ret
= start_consumerd(&ustconsumer32_data
);
3145 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3146 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3150 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3151 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3153 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3157 * Setup socket for consumer 32 bit. No need for atomic access
3158 * since it was set above and can ONLY be set in this thread.
3160 ret
= consumer_create_socket(&ustconsumer32_data
,
3161 cmd_ctx
->session
->ust_session
->consumer
);
3173 /* Validate consumer daemon state when start/stop trace command */
3174 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3175 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3176 switch (cmd_ctx
->lsm
->domain
.type
) {
3177 case LTTNG_DOMAIN_NONE
:
3179 case LTTNG_DOMAIN_JUL
:
3180 case LTTNG_DOMAIN_LOG4J
:
3181 case LTTNG_DOMAIN_PYTHON
:
3182 case LTTNG_DOMAIN_UST
:
3183 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3184 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3188 case LTTNG_DOMAIN_KERNEL
:
3189 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3190 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3195 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3201 * Check that the UID or GID match that of the tracing session.
3202 * The root user can interact with all sessions.
3204 if (need_tracing_session
) {
3205 if (!session_access_ok(cmd_ctx
->session
,
3206 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3207 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
3208 cmd_ctx
->session
->destroyed
) {
3209 ret
= LTTNG_ERR_EPERM
;
3215 * Send relayd information to consumer as soon as we have a domain and a
3218 if (cmd_ctx
->session
&& need_domain
) {
3220 * Setup relayd if not done yet. If the relayd information was already
3221 * sent to the consumer, this call will gracefully return.
3223 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3224 if (ret
!= LTTNG_OK
) {
3229 /* Process by command type */
3230 switch (cmd_ctx
->lsm
->cmd_type
) {
3231 case LTTNG_ADD_CONTEXT
:
3234 * An LTTNG_ADD_CONTEXT command might have a supplementary
3235 * payload if the context being added is an application context.
3237 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3238 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3239 char *provider_name
= NULL
, *context_name
= NULL
;
3240 size_t provider_name_len
=
3241 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3242 size_t context_name_len
=
3243 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3245 if (provider_name_len
== 0 || context_name_len
== 0) {
3247 * Application provider and context names MUST
3250 ret
= -LTTNG_ERR_INVALID
;
3254 provider_name
= zmalloc(provider_name_len
+ 1);
3255 if (!provider_name
) {
3256 ret
= -LTTNG_ERR_NOMEM
;
3259 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3262 context_name
= zmalloc(context_name_len
+ 1);
3263 if (!context_name
) {
3264 ret
= -LTTNG_ERR_NOMEM
;
3265 goto error_add_context
;
3267 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3270 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3273 goto error_add_context
;
3276 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3279 goto error_add_context
;
3284 * cmd_add_context assumes ownership of the provider and context
3287 ret
= cmd_add_context(cmd_ctx
->session
,
3288 cmd_ctx
->lsm
->domain
.type
,
3289 cmd_ctx
->lsm
->u
.context
.channel_name
,
3290 &cmd_ctx
->lsm
->u
.context
.ctx
,
3291 kernel_poll_pipe
[1]);
3293 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3294 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3296 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3297 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3303 case LTTNG_DISABLE_CHANNEL
:
3305 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3306 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3309 case LTTNG_DISABLE_EVENT
:
3313 * FIXME: handle filter; for now we just receive the filter's
3314 * bytecode along with the filter expression which are sent by
3315 * liblttng-ctl and discard them.
3317 * This fixes an issue where the client may block while sending
3318 * the filter payload and encounter an error because the session
3319 * daemon closes the socket without ever handling this data.
3321 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3322 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3325 char data
[LTTNG_FILTER_MAX_LEN
];
3327 DBG("Discarding disable event command payload of size %zu", count
);
3329 ret
= lttcomm_recv_unix_sock(sock
, data
,
3330 count
> sizeof(data
) ? sizeof(data
) : count
);
3335 count
-= (size_t) ret
;
3338 /* FIXME: passing packed structure to non-packed pointer */
3339 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3340 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3341 &cmd_ctx
->lsm
->u
.disable
.event
);
3344 case LTTNG_ENABLE_CHANNEL
:
3346 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3347 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3348 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3349 &cmd_ctx
->lsm
->u
.channel
.chan
,
3350 kernel_poll_pipe
[1]);
3353 case LTTNG_TRACK_PID
:
3355 ret
= cmd_track_pid(cmd_ctx
->session
,
3356 cmd_ctx
->lsm
->domain
.type
,
3357 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3360 case LTTNG_UNTRACK_PID
:
3362 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3363 cmd_ctx
->lsm
->domain
.type
,
3364 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3367 case LTTNG_ENABLE_EVENT
:
3369 struct lttng_event
*ev
= NULL
;
3370 struct lttng_event_exclusion
*exclusion
= NULL
;
3371 struct lttng_filter_bytecode
*bytecode
= NULL
;
3372 char *filter_expression
= NULL
;
3374 /* Handle exclusion events and receive it from the client. */
3375 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3376 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3378 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3379 (count
* LTTNG_SYMBOL_NAME_LEN
));
3381 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3385 DBG("Receiving var len exclusion event list from client ...");
3386 exclusion
->count
= count
;
3387 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3388 count
* LTTNG_SYMBOL_NAME_LEN
);
3390 DBG("Nothing recv() from client var len data... continuing");
3393 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3398 /* Get filter expression from client. */
3399 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3400 size_t expression_len
=
3401 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3403 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3404 ret
= LTTNG_ERR_FILTER_INVAL
;
3409 filter_expression
= zmalloc(expression_len
);
3410 if (!filter_expression
) {
3412 ret
= LTTNG_ERR_FILTER_NOMEM
;
3416 /* Receive var. len. data */
3417 DBG("Receiving var len filter's expression from client ...");
3418 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3421 DBG("Nothing recv() from client var len data... continuing");
3423 free(filter_expression
);
3425 ret
= LTTNG_ERR_FILTER_INVAL
;
3430 /* Handle filter and get bytecode from client. */
3431 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3432 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3434 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3435 ret
= LTTNG_ERR_FILTER_INVAL
;
3436 free(filter_expression
);
3441 bytecode
= zmalloc(bytecode_len
);
3443 free(filter_expression
);
3445 ret
= LTTNG_ERR_FILTER_NOMEM
;
3449 /* Receive var. len. data */
3450 DBG("Receiving var len filter's bytecode from client ...");
3451 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3453 DBG("Nothing recv() from client var len data... continuing");
3455 free(filter_expression
);
3458 ret
= LTTNG_ERR_FILTER_INVAL
;
3462 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3463 free(filter_expression
);
3466 ret
= LTTNG_ERR_FILTER_INVAL
;
3471 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
3473 DBG("Failed to copy event: %s",
3474 cmd_ctx
->lsm
->u
.enable
.event
.name
);
3475 free(filter_expression
);
3478 ret
= LTTNG_ERR_NOMEM
;
3483 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
3484 /* Expect a userspace probe description. */
3485 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
3487 free(filter_expression
);
3490 lttng_event_destroy(ev
);
3495 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3496 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3498 filter_expression
, bytecode
, exclusion
,
3499 kernel_poll_pipe
[1]);
3500 lttng_event_destroy(ev
);
3503 case LTTNG_LIST_TRACEPOINTS
:
3505 struct lttng_event
*events
;
3508 session_lock_list();
3509 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3510 session_unlock_list();
3511 if (nb_events
< 0) {
3512 /* Return value is a negative lttng_error_code. */
3518 * Setup lttng message with payload size set to the event list size in
3519 * bytes and then copy list into the llm payload.
3521 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3522 sizeof(struct lttng_event
) * nb_events
);
3532 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3534 struct lttng_event_field
*fields
;
3537 session_lock_list();
3538 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3540 session_unlock_list();
3541 if (nb_fields
< 0) {
3542 /* Return value is a negative lttng_error_code. */
3548 * Setup lttng message with payload size set to the event list size in
3549 * bytes and then copy list into the llm payload.
3551 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3552 sizeof(struct lttng_event_field
) * nb_fields
);
3562 case LTTNG_LIST_SYSCALLS
:
3564 struct lttng_event
*events
;
3567 nb_events
= cmd_list_syscalls(&events
);
3568 if (nb_events
< 0) {
3569 /* Return value is a negative lttng_error_code. */
3575 * Setup lttng message with payload size set to the event list size in
3576 * bytes and then copy list into the llm payload.
3578 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3579 sizeof(struct lttng_event
) * nb_events
);
3589 case LTTNG_LIST_TRACKER_PIDS
:
3591 int32_t *pids
= NULL
;
3594 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3595 cmd_ctx
->lsm
->domain
.type
, &pids
);
3597 /* Return value is a negative lttng_error_code. */
3603 * Setup lttng message with payload size set to the event list size in
3604 * bytes and then copy list into the llm payload.
3606 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3607 sizeof(int32_t) * nr_pids
);
3617 case LTTNG_SET_CONSUMER_URI
:
3620 struct lttng_uri
*uris
;
3622 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3623 len
= nb_uri
* sizeof(struct lttng_uri
);
3626 ret
= LTTNG_ERR_INVALID
;
3630 uris
= zmalloc(len
);
3632 ret
= LTTNG_ERR_FATAL
;
3636 /* Receive variable len data */
3637 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3638 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3640 DBG("No URIs received from client... continuing");
3642 ret
= LTTNG_ERR_SESSION_FAIL
;
3647 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3649 if (ret
!= LTTNG_OK
) {
3656 case LTTNG_START_TRACE
:
3659 * On the first start, if we have a kernel session and we have
3660 * enabled time or size-based rotations, we have to make sure
3661 * the kernel tracer supports it.
3663 if (!cmd_ctx
->session
->has_been_started
&& \
3664 cmd_ctx
->session
->kernel_session
&& \
3665 (cmd_ctx
->session
->rotate_timer_period
|| \
3666 cmd_ctx
->session
->rotate_size
) && \
3667 !check_rotate_compatible()) {
3668 DBG("Kernel tracer version is not compatible with the rotation feature");
3669 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3672 ret
= cmd_start_trace(cmd_ctx
->session
);
3675 case LTTNG_STOP_TRACE
:
3677 ret
= cmd_stop_trace(cmd_ctx
->session
);
3680 case LTTNG_CREATE_SESSION
:
3683 struct lttng_uri
*uris
= NULL
;
3685 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3686 len
= nb_uri
* sizeof(struct lttng_uri
);
3689 uris
= zmalloc(len
);
3691 ret
= LTTNG_ERR_FATAL
;
3695 /* Receive variable len data */
3696 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3697 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3699 DBG("No URIs received from client... continuing");
3701 ret
= LTTNG_ERR_SESSION_FAIL
;
3706 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3707 DBG("Creating session with ONE network URI is a bad call");
3708 ret
= LTTNG_ERR_SESSION_FAIL
;
3714 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3715 &cmd_ctx
->creds
, 0);
3721 case LTTNG_DESTROY_SESSION
:
3723 ret
= cmd_destroy_session(cmd_ctx
->session
,
3724 notification_thread_handle
);
3727 case LTTNG_LIST_DOMAINS
:
3730 struct lttng_domain
*domains
= NULL
;
3732 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3734 /* Return value is a negative lttng_error_code. */
3739 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3740 nb_dom
* sizeof(struct lttng_domain
));
3750 case LTTNG_LIST_CHANNELS
:
3752 ssize_t payload_size
;
3753 struct lttng_channel
*channels
= NULL
;
3755 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3756 cmd_ctx
->session
, &channels
);
3757 if (payload_size
< 0) {
3758 /* Return value is a negative lttng_error_code. */
3759 ret
= -payload_size
;
3763 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3774 case LTTNG_LIST_EVENTS
:
3777 struct lttng_event
*events
= NULL
;
3778 struct lttcomm_event_command_header cmd_header
;
3781 memset(&cmd_header
, 0, sizeof(cmd_header
));
3782 /* Extended infos are included at the end of events */
3783 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3784 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3785 &events
, &total_size
);
3788 /* Return value is a negative lttng_error_code. */
3793 cmd_header
.nb_events
= nb_event
;
3794 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3795 &cmd_header
, sizeof(cmd_header
));
3805 case LTTNG_LIST_SESSIONS
:
3807 unsigned int nr_sessions
;
3808 void *sessions_payload
;
3811 session_lock_list();
3812 nr_sessions
= lttng_sessions_count(
3813 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3814 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3815 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3816 sessions_payload
= zmalloc(payload_len
);
3818 if (!sessions_payload
) {
3819 session_unlock_list();
3824 cmd_list_lttng_sessions(sessions_payload
,
3825 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3826 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3827 session_unlock_list();
3829 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3831 free(sessions_payload
);
3840 case LTTNG_REGISTER_CONSUMER
:
3842 struct consumer_data
*cdata
;
3844 switch (cmd_ctx
->lsm
->domain
.type
) {
3845 case LTTNG_DOMAIN_KERNEL
:
3846 cdata
= &kconsumer_data
;
3849 ret
= LTTNG_ERR_UND
;
3853 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3854 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3857 case LTTNG_DATA_PENDING
:
3860 uint8_t pending_ret_byte
;
3862 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3867 * This function may returns 0 or 1 to indicate whether or not
3868 * there is data pending. In case of error, it should return an
3869 * LTTNG_ERR code. However, some code paths may still return
3870 * a nondescript error code, which we handle by returning an
3873 if (pending_ret
== 0 || pending_ret
== 1) {
3875 * ret will be set to LTTNG_OK at the end of
3878 } else if (pending_ret
< 0) {
3879 ret
= LTTNG_ERR_UNK
;
3886 pending_ret_byte
= (uint8_t) pending_ret
;
3888 /* 1 byte to return whether or not data is pending */
3889 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3890 &pending_ret_byte
, 1);
3899 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3901 struct lttcomm_lttng_output_id reply
;
3903 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3904 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3905 if (ret
!= LTTNG_OK
) {
3909 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3915 /* Copy output list into message payload */
3919 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3921 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3922 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3925 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3928 struct lttng_snapshot_output
*outputs
= NULL
;
3930 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3931 if (nb_output
< 0) {
3936 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3937 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3938 nb_output
* sizeof(struct lttng_snapshot_output
));
3948 case LTTNG_SNAPSHOT_RECORD
:
3950 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3951 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3952 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3955 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3958 struct lttng_uri
*uris
= NULL
;
3960 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3961 len
= nb_uri
* sizeof(struct lttng_uri
);
3964 uris
= zmalloc(len
);
3966 ret
= LTTNG_ERR_FATAL
;
3970 /* Receive variable len data */
3971 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3972 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3974 DBG("No URIs received from client... continuing");
3976 ret
= LTTNG_ERR_SESSION_FAIL
;
3981 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3982 DBG("Creating session with ONE network URI is a bad call");
3983 ret
= LTTNG_ERR_SESSION_FAIL
;
3989 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3990 nb_uri
, &cmd_ctx
->creds
);
3994 case LTTNG_CREATE_SESSION_LIVE
:
3997 struct lttng_uri
*uris
= NULL
;
3999 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4000 len
= nb_uri
* sizeof(struct lttng_uri
);
4003 uris
= zmalloc(len
);
4005 ret
= LTTNG_ERR_FATAL
;
4009 /* Receive variable len data */
4010 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4011 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4013 DBG("No URIs received from client... continuing");
4015 ret
= LTTNG_ERR_SESSION_FAIL
;
4020 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4021 DBG("Creating session with ONE network URI is a bad call");
4022 ret
= LTTNG_ERR_SESSION_FAIL
;
4028 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4029 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4033 case LTTNG_SAVE_SESSION
:
4035 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4039 case LTTNG_SET_SESSION_SHM_PATH
:
4041 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4042 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4045 case LTTNG_REGENERATE_METADATA
:
4047 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4050 case LTTNG_REGENERATE_STATEDUMP
:
4052 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4055 case LTTNG_REGISTER_TRIGGER
:
4057 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4058 notification_thread_handle
);
4061 case LTTNG_UNREGISTER_TRIGGER
:
4063 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4064 notification_thread_handle
);
4067 case LTTNG_ROTATE_SESSION
:
4069 struct lttng_rotate_session_return rotate_return
;
4071 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4073 memset(&rotate_return
, 0, sizeof(rotate_return
));
4074 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4075 DBG("Kernel tracer version is not compatible with the rotation feature");
4076 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4080 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4086 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4087 sizeof(rotate_return
));
4096 case LTTNG_ROTATION_GET_INFO
:
4098 struct lttng_rotation_get_info_return get_info_return
;
4100 memset(&get_info_return
, 0, sizeof(get_info_return
));
4101 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4102 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4108 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4109 sizeof(get_info_return
));
4118 case LTTNG_ROTATION_SET_SCHEDULE
:
4121 enum lttng_rotation_schedule_type schedule_type
;
4124 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4125 DBG("Kernel tracer version does not support session rotations");
4126 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4130 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
4131 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
4132 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
4134 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4138 notification_thread_handle
);
4139 if (ret
!= LTTNG_OK
) {
4145 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
4147 struct lttng_session_list_schedules_return schedules
= {
4148 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
4149 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
4150 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
4151 .size
.value
= cmd_ctx
->session
->rotate_size
,
4154 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
4165 ret
= LTTNG_ERR_UND
;
4170 if (cmd_ctx
->llm
== NULL
) {
4171 DBG("Missing llm structure. Allocating one.");
4172 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4176 /* Set return code */
4177 cmd_ctx
->llm
->ret_code
= ret
;
4179 if (cmd_ctx
->session
) {
4180 session_unlock(cmd_ctx
->session
);
4181 session_put(cmd_ctx
->session
);
4183 if (need_tracing_session
) {
4184 session_unlock_list();
4187 assert(!rcu_read_ongoing());
4192 * This thread manage all clients request using the unix client socket for
4195 static void *thread_manage_clients(void *data
)
4197 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4199 uint32_t revents
, nb_fd
;
4200 struct command_ctx
*cmd_ctx
= NULL
;
4201 struct lttng_poll_event events
;
4203 DBG("[thread] Manage client started");
4205 rcu_register_thread();
4207 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4209 health_code_update();
4211 ret
= lttcomm_listen_unix_sock(client_sock
);
4217 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4218 * more will be added to this poll set.
4220 ret
= sessiond_set_thread_pollset(&events
, 2);
4222 goto error_create_poll
;
4225 /* Add the application registration socket */
4226 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4231 ret
= sem_post(&load_info
->message_thread_ready
);
4233 PERROR("sem_post message_thread_ready");
4238 * Wait until all support threads are initialized before accepting
4241 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4243 * If a support thread failed to launch, it may signal that
4244 * we must exit and the sessiond would never be marked as
4247 * The timeout is set to 1ms, which serves as a way to
4248 * pace down this check.
4250 ret
= sessiond_wait_for_quit_pipe(1000);
4256 * This barrier is paired with the one in sessiond_notify_ready() to
4257 * ensure that loads accessing data initialized by the other threads,
4258 * on which this thread was waiting, are not performed before this point.
4260 * Note that this could be a 'read' memory barrier, but a full barrier
4261 * is used in case the code changes. The performance implications of
4262 * this choice are minimal since this is a slow path.
4266 /* This testpoint is after we signal readiness to the parent. */
4267 if (testpoint(sessiond_thread_manage_clients
)) {
4271 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4275 health_code_update();
4277 /* Set state as running. */
4278 sessiond_set_client_thread_state(true);
4281 const struct cmd_completion_handler
*cmd_completion_handler
;
4283 DBG("Accepting client command ...");
4285 /* Inifinite blocking call, waiting for transmission */
4287 health_poll_entry();
4288 ret
= lttng_poll_wait(&events
, -1);
4292 * Restart interrupted system call.
4294 if (errno
== EINTR
) {
4302 for (i
= 0; i
< nb_fd
; i
++) {
4303 /* Fetch once the poll data */
4304 revents
= LTTNG_POLL_GETEV(&events
, i
);
4305 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4307 health_code_update();
4310 /* No activity for this FD (poll implementation). */
4314 /* Thread quit pipe has been closed. Killing thread. */
4315 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4321 /* Event on the registration socket */
4322 if (pollfd
== client_sock
) {
4323 if (revents
& LPOLLIN
) {
4325 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4326 ERR("Client socket poll error");
4329 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4335 DBG("Wait for client response");
4337 health_code_update();
4339 sock
= lttcomm_accept_unix_sock(client_sock
);
4345 * Set the CLOEXEC flag. Return code is useless because either way, the
4348 (void) utils_set_fd_cloexec(sock
);
4350 /* Set socket option for credentials retrieval */
4351 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4356 /* Allocate context command to process the client request */
4357 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4358 if (cmd_ctx
== NULL
) {
4359 PERROR("zmalloc cmd_ctx");
4363 /* Allocate data buffer for reception */
4364 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4365 if (cmd_ctx
->lsm
== NULL
) {
4366 PERROR("zmalloc cmd_ctx->lsm");
4370 cmd_ctx
->llm
= NULL
;
4371 cmd_ctx
->session
= NULL
;
4373 health_code_update();
4376 * Data is received from the lttng client. The struct
4377 * lttcomm_session_msg (lsm) contains the command and data request of
4380 DBG("Receiving data from client ...");
4381 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4382 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4384 DBG("Nothing recv() from client... continuing");
4390 clean_command_ctx(&cmd_ctx
);
4394 health_code_update();
4396 // TODO: Validate cmd_ctx including sanity check for
4397 // security purpose.
4399 rcu_thread_online();
4401 * This function dispatch the work to the kernel or userspace tracer
4402 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4403 * informations for the client. The command context struct contains
4404 * everything this function may needs.
4406 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4407 rcu_thread_offline();
4415 * TODO: Inform client somehow of the fatal error. At
4416 * this point, ret < 0 means that a zmalloc failed
4417 * (ENOMEM). Error detected but still accept
4418 * command, unless a socket error has been
4421 clean_command_ctx(&cmd_ctx
);
4425 cmd_completion_handler
= cmd_pop_completion_handler();
4426 if (cmd_completion_handler
) {
4427 enum lttng_error_code completion_code
;
4429 completion_code
= cmd_completion_handler
->run(
4430 cmd_completion_handler
->data
);
4431 if (completion_code
!= LTTNG_OK
) {
4432 clean_command_ctx(&cmd_ctx
);
4437 health_code_update();
4439 DBG("Sending response (size: %d, retcode: %s (%d))",
4440 cmd_ctx
->lttng_msg_size
,
4441 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4442 cmd_ctx
->llm
->ret_code
);
4443 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4445 ERR("Failed to send data back to client");
4448 /* End of transmission */
4455 clean_command_ctx(&cmd_ctx
);
4457 health_code_update();
4469 lttng_poll_clean(&events
);
4470 clean_command_ctx(&cmd_ctx
);
4474 unlink(config
.client_unix_sock_path
.value
);
4475 if (client_sock
>= 0) {
4476 ret
= close(client_sock
);
4484 ERR("Health error occurred in %s", __func__
);
4487 health_unregister(health_sessiond
);
4489 DBG("Client thread dying");
4491 rcu_unregister_thread();
4494 * Since we are creating the consumer threads, we own them, so we need
4495 * to join them before our thread exits.
4497 ret
= join_consumer_thread(&kconsumer_data
);
4500 PERROR("join_consumer");
4503 ret
= join_consumer_thread(&ustconsumer32_data
);
4506 PERROR("join_consumer ust32");
4509 ret
= join_consumer_thread(&ustconsumer64_data
);
4512 PERROR("join_consumer ust64");
4515 /* Set state as non-running. */
4516 sessiond_set_client_thread_state(false);
4520 static int string_match(const char *str1
, const char *str2
)
4522 return (str1
&& str2
) && !strcmp(str1
, str2
);
4526 * Take an option from the getopt output and set it in the right variable to be
4529 * Return 0 on success else a negative value.
4531 static int set_option(int opt
, const char *arg
, const char *optname
)
4535 if (string_match(optname
, "client-sock") || opt
== 'c') {
4536 if (!arg
|| *arg
== '\0') {
4540 if (lttng_is_setuid_setgid()) {
4541 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4542 "-c, --client-sock");
4544 config_string_set(&config
.client_unix_sock_path
,
4546 if (!config
.client_unix_sock_path
.value
) {
4551 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4552 if (!arg
|| *arg
== '\0') {
4556 if (lttng_is_setuid_setgid()) {
4557 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4560 config_string_set(&config
.apps_unix_sock_path
,
4562 if (!config
.apps_unix_sock_path
.value
) {
4567 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4568 config
.daemonize
= true;
4569 } else if (string_match(optname
, "background") || opt
== 'b') {
4570 config
.background
= true;
4571 } else if (string_match(optname
, "group") || opt
== 'g') {
4572 if (!arg
|| *arg
== '\0') {
4576 if (lttng_is_setuid_setgid()) {
4577 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4580 config_string_set(&config
.tracing_group_name
,
4582 if (!config
.tracing_group_name
.value
) {
4587 } else if (string_match(optname
, "help") || opt
== 'h') {
4588 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4590 ERR("Cannot show --help for `lttng-sessiond`");
4593 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4594 } else if (string_match(optname
, "version") || opt
== 'V') {
4595 fprintf(stdout
, "%s\n", VERSION
);
4597 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4598 config
.sig_parent
= true;
4599 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4600 if (!arg
|| *arg
== '\0') {
4604 if (lttng_is_setuid_setgid()) {
4605 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4606 "--kconsumerd-err-sock");
4608 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4610 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4615 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4616 if (!arg
|| *arg
== '\0') {
4620 if (lttng_is_setuid_setgid()) {
4621 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4622 "--kconsumerd-cmd-sock");
4624 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4626 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4631 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4632 if (!arg
|| *arg
== '\0') {
4636 if (lttng_is_setuid_setgid()) {
4637 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4638 "--ustconsumerd64-err-sock");
4640 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4642 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4647 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4648 if (!arg
|| *arg
== '\0') {
4652 if (lttng_is_setuid_setgid()) {
4653 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4654 "--ustconsumerd64-cmd-sock");
4656 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4658 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4663 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4664 if (!arg
|| *arg
== '\0') {
4668 if (lttng_is_setuid_setgid()) {
4669 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4670 "--ustconsumerd32-err-sock");
4672 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4674 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4679 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4680 if (!arg
|| *arg
== '\0') {
4684 if (lttng_is_setuid_setgid()) {
4685 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4686 "--ustconsumerd32-cmd-sock");
4688 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4690 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4695 } else if (string_match(optname
, "no-kernel")) {
4696 config
.no_kernel
= true;
4697 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4698 config
.quiet
= true;
4699 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4700 /* Verbose level can increase using multiple -v */
4702 /* Value obtained from config file */
4703 config
.verbose
= config_parse_value(arg
);
4705 /* -v used on command line */
4708 /* Clamp value to [0, 3] */
4709 config
.verbose
= config
.verbose
< 0 ? 0 :
4710 (config
.verbose
<= 3 ? config
.verbose
: 3);
4711 } else if (string_match(optname
, "verbose-consumer")) {
4713 config
.verbose_consumer
= config_parse_value(arg
);
4715 config
.verbose_consumer
++;
4717 } else if (string_match(optname
, "consumerd32-path")) {
4718 if (!arg
|| *arg
== '\0') {
4722 if (lttng_is_setuid_setgid()) {
4723 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4724 "--consumerd32-path");
4726 config_string_set(&config
.consumerd32_bin_path
,
4728 if (!config
.consumerd32_bin_path
.value
) {
4733 } else if (string_match(optname
, "consumerd32-libdir")) {
4734 if (!arg
|| *arg
== '\0') {
4738 if (lttng_is_setuid_setgid()) {
4739 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4740 "--consumerd32-libdir");
4742 config_string_set(&config
.consumerd32_lib_dir
,
4744 if (!config
.consumerd32_lib_dir
.value
) {
4749 } else if (string_match(optname
, "consumerd64-path")) {
4750 if (!arg
|| *arg
== '\0') {
4754 if (lttng_is_setuid_setgid()) {
4755 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4756 "--consumerd64-path");
4758 config_string_set(&config
.consumerd64_bin_path
,
4760 if (!config
.consumerd64_bin_path
.value
) {
4765 } else if (string_match(optname
, "consumerd64-libdir")) {
4766 if (!arg
|| *arg
== '\0') {
4770 if (lttng_is_setuid_setgid()) {
4771 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4772 "--consumerd64-libdir");
4774 config_string_set(&config
.consumerd64_lib_dir
,
4776 if (!config
.consumerd64_lib_dir
.value
) {
4781 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4782 if (!arg
|| *arg
== '\0') {
4786 if (lttng_is_setuid_setgid()) {
4787 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4790 config_string_set(&config
.pid_file_path
, strdup(arg
));
4791 if (!config
.pid_file_path
.value
) {
4796 } else if (string_match(optname
, "agent-tcp-port")) {
4797 if (!arg
|| *arg
== '\0') {
4801 if (lttng_is_setuid_setgid()) {
4802 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4803 "--agent-tcp-port");
4808 v
= strtoul(arg
, NULL
, 0);
4809 if (errno
!= 0 || !isdigit(arg
[0])) {
4810 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4813 if (v
== 0 || v
>= 65535) {
4814 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4817 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
4818 DBG3("Agent TCP port set to non default: %i", (int) v
);
4820 } else if (string_match(optname
, "load") || opt
== 'l') {
4821 if (!arg
|| *arg
== '\0') {
4825 if (lttng_is_setuid_setgid()) {
4826 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4829 config_string_set(&config
.load_session_path
, strdup(arg
));
4830 if (!config
.load_session_path
.value
) {
4835 } else if (string_match(optname
, "kmod-probes")) {
4836 if (!arg
|| *arg
== '\0') {
4840 if (lttng_is_setuid_setgid()) {
4841 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4844 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
4845 if (!config
.kmod_probes_list
.value
) {
4850 } else if (string_match(optname
, "extra-kmod-probes")) {
4851 if (!arg
|| *arg
== '\0') {
4855 if (lttng_is_setuid_setgid()) {
4856 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4857 "--extra-kmod-probes");
4859 config_string_set(&config
.kmod_extra_probes_list
,
4861 if (!config
.kmod_extra_probes_list
.value
) {
4866 } else if (string_match(optname
, "config") || opt
== 'f') {
4867 /* This is handled in set_options() thus silent skip. */
4870 /* Unknown option or other error.
4871 * Error is printed by getopt, just return */
4876 if (ret
== -EINVAL
) {
4877 const char *opt_name
= "unknown";
4880 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4882 if (opt
== long_options
[i
].val
) {
4883 opt_name
= long_options
[i
].name
;
4888 WARN("Invalid argument provided for option \"%s\", using default value.",
4896 * config_entry_handler_cb used to handle options read from a config file.
4897 * See config_entry_handler_cb comment in common/config/session-config.h for the
4898 * return value conventions.
4900 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4904 if (!entry
|| !entry
->name
|| !entry
->value
) {
4909 /* Check if the option is to be ignored */
4910 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4911 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4916 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4919 /* Ignore if not fully matched. */
4920 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4925 * If the option takes no argument on the command line, we have to
4926 * check if the value is "true". We support non-zero numeric values,
4929 if (!long_options
[i
].has_arg
) {
4930 ret
= config_parse_value(entry
->value
);
4933 WARN("Invalid configuration value \"%s\" for option %s",
4934 entry
->value
, entry
->name
);
4936 /* False, skip boolean config option. */
4941 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4945 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4952 * daemon configuration loading and argument parsing
4954 static int set_options(int argc
, char **argv
)
4956 int ret
= 0, c
= 0, option_index
= 0;
4957 int orig_optopt
= optopt
, orig_optind
= optind
;
4959 const char *config_path
= NULL
;
4961 optstring
= utils_generate_optstring(long_options
,
4962 sizeof(long_options
) / sizeof(struct option
));
4968 /* Check for the --config option */
4969 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4970 &option_index
)) != -1) {
4974 } else if (c
!= 'f') {
4975 /* if not equal to --config option. */
4979 if (lttng_is_setuid_setgid()) {
4980 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4983 config_path
= utils_expand_path(optarg
);
4985 ERR("Failed to resolve path: %s", optarg
);
4990 ret
= config_get_section_entries(config_path
, config_section_name
,
4991 config_entry_handler
, NULL
);
4994 ERR("Invalid configuration option at line %i", ret
);
5000 /* Reset getopt's global state */
5001 optopt
= orig_optopt
;
5002 optind
= orig_optind
;
5006 * getopt_long() will not set option_index if it encounters a
5009 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5016 * Pass NULL as the long option name if popt left the index
5019 ret
= set_option(c
, optarg
,
5020 option_index
< 0 ? NULL
:
5021 long_options
[option_index
].name
);
5033 * Creates the two needed socket by the daemon.
5034 * apps_sock - The communication socket for all UST apps.
5035 * client_sock - The communication of the cli tool (lttng).
5037 static int init_daemon_socket(void)
5042 old_umask
= umask(0);
5044 /* Create client tool unix socket */
5045 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5046 if (client_sock
< 0) {
5047 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5052 /* Set the cloexec flag */
5053 ret
= utils_set_fd_cloexec(client_sock
);
5055 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5056 "Continuing but note that the consumer daemon will have a "
5057 "reference to this socket on exec()", client_sock
);
5060 /* File permission MUST be 660 */
5061 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5063 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5068 /* Create the application unix socket */
5069 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5070 if (apps_sock
< 0) {
5071 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5076 /* Set the cloexec flag */
5077 ret
= utils_set_fd_cloexec(apps_sock
);
5079 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5080 "Continuing but note that the consumer daemon will have a "
5081 "reference to this socket on exec()", apps_sock
);
5084 /* File permission MUST be 666 */
5085 ret
= chmod(config
.apps_unix_sock_path
.value
,
5086 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5088 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5093 DBG3("Session daemon client socket %d and application socket %d created",
5094 client_sock
, apps_sock
);
5102 * Create lockfile using the rundir and return its fd.
5104 static int create_lockfile(void)
5106 return utils_create_lock_file(config
.lock_file_path
.value
);
5110 * Check if the global socket is available, and if a daemon is answering at the
5111 * other side. If yes, error is returned.
5113 * Also attempts to create and hold the lock file.
5115 static int check_existing_daemon(void)
5119 /* Is there anybody out there ? */
5120 if (lttng_session_daemon_alive()) {
5125 lockfile_fd
= create_lockfile();
5126 if (lockfile_fd
< 0) {
5134 static void sessiond_cleanup_lock_file(void)
5139 * Cleanup lock file by deleting it and finaly closing it which will
5140 * release the file system lock.
5142 if (lockfile_fd
>= 0) {
5143 ret
= remove(config
.lock_file_path
.value
);
5145 PERROR("remove lock file");
5147 ret
= close(lockfile_fd
);
5149 PERROR("close lock file");
5155 * Set the tracing group gid onto the client socket.
5157 * Race window between mkdir and chown is OK because we are going from more
5158 * permissive (root.root) to less permissive (root.tracing).
5160 static int set_permissions(char *rundir
)
5165 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5167 /* Set lttng run dir */
5168 ret
= chown(rundir
, 0, gid
);
5170 ERR("Unable to set group on %s", rundir
);
5175 * Ensure all applications and tracing group can search the run
5176 * dir. Allow everyone to read the directory, since it does not
5177 * buy us anything to hide its content.
5179 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5181 ERR("Unable to set permissions on %s", rundir
);
5185 /* lttng client socket path */
5186 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5188 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5192 /* kconsumer error socket path */
5193 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5195 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5199 /* 64-bit ustconsumer error socket path */
5200 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5202 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5206 /* 32-bit ustconsumer compat32 error socket path */
5207 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5209 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5213 DBG("All permissions are set");
5219 * Create the lttng run directory needed for all global sockets and pipe.
5221 static int create_lttng_rundir(void)
5225 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5227 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5229 if (errno
!= EEXIST
) {
5230 ERR("Unable to create %s", config
.rundir
.value
);
5242 * Setup sockets and directory needed by the consumerds' communication with the
5245 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5250 switch (consumer_data
->type
) {
5251 case LTTNG_CONSUMER_KERNEL
:
5252 path
= config
.kconsumerd_path
.value
;
5254 case LTTNG_CONSUMER64_UST
:
5255 path
= config
.consumerd64_path
.value
;
5257 case LTTNG_CONSUMER32_UST
:
5258 path
= config
.consumerd32_path
.value
;
5261 ERR("Consumer type unknown");
5267 DBG2("Creating consumer directory: %s", path
);
5269 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5270 if (ret
< 0 && errno
!= EEXIST
) {
5272 ERR("Failed to create %s", path
);
5276 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5278 ERR("Unable to set group on %s", path
);
5284 /* Create the consumerd error unix socket */
5285 consumer_data
->err_sock
=
5286 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5287 if (consumer_data
->err_sock
< 0) {
5288 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5294 * Set the CLOEXEC flag. Return code is useless because either way, the
5297 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5299 PERROR("utils_set_fd_cloexec");
5300 /* continue anyway */
5303 /* File permission MUST be 660 */
5304 ret
= chmod(consumer_data
->err_unix_sock_path
,
5305 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5307 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5317 * Signal handler for the daemon
5319 * Simply stop all worker threads, leaving main() return gracefully after
5320 * joining all threads and calling cleanup().
5322 static void sighandler(int sig
)
5326 DBG("SIGINT caught");
5330 DBG("SIGTERM caught");
5334 CMM_STORE_SHARED(recv_child_signal
, 1);
5342 * Setup signal handler for :
5343 * SIGINT, SIGTERM, SIGPIPE
5345 static int set_signal_handler(void)
5348 struct sigaction sa
;
5351 if ((ret
= sigemptyset(&sigset
)) < 0) {
5352 PERROR("sigemptyset");
5356 sa
.sa_mask
= sigset
;
5359 sa
.sa_handler
= sighandler
;
5360 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5361 PERROR("sigaction");
5365 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5366 PERROR("sigaction");
5370 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5371 PERROR("sigaction");
5375 sa
.sa_handler
= SIG_IGN
;
5376 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5377 PERROR("sigaction");
5381 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5387 * Set open files limit to unlimited. This daemon can open a large number of
5388 * file descriptors in order to consume multiple kernel traces.
5390 static void set_ulimit(void)
5395 /* The kernel does not allow an infinite limit for open files */
5396 lim
.rlim_cur
= 65535;
5397 lim
.rlim_max
= 65535;
5399 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5401 PERROR("failed to set open files limit");
5405 static int write_pidfile(void)
5407 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5410 static int set_clock_plugin_env(void)
5413 char *env_value
= NULL
;
5415 if (!config
.lttng_ust_clock_plugin
.value
) {
5419 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5420 config
.lttng_ust_clock_plugin
.value
);
5426 ret
= putenv(env_value
);
5429 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5433 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5434 config
.lttng_ust_clock_plugin
.value
);
5439 static void destroy_all_sessions_and_wait(void)
5441 struct ltt_session
*session
, *tmp
;
5442 struct ltt_session_list
*session_list
;
5444 session_list
= session_get_list();
5445 DBG("Initiating destruction of all sessions");
5447 if (!session_list
) {
5452 * Ensure that the client thread is no longer accepting new commands,
5453 * which could cause new sessions to be created.
5455 sessiond_wait_client_thread_stopped();
5457 session_lock_list();
5458 /* Initiate the destruction of all sessions. */
5459 cds_list_for_each_entry_safe(session
, tmp
,
5460 &session_list
->head
, list
) {
5461 if (!session_get(session
)) {
5465 session_lock(session
);
5466 if (session
->destroyed
) {
5467 goto unlock_session
;
5469 (void) cmd_destroy_session(session
,
5470 notification_thread_handle
);
5472 session_unlock(session
);
5473 session_put(session
);
5475 session_unlock_list();
5477 /* Wait for the destruction of all sessions to complete. */
5478 DBG("Waiting for the destruction of all sessions to complete");
5479 session_list_wait_empty();
5480 DBG("Destruction of all sessions completed");
5486 int main(int argc
, char **argv
)
5488 int ret
= 0, retval
= 0;
5490 const char *env_app_timeout
;
5491 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5492 *ust64_channel_monitor_pipe
= NULL
,
5493 *kernel_channel_monitor_pipe
= NULL
;
5494 struct lttng_thread
*ht_cleanup_thread
= NULL
;
5495 struct timer_thread_parameters timer_thread_parameters
;
5496 /* Rotation thread handle. */
5497 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
5498 /* Queue of rotation jobs populated by the sessiond-timer. */
5499 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5501 init_kernel_workarounds();
5503 rcu_register_thread();
5505 if (set_signal_handler()) {
5507 goto exit_set_signal_handler
;
5510 if (timer_signal_init()) {
5512 goto exit_set_signal_handler
;
5515 page_size
= sysconf(_SC_PAGESIZE
);
5516 if (page_size
< 0) {
5517 PERROR("sysconf _SC_PAGESIZE");
5518 page_size
= LONG_MAX
;
5519 WARN("Fallback page size to %ld", page_size
);
5522 ret
= sessiond_config_init(&config
);
5525 goto exit_set_signal_handler
;
5529 * Init config from environment variables.
5530 * Command line option override env configuration per-doc. Do env first.
5532 sessiond_config_apply_env_config(&config
);
5535 * Parse arguments and load the daemon configuration file.
5537 * We have an exit_options exit path to free memory reserved by
5538 * set_options. This is needed because the rest of sessiond_cleanup()
5539 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5540 * depends on set_options.
5543 if (set_options(argc
, argv
)) {
5549 * Resolve all paths received as arguments, configuration option, or
5550 * through environment variable as absolute paths. This is necessary
5551 * since daemonizing causes the sessiond's current working directory
5554 ret
= sessiond_config_resolve_paths(&config
);
5560 lttng_opt_verbose
= config
.verbose
;
5561 lttng_opt_quiet
= config
.quiet
;
5562 kconsumer_data
.err_unix_sock_path
=
5563 config
.kconsumerd_err_unix_sock_path
.value
;
5564 kconsumer_data
.cmd_unix_sock_path
=
5565 config
.kconsumerd_cmd_unix_sock_path
.value
;
5566 ustconsumer32_data
.err_unix_sock_path
=
5567 config
.consumerd32_err_unix_sock_path
.value
;
5568 ustconsumer32_data
.cmd_unix_sock_path
=
5569 config
.consumerd32_cmd_unix_sock_path
.value
;
5570 ustconsumer64_data
.err_unix_sock_path
=
5571 config
.consumerd64_err_unix_sock_path
.value
;
5572 ustconsumer64_data
.cmd_unix_sock_path
=
5573 config
.consumerd64_cmd_unix_sock_path
.value
;
5574 set_clock_plugin_env();
5576 sessiond_config_log(&config
);
5578 if (create_lttng_rundir()) {
5583 /* Abort launch if a session daemon is already running. */
5584 if (check_existing_daemon()) {
5585 ERR("A session daemon is already running.");
5591 if (config
.daemonize
|| config
.background
) {
5594 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5595 !config
.background
);
5602 * We are in the child. Make sure all other file descriptors are
5603 * closed, in case we are called with more opened file
5604 * descriptors than the standard ones and the lock file.
5606 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5607 if (i
== lockfile_fd
) {
5614 if (run_as_create_worker(argv
[0]) < 0) {
5615 goto exit_create_run_as_worker_cleanup
;
5619 * Starting from here, we can create threads. This needs to be after
5620 * lttng_daemonize due to RCU.
5624 * Initialize the health check subsystem. This call should set the
5625 * appropriate time values.
5627 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5628 if (!health_sessiond
) {
5629 PERROR("health_app_create error");
5631 goto exit_health_sessiond_cleanup
;
5634 /* Create thread to clean up RCU hash tables */
5635 ht_cleanup_thread
= launch_ht_cleanup_thread();
5636 if (!ht_cleanup_thread
) {
5638 goto exit_ht_cleanup
;
5641 /* Create thread quit pipe */
5642 if (sessiond_init_thread_quit_pipe()) {
5644 goto exit_init_data
;
5647 /* Check if daemon is UID = 0 */
5648 is_root
= !getuid();
5650 /* Create global run dir with root access */
5652 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5653 if (!kernel_channel_monitor_pipe
) {
5654 ERR("Failed to create kernel consumer channel monitor pipe");
5656 goto exit_init_data
;
5658 kconsumer_data
.channel_monitor_pipe
=
5659 lttng_pipe_release_writefd(
5660 kernel_channel_monitor_pipe
);
5661 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5663 goto exit_init_data
;
5667 /* Set consumer initial state */
5668 kernel_consumerd_state
= CONSUMER_STOPPED
;
5669 ust_consumerd_state
= CONSUMER_STOPPED
;
5671 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5672 if (!ust32_channel_monitor_pipe
) {
5673 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5675 goto exit_init_data
;
5677 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5678 ust32_channel_monitor_pipe
);
5679 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5681 goto exit_init_data
;
5685 * The rotation_thread_timer_queue structure is shared between the
5686 * sessiond timer thread and the rotation thread. The main thread keeps
5687 * its ownership and destroys it when both threads have been joined.
5689 rotation_timer_queue
= rotation_thread_timer_queue_create();
5690 if (!rotation_timer_queue
) {
5692 goto exit_init_data
;
5694 timer_thread_parameters
.rotation_thread_job_queue
=
5695 rotation_timer_queue
;
5697 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5698 if (!ust64_channel_monitor_pipe
) {
5699 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5701 goto exit_init_data
;
5703 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5704 ust64_channel_monitor_pipe
);
5705 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5707 goto exit_init_data
;
5711 * Init UST app hash table. Alloc hash table before this point since
5712 * cleanup() can get called after that point.
5714 if (ust_app_ht_alloc()) {
5715 ERR("Failed to allocate UST app hash table");
5717 goto exit_init_data
;
5721 * Initialize agent app hash table. We allocate the hash table here
5722 * since cleanup() can get called after this point.
5724 if (agent_app_ht_alloc()) {
5725 ERR("Failed to allocate Agent app hash table");
5727 goto exit_init_data
;
5731 * These actions must be executed as root. We do that *after* setting up
5732 * the sockets path because we MUST make the check for another daemon using
5733 * those paths *before* trying to set the kernel consumer sockets and init
5737 if (set_consumer_sockets(&kconsumer_data
)) {
5739 goto exit_init_data
;
5742 /* Setup kernel tracer */
5743 if (!config
.no_kernel
) {
5744 init_kernel_tracer();
5745 if (kernel_tracer_fd
>= 0) {
5746 ret
= syscall_init_table();
5748 ERR("Unable to populate syscall table. "
5749 "Syscall tracing won't work "
5750 "for this session daemon.");
5755 /* Set ulimit for open files */
5758 /* init lttng_fd tracking must be done after set_ulimit. */
5761 if (set_consumer_sockets(&ustconsumer64_data
)) {
5763 goto exit_init_data
;
5766 if (set_consumer_sockets(&ustconsumer32_data
)) {
5768 goto exit_init_data
;
5771 /* Setup the needed unix socket */
5772 if (init_daemon_socket()) {
5774 goto exit_init_data
;
5777 /* Set credentials to socket */
5778 if (is_root
&& set_permissions(config
.rundir
.value
)) {
5780 goto exit_init_data
;
5783 /* Get parent pid if -S, --sig-parent is specified. */
5784 if (config
.sig_parent
) {
5788 /* Setup the kernel pipe for waking up the kernel thread */
5789 if (is_root
&& !config
.no_kernel
) {
5790 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5792 goto exit_init_data
;
5796 /* Setup the thread apps communication pipe. */
5797 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5799 goto exit_init_data
;
5802 /* Setup the thread apps notify communication pipe. */
5803 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5805 goto exit_init_data
;
5808 /* Initialize global buffer per UID and PID registry. */
5809 buffer_reg_init_uid_registry();
5810 buffer_reg_init_pid_registry();
5812 /* Init UST command queue. */
5813 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5817 /* Check for the application socket timeout env variable. */
5818 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5819 if (env_app_timeout
) {
5820 config
.app_socket_timeout
= atoi(env_app_timeout
);
5822 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5825 ret
= write_pidfile();
5827 ERR("Error in write_pidfile");
5829 goto exit_init_data
;
5832 /* Initialize communication library */
5834 /* Initialize TCP timeout values */
5835 lttcomm_inet_init();
5837 if (load_session_init_data(&load_info
) < 0) {
5839 goto exit_init_data
;
5841 load_info
->path
= config
.load_session_path
.value
;
5843 /* Create health-check thread. */
5844 if (!launch_health_management_thread()) {
5849 /* notification_thread_data acquires the pipes' read side. */
5850 notification_thread_handle
= notification_thread_handle_create(
5851 ust32_channel_monitor_pipe
,
5852 ust64_channel_monitor_pipe
,
5853 kernel_channel_monitor_pipe
);
5854 if (!notification_thread_handle
) {
5856 ERR("Failed to create notification thread shared data");
5857 goto exit_notification
;
5860 /* Create notification thread. */
5861 if (!launch_notification_thread(notification_thread_handle
)) {
5863 goto exit_notification
;
5866 /* Create timer thread. */
5867 if (!launch_timer_thread(&timer_thread_parameters
)) {
5869 goto exit_notification
;
5872 /* rotation_thread_data acquires the pipes' read side. */
5873 rotation_thread_handle
= rotation_thread_handle_create(
5874 rotation_timer_queue
,
5875 notification_thread_handle
);
5876 if (!rotation_thread_handle
) {
5878 ERR("Failed to create rotation thread shared data");
5883 /* Create rotation thread. */
5884 if (!launch_rotation_thread(rotation_thread_handle
)) {
5889 /* Create thread to manage the client socket */
5890 ret
= pthread_create(&client_thread
, default_pthread_attr(),
5891 thread_manage_clients
, (void *) NULL
);
5894 PERROR("pthread_create clients");
5900 /* Create thread to dispatch registration */
5901 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
5902 thread_dispatch_ust_registration
, (void *) NULL
);
5905 PERROR("pthread_create dispatch");
5911 /* Create thread to manage application registration. */
5912 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
5913 thread_registration_apps
, (void *) NULL
);
5916 PERROR("pthread_create registration");
5922 /* Create thread to manage application socket */
5923 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
5924 thread_manage_apps
, (void *) NULL
);
5927 PERROR("pthread_create apps");
5933 /* Create thread to manage application notify socket */
5934 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
5935 ust_thread_manage_notify
, (void *) NULL
);
5938 PERROR("pthread_create notify");
5941 goto exit_apps_notify
;
5944 /* Create agent registration thread. */
5945 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
5946 agent_thread_manage_registration
, (void *) NULL
);
5949 PERROR("pthread_create agent");
5952 goto exit_agent_reg
;
5955 /* Don't start this thread if kernel tracing is not requested nor root */
5956 if (is_root
&& !config
.no_kernel
) {
5957 /* Create kernel thread to manage kernel event */
5958 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
5959 thread_manage_kernel
, (void *) NULL
);
5962 PERROR("pthread_create kernel");
5969 /* Create session loading thread. */
5970 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
5971 thread_load_session
, load_info
);
5974 PERROR("pthread_create load_session_thread");
5977 goto exit_load_session
;
5981 * This is where we start awaiting program completion (e.g. through
5982 * signal that asks threads to teardown).
5985 ret
= pthread_join(load_session_thread
, &status
);
5988 PERROR("pthread_join load_session_thread");
5992 /* Initiate teardown once activity occurs on the quit pipe. */
5993 sessiond_wait_for_quit_pipe(-1U);
5994 destroy_all_sessions_and_wait();
5997 if (is_root
&& !config
.no_kernel
) {
5998 ret
= pthread_join(kernel_thread
, &status
);
6001 PERROR("pthread_join");
6007 ret
= pthread_join(agent_reg_thread
, &status
);
6010 PERROR("pthread_join agent");
6015 ret
= pthread_join(apps_notify_thread
, &status
);
6018 PERROR("pthread_join apps notify");
6023 ret
= pthread_join(apps_thread
, &status
);
6026 PERROR("pthread_join apps");
6031 ret
= pthread_join(reg_apps_thread
, &status
);
6034 PERROR("pthread_join");
6040 * Join dispatch thread after joining reg_apps_thread to ensure
6041 * we don't leak applications in the queue.
6043 ret
= pthread_join(dispatch_thread
, &status
);
6046 PERROR("pthread_join");
6051 ret
= pthread_join(client_thread
, &status
);
6054 PERROR("pthread_join");
6061 lttng_thread_list_shutdown_orphans();
6065 * Wait for all pending call_rcu work to complete before tearing
6066 * down data structures. call_rcu worker may be trying to
6067 * perform lookups in those structures.
6071 * sessiond_cleanup() is called when no other thread is running, except
6072 * the ht_cleanup thread, which is needed to destroy the hash tables.
6074 rcu_thread_online();
6078 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6079 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6080 * the queue is empty before shutting down the clean-up thread.
6084 if (ht_cleanup_thread
) {
6085 lttng_thread_shutdown(ht_cleanup_thread
);
6086 lttng_thread_put(ht_cleanup_thread
);
6089 rcu_thread_offline();
6090 rcu_unregister_thread();
6092 if (rotation_thread_handle
) {
6093 rotation_thread_handle_destroy(rotation_thread_handle
);
6097 * After the rotation and timer thread have quit, we can safely destroy
6098 * the rotation_timer_queue.
6100 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
6102 * The teardown of the notification system is performed after the
6103 * session daemon's teardown in order to allow it to be notified
6104 * of the active session and channels at the moment of the teardown.
6106 if (notification_thread_handle
) {
6107 notification_thread_handle_destroy(notification_thread_handle
);
6109 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6110 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6111 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6114 health_app_destroy(health_sessiond
);
6115 exit_health_sessiond_cleanup
:
6116 exit_create_run_as_worker_cleanup
:
6119 sessiond_cleanup_lock_file();
6120 sessiond_cleanup_options();
6122 exit_set_signal_handler
: