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
;
197 static pthread_t timer_thread
;
200 * UST registration command queue. This queue is tied with a futex and uses a N
201 * wakers / 1 waiter implemented and detailed in futex.c/.h
203 * The thread_registration_apps and thread_dispatch_ust_registration uses this
204 * queue along with the wait/wake scheme. The thread_manage_apps receives down
205 * the line new application socket and monitors it for any I/O error or clean
206 * close that triggers an unregistration of the application.
208 static struct ust_cmd_queue ust_cmd_queue
;
210 static const char *module_proc_lttng
= "/proc/lttng";
213 * Consumer daemon state which is changed when spawning it, killing it or in
214 * case of a fatal error.
216 enum consumerd_state
{
217 CONSUMER_STARTED
= 1,
218 CONSUMER_STOPPED
= 2,
223 * This consumer daemon state is used to validate if a client command will be
224 * able to reach the consumer. If not, the client is informed. For instance,
225 * doing a "lttng start" when the consumer state is set to ERROR will return an
226 * error to the client.
228 * The following example shows a possible race condition of this scheme:
230 * consumer thread error happens
232 * client cmd checks state -> still OK
233 * consumer thread exit, sets error
234 * client cmd try to talk to consumer
237 * However, since the consumer is a different daemon, we have no way of making
238 * sure the command will reach it safely even with this state flag. This is why
239 * we consider that up to the state validation during command processing, the
240 * command is safe. After that, we can not guarantee the correctness of the
241 * client request vis-a-vis the consumer.
243 static enum consumerd_state ust_consumerd_state
;
244 static enum consumerd_state kernel_consumerd_state
;
246 /* Load session thread information to operate. */
247 static struct load_session_thread_data
*load_info
;
250 * Section name to look for in the daemon configuration file.
252 static const char * const config_section_name
= "sessiond";
254 /* Am I root or not. Set to 1 if the daemon is running as root */
258 * Stop all threads by closing the thread quit pipe.
260 static void stop_threads(void)
264 /* Stopping all threads */
265 DBG("Terminating all threads");
266 ret
= sessiond_notify_quit_pipe();
268 ERR("write error on thread quit pipe");
271 /* Dispatch thread */
272 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
273 futex_nto1_wake(&ust_cmd_queue
.futex
);
277 * Close every consumer sockets.
279 static void close_consumer_sockets(void)
283 if (kconsumer_data
.err_sock
>= 0) {
284 ret
= close(kconsumer_data
.err_sock
);
286 PERROR("kernel consumer err_sock close");
289 if (ustconsumer32_data
.err_sock
>= 0) {
290 ret
= close(ustconsumer32_data
.err_sock
);
292 PERROR("UST consumerd32 err_sock close");
295 if (ustconsumer64_data
.err_sock
>= 0) {
296 ret
= close(ustconsumer64_data
.err_sock
);
298 PERROR("UST consumerd64 err_sock close");
301 if (kconsumer_data
.cmd_sock
>= 0) {
302 ret
= close(kconsumer_data
.cmd_sock
);
304 PERROR("kernel consumer cmd_sock close");
307 if (ustconsumer32_data
.cmd_sock
>= 0) {
308 ret
= close(ustconsumer32_data
.cmd_sock
);
310 PERROR("UST consumerd32 cmd_sock close");
313 if (ustconsumer64_data
.cmd_sock
>= 0) {
314 ret
= close(ustconsumer64_data
.cmd_sock
);
316 PERROR("UST consumerd64 cmd_sock close");
319 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
320 ret
= close(kconsumer_data
.channel_monitor_pipe
);
322 PERROR("kernel consumer channel monitor pipe close");
325 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
326 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
328 PERROR("UST consumerd32 channel monitor pipe close");
331 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
332 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
334 PERROR("UST consumerd64 channel monitor pipe close");
340 * Wait on consumer process termination.
342 * Need to be called with the consumer data lock held or from a context
343 * ensuring no concurrent access to data (e.g: cleanup).
345 static void wait_consumer(struct consumer_data
*consumer_data
)
350 if (consumer_data
->pid
<= 0) {
354 DBG("Waiting for complete teardown of consumerd (PID: %d)",
356 ret
= waitpid(consumer_data
->pid
, &status
, 0);
358 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
359 } else if (!WIFEXITED(status
)) {
360 ERR("consumerd termination with error: %d",
363 consumer_data
->pid
= 0;
367 * Cleanup the session daemon's data structures.
369 static void sessiond_cleanup(void)
372 struct ltt_session_list
*session_list
= session_get_list();
374 DBG("Cleanup sessiond");
377 * Close the thread quit pipe. It has already done its job,
378 * since we are now called.
380 sessiond_close_quit_pipe();
382 ret
= remove(config
.pid_file_path
.value
);
384 PERROR("remove pidfile %s", config
.pid_file_path
.value
);
387 DBG("Removing sessiond and consumerd content of directory %s",
388 config
.rundir
.value
);
391 DBG("Removing %s", config
.pid_file_path
.value
);
392 (void) unlink(config
.pid_file_path
.value
);
394 DBG("Removing %s", config
.agent_port_file_path
.value
);
395 (void) unlink(config
.agent_port_file_path
.value
);
398 DBG("Removing %s", kconsumer_data
.err_unix_sock_path
);
399 (void) unlink(kconsumer_data
.err_unix_sock_path
);
401 DBG("Removing directory %s", config
.kconsumerd_path
.value
);
402 (void) rmdir(config
.kconsumerd_path
.value
);
404 /* ust consumerd 32 */
405 DBG("Removing %s", config
.consumerd32_err_unix_sock_path
.value
);
406 (void) unlink(config
.consumerd32_err_unix_sock_path
.value
);
408 DBG("Removing directory %s", config
.consumerd32_path
.value
);
409 (void) rmdir(config
.consumerd32_path
.value
);
411 /* ust consumerd 64 */
412 DBG("Removing %s", config
.consumerd64_err_unix_sock_path
.value
);
413 (void) unlink(config
.consumerd64_err_unix_sock_path
.value
);
415 DBG("Removing directory %s", config
.consumerd64_path
.value
);
416 (void) rmdir(config
.consumerd64_path
.value
);
418 pthread_mutex_destroy(&session_list
->lock
);
420 wait_consumer(&kconsumer_data
);
421 wait_consumer(&ustconsumer64_data
);
422 wait_consumer(&ustconsumer32_data
);
424 DBG("Cleaning up all agent apps");
425 agent_app_ht_clean();
427 DBG("Closing all UST sockets");
428 ust_app_clean_list();
429 buffer_reg_destroy_registries();
431 if (is_root
&& !config
.no_kernel
) {
432 DBG2("Closing kernel fd");
433 if (kernel_tracer_fd
>= 0) {
434 ret
= close(kernel_tracer_fd
);
439 DBG("Unloading kernel modules");
440 modprobe_remove_lttng_all();
444 close_consumer_sockets();
447 load_session_destroy_data(load_info
);
452 * We do NOT rmdir rundir because there are other processes
453 * using it, for instance lttng-relayd, which can start in
454 * parallel with this teardown.
459 * Cleanup the daemon's option data structures.
461 static void sessiond_cleanup_options(void)
463 DBG("Cleaning up options");
465 sessiond_config_fini(&config
);
467 run_as_destroy_worker();
471 * Send data on a unix socket using the liblttsessiondcomm API.
473 * Return lttcomm error code.
475 static int send_unix_sock(int sock
, void *buf
, size_t len
)
477 /* Check valid length */
482 return lttcomm_send_unix_sock(sock
, buf
, len
);
486 * Free memory of a command context structure.
488 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
490 DBG("Clean command context structure");
492 if ((*cmd_ctx
)->llm
) {
493 free((*cmd_ctx
)->llm
);
495 if ((*cmd_ctx
)->lsm
) {
496 free((*cmd_ctx
)->lsm
);
504 * Notify UST applications using the shm mmap futex.
506 static int notify_ust_apps(int active
)
510 DBG("Notifying applications of session daemon state: %d", active
);
512 /* See shm.c for this call implying mmap, shm and futex calls */
513 wait_shm_mmap
= shm_ust_get_mmap(config
.wait_shm_path
.value
, is_root
);
514 if (wait_shm_mmap
== NULL
) {
518 /* Wake waiting process */
519 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
521 /* Apps notified successfully */
529 * Setup the outgoing data buffer for the response (llm) by allocating the
530 * right amount of memory and copying the original information from the lsm
533 * Return 0 on success, negative value on error.
535 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
536 const void *payload_buf
, size_t payload_len
,
537 const void *cmd_header_buf
, size_t cmd_header_len
)
540 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
541 const size_t cmd_header_offset
= header_len
;
542 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
543 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
545 cmd_ctx
->llm
= zmalloc(total_msg_size
);
547 if (cmd_ctx
->llm
== NULL
) {
553 /* Copy common data */
554 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
555 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
556 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
557 cmd_ctx
->llm
->data_size
= payload_len
;
558 cmd_ctx
->lttng_msg_size
= total_msg_size
;
560 /* Copy command header */
561 if (cmd_header_len
) {
562 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
568 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
577 * Version of setup_lttng_msg() without command header.
579 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
580 void *payload_buf
, size_t payload_len
)
582 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
585 * Update the kernel poll set of all channel fd available over all tracing
586 * session. Add the wakeup pipe at the end of the set.
588 static int update_kernel_poll(struct lttng_poll_event
*events
)
591 struct ltt_kernel_channel
*channel
;
592 struct ltt_session
*session
;
593 const struct ltt_session_list
*session_list
= session_get_list();
595 DBG("Updating kernel poll set");
598 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
599 if (!session_get(session
)) {
602 session_lock(session
);
603 if (session
->kernel_session
== NULL
) {
604 session_unlock(session
);
605 session_put(session
);
609 cds_list_for_each_entry(channel
,
610 &session
->kernel_session
->channel_list
.head
, list
) {
611 /* Add channel fd to the kernel poll set */
612 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
614 session_unlock(session
);
615 session_put(session
);
618 DBG("Channel fd %d added to kernel set", channel
->fd
);
620 session_unlock(session
);
622 session_unlock_list();
627 session_unlock_list();
632 * Find the channel fd from 'fd' over all tracing session. When found, check
633 * for new channel stream and send those stream fds to the kernel consumer.
635 * Useful for CPU hotplug feature.
637 static int update_kernel_stream(int fd
)
640 struct ltt_session
*session
;
641 struct ltt_kernel_session
*ksess
;
642 struct ltt_kernel_channel
*channel
;
643 const struct ltt_session_list
*session_list
= session_get_list();
645 DBG("Updating kernel streams for channel fd %d", fd
);
648 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
649 if (!session_get(session
)) {
652 session_lock(session
);
653 if (session
->kernel_session
== NULL
) {
654 session_unlock(session
);
655 session_put(session
);
658 ksess
= session
->kernel_session
;
660 cds_list_for_each_entry(channel
,
661 &ksess
->channel_list
.head
, list
) {
662 struct lttng_ht_iter iter
;
663 struct consumer_socket
*socket
;
665 if (channel
->fd
!= fd
) {
668 DBG("Channel found, updating kernel streams");
669 ret
= kernel_open_channel_stream(channel
);
673 /* Update the stream global counter */
674 ksess
->stream_count_global
+= ret
;
677 * Have we already sent fds to the consumer? If yes, it
678 * means that tracing is started so it is safe to send
679 * our updated stream fds.
681 if (ksess
->consumer_fds_sent
!= 1
682 || ksess
->consumer
== NULL
) {
688 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
689 &iter
.iter
, socket
, node
.node
) {
690 pthread_mutex_lock(socket
->lock
);
691 ret
= kernel_consumer_send_channel_streams(socket
,
693 session
->output_traces
? 1 : 0);
694 pthread_mutex_unlock(socket
->lock
);
702 session_unlock(session
);
703 session_put(session
);
705 session_unlock_list();
709 session_unlock(session
);
710 session_put(session
);
711 session_unlock_list();
716 * For each tracing session, update newly registered apps. The session list
717 * lock MUST be acquired before calling this.
719 static void update_ust_app(int app_sock
)
721 struct ltt_session
*sess
, *stmp
;
722 const struct ltt_session_list
*session_list
= session_get_list();
724 /* Consumer is in an ERROR state. Stop any application update. */
725 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
726 /* Stop the update process since the consumer is dead. */
730 /* For all tracing session(s) */
731 cds_list_for_each_entry_safe(sess
, stmp
, &session_list
->head
, list
) {
734 if (!session_get(sess
)) {
738 if (!sess
->ust_session
) {
743 assert(app_sock
>= 0);
744 app
= ust_app_find_by_sock(app_sock
);
747 * Application can be unregistered before so
748 * this is possible hence simply stopping the
751 DBG3("UST app update failed to find app sock %d",
755 ust_app_global_update(sess
->ust_session
, app
);
759 session_unlock(sess
);
765 * This thread manage event coming from the kernel.
767 * Features supported in this thread:
770 static void *thread_manage_kernel(void *data
)
772 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
773 uint32_t revents
, nb_fd
;
775 struct lttng_poll_event events
;
777 DBG("[thread] Thread manage kernel started");
779 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
782 * This first step of the while is to clean this structure which could free
783 * non NULL pointers so initialize it before the loop.
785 lttng_poll_init(&events
);
787 if (testpoint(sessiond_thread_manage_kernel
)) {
788 goto error_testpoint
;
791 health_code_update();
793 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
794 goto error_testpoint
;
798 health_code_update();
800 if (update_poll_flag
== 1) {
801 /* Clean events object. We are about to populate it again. */
802 lttng_poll_clean(&events
);
804 ret
= sessiond_set_thread_pollset(&events
, 2);
806 goto error_poll_create
;
809 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
814 /* This will add the available kernel channel if any. */
815 ret
= update_kernel_poll(&events
);
819 update_poll_flag
= 0;
822 DBG("Thread kernel polling");
824 /* Poll infinite value of time */
827 ret
= lttng_poll_wait(&events
, -1);
828 DBG("Thread kernel return from poll on %d fds",
829 LTTNG_POLL_GETNB(&events
));
833 * Restart interrupted system call.
835 if (errno
== EINTR
) {
839 } else if (ret
== 0) {
840 /* Should not happen since timeout is infinite */
841 ERR("Return value of poll is 0 with an infinite timeout.\n"
842 "This should not have happened! Continuing...");
848 for (i
= 0; i
< nb_fd
; i
++) {
849 /* Fetch once the poll data */
850 revents
= LTTNG_POLL_GETEV(&events
, i
);
851 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
853 health_code_update();
856 /* No activity for this FD (poll implementation). */
860 /* Thread quit pipe has been closed. Killing thread. */
861 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
867 /* Check for data on kernel pipe */
868 if (revents
& LPOLLIN
) {
869 if (pollfd
== kernel_poll_pipe
[0]) {
870 (void) lttng_read(kernel_poll_pipe
[0],
873 * Ret value is useless here, if this pipe gets any actions an
874 * update is required anyway.
876 update_poll_flag
= 1;
880 * New CPU detected by the kernel. Adding kernel stream to
881 * kernel session and updating the kernel consumer
883 ret
= update_kernel_stream(pollfd
);
889 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
890 update_poll_flag
= 1;
893 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
901 lttng_poll_clean(&events
);
904 utils_close_pipe(kernel_poll_pipe
);
905 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
908 ERR("Health error occurred in %s", __func__
);
909 WARN("Kernel thread died unexpectedly. "
910 "Kernel tracing can continue but CPU hotplug is disabled.");
912 health_unregister(health_sessiond
);
913 DBG("Kernel thread dying");
918 * Signal pthread condition of the consumer data that the thread.
920 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
922 pthread_mutex_lock(&data
->cond_mutex
);
925 * The state is set before signaling. It can be any value, it's the waiter
926 * job to correctly interpret this condition variable associated to the
927 * consumer pthread_cond.
929 * A value of 0 means that the corresponding thread of the consumer data
930 * was not started. 1 indicates that the thread has started and is ready
931 * for action. A negative value means that there was an error during the
934 data
->consumer_thread_is_ready
= state
;
935 (void) pthread_cond_signal(&data
->cond
);
937 pthread_mutex_unlock(&data
->cond_mutex
);
941 * This thread manage the consumer error sent back to the session daemon.
943 static void *thread_manage_consumer(void *data
)
945 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
946 uint32_t revents
, nb_fd
;
947 enum lttcomm_return_code code
;
948 struct lttng_poll_event events
;
949 struct consumer_data
*consumer_data
= data
;
950 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
952 DBG("[thread] Manage consumer started");
954 rcu_register_thread();
957 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
959 health_code_update();
962 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
963 * metadata_sock. Nothing more will be added to this poll set.
965 ret
= sessiond_set_thread_pollset(&events
, 3);
971 * The error socket here is already in a listening state which was done
972 * just before spawning this thread to avoid a race between the consumer
973 * daemon exec trying to connect and the listen() call.
975 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
980 health_code_update();
982 /* Infinite blocking call, waiting for transmission */
986 if (testpoint(sessiond_thread_manage_consumer
)) {
990 ret
= lttng_poll_wait(&events
, -1);
994 * Restart interrupted system call.
996 if (errno
== EINTR
) {
1004 for (i
= 0; i
< nb_fd
; i
++) {
1005 /* Fetch once the poll data */
1006 revents
= LTTNG_POLL_GETEV(&events
, i
);
1007 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1009 health_code_update();
1012 /* No activity for this FD (poll implementation). */
1016 /* Thread quit pipe has been closed. Killing thread. */
1017 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1023 /* Event on the registration socket */
1024 if (pollfd
== consumer_data
->err_sock
) {
1025 if (revents
& LPOLLIN
) {
1027 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1028 ERR("consumer err socket poll error");
1031 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1037 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1043 * Set the CLOEXEC flag. Return code is useless because either way, the
1046 (void) utils_set_fd_cloexec(sock
);
1048 health_code_update();
1050 DBG2("Receiving code from consumer err_sock");
1052 /* Getting status code from kconsumerd */
1053 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1054 sizeof(enum lttcomm_return_code
));
1059 health_code_update();
1060 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1061 ERR("consumer error when waiting for SOCK_READY : %s",
1062 lttcomm_get_readable_code(-code
));
1066 /* Connect both command and metadata sockets. */
1067 consumer_data
->cmd_sock
=
1068 lttcomm_connect_unix_sock(
1069 consumer_data
->cmd_unix_sock_path
);
1070 consumer_data
->metadata_fd
=
1071 lttcomm_connect_unix_sock(
1072 consumer_data
->cmd_unix_sock_path
);
1073 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1074 PERROR("consumer connect cmd socket");
1075 /* On error, signal condition and quit. */
1076 signal_consumer_condition(consumer_data
, -1);
1080 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1082 /* Create metadata socket lock. */
1083 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1084 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1085 PERROR("zmalloc pthread mutex");
1088 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1090 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1091 DBG("Consumer metadata socket ready (fd: %d)",
1092 consumer_data
->metadata_fd
);
1095 * Remove the consumerd error sock since we've established a connection.
1097 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1102 /* Add new accepted error socket. */
1103 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1108 /* Add metadata socket that is successfully connected. */
1109 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1110 LPOLLIN
| LPOLLRDHUP
);
1115 health_code_update();
1118 * Transfer the write-end of the channel monitoring and rotate pipe
1119 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE command.
1121 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1122 if (!cmd_socket_wrapper
) {
1125 cmd_socket_wrapper
->lock
= &consumer_data
->lock
;
1127 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1128 consumer_data
->channel_monitor_pipe
);
1133 /* Discard the socket wrapper as it is no longer needed. */
1134 consumer_destroy_socket(cmd_socket_wrapper
);
1135 cmd_socket_wrapper
= NULL
;
1137 /* The thread is completely initialized, signal that it is ready. */
1138 signal_consumer_condition(consumer_data
, 1);
1140 /* Infinite blocking call, waiting for transmission */
1143 health_code_update();
1145 /* Exit the thread because the thread quit pipe has been triggered. */
1147 /* Not a health error. */
1152 health_poll_entry();
1153 ret
= lttng_poll_wait(&events
, -1);
1157 * Restart interrupted system call.
1159 if (errno
== EINTR
) {
1167 for (i
= 0; i
< nb_fd
; i
++) {
1168 /* Fetch once the poll data */
1169 revents
= LTTNG_POLL_GETEV(&events
, i
);
1170 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1172 health_code_update();
1175 /* No activity for this FD (poll implementation). */
1180 * Thread quit pipe has been triggered, flag that we should stop
1181 * but continue the current loop to handle potential data from
1184 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1186 if (pollfd
== sock
) {
1187 /* Event on the consumerd socket */
1188 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1189 && !(revents
& LPOLLIN
)) {
1190 ERR("consumer err socket second poll error");
1193 health_code_update();
1194 /* Wait for any kconsumerd error */
1195 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1196 sizeof(enum lttcomm_return_code
));
1198 ERR("consumer closed the command socket");
1202 ERR("consumer return code : %s",
1203 lttcomm_get_readable_code(-code
));
1206 } else if (pollfd
== consumer_data
->metadata_fd
) {
1207 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1208 && !(revents
& LPOLLIN
)) {
1209 ERR("consumer err metadata socket second poll error");
1212 /* UST metadata requests */
1213 ret
= ust_consumer_metadata_request(
1214 &consumer_data
->metadata_sock
);
1216 ERR("Handling metadata request");
1220 /* No need for an else branch all FDs are tested prior. */
1222 health_code_update();
1228 * We lock here because we are about to close the sockets and some other
1229 * thread might be using them so get exclusive access which will abort all
1230 * other consumer command by other threads.
1232 pthread_mutex_lock(&consumer_data
->lock
);
1234 /* Immediately set the consumerd state to stopped */
1235 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1236 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1237 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1238 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1239 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1241 /* Code flow error... */
1245 if (consumer_data
->err_sock
>= 0) {
1246 ret
= close(consumer_data
->err_sock
);
1250 consumer_data
->err_sock
= -1;
1252 if (consumer_data
->cmd_sock
>= 0) {
1253 ret
= close(consumer_data
->cmd_sock
);
1257 consumer_data
->cmd_sock
= -1;
1259 if (consumer_data
->metadata_sock
.fd_ptr
&&
1260 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1261 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1273 unlink(consumer_data
->err_unix_sock_path
);
1274 unlink(consumer_data
->cmd_unix_sock_path
);
1275 pthread_mutex_unlock(&consumer_data
->lock
);
1277 /* Cleanup metadata socket mutex. */
1278 if (consumer_data
->metadata_sock
.lock
) {
1279 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1280 free(consumer_data
->metadata_sock
.lock
);
1282 lttng_poll_clean(&events
);
1284 if (cmd_socket_wrapper
) {
1285 consumer_destroy_socket(cmd_socket_wrapper
);
1290 ERR("Health error occurred in %s", __func__
);
1292 health_unregister(health_sessiond
);
1293 DBG("consumer thread cleanup completed");
1295 rcu_thread_offline();
1296 rcu_unregister_thread();
1302 * This thread receives application command sockets (FDs) on the
1303 * apps_cmd_pipe and waits (polls) on them until they are closed
1304 * or an error occurs.
1306 * At that point, it flushes the data (tracing and metadata) associated
1307 * with this application and tears down ust app sessions and other
1308 * associated data structures through ust_app_unregister().
1310 * Note that this thread never sends commands to the applications
1311 * through the command sockets; it merely listens for hang-ups
1312 * and errors on those sockets and cleans-up as they occur.
1314 static void *thread_manage_apps(void *data
)
1316 int i
, ret
, pollfd
, err
= -1;
1318 uint32_t revents
, nb_fd
;
1319 struct lttng_poll_event events
;
1321 DBG("[thread] Manage application started");
1323 rcu_register_thread();
1324 rcu_thread_online();
1326 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1328 if (testpoint(sessiond_thread_manage_apps
)) {
1329 goto error_testpoint
;
1332 health_code_update();
1334 ret
= sessiond_set_thread_pollset(&events
, 2);
1336 goto error_poll_create
;
1339 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1344 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1348 health_code_update();
1351 DBG("Apps thread polling");
1353 /* Inifinite blocking call, waiting for transmission */
1355 health_poll_entry();
1356 ret
= lttng_poll_wait(&events
, -1);
1357 DBG("Apps thread return from poll on %d fds",
1358 LTTNG_POLL_GETNB(&events
));
1362 * Restart interrupted system call.
1364 if (errno
== EINTR
) {
1372 for (i
= 0; i
< nb_fd
; i
++) {
1373 /* Fetch once the poll data */
1374 revents
= LTTNG_POLL_GETEV(&events
, i
);
1375 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1377 health_code_update();
1380 /* No activity for this FD (poll implementation). */
1384 /* Thread quit pipe has been closed. Killing thread. */
1385 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1391 /* Inspect the apps cmd pipe */
1392 if (pollfd
== apps_cmd_pipe
[0]) {
1393 if (revents
& LPOLLIN
) {
1397 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1398 if (size_ret
< sizeof(sock
)) {
1399 PERROR("read apps cmd pipe");
1403 health_code_update();
1406 * Since this is a command socket (write then read),
1407 * we only monitor the error events of the socket.
1409 ret
= lttng_poll_add(&events
, sock
,
1410 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1415 DBG("Apps with sock %d added to poll set", sock
);
1416 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1417 ERR("Apps command pipe error");
1420 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1425 * At this point, we know that a registered application made
1426 * the event at poll_wait.
1428 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1429 /* Removing from the poll set */
1430 ret
= lttng_poll_del(&events
, pollfd
);
1435 /* Socket closed on remote end. */
1436 ust_app_unregister(pollfd
);
1438 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1443 health_code_update();
1449 lttng_poll_clean(&events
);
1452 utils_close_pipe(apps_cmd_pipe
);
1453 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1456 * We don't clean the UST app hash table here since already registered
1457 * applications can still be controlled so let them be until the session
1458 * daemon dies or the applications stop.
1463 ERR("Health error occurred in %s", __func__
);
1465 health_unregister(health_sessiond
);
1466 DBG("Application communication apps thread cleanup complete");
1467 rcu_thread_offline();
1468 rcu_unregister_thread();
1473 * Send a socket to a thread This is called from the dispatch UST registration
1474 * thread once all sockets are set for the application.
1476 * The sock value can be invalid, we don't really care, the thread will handle
1477 * it and make the necessary cleanup if so.
1479 * On success, return 0 else a negative value being the errno message of the
1482 static int send_socket_to_thread(int fd
, int sock
)
1487 * It's possible that the FD is set as invalid with -1 concurrently just
1488 * before calling this function being a shutdown state of the thread.
1495 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1496 if (ret
< sizeof(sock
)) {
1497 PERROR("write apps pipe %d", fd
);
1504 /* All good. Don't send back the write positive ret value. */
1511 * Sanitize the wait queue of the dispatch registration thread meaning removing
1512 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1513 * notify socket is never received.
1515 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1517 int ret
, nb_fd
= 0, i
;
1518 unsigned int fd_added
= 0;
1519 struct lttng_poll_event events
;
1520 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1524 lttng_poll_init(&events
);
1526 /* Just skip everything for an empty queue. */
1527 if (!wait_queue
->count
) {
1531 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1536 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1537 &wait_queue
->head
, head
) {
1538 assert(wait_node
->app
);
1539 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1540 LPOLLHUP
| LPOLLERR
);
1553 * Poll but don't block so we can quickly identify the faulty events and
1554 * clean them afterwards from the wait queue.
1556 ret
= lttng_poll_wait(&events
, 0);
1562 for (i
= 0; i
< nb_fd
; i
++) {
1563 /* Get faulty FD. */
1564 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1565 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1568 /* No activity for this FD (poll implementation). */
1572 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1573 &wait_queue
->head
, head
) {
1574 if (pollfd
== wait_node
->app
->sock
&&
1575 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1576 cds_list_del(&wait_node
->head
);
1577 wait_queue
->count
--;
1578 ust_app_destroy(wait_node
->app
);
1581 * Silence warning of use-after-free in
1582 * cds_list_for_each_entry_safe which uses
1583 * __typeof__(*wait_node).
1588 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1595 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1599 lttng_poll_clean(&events
);
1603 lttng_poll_clean(&events
);
1605 ERR("Unable to sanitize wait queue");
1610 * Dispatch request from the registration threads to the application
1611 * communication thread.
1613 static void *thread_dispatch_ust_registration(void *data
)
1616 struct cds_wfcq_node
*node
;
1617 struct ust_command
*ust_cmd
= NULL
;
1618 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1619 struct ust_reg_wait_queue wait_queue
= {
1623 rcu_register_thread();
1625 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1627 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1628 goto error_testpoint
;
1631 health_code_update();
1633 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1635 DBG("[thread] Dispatch UST command started");
1638 health_code_update();
1640 /* Atomically prepare the queue futex */
1641 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1643 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1648 struct ust_app
*app
= NULL
;
1652 * Make sure we don't have node(s) that have hung up before receiving
1653 * the notify socket. This is to clean the list in order to avoid
1654 * memory leaks from notify socket that are never seen.
1656 sanitize_wait_queue(&wait_queue
);
1658 health_code_update();
1659 /* Dequeue command for registration */
1660 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1662 DBG("Woken up but nothing in the UST command queue");
1663 /* Continue thread execution */
1667 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1669 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1670 " gid:%d sock:%d name:%s (version %d.%d)",
1671 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1672 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1673 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1674 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1676 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1677 wait_node
= zmalloc(sizeof(*wait_node
));
1679 PERROR("zmalloc wait_node dispatch");
1680 ret
= close(ust_cmd
->sock
);
1682 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1684 lttng_fd_put(LTTNG_FD_APPS
, 1);
1688 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1690 /* Create application object if socket is CMD. */
1691 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1693 if (!wait_node
->app
) {
1694 ret
= close(ust_cmd
->sock
);
1696 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1698 lttng_fd_put(LTTNG_FD_APPS
, 1);
1704 * Add application to the wait queue so we can set the notify
1705 * socket before putting this object in the global ht.
1707 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1712 * We have to continue here since we don't have the notify
1713 * socket and the application MUST be added to the hash table
1714 * only at that moment.
1719 * Look for the application in the local wait queue and set the
1720 * notify socket if found.
1722 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1723 &wait_queue
.head
, head
) {
1724 health_code_update();
1725 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1726 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1727 cds_list_del(&wait_node
->head
);
1729 app
= wait_node
->app
;
1731 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1737 * With no application at this stage the received socket is
1738 * basically useless so close it before we free the cmd data
1739 * structure for good.
1742 ret
= close(ust_cmd
->sock
);
1744 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1746 lttng_fd_put(LTTNG_FD_APPS
, 1);
1753 * @session_lock_list
1755 * Lock the global session list so from the register up to the
1756 * registration done message, no thread can see the application
1757 * and change its state.
1759 session_lock_list();
1763 * Add application to the global hash table. This needs to be
1764 * done before the update to the UST registry can locate the
1769 /* Set app version. This call will print an error if needed. */
1770 (void) ust_app_version(app
);
1772 /* Send notify socket through the notify pipe. */
1773 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1777 session_unlock_list();
1779 * No notify thread, stop the UST tracing. However, this is
1780 * not an internal error of the this thread thus setting
1781 * the health error code to a normal exit.
1788 * Update newly registered application with the tracing
1789 * registry info already enabled information.
1791 update_ust_app(app
->sock
);
1794 * Don't care about return value. Let the manage apps threads
1795 * handle app unregistration upon socket close.
1797 (void) ust_app_register_done(app
);
1800 * Even if the application socket has been closed, send the app
1801 * to the thread and unregistration will take place at that
1804 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1807 session_unlock_list();
1809 * No apps. thread, stop the UST tracing. However, this is
1810 * not an internal error of the this thread thus setting
1811 * the health error code to a normal exit.
1818 session_unlock_list();
1820 } while (node
!= NULL
);
1822 health_poll_entry();
1823 /* Futex wait on queue. Blocking call on futex() */
1824 futex_nto1_wait(&ust_cmd_queue
.futex
);
1827 /* Normal exit, no error */
1831 /* Clean up wait queue. */
1832 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1833 &wait_queue
.head
, head
) {
1834 cds_list_del(&wait_node
->head
);
1839 /* Empty command queue. */
1841 /* Dequeue command for registration */
1842 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1846 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1847 ret
= close(ust_cmd
->sock
);
1849 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
1851 lttng_fd_put(LTTNG_FD_APPS
, 1);
1856 DBG("Dispatch thread dying");
1859 ERR("Health error occurred in %s", __func__
);
1861 health_unregister(health_sessiond
);
1862 rcu_unregister_thread();
1867 * This thread manage application registration.
1869 static void *thread_registration_apps(void *data
)
1871 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1872 uint32_t revents
, nb_fd
;
1873 struct lttng_poll_event events
;
1875 * Get allocated in this thread, enqueued to a global queue, dequeued and
1876 * freed in the manage apps thread.
1878 struct ust_command
*ust_cmd
= NULL
;
1880 DBG("[thread] Manage application registration started");
1882 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1884 if (testpoint(sessiond_thread_registration_apps
)) {
1885 goto error_testpoint
;
1888 ret
= lttcomm_listen_unix_sock(apps_sock
);
1894 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1895 * more will be added to this poll set.
1897 ret
= sessiond_set_thread_pollset(&events
, 2);
1899 goto error_create_poll
;
1902 /* Add the application registration socket */
1903 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1905 goto error_poll_add
;
1908 /* Notify all applications to register */
1909 ret
= notify_ust_apps(1);
1911 ERR("Failed to notify applications or create the wait shared memory.\n"
1912 "Execution continues but there might be problem for already\n"
1913 "running applications that wishes to register.");
1917 DBG("Accepting application registration");
1919 /* Inifinite blocking call, waiting for transmission */
1921 health_poll_entry();
1922 ret
= lttng_poll_wait(&events
, -1);
1926 * Restart interrupted system call.
1928 if (errno
== EINTR
) {
1936 for (i
= 0; i
< nb_fd
; i
++) {
1937 health_code_update();
1939 /* Fetch once the poll data */
1940 revents
= LTTNG_POLL_GETEV(&events
, i
);
1941 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1944 /* No activity for this FD (poll implementation). */
1948 /* Thread quit pipe has been closed. Killing thread. */
1949 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1955 /* Event on the registration socket */
1956 if (pollfd
== apps_sock
) {
1957 if (revents
& LPOLLIN
) {
1958 sock
= lttcomm_accept_unix_sock(apps_sock
);
1964 * Set socket timeout for both receiving and ending.
1965 * app_socket_timeout is in seconds, whereas
1966 * lttcomm_setsockopt_rcv_timeout and
1967 * lttcomm_setsockopt_snd_timeout expect msec as
1970 if (config
.app_socket_timeout
>= 0) {
1971 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1972 config
.app_socket_timeout
* 1000);
1973 (void) lttcomm_setsockopt_snd_timeout(sock
,
1974 config
.app_socket_timeout
* 1000);
1978 * Set the CLOEXEC flag. Return code is useless because
1979 * either way, the show must go on.
1981 (void) utils_set_fd_cloexec(sock
);
1983 /* Create UST registration command for enqueuing */
1984 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1985 if (ust_cmd
== NULL
) {
1986 PERROR("ust command zmalloc");
1995 * Using message-based transmissions to ensure we don't
1996 * have to deal with partially received messages.
1998 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2000 ERR("Exhausted file descriptors allowed for applications.");
2010 health_code_update();
2011 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2014 /* Close socket of the application. */
2019 lttng_fd_put(LTTNG_FD_APPS
, 1);
2023 health_code_update();
2025 ust_cmd
->sock
= sock
;
2028 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2029 " gid:%d sock:%d name:%s (version %d.%d)",
2030 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2031 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2032 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2033 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2036 * Lock free enqueue the registration request. The red pill
2037 * has been taken! This apps will be part of the *system*.
2039 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2042 * Wake the registration queue futex. Implicit memory
2043 * barrier with the exchange in cds_wfcq_enqueue.
2045 futex_nto1_wake(&ust_cmd_queue
.futex
);
2046 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2047 ERR("Register apps socket poll error");
2050 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2059 /* Notify that the registration thread is gone */
2062 if (apps_sock
>= 0) {
2063 ret
= close(apps_sock
);
2073 lttng_fd_put(LTTNG_FD_APPS
, 1);
2075 unlink(config
.apps_unix_sock_path
.value
);
2078 lttng_poll_clean(&events
);
2082 DBG("UST Registration thread cleanup complete");
2085 ERR("Health error occurred in %s", __func__
);
2087 health_unregister(health_sessiond
);
2093 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2094 * exec or it will fails.
2096 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2099 struct timespec timeout
;
2102 * Make sure we set the readiness flag to 0 because we are NOT ready.
2103 * This access to consumer_thread_is_ready does not need to be
2104 * protected by consumer_data.cond_mutex (yet) since the consumer
2105 * management thread has not been started at this point.
2107 consumer_data
->consumer_thread_is_ready
= 0;
2109 /* Setup pthread condition */
2110 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2113 PERROR("pthread_condattr_init consumer data");
2118 * Set the monotonic clock in order to make sure we DO NOT jump in time
2119 * between the clock_gettime() call and the timedwait call. See bug #324
2120 * for a more details and how we noticed it.
2122 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2125 PERROR("pthread_condattr_setclock consumer data");
2129 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2132 PERROR("pthread_cond_init consumer data");
2136 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2137 thread_manage_consumer
, consumer_data
);
2140 PERROR("pthread_create consumer");
2145 /* We are about to wait on a pthread condition */
2146 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2148 /* Get time for sem_timedwait absolute timeout */
2149 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2151 * Set the timeout for the condition timed wait even if the clock gettime
2152 * call fails since we might loop on that call and we want to avoid to
2153 * increment the timeout too many times.
2155 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2158 * The following loop COULD be skipped in some conditions so this is why we
2159 * set ret to 0 in order to make sure at least one round of the loop is
2165 * Loop until the condition is reached or when a timeout is reached. Note
2166 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2167 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2168 * possible. This loop does not take any chances and works with both of
2171 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2172 if (clock_ret
< 0) {
2173 PERROR("clock_gettime spawn consumer");
2174 /* Infinite wait for the consumerd thread to be ready */
2175 ret
= pthread_cond_wait(&consumer_data
->cond
,
2176 &consumer_data
->cond_mutex
);
2178 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2179 &consumer_data
->cond_mutex
, &timeout
);
2183 /* Release the pthread condition */
2184 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2188 if (ret
== ETIMEDOUT
) {
2192 * Call has timed out so we kill the kconsumerd_thread and return
2195 ERR("Condition timed out. The consumer thread was never ready."
2197 pth_ret
= pthread_cancel(consumer_data
->thread
);
2199 PERROR("pthread_cancel consumer thread");
2202 PERROR("pthread_cond_wait failed consumer thread");
2204 /* Caller is expecting a negative value on failure. */
2209 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2210 if (consumer_data
->pid
== 0) {
2211 ERR("Consumerd did not start");
2212 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2215 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2224 * Join consumer thread
2226 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2230 /* Consumer pid must be a real one. */
2231 if (consumer_data
->pid
> 0) {
2233 ret
= kill(consumer_data
->pid
, SIGTERM
);
2235 PERROR("Error killing consumer daemon");
2238 return pthread_join(consumer_data
->thread
, &status
);
2245 * Fork and exec a consumer daemon (consumerd).
2247 * Return pid if successful else -1.
2249 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2253 const char *consumer_to_use
;
2254 const char *verbosity
;
2257 DBG("Spawning consumerd");
2264 if (config
.verbose_consumer
) {
2265 verbosity
= "--verbose";
2266 } else if (lttng_opt_quiet
) {
2267 verbosity
= "--quiet";
2272 switch (consumer_data
->type
) {
2273 case LTTNG_CONSUMER_KERNEL
:
2275 * Find out which consumerd to execute. We will first try the
2276 * 64-bit path, then the sessiond's installation directory, and
2277 * fallback on the 32-bit one,
2279 DBG3("Looking for a kernel consumer at these locations:");
2280 DBG3(" 1) %s", config
.consumerd64_bin_path
.value
? : "NULL");
2281 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, DEFAULT_CONSUMERD_FILE
);
2282 DBG3(" 3) %s", config
.consumerd32_bin_path
.value
? : "NULL");
2283 if (stat(config
.consumerd64_bin_path
.value
, &st
) == 0) {
2284 DBG3("Found location #1");
2285 consumer_to_use
= config
.consumerd64_bin_path
.value
;
2286 } else if (stat(INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
, &st
) == 0) {
2287 DBG3("Found location #2");
2288 consumer_to_use
= INSTALL_BIN_PATH
"/" DEFAULT_CONSUMERD_FILE
;
2289 } else if (config
.consumerd32_bin_path
.value
&&
2290 stat(config
.consumerd32_bin_path
.value
, &st
) == 0) {
2291 DBG3("Found location #3");
2292 consumer_to_use
= config
.consumerd32_bin_path
.value
;
2294 DBG("Could not find any valid consumerd executable");
2298 DBG("Using kernel consumer at: %s", consumer_to_use
);
2299 (void) execl(consumer_to_use
,
2300 "lttng-consumerd", verbosity
, "-k",
2301 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2302 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2303 "--group", config
.tracing_group_name
.value
,
2306 case LTTNG_CONSUMER64_UST
:
2308 if (config
.consumerd64_lib_dir
.value
) {
2313 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2317 tmplen
= strlen(config
.consumerd64_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2318 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2323 strcat(tmpnew
, config
.consumerd64_lib_dir
.value
);
2324 if (tmp
[0] != '\0') {
2325 strcat(tmpnew
, ":");
2326 strcat(tmpnew
, tmp
);
2328 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2335 DBG("Using 64-bit UST consumer at: %s", config
.consumerd64_bin_path
.value
);
2336 (void) execl(config
.consumerd64_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2337 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2338 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2339 "--group", config
.tracing_group_name
.value
,
2343 case LTTNG_CONSUMER32_UST
:
2345 if (config
.consumerd32_lib_dir
.value
) {
2350 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2354 tmplen
= strlen(config
.consumerd32_lib_dir
.value
) + 1 /* : */ + strlen(tmp
);
2355 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2360 strcat(tmpnew
, config
.consumerd32_lib_dir
.value
);
2361 if (tmp
[0] != '\0') {
2362 strcat(tmpnew
, ":");
2363 strcat(tmpnew
, tmp
);
2365 ret
= setenv("LD_LIBRARY_PATH", tmpnew
, 1);
2372 DBG("Using 32-bit UST consumer at: %s", config
.consumerd32_bin_path
.value
);
2373 (void) execl(config
.consumerd32_bin_path
.value
, "lttng-consumerd", verbosity
, "-u",
2374 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2375 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2376 "--group", config
.tracing_group_name
.value
,
2381 ERR("unknown consumer type");
2385 PERROR("Consumer execl()");
2387 /* Reaching this point, we got a failure on our execl(). */
2389 } else if (pid
> 0) {
2392 PERROR("start consumer fork");
2400 * Spawn the consumerd daemon and session daemon thread.
2402 static int start_consumerd(struct consumer_data
*consumer_data
)
2407 * Set the listen() state on the socket since there is a possible race
2408 * between the exec() of the consumer daemon and this call if place in the
2409 * consumer thread. See bug #366 for more details.
2411 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2416 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2417 if (consumer_data
->pid
!= 0) {
2418 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2422 ret
= spawn_consumerd(consumer_data
);
2424 ERR("Spawning consumerd failed");
2425 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2429 /* Setting up the consumer_data pid */
2430 consumer_data
->pid
= ret
;
2431 DBG2("Consumer pid %d", consumer_data
->pid
);
2432 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2434 DBG2("Spawning consumer control thread");
2435 ret
= spawn_consumer_thread(consumer_data
);
2437 ERR("Fatal error spawning consumer control thread");
2445 /* Cleanup already created sockets on error. */
2446 if (consumer_data
->err_sock
>= 0) {
2449 err
= close(consumer_data
->err_sock
);
2451 PERROR("close consumer data error socket");
2458 * Setup necessary data for kernel tracer action.
2460 static int init_kernel_tracer(void)
2464 /* Modprobe lttng kernel modules */
2465 ret
= modprobe_lttng_control();
2470 /* Open debugfs lttng */
2471 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2472 if (kernel_tracer_fd
< 0) {
2473 DBG("Failed to open %s", module_proc_lttng
);
2477 /* Validate kernel version */
2478 ret
= kernel_validate_version(kernel_tracer_fd
, &kernel_tracer_version
,
2479 &kernel_tracer_abi_version
);
2484 ret
= modprobe_lttng_data();
2489 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2496 WARN("Kernel tracer does not support buffer monitoring. "
2497 "The monitoring timer of channels in the kernel domain "
2498 "will be set to 0 (disabled).");
2501 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2505 modprobe_remove_lttng_control();
2506 ret
= close(kernel_tracer_fd
);
2510 kernel_tracer_fd
= -1;
2511 return LTTNG_ERR_KERN_VERSION
;
2514 ret
= close(kernel_tracer_fd
);
2520 modprobe_remove_lttng_control();
2523 WARN("No kernel tracer available");
2524 kernel_tracer_fd
= -1;
2526 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2528 return LTTNG_ERR_KERN_NA
;
2534 * Copy consumer output from the tracing session to the domain session. The
2535 * function also applies the right modification on a per domain basis for the
2536 * trace files destination directory.
2538 * Should *NOT* be called with RCU read-side lock held.
2540 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2543 const char *dir_name
;
2544 struct consumer_output
*consumer
;
2547 assert(session
->consumer
);
2550 case LTTNG_DOMAIN_KERNEL
:
2551 DBG3("Copying tracing session consumer output in kernel session");
2553 * XXX: We should audit the session creation and what this function
2554 * does "extra" in order to avoid a destroy since this function is used
2555 * in the domain session creation (kernel and ust) only. Same for UST
2558 if (session
->kernel_session
->consumer
) {
2559 consumer_output_put(session
->kernel_session
->consumer
);
2561 session
->kernel_session
->consumer
=
2562 consumer_copy_output(session
->consumer
);
2563 /* Ease our life a bit for the next part */
2564 consumer
= session
->kernel_session
->consumer
;
2565 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2567 case LTTNG_DOMAIN_JUL
:
2568 case LTTNG_DOMAIN_LOG4J
:
2569 case LTTNG_DOMAIN_PYTHON
:
2570 case LTTNG_DOMAIN_UST
:
2571 DBG3("Copying tracing session consumer output in UST session");
2572 if (session
->ust_session
->consumer
) {
2573 consumer_output_put(session
->ust_session
->consumer
);
2575 session
->ust_session
->consumer
=
2576 consumer_copy_output(session
->consumer
);
2577 /* Ease our life a bit for the next part */
2578 consumer
= session
->ust_session
->consumer
;
2579 dir_name
= DEFAULT_UST_TRACE_DIR
;
2582 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2586 /* Append correct directory to subdir */
2587 strncat(consumer
->subdir
, dir_name
,
2588 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2589 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2598 * Create an UST session and add it to the session ust list.
2600 * Should *NOT* be called with RCU read-side lock held.
2602 static int create_ust_session(struct ltt_session
*session
,
2603 struct lttng_domain
*domain
)
2606 struct ltt_ust_session
*lus
= NULL
;
2610 assert(session
->consumer
);
2612 switch (domain
->type
) {
2613 case LTTNG_DOMAIN_JUL
:
2614 case LTTNG_DOMAIN_LOG4J
:
2615 case LTTNG_DOMAIN_PYTHON
:
2616 case LTTNG_DOMAIN_UST
:
2619 ERR("Unknown UST domain on create session %d", domain
->type
);
2620 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2624 DBG("Creating UST session");
2626 lus
= trace_ust_create_session(session
->id
);
2628 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2632 lus
->uid
= session
->uid
;
2633 lus
->gid
= session
->gid
;
2634 lus
->output_traces
= session
->output_traces
;
2635 lus
->snapshot_mode
= session
->snapshot_mode
;
2636 lus
->live_timer_interval
= session
->live_timer
;
2637 session
->ust_session
= lus
;
2638 if (session
->shm_path
[0]) {
2639 strncpy(lus
->root_shm_path
, session
->shm_path
,
2640 sizeof(lus
->root_shm_path
));
2641 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2642 strncpy(lus
->shm_path
, session
->shm_path
,
2643 sizeof(lus
->shm_path
));
2644 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2645 strncat(lus
->shm_path
, "/ust",
2646 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2648 /* Copy session output to the newly created UST session */
2649 ret
= copy_session_consumer(domain
->type
, session
);
2650 if (ret
!= LTTNG_OK
) {
2658 session
->ust_session
= NULL
;
2663 * Create a kernel tracer session then create the default channel.
2665 static int create_kernel_session(struct ltt_session
*session
)
2669 DBG("Creating kernel session");
2671 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2673 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2677 /* Code flow safety */
2678 assert(session
->kernel_session
);
2680 /* Copy session output to the newly created Kernel session */
2681 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2682 if (ret
!= LTTNG_OK
) {
2686 session
->kernel_session
->uid
= session
->uid
;
2687 session
->kernel_session
->gid
= session
->gid
;
2688 session
->kernel_session
->output_traces
= session
->output_traces
;
2689 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2694 trace_kernel_destroy_session(session
->kernel_session
);
2695 session
->kernel_session
= NULL
;
2700 * Count number of session permitted by uid/gid.
2702 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2705 struct ltt_session
*session
;
2706 const struct ltt_session_list
*session_list
= session_get_list();
2708 DBG("Counting number of available session for UID %d GID %d",
2710 cds_list_for_each_entry(session
, &session_list
->head
, list
) {
2711 if (!session_get(session
)) {
2714 session_lock(session
);
2715 /* Only count the sessions the user can control. */
2716 if (session_access_ok(session
, uid
, gid
) &&
2717 !session
->destroyed
) {
2720 session_unlock(session
);
2721 session_put(session
);
2726 static int receive_userspace_probe(struct command_ctx
*cmd_ctx
, int sock
,
2727 int *sock_error
, struct lttng_event
*event
)
2730 struct lttng_userspace_probe_location
*probe_location
;
2731 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
2732 struct lttng_dynamic_buffer probe_location_buffer
;
2733 struct lttng_buffer_view buffer_view
;
2736 * Create a buffer to store the serialized version of the probe
2739 lttng_dynamic_buffer_init(&probe_location_buffer
);
2740 ret
= lttng_dynamic_buffer_set_size(&probe_location_buffer
,
2741 cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
);
2743 ret
= LTTNG_ERR_NOMEM
;
2748 * Receive the probe location.
2750 ret
= lttcomm_recv_unix_sock(sock
, probe_location_buffer
.data
,
2751 probe_location_buffer
.size
);
2753 DBG("Nothing recv() from client var len data... continuing");
2755 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2756 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2760 buffer_view
= lttng_buffer_view_from_dynamic_buffer(
2761 &probe_location_buffer
, 0, probe_location_buffer
.size
);
2764 * Extract the probe location from the serialized version.
2766 ret
= lttng_userspace_probe_location_create_from_buffer(
2767 &buffer_view
, &probe_location
);
2769 WARN("Failed to create a userspace probe location from the received buffer");
2770 lttng_dynamic_buffer_reset( &probe_location_buffer
);
2771 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2776 * Receive the file descriptor to the target binary from the client.
2778 DBG("Receiving userspace probe target FD from client ...");
2779 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2781 DBG("Nothing recv() from client userspace probe fd... continuing");
2783 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2788 * Set the file descriptor received from the client through the unix
2789 * socket in the probe location.
2791 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
2793 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2798 * From the kernel tracer's perspective, all userspace probe event types
2799 * are all the same: a file and an offset.
2801 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
2802 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
2803 ret
= lttng_userspace_probe_location_function_set_binary_fd(
2804 probe_location
, fd
);
2806 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
2807 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
2808 probe_location
, fd
);
2811 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2816 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2820 /* Attach the probe location to the event. */
2821 ret
= lttng_event_set_userspace_probe_location(event
, probe_location
);
2823 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
2827 lttng_dynamic_buffer_reset(&probe_location_buffer
);
2833 * Check if the current kernel tracer supports the session rotation feature.
2834 * Return 1 if it does, 0 otherwise.
2836 static int check_rotate_compatible(void)
2840 if (kernel_tracer_version
.major
!= 2 || kernel_tracer_version
.minor
< 11) {
2841 DBG("Kernel tracer version is not compatible with the rotation feature");
2849 * Process the command requested by the lttng client within the command
2850 * context structure. This function make sure that the return structure (llm)
2851 * is set and ready for transmission before returning.
2853 * Return any error encountered or 0 for success.
2855 * "sock" is only used for special-case var. len data.
2857 * Should *NOT* be called with RCU read-side lock held.
2859 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2863 int need_tracing_session
= 1;
2866 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2868 assert(!rcu_read_ongoing());
2872 switch (cmd_ctx
->lsm
->cmd_type
) {
2873 case LTTNG_CREATE_SESSION
:
2874 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2875 case LTTNG_CREATE_SESSION_LIVE
:
2876 case LTTNG_DESTROY_SESSION
:
2877 case LTTNG_LIST_SESSIONS
:
2878 case LTTNG_LIST_DOMAINS
:
2879 case LTTNG_START_TRACE
:
2880 case LTTNG_STOP_TRACE
:
2881 case LTTNG_DATA_PENDING
:
2882 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2883 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2884 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2885 case LTTNG_SNAPSHOT_RECORD
:
2886 case LTTNG_SAVE_SESSION
:
2887 case LTTNG_SET_SESSION_SHM_PATH
:
2888 case LTTNG_REGENERATE_METADATA
:
2889 case LTTNG_REGENERATE_STATEDUMP
:
2890 case LTTNG_REGISTER_TRIGGER
:
2891 case LTTNG_UNREGISTER_TRIGGER
:
2892 case LTTNG_ROTATE_SESSION
:
2893 case LTTNG_ROTATION_GET_INFO
:
2894 case LTTNG_ROTATION_SET_SCHEDULE
:
2895 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2902 if (config
.no_kernel
&& need_domain
2903 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2905 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2907 ret
= LTTNG_ERR_KERN_NA
;
2912 /* Deny register consumer if we already have a spawned consumer. */
2913 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2914 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2915 if (kconsumer_data
.pid
> 0) {
2916 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2917 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2920 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2924 * Check for command that don't needs to allocate a returned payload. We do
2925 * this here so we don't have to make the call for no payload at each
2928 switch(cmd_ctx
->lsm
->cmd_type
) {
2929 case LTTNG_LIST_SESSIONS
:
2930 case LTTNG_LIST_TRACEPOINTS
:
2931 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2932 case LTTNG_LIST_DOMAINS
:
2933 case LTTNG_LIST_CHANNELS
:
2934 case LTTNG_LIST_EVENTS
:
2935 case LTTNG_LIST_SYSCALLS
:
2936 case LTTNG_LIST_TRACKER_PIDS
:
2937 case LTTNG_DATA_PENDING
:
2938 case LTTNG_ROTATE_SESSION
:
2939 case LTTNG_ROTATION_GET_INFO
:
2940 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
2943 /* Setup lttng message with no payload */
2944 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
2946 /* This label does not try to unlock the session */
2947 goto init_setup_error
;
2951 /* Commands that DO NOT need a session. */
2952 switch (cmd_ctx
->lsm
->cmd_type
) {
2953 case LTTNG_CREATE_SESSION
:
2954 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2955 case LTTNG_CREATE_SESSION_LIVE
:
2956 case LTTNG_LIST_SESSIONS
:
2957 case LTTNG_LIST_TRACEPOINTS
:
2958 case LTTNG_LIST_SYSCALLS
:
2959 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2960 case LTTNG_SAVE_SESSION
:
2961 case LTTNG_REGISTER_TRIGGER
:
2962 case LTTNG_UNREGISTER_TRIGGER
:
2963 need_tracing_session
= 0;
2966 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2968 * We keep the session list lock across _all_ commands
2969 * for now, because the per-session lock does not
2970 * handle teardown properly.
2972 session_lock_list();
2973 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2974 if (cmd_ctx
->session
== NULL
) {
2975 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2978 /* Acquire lock for the session */
2979 session_lock(cmd_ctx
->session
);
2985 * Commands that need a valid session but should NOT create one if none
2986 * exists. Instead of creating one and destroying it when the command is
2987 * handled, process that right before so we save some round trip in useless
2990 switch (cmd_ctx
->lsm
->cmd_type
) {
2991 case LTTNG_DISABLE_CHANNEL
:
2992 case LTTNG_DISABLE_EVENT
:
2993 switch (cmd_ctx
->lsm
->domain
.type
) {
2994 case LTTNG_DOMAIN_KERNEL
:
2995 if (!cmd_ctx
->session
->kernel_session
) {
2996 ret
= LTTNG_ERR_NO_CHANNEL
;
3000 case LTTNG_DOMAIN_JUL
:
3001 case LTTNG_DOMAIN_LOG4J
:
3002 case LTTNG_DOMAIN_PYTHON
:
3003 case LTTNG_DOMAIN_UST
:
3004 if (!cmd_ctx
->session
->ust_session
) {
3005 ret
= LTTNG_ERR_NO_CHANNEL
;
3010 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3022 * Check domain type for specific "pre-action".
3024 switch (cmd_ctx
->lsm
->domain
.type
) {
3025 case LTTNG_DOMAIN_KERNEL
:
3027 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3031 /* Kernel tracer check */
3032 if (kernel_tracer_fd
== -1) {
3033 /* Basically, load kernel tracer modules */
3034 ret
= init_kernel_tracer();
3040 /* Consumer is in an ERROR state. Report back to client */
3041 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3042 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3046 /* Need a session for kernel command */
3047 if (need_tracing_session
) {
3048 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3049 ret
= create_kernel_session(cmd_ctx
->session
);
3051 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3056 /* Start the kernel consumer daemon */
3057 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3058 if (kconsumer_data
.pid
== 0 &&
3059 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3060 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3061 ret
= start_consumerd(&kconsumer_data
);
3063 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3066 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3068 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3072 * The consumer was just spawned so we need to add the socket to
3073 * the consumer output of the session if exist.
3075 ret
= consumer_create_socket(&kconsumer_data
,
3076 cmd_ctx
->session
->kernel_session
->consumer
);
3083 case LTTNG_DOMAIN_JUL
:
3084 case LTTNG_DOMAIN_LOG4J
:
3085 case LTTNG_DOMAIN_PYTHON
:
3086 case LTTNG_DOMAIN_UST
:
3088 if (!ust_app_supported()) {
3089 ret
= LTTNG_ERR_NO_UST
;
3092 /* Consumer is in an ERROR state. Report back to client */
3093 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3094 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3098 if (need_tracing_session
) {
3099 /* Create UST session if none exist. */
3100 if (cmd_ctx
->session
->ust_session
== NULL
) {
3101 ret
= create_ust_session(cmd_ctx
->session
,
3102 &cmd_ctx
->lsm
->domain
);
3103 if (ret
!= LTTNG_OK
) {
3108 /* Start the UST consumer daemons */
3110 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3111 if (config
.consumerd64_bin_path
.value
&&
3112 ustconsumer64_data
.pid
== 0 &&
3113 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3114 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3115 ret
= start_consumerd(&ustconsumer64_data
);
3117 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3118 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3122 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3123 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3125 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3129 * Setup socket for consumer 64 bit. No need for atomic access
3130 * since it was set above and can ONLY be set in this thread.
3132 ret
= consumer_create_socket(&ustconsumer64_data
,
3133 cmd_ctx
->session
->ust_session
->consumer
);
3139 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3140 if (config
.consumerd32_bin_path
.value
&&
3141 ustconsumer32_data
.pid
== 0 &&
3142 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3143 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3144 ret
= start_consumerd(&ustconsumer32_data
);
3146 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3147 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3151 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3152 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3154 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3158 * Setup socket for consumer 32 bit. No need for atomic access
3159 * since it was set above and can ONLY be set in this thread.
3161 ret
= consumer_create_socket(&ustconsumer32_data
,
3162 cmd_ctx
->session
->ust_session
->consumer
);
3174 /* Validate consumer daemon state when start/stop trace command */
3175 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3176 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3177 switch (cmd_ctx
->lsm
->domain
.type
) {
3178 case LTTNG_DOMAIN_NONE
:
3180 case LTTNG_DOMAIN_JUL
:
3181 case LTTNG_DOMAIN_LOG4J
:
3182 case LTTNG_DOMAIN_PYTHON
:
3183 case LTTNG_DOMAIN_UST
:
3184 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3185 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3189 case LTTNG_DOMAIN_KERNEL
:
3190 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3191 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3196 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3202 * Check that the UID or GID match that of the tracing session.
3203 * The root user can interact with all sessions.
3205 if (need_tracing_session
) {
3206 if (!session_access_ok(cmd_ctx
->session
,
3207 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3208 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
)) ||
3209 cmd_ctx
->session
->destroyed
) {
3210 ret
= LTTNG_ERR_EPERM
;
3216 * Send relayd information to consumer as soon as we have a domain and a
3219 if (cmd_ctx
->session
&& need_domain
) {
3221 * Setup relayd if not done yet. If the relayd information was already
3222 * sent to the consumer, this call will gracefully return.
3224 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3225 if (ret
!= LTTNG_OK
) {
3230 /* Process by command type */
3231 switch (cmd_ctx
->lsm
->cmd_type
) {
3232 case LTTNG_ADD_CONTEXT
:
3235 * An LTTNG_ADD_CONTEXT command might have a supplementary
3236 * payload if the context being added is an application context.
3238 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3239 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3240 char *provider_name
= NULL
, *context_name
= NULL
;
3241 size_t provider_name_len
=
3242 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3243 size_t context_name_len
=
3244 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3246 if (provider_name_len
== 0 || context_name_len
== 0) {
3248 * Application provider and context names MUST
3251 ret
= -LTTNG_ERR_INVALID
;
3255 provider_name
= zmalloc(provider_name_len
+ 1);
3256 if (!provider_name
) {
3257 ret
= -LTTNG_ERR_NOMEM
;
3260 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3263 context_name
= zmalloc(context_name_len
+ 1);
3264 if (!context_name
) {
3265 ret
= -LTTNG_ERR_NOMEM
;
3266 goto error_add_context
;
3268 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3271 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3274 goto error_add_context
;
3277 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3280 goto error_add_context
;
3285 * cmd_add_context assumes ownership of the provider and context
3288 ret
= cmd_add_context(cmd_ctx
->session
,
3289 cmd_ctx
->lsm
->domain
.type
,
3290 cmd_ctx
->lsm
->u
.context
.channel_name
,
3291 &cmd_ctx
->lsm
->u
.context
.ctx
,
3292 kernel_poll_pipe
[1]);
3294 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3295 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3297 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3298 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3304 case LTTNG_DISABLE_CHANNEL
:
3306 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3307 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3310 case LTTNG_DISABLE_EVENT
:
3314 * FIXME: handle filter; for now we just receive the filter's
3315 * bytecode along with the filter expression which are sent by
3316 * liblttng-ctl and discard them.
3318 * This fixes an issue where the client may block while sending
3319 * the filter payload and encounter an error because the session
3320 * daemon closes the socket without ever handling this data.
3322 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3323 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3326 char data
[LTTNG_FILTER_MAX_LEN
];
3328 DBG("Discarding disable event command payload of size %zu", count
);
3330 ret
= lttcomm_recv_unix_sock(sock
, data
,
3331 count
> sizeof(data
) ? sizeof(data
) : count
);
3336 count
-= (size_t) ret
;
3339 /* FIXME: passing packed structure to non-packed pointer */
3340 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3341 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3342 &cmd_ctx
->lsm
->u
.disable
.event
);
3345 case LTTNG_ENABLE_CHANNEL
:
3347 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3348 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3349 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3350 &cmd_ctx
->lsm
->u
.channel
.chan
,
3351 kernel_poll_pipe
[1]);
3354 case LTTNG_TRACK_PID
:
3356 ret
= cmd_track_pid(cmd_ctx
->session
,
3357 cmd_ctx
->lsm
->domain
.type
,
3358 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3361 case LTTNG_UNTRACK_PID
:
3363 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3364 cmd_ctx
->lsm
->domain
.type
,
3365 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3368 case LTTNG_ENABLE_EVENT
:
3370 struct lttng_event
*ev
= NULL
;
3371 struct lttng_event_exclusion
*exclusion
= NULL
;
3372 struct lttng_filter_bytecode
*bytecode
= NULL
;
3373 char *filter_expression
= NULL
;
3375 /* Handle exclusion events and receive it from the client. */
3376 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3377 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3379 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3380 (count
* LTTNG_SYMBOL_NAME_LEN
));
3382 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3386 DBG("Receiving var len exclusion event list from client ...");
3387 exclusion
->count
= count
;
3388 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3389 count
* LTTNG_SYMBOL_NAME_LEN
);
3391 DBG("Nothing recv() from client var len data... continuing");
3394 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3399 /* Get filter expression from client. */
3400 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3401 size_t expression_len
=
3402 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3404 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3405 ret
= LTTNG_ERR_FILTER_INVAL
;
3410 filter_expression
= zmalloc(expression_len
);
3411 if (!filter_expression
) {
3413 ret
= LTTNG_ERR_FILTER_NOMEM
;
3417 /* Receive var. len. data */
3418 DBG("Receiving var len filter's expression from client ...");
3419 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3422 DBG("Nothing recv() from client var len data... continuing");
3424 free(filter_expression
);
3426 ret
= LTTNG_ERR_FILTER_INVAL
;
3431 /* Handle filter and get bytecode from client. */
3432 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3433 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3435 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3436 ret
= LTTNG_ERR_FILTER_INVAL
;
3437 free(filter_expression
);
3442 bytecode
= zmalloc(bytecode_len
);
3444 free(filter_expression
);
3446 ret
= LTTNG_ERR_FILTER_NOMEM
;
3450 /* Receive var. len. data */
3451 DBG("Receiving var len filter's bytecode from client ...");
3452 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3454 DBG("Nothing recv() from client var len data... continuing");
3456 free(filter_expression
);
3459 ret
= LTTNG_ERR_FILTER_INVAL
;
3463 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3464 free(filter_expression
);
3467 ret
= LTTNG_ERR_FILTER_INVAL
;
3472 ev
= lttng_event_copy(&cmd_ctx
->lsm
->u
.enable
.event
);
3474 DBG("Failed to copy event: %s",
3475 cmd_ctx
->lsm
->u
.enable
.event
.name
);
3476 free(filter_expression
);
3479 ret
= LTTNG_ERR_NOMEM
;
3484 if (cmd_ctx
->lsm
->u
.enable
.userspace_probe_location_len
> 0) {
3485 /* Expect a userspace probe description. */
3486 ret
= receive_userspace_probe(cmd_ctx
, sock
, sock_error
, ev
);
3488 free(filter_expression
);
3491 lttng_event_destroy(ev
);
3496 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3497 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3499 filter_expression
, bytecode
, exclusion
,
3500 kernel_poll_pipe
[1]);
3501 lttng_event_destroy(ev
);
3504 case LTTNG_LIST_TRACEPOINTS
:
3506 struct lttng_event
*events
;
3509 session_lock_list();
3510 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3511 session_unlock_list();
3512 if (nb_events
< 0) {
3513 /* Return value is a negative lttng_error_code. */
3519 * Setup lttng message with payload size set to the event list size in
3520 * bytes and then copy list into the llm payload.
3522 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3523 sizeof(struct lttng_event
) * nb_events
);
3533 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3535 struct lttng_event_field
*fields
;
3538 session_lock_list();
3539 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3541 session_unlock_list();
3542 if (nb_fields
< 0) {
3543 /* Return value is a negative lttng_error_code. */
3549 * Setup lttng message with payload size set to the event list size in
3550 * bytes and then copy list into the llm payload.
3552 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3553 sizeof(struct lttng_event_field
) * nb_fields
);
3563 case LTTNG_LIST_SYSCALLS
:
3565 struct lttng_event
*events
;
3568 nb_events
= cmd_list_syscalls(&events
);
3569 if (nb_events
< 0) {
3570 /* Return value is a negative lttng_error_code. */
3576 * Setup lttng message with payload size set to the event list size in
3577 * bytes and then copy list into the llm payload.
3579 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3580 sizeof(struct lttng_event
) * nb_events
);
3590 case LTTNG_LIST_TRACKER_PIDS
:
3592 int32_t *pids
= NULL
;
3595 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3596 cmd_ctx
->lsm
->domain
.type
, &pids
);
3598 /* Return value is a negative lttng_error_code. */
3604 * Setup lttng message with payload size set to the event list size in
3605 * bytes and then copy list into the llm payload.
3607 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3608 sizeof(int32_t) * nr_pids
);
3618 case LTTNG_SET_CONSUMER_URI
:
3621 struct lttng_uri
*uris
;
3623 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3624 len
= nb_uri
* sizeof(struct lttng_uri
);
3627 ret
= LTTNG_ERR_INVALID
;
3631 uris
= zmalloc(len
);
3633 ret
= LTTNG_ERR_FATAL
;
3637 /* Receive variable len data */
3638 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3639 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3641 DBG("No URIs received from client... continuing");
3643 ret
= LTTNG_ERR_SESSION_FAIL
;
3648 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3650 if (ret
!= LTTNG_OK
) {
3657 case LTTNG_START_TRACE
:
3660 * On the first start, if we have a kernel session and we have
3661 * enabled time or size-based rotations, we have to make sure
3662 * the kernel tracer supports it.
3664 if (!cmd_ctx
->session
->has_been_started
&& \
3665 cmd_ctx
->session
->kernel_session
&& \
3666 (cmd_ctx
->session
->rotate_timer_period
|| \
3667 cmd_ctx
->session
->rotate_size
) && \
3668 !check_rotate_compatible()) {
3669 DBG("Kernel tracer version is not compatible with the rotation feature");
3670 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
3673 ret
= cmd_start_trace(cmd_ctx
->session
);
3676 case LTTNG_STOP_TRACE
:
3678 ret
= cmd_stop_trace(cmd_ctx
->session
);
3681 case LTTNG_CREATE_SESSION
:
3684 struct lttng_uri
*uris
= NULL
;
3686 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3687 len
= nb_uri
* sizeof(struct lttng_uri
);
3690 uris
= zmalloc(len
);
3692 ret
= LTTNG_ERR_FATAL
;
3696 /* Receive variable len data */
3697 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3698 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3700 DBG("No URIs received from client... continuing");
3702 ret
= LTTNG_ERR_SESSION_FAIL
;
3707 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3708 DBG("Creating session with ONE network URI is a bad call");
3709 ret
= LTTNG_ERR_SESSION_FAIL
;
3715 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3716 &cmd_ctx
->creds
, 0);
3722 case LTTNG_DESTROY_SESSION
:
3724 ret
= cmd_destroy_session(cmd_ctx
->session
,
3725 notification_thread_handle
);
3728 case LTTNG_LIST_DOMAINS
:
3731 struct lttng_domain
*domains
= NULL
;
3733 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3735 /* Return value is a negative lttng_error_code. */
3740 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3741 nb_dom
* sizeof(struct lttng_domain
));
3751 case LTTNG_LIST_CHANNELS
:
3753 ssize_t payload_size
;
3754 struct lttng_channel
*channels
= NULL
;
3756 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3757 cmd_ctx
->session
, &channels
);
3758 if (payload_size
< 0) {
3759 /* Return value is a negative lttng_error_code. */
3760 ret
= -payload_size
;
3764 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3775 case LTTNG_LIST_EVENTS
:
3778 struct lttng_event
*events
= NULL
;
3779 struct lttcomm_event_command_header cmd_header
;
3782 memset(&cmd_header
, 0, sizeof(cmd_header
));
3783 /* Extended infos are included at the end of events */
3784 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3785 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3786 &events
, &total_size
);
3789 /* Return value is a negative lttng_error_code. */
3794 cmd_header
.nb_events
= nb_event
;
3795 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3796 &cmd_header
, sizeof(cmd_header
));
3806 case LTTNG_LIST_SESSIONS
:
3808 unsigned int nr_sessions
;
3809 void *sessions_payload
;
3812 session_lock_list();
3813 nr_sessions
= lttng_sessions_count(
3814 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3815 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3816 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3817 sessions_payload
= zmalloc(payload_len
);
3819 if (!sessions_payload
) {
3820 session_unlock_list();
3825 cmd_list_lttng_sessions(sessions_payload
,
3826 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3827 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3828 session_unlock_list();
3830 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3832 free(sessions_payload
);
3841 case LTTNG_REGISTER_CONSUMER
:
3843 struct consumer_data
*cdata
;
3845 switch (cmd_ctx
->lsm
->domain
.type
) {
3846 case LTTNG_DOMAIN_KERNEL
:
3847 cdata
= &kconsumer_data
;
3850 ret
= LTTNG_ERR_UND
;
3854 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3855 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3858 case LTTNG_DATA_PENDING
:
3861 uint8_t pending_ret_byte
;
3863 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3868 * This function may returns 0 or 1 to indicate whether or not
3869 * there is data pending. In case of error, it should return an
3870 * LTTNG_ERR code. However, some code paths may still return
3871 * a nondescript error code, which we handle by returning an
3874 if (pending_ret
== 0 || pending_ret
== 1) {
3876 * ret will be set to LTTNG_OK at the end of
3879 } else if (pending_ret
< 0) {
3880 ret
= LTTNG_ERR_UNK
;
3887 pending_ret_byte
= (uint8_t) pending_ret
;
3889 /* 1 byte to return whether or not data is pending */
3890 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
3891 &pending_ret_byte
, 1);
3900 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3902 struct lttcomm_lttng_output_id reply
;
3904 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3905 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3906 if (ret
!= LTTNG_OK
) {
3910 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
3916 /* Copy output list into message payload */
3920 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3922 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3923 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3926 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3929 struct lttng_snapshot_output
*outputs
= NULL
;
3931 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3932 if (nb_output
< 0) {
3937 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
3938 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
3939 nb_output
* sizeof(struct lttng_snapshot_output
));
3949 case LTTNG_SNAPSHOT_RECORD
:
3951 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3952 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3953 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3956 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3959 struct lttng_uri
*uris
= NULL
;
3961 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3962 len
= nb_uri
* sizeof(struct lttng_uri
);
3965 uris
= zmalloc(len
);
3967 ret
= LTTNG_ERR_FATAL
;
3971 /* Receive variable len data */
3972 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3973 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3975 DBG("No URIs received from client... continuing");
3977 ret
= LTTNG_ERR_SESSION_FAIL
;
3982 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3983 DBG("Creating session with ONE network URI is a bad call");
3984 ret
= LTTNG_ERR_SESSION_FAIL
;
3990 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3991 nb_uri
, &cmd_ctx
->creds
);
3995 case LTTNG_CREATE_SESSION_LIVE
:
3998 struct lttng_uri
*uris
= NULL
;
4000 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4001 len
= nb_uri
* sizeof(struct lttng_uri
);
4004 uris
= zmalloc(len
);
4006 ret
= LTTNG_ERR_FATAL
;
4010 /* Receive variable len data */
4011 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4012 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4014 DBG("No URIs received from client... continuing");
4016 ret
= LTTNG_ERR_SESSION_FAIL
;
4021 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4022 DBG("Creating session with ONE network URI is a bad call");
4023 ret
= LTTNG_ERR_SESSION_FAIL
;
4029 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4030 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4034 case LTTNG_SAVE_SESSION
:
4036 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4040 case LTTNG_SET_SESSION_SHM_PATH
:
4042 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4043 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4046 case LTTNG_REGENERATE_METADATA
:
4048 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4051 case LTTNG_REGENERATE_STATEDUMP
:
4053 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4056 case LTTNG_REGISTER_TRIGGER
:
4058 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4059 notification_thread_handle
);
4062 case LTTNG_UNREGISTER_TRIGGER
:
4064 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4065 notification_thread_handle
);
4068 case LTTNG_ROTATE_SESSION
:
4070 struct lttng_rotate_session_return rotate_return
;
4072 DBG("Client rotate session \"%s\"", cmd_ctx
->session
->name
);
4074 memset(&rotate_return
, 0, sizeof(rotate_return
));
4075 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4076 DBG("Kernel tracer version is not compatible with the rotation feature");
4077 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4081 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4087 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &rotate_return
,
4088 sizeof(rotate_return
));
4097 case LTTNG_ROTATION_GET_INFO
:
4099 struct lttng_rotation_get_info_return get_info_return
;
4101 memset(&get_info_return
, 0, sizeof(get_info_return
));
4102 ret
= cmd_rotate_get_info(cmd_ctx
->session
, &get_info_return
,
4103 cmd_ctx
->lsm
->u
.get_rotation_info
.rotation_id
);
4109 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &get_info_return
,
4110 sizeof(get_info_return
));
4119 case LTTNG_ROTATION_SET_SCHEDULE
:
4122 enum lttng_rotation_schedule_type schedule_type
;
4125 if (cmd_ctx
->session
->kernel_session
&& !check_rotate_compatible()) {
4126 DBG("Kernel tracer version does not support session rotations");
4127 ret
= LTTNG_ERR_ROTATION_WRONG_VERSION
;
4131 set_schedule
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.set
== 1;
4132 schedule_type
= (enum lttng_rotation_schedule_type
) cmd_ctx
->lsm
->u
.rotation_set_schedule
.type
;
4133 value
= cmd_ctx
->lsm
->u
.rotation_set_schedule
.value
;
4135 ret
= cmd_rotation_set_schedule(cmd_ctx
->session
,
4139 notification_thread_handle
);
4140 if (ret
!= LTTNG_OK
) {
4146 case LTTNG_SESSION_LIST_ROTATION_SCHEDULES
:
4148 struct lttng_session_list_schedules_return schedules
= {
4149 .periodic
.set
= !!cmd_ctx
->session
->rotate_timer_period
,
4150 .periodic
.value
= cmd_ctx
->session
->rotate_timer_period
,
4151 .size
.set
= !!cmd_ctx
->session
->rotate_size
,
4152 .size
.value
= cmd_ctx
->session
->rotate_size
,
4155 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &schedules
,
4166 ret
= LTTNG_ERR_UND
;
4171 if (cmd_ctx
->llm
== NULL
) {
4172 DBG("Missing llm structure. Allocating one.");
4173 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4177 /* Set return code */
4178 cmd_ctx
->llm
->ret_code
= ret
;
4180 if (cmd_ctx
->session
) {
4181 session_unlock(cmd_ctx
->session
);
4182 session_put(cmd_ctx
->session
);
4184 if (need_tracing_session
) {
4185 session_unlock_list();
4188 assert(!rcu_read_ongoing());
4193 * This thread manage all clients request using the unix client socket for
4196 static void *thread_manage_clients(void *data
)
4198 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4200 uint32_t revents
, nb_fd
;
4201 struct command_ctx
*cmd_ctx
= NULL
;
4202 struct lttng_poll_event events
;
4204 DBG("[thread] Manage client started");
4206 rcu_register_thread();
4208 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4210 health_code_update();
4212 ret
= lttcomm_listen_unix_sock(client_sock
);
4218 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4219 * more will be added to this poll set.
4221 ret
= sessiond_set_thread_pollset(&events
, 2);
4223 goto error_create_poll
;
4226 /* Add the application registration socket */
4227 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4232 ret
= sem_post(&load_info
->message_thread_ready
);
4234 PERROR("sem_post message_thread_ready");
4239 * Wait until all support threads are initialized before accepting
4242 while (uatomic_read(<tng_sessiond_ready
) != 0) {
4244 * If a support thread failed to launch, it may signal that
4245 * we must exit and the sessiond would never be marked as
4248 * The timeout is set to 1ms, which serves as a way to
4249 * pace down this check.
4251 ret
= sessiond_wait_for_quit_pipe(1000);
4257 * This barrier is paired with the one in sessiond_notify_ready() to
4258 * ensure that loads accessing data initialized by the other threads,
4259 * on which this thread was waiting, are not performed before this point.
4261 * Note that this could be a 'read' memory barrier, but a full barrier
4262 * is used in case the code changes. The performance implications of
4263 * this choice are minimal since this is a slow path.
4267 /* This testpoint is after we signal readiness to the parent. */
4268 if (testpoint(sessiond_thread_manage_clients
)) {
4272 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4276 health_code_update();
4278 /* Set state as running. */
4279 sessiond_set_client_thread_state(true);
4282 const struct cmd_completion_handler
*cmd_completion_handler
;
4284 DBG("Accepting client command ...");
4286 /* Inifinite blocking call, waiting for transmission */
4288 health_poll_entry();
4289 ret
= lttng_poll_wait(&events
, -1);
4293 * Restart interrupted system call.
4295 if (errno
== EINTR
) {
4303 for (i
= 0; i
< nb_fd
; i
++) {
4304 /* Fetch once the poll data */
4305 revents
= LTTNG_POLL_GETEV(&events
, i
);
4306 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4308 health_code_update();
4311 /* No activity for this FD (poll implementation). */
4315 /* Thread quit pipe has been closed. Killing thread. */
4316 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4322 /* Event on the registration socket */
4323 if (pollfd
== client_sock
) {
4324 if (revents
& LPOLLIN
) {
4326 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4327 ERR("Client socket poll error");
4330 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4336 DBG("Wait for client response");
4338 health_code_update();
4340 sock
= lttcomm_accept_unix_sock(client_sock
);
4346 * Set the CLOEXEC flag. Return code is useless because either way, the
4349 (void) utils_set_fd_cloexec(sock
);
4351 /* Set socket option for credentials retrieval */
4352 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4357 /* Allocate context command to process the client request */
4358 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4359 if (cmd_ctx
== NULL
) {
4360 PERROR("zmalloc cmd_ctx");
4364 /* Allocate data buffer for reception */
4365 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4366 if (cmd_ctx
->lsm
== NULL
) {
4367 PERROR("zmalloc cmd_ctx->lsm");
4371 cmd_ctx
->llm
= NULL
;
4372 cmd_ctx
->session
= NULL
;
4374 health_code_update();
4377 * Data is received from the lttng client. The struct
4378 * lttcomm_session_msg (lsm) contains the command and data request of
4381 DBG("Receiving data from client ...");
4382 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4383 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4385 DBG("Nothing recv() from client... continuing");
4391 clean_command_ctx(&cmd_ctx
);
4395 health_code_update();
4397 // TODO: Validate cmd_ctx including sanity check for
4398 // security purpose.
4400 rcu_thread_online();
4402 * This function dispatch the work to the kernel or userspace tracer
4403 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4404 * informations for the client. The command context struct contains
4405 * everything this function may needs.
4407 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4408 rcu_thread_offline();
4416 * TODO: Inform client somehow of the fatal error. At
4417 * this point, ret < 0 means that a zmalloc failed
4418 * (ENOMEM). Error detected but still accept
4419 * command, unless a socket error has been
4422 clean_command_ctx(&cmd_ctx
);
4426 cmd_completion_handler
= cmd_pop_completion_handler();
4427 if (cmd_completion_handler
) {
4428 enum lttng_error_code completion_code
;
4430 completion_code
= cmd_completion_handler
->run(
4431 cmd_completion_handler
->data
);
4432 if (completion_code
!= LTTNG_OK
) {
4433 clean_command_ctx(&cmd_ctx
);
4438 health_code_update();
4440 DBG("Sending response (size: %d, retcode: %s (%d))",
4441 cmd_ctx
->lttng_msg_size
,
4442 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4443 cmd_ctx
->llm
->ret_code
);
4444 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4446 ERR("Failed to send data back to client");
4449 /* End of transmission */
4456 clean_command_ctx(&cmd_ctx
);
4458 health_code_update();
4470 lttng_poll_clean(&events
);
4471 clean_command_ctx(&cmd_ctx
);
4475 unlink(config
.client_unix_sock_path
.value
);
4476 if (client_sock
>= 0) {
4477 ret
= close(client_sock
);
4485 ERR("Health error occurred in %s", __func__
);
4488 health_unregister(health_sessiond
);
4490 DBG("Client thread dying");
4492 rcu_unregister_thread();
4495 * Since we are creating the consumer threads, we own them, so we need
4496 * to join them before our thread exits.
4498 ret
= join_consumer_thread(&kconsumer_data
);
4501 PERROR("join_consumer");
4504 ret
= join_consumer_thread(&ustconsumer32_data
);
4507 PERROR("join_consumer ust32");
4510 ret
= join_consumer_thread(&ustconsumer64_data
);
4513 PERROR("join_consumer ust64");
4516 /* Set state as non-running. */
4517 sessiond_set_client_thread_state(false);
4521 static int string_match(const char *str1
, const char *str2
)
4523 return (str1
&& str2
) && !strcmp(str1
, str2
);
4527 * Take an option from the getopt output and set it in the right variable to be
4530 * Return 0 on success else a negative value.
4532 static int set_option(int opt
, const char *arg
, const char *optname
)
4536 if (string_match(optname
, "client-sock") || opt
== 'c') {
4537 if (!arg
|| *arg
== '\0') {
4541 if (lttng_is_setuid_setgid()) {
4542 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4543 "-c, --client-sock");
4545 config_string_set(&config
.client_unix_sock_path
,
4547 if (!config
.client_unix_sock_path
.value
) {
4552 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4553 if (!arg
|| *arg
== '\0') {
4557 if (lttng_is_setuid_setgid()) {
4558 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4561 config_string_set(&config
.apps_unix_sock_path
,
4563 if (!config
.apps_unix_sock_path
.value
) {
4568 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4569 config
.daemonize
= true;
4570 } else if (string_match(optname
, "background") || opt
== 'b') {
4571 config
.background
= true;
4572 } else if (string_match(optname
, "group") || opt
== 'g') {
4573 if (!arg
|| *arg
== '\0') {
4577 if (lttng_is_setuid_setgid()) {
4578 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4581 config_string_set(&config
.tracing_group_name
,
4583 if (!config
.tracing_group_name
.value
) {
4588 } else if (string_match(optname
, "help") || opt
== 'h') {
4589 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4591 ERR("Cannot show --help for `lttng-sessiond`");
4594 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4595 } else if (string_match(optname
, "version") || opt
== 'V') {
4596 fprintf(stdout
, "%s\n", VERSION
);
4598 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4599 config
.sig_parent
= true;
4600 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4601 if (!arg
|| *arg
== '\0') {
4605 if (lttng_is_setuid_setgid()) {
4606 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4607 "--kconsumerd-err-sock");
4609 config_string_set(&config
.kconsumerd_err_unix_sock_path
,
4611 if (!config
.kconsumerd_err_unix_sock_path
.value
) {
4616 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4617 if (!arg
|| *arg
== '\0') {
4621 if (lttng_is_setuid_setgid()) {
4622 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4623 "--kconsumerd-cmd-sock");
4625 config_string_set(&config
.kconsumerd_cmd_unix_sock_path
,
4627 if (!config
.kconsumerd_cmd_unix_sock_path
.value
) {
4632 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4633 if (!arg
|| *arg
== '\0') {
4637 if (lttng_is_setuid_setgid()) {
4638 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4639 "--ustconsumerd64-err-sock");
4641 config_string_set(&config
.consumerd64_err_unix_sock_path
,
4643 if (!config
.consumerd64_err_unix_sock_path
.value
) {
4648 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4649 if (!arg
|| *arg
== '\0') {
4653 if (lttng_is_setuid_setgid()) {
4654 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4655 "--ustconsumerd64-cmd-sock");
4657 config_string_set(&config
.consumerd64_cmd_unix_sock_path
,
4659 if (!config
.consumerd64_cmd_unix_sock_path
.value
) {
4664 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4665 if (!arg
|| *arg
== '\0') {
4669 if (lttng_is_setuid_setgid()) {
4670 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4671 "--ustconsumerd32-err-sock");
4673 config_string_set(&config
.consumerd32_err_unix_sock_path
,
4675 if (!config
.consumerd32_err_unix_sock_path
.value
) {
4680 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4681 if (!arg
|| *arg
== '\0') {
4685 if (lttng_is_setuid_setgid()) {
4686 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4687 "--ustconsumerd32-cmd-sock");
4689 config_string_set(&config
.consumerd32_cmd_unix_sock_path
,
4691 if (!config
.consumerd32_cmd_unix_sock_path
.value
) {
4696 } else if (string_match(optname
, "no-kernel")) {
4697 config
.no_kernel
= true;
4698 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4699 config
.quiet
= true;
4700 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4701 /* Verbose level can increase using multiple -v */
4703 /* Value obtained from config file */
4704 config
.verbose
= config_parse_value(arg
);
4706 /* -v used on command line */
4709 /* Clamp value to [0, 3] */
4710 config
.verbose
= config
.verbose
< 0 ? 0 :
4711 (config
.verbose
<= 3 ? config
.verbose
: 3);
4712 } else if (string_match(optname
, "verbose-consumer")) {
4714 config
.verbose_consumer
= config_parse_value(arg
);
4716 config
.verbose_consumer
++;
4718 } else if (string_match(optname
, "consumerd32-path")) {
4719 if (!arg
|| *arg
== '\0') {
4723 if (lttng_is_setuid_setgid()) {
4724 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4725 "--consumerd32-path");
4727 config_string_set(&config
.consumerd32_bin_path
,
4729 if (!config
.consumerd32_bin_path
.value
) {
4734 } else if (string_match(optname
, "consumerd32-libdir")) {
4735 if (!arg
|| *arg
== '\0') {
4739 if (lttng_is_setuid_setgid()) {
4740 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4741 "--consumerd32-libdir");
4743 config_string_set(&config
.consumerd32_lib_dir
,
4745 if (!config
.consumerd32_lib_dir
.value
) {
4750 } else if (string_match(optname
, "consumerd64-path")) {
4751 if (!arg
|| *arg
== '\0') {
4755 if (lttng_is_setuid_setgid()) {
4756 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4757 "--consumerd64-path");
4759 config_string_set(&config
.consumerd64_bin_path
,
4761 if (!config
.consumerd64_bin_path
.value
) {
4766 } else if (string_match(optname
, "consumerd64-libdir")) {
4767 if (!arg
|| *arg
== '\0') {
4771 if (lttng_is_setuid_setgid()) {
4772 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4773 "--consumerd64-libdir");
4775 config_string_set(&config
.consumerd64_lib_dir
,
4777 if (!config
.consumerd64_lib_dir
.value
) {
4782 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4783 if (!arg
|| *arg
== '\0') {
4787 if (lttng_is_setuid_setgid()) {
4788 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4791 config_string_set(&config
.pid_file_path
, strdup(arg
));
4792 if (!config
.pid_file_path
.value
) {
4797 } else if (string_match(optname
, "agent-tcp-port")) {
4798 if (!arg
|| *arg
== '\0') {
4802 if (lttng_is_setuid_setgid()) {
4803 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4804 "--agent-tcp-port");
4809 v
= strtoul(arg
, NULL
, 0);
4810 if (errno
!= 0 || !isdigit(arg
[0])) {
4811 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4814 if (v
== 0 || v
>= 65535) {
4815 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4818 config
.agent_tcp_port
.begin
= config
.agent_tcp_port
.end
= (int) v
;
4819 DBG3("Agent TCP port set to non default: %i", (int) v
);
4821 } else if (string_match(optname
, "load") || opt
== 'l') {
4822 if (!arg
|| *arg
== '\0') {
4826 if (lttng_is_setuid_setgid()) {
4827 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4830 config_string_set(&config
.load_session_path
, strdup(arg
));
4831 if (!config
.load_session_path
.value
) {
4836 } else if (string_match(optname
, "kmod-probes")) {
4837 if (!arg
|| *arg
== '\0') {
4841 if (lttng_is_setuid_setgid()) {
4842 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4845 config_string_set(&config
.kmod_probes_list
, strdup(arg
));
4846 if (!config
.kmod_probes_list
.value
) {
4851 } else if (string_match(optname
, "extra-kmod-probes")) {
4852 if (!arg
|| *arg
== '\0') {
4856 if (lttng_is_setuid_setgid()) {
4857 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4858 "--extra-kmod-probes");
4860 config_string_set(&config
.kmod_extra_probes_list
,
4862 if (!config
.kmod_extra_probes_list
.value
) {
4867 } else if (string_match(optname
, "config") || opt
== 'f') {
4868 /* This is handled in set_options() thus silent skip. */
4871 /* Unknown option or other error.
4872 * Error is printed by getopt, just return */
4877 if (ret
== -EINVAL
) {
4878 const char *opt_name
= "unknown";
4881 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4883 if (opt
== long_options
[i
].val
) {
4884 opt_name
= long_options
[i
].name
;
4889 WARN("Invalid argument provided for option \"%s\", using default value.",
4897 * config_entry_handler_cb used to handle options read from a config file.
4898 * See config_entry_handler_cb comment in common/config/session-config.h for the
4899 * return value conventions.
4901 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4905 if (!entry
|| !entry
->name
|| !entry
->value
) {
4910 /* Check if the option is to be ignored */
4911 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4912 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4917 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4920 /* Ignore if not fully matched. */
4921 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4926 * If the option takes no argument on the command line, we have to
4927 * check if the value is "true". We support non-zero numeric values,
4930 if (!long_options
[i
].has_arg
) {
4931 ret
= config_parse_value(entry
->value
);
4934 WARN("Invalid configuration value \"%s\" for option %s",
4935 entry
->value
, entry
->name
);
4937 /* False, skip boolean config option. */
4942 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4946 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4953 * daemon configuration loading and argument parsing
4955 static int set_options(int argc
, char **argv
)
4957 int ret
= 0, c
= 0, option_index
= 0;
4958 int orig_optopt
= optopt
, orig_optind
= optind
;
4960 const char *config_path
= NULL
;
4962 optstring
= utils_generate_optstring(long_options
,
4963 sizeof(long_options
) / sizeof(struct option
));
4969 /* Check for the --config option */
4970 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4971 &option_index
)) != -1) {
4975 } else if (c
!= 'f') {
4976 /* if not equal to --config option. */
4980 if (lttng_is_setuid_setgid()) {
4981 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4984 config_path
= utils_expand_path(optarg
);
4986 ERR("Failed to resolve path: %s", optarg
);
4991 ret
= config_get_section_entries(config_path
, config_section_name
,
4992 config_entry_handler
, NULL
);
4995 ERR("Invalid configuration option at line %i", ret
);
5001 /* Reset getopt's global state */
5002 optopt
= orig_optopt
;
5003 optind
= orig_optind
;
5007 * getopt_long() will not set option_index if it encounters a
5010 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5017 * Pass NULL as the long option name if popt left the index
5020 ret
= set_option(c
, optarg
,
5021 option_index
< 0 ? NULL
:
5022 long_options
[option_index
].name
);
5034 * Creates the two needed socket by the daemon.
5035 * apps_sock - The communication socket for all UST apps.
5036 * client_sock - The communication of the cli tool (lttng).
5038 static int init_daemon_socket(void)
5043 old_umask
= umask(0);
5045 /* Create client tool unix socket */
5046 client_sock
= lttcomm_create_unix_sock(config
.client_unix_sock_path
.value
);
5047 if (client_sock
< 0) {
5048 ERR("Create unix sock failed: %s", config
.client_unix_sock_path
.value
);
5053 /* Set the cloexec flag */
5054 ret
= utils_set_fd_cloexec(client_sock
);
5056 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5057 "Continuing but note that the consumer daemon will have a "
5058 "reference to this socket on exec()", client_sock
);
5061 /* File permission MUST be 660 */
5062 ret
= chmod(config
.client_unix_sock_path
.value
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5064 ERR("Set file permissions failed: %s", config
.client_unix_sock_path
.value
);
5069 /* Create the application unix socket */
5070 apps_sock
= lttcomm_create_unix_sock(config
.apps_unix_sock_path
.value
);
5071 if (apps_sock
< 0) {
5072 ERR("Create unix sock failed: %s", config
.apps_unix_sock_path
.value
);
5077 /* Set the cloexec flag */
5078 ret
= utils_set_fd_cloexec(apps_sock
);
5080 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5081 "Continuing but note that the consumer daemon will have a "
5082 "reference to this socket on exec()", apps_sock
);
5085 /* File permission MUST be 666 */
5086 ret
= chmod(config
.apps_unix_sock_path
.value
,
5087 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5089 ERR("Set file permissions failed: %s", config
.apps_unix_sock_path
.value
);
5094 DBG3("Session daemon client socket %d and application socket %d created",
5095 client_sock
, apps_sock
);
5103 * Create lockfile using the rundir and return its fd.
5105 static int create_lockfile(void)
5107 return utils_create_lock_file(config
.lock_file_path
.value
);
5111 * Check if the global socket is available, and if a daemon is answering at the
5112 * other side. If yes, error is returned.
5114 * Also attempts to create and hold the lock file.
5116 static int check_existing_daemon(void)
5120 /* Is there anybody out there ? */
5121 if (lttng_session_daemon_alive()) {
5126 lockfile_fd
= create_lockfile();
5127 if (lockfile_fd
< 0) {
5135 static void sessiond_cleanup_lock_file(void)
5140 * Cleanup lock file by deleting it and finaly closing it which will
5141 * release the file system lock.
5143 if (lockfile_fd
>= 0) {
5144 ret
= remove(config
.lock_file_path
.value
);
5146 PERROR("remove lock file");
5148 ret
= close(lockfile_fd
);
5150 PERROR("close lock file");
5156 * Set the tracing group gid onto the client socket.
5158 * Race window between mkdir and chown is OK because we are going from more
5159 * permissive (root.root) to less permissive (root.tracing).
5161 static int set_permissions(char *rundir
)
5166 gid
= utils_get_group_id(config
.tracing_group_name
.value
);
5168 /* Set lttng run dir */
5169 ret
= chown(rundir
, 0, gid
);
5171 ERR("Unable to set group on %s", rundir
);
5176 * Ensure all applications and tracing group can search the run
5177 * dir. Allow everyone to read the directory, since it does not
5178 * buy us anything to hide its content.
5180 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5182 ERR("Unable to set permissions on %s", rundir
);
5186 /* lttng client socket path */
5187 ret
= chown(config
.client_unix_sock_path
.value
, 0, gid
);
5189 ERR("Unable to set group on %s", config
.client_unix_sock_path
.value
);
5193 /* kconsumer error socket path */
5194 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5196 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5200 /* 64-bit ustconsumer error socket path */
5201 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5203 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5207 /* 32-bit ustconsumer compat32 error socket path */
5208 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5210 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5214 DBG("All permissions are set");
5220 * Create the lttng run directory needed for all global sockets and pipe.
5222 static int create_lttng_rundir(void)
5226 DBG3("Creating LTTng run directory: %s", config
.rundir
.value
);
5228 ret
= mkdir(config
.rundir
.value
, S_IRWXU
);
5230 if (errno
!= EEXIST
) {
5231 ERR("Unable to create %s", config
.rundir
.value
);
5243 * Setup sockets and directory needed by the consumerds' communication with the
5246 static int set_consumer_sockets(struct consumer_data
*consumer_data
)
5251 switch (consumer_data
->type
) {
5252 case LTTNG_CONSUMER_KERNEL
:
5253 path
= config
.kconsumerd_path
.value
;
5255 case LTTNG_CONSUMER64_UST
:
5256 path
= config
.consumerd64_path
.value
;
5258 case LTTNG_CONSUMER32_UST
:
5259 path
= config
.consumerd32_path
.value
;
5262 ERR("Consumer type unknown");
5268 DBG2("Creating consumer directory: %s", path
);
5270 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5271 if (ret
< 0 && errno
!= EEXIST
) {
5273 ERR("Failed to create %s", path
);
5277 ret
= chown(path
, 0, utils_get_group_id(config
.tracing_group_name
.value
));
5279 ERR("Unable to set group on %s", path
);
5285 /* Create the consumerd error unix socket */
5286 consumer_data
->err_sock
=
5287 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5288 if (consumer_data
->err_sock
< 0) {
5289 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5295 * Set the CLOEXEC flag. Return code is useless because either way, the
5298 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5300 PERROR("utils_set_fd_cloexec");
5301 /* continue anyway */
5304 /* File permission MUST be 660 */
5305 ret
= chmod(consumer_data
->err_unix_sock_path
,
5306 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5308 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5318 * Signal handler for the daemon
5320 * Simply stop all worker threads, leaving main() return gracefully after
5321 * joining all threads and calling cleanup().
5323 static void sighandler(int sig
)
5327 DBG("SIGINT caught");
5331 DBG("SIGTERM caught");
5335 CMM_STORE_SHARED(recv_child_signal
, 1);
5343 * Setup signal handler for :
5344 * SIGINT, SIGTERM, SIGPIPE
5346 static int set_signal_handler(void)
5349 struct sigaction sa
;
5352 if ((ret
= sigemptyset(&sigset
)) < 0) {
5353 PERROR("sigemptyset");
5357 sa
.sa_mask
= sigset
;
5360 sa
.sa_handler
= sighandler
;
5361 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5362 PERROR("sigaction");
5366 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5367 PERROR("sigaction");
5371 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5372 PERROR("sigaction");
5376 sa
.sa_handler
= SIG_IGN
;
5377 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5378 PERROR("sigaction");
5382 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5388 * Set open files limit to unlimited. This daemon can open a large number of
5389 * file descriptors in order to consume multiple kernel traces.
5391 static void set_ulimit(void)
5396 /* The kernel does not allow an infinite limit for open files */
5397 lim
.rlim_cur
= 65535;
5398 lim
.rlim_max
= 65535;
5400 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5402 PERROR("failed to set open files limit");
5406 static int write_pidfile(void)
5408 return utils_create_pid_file(getpid(), config
.pid_file_path
.value
);
5411 static int set_clock_plugin_env(void)
5414 char *env_value
= NULL
;
5416 if (!config
.lttng_ust_clock_plugin
.value
) {
5420 ret
= asprintf(&env_value
, "LTTNG_UST_CLOCK_PLUGIN=%s",
5421 config
.lttng_ust_clock_plugin
.value
);
5427 ret
= putenv(env_value
);
5430 PERROR("putenv of LTTNG_UST_CLOCK_PLUGIN");
5434 DBG("Updated LTTNG_UST_CLOCK_PLUGIN environment variable to \"%s\"",
5435 config
.lttng_ust_clock_plugin
.value
);
5440 static void destroy_all_sessions_and_wait(void)
5442 struct ltt_session
*session
, *tmp
;
5443 struct ltt_session_list
*session_list
;
5445 session_list
= session_get_list();
5446 DBG("Initiating destruction of all sessions");
5448 if (!session_list
) {
5453 * Ensure that the client thread is no longer accepting new commands,
5454 * which could cause new sessions to be created.
5456 sessiond_wait_client_thread_stopped();
5458 session_lock_list();
5459 /* Initiate the destruction of all sessions. */
5460 cds_list_for_each_entry_safe(session
, tmp
,
5461 &session_list
->head
, list
) {
5462 if (!session_get(session
)) {
5466 session_lock(session
);
5467 if (session
->destroyed
) {
5468 goto unlock_session
;
5470 (void) cmd_destroy_session(session
,
5471 notification_thread_handle
);
5473 session_unlock(session
);
5474 session_put(session
);
5476 session_unlock_list();
5478 /* Wait for the destruction of all sessions to complete. */
5479 DBG("Waiting for the destruction of all sessions to complete");
5480 session_list_wait_empty();
5481 DBG("Destruction of all sessions completed");
5487 int main(int argc
, char **argv
)
5489 int ret
= 0, retval
= 0;
5491 const char *env_app_timeout
;
5492 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5493 *ust64_channel_monitor_pipe
= NULL
,
5494 *kernel_channel_monitor_pipe
= NULL
;
5495 bool timer_thread_launched
= false;
5496 struct lttng_thread
*ht_cleanup_thread
= NULL
;
5497 struct timer_thread_parameters timer_thread_ctx
;
5498 /* Rotation thread handle. */
5499 struct rotation_thread_handle
*rotation_thread_handle
= NULL
;
5500 /* Queue of rotation jobs populated by the sessiond-timer. */
5501 struct rotation_thread_timer_queue
*rotation_timer_queue
= NULL
;
5503 init_kernel_workarounds();
5505 rcu_register_thread();
5507 if (set_signal_handler()) {
5509 goto exit_set_signal_handler
;
5512 if (timer_signal_init()) {
5514 goto exit_set_signal_handler
;
5517 page_size
= sysconf(_SC_PAGESIZE
);
5518 if (page_size
< 0) {
5519 PERROR("sysconf _SC_PAGESIZE");
5520 page_size
= LONG_MAX
;
5521 WARN("Fallback page size to %ld", page_size
);
5524 ret
= sessiond_config_init(&config
);
5527 goto exit_set_signal_handler
;
5531 * Init config from environment variables.
5532 * Command line option override env configuration per-doc. Do env first.
5534 sessiond_config_apply_env_config(&config
);
5537 * Parse arguments and load the daemon configuration file.
5539 * We have an exit_options exit path to free memory reserved by
5540 * set_options. This is needed because the rest of sessiond_cleanup()
5541 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5542 * depends on set_options.
5545 if (set_options(argc
, argv
)) {
5551 * Resolve all paths received as arguments, configuration option, or
5552 * through environment variable as absolute paths. This is necessary
5553 * since daemonizing causes the sessiond's current working directory
5556 ret
= sessiond_config_resolve_paths(&config
);
5562 lttng_opt_verbose
= config
.verbose
;
5563 lttng_opt_quiet
= config
.quiet
;
5564 kconsumer_data
.err_unix_sock_path
=
5565 config
.kconsumerd_err_unix_sock_path
.value
;
5566 kconsumer_data
.cmd_unix_sock_path
=
5567 config
.kconsumerd_cmd_unix_sock_path
.value
;
5568 ustconsumer32_data
.err_unix_sock_path
=
5569 config
.consumerd32_err_unix_sock_path
.value
;
5570 ustconsumer32_data
.cmd_unix_sock_path
=
5571 config
.consumerd32_cmd_unix_sock_path
.value
;
5572 ustconsumer64_data
.err_unix_sock_path
=
5573 config
.consumerd64_err_unix_sock_path
.value
;
5574 ustconsumer64_data
.cmd_unix_sock_path
=
5575 config
.consumerd64_cmd_unix_sock_path
.value
;
5576 set_clock_plugin_env();
5578 sessiond_config_log(&config
);
5580 if (create_lttng_rundir()) {
5585 /* Abort launch if a session daemon is already running. */
5586 if (check_existing_daemon()) {
5587 ERR("A session daemon is already running.");
5593 if (config
.daemonize
|| config
.background
) {
5596 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5597 !config
.background
);
5604 * We are in the child. Make sure all other file descriptors are
5605 * closed, in case we are called with more opened file
5606 * descriptors than the standard ones and the lock file.
5608 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5609 if (i
== lockfile_fd
) {
5616 if (run_as_create_worker(argv
[0]) < 0) {
5617 goto exit_create_run_as_worker_cleanup
;
5621 * Starting from here, we can create threads. This needs to be after
5622 * lttng_daemonize due to RCU.
5626 * Initialize the health check subsystem. This call should set the
5627 * appropriate time values.
5629 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5630 if (!health_sessiond
) {
5631 PERROR("health_app_create error");
5633 goto exit_health_sessiond_cleanup
;
5636 /* Create thread to clean up RCU hash tables */
5637 ht_cleanup_thread
= launch_ht_cleanup_thread();
5638 if (!ht_cleanup_thread
) {
5640 goto exit_ht_cleanup
;
5643 /* Create thread quit pipe */
5644 if (sessiond_init_thread_quit_pipe()) {
5646 goto exit_init_data
;
5649 /* Check if daemon is UID = 0 */
5650 is_root
= !getuid();
5652 /* Create global run dir with root access */
5654 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5655 if (!kernel_channel_monitor_pipe
) {
5656 ERR("Failed to create kernel consumer channel monitor pipe");
5658 goto exit_init_data
;
5660 kconsumer_data
.channel_monitor_pipe
=
5661 lttng_pipe_release_writefd(
5662 kernel_channel_monitor_pipe
);
5663 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5665 goto exit_init_data
;
5669 /* Set consumer initial state */
5670 kernel_consumerd_state
= CONSUMER_STOPPED
;
5671 ust_consumerd_state
= CONSUMER_STOPPED
;
5673 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
5674 if (!ust32_channel_monitor_pipe
) {
5675 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
5677 goto exit_init_data
;
5679 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5680 ust32_channel_monitor_pipe
);
5681 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
5683 goto exit_init_data
;
5687 * The rotation_thread_timer_queue structure is shared between the
5688 * sessiond timer thread and the rotation thread. The main thread keeps
5689 * its ownership and destroys it when both threads have been joined.
5691 rotation_timer_queue
= rotation_thread_timer_queue_create();
5692 if (!rotation_timer_queue
) {
5694 goto exit_init_data
;
5696 timer_thread_ctx
.rotation_thread_job_queue
= rotation_timer_queue
;
5698 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
5699 if (!ust64_channel_monitor_pipe
) {
5700 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
5702 goto exit_init_data
;
5704 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
5705 ust64_channel_monitor_pipe
);
5706 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
5708 goto exit_init_data
;
5712 * Init UST app hash table. Alloc hash table before this point since
5713 * cleanup() can get called after that point.
5715 if (ust_app_ht_alloc()) {
5716 ERR("Failed to allocate UST app hash table");
5718 goto exit_init_data
;
5722 * Initialize agent app hash table. We allocate the hash table here
5723 * since cleanup() can get called after this point.
5725 if (agent_app_ht_alloc()) {
5726 ERR("Failed to allocate Agent app hash table");
5728 goto exit_init_data
;
5732 * These actions must be executed as root. We do that *after* setting up
5733 * the sockets path because we MUST make the check for another daemon using
5734 * those paths *before* trying to set the kernel consumer sockets and init
5738 if (set_consumer_sockets(&kconsumer_data
)) {
5740 goto exit_init_data
;
5743 /* Setup kernel tracer */
5744 if (!config
.no_kernel
) {
5745 init_kernel_tracer();
5746 if (kernel_tracer_fd
>= 0) {
5747 ret
= syscall_init_table();
5749 ERR("Unable to populate syscall table. "
5750 "Syscall tracing won't work "
5751 "for this session daemon.");
5756 /* Set ulimit for open files */
5759 /* init lttng_fd tracking must be done after set_ulimit. */
5762 if (set_consumer_sockets(&ustconsumer64_data
)) {
5764 goto exit_init_data
;
5767 if (set_consumer_sockets(&ustconsumer32_data
)) {
5769 goto exit_init_data
;
5772 /* Setup the needed unix socket */
5773 if (init_daemon_socket()) {
5775 goto exit_init_data
;
5778 /* Set credentials to socket */
5779 if (is_root
&& set_permissions(config
.rundir
.value
)) {
5781 goto exit_init_data
;
5784 /* Get parent pid if -S, --sig-parent is specified. */
5785 if (config
.sig_parent
) {
5789 /* Setup the kernel pipe for waking up the kernel thread */
5790 if (is_root
&& !config
.no_kernel
) {
5791 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5793 goto exit_init_data
;
5797 /* Setup the thread apps communication pipe. */
5798 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5800 goto exit_init_data
;
5803 /* Setup the thread apps notify communication pipe. */
5804 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5806 goto exit_init_data
;
5809 /* Initialize global buffer per UID and PID registry. */
5810 buffer_reg_init_uid_registry();
5811 buffer_reg_init_pid_registry();
5813 /* Init UST command queue. */
5814 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5818 /* Check for the application socket timeout env variable. */
5819 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5820 if (env_app_timeout
) {
5821 config
.app_socket_timeout
= atoi(env_app_timeout
);
5823 config
.app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5826 ret
= write_pidfile();
5828 ERR("Error in write_pidfile");
5830 goto exit_init_data
;
5833 /* Initialize communication library */
5835 /* Initialize TCP timeout values */
5836 lttcomm_inet_init();
5838 if (load_session_init_data(&load_info
) < 0) {
5840 goto exit_init_data
;
5842 load_info
->path
= config
.load_session_path
.value
;
5844 /* Create health-check thread. */
5845 if (!launch_health_management_thread()) {
5850 /* notification_thread_data acquires the pipes' read side. */
5851 notification_thread_handle
= notification_thread_handle_create(
5852 ust32_channel_monitor_pipe
,
5853 ust64_channel_monitor_pipe
,
5854 kernel_channel_monitor_pipe
);
5855 if (!notification_thread_handle
) {
5857 ERR("Failed to create notification thread shared data");
5858 goto exit_notification
;
5861 /* Create notification thread. */
5862 if (!launch_notification_thread(notification_thread_handle
)) {
5864 goto exit_notification
;
5868 /* Create timer thread. */
5869 ret
= pthread_create(&timer_thread
, default_pthread_attr(),
5870 timer_thread_func
, &timer_thread_ctx
);
5873 PERROR("pthread_create timer");
5876 goto exit_notification
;
5878 timer_thread_launched
= true;
5880 /* rotation_thread_data acquires the pipes' read side. */
5881 rotation_thread_handle
= rotation_thread_handle_create(
5882 rotation_timer_queue
,
5883 notification_thread_handle
);
5884 if (!rotation_thread_handle
) {
5886 ERR("Failed to create rotation thread shared data");
5891 /* Create rotation thread. */
5892 if (!launch_rotation_thread(rotation_thread_handle
)) {
5897 /* Create thread to manage the client socket */
5898 ret
= pthread_create(&client_thread
, default_pthread_attr(),
5899 thread_manage_clients
, (void *) NULL
);
5902 PERROR("pthread_create clients");
5908 /* Create thread to dispatch registration */
5909 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
5910 thread_dispatch_ust_registration
, (void *) NULL
);
5913 PERROR("pthread_create dispatch");
5919 /* Create thread to manage application registration. */
5920 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
5921 thread_registration_apps
, (void *) NULL
);
5924 PERROR("pthread_create registration");
5930 /* Create thread to manage application socket */
5931 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
5932 thread_manage_apps
, (void *) NULL
);
5935 PERROR("pthread_create apps");
5941 /* Create thread to manage application notify socket */
5942 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
5943 ust_thread_manage_notify
, (void *) NULL
);
5946 PERROR("pthread_create notify");
5949 goto exit_apps_notify
;
5952 /* Create agent registration thread. */
5953 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
5954 agent_thread_manage_registration
, (void *) NULL
);
5957 PERROR("pthread_create agent");
5960 goto exit_agent_reg
;
5963 /* Don't start this thread if kernel tracing is not requested nor root */
5964 if (is_root
&& !config
.no_kernel
) {
5965 /* Create kernel thread to manage kernel event */
5966 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
5967 thread_manage_kernel
, (void *) NULL
);
5970 PERROR("pthread_create kernel");
5977 /* Create session loading thread. */
5978 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
5979 thread_load_session
, load_info
);
5982 PERROR("pthread_create load_session_thread");
5985 goto exit_load_session
;
5989 * This is where we start awaiting program completion (e.g. through
5990 * signal that asks threads to teardown).
5993 ret
= pthread_join(load_session_thread
, &status
);
5996 PERROR("pthread_join load_session_thread");
6000 /* Initiate teardown once activity occurs on the quit pipe. */
6001 sessiond_wait_for_quit_pipe(-1U);
6002 destroy_all_sessions_and_wait();
6005 if (is_root
&& !config
.no_kernel
) {
6006 ret
= pthread_join(kernel_thread
, &status
);
6009 PERROR("pthread_join");
6015 ret
= pthread_join(agent_reg_thread
, &status
);
6018 PERROR("pthread_join agent");
6023 ret
= pthread_join(apps_notify_thread
, &status
);
6026 PERROR("pthread_join apps notify");
6031 ret
= pthread_join(apps_thread
, &status
);
6034 PERROR("pthread_join apps");
6039 ret
= pthread_join(reg_apps_thread
, &status
);
6042 PERROR("pthread_join");
6048 * Join dispatch thread after joining reg_apps_thread to ensure
6049 * we don't leak applications in the queue.
6051 ret
= pthread_join(dispatch_thread
, &status
);
6054 PERROR("pthread_join");
6059 ret
= pthread_join(client_thread
, &status
);
6062 PERROR("pthread_join");
6069 lttng_thread_list_shutdown_orphans();
6073 * Wait for all pending call_rcu work to complete before tearing
6074 * down data structures. call_rcu worker may be trying to
6075 * perform lookups in those structures.
6079 * sessiond_cleanup() is called when no other thread is running, except
6080 * the ht_cleanup thread, which is needed to destroy the hash tables.
6082 rcu_thread_online();
6086 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6087 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6088 * the queue is empty before shutting down the clean-up thread.
6092 if (timer_thread_launched
) {
6094 ret
= pthread_join(timer_thread
, &status
);
6097 PERROR("pthread_join timer thread");
6102 if (ht_cleanup_thread
) {
6103 lttng_thread_shutdown(ht_cleanup_thread
);
6104 lttng_thread_put(ht_cleanup_thread
);
6107 rcu_thread_offline();
6108 rcu_unregister_thread();
6110 if (rotation_thread_handle
) {
6111 rotation_thread_handle_destroy(rotation_thread_handle
);
6115 * After the rotation and timer thread have quit, we can safely destroy
6116 * the rotation_timer_queue.
6118 rotation_thread_timer_queue_destroy(rotation_timer_queue
);
6120 * The teardown of the notification system is performed after the
6121 * session daemon's teardown in order to allow it to be notified
6122 * of the active session and channels at the moment of the teardown.
6124 if (notification_thread_handle
) {
6125 notification_thread_handle_destroy(notification_thread_handle
);
6127 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6128 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6129 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6132 health_app_destroy(health_sessiond
);
6133 exit_health_sessiond_cleanup
:
6134 exit_create_run_as_worker_cleanup
:
6137 sessiond_cleanup_lock_file();
6138 sessiond_cleanup_options();
6140 exit_set_signal_handler
: