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/defaults.h>
45 #include <common/kernel-consumer/kernel-consumer.h>
46 #include <common/futex.h>
47 #include <common/relayd/relayd.h>
48 #include <common/utils.h>
49 #include <common/config/config.h>
51 #include "lttng-sessiond.h"
52 #include "buffer-registry.h"
59 #include "kernel-consumer.h"
63 #include "ust-consumer.h"
66 #include "health-sessiond.h"
67 #include "testpoint.h"
68 #include "ust-thread.h"
69 #include "jul-thread.h"
71 #define CONSUMERD_FILE "lttng-consumerd"
74 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
75 static int tracing_group_name_override
;
76 static char *opt_pidfile
;
77 static int opt_sig_parent
;
78 static int opt_verbose_consumer
;
79 static int opt_daemon
;
80 static int opt_no_kernel
;
81 static int is_root
; /* Set to 1 if the daemon is running as root */
82 static pid_t ppid
; /* Parent PID for --sig-parent option */
83 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
86 /* Set to 1 when a SIGUSR1 signal is received. */
87 static int recv_child_signal
;
90 * Consumer daemon specific control data. Every value not initialized here is
91 * set to 0 by the static definition.
93 static struct consumer_data kconsumer_data
= {
94 .type
= LTTNG_CONSUMER_KERNEL
,
95 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
96 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
99 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
100 .lock
= PTHREAD_MUTEX_INITIALIZER
,
101 .cond
= PTHREAD_COND_INITIALIZER
,
102 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
104 static struct consumer_data ustconsumer64_data
= {
105 .type
= LTTNG_CONSUMER64_UST
,
106 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
107 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
110 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
111 .lock
= PTHREAD_MUTEX_INITIALIZER
,
112 .cond
= PTHREAD_COND_INITIALIZER
,
113 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
115 static struct consumer_data ustconsumer32_data
= {
116 .type
= LTTNG_CONSUMER32_UST
,
117 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
118 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
121 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
122 .lock
= PTHREAD_MUTEX_INITIALIZER
,
123 .cond
= PTHREAD_COND_INITIALIZER
,
124 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
127 /* Command line options */
128 static const struct option long_options
[] = {
129 { "client-sock", 1, 0, 'c' },
130 { "apps-sock", 1, 0, 'a' },
131 { "kconsumerd-cmd-sock", 1, 0, 'C' },
132 { "kconsumerd-err-sock", 1, 0, 'E' },
133 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
134 { "ustconsumerd32-err-sock", 1, 0, 'H' },
135 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
136 { "ustconsumerd64-err-sock", 1, 0, 'F' },
137 { "consumerd32-path", 1, 0, 'u' },
138 { "consumerd32-libdir", 1, 0, 'U' },
139 { "consumerd64-path", 1, 0, 't' },
140 { "consumerd64-libdir", 1, 0, 'T' },
141 { "daemonize", 0, 0, 'd' },
142 { "sig-parent", 0, 0, 'S' },
143 { "help", 0, 0, 'h' },
144 { "group", 1, 0, 'g' },
145 { "version", 0, 0, 'V' },
146 { "quiet", 0, 0, 'q' },
147 { "verbose", 0, 0, 'v' },
148 { "verbose-consumer", 0, 0, 'Z' },
149 { "no-kernel", 0, 0, 'N' },
150 { "pidfile", 1, 0, 'p' },
151 { "jul-tcp-port", 1, 0, 'J' },
152 { "config", 1, 0, 'f' },
156 /* Command line options to ignore from configuration file */
157 static const char *config_ignore_options
[] = { "help", "version", "config" };
159 /* Shared between threads */
160 static int dispatch_thread_exit
;
162 /* Global application Unix socket path */
163 static char apps_unix_sock_path
[PATH_MAX
];
164 /* Global client Unix socket path */
165 static char client_unix_sock_path
[PATH_MAX
];
166 /* global wait shm path for UST */
167 static char wait_shm_path
[PATH_MAX
];
168 /* Global health check unix path */
169 static char health_unix_sock_path
[PATH_MAX
];
171 /* Sockets and FDs */
172 static int client_sock
= -1;
173 static int apps_sock
= -1;
174 int kernel_tracer_fd
= -1;
175 static int kernel_poll_pipe
[2] = { -1, -1 };
178 * Quit pipe for all threads. This permits a single cancellation point
179 * for all threads when receiving an event on the pipe.
181 static int thread_quit_pipe
[2] = { -1, -1 };
184 * This pipe is used to inform the thread managing application communication
185 * that a command is queued and ready to be processed.
187 static int apps_cmd_pipe
[2] = { -1, -1 };
189 int apps_cmd_notify_pipe
[2] = { -1, -1 };
191 /* Pthread, Mutexes and Semaphores */
192 static pthread_t apps_thread
;
193 static pthread_t apps_notify_thread
;
194 static pthread_t reg_apps_thread
;
195 static pthread_t client_thread
;
196 static pthread_t kernel_thread
;
197 static pthread_t dispatch_thread
;
198 static pthread_t health_thread
;
199 static pthread_t ht_cleanup_thread
;
200 static pthread_t jul_reg_thread
;
203 * UST registration command queue. This queue is tied with a futex and uses a N
204 * wakers / 1 waiter implemented and detailed in futex.c/.h
206 * The thread_manage_apps and thread_dispatch_ust_registration interact with
207 * this queue and the wait/wake scheme.
209 static struct ust_cmd_queue ust_cmd_queue
;
212 * Pointer initialized before thread creation.
214 * This points to the tracing session list containing the session count and a
215 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
216 * MUST NOT be taken if you call a public function in session.c.
218 * The lock is nested inside the structure: session_list_ptr->lock. Please use
219 * session_lock_list and session_unlock_list for lock acquisition.
221 static struct ltt_session_list
*session_list_ptr
;
223 int ust_consumerd64_fd
= -1;
224 int ust_consumerd32_fd
= -1;
226 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
227 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
228 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
229 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
230 static int consumerd32_bin_override
;
231 static int consumerd64_bin_override
;
232 static int consumerd32_libdir_override
;
233 static int consumerd64_libdir_override
;
235 static const char *module_proc_lttng
= "/proc/lttng";
238 * Consumer daemon state which is changed when spawning it, killing it or in
239 * case of a fatal error.
241 enum consumerd_state
{
242 CONSUMER_STARTED
= 1,
243 CONSUMER_STOPPED
= 2,
248 * This consumer daemon state is used to validate if a client command will be
249 * able to reach the consumer. If not, the client is informed. For instance,
250 * doing a "lttng start" when the consumer state is set to ERROR will return an
251 * error to the client.
253 * The following example shows a possible race condition of this scheme:
255 * consumer thread error happens
257 * client cmd checks state -> still OK
258 * consumer thread exit, sets error
259 * client cmd try to talk to consumer
262 * However, since the consumer is a different daemon, we have no way of making
263 * sure the command will reach it safely even with this state flag. This is why
264 * we consider that up to the state validation during command processing, the
265 * command is safe. After that, we can not guarantee the correctness of the
266 * client request vis-a-vis the consumer.
268 static enum consumerd_state ust_consumerd_state
;
269 static enum consumerd_state kernel_consumerd_state
;
272 * Socket timeout for receiving and sending in seconds.
274 static int app_socket_timeout
;
276 /* Set in main() with the current page size. */
279 /* Application health monitoring */
280 struct health_app
*health_sessiond
;
282 /* JUL TCP port for registration. Used by the JUL thread. */
283 unsigned int jul_tcp_port
= DEFAULT_JUL_TCP_PORT
;
285 const char * const config_section_name
= "sessiond";
288 * Whether sessiond is ready for commands/health check requests.
289 * NR_LTTNG_SESSIOND_READY must match the number of calls to
290 * lttng_sessiond_notify_ready().
292 #define NR_LTTNG_SESSIOND_READY 2
293 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
295 /* Notify parents that we are ready for cmd and health check */
297 void lttng_sessiond_notify_ready(void)
299 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
301 * Notify parent pid that we are ready to accept command
302 * for client side. This ppid is the one from the
303 * external process that spawned us.
305 if (opt_sig_parent
) {
310 * Notify the parent of the fork() process that we are
314 kill(child_ppid
, SIGUSR1
);
320 void setup_consumerd_path(void)
322 const char *bin
, *libdir
;
325 * Allow INSTALL_BIN_PATH to be used as a target path for the
326 * native architecture size consumer if CONFIG_CONSUMER*_PATH
327 * has not been defined.
329 #if (CAA_BITS_PER_LONG == 32)
330 if (!consumerd32_bin
[0]) {
331 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
333 if (!consumerd32_libdir
[0]) {
334 consumerd32_libdir
= INSTALL_LIB_PATH
;
336 #elif (CAA_BITS_PER_LONG == 64)
337 if (!consumerd64_bin
[0]) {
338 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
340 if (!consumerd64_libdir
[0]) {
341 consumerd64_libdir
= INSTALL_LIB_PATH
;
344 #error "Unknown bitness"
348 * runtime env. var. overrides the build default.
350 bin
= getenv("LTTNG_CONSUMERD32_BIN");
352 consumerd32_bin
= bin
;
354 bin
= getenv("LTTNG_CONSUMERD64_BIN");
356 consumerd64_bin
= bin
;
358 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
360 consumerd32_libdir
= libdir
;
362 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
364 consumerd64_libdir
= libdir
;
369 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
371 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
377 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
383 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
395 * Check if the thread quit pipe was triggered.
397 * Return 1 if it was triggered else 0;
399 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
401 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
409 * Init thread quit pipe.
411 * Return -1 on error or 0 if all pipes are created.
413 static int init_thread_quit_pipe(void)
417 ret
= pipe(thread_quit_pipe
);
419 PERROR("thread quit pipe");
423 for (i
= 0; i
< 2; i
++) {
424 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
436 * Stop all threads by closing the thread quit pipe.
438 static void stop_threads(void)
442 /* Stopping all threads */
443 DBG("Terminating all threads");
444 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
446 ERR("write error on thread quit pipe");
449 /* Dispatch thread */
450 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
451 futex_nto1_wake(&ust_cmd_queue
.futex
);
455 * Close every consumer sockets.
457 static void close_consumer_sockets(void)
461 if (kconsumer_data
.err_sock
>= 0) {
462 ret
= close(kconsumer_data
.err_sock
);
464 PERROR("kernel consumer err_sock close");
467 if (ustconsumer32_data
.err_sock
>= 0) {
468 ret
= close(ustconsumer32_data
.err_sock
);
470 PERROR("UST consumerd32 err_sock close");
473 if (ustconsumer64_data
.err_sock
>= 0) {
474 ret
= close(ustconsumer64_data
.err_sock
);
476 PERROR("UST consumerd64 err_sock close");
479 if (kconsumer_data
.cmd_sock
>= 0) {
480 ret
= close(kconsumer_data
.cmd_sock
);
482 PERROR("kernel consumer cmd_sock close");
485 if (ustconsumer32_data
.cmd_sock
>= 0) {
486 ret
= close(ustconsumer32_data
.cmd_sock
);
488 PERROR("UST consumerd32 cmd_sock close");
491 if (ustconsumer64_data
.cmd_sock
>= 0) {
492 ret
= close(ustconsumer64_data
.cmd_sock
);
494 PERROR("UST consumerd64 cmd_sock close");
502 static void cleanup(void)
505 struct ltt_session
*sess
, *stmp
;
511 * Close the thread quit pipe. It has already done its job,
512 * since we are now called.
514 utils_close_pipe(thread_quit_pipe
);
517 * If opt_pidfile is undefined, the default file will be wiped when
518 * removing the rundir.
521 ret
= remove(opt_pidfile
);
523 PERROR("remove pidfile %s", opt_pidfile
);
527 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
530 snprintf(path
, PATH_MAX
,
532 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
533 DBG("Removing %s", path
);
536 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
537 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
);
538 DBG("Removing %s", path
);
542 snprintf(path
, PATH_MAX
,
543 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
545 DBG("Removing %s", path
);
548 snprintf(path
, PATH_MAX
,
549 DEFAULT_KCONSUMERD_PATH
,
551 DBG("Removing directory %s", path
);
554 /* ust consumerd 32 */
555 snprintf(path
, PATH_MAX
,
556 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
558 DBG("Removing %s", path
);
561 snprintf(path
, PATH_MAX
,
562 DEFAULT_USTCONSUMERD32_PATH
,
564 DBG("Removing directory %s", path
);
567 /* ust consumerd 64 */
568 snprintf(path
, PATH_MAX
,
569 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
571 DBG("Removing %s", path
);
574 snprintf(path
, PATH_MAX
,
575 DEFAULT_USTCONSUMERD64_PATH
,
577 DBG("Removing directory %s", path
);
581 * We do NOT rmdir rundir because there are other processes
582 * using it, for instance lttng-relayd, which can start in
583 * parallel with this teardown.
588 DBG("Cleaning up all sessions");
590 /* Destroy session list mutex */
591 if (session_list_ptr
!= NULL
) {
592 pthread_mutex_destroy(&session_list_ptr
->lock
);
594 /* Cleanup ALL session */
595 cds_list_for_each_entry_safe(sess
, stmp
,
596 &session_list_ptr
->head
, list
) {
597 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
601 DBG("Closing all UST sockets");
602 ust_app_clean_list();
603 buffer_reg_destroy_registries();
605 if (is_root
&& !opt_no_kernel
) {
606 DBG2("Closing kernel fd");
607 if (kernel_tracer_fd
>= 0) {
608 ret
= close(kernel_tracer_fd
);
613 DBG("Unloading kernel modules");
614 modprobe_remove_lttng_all();
617 close_consumer_sockets();
620 * If the override option is set, the pointer points to a *non* const thus
621 * freeing it even though the variable type is set to const.
623 if (tracing_group_name_override
) {
624 free((void *) tracing_group_name
);
626 if (consumerd32_bin_override
) {
627 free((void *) consumerd32_bin
);
629 if (consumerd64_bin_override
) {
630 free((void *) consumerd64_bin
);
632 if (consumerd32_libdir_override
) {
633 free((void *) consumerd32_libdir
);
635 if (consumerd64_libdir_override
) {
636 free((void *) consumerd64_libdir
);
644 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
645 "Matthew, BEET driven development works!%c[%dm",
646 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
651 * Send data on a unix socket using the liblttsessiondcomm API.
653 * Return lttcomm error code.
655 static int send_unix_sock(int sock
, void *buf
, size_t len
)
657 /* Check valid length */
662 return lttcomm_send_unix_sock(sock
, buf
, len
);
666 * Free memory of a command context structure.
668 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
670 DBG("Clean command context structure");
672 if ((*cmd_ctx
)->llm
) {
673 free((*cmd_ctx
)->llm
);
675 if ((*cmd_ctx
)->lsm
) {
676 free((*cmd_ctx
)->lsm
);
684 * Notify UST applications using the shm mmap futex.
686 static int notify_ust_apps(int active
)
690 DBG("Notifying applications of session daemon state: %d", active
);
692 /* See shm.c for this call implying mmap, shm and futex calls */
693 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
694 if (wait_shm_mmap
== NULL
) {
698 /* Wake waiting process */
699 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
701 /* Apps notified successfully */
709 * Setup the outgoing data buffer for the response (llm) by allocating the
710 * right amount of memory and copying the original information from the lsm
713 * Return total size of the buffer pointed by buf.
715 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
721 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
722 if (cmd_ctx
->llm
== NULL
) {
728 /* Copy common data */
729 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
730 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
732 cmd_ctx
->llm
->data_size
= size
;
733 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
742 * Update the kernel poll set of all channel fd available over all tracing
743 * session. Add the wakeup pipe at the end of the set.
745 static int update_kernel_poll(struct lttng_poll_event
*events
)
748 struct ltt_session
*session
;
749 struct ltt_kernel_channel
*channel
;
751 DBG("Updating kernel poll set");
754 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
755 session_lock(session
);
756 if (session
->kernel_session
== NULL
) {
757 session_unlock(session
);
761 cds_list_for_each_entry(channel
,
762 &session
->kernel_session
->channel_list
.head
, list
) {
763 /* Add channel fd to the kernel poll set */
764 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
766 session_unlock(session
);
769 DBG("Channel fd %d added to kernel set", channel
->fd
);
771 session_unlock(session
);
773 session_unlock_list();
778 session_unlock_list();
783 * Find the channel fd from 'fd' over all tracing session. When found, check
784 * for new channel stream and send those stream fds to the kernel consumer.
786 * Useful for CPU hotplug feature.
788 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
791 struct ltt_session
*session
;
792 struct ltt_kernel_session
*ksess
;
793 struct ltt_kernel_channel
*channel
;
795 DBG("Updating kernel streams for channel fd %d", fd
);
798 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
799 session_lock(session
);
800 if (session
->kernel_session
== NULL
) {
801 session_unlock(session
);
804 ksess
= session
->kernel_session
;
806 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
807 if (channel
->fd
== fd
) {
808 DBG("Channel found, updating kernel streams");
809 ret
= kernel_open_channel_stream(channel
);
813 /* Update the stream global counter */
814 ksess
->stream_count_global
+= ret
;
817 * Have we already sent fds to the consumer? If yes, it means
818 * that tracing is started so it is safe to send our updated
821 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
822 struct lttng_ht_iter iter
;
823 struct consumer_socket
*socket
;
826 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
827 &iter
.iter
, socket
, node
.node
) {
828 pthread_mutex_lock(socket
->lock
);
829 ret
= kernel_consumer_send_channel_stream(socket
,
831 session
->output_traces
? 1 : 0);
832 pthread_mutex_unlock(socket
->lock
);
843 session_unlock(session
);
845 session_unlock_list();
849 session_unlock(session
);
850 session_unlock_list();
855 * For each tracing session, update newly registered apps. The session list
856 * lock MUST be acquired before calling this.
858 static void update_ust_app(int app_sock
)
860 struct ltt_session
*sess
, *stmp
;
862 /* Consumer is in an ERROR state. Stop any application update. */
863 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
864 /* Stop the update process since the consumer is dead. */
868 /* For all tracing session(s) */
869 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
871 if (sess
->ust_session
) {
872 ust_app_global_update(sess
->ust_session
, app_sock
);
874 session_unlock(sess
);
879 * This thread manage event coming from the kernel.
881 * Features supported in this thread:
884 static void *thread_manage_kernel(void *data
)
886 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
887 uint32_t revents
, nb_fd
;
889 struct lttng_poll_event events
;
891 DBG("[thread] Thread manage kernel started");
893 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
896 * This first step of the while is to clean this structure which could free
897 * non NULL pointers so initialize it before the loop.
899 lttng_poll_init(&events
);
901 if (testpoint(sessiond_thread_manage_kernel
)) {
902 goto error_testpoint
;
905 health_code_update();
907 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
908 goto error_testpoint
;
912 health_code_update();
914 if (update_poll_flag
== 1) {
915 /* Clean events object. We are about to populate it again. */
916 lttng_poll_clean(&events
);
918 ret
= sessiond_set_thread_pollset(&events
, 2);
920 goto error_poll_create
;
923 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
928 /* This will add the available kernel channel if any. */
929 ret
= update_kernel_poll(&events
);
933 update_poll_flag
= 0;
936 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
938 /* Poll infinite value of time */
941 ret
= lttng_poll_wait(&events
, -1);
945 * Restart interrupted system call.
947 if (errno
== EINTR
) {
951 } else if (ret
== 0) {
952 /* Should not happen since timeout is infinite */
953 ERR("Return value of poll is 0 with an infinite timeout.\n"
954 "This should not have happened! Continuing...");
960 for (i
= 0; i
< nb_fd
; i
++) {
961 /* Fetch once the poll data */
962 revents
= LTTNG_POLL_GETEV(&events
, i
);
963 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
965 health_code_update();
967 /* Thread quit pipe has been closed. Killing thread. */
968 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
974 /* Check for data on kernel pipe */
975 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
976 (void) lttng_read(kernel_poll_pipe
[0],
979 * Ret value is useless here, if this pipe gets any actions an
980 * update is required anyway.
982 update_poll_flag
= 1;
986 * New CPU detected by the kernel. Adding kernel stream to
987 * kernel session and updating the kernel consumer
989 if (revents
& LPOLLIN
) {
990 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
996 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
997 * and unregister kernel stream at this point.
1006 lttng_poll_clean(&events
);
1009 utils_close_pipe(kernel_poll_pipe
);
1010 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1013 ERR("Health error occurred in %s", __func__
);
1014 WARN("Kernel thread died unexpectedly. "
1015 "Kernel tracing can continue but CPU hotplug is disabled.");
1017 health_unregister(health_sessiond
);
1018 DBG("Kernel thread dying");
1023 * Signal pthread condition of the consumer data that the thread.
1025 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1027 pthread_mutex_lock(&data
->cond_mutex
);
1030 * The state is set before signaling. It can be any value, it's the waiter
1031 * job to correctly interpret this condition variable associated to the
1032 * consumer pthread_cond.
1034 * A value of 0 means that the corresponding thread of the consumer data
1035 * was not started. 1 indicates that the thread has started and is ready
1036 * for action. A negative value means that there was an error during the
1039 data
->consumer_thread_is_ready
= state
;
1040 (void) pthread_cond_signal(&data
->cond
);
1042 pthread_mutex_unlock(&data
->cond_mutex
);
1046 * This thread manage the consumer error sent back to the session daemon.
1048 static void *thread_manage_consumer(void *data
)
1050 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1051 uint32_t revents
, nb_fd
;
1052 enum lttcomm_return_code code
;
1053 struct lttng_poll_event events
;
1054 struct consumer_data
*consumer_data
= data
;
1056 DBG("[thread] Manage consumer started");
1058 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1060 health_code_update();
1063 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1064 * metadata_sock. Nothing more will be added to this poll set.
1066 ret
= sessiond_set_thread_pollset(&events
, 3);
1072 * The error socket here is already in a listening state which was done
1073 * just before spawning this thread to avoid a race between the consumer
1074 * daemon exec trying to connect and the listen() call.
1076 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1081 health_code_update();
1083 /* Infinite blocking call, waiting for transmission */
1085 health_poll_entry();
1087 if (testpoint(sessiond_thread_manage_consumer
)) {
1091 ret
= lttng_poll_wait(&events
, -1);
1095 * Restart interrupted system call.
1097 if (errno
== EINTR
) {
1105 for (i
= 0; i
< nb_fd
; i
++) {
1106 /* Fetch once the poll data */
1107 revents
= LTTNG_POLL_GETEV(&events
, i
);
1108 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1110 health_code_update();
1112 /* Thread quit pipe has been closed. Killing thread. */
1113 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1119 /* Event on the registration socket */
1120 if (pollfd
== consumer_data
->err_sock
) {
1121 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1122 ERR("consumer err socket poll error");
1128 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1134 * Set the CLOEXEC flag. Return code is useless because either way, the
1137 (void) utils_set_fd_cloexec(sock
);
1139 health_code_update();
1141 DBG2("Receiving code from consumer err_sock");
1143 /* Getting status code from kconsumerd */
1144 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1145 sizeof(enum lttcomm_return_code
));
1150 health_code_update();
1152 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1153 /* Connect both socket, command and metadata. */
1154 consumer_data
->cmd_sock
=
1155 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1156 consumer_data
->metadata_fd
=
1157 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1158 if (consumer_data
->cmd_sock
< 0
1159 || consumer_data
->metadata_fd
< 0) {
1160 PERROR("consumer connect cmd socket");
1161 /* On error, signal condition and quit. */
1162 signal_consumer_condition(consumer_data
, -1);
1165 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1166 /* Create metadata socket lock. */
1167 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1168 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1169 PERROR("zmalloc pthread mutex");
1173 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1175 signal_consumer_condition(consumer_data
, 1);
1176 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1177 DBG("Consumer metadata socket ready (fd: %d)",
1178 consumer_data
->metadata_fd
);
1180 ERR("consumer error when waiting for SOCK_READY : %s",
1181 lttcomm_get_readable_code(-code
));
1185 /* Remove the consumerd error sock since we've established a connexion */
1186 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1191 /* Add new accepted error socket. */
1192 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1197 /* Add metadata socket that is successfully connected. */
1198 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1199 LPOLLIN
| LPOLLRDHUP
);
1204 health_code_update();
1206 /* Infinite blocking call, waiting for transmission */
1209 health_poll_entry();
1210 ret
= lttng_poll_wait(&events
, -1);
1214 * Restart interrupted system call.
1216 if (errno
== EINTR
) {
1224 for (i
= 0; i
< nb_fd
; i
++) {
1225 /* Fetch once the poll data */
1226 revents
= LTTNG_POLL_GETEV(&events
, i
);
1227 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1229 health_code_update();
1231 /* Thread quit pipe has been closed. Killing thread. */
1232 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1238 if (pollfd
== sock
) {
1239 /* Event on the consumerd socket */
1240 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1241 ERR("consumer err socket second poll error");
1244 health_code_update();
1245 /* Wait for any kconsumerd error */
1246 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1247 sizeof(enum lttcomm_return_code
));
1249 ERR("consumer closed the command socket");
1253 ERR("consumer return code : %s",
1254 lttcomm_get_readable_code(-code
));
1257 } else if (pollfd
== consumer_data
->metadata_fd
) {
1258 /* UST metadata requests */
1259 ret
= ust_consumer_metadata_request(
1260 &consumer_data
->metadata_sock
);
1262 ERR("Handling metadata request");
1267 ERR("Unknown pollfd");
1271 health_code_update();
1277 * We lock here because we are about to close the sockets and some other
1278 * thread might be using them so get exclusive access which will abort all
1279 * other consumer command by other threads.
1281 pthread_mutex_lock(&consumer_data
->lock
);
1283 /* Immediately set the consumerd state to stopped */
1284 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1285 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1286 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1287 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1288 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1290 /* Code flow error... */
1294 if (consumer_data
->err_sock
>= 0) {
1295 ret
= close(consumer_data
->err_sock
);
1299 consumer_data
->err_sock
= -1;
1301 if (consumer_data
->cmd_sock
>= 0) {
1302 ret
= close(consumer_data
->cmd_sock
);
1306 consumer_data
->cmd_sock
= -1;
1308 if (*consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1309 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1322 unlink(consumer_data
->err_unix_sock_path
);
1323 unlink(consumer_data
->cmd_unix_sock_path
);
1324 consumer_data
->pid
= 0;
1325 pthread_mutex_unlock(&consumer_data
->lock
);
1327 /* Cleanup metadata socket mutex. */
1328 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1329 free(consumer_data
->metadata_sock
.lock
);
1331 lttng_poll_clean(&events
);
1335 ERR("Health error occurred in %s", __func__
);
1337 health_unregister(health_sessiond
);
1338 DBG("consumer thread cleanup completed");
1344 * This thread manage application communication.
1346 static void *thread_manage_apps(void *data
)
1348 int i
, ret
, pollfd
, err
= -1;
1350 uint32_t revents
, nb_fd
;
1351 struct lttng_poll_event events
;
1353 DBG("[thread] Manage application started");
1355 rcu_register_thread();
1356 rcu_thread_online();
1358 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1360 if (testpoint(sessiond_thread_manage_apps
)) {
1361 goto error_testpoint
;
1364 health_code_update();
1366 ret
= sessiond_set_thread_pollset(&events
, 2);
1368 goto error_poll_create
;
1371 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1376 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1380 health_code_update();
1383 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1385 /* Inifinite blocking call, waiting for transmission */
1387 health_poll_entry();
1388 ret
= lttng_poll_wait(&events
, -1);
1392 * Restart interrupted system call.
1394 if (errno
== EINTR
) {
1402 for (i
= 0; i
< nb_fd
; i
++) {
1403 /* Fetch once the poll data */
1404 revents
= LTTNG_POLL_GETEV(&events
, i
);
1405 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1407 health_code_update();
1409 /* Thread quit pipe has been closed. Killing thread. */
1410 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1416 /* Inspect the apps cmd pipe */
1417 if (pollfd
== apps_cmd_pipe
[0]) {
1418 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1419 ERR("Apps command pipe error");
1421 } else if (revents
& LPOLLIN
) {
1425 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1426 if (size_ret
< sizeof(sock
)) {
1427 PERROR("read apps cmd pipe");
1431 health_code_update();
1434 * We only monitor the error events of the socket. This
1435 * thread does not handle any incoming data from UST
1438 ret
= lttng_poll_add(&events
, sock
,
1439 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1444 DBG("Apps with sock %d added to poll set", sock
);
1448 * At this point, we know that a registered application made
1449 * the event at poll_wait.
1451 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1452 /* Removing from the poll set */
1453 ret
= lttng_poll_del(&events
, pollfd
);
1458 /* Socket closed on remote end. */
1459 ust_app_unregister(pollfd
);
1463 health_code_update();
1469 lttng_poll_clean(&events
);
1472 utils_close_pipe(apps_cmd_pipe
);
1473 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1476 * We don't clean the UST app hash table here since already registered
1477 * applications can still be controlled so let them be until the session
1478 * daemon dies or the applications stop.
1483 ERR("Health error occurred in %s", __func__
);
1485 health_unregister(health_sessiond
);
1486 DBG("Application communication apps thread cleanup complete");
1487 rcu_thread_offline();
1488 rcu_unregister_thread();
1493 * Send a socket to a thread This is called from the dispatch UST registration
1494 * thread once all sockets are set for the application.
1496 * The sock value can be invalid, we don't really care, the thread will handle
1497 * it and make the necessary cleanup if so.
1499 * On success, return 0 else a negative value being the errno message of the
1502 static int send_socket_to_thread(int fd
, int sock
)
1507 * It's possible that the FD is set as invalid with -1 concurrently just
1508 * before calling this function being a shutdown state of the thread.
1515 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1516 if (ret
< sizeof(sock
)) {
1517 PERROR("write apps pipe %d", fd
);
1524 /* All good. Don't send back the write positive ret value. */
1531 * Sanitize the wait queue of the dispatch registration thread meaning removing
1532 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1533 * notify socket is never received.
1535 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1537 int ret
, nb_fd
= 0, i
;
1538 unsigned int fd_added
= 0;
1539 struct lttng_poll_event events
;
1540 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1544 lttng_poll_init(&events
);
1546 /* Just skip everything for an empty queue. */
1547 if (!wait_queue
->count
) {
1551 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1556 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1557 &wait_queue
->head
, head
) {
1558 assert(wait_node
->app
);
1559 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1560 LPOLLHUP
| LPOLLERR
);
1573 * Poll but don't block so we can quickly identify the faulty events and
1574 * clean them afterwards from the wait queue.
1576 ret
= lttng_poll_wait(&events
, 0);
1582 for (i
= 0; i
< nb_fd
; i
++) {
1583 /* Get faulty FD. */
1584 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1585 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1587 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1588 &wait_queue
->head
, head
) {
1589 if (pollfd
== wait_node
->app
->sock
&&
1590 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1591 cds_list_del(&wait_node
->head
);
1592 wait_queue
->count
--;
1593 ust_app_destroy(wait_node
->app
);
1601 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1605 lttng_poll_clean(&events
);
1609 lttng_poll_clean(&events
);
1611 ERR("Unable to sanitize wait queue");
1616 * Dispatch request from the registration threads to the application
1617 * communication thread.
1619 static void *thread_dispatch_ust_registration(void *data
)
1622 struct cds_wfq_node
*node
;
1623 struct ust_command
*ust_cmd
= NULL
;
1624 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1625 struct ust_reg_wait_queue wait_queue
= {
1629 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1631 health_code_update();
1633 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1635 DBG("[thread] Dispatch UST command started");
1637 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1638 health_code_update();
1640 /* Atomically prepare the queue futex */
1641 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1644 struct ust_app
*app
= NULL
;
1648 * Make sure we don't have node(s) that have hung up before receiving
1649 * the notify socket. This is to clean the list in order to avoid
1650 * memory leaks from notify socket that are never seen.
1652 sanitize_wait_queue(&wait_queue
);
1654 health_code_update();
1655 /* Dequeue command for registration */
1656 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1658 DBG("Woken up but nothing in the UST command queue");
1659 /* Continue thread execution */
1663 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1665 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1666 " gid:%d sock:%d name:%s (version %d.%d)",
1667 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1668 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1669 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1670 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1672 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1673 wait_node
= zmalloc(sizeof(*wait_node
));
1675 PERROR("zmalloc wait_node dispatch");
1676 ret
= close(ust_cmd
->sock
);
1678 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1680 lttng_fd_put(LTTNG_FD_APPS
, 1);
1684 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1686 /* Create application object if socket is CMD. */
1687 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1689 if (!wait_node
->app
) {
1690 ret
= close(ust_cmd
->sock
);
1692 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1694 lttng_fd_put(LTTNG_FD_APPS
, 1);
1700 * Add application to the wait queue so we can set the notify
1701 * socket before putting this object in the global ht.
1703 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1708 * We have to continue here since we don't have the notify
1709 * socket and the application MUST be added to the hash table
1710 * only at that moment.
1715 * Look for the application in the local wait queue and set the
1716 * notify socket if found.
1718 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1719 &wait_queue
.head
, head
) {
1720 health_code_update();
1721 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1722 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1723 cds_list_del(&wait_node
->head
);
1725 app
= wait_node
->app
;
1727 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1733 * With no application at this stage the received socket is
1734 * basically useless so close it before we free the cmd data
1735 * structure for good.
1738 ret
= close(ust_cmd
->sock
);
1740 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1742 lttng_fd_put(LTTNG_FD_APPS
, 1);
1749 * @session_lock_list
1751 * Lock the global session list so from the register up to the
1752 * registration done message, no thread can see the application
1753 * and change its state.
1755 session_lock_list();
1759 * Add application to the global hash table. This needs to be
1760 * done before the update to the UST registry can locate the
1765 /* Set app version. This call will print an error if needed. */
1766 (void) ust_app_version(app
);
1768 /* Send notify socket through the notify pipe. */
1769 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1773 session_unlock_list();
1775 * No notify thread, stop the UST tracing. However, this is
1776 * not an internal error of the this thread thus setting
1777 * the health error code to a normal exit.
1784 * Update newly registered application with the tracing
1785 * registry info already enabled information.
1787 update_ust_app(app
->sock
);
1790 * Don't care about return value. Let the manage apps threads
1791 * handle app unregistration upon socket close.
1793 (void) ust_app_register_done(app
->sock
);
1796 * Even if the application socket has been closed, send the app
1797 * to the thread and unregistration will take place at that
1800 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1803 session_unlock_list();
1805 * No apps. thread, stop the UST tracing. However, this is
1806 * not an internal error of the this thread thus setting
1807 * the health error code to a normal exit.
1814 session_unlock_list();
1816 } while (node
!= NULL
);
1818 health_poll_entry();
1819 /* Futex wait on queue. Blocking call on futex() */
1820 futex_nto1_wait(&ust_cmd_queue
.futex
);
1823 /* Normal exit, no error */
1827 /* Clean up wait queue. */
1828 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1829 &wait_queue
.head
, head
) {
1830 cds_list_del(&wait_node
->head
);
1835 DBG("Dispatch thread dying");
1838 ERR("Health error occurred in %s", __func__
);
1840 health_unregister(health_sessiond
);
1845 * This thread manage application registration.
1847 static void *thread_registration_apps(void *data
)
1849 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1850 uint32_t revents
, nb_fd
;
1851 struct lttng_poll_event events
;
1853 * Get allocated in this thread, enqueued to a global queue, dequeued and
1854 * freed in the manage apps thread.
1856 struct ust_command
*ust_cmd
= NULL
;
1858 DBG("[thread] Manage application registration started");
1860 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1862 if (testpoint(sessiond_thread_registration_apps
)) {
1863 goto error_testpoint
;
1866 ret
= lttcomm_listen_unix_sock(apps_sock
);
1872 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1873 * more will be added to this poll set.
1875 ret
= sessiond_set_thread_pollset(&events
, 2);
1877 goto error_create_poll
;
1880 /* Add the application registration socket */
1881 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1883 goto error_poll_add
;
1886 /* Notify all applications to register */
1887 ret
= notify_ust_apps(1);
1889 ERR("Failed to notify applications or create the wait shared memory.\n"
1890 "Execution continues but there might be problem for already\n"
1891 "running applications that wishes to register.");
1895 DBG("Accepting application registration");
1897 /* Inifinite blocking call, waiting for transmission */
1899 health_poll_entry();
1900 ret
= lttng_poll_wait(&events
, -1);
1904 * Restart interrupted system call.
1906 if (errno
== EINTR
) {
1914 for (i
= 0; i
< nb_fd
; i
++) {
1915 health_code_update();
1917 /* Fetch once the poll data */
1918 revents
= LTTNG_POLL_GETEV(&events
, i
);
1919 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1921 /* Thread quit pipe has been closed. Killing thread. */
1922 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1928 /* Event on the registration socket */
1929 if (pollfd
== apps_sock
) {
1930 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1931 ERR("Register apps socket poll error");
1933 } else if (revents
& LPOLLIN
) {
1934 sock
= lttcomm_accept_unix_sock(apps_sock
);
1940 * Set socket timeout for both receiving and ending.
1941 * app_socket_timeout is in seconds, whereas
1942 * lttcomm_setsockopt_rcv_timeout and
1943 * lttcomm_setsockopt_snd_timeout expect msec as
1946 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1947 app_socket_timeout
* 1000);
1948 (void) lttcomm_setsockopt_snd_timeout(sock
,
1949 app_socket_timeout
* 1000);
1952 * Set the CLOEXEC flag. Return code is useless because
1953 * either way, the show must go on.
1955 (void) utils_set_fd_cloexec(sock
);
1957 /* Create UST registration command for enqueuing */
1958 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1959 if (ust_cmd
== NULL
) {
1960 PERROR("ust command zmalloc");
1965 * Using message-based transmissions to ensure we don't
1966 * have to deal with partially received messages.
1968 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1970 ERR("Exhausted file descriptors allowed for applications.");
1980 health_code_update();
1981 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
1984 /* Close socket of the application. */
1989 lttng_fd_put(LTTNG_FD_APPS
, 1);
1993 health_code_update();
1995 ust_cmd
->sock
= sock
;
1998 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1999 " gid:%d sock:%d name:%s (version %d.%d)",
2000 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2001 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2002 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2003 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2006 * Lock free enqueue the registration request. The red pill
2007 * has been taken! This apps will be part of the *system*.
2009 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
2012 * Wake the registration queue futex. Implicit memory
2013 * barrier with the exchange in cds_wfq_enqueue.
2015 futex_nto1_wake(&ust_cmd_queue
.futex
);
2025 ERR("Health error occurred in %s", __func__
);
2028 /* Notify that the registration thread is gone */
2031 if (apps_sock
>= 0) {
2032 ret
= close(apps_sock
);
2042 lttng_fd_put(LTTNG_FD_APPS
, 1);
2044 unlink(apps_unix_sock_path
);
2047 lttng_poll_clean(&events
);
2051 DBG("UST Registration thread cleanup complete");
2052 health_unregister(health_sessiond
);
2058 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2059 * exec or it will fails.
2061 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2064 struct timespec timeout
;
2066 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2067 consumer_data
->consumer_thread_is_ready
= 0;
2069 /* Setup pthread condition */
2070 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2073 PERROR("pthread_condattr_init consumer data");
2078 * Set the monotonic clock in order to make sure we DO NOT jump in time
2079 * between the clock_gettime() call and the timedwait call. See bug #324
2080 * for a more details and how we noticed it.
2082 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2085 PERROR("pthread_condattr_setclock consumer data");
2089 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2092 PERROR("pthread_cond_init consumer data");
2096 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2099 PERROR("pthread_create consumer");
2104 /* We are about to wait on a pthread condition */
2105 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2107 /* Get time for sem_timedwait absolute timeout */
2108 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2110 * Set the timeout for the condition timed wait even if the clock gettime
2111 * call fails since we might loop on that call and we want to avoid to
2112 * increment the timeout too many times.
2114 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2117 * The following loop COULD be skipped in some conditions so this is why we
2118 * set ret to 0 in order to make sure at least one round of the loop is
2124 * Loop until the condition is reached or when a timeout is reached. Note
2125 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2126 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2127 * possible. This loop does not take any chances and works with both of
2130 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2131 if (clock_ret
< 0) {
2132 PERROR("clock_gettime spawn consumer");
2133 /* Infinite wait for the consumerd thread to be ready */
2134 ret
= pthread_cond_wait(&consumer_data
->cond
,
2135 &consumer_data
->cond_mutex
);
2137 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2138 &consumer_data
->cond_mutex
, &timeout
);
2142 /* Release the pthread condition */
2143 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2147 if (ret
== ETIMEDOUT
) {
2149 * Call has timed out so we kill the kconsumerd_thread and return
2152 ERR("Condition timed out. The consumer thread was never ready."
2154 ret
= pthread_cancel(consumer_data
->thread
);
2156 PERROR("pthread_cancel consumer thread");
2159 PERROR("pthread_cond_wait failed consumer thread");
2164 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2165 if (consumer_data
->pid
== 0) {
2166 ERR("Consumerd did not start");
2167 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2170 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2179 * Join consumer thread
2181 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2185 /* Consumer pid must be a real one. */
2186 if (consumer_data
->pid
> 0) {
2188 ret
= kill(consumer_data
->pid
, SIGTERM
);
2190 ERR("Error killing consumer daemon");
2193 return pthread_join(consumer_data
->thread
, &status
);
2200 * Fork and exec a consumer daemon (consumerd).
2202 * Return pid if successful else -1.
2204 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2208 const char *consumer_to_use
;
2209 const char *verbosity
;
2212 DBG("Spawning consumerd");
2219 if (opt_verbose_consumer
) {
2220 verbosity
= "--verbose";
2222 verbosity
= "--quiet";
2224 switch (consumer_data
->type
) {
2225 case LTTNG_CONSUMER_KERNEL
:
2227 * Find out which consumerd to execute. We will first try the
2228 * 64-bit path, then the sessiond's installation directory, and
2229 * fallback on the 32-bit one,
2231 DBG3("Looking for a kernel consumer at these locations:");
2232 DBG3(" 1) %s", consumerd64_bin
);
2233 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2234 DBG3(" 3) %s", consumerd32_bin
);
2235 if (stat(consumerd64_bin
, &st
) == 0) {
2236 DBG3("Found location #1");
2237 consumer_to_use
= consumerd64_bin
;
2238 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2239 DBG3("Found location #2");
2240 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2241 } else if (stat(consumerd32_bin
, &st
) == 0) {
2242 DBG3("Found location #3");
2243 consumer_to_use
= consumerd32_bin
;
2245 DBG("Could not find any valid consumerd executable");
2248 DBG("Using kernel consumer at: %s", consumer_to_use
);
2249 execl(consumer_to_use
,
2250 "lttng-consumerd", verbosity
, "-k",
2251 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2252 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2253 "--group", tracing_group_name
,
2256 case LTTNG_CONSUMER64_UST
:
2258 char *tmpnew
= NULL
;
2260 if (consumerd64_libdir
[0] != '\0') {
2264 tmp
= getenv("LD_LIBRARY_PATH");
2268 tmplen
= strlen("LD_LIBRARY_PATH=")
2269 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2270 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2275 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2276 strcat(tmpnew
, consumerd64_libdir
);
2277 if (tmp
[0] != '\0') {
2278 strcat(tmpnew
, ":");
2279 strcat(tmpnew
, tmp
);
2281 ret
= putenv(tmpnew
);
2288 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2289 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2290 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2291 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2292 "--group", tracing_group_name
,
2294 if (consumerd64_libdir
[0] != '\0') {
2302 case LTTNG_CONSUMER32_UST
:
2304 char *tmpnew
= NULL
;
2306 if (consumerd32_libdir
[0] != '\0') {
2310 tmp
= getenv("LD_LIBRARY_PATH");
2314 tmplen
= strlen("LD_LIBRARY_PATH=")
2315 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2316 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2321 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2322 strcat(tmpnew
, consumerd32_libdir
);
2323 if (tmp
[0] != '\0') {
2324 strcat(tmpnew
, ":");
2325 strcat(tmpnew
, tmp
);
2327 ret
= putenv(tmpnew
);
2334 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2335 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2336 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2337 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2338 "--group", tracing_group_name
,
2340 if (consumerd32_libdir
[0] != '\0') {
2349 PERROR("unknown consumer type");
2353 PERROR("kernel start consumer exec");
2356 } else if (pid
> 0) {
2359 PERROR("start consumer fork");
2367 * Spawn the consumerd daemon and session daemon thread.
2369 static int start_consumerd(struct consumer_data
*consumer_data
)
2374 * Set the listen() state on the socket since there is a possible race
2375 * between the exec() of the consumer daemon and this call if place in the
2376 * consumer thread. See bug #366 for more details.
2378 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2383 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2384 if (consumer_data
->pid
!= 0) {
2385 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2389 ret
= spawn_consumerd(consumer_data
);
2391 ERR("Spawning consumerd failed");
2392 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2396 /* Setting up the consumer_data pid */
2397 consumer_data
->pid
= ret
;
2398 DBG2("Consumer pid %d", consumer_data
->pid
);
2399 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2401 DBG2("Spawning consumer control thread");
2402 ret
= spawn_consumer_thread(consumer_data
);
2404 ERR("Fatal error spawning consumer control thread");
2412 /* Cleanup already created sockets on error. */
2413 if (consumer_data
->err_sock
>= 0) {
2416 err
= close(consumer_data
->err_sock
);
2418 PERROR("close consumer data error socket");
2425 * Setup necessary data for kernel tracer action.
2427 static int init_kernel_tracer(void)
2431 /* Modprobe lttng kernel modules */
2432 ret
= modprobe_lttng_control();
2437 /* Open debugfs lttng */
2438 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2439 if (kernel_tracer_fd
< 0) {
2440 DBG("Failed to open %s", module_proc_lttng
);
2445 /* Validate kernel version */
2446 ret
= kernel_validate_version(kernel_tracer_fd
);
2451 ret
= modprobe_lttng_data();
2456 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2460 modprobe_remove_lttng_control();
2461 ret
= close(kernel_tracer_fd
);
2465 kernel_tracer_fd
= -1;
2466 return LTTNG_ERR_KERN_VERSION
;
2469 ret
= close(kernel_tracer_fd
);
2475 modprobe_remove_lttng_control();
2478 WARN("No kernel tracer available");
2479 kernel_tracer_fd
= -1;
2481 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2483 return LTTNG_ERR_KERN_NA
;
2489 * Copy consumer output from the tracing session to the domain session. The
2490 * function also applies the right modification on a per domain basis for the
2491 * trace files destination directory.
2493 * Should *NOT* be called with RCU read-side lock held.
2495 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2498 const char *dir_name
;
2499 struct consumer_output
*consumer
;
2502 assert(session
->consumer
);
2505 case LTTNG_DOMAIN_KERNEL
:
2506 DBG3("Copying tracing session consumer output in kernel session");
2508 * XXX: We should audit the session creation and what this function
2509 * does "extra" in order to avoid a destroy since this function is used
2510 * in the domain session creation (kernel and ust) only. Same for UST
2513 if (session
->kernel_session
->consumer
) {
2514 consumer_destroy_output(session
->kernel_session
->consumer
);
2516 session
->kernel_session
->consumer
=
2517 consumer_copy_output(session
->consumer
);
2518 /* Ease our life a bit for the next part */
2519 consumer
= session
->kernel_session
->consumer
;
2520 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2522 case LTTNG_DOMAIN_JUL
:
2523 case LTTNG_DOMAIN_UST
:
2524 DBG3("Copying tracing session consumer output in UST session");
2525 if (session
->ust_session
->consumer
) {
2526 consumer_destroy_output(session
->ust_session
->consumer
);
2528 session
->ust_session
->consumer
=
2529 consumer_copy_output(session
->consumer
);
2530 /* Ease our life a bit for the next part */
2531 consumer
= session
->ust_session
->consumer
;
2532 dir_name
= DEFAULT_UST_TRACE_DIR
;
2535 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2539 /* Append correct directory to subdir */
2540 strncat(consumer
->subdir
, dir_name
,
2541 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2542 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2551 * Create an UST session and add it to the session ust list.
2553 * Should *NOT* be called with RCU read-side lock held.
2555 static int create_ust_session(struct ltt_session
*session
,
2556 struct lttng_domain
*domain
)
2559 struct ltt_ust_session
*lus
= NULL
;
2563 assert(session
->consumer
);
2565 switch (domain
->type
) {
2566 case LTTNG_DOMAIN_JUL
:
2567 case LTTNG_DOMAIN_UST
:
2570 ERR("Unknown UST domain on create session %d", domain
->type
);
2571 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2575 DBG("Creating UST session");
2577 lus
= trace_ust_create_session(session
->id
);
2579 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2583 lus
->uid
= session
->uid
;
2584 lus
->gid
= session
->gid
;
2585 lus
->output_traces
= session
->output_traces
;
2586 lus
->snapshot_mode
= session
->snapshot_mode
;
2587 lus
->live_timer_interval
= session
->live_timer
;
2588 session
->ust_session
= lus
;
2590 /* Copy session output to the newly created UST session */
2591 ret
= copy_session_consumer(domain
->type
, session
);
2592 if (ret
!= LTTNG_OK
) {
2600 session
->ust_session
= NULL
;
2605 * Create a kernel tracer session then create the default channel.
2607 static int create_kernel_session(struct ltt_session
*session
)
2611 DBG("Creating kernel session");
2613 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2615 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2619 /* Code flow safety */
2620 assert(session
->kernel_session
);
2622 /* Copy session output to the newly created Kernel session */
2623 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2624 if (ret
!= LTTNG_OK
) {
2628 /* Create directory(ies) on local filesystem. */
2629 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2630 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2631 ret
= run_as_mkdir_recursive(
2632 session
->kernel_session
->consumer
->dst
.trace_path
,
2633 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2635 if (ret
!= -EEXIST
) {
2636 ERR("Trace directory creation error");
2642 session
->kernel_session
->uid
= session
->uid
;
2643 session
->kernel_session
->gid
= session
->gid
;
2644 session
->kernel_session
->output_traces
= session
->output_traces
;
2645 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2650 trace_kernel_destroy_session(session
->kernel_session
);
2651 session
->kernel_session
= NULL
;
2656 * Count number of session permitted by uid/gid.
2658 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2661 struct ltt_session
*session
;
2663 DBG("Counting number of available session for UID %d GID %d",
2665 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2667 * Only list the sessions the user can control.
2669 if (!session_access_ok(session
, uid
, gid
)) {
2678 * Process the command requested by the lttng client within the command
2679 * context structure. This function make sure that the return structure (llm)
2680 * is set and ready for transmission before returning.
2682 * Return any error encountered or 0 for success.
2684 * "sock" is only used for special-case var. len data.
2686 * Should *NOT* be called with RCU read-side lock held.
2688 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2692 int need_tracing_session
= 1;
2695 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2699 switch (cmd_ctx
->lsm
->cmd_type
) {
2700 case LTTNG_CREATE_SESSION
:
2701 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2702 case LTTNG_CREATE_SESSION_LIVE
:
2703 case LTTNG_DESTROY_SESSION
:
2704 case LTTNG_LIST_SESSIONS
:
2705 case LTTNG_LIST_DOMAINS
:
2706 case LTTNG_START_TRACE
:
2707 case LTTNG_STOP_TRACE
:
2708 case LTTNG_DATA_PENDING
:
2709 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2710 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2711 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2712 case LTTNG_SNAPSHOT_RECORD
:
2719 if (opt_no_kernel
&& need_domain
2720 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2722 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2724 ret
= LTTNG_ERR_KERN_NA
;
2729 /* Deny register consumer if we already have a spawned consumer. */
2730 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2731 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2732 if (kconsumer_data
.pid
> 0) {
2733 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2734 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2737 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2741 * Check for command that don't needs to allocate a returned payload. We do
2742 * this here so we don't have to make the call for no payload at each
2745 switch(cmd_ctx
->lsm
->cmd_type
) {
2746 case LTTNG_LIST_SESSIONS
:
2747 case LTTNG_LIST_TRACEPOINTS
:
2748 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2749 case LTTNG_LIST_DOMAINS
:
2750 case LTTNG_LIST_CHANNELS
:
2751 case LTTNG_LIST_EVENTS
:
2754 /* Setup lttng message with no payload */
2755 ret
= setup_lttng_msg(cmd_ctx
, 0);
2757 /* This label does not try to unlock the session */
2758 goto init_setup_error
;
2762 /* Commands that DO NOT need a session. */
2763 switch (cmd_ctx
->lsm
->cmd_type
) {
2764 case LTTNG_CREATE_SESSION
:
2765 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2766 case LTTNG_CREATE_SESSION_LIVE
:
2767 case LTTNG_CALIBRATE
:
2768 case LTTNG_LIST_SESSIONS
:
2769 case LTTNG_LIST_TRACEPOINTS
:
2770 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2771 need_tracing_session
= 0;
2774 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2776 * We keep the session list lock across _all_ commands
2777 * for now, because the per-session lock does not
2778 * handle teardown properly.
2780 session_lock_list();
2781 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2782 if (cmd_ctx
->session
== NULL
) {
2783 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2786 /* Acquire lock for the session */
2787 session_lock(cmd_ctx
->session
);
2797 * Check domain type for specific "pre-action".
2799 switch (cmd_ctx
->lsm
->domain
.type
) {
2800 case LTTNG_DOMAIN_KERNEL
:
2802 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2806 /* Kernel tracer check */
2807 if (kernel_tracer_fd
== -1) {
2808 /* Basically, load kernel tracer modules */
2809 ret
= init_kernel_tracer();
2815 /* Consumer is in an ERROR state. Report back to client */
2816 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2817 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2821 /* Need a session for kernel command */
2822 if (need_tracing_session
) {
2823 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2824 ret
= create_kernel_session(cmd_ctx
->session
);
2826 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2831 /* Start the kernel consumer daemon */
2832 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2833 if (kconsumer_data
.pid
== 0 &&
2834 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2835 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2836 ret
= start_consumerd(&kconsumer_data
);
2838 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2841 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2843 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2847 * The consumer was just spawned so we need to add the socket to
2848 * the consumer output of the session if exist.
2850 ret
= consumer_create_socket(&kconsumer_data
,
2851 cmd_ctx
->session
->kernel_session
->consumer
);
2858 case LTTNG_DOMAIN_JUL
:
2859 case LTTNG_DOMAIN_UST
:
2861 if (!ust_app_supported()) {
2862 ret
= LTTNG_ERR_NO_UST
;
2865 /* Consumer is in an ERROR state. Report back to client */
2866 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2867 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2871 if (need_tracing_session
) {
2872 /* Create UST session if none exist. */
2873 if (cmd_ctx
->session
->ust_session
== NULL
) {
2874 ret
= create_ust_session(cmd_ctx
->session
,
2875 &cmd_ctx
->lsm
->domain
);
2876 if (ret
!= LTTNG_OK
) {
2881 /* Start the UST consumer daemons */
2883 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2884 if (consumerd64_bin
[0] != '\0' &&
2885 ustconsumer64_data
.pid
== 0 &&
2886 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2887 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2888 ret
= start_consumerd(&ustconsumer64_data
);
2890 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2891 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2895 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2896 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2898 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2902 * Setup socket for consumer 64 bit. No need for atomic access
2903 * since it was set above and can ONLY be set in this thread.
2905 ret
= consumer_create_socket(&ustconsumer64_data
,
2906 cmd_ctx
->session
->ust_session
->consumer
);
2912 if (consumerd32_bin
[0] != '\0' &&
2913 ustconsumer32_data
.pid
== 0 &&
2914 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2915 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2916 ret
= start_consumerd(&ustconsumer32_data
);
2918 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2919 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2923 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2924 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2926 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2930 * Setup socket for consumer 64 bit. No need for atomic access
2931 * since it was set above and can ONLY be set in this thread.
2933 ret
= consumer_create_socket(&ustconsumer32_data
,
2934 cmd_ctx
->session
->ust_session
->consumer
);
2946 /* Validate consumer daemon state when start/stop trace command */
2947 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2948 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2949 switch (cmd_ctx
->lsm
->domain
.type
) {
2950 case LTTNG_DOMAIN_JUL
:
2951 case LTTNG_DOMAIN_UST
:
2952 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2953 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2957 case LTTNG_DOMAIN_KERNEL
:
2958 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2959 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2967 * Check that the UID or GID match that of the tracing session.
2968 * The root user can interact with all sessions.
2970 if (need_tracing_session
) {
2971 if (!session_access_ok(cmd_ctx
->session
,
2972 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2973 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2974 ret
= LTTNG_ERR_EPERM
;
2980 * Send relayd information to consumer as soon as we have a domain and a
2983 if (cmd_ctx
->session
&& need_domain
) {
2985 * Setup relayd if not done yet. If the relayd information was already
2986 * sent to the consumer, this call will gracefully return.
2988 ret
= cmd_setup_relayd(cmd_ctx
->session
);
2989 if (ret
!= LTTNG_OK
) {
2994 /* Process by command type */
2995 switch (cmd_ctx
->lsm
->cmd_type
) {
2996 case LTTNG_ADD_CONTEXT
:
2998 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
2999 cmd_ctx
->lsm
->u
.context
.channel_name
,
3000 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
3003 case LTTNG_DISABLE_CHANNEL
:
3005 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3006 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3009 case LTTNG_DISABLE_EVENT
:
3011 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3012 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3013 cmd_ctx
->lsm
->u
.disable
.name
);
3016 case LTTNG_DISABLE_ALL_EVENT
:
3018 DBG("Disabling all events");
3020 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3021 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3024 case LTTNG_ENABLE_CHANNEL
:
3026 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3027 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3030 case LTTNG_ENABLE_EVENT
:
3032 struct lttng_event_exclusion
*exclusion
= NULL
;
3033 struct lttng_filter_bytecode
*bytecode
= NULL
;
3035 /* Handle exclusion events and receive it from the client. */
3036 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3037 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3039 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3040 (count
* LTTNG_SYMBOL_NAME_LEN
));
3042 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3046 DBG("Receiving var len exclusion event list from client ...");
3047 exclusion
->count
= count
;
3048 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3049 count
* LTTNG_SYMBOL_NAME_LEN
);
3051 DBG("Nothing recv() from client var len data... continuing");
3054 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3059 /* Handle filter and get bytecode from client. */
3060 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3061 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3063 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3064 ret
= LTTNG_ERR_FILTER_INVAL
;
3069 bytecode
= zmalloc(bytecode_len
);
3072 ret
= LTTNG_ERR_FILTER_NOMEM
;
3076 /* Receive var. len. data */
3077 DBG("Receiving var len filter's bytecode from client ...");
3078 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3080 DBG("Nothing recv() from client car len data... continuing");
3084 ret
= LTTNG_ERR_FILTER_INVAL
;
3088 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3091 ret
= LTTNG_ERR_FILTER_INVAL
;
3096 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3097 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3098 &cmd_ctx
->lsm
->u
.enable
.event
, bytecode
, exclusion
,
3099 kernel_poll_pipe
[1]);
3102 case LTTNG_ENABLE_ALL_EVENT
:
3104 DBG("Enabling all events");
3106 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3107 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3108 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, kernel_poll_pipe
[1]);
3111 case LTTNG_LIST_TRACEPOINTS
:
3113 struct lttng_event
*events
;
3116 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3117 if (nb_events
< 0) {
3118 /* Return value is a negative lttng_error_code. */
3124 * Setup lttng message with payload size set to the event list size in
3125 * bytes and then copy list into the llm payload.
3127 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3133 /* Copy event list into message payload */
3134 memcpy(cmd_ctx
->llm
->payload
, events
,
3135 sizeof(struct lttng_event
) * nb_events
);
3142 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3144 struct lttng_event_field
*fields
;
3147 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3149 if (nb_fields
< 0) {
3150 /* Return value is a negative lttng_error_code. */
3156 * Setup lttng message with payload size set to the event list size in
3157 * bytes and then copy list into the llm payload.
3159 ret
= setup_lttng_msg(cmd_ctx
,
3160 sizeof(struct lttng_event_field
) * nb_fields
);
3166 /* Copy event list into message payload */
3167 memcpy(cmd_ctx
->llm
->payload
, fields
,
3168 sizeof(struct lttng_event_field
) * nb_fields
);
3175 case LTTNG_SET_CONSUMER_URI
:
3178 struct lttng_uri
*uris
;
3180 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3181 len
= nb_uri
* sizeof(struct lttng_uri
);
3184 ret
= LTTNG_ERR_INVALID
;
3188 uris
= zmalloc(len
);
3190 ret
= LTTNG_ERR_FATAL
;
3194 /* Receive variable len data */
3195 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3196 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3198 DBG("No URIs received from client... continuing");
3200 ret
= LTTNG_ERR_SESSION_FAIL
;
3205 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3207 if (ret
!= LTTNG_OK
) {
3213 * XXX: 0 means that this URI should be applied on the session. Should
3214 * be a DOMAIN enuam.
3216 if (cmd_ctx
->lsm
->domain
.type
== 0) {
3217 /* Add the URI for the UST session if a consumer is present. */
3218 if (cmd_ctx
->session
->ust_session
&&
3219 cmd_ctx
->session
->ust_session
->consumer
) {
3220 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
3222 } else if (cmd_ctx
->session
->kernel_session
&&
3223 cmd_ctx
->session
->kernel_session
->consumer
) {
3224 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
3225 cmd_ctx
->session
, nb_uri
, uris
);
3233 case LTTNG_START_TRACE
:
3235 ret
= cmd_start_trace(cmd_ctx
->session
);
3238 case LTTNG_STOP_TRACE
:
3240 ret
= cmd_stop_trace(cmd_ctx
->session
);
3243 case LTTNG_CREATE_SESSION
:
3246 struct lttng_uri
*uris
= NULL
;
3248 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3249 len
= nb_uri
* sizeof(struct lttng_uri
);
3252 uris
= zmalloc(len
);
3254 ret
= LTTNG_ERR_FATAL
;
3258 /* Receive variable len data */
3259 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3260 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3262 DBG("No URIs received from client... continuing");
3264 ret
= LTTNG_ERR_SESSION_FAIL
;
3269 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3270 DBG("Creating session with ONE network URI is a bad call");
3271 ret
= LTTNG_ERR_SESSION_FAIL
;
3277 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3278 &cmd_ctx
->creds
, 0);
3284 case LTTNG_DESTROY_SESSION
:
3286 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3288 /* Set session to NULL so we do not unlock it after free. */
3289 cmd_ctx
->session
= NULL
;
3292 case LTTNG_LIST_DOMAINS
:
3295 struct lttng_domain
*domains
;
3297 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3299 /* Return value is a negative lttng_error_code. */
3304 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3310 /* Copy event list into message payload */
3311 memcpy(cmd_ctx
->llm
->payload
, domains
,
3312 nb_dom
* sizeof(struct lttng_domain
));
3319 case LTTNG_LIST_CHANNELS
:
3322 struct lttng_channel
*channels
;
3324 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3325 cmd_ctx
->session
, &channels
);
3327 /* Return value is a negative lttng_error_code. */
3332 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3338 /* Copy event list into message payload */
3339 memcpy(cmd_ctx
->llm
->payload
, channels
,
3340 nb_chan
* sizeof(struct lttng_channel
));
3347 case LTTNG_LIST_EVENTS
:
3350 struct lttng_event
*events
= NULL
;
3352 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3353 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3355 /* Return value is a negative lttng_error_code. */
3360 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3366 /* Copy event list into message payload */
3367 memcpy(cmd_ctx
->llm
->payload
, events
,
3368 nb_event
* sizeof(struct lttng_event
));
3375 case LTTNG_LIST_SESSIONS
:
3377 unsigned int nr_sessions
;
3379 session_lock_list();
3380 nr_sessions
= lttng_sessions_count(
3381 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3382 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3384 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3386 session_unlock_list();
3390 /* Filled the session array */
3391 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3392 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3393 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3395 session_unlock_list();
3400 case LTTNG_CALIBRATE
:
3402 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3403 &cmd_ctx
->lsm
->u
.calibrate
);
3406 case LTTNG_REGISTER_CONSUMER
:
3408 struct consumer_data
*cdata
;
3410 switch (cmd_ctx
->lsm
->domain
.type
) {
3411 case LTTNG_DOMAIN_KERNEL
:
3412 cdata
= &kconsumer_data
;
3415 ret
= LTTNG_ERR_UND
;
3419 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3420 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3423 case LTTNG_DATA_PENDING
:
3425 ret
= cmd_data_pending(cmd_ctx
->session
);
3428 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3430 struct lttcomm_lttng_output_id reply
;
3432 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3433 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3434 if (ret
!= LTTNG_OK
) {
3438 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3443 /* Copy output list into message payload */
3444 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3448 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3450 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3451 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3454 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3457 struct lttng_snapshot_output
*outputs
= NULL
;
3459 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3460 if (nb_output
< 0) {
3465 ret
= setup_lttng_msg(cmd_ctx
,
3466 nb_output
* sizeof(struct lttng_snapshot_output
));
3473 /* Copy output list into message payload */
3474 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3475 nb_output
* sizeof(struct lttng_snapshot_output
));
3482 case LTTNG_SNAPSHOT_RECORD
:
3484 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3485 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3486 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3489 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3492 struct lttng_uri
*uris
= NULL
;
3494 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3495 len
= nb_uri
* sizeof(struct lttng_uri
);
3498 uris
= zmalloc(len
);
3500 ret
= LTTNG_ERR_FATAL
;
3504 /* Receive variable len data */
3505 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3506 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3508 DBG("No URIs received from client... continuing");
3510 ret
= LTTNG_ERR_SESSION_FAIL
;
3515 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3516 DBG("Creating session with ONE network URI is a bad call");
3517 ret
= LTTNG_ERR_SESSION_FAIL
;
3523 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3524 nb_uri
, &cmd_ctx
->creds
);
3528 case LTTNG_CREATE_SESSION_LIVE
:
3531 struct lttng_uri
*uris
= NULL
;
3533 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3534 len
= nb_uri
* sizeof(struct lttng_uri
);
3537 uris
= zmalloc(len
);
3539 ret
= LTTNG_ERR_FATAL
;
3543 /* Receive variable len data */
3544 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3545 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3547 DBG("No URIs received from client... continuing");
3549 ret
= LTTNG_ERR_SESSION_FAIL
;
3554 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3555 DBG("Creating session with ONE network URI is a bad call");
3556 ret
= LTTNG_ERR_SESSION_FAIL
;
3562 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3563 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3568 ret
= LTTNG_ERR_UND
;
3573 if (cmd_ctx
->llm
== NULL
) {
3574 DBG("Missing llm structure. Allocating one.");
3575 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3579 /* Set return code */
3580 cmd_ctx
->llm
->ret_code
= ret
;
3582 if (cmd_ctx
->session
) {
3583 session_unlock(cmd_ctx
->session
);
3585 if (need_tracing_session
) {
3586 session_unlock_list();
3593 * Thread managing health check socket.
3595 static void *thread_manage_health(void *data
)
3597 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3598 uint32_t revents
, nb_fd
;
3599 struct lttng_poll_event events
;
3600 struct health_comm_msg msg
;
3601 struct health_comm_reply reply
;
3603 DBG("[thread] Manage health check started");
3605 rcu_register_thread();
3607 /* We might hit an error path before this is created. */
3608 lttng_poll_init(&events
);
3610 /* Create unix socket */
3611 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3613 ERR("Unable to create health check Unix socket");
3619 /* lttng health client socket path permissions */
3620 ret
= chown(health_unix_sock_path
, 0,
3621 utils_get_group_id(tracing_group_name
));
3623 ERR("Unable to set group on %s", health_unix_sock_path
);
3629 ret
= chmod(health_unix_sock_path
,
3630 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3632 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3640 * Set the CLOEXEC flag. Return code is useless because either way, the
3643 (void) utils_set_fd_cloexec(sock
);
3645 ret
= lttcomm_listen_unix_sock(sock
);
3651 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3652 * more will be added to this poll set.
3654 ret
= sessiond_set_thread_pollset(&events
, 2);
3659 /* Add the application registration socket */
3660 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3665 lttng_sessiond_notify_ready();
3668 DBG("Health check ready");
3670 /* Inifinite blocking call, waiting for transmission */
3672 ret
= lttng_poll_wait(&events
, -1);
3675 * Restart interrupted system call.
3677 if (errno
== EINTR
) {
3685 for (i
= 0; i
< nb_fd
; i
++) {
3686 /* Fetch once the poll data */
3687 revents
= LTTNG_POLL_GETEV(&events
, i
);
3688 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3690 /* Thread quit pipe has been closed. Killing thread. */
3691 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3697 /* Event on the registration socket */
3698 if (pollfd
== sock
) {
3699 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3700 ERR("Health socket poll error");
3706 new_sock
= lttcomm_accept_unix_sock(sock
);
3712 * Set the CLOEXEC flag. Return code is useless because either way, the
3715 (void) utils_set_fd_cloexec(new_sock
);
3717 DBG("Receiving data from client for health...");
3718 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3720 DBG("Nothing recv() from client... continuing");
3721 ret
= close(new_sock
);
3729 rcu_thread_online();
3732 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
3734 * health_check_state returns 0 if health is
3737 if (!health_check_state(health_sessiond
, i
)) {
3738 reply
.ret_code
|= 1ULL << i
;
3742 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
3744 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3746 ERR("Failed to send health data back to client");
3749 /* End of transmission */
3750 ret
= close(new_sock
);
3760 ERR("Health error occurred in %s", __func__
);
3762 DBG("Health check thread dying");
3763 unlink(health_unix_sock_path
);
3771 lttng_poll_clean(&events
);
3773 rcu_unregister_thread();
3778 * This thread manage all clients request using the unix client socket for
3781 static void *thread_manage_clients(void *data
)
3783 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3785 uint32_t revents
, nb_fd
;
3786 struct command_ctx
*cmd_ctx
= NULL
;
3787 struct lttng_poll_event events
;
3789 DBG("[thread] Manage client started");
3791 rcu_register_thread();
3793 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
3795 health_code_update();
3797 ret
= lttcomm_listen_unix_sock(client_sock
);
3803 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3804 * more will be added to this poll set.
3806 ret
= sessiond_set_thread_pollset(&events
, 2);
3808 goto error_create_poll
;
3811 /* Add the application registration socket */
3812 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3817 lttng_sessiond_notify_ready();
3819 /* This testpoint is after we signal readiness to the parent. */
3820 if (testpoint(sessiond_thread_manage_clients
)) {
3824 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
3828 health_code_update();
3831 DBG("Accepting client command ...");
3833 /* Inifinite blocking call, waiting for transmission */
3835 health_poll_entry();
3836 ret
= lttng_poll_wait(&events
, -1);
3840 * Restart interrupted system call.
3842 if (errno
== EINTR
) {
3850 for (i
= 0; i
< nb_fd
; i
++) {
3851 /* Fetch once the poll data */
3852 revents
= LTTNG_POLL_GETEV(&events
, i
);
3853 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3855 health_code_update();
3857 /* Thread quit pipe has been closed. Killing thread. */
3858 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3864 /* Event on the registration socket */
3865 if (pollfd
== client_sock
) {
3866 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3867 ERR("Client socket poll error");
3873 DBG("Wait for client response");
3875 health_code_update();
3877 sock
= lttcomm_accept_unix_sock(client_sock
);
3883 * Set the CLOEXEC flag. Return code is useless because either way, the
3886 (void) utils_set_fd_cloexec(sock
);
3888 /* Set socket option for credentials retrieval */
3889 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3894 /* Allocate context command to process the client request */
3895 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3896 if (cmd_ctx
== NULL
) {
3897 PERROR("zmalloc cmd_ctx");
3901 /* Allocate data buffer for reception */
3902 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3903 if (cmd_ctx
->lsm
== NULL
) {
3904 PERROR("zmalloc cmd_ctx->lsm");
3908 cmd_ctx
->llm
= NULL
;
3909 cmd_ctx
->session
= NULL
;
3911 health_code_update();
3914 * Data is received from the lttng client. The struct
3915 * lttcomm_session_msg (lsm) contains the command and data request of
3918 DBG("Receiving data from client ...");
3919 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3920 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3922 DBG("Nothing recv() from client... continuing");
3928 clean_command_ctx(&cmd_ctx
);
3932 health_code_update();
3934 // TODO: Validate cmd_ctx including sanity check for
3935 // security purpose.
3937 rcu_thread_online();
3939 * This function dispatch the work to the kernel or userspace tracer
3940 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3941 * informations for the client. The command context struct contains
3942 * everything this function may needs.
3944 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3945 rcu_thread_offline();
3953 * TODO: Inform client somehow of the fatal error. At
3954 * this point, ret < 0 means that a zmalloc failed
3955 * (ENOMEM). Error detected but still accept
3956 * command, unless a socket error has been
3959 clean_command_ctx(&cmd_ctx
);
3963 health_code_update();
3965 DBG("Sending response (size: %d, retcode: %s)",
3966 cmd_ctx
->lttng_msg_size
,
3967 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3968 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3970 ERR("Failed to send data back to client");
3973 /* End of transmission */
3980 clean_command_ctx(&cmd_ctx
);
3982 health_code_update();
3994 lttng_poll_clean(&events
);
3995 clean_command_ctx(&cmd_ctx
);
3999 unlink(client_unix_sock_path
);
4000 if (client_sock
>= 0) {
4001 ret
= close(client_sock
);
4009 ERR("Health error occurred in %s", __func__
);
4012 health_unregister(health_sessiond
);
4014 DBG("Client thread dying");
4016 rcu_unregister_thread();
4022 * usage function on stderr
4024 static void usage(void)
4026 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4027 fprintf(stderr
, " -h, --help Display this usage.\n");
4028 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4029 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4030 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4031 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4032 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4033 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4034 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4035 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4036 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4037 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4038 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4039 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4040 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4041 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4042 fprintf(stderr
, " -V, --version Show version number.\n");
4043 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4044 fprintf(stderr
, " -q, --quiet No output at all.\n");
4045 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4046 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4047 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4048 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4049 fprintf(stderr
, " --jul-tcp-port JUL application registration TCP port\n");
4050 fprintf(stderr
, " -f --config Load daemon configuration file\n");
4054 * Take an option from the getopt output and set it in the right variable to be
4057 * Return 0 on success else a negative value.
4059 static int set_option(int opt
, const char *arg
, const char *optname
)
4065 fprintf(stderr
, "option %s", optname
);
4067 fprintf(stderr
, " with arg %s\n", arg
);
4071 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4074 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4080 tracing_group_name
= strdup(arg
);
4086 fprintf(stdout
, "%s\n", VERSION
);
4092 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4095 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4098 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4101 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4104 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4107 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4113 lttng_opt_quiet
= 1;
4116 /* Verbose level can increase using multiple -v */
4118 lttng_opt_verbose
= config_parse_value(arg
);
4120 lttng_opt_verbose
+= 1;
4125 opt_verbose_consumer
= config_parse_value(arg
);
4127 opt_verbose_consumer
+= 1;
4131 consumerd32_bin
= strdup(arg
);
4132 consumerd32_bin_override
= 1;
4135 consumerd32_libdir
= strdup(arg
);
4136 consumerd32_libdir_override
= 1;
4139 consumerd64_bin
= strdup(arg
);
4140 consumerd64_bin_override
= 1;
4143 consumerd64_libdir
= strdup(arg
);
4144 consumerd64_libdir_override
= 1;
4147 opt_pidfile
= strdup(arg
);
4149 case 'J': /* JUL TCP port. */
4154 v
= strtoul(arg
, NULL
, 0);
4155 if (errno
!= 0 || !isdigit(arg
[0])) {
4156 ERR("Wrong value in --jul-tcp-port parameter: %s", arg
);
4159 if (v
== 0 || v
>= 65535) {
4160 ERR("Port overflow in --jul-tcp-port parameter: %s", arg
);
4163 jul_tcp_port
= (uint32_t) v
;
4164 DBG3("JUL TCP port set to non default: %u", jul_tcp_port
);
4168 /* Unknown option or other error.
4169 * Error is printed by getopt, just return */
4177 * config_entry_handler_cb used to handle options read from a config file.
4178 * See config_entry_handler_cb comment in common/config/config.h for the
4179 * return value conventions.
4181 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4185 if (!entry
|| !entry
->name
|| !entry
->value
) {
4190 /* Check if the option is to be ignored */
4191 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4192 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4197 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4200 /* Ignore if not fully matched. */
4201 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4206 * If the option takes no argument on the command line, we have to
4207 * check if the value is "true". We support non-zero numeric values,
4210 if (!long_options
[i
].has_arg
) {
4211 ret
= config_parse_value(entry
->value
);
4214 WARN("Invalid configuration value \"%s\" for option %s",
4215 entry
->value
, entry
->name
);
4217 /* False, skip boolean config option. */
4222 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4226 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4233 * daemon configuration loading and argument parsing
4235 static int set_options(int argc
, char **argv
)
4237 int ret
= 0, c
= 0, option_index
= 0;
4238 int orig_optopt
= optopt
, orig_optind
= optind
;
4240 const char *config_path
= NULL
;
4242 optstring
= utils_generate_optstring(long_options
,
4243 sizeof(long_options
) / sizeof(struct option
));
4249 /* Check for the --config option */
4250 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4251 &option_index
)) != -1) {
4255 } else if (c
!= 'f') {
4256 /* if not equal to --config option. */
4260 config_path
= utils_expand_path(optarg
);
4262 ERR("Failed to resolve path: %s", optarg
);
4266 ret
= config_get_section_entries(config_path
, config_section_name
,
4267 config_entry_handler
, NULL
);
4270 ERR("Invalid configuration option at line %i", ret
);
4276 /* Reset getopt's global state */
4277 optopt
= orig_optopt
;
4278 optind
= orig_optind
;
4280 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
4285 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
4297 * Creates the two needed socket by the daemon.
4298 * apps_sock - The communication socket for all UST apps.
4299 * client_sock - The communication of the cli tool (lttng).
4301 static int init_daemon_socket(void)
4306 old_umask
= umask(0);
4308 /* Create client tool unix socket */
4309 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4310 if (client_sock
< 0) {
4311 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4316 /* Set the cloexec flag */
4317 ret
= utils_set_fd_cloexec(client_sock
);
4319 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4320 "Continuing but note that the consumer daemon will have a "
4321 "reference to this socket on exec()", client_sock
);
4324 /* File permission MUST be 660 */
4325 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4327 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4332 /* Create the application unix socket */
4333 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4334 if (apps_sock
< 0) {
4335 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4340 /* Set the cloexec flag */
4341 ret
= utils_set_fd_cloexec(apps_sock
);
4343 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4344 "Continuing but note that the consumer daemon will have a "
4345 "reference to this socket on exec()", apps_sock
);
4348 /* File permission MUST be 666 */
4349 ret
= chmod(apps_unix_sock_path
,
4350 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4352 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4357 DBG3("Session daemon client socket %d and application socket %d created",
4358 client_sock
, apps_sock
);
4366 * Check if the global socket is available, and if a daemon is answering at the
4367 * other side. If yes, error is returned.
4369 static int check_existing_daemon(void)
4371 /* Is there anybody out there ? */
4372 if (lttng_session_daemon_alive()) {
4380 * Set the tracing group gid onto the client socket.
4382 * Race window between mkdir and chown is OK because we are going from more
4383 * permissive (root.root) to less permissive (root.tracing).
4385 static int set_permissions(char *rundir
)
4390 gid
= utils_get_group_id(tracing_group_name
);
4392 /* Set lttng run dir */
4393 ret
= chown(rundir
, 0, gid
);
4395 ERR("Unable to set group on %s", rundir
);
4400 * Ensure all applications and tracing group can search the run
4401 * dir. Allow everyone to read the directory, since it does not
4402 * buy us anything to hide its content.
4404 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4406 ERR("Unable to set permissions on %s", rundir
);
4410 /* lttng client socket path */
4411 ret
= chown(client_unix_sock_path
, 0, gid
);
4413 ERR("Unable to set group on %s", client_unix_sock_path
);
4417 /* kconsumer error socket path */
4418 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4420 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4424 /* 64-bit ustconsumer error socket path */
4425 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4427 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4431 /* 32-bit ustconsumer compat32 error socket path */
4432 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4434 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4438 DBG("All permissions are set");
4444 * Create the lttng run directory needed for all global sockets and pipe.
4446 static int create_lttng_rundir(const char *rundir
)
4450 DBG3("Creating LTTng run directory: %s", rundir
);
4452 ret
= mkdir(rundir
, S_IRWXU
);
4454 if (errno
!= EEXIST
) {
4455 ERR("Unable to create %s", rundir
);
4467 * Setup sockets and directory needed by the kconsumerd communication with the
4470 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4474 char path
[PATH_MAX
];
4476 switch (consumer_data
->type
) {
4477 case LTTNG_CONSUMER_KERNEL
:
4478 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4480 case LTTNG_CONSUMER64_UST
:
4481 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4483 case LTTNG_CONSUMER32_UST
:
4484 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4487 ERR("Consumer type unknown");
4492 DBG2("Creating consumer directory: %s", path
);
4494 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4496 if (errno
!= EEXIST
) {
4498 ERR("Failed to create %s", path
);
4504 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4506 ERR("Unable to set group on %s", path
);
4512 /* Create the kconsumerd error unix socket */
4513 consumer_data
->err_sock
=
4514 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4515 if (consumer_data
->err_sock
< 0) {
4516 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4522 * Set the CLOEXEC flag. Return code is useless because either way, the
4525 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4527 PERROR("utils_set_fd_cloexec");
4528 /* continue anyway */
4531 /* File permission MUST be 660 */
4532 ret
= chmod(consumer_data
->err_unix_sock_path
,
4533 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4535 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4545 * Signal handler for the daemon
4547 * Simply stop all worker threads, leaving main() return gracefully after
4548 * joining all threads and calling cleanup().
4550 static void sighandler(int sig
)
4554 DBG("SIGPIPE caught");
4557 DBG("SIGINT caught");
4561 DBG("SIGTERM caught");
4565 CMM_STORE_SHARED(recv_child_signal
, 1);
4573 * Setup signal handler for :
4574 * SIGINT, SIGTERM, SIGPIPE
4576 static int set_signal_handler(void)
4579 struct sigaction sa
;
4582 if ((ret
= sigemptyset(&sigset
)) < 0) {
4583 PERROR("sigemptyset");
4587 sa
.sa_handler
= sighandler
;
4588 sa
.sa_mask
= sigset
;
4590 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
4591 PERROR("sigaction");
4595 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
4596 PERROR("sigaction");
4600 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
4601 PERROR("sigaction");
4605 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
4606 PERROR("sigaction");
4610 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
4616 * Set open files limit to unlimited. This daemon can open a large number of
4617 * file descriptors in order to consumer multiple kernel traces.
4619 static void set_ulimit(void)
4624 /* The kernel does not allowed an infinite limit for open files */
4625 lim
.rlim_cur
= 65535;
4626 lim
.rlim_max
= 65535;
4628 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
4630 PERROR("failed to set open files limit");
4635 * Write pidfile using the rundir and opt_pidfile.
4637 static void write_pidfile(void)
4640 char pidfile_path
[PATH_MAX
];
4645 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4647 /* Build pidfile path from rundir and opt_pidfile. */
4648 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4649 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4651 PERROR("snprintf pidfile path");
4657 * Create pid file in rundir. Return value is of no importance. The
4658 * execution will continue even though we are not able to write the file.
4660 (void) utils_create_pid_file(getpid(), pidfile_path
);
4667 * Write JUL TCP port using the rundir.
4669 static void write_julport(void)
4672 char path
[PATH_MAX
];
4676 ret
= snprintf(path
, sizeof(path
), "%s/"
4677 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
, rundir
);
4679 PERROR("snprintf julport path");
4684 * Create TCP JUL port file in rundir. Return value is of no importance.
4685 * The execution will continue even though we are not able to write the
4688 (void) utils_create_pid_file(jul_tcp_port
, path
);
4695 * Daemonize this process by forking and making the parent wait for the child
4696 * to signal it indicating readiness. Once received, the parent successfully
4699 * The child process undergoes the same action that daemon(3) does meaning
4700 * setsid, chdir, and dup /dev/null into 0, 1 and 2.
4702 * Return 0 on success else -1 on error.
4704 static int daemonize(void)
4709 /* Get parent pid of this process. */
4710 child_ppid
= getppid();
4716 } else if (pid
== 0) {
4723 * Get the newly created parent pid so we can signal that process when
4724 * we are ready to operate.
4726 child_ppid
= getppid();
4734 /* Try to change directory to /. If we can't well at least notify. */
4740 fd
= open(_PATH_DEVNULL
, O_RDWR
, 0);
4742 PERROR("open %s", _PATH_DEVNULL
);
4743 /* Let 0, 1 and 2 open since we can't bind them to /dev/null. */
4745 (void) dup2(fd
, STDIN_FILENO
);
4746 (void) dup2(fd
, STDOUT_FILENO
);
4747 (void) dup2(fd
, STDERR_FILENO
);
4760 * Waiting for child to notify this parent that it can exit. Note that
4761 * sleep() is interrupted before the 1 second delay as soon as the
4762 * signal is received, so it will not cause visible delay for the
4765 while (!CMM_LOAD_SHARED(recv_child_signal
)) {
4770 * Check if child exists without blocking. If so, we have to stop
4771 * this parent process and return an error.
4773 ret
= waitpid(pid
, &status
, WNOHANG
);
4774 if (ret
< 0 || (ret
!= 0 && WIFEXITED(status
))) {
4775 /* The child exited somehow or was not valid. */
4782 * From this point on, the parent can exit and the child is now an
4783 * operationnal session daemon ready to serve clients and applications.
4798 int main(int argc
, char **argv
)
4802 const char *home_path
, *env_app_timeout
;
4804 init_kernel_workarounds();
4806 rcu_register_thread();
4808 if ((ret
= set_signal_handler()) < 0) {
4812 setup_consumerd_path();
4814 page_size
= sysconf(_SC_PAGESIZE
);
4815 if (page_size
< 0) {
4816 PERROR("sysconf _SC_PAGESIZE");
4817 page_size
= LONG_MAX
;
4818 WARN("Fallback page size to %ld", page_size
);
4821 /* Parse arguments and load the daemon configuration file */
4823 if ((ret
= set_options(argc
, argv
)) < 0) {
4837 * We are in the child. Make sure all other file descriptors are
4838 * closed, in case we are called with more opened file descriptors than
4839 * the standard ones.
4841 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4846 /* Create thread quit pipe */
4847 if ((ret
= init_thread_quit_pipe()) < 0) {
4851 /* Check if daemon is UID = 0 */
4852 is_root
= !getuid();
4855 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4857 /* Create global run dir with root access */
4858 ret
= create_lttng_rundir(rundir
);
4863 if (strlen(apps_unix_sock_path
) == 0) {
4864 snprintf(apps_unix_sock_path
, PATH_MAX
,
4865 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4868 if (strlen(client_unix_sock_path
) == 0) {
4869 snprintf(client_unix_sock_path
, PATH_MAX
,
4870 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4873 /* Set global SHM for ust */
4874 if (strlen(wait_shm_path
) == 0) {
4875 snprintf(wait_shm_path
, PATH_MAX
,
4876 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4879 if (strlen(health_unix_sock_path
) == 0) {
4880 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4881 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4884 /* Setup kernel consumerd path */
4885 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4886 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4887 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4888 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4890 DBG2("Kernel consumer err path: %s",
4891 kconsumer_data
.err_unix_sock_path
);
4892 DBG2("Kernel consumer cmd path: %s",
4893 kconsumer_data
.cmd_unix_sock_path
);
4895 home_path
= utils_get_home_dir();
4896 if (home_path
== NULL
) {
4897 /* TODO: Add --socket PATH option */
4898 ERR("Can't get HOME directory for sockets creation.");
4904 * Create rundir from home path. This will create something like
4907 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4913 ret
= create_lttng_rundir(rundir
);
4918 if (strlen(apps_unix_sock_path
) == 0) {
4919 snprintf(apps_unix_sock_path
, PATH_MAX
,
4920 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4923 /* Set the cli tool unix socket path */
4924 if (strlen(client_unix_sock_path
) == 0) {
4925 snprintf(client_unix_sock_path
, PATH_MAX
,
4926 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4929 /* Set global SHM for ust */
4930 if (strlen(wait_shm_path
) == 0) {
4931 snprintf(wait_shm_path
, PATH_MAX
,
4932 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4935 /* Set health check Unix path */
4936 if (strlen(health_unix_sock_path
) == 0) {
4937 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4938 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4942 /* Set consumer initial state */
4943 kernel_consumerd_state
= CONSUMER_STOPPED
;
4944 ust_consumerd_state
= CONSUMER_STOPPED
;
4946 DBG("Client socket path %s", client_unix_sock_path
);
4947 DBG("Application socket path %s", apps_unix_sock_path
);
4948 DBG("Application wait path %s", wait_shm_path
);
4949 DBG("LTTng run directory path: %s", rundir
);
4951 /* 32 bits consumerd path setup */
4952 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4953 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4954 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4955 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4957 DBG2("UST consumer 32 bits err path: %s",
4958 ustconsumer32_data
.err_unix_sock_path
);
4959 DBG2("UST consumer 32 bits cmd path: %s",
4960 ustconsumer32_data
.cmd_unix_sock_path
);
4962 /* 64 bits consumerd path setup */
4963 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4964 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4965 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4966 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4968 DBG2("UST consumer 64 bits err path: %s",
4969 ustconsumer64_data
.err_unix_sock_path
);
4970 DBG2("UST consumer 64 bits cmd path: %s",
4971 ustconsumer64_data
.cmd_unix_sock_path
);
4974 * See if daemon already exist.
4976 if ((ret
= check_existing_daemon()) < 0) {
4977 ERR("Already running daemon.\n");
4979 * We do not goto exit because we must not cleanup()
4980 * because a daemon is already running.
4986 * Init UST app hash table. Alloc hash table before this point since
4987 * cleanup() can get called after that point.
4991 /* Initialize JUL domain subsystem. */
4992 if ((ret
= jul_init()) < 0) {
4993 /* ENOMEM at this point. */
4997 /* After this point, we can safely call cleanup() with "goto exit" */
5000 * These actions must be executed as root. We do that *after* setting up
5001 * the sockets path because we MUST make the check for another daemon using
5002 * those paths *before* trying to set the kernel consumer sockets and init
5006 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
5011 /* Setup kernel tracer */
5012 if (!opt_no_kernel
) {
5013 init_kernel_tracer();
5016 /* Set ulimit for open files */
5019 /* init lttng_fd tracking must be done after set_ulimit. */
5022 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
5027 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
5032 /* Setup the needed unix socket */
5033 if ((ret
= init_daemon_socket()) < 0) {
5037 /* Set credentials to socket */
5038 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
5042 /* Get parent pid if -S, --sig-parent is specified. */
5043 if (opt_sig_parent
) {
5047 /* Setup the kernel pipe for waking up the kernel thread */
5048 if (is_root
&& !opt_no_kernel
) {
5049 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
5054 /* Setup the thread ht_cleanup communication pipe. */
5055 if (utils_create_pipe_cloexec(ht_cleanup_pipe
) < 0) {
5059 /* Setup the thread apps communication pipe. */
5060 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
5064 /* Setup the thread apps notify communication pipe. */
5065 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
5069 /* Initialize global buffer per UID and PID registry. */
5070 buffer_reg_init_uid_registry();
5071 buffer_reg_init_pid_registry();
5073 /* Init UST command queue. */
5074 cds_wfq_init(&ust_cmd_queue
.queue
);
5077 * Get session list pointer. This pointer MUST NOT be free(). This list is
5078 * statically declared in session.c
5080 session_list_ptr
= session_get_list();
5082 /* Set up max poll set size */
5083 lttng_poll_set_max_size();
5087 /* Check for the application socket timeout env variable. */
5088 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5089 if (env_app_timeout
) {
5090 app_socket_timeout
= atoi(env_app_timeout
);
5092 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5098 /* Initialize communication library */
5100 /* This is to get the TCP timeout value. */
5101 lttcomm_inet_init();
5104 * Initialize the health check subsystem. This call should set the
5105 * appropriate time values.
5107 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5108 if (!health_sessiond
) {
5109 PERROR("health_app_create error");
5110 goto exit_health_sessiond_cleanup
;
5113 /* Create thread to clean up RCU hash tables */
5114 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5115 thread_ht_cleanup
, (void *) NULL
);
5117 PERROR("pthread_create ht_cleanup");
5118 goto exit_ht_cleanup
;
5121 /* Create health-check thread */
5122 ret
= pthread_create(&health_thread
, NULL
,
5123 thread_manage_health
, (void *) NULL
);
5125 PERROR("pthread_create health");
5129 /* Create thread to manage the client socket */
5130 ret
= pthread_create(&client_thread
, NULL
,
5131 thread_manage_clients
, (void *) NULL
);
5133 PERROR("pthread_create clients");
5137 /* Create thread to dispatch registration */
5138 ret
= pthread_create(&dispatch_thread
, NULL
,
5139 thread_dispatch_ust_registration
, (void *) NULL
);
5141 PERROR("pthread_create dispatch");
5145 /* Create thread to manage application registration. */
5146 ret
= pthread_create(®_apps_thread
, NULL
,
5147 thread_registration_apps
, (void *) NULL
);
5149 PERROR("pthread_create registration");
5153 /* Create thread to manage application socket */
5154 ret
= pthread_create(&apps_thread
, NULL
,
5155 thread_manage_apps
, (void *) NULL
);
5157 PERROR("pthread_create apps");
5161 /* Create thread to manage application notify socket */
5162 ret
= pthread_create(&apps_notify_thread
, NULL
,
5163 ust_thread_manage_notify
, (void *) NULL
);
5165 PERROR("pthread_create apps");
5166 goto exit_apps_notify
;
5169 /* Create JUL registration thread. */
5170 ret
= pthread_create(&jul_reg_thread
, NULL
,
5171 jul_thread_manage_registration
, (void *) NULL
);
5173 PERROR("pthread_create apps");
5177 /* Don't start this thread if kernel tracing is not requested nor root */
5178 if (is_root
&& !opt_no_kernel
) {
5179 /* Create kernel thread to manage kernel event */
5180 ret
= pthread_create(&kernel_thread
, NULL
,
5181 thread_manage_kernel
, (void *) NULL
);
5183 PERROR("pthread_create kernel");
5187 ret
= pthread_join(kernel_thread
, &status
);
5189 PERROR("pthread_join");
5190 goto error
; /* join error, exit without cleanup */
5195 ret
= pthread_join(jul_reg_thread
, &status
);
5197 PERROR("pthread_join JUL");
5198 goto error
; /* join error, exit without cleanup */
5202 ret
= pthread_join(apps_notify_thread
, &status
);
5204 PERROR("pthread_join apps notify");
5205 goto error
; /* join error, exit without cleanup */
5209 ret
= pthread_join(apps_thread
, &status
);
5211 PERROR("pthread_join apps");
5212 goto error
; /* join error, exit without cleanup */
5217 ret
= pthread_join(reg_apps_thread
, &status
);
5219 PERROR("pthread_join");
5220 goto error
; /* join error, exit without cleanup */
5224 ret
= pthread_join(dispatch_thread
, &status
);
5226 PERROR("pthread_join");
5227 goto error
; /* join error, exit without cleanup */
5231 ret
= pthread_join(client_thread
, &status
);
5233 PERROR("pthread_join");
5234 goto error
; /* join error, exit without cleanup */
5237 ret
= join_consumer_thread(&kconsumer_data
);
5239 PERROR("join_consumer");
5240 goto error
; /* join error, exit without cleanup */
5243 ret
= join_consumer_thread(&ustconsumer32_data
);
5245 PERROR("join_consumer ust32");
5246 goto error
; /* join error, exit without cleanup */
5249 ret
= join_consumer_thread(&ustconsumer64_data
);
5251 PERROR("join_consumer ust64");
5252 goto error
; /* join error, exit without cleanup */
5256 ret
= pthread_join(health_thread
, &status
);
5258 PERROR("pthread_join health thread");
5259 goto error
; /* join error, exit without cleanup */
5263 ret
= pthread_join(ht_cleanup_thread
, &status
);
5265 PERROR("pthread_join ht cleanup thread");
5266 goto error
; /* join error, exit without cleanup */
5269 health_app_destroy(health_sessiond
);
5270 exit_health_sessiond_cleanup
:
5273 * cleanup() is called when no other thread is running.
5275 rcu_thread_online();
5277 rcu_thread_offline();
5278 rcu_unregister_thread();