2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include <sys/mount.h>
32 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <urcu/uatomic.h>
41 #include <common/common.h>
42 #include <common/compat/socket.h>
43 #include <common/defaults.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/futex.h>
46 #include <common/relayd/relayd.h>
47 #include <common/utils.h>
48 #include <common/daemonize.h>
50 #include "lttng-sessiond.h"
51 #include "buffer-registry.h"
58 #include "kernel-consumer.h"
62 #include "ust-consumer.h"
65 #include "health-sessiond.h"
66 #include "testpoint.h"
67 #include "ust-thread.h"
68 #include "jul-thread.h"
70 #define CONSUMERD_FILE "lttng-consumerd"
73 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
74 static const char *opt_pidfile
;
75 static int opt_sig_parent
;
76 static int opt_verbose_consumer
;
77 static int opt_daemon
, opt_background
;
78 static int opt_no_kernel
;
79 static pid_t ppid
; /* Parent PID for --sig-parent option */
80 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
82 static int lockfile_fd
= -1;
84 /* Set to 1 when a SIGUSR1 signal is received. */
85 static int recv_child_signal
;
88 * Consumer daemon specific control data. Every value not initialized here is
89 * set to 0 by the static definition.
91 static struct consumer_data kconsumer_data
= {
92 .type
= LTTNG_CONSUMER_KERNEL
,
93 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
94 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
97 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
98 .lock
= PTHREAD_MUTEX_INITIALIZER
,
99 .cond
= PTHREAD_COND_INITIALIZER
,
100 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
102 static struct consumer_data ustconsumer64_data
= {
103 .type
= LTTNG_CONSUMER64_UST
,
104 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
105 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
108 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
109 .lock
= PTHREAD_MUTEX_INITIALIZER
,
110 .cond
= PTHREAD_COND_INITIALIZER
,
111 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
113 static struct consumer_data ustconsumer32_data
= {
114 .type
= LTTNG_CONSUMER32_UST
,
115 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
116 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
119 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
120 .lock
= PTHREAD_MUTEX_INITIALIZER
,
121 .cond
= PTHREAD_COND_INITIALIZER
,
122 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
125 /* Shared between threads */
126 static int dispatch_thread_exit
;
128 /* Global application Unix socket path */
129 static char apps_unix_sock_path
[PATH_MAX
];
130 /* Global client Unix socket path */
131 static char client_unix_sock_path
[PATH_MAX
];
132 /* global wait shm path for UST */
133 static char wait_shm_path
[PATH_MAX
];
134 /* Global health check unix path */
135 static char health_unix_sock_path
[PATH_MAX
];
137 /* Sockets and FDs */
138 static int client_sock
= -1;
139 static int apps_sock
= -1;
140 int kernel_tracer_fd
= -1;
141 static int kernel_poll_pipe
[2] = { -1, -1 };
144 * Quit pipe for all threads. This permits a single cancellation point
145 * for all threads when receiving an event on the pipe.
147 static int thread_quit_pipe
[2] = { -1, -1 };
150 * This pipe is used to inform the thread managing application communication
151 * that a command is queued and ready to be processed.
153 static int apps_cmd_pipe
[2] = { -1, -1 };
155 int apps_cmd_notify_pipe
[2] = { -1, -1 };
157 /* Pthread, Mutexes and Semaphores */
158 static pthread_t apps_thread
;
159 static pthread_t apps_notify_thread
;
160 static pthread_t reg_apps_thread
;
161 static pthread_t client_thread
;
162 static pthread_t kernel_thread
;
163 static pthread_t dispatch_thread
;
164 static pthread_t health_thread
;
165 static pthread_t ht_cleanup_thread
;
166 static pthread_t jul_reg_thread
;
169 * UST registration command queue. This queue is tied with a futex and uses a N
170 * wakers / 1 waiter implemented and detailed in futex.c/.h
172 * The thread_manage_apps and thread_dispatch_ust_registration interact with
173 * this queue and the wait/wake scheme.
175 static struct ust_cmd_queue ust_cmd_queue
;
178 * Pointer initialized before thread creation.
180 * This points to the tracing session list containing the session count and a
181 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
182 * MUST NOT be taken if you call a public function in session.c.
184 * The lock is nested inside the structure: session_list_ptr->lock. Please use
185 * session_lock_list and session_unlock_list for lock acquisition.
187 static struct ltt_session_list
*session_list_ptr
;
189 int ust_consumerd64_fd
= -1;
190 int ust_consumerd32_fd
= -1;
192 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
193 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
194 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
195 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
197 static const char *module_proc_lttng
= "/proc/lttng";
200 * Consumer daemon state which is changed when spawning it, killing it or in
201 * case of a fatal error.
203 enum consumerd_state
{
204 CONSUMER_STARTED
= 1,
205 CONSUMER_STOPPED
= 2,
210 * This consumer daemon state is used to validate if a client command will be
211 * able to reach the consumer. If not, the client is informed. For instance,
212 * doing a "lttng start" when the consumer state is set to ERROR will return an
213 * error to the client.
215 * The following example shows a possible race condition of this scheme:
217 * consumer thread error happens
219 * client cmd checks state -> still OK
220 * consumer thread exit, sets error
221 * client cmd try to talk to consumer
224 * However, since the consumer is a different daemon, we have no way of making
225 * sure the command will reach it safely even with this state flag. This is why
226 * we consider that up to the state validation during command processing, the
227 * command is safe. After that, we can not guarantee the correctness of the
228 * client request vis-a-vis the consumer.
230 static enum consumerd_state ust_consumerd_state
;
231 static enum consumerd_state kernel_consumerd_state
;
234 * Socket timeout for receiving and sending in seconds.
236 static int app_socket_timeout
;
238 /* Set in main() with the current page size. */
241 /* Application health monitoring */
242 struct health_app
*health_sessiond
;
244 /* JUL TCP port for registration. Used by the JUL thread. */
245 unsigned int jul_tcp_port
= DEFAULT_JUL_TCP_PORT
;
247 /* Am I root or not. */
248 int is_root
; /* Set to 1 if the daemon is running as root */
251 * Whether sessiond is ready for commands/health check requests.
252 * NR_LTTNG_SESSIOND_READY must match the number of calls to
253 * lttng_sessiond_notify_ready().
255 #define NR_LTTNG_SESSIOND_READY 2
256 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
258 /* Notify parents that we are ready for cmd and health check */
260 void lttng_sessiond_notify_ready(void)
262 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
264 * Notify parent pid that we are ready to accept command
265 * for client side. This ppid is the one from the
266 * external process that spawned us.
268 if (opt_sig_parent
) {
273 * Notify the parent of the fork() process that we are
276 if (opt_daemon
|| opt_background
) {
277 kill(child_ppid
, SIGUSR1
);
283 void setup_consumerd_path(void)
285 const char *bin
, *libdir
;
288 * Allow INSTALL_BIN_PATH to be used as a target path for the
289 * native architecture size consumer if CONFIG_CONSUMER*_PATH
290 * has not been defined.
292 #if (CAA_BITS_PER_LONG == 32)
293 if (!consumerd32_bin
[0]) {
294 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
296 if (!consumerd32_libdir
[0]) {
297 consumerd32_libdir
= INSTALL_LIB_PATH
;
299 #elif (CAA_BITS_PER_LONG == 64)
300 if (!consumerd64_bin
[0]) {
301 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
303 if (!consumerd64_libdir
[0]) {
304 consumerd64_libdir
= INSTALL_LIB_PATH
;
307 #error "Unknown bitness"
311 * runtime env. var. overrides the build default.
313 bin
= getenv("LTTNG_CONSUMERD32_BIN");
315 consumerd32_bin
= bin
;
317 bin
= getenv("LTTNG_CONSUMERD64_BIN");
319 consumerd64_bin
= bin
;
321 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
323 consumerd32_libdir
= libdir
;
325 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
327 consumerd64_libdir
= libdir
;
332 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
334 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
340 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
346 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
358 * Check if the thread quit pipe was triggered.
360 * Return 1 if it was triggered else 0;
362 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
364 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
372 * Init thread quit pipe.
374 * Return -1 on error or 0 if all pipes are created.
376 static int init_thread_quit_pipe(void)
380 ret
= pipe(thread_quit_pipe
);
382 PERROR("thread quit pipe");
386 for (i
= 0; i
< 2; i
++) {
387 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
399 * Stop all threads by closing the thread quit pipe.
401 static void stop_threads(void)
405 /* Stopping all threads */
406 DBG("Terminating all threads");
407 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
409 ERR("write error on thread quit pipe");
412 /* Dispatch thread */
413 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
414 futex_nto1_wake(&ust_cmd_queue
.futex
);
418 * Close every consumer sockets.
420 static void close_consumer_sockets(void)
424 if (kconsumer_data
.err_sock
>= 0) {
425 ret
= close(kconsumer_data
.err_sock
);
427 PERROR("kernel consumer err_sock close");
430 if (ustconsumer32_data
.err_sock
>= 0) {
431 ret
= close(ustconsumer32_data
.err_sock
);
433 PERROR("UST consumerd32 err_sock close");
436 if (ustconsumer64_data
.err_sock
>= 0) {
437 ret
= close(ustconsumer64_data
.err_sock
);
439 PERROR("UST consumerd64 err_sock close");
442 if (kconsumer_data
.cmd_sock
>= 0) {
443 ret
= close(kconsumer_data
.cmd_sock
);
445 PERROR("kernel consumer cmd_sock close");
448 if (ustconsumer32_data
.cmd_sock
>= 0) {
449 ret
= close(ustconsumer32_data
.cmd_sock
);
451 PERROR("UST consumerd32 cmd_sock close");
454 if (ustconsumer64_data
.cmd_sock
>= 0) {
455 ret
= close(ustconsumer64_data
.cmd_sock
);
457 PERROR("UST consumerd64 cmd_sock close");
463 * Generate the full lock file path using the rundir.
465 * Return the snprintf() return value thus a negative value is an error.
467 static int generate_lock_file_path(char *path
, size_t len
)
474 /* Build lockfile path from rundir. */
475 ret
= snprintf(path
, len
, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE
, rundir
);
477 PERROR("snprintf lockfile path");
486 static void cleanup(void)
489 struct ltt_session
*sess
, *stmp
;
495 * Close the thread quit pipe. It has already done its job,
496 * since we are now called.
498 utils_close_pipe(thread_quit_pipe
);
501 * If opt_pidfile is undefined, the default file will be wiped when
502 * removing the rundir.
505 ret
= remove(opt_pidfile
);
507 PERROR("remove pidfile %s", opt_pidfile
);
511 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
514 snprintf(path
, PATH_MAX
,
516 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
517 DBG("Removing %s", path
);
520 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
521 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
);
522 DBG("Removing %s", path
);
526 snprintf(path
, PATH_MAX
,
527 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
529 DBG("Removing %s", path
);
532 snprintf(path
, PATH_MAX
,
533 DEFAULT_KCONSUMERD_PATH
,
535 DBG("Removing directory %s", path
);
538 /* ust consumerd 32 */
539 snprintf(path
, PATH_MAX
,
540 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
542 DBG("Removing %s", path
);
545 snprintf(path
, PATH_MAX
,
546 DEFAULT_USTCONSUMERD32_PATH
,
548 DBG("Removing directory %s", path
);
551 /* ust consumerd 64 */
552 snprintf(path
, PATH_MAX
,
553 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
555 DBG("Removing %s", path
);
558 snprintf(path
, PATH_MAX
,
559 DEFAULT_USTCONSUMERD64_PATH
,
561 DBG("Removing directory %s", path
);
564 DBG("Cleaning up all sessions");
566 /* Destroy session list mutex */
567 if (session_list_ptr
!= NULL
) {
568 pthread_mutex_destroy(&session_list_ptr
->lock
);
570 /* Cleanup ALL session */
571 cds_list_for_each_entry_safe(sess
, stmp
,
572 &session_list_ptr
->head
, list
) {
573 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
577 DBG("Closing all UST sockets");
578 ust_app_clean_list();
579 buffer_reg_destroy_registries();
581 if (is_root
&& !opt_no_kernel
) {
582 DBG2("Closing kernel fd");
583 if (kernel_tracer_fd
>= 0) {
584 ret
= close(kernel_tracer_fd
);
589 DBG("Unloading kernel modules");
590 modprobe_remove_lttng_all();
593 close_consumer_sockets();
597 * Cleanup lock file by deleting it and finaly closing it which will
598 * release the file system lock.
600 if (lockfile_fd
>= 0) {
601 char lockfile_path
[PATH_MAX
];
603 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
605 ret
= remove(lockfile_path
);
607 PERROR("remove lock file");
609 ret
= close(lockfile_fd
);
611 PERROR("close lock file");
617 * We do NOT rmdir rundir because there are other processes
618 * using it, for instance lttng-relayd, which can start in
619 * parallel with this teardown.
625 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
626 "Matthew, BEET driven development works!%c[%dm",
627 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
632 * Send data on a unix socket using the liblttsessiondcomm API.
634 * Return lttcomm error code.
636 static int send_unix_sock(int sock
, void *buf
, size_t len
)
638 /* Check valid length */
643 return lttcomm_send_unix_sock(sock
, buf
, len
);
647 * Free memory of a command context structure.
649 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
651 DBG("Clean command context structure");
653 if ((*cmd_ctx
)->llm
) {
654 free((*cmd_ctx
)->llm
);
656 if ((*cmd_ctx
)->lsm
) {
657 free((*cmd_ctx
)->lsm
);
665 * Notify UST applications using the shm mmap futex.
667 static int notify_ust_apps(int active
)
671 DBG("Notifying applications of session daemon state: %d", active
);
673 /* See shm.c for this call implying mmap, shm and futex calls */
674 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
675 if (wait_shm_mmap
== NULL
) {
679 /* Wake waiting process */
680 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
682 /* Apps notified successfully */
690 * Setup the outgoing data buffer for the response (llm) by allocating the
691 * right amount of memory and copying the original information from the lsm
694 * Return total size of the buffer pointed by buf.
696 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
702 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
703 if (cmd_ctx
->llm
== NULL
) {
709 /* Copy common data */
710 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
711 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
713 cmd_ctx
->llm
->data_size
= size
;
714 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
723 * Update the kernel poll set of all channel fd available over all tracing
724 * session. Add the wakeup pipe at the end of the set.
726 static int update_kernel_poll(struct lttng_poll_event
*events
)
729 struct ltt_session
*session
;
730 struct ltt_kernel_channel
*channel
;
732 DBG("Updating kernel poll set");
735 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
736 session_lock(session
);
737 if (session
->kernel_session
== NULL
) {
738 session_unlock(session
);
742 cds_list_for_each_entry(channel
,
743 &session
->kernel_session
->channel_list
.head
, list
) {
744 /* Add channel fd to the kernel poll set */
745 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
747 session_unlock(session
);
750 DBG("Channel fd %d added to kernel set", channel
->fd
);
752 session_unlock(session
);
754 session_unlock_list();
759 session_unlock_list();
764 * Find the channel fd from 'fd' over all tracing session. When found, check
765 * for new channel stream and send those stream fds to the kernel consumer.
767 * Useful for CPU hotplug feature.
769 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
772 struct ltt_session
*session
;
773 struct ltt_kernel_session
*ksess
;
774 struct ltt_kernel_channel
*channel
;
776 DBG("Updating kernel streams for channel fd %d", fd
);
779 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
780 session_lock(session
);
781 if (session
->kernel_session
== NULL
) {
782 session_unlock(session
);
785 ksess
= session
->kernel_session
;
787 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
788 if (channel
->fd
== fd
) {
789 DBG("Channel found, updating kernel streams");
790 ret
= kernel_open_channel_stream(channel
);
794 /* Update the stream global counter */
795 ksess
->stream_count_global
+= ret
;
798 * Have we already sent fds to the consumer? If yes, it means
799 * that tracing is started so it is safe to send our updated
802 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
803 struct lttng_ht_iter iter
;
804 struct consumer_socket
*socket
;
807 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
808 &iter
.iter
, socket
, node
.node
) {
809 pthread_mutex_lock(socket
->lock
);
810 ret
= kernel_consumer_send_channel_stream(socket
,
812 session
->output_traces
? 1 : 0);
813 pthread_mutex_unlock(socket
->lock
);
824 session_unlock(session
);
826 session_unlock_list();
830 session_unlock(session
);
831 session_unlock_list();
836 * For each tracing session, update newly registered apps. The session list
837 * lock MUST be acquired before calling this.
839 static void update_ust_app(int app_sock
)
841 struct ltt_session
*sess
, *stmp
;
843 /* Consumer is in an ERROR state. Stop any application update. */
844 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
845 /* Stop the update process since the consumer is dead. */
849 /* For all tracing session(s) */
850 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
852 if (sess
->ust_session
) {
853 ust_app_global_update(sess
->ust_session
, app_sock
);
855 session_unlock(sess
);
860 * This thread manage event coming from the kernel.
862 * Features supported in this thread:
865 static void *thread_manage_kernel(void *data
)
867 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
868 uint32_t revents
, nb_fd
;
870 struct lttng_poll_event events
;
872 DBG("[thread] Thread manage kernel started");
874 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
877 * This first step of the while is to clean this structure which could free
878 * non NULL pointers so initialize it before the loop.
880 lttng_poll_init(&events
);
882 if (testpoint(sessiond_thread_manage_kernel
)) {
883 goto error_testpoint
;
886 health_code_update();
888 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
889 goto error_testpoint
;
893 health_code_update();
895 if (update_poll_flag
== 1) {
896 /* Clean events object. We are about to populate it again. */
897 lttng_poll_clean(&events
);
899 ret
= sessiond_set_thread_pollset(&events
, 2);
901 goto error_poll_create
;
904 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
909 /* This will add the available kernel channel if any. */
910 ret
= update_kernel_poll(&events
);
914 update_poll_flag
= 0;
917 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
919 /* Poll infinite value of time */
922 ret
= lttng_poll_wait(&events
, -1);
926 * Restart interrupted system call.
928 if (errno
== EINTR
) {
932 } else if (ret
== 0) {
933 /* Should not happen since timeout is infinite */
934 ERR("Return value of poll is 0 with an infinite timeout.\n"
935 "This should not have happened! Continuing...");
941 for (i
= 0; i
< nb_fd
; i
++) {
942 /* Fetch once the poll data */
943 revents
= LTTNG_POLL_GETEV(&events
, i
);
944 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
946 health_code_update();
949 /* No activity for this FD (poll implementation). */
953 /* Thread quit pipe has been closed. Killing thread. */
954 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
960 /* Check for data on kernel pipe */
961 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
962 (void) lttng_read(kernel_poll_pipe
[0],
965 * Ret value is useless here, if this pipe gets any actions an
966 * update is required anyway.
968 update_poll_flag
= 1;
972 * New CPU detected by the kernel. Adding kernel stream to
973 * kernel session and updating the kernel consumer
975 if (revents
& LPOLLIN
) {
976 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
982 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
983 * and unregister kernel stream at this point.
992 lttng_poll_clean(&events
);
995 utils_close_pipe(kernel_poll_pipe
);
996 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
999 ERR("Health error occurred in %s", __func__
);
1000 WARN("Kernel thread died unexpectedly. "
1001 "Kernel tracing can continue but CPU hotplug is disabled.");
1003 health_unregister(health_sessiond
);
1004 DBG("Kernel thread dying");
1009 * Signal pthread condition of the consumer data that the thread.
1011 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1013 pthread_mutex_lock(&data
->cond_mutex
);
1016 * The state is set before signaling. It can be any value, it's the waiter
1017 * job to correctly interpret this condition variable associated to the
1018 * consumer pthread_cond.
1020 * A value of 0 means that the corresponding thread of the consumer data
1021 * was not started. 1 indicates that the thread has started and is ready
1022 * for action. A negative value means that there was an error during the
1025 data
->consumer_thread_is_ready
= state
;
1026 (void) pthread_cond_signal(&data
->cond
);
1028 pthread_mutex_unlock(&data
->cond_mutex
);
1032 * This thread manage the consumer error sent back to the session daemon.
1034 static void *thread_manage_consumer(void *data
)
1036 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1037 uint32_t revents
, nb_fd
;
1038 enum lttcomm_return_code code
;
1039 struct lttng_poll_event events
;
1040 struct consumer_data
*consumer_data
= data
;
1042 DBG("[thread] Manage consumer started");
1044 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1046 health_code_update();
1049 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1050 * metadata_sock. Nothing more will be added to this poll set.
1052 ret
= sessiond_set_thread_pollset(&events
, 3);
1058 * The error socket here is already in a listening state which was done
1059 * just before spawning this thread to avoid a race between the consumer
1060 * daemon exec trying to connect and the listen() call.
1062 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1067 health_code_update();
1069 /* Infinite blocking call, waiting for transmission */
1071 health_poll_entry();
1073 if (testpoint(sessiond_thread_manage_consumer
)) {
1077 ret
= lttng_poll_wait(&events
, -1);
1081 * Restart interrupted system call.
1083 if (errno
== EINTR
) {
1091 for (i
= 0; i
< nb_fd
; i
++) {
1092 /* Fetch once the poll data */
1093 revents
= LTTNG_POLL_GETEV(&events
, i
);
1094 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1096 health_code_update();
1099 /* No activity for this FD (poll implementation). */
1103 /* Thread quit pipe has been closed. Killing thread. */
1104 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1110 /* Event on the registration socket */
1111 if (pollfd
== consumer_data
->err_sock
) {
1112 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1113 ERR("consumer err socket poll error");
1119 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1125 * Set the CLOEXEC flag. Return code is useless because either way, the
1128 (void) utils_set_fd_cloexec(sock
);
1130 health_code_update();
1132 DBG2("Receiving code from consumer err_sock");
1134 /* Getting status code from kconsumerd */
1135 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1136 sizeof(enum lttcomm_return_code
));
1141 health_code_update();
1142 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1143 /* Connect both socket, command and metadata. */
1144 consumer_data
->cmd_sock
=
1145 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1146 consumer_data
->metadata_fd
=
1147 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1148 if (consumer_data
->cmd_sock
< 0
1149 || consumer_data
->metadata_fd
< 0) {
1150 PERROR("consumer connect cmd socket");
1151 /* On error, signal condition and quit. */
1152 signal_consumer_condition(consumer_data
, -1);
1155 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1156 /* Create metadata socket lock. */
1157 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1158 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1159 PERROR("zmalloc pthread mutex");
1163 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1165 signal_consumer_condition(consumer_data
, 1);
1166 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1167 DBG("Consumer metadata socket ready (fd: %d)",
1168 consumer_data
->metadata_fd
);
1170 ERR("consumer error when waiting for SOCK_READY : %s",
1171 lttcomm_get_readable_code(-code
));
1175 /* Remove the consumerd error sock since we've established a connexion */
1176 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1181 /* Add new accepted error socket. */
1182 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1187 /* Add metadata socket that is successfully connected. */
1188 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1189 LPOLLIN
| LPOLLRDHUP
);
1194 health_code_update();
1196 /* Infinite blocking call, waiting for transmission */
1199 health_code_update();
1201 /* Exit the thread because the thread quit pipe has been triggered. */
1203 /* Not a health error. */
1208 health_poll_entry();
1209 ret
= lttng_poll_wait(&events
, -1);
1213 * Restart interrupted system call.
1215 if (errno
== EINTR
) {
1223 for (i
= 0; i
< nb_fd
; i
++) {
1224 /* Fetch once the poll data */
1225 revents
= LTTNG_POLL_GETEV(&events
, i
);
1226 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1228 health_code_update();
1231 /* No activity for this FD (poll implementation). */
1236 * Thread quit pipe has been triggered, flag that we should stop
1237 * but continue the current loop to handle potential data from
1240 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1242 if (pollfd
== sock
) {
1243 /* Event on the consumerd socket */
1244 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1245 ERR("consumer err socket second poll error");
1248 health_code_update();
1249 /* Wait for any kconsumerd error */
1250 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1251 sizeof(enum lttcomm_return_code
));
1253 ERR("consumer closed the command socket");
1257 ERR("consumer return code : %s",
1258 lttcomm_get_readable_code(-code
));
1261 } else if (pollfd
== consumer_data
->metadata_fd
) {
1262 /* UST metadata requests */
1263 ret
= ust_consumer_metadata_request(
1264 &consumer_data
->metadata_sock
);
1266 ERR("Handling metadata request");
1270 /* No need for an else branch all FDs are tested prior. */
1272 health_code_update();
1278 * We lock here because we are about to close the sockets and some other
1279 * thread might be using them so get exclusive access which will abort all
1280 * other consumer command by other threads.
1282 pthread_mutex_lock(&consumer_data
->lock
);
1284 /* Immediately set the consumerd state to stopped */
1285 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1286 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1287 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1288 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1289 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1291 /* Code flow error... */
1295 if (consumer_data
->err_sock
>= 0) {
1296 ret
= close(consumer_data
->err_sock
);
1300 consumer_data
->err_sock
= -1;
1302 if (consumer_data
->cmd_sock
>= 0) {
1303 ret
= close(consumer_data
->cmd_sock
);
1307 consumer_data
->cmd_sock
= -1;
1309 if (consumer_data
->metadata_sock
.fd_ptr
&&
1310 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1311 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1323 unlink(consumer_data
->err_unix_sock_path
);
1324 unlink(consumer_data
->cmd_unix_sock_path
);
1325 consumer_data
->pid
= 0;
1326 pthread_mutex_unlock(&consumer_data
->lock
);
1328 /* Cleanup metadata socket mutex. */
1329 if (consumer_data
->metadata_sock
.lock
) {
1330 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1331 free(consumer_data
->metadata_sock
.lock
);
1333 lttng_poll_clean(&events
);
1337 ERR("Health error occurred in %s", __func__
);
1339 health_unregister(health_sessiond
);
1340 DBG("consumer thread cleanup completed");
1346 * This thread manage application communication.
1348 static void *thread_manage_apps(void *data
)
1350 int i
, ret
, pollfd
, err
= -1;
1352 uint32_t revents
, nb_fd
;
1353 struct lttng_poll_event events
;
1355 DBG("[thread] Manage application started");
1357 rcu_register_thread();
1358 rcu_thread_online();
1360 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1362 if (testpoint(sessiond_thread_manage_apps
)) {
1363 goto error_testpoint
;
1366 health_code_update();
1368 ret
= sessiond_set_thread_pollset(&events
, 2);
1370 goto error_poll_create
;
1373 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1378 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1382 health_code_update();
1385 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1387 /* Inifinite blocking call, waiting for transmission */
1389 health_poll_entry();
1390 ret
= lttng_poll_wait(&events
, -1);
1394 * Restart interrupted system call.
1396 if (errno
== EINTR
) {
1404 for (i
= 0; i
< nb_fd
; i
++) {
1405 /* Fetch once the poll data */
1406 revents
= LTTNG_POLL_GETEV(&events
, i
);
1407 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1409 health_code_update();
1412 /* No activity for this FD (poll implementation). */
1416 /* Thread quit pipe has been closed. Killing thread. */
1417 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1423 /* Inspect the apps cmd pipe */
1424 if (pollfd
== apps_cmd_pipe
[0]) {
1425 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1426 ERR("Apps command pipe error");
1428 } else if (revents
& LPOLLIN
) {
1432 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1433 if (size_ret
< sizeof(sock
)) {
1434 PERROR("read apps cmd pipe");
1438 health_code_update();
1441 * We only monitor the error events of the socket. This
1442 * thread does not handle any incoming data from UST
1445 ret
= lttng_poll_add(&events
, sock
,
1446 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1451 DBG("Apps with sock %d added to poll set", sock
);
1455 * At this point, we know that a registered application made
1456 * the event at poll_wait.
1458 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1459 /* Removing from the poll set */
1460 ret
= lttng_poll_del(&events
, pollfd
);
1465 /* Socket closed on remote end. */
1466 ust_app_unregister(pollfd
);
1470 health_code_update();
1476 lttng_poll_clean(&events
);
1479 utils_close_pipe(apps_cmd_pipe
);
1480 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1483 * We don't clean the UST app hash table here since already registered
1484 * applications can still be controlled so let them be until the session
1485 * daemon dies or the applications stop.
1490 ERR("Health error occurred in %s", __func__
);
1492 health_unregister(health_sessiond
);
1493 DBG("Application communication apps thread cleanup complete");
1494 rcu_thread_offline();
1495 rcu_unregister_thread();
1500 * Send a socket to a thread This is called from the dispatch UST registration
1501 * thread once all sockets are set for the application.
1503 * The sock value can be invalid, we don't really care, the thread will handle
1504 * it and make the necessary cleanup if so.
1506 * On success, return 0 else a negative value being the errno message of the
1509 static int send_socket_to_thread(int fd
, int sock
)
1514 * It's possible that the FD is set as invalid with -1 concurrently just
1515 * before calling this function being a shutdown state of the thread.
1522 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1523 if (ret
< sizeof(sock
)) {
1524 PERROR("write apps pipe %d", fd
);
1531 /* All good. Don't send back the write positive ret value. */
1538 * Sanitize the wait queue of the dispatch registration thread meaning removing
1539 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1540 * notify socket is never received.
1542 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1544 int ret
, nb_fd
= 0, i
;
1545 unsigned int fd_added
= 0;
1546 struct lttng_poll_event events
;
1547 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1551 lttng_poll_init(&events
);
1553 /* Just skip everything for an empty queue. */
1554 if (!wait_queue
->count
) {
1558 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1563 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1564 &wait_queue
->head
, head
) {
1565 assert(wait_node
->app
);
1566 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1567 LPOLLHUP
| LPOLLERR
);
1580 * Poll but don't block so we can quickly identify the faulty events and
1581 * clean them afterwards from the wait queue.
1583 ret
= lttng_poll_wait(&events
, 0);
1589 for (i
= 0; i
< nb_fd
; i
++) {
1590 /* Get faulty FD. */
1591 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1592 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1595 /* No activity for this FD (poll implementation). */
1599 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1600 &wait_queue
->head
, head
) {
1601 if (pollfd
== wait_node
->app
->sock
&&
1602 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1603 cds_list_del(&wait_node
->head
);
1604 wait_queue
->count
--;
1605 ust_app_destroy(wait_node
->app
);
1613 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1617 lttng_poll_clean(&events
);
1621 lttng_poll_clean(&events
);
1623 ERR("Unable to sanitize wait queue");
1628 * Dispatch request from the registration threads to the application
1629 * communication thread.
1631 static void *thread_dispatch_ust_registration(void *data
)
1634 struct cds_wfq_node
*node
;
1635 struct ust_command
*ust_cmd
= NULL
;
1636 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1637 struct ust_reg_wait_queue wait_queue
= {
1641 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1643 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1644 goto error_testpoint
;
1647 health_code_update();
1649 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1651 DBG("[thread] Dispatch UST command started");
1653 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1654 health_code_update();
1656 /* Atomically prepare the queue futex */
1657 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1660 struct ust_app
*app
= NULL
;
1664 * Make sure we don't have node(s) that have hung up before receiving
1665 * the notify socket. This is to clean the list in order to avoid
1666 * memory leaks from notify socket that are never seen.
1668 sanitize_wait_queue(&wait_queue
);
1670 health_code_update();
1671 /* Dequeue command for registration */
1672 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1674 DBG("Woken up but nothing in the UST command queue");
1675 /* Continue thread execution */
1679 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1681 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1682 " gid:%d sock:%d name:%s (version %d.%d)",
1683 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1684 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1685 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1686 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1688 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1689 wait_node
= zmalloc(sizeof(*wait_node
));
1691 PERROR("zmalloc wait_node dispatch");
1692 ret
= close(ust_cmd
->sock
);
1694 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1696 lttng_fd_put(LTTNG_FD_APPS
, 1);
1700 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1702 /* Create application object if socket is CMD. */
1703 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1705 if (!wait_node
->app
) {
1706 ret
= close(ust_cmd
->sock
);
1708 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1710 lttng_fd_put(LTTNG_FD_APPS
, 1);
1716 * Add application to the wait queue so we can set the notify
1717 * socket before putting this object in the global ht.
1719 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1724 * We have to continue here since we don't have the notify
1725 * socket and the application MUST be added to the hash table
1726 * only at that moment.
1731 * Look for the application in the local wait queue and set the
1732 * notify socket if found.
1734 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1735 &wait_queue
.head
, head
) {
1736 health_code_update();
1737 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1738 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1739 cds_list_del(&wait_node
->head
);
1741 app
= wait_node
->app
;
1743 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1749 * With no application at this stage the received socket is
1750 * basically useless so close it before we free the cmd data
1751 * structure for good.
1754 ret
= close(ust_cmd
->sock
);
1756 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1758 lttng_fd_put(LTTNG_FD_APPS
, 1);
1765 * @session_lock_list
1767 * Lock the global session list so from the register up to the
1768 * registration done message, no thread can see the application
1769 * and change its state.
1771 session_lock_list();
1775 * Add application to the global hash table. This needs to be
1776 * done before the update to the UST registry can locate the
1781 /* Set app version. This call will print an error if needed. */
1782 (void) ust_app_version(app
);
1784 /* Send notify socket through the notify pipe. */
1785 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1789 session_unlock_list();
1791 * No notify thread, stop the UST tracing. However, this is
1792 * not an internal error of the this thread thus setting
1793 * the health error code to a normal exit.
1800 * Update newly registered application with the tracing
1801 * registry info already enabled information.
1803 update_ust_app(app
->sock
);
1806 * Don't care about return value. Let the manage apps threads
1807 * handle app unregistration upon socket close.
1809 (void) ust_app_register_done(app
->sock
);
1812 * Even if the application socket has been closed, send the app
1813 * to the thread and unregistration will take place at that
1816 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1819 session_unlock_list();
1821 * No apps. thread, stop the UST tracing. However, this is
1822 * not an internal error of the this thread thus setting
1823 * the health error code to a normal exit.
1830 session_unlock_list();
1832 } while (node
!= NULL
);
1834 health_poll_entry();
1835 /* Futex wait on queue. Blocking call on futex() */
1836 futex_nto1_wait(&ust_cmd_queue
.futex
);
1839 /* Normal exit, no error */
1843 /* Clean up wait queue. */
1844 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1845 &wait_queue
.head
, head
) {
1846 cds_list_del(&wait_node
->head
);
1852 DBG("Dispatch thread dying");
1855 ERR("Health error occurred in %s", __func__
);
1857 health_unregister(health_sessiond
);
1862 * This thread manage application registration.
1864 static void *thread_registration_apps(void *data
)
1866 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1867 uint32_t revents
, nb_fd
;
1868 struct lttng_poll_event events
;
1870 * Get allocated in this thread, enqueued to a global queue, dequeued and
1871 * freed in the manage apps thread.
1873 struct ust_command
*ust_cmd
= NULL
;
1875 DBG("[thread] Manage application registration started");
1877 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1879 if (testpoint(sessiond_thread_registration_apps
)) {
1880 goto error_testpoint
;
1883 ret
= lttcomm_listen_unix_sock(apps_sock
);
1889 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1890 * more will be added to this poll set.
1892 ret
= sessiond_set_thread_pollset(&events
, 2);
1894 goto error_create_poll
;
1897 /* Add the application registration socket */
1898 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1900 goto error_poll_add
;
1903 /* Notify all applications to register */
1904 ret
= notify_ust_apps(1);
1906 ERR("Failed to notify applications or create the wait shared memory.\n"
1907 "Execution continues but there might be problem for already\n"
1908 "running applications that wishes to register.");
1912 DBG("Accepting application registration");
1914 /* Inifinite blocking call, waiting for transmission */
1916 health_poll_entry();
1917 ret
= lttng_poll_wait(&events
, -1);
1921 * Restart interrupted system call.
1923 if (errno
== EINTR
) {
1931 for (i
= 0; i
< nb_fd
; i
++) {
1932 health_code_update();
1934 /* Fetch once the poll data */
1935 revents
= LTTNG_POLL_GETEV(&events
, i
);
1936 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1939 /* No activity for this FD (poll implementation). */
1943 /* Thread quit pipe has been closed. Killing thread. */
1944 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1950 /* Event on the registration socket */
1951 if (pollfd
== apps_sock
) {
1952 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1953 ERR("Register apps socket poll error");
1955 } else if (revents
& LPOLLIN
) {
1956 sock
= lttcomm_accept_unix_sock(apps_sock
);
1962 * Set socket timeout for both receiving and ending.
1963 * app_socket_timeout is in seconds, whereas
1964 * lttcomm_setsockopt_rcv_timeout and
1965 * lttcomm_setsockopt_snd_timeout expect msec as
1968 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1969 app_socket_timeout
* 1000);
1970 (void) lttcomm_setsockopt_snd_timeout(sock
,
1971 app_socket_timeout
* 1000);
1974 * Set the CLOEXEC flag. Return code is useless because
1975 * either way, the show must go on.
1977 (void) utils_set_fd_cloexec(sock
);
1979 /* Create UST registration command for enqueuing */
1980 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1981 if (ust_cmd
== NULL
) {
1982 PERROR("ust command zmalloc");
1987 * Using message-based transmissions to ensure we don't
1988 * have to deal with partially received messages.
1990 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1992 ERR("Exhausted file descriptors allowed for applications.");
2002 health_code_update();
2003 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2006 /* Close socket of the application. */
2011 lttng_fd_put(LTTNG_FD_APPS
, 1);
2015 health_code_update();
2017 ust_cmd
->sock
= sock
;
2020 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2021 " gid:%d sock:%d name:%s (version %d.%d)",
2022 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2023 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2024 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2025 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2028 * Lock free enqueue the registration request. The red pill
2029 * has been taken! This apps will be part of the *system*.
2031 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
2034 * Wake the registration queue futex. Implicit memory
2035 * barrier with the exchange in cds_wfq_enqueue.
2037 futex_nto1_wake(&ust_cmd_queue
.futex
);
2045 /* Notify that the registration thread is gone */
2048 if (apps_sock
>= 0) {
2049 ret
= close(apps_sock
);
2059 lttng_fd_put(LTTNG_FD_APPS
, 1);
2061 unlink(apps_unix_sock_path
);
2064 lttng_poll_clean(&events
);
2068 DBG("UST Registration thread cleanup complete");
2071 ERR("Health error occurred in %s", __func__
);
2073 health_unregister(health_sessiond
);
2079 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2080 * exec or it will fails.
2082 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2085 struct timespec timeout
;
2087 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2088 consumer_data
->consumer_thread_is_ready
= 0;
2090 /* Setup pthread condition */
2091 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2094 PERROR("pthread_condattr_init consumer data");
2099 * Set the monotonic clock in order to make sure we DO NOT jump in time
2100 * between the clock_gettime() call and the timedwait call. See bug #324
2101 * for a more details and how we noticed it.
2103 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2106 PERROR("pthread_condattr_setclock consumer data");
2110 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2113 PERROR("pthread_cond_init consumer data");
2117 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2120 PERROR("pthread_create consumer");
2125 /* We are about to wait on a pthread condition */
2126 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2128 /* Get time for sem_timedwait absolute timeout */
2129 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2131 * Set the timeout for the condition timed wait even if the clock gettime
2132 * call fails since we might loop on that call and we want to avoid to
2133 * increment the timeout too many times.
2135 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2138 * The following loop COULD be skipped in some conditions so this is why we
2139 * set ret to 0 in order to make sure at least one round of the loop is
2145 * Loop until the condition is reached or when a timeout is reached. Note
2146 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2147 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2148 * possible. This loop does not take any chances and works with both of
2151 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2152 if (clock_ret
< 0) {
2153 PERROR("clock_gettime spawn consumer");
2154 /* Infinite wait for the consumerd thread to be ready */
2155 ret
= pthread_cond_wait(&consumer_data
->cond
,
2156 &consumer_data
->cond_mutex
);
2158 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2159 &consumer_data
->cond_mutex
, &timeout
);
2163 /* Release the pthread condition */
2164 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2168 if (ret
== ETIMEDOUT
) {
2172 * Call has timed out so we kill the kconsumerd_thread and return
2175 ERR("Condition timed out. The consumer thread was never ready."
2177 pth_ret
= pthread_cancel(consumer_data
->thread
);
2179 PERROR("pthread_cancel consumer thread");
2182 PERROR("pthread_cond_wait failed consumer thread");
2184 /* Caller is expecting a negative value on failure. */
2189 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2190 if (consumer_data
->pid
== 0) {
2191 ERR("Consumerd did not start");
2192 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2195 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2204 * Join consumer thread
2206 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2210 /* Consumer pid must be a real one. */
2211 if (consumer_data
->pid
> 0) {
2213 ret
= kill(consumer_data
->pid
, SIGTERM
);
2215 ERR("Error killing consumer daemon");
2218 return pthread_join(consumer_data
->thread
, &status
);
2225 * Fork and exec a consumer daemon (consumerd).
2227 * Return pid if successful else -1.
2229 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2233 const char *consumer_to_use
;
2234 const char *verbosity
;
2237 DBG("Spawning consumerd");
2244 if (opt_verbose_consumer
) {
2245 verbosity
= "--verbose";
2247 verbosity
= "--quiet";
2249 switch (consumer_data
->type
) {
2250 case LTTNG_CONSUMER_KERNEL
:
2252 * Find out which consumerd to execute. We will first try the
2253 * 64-bit path, then the sessiond's installation directory, and
2254 * fallback on the 32-bit one,
2256 DBG3("Looking for a kernel consumer at these locations:");
2257 DBG3(" 1) %s", consumerd64_bin
);
2258 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2259 DBG3(" 3) %s", consumerd32_bin
);
2260 if (stat(consumerd64_bin
, &st
) == 0) {
2261 DBG3("Found location #1");
2262 consumer_to_use
= consumerd64_bin
;
2263 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2264 DBG3("Found location #2");
2265 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2266 } else if (stat(consumerd32_bin
, &st
) == 0) {
2267 DBG3("Found location #3");
2268 consumer_to_use
= consumerd32_bin
;
2270 DBG("Could not find any valid consumerd executable");
2274 DBG("Using kernel consumer at: %s", consumer_to_use
);
2275 ret
= execl(consumer_to_use
,
2276 "lttng-consumerd", verbosity
, "-k",
2277 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2278 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2279 "--group", tracing_group_name
,
2282 case LTTNG_CONSUMER64_UST
:
2284 char *tmpnew
= NULL
;
2286 if (consumerd64_libdir
[0] != '\0') {
2290 tmp
= getenv("LD_LIBRARY_PATH");
2294 tmplen
= strlen("LD_LIBRARY_PATH=")
2295 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2296 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2301 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2302 strcat(tmpnew
, consumerd64_libdir
);
2303 if (tmp
[0] != '\0') {
2304 strcat(tmpnew
, ":");
2305 strcat(tmpnew
, tmp
);
2307 ret
= putenv(tmpnew
);
2314 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2315 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2316 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2317 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2318 "--group", tracing_group_name
,
2320 if (consumerd64_libdir
[0] != '\0') {
2325 case LTTNG_CONSUMER32_UST
:
2327 char *tmpnew
= NULL
;
2329 if (consumerd32_libdir
[0] != '\0') {
2333 tmp
= getenv("LD_LIBRARY_PATH");
2337 tmplen
= strlen("LD_LIBRARY_PATH=")
2338 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2339 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2344 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2345 strcat(tmpnew
, consumerd32_libdir
);
2346 if (tmp
[0] != '\0') {
2347 strcat(tmpnew
, ":");
2348 strcat(tmpnew
, tmp
);
2350 ret
= putenv(tmpnew
);
2357 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2358 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2359 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2360 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2361 "--group", tracing_group_name
,
2363 if (consumerd32_libdir
[0] != '\0') {
2369 PERROR("unknown consumer type");
2373 PERROR("Consumer execl()");
2375 /* Reaching this point, we got a failure on our execl(). */
2377 } else if (pid
> 0) {
2380 PERROR("start consumer fork");
2388 * Spawn the consumerd daemon and session daemon thread.
2390 static int start_consumerd(struct consumer_data
*consumer_data
)
2395 * Set the listen() state on the socket since there is a possible race
2396 * between the exec() of the consumer daemon and this call if place in the
2397 * consumer thread. See bug #366 for more details.
2399 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2404 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2405 if (consumer_data
->pid
!= 0) {
2406 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2410 ret
= spawn_consumerd(consumer_data
);
2412 ERR("Spawning consumerd failed");
2413 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2417 /* Setting up the consumer_data pid */
2418 consumer_data
->pid
= ret
;
2419 DBG2("Consumer pid %d", consumer_data
->pid
);
2420 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2422 DBG2("Spawning consumer control thread");
2423 ret
= spawn_consumer_thread(consumer_data
);
2425 ERR("Fatal error spawning consumer control thread");
2433 /* Cleanup already created sockets on error. */
2434 if (consumer_data
->err_sock
>= 0) {
2437 err
= close(consumer_data
->err_sock
);
2439 PERROR("close consumer data error socket");
2446 * Setup necessary data for kernel tracer action.
2448 static int init_kernel_tracer(void)
2452 /* Modprobe lttng kernel modules */
2453 ret
= modprobe_lttng_control();
2458 /* Open debugfs lttng */
2459 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2460 if (kernel_tracer_fd
< 0) {
2461 DBG("Failed to open %s", module_proc_lttng
);
2466 /* Validate kernel version */
2467 ret
= kernel_validate_version(kernel_tracer_fd
);
2472 ret
= modprobe_lttng_data();
2477 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2481 modprobe_remove_lttng_control();
2482 ret
= close(kernel_tracer_fd
);
2486 kernel_tracer_fd
= -1;
2487 return LTTNG_ERR_KERN_VERSION
;
2490 ret
= close(kernel_tracer_fd
);
2496 modprobe_remove_lttng_control();
2499 WARN("No kernel tracer available");
2500 kernel_tracer_fd
= -1;
2502 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2504 return LTTNG_ERR_KERN_NA
;
2510 * Copy consumer output from the tracing session to the domain session. The
2511 * function also applies the right modification on a per domain basis for the
2512 * trace files destination directory.
2514 * Should *NOT* be called with RCU read-side lock held.
2516 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2519 const char *dir_name
;
2520 struct consumer_output
*consumer
;
2523 assert(session
->consumer
);
2526 case LTTNG_DOMAIN_KERNEL
:
2527 DBG3("Copying tracing session consumer output in kernel session");
2529 * XXX: We should audit the session creation and what this function
2530 * does "extra" in order to avoid a destroy since this function is used
2531 * in the domain session creation (kernel and ust) only. Same for UST
2534 if (session
->kernel_session
->consumer
) {
2535 consumer_destroy_output(session
->kernel_session
->consumer
);
2537 session
->kernel_session
->consumer
=
2538 consumer_copy_output(session
->consumer
);
2539 /* Ease our life a bit for the next part */
2540 consumer
= session
->kernel_session
->consumer
;
2541 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2543 case LTTNG_DOMAIN_JUL
:
2544 case LTTNG_DOMAIN_UST
:
2545 DBG3("Copying tracing session consumer output in UST session");
2546 if (session
->ust_session
->consumer
) {
2547 consumer_destroy_output(session
->ust_session
->consumer
);
2549 session
->ust_session
->consumer
=
2550 consumer_copy_output(session
->consumer
);
2551 /* Ease our life a bit for the next part */
2552 consumer
= session
->ust_session
->consumer
;
2553 dir_name
= DEFAULT_UST_TRACE_DIR
;
2556 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2560 /* Append correct directory to subdir */
2561 strncat(consumer
->subdir
, dir_name
,
2562 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2563 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2572 * Create an UST session and add it to the session ust list.
2574 * Should *NOT* be called with RCU read-side lock held.
2576 static int create_ust_session(struct ltt_session
*session
,
2577 struct lttng_domain
*domain
)
2580 struct ltt_ust_session
*lus
= NULL
;
2584 assert(session
->consumer
);
2586 switch (domain
->type
) {
2587 case LTTNG_DOMAIN_JUL
:
2588 case LTTNG_DOMAIN_UST
:
2591 ERR("Unknown UST domain on create session %d", domain
->type
);
2592 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2596 DBG("Creating UST session");
2598 lus
= trace_ust_create_session(session
->id
);
2600 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2604 lus
->uid
= session
->uid
;
2605 lus
->gid
= session
->gid
;
2606 lus
->output_traces
= session
->output_traces
;
2607 lus
->snapshot_mode
= session
->snapshot_mode
;
2608 lus
->live_timer_interval
= session
->live_timer
;
2609 session
->ust_session
= lus
;
2611 /* Copy session output to the newly created UST session */
2612 ret
= copy_session_consumer(domain
->type
, session
);
2613 if (ret
!= LTTNG_OK
) {
2621 session
->ust_session
= NULL
;
2626 * Create a kernel tracer session then create the default channel.
2628 static int create_kernel_session(struct ltt_session
*session
)
2632 DBG("Creating kernel session");
2634 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2636 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2640 /* Code flow safety */
2641 assert(session
->kernel_session
);
2643 /* Copy session output to the newly created Kernel session */
2644 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2645 if (ret
!= LTTNG_OK
) {
2649 /* Create directory(ies) on local filesystem. */
2650 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2651 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2652 ret
= run_as_mkdir_recursive(
2653 session
->kernel_session
->consumer
->dst
.trace_path
,
2654 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2656 if (ret
!= -EEXIST
) {
2657 ERR("Trace directory creation error");
2663 session
->kernel_session
->uid
= session
->uid
;
2664 session
->kernel_session
->gid
= session
->gid
;
2665 session
->kernel_session
->output_traces
= session
->output_traces
;
2666 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2671 trace_kernel_destroy_session(session
->kernel_session
);
2672 session
->kernel_session
= NULL
;
2677 * Count number of session permitted by uid/gid.
2679 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2682 struct ltt_session
*session
;
2684 DBG("Counting number of available session for UID %d GID %d",
2686 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2688 * Only list the sessions the user can control.
2690 if (!session_access_ok(session
, uid
, gid
)) {
2699 * Process the command requested by the lttng client within the command
2700 * context structure. This function make sure that the return structure (llm)
2701 * is set and ready for transmission before returning.
2703 * Return any error encountered or 0 for success.
2705 * "sock" is only used for special-case var. len data.
2707 * Should *NOT* be called with RCU read-side lock held.
2709 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2713 int need_tracing_session
= 1;
2716 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2720 switch (cmd_ctx
->lsm
->cmd_type
) {
2721 case LTTNG_CREATE_SESSION
:
2722 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2723 case LTTNG_CREATE_SESSION_LIVE
:
2724 case LTTNG_DESTROY_SESSION
:
2725 case LTTNG_LIST_SESSIONS
:
2726 case LTTNG_LIST_DOMAINS
:
2727 case LTTNG_START_TRACE
:
2728 case LTTNG_STOP_TRACE
:
2729 case LTTNG_DATA_PENDING
:
2730 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2731 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2732 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2733 case LTTNG_SNAPSHOT_RECORD
:
2740 if (opt_no_kernel
&& need_domain
2741 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2743 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2745 ret
= LTTNG_ERR_KERN_NA
;
2750 /* Deny register consumer if we already have a spawned consumer. */
2751 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2752 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2753 if (kconsumer_data
.pid
> 0) {
2754 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2755 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2758 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2762 * Check for command that don't needs to allocate a returned payload. We do
2763 * this here so we don't have to make the call for no payload at each
2766 switch(cmd_ctx
->lsm
->cmd_type
) {
2767 case LTTNG_LIST_SESSIONS
:
2768 case LTTNG_LIST_TRACEPOINTS
:
2769 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2770 case LTTNG_LIST_DOMAINS
:
2771 case LTTNG_LIST_CHANNELS
:
2772 case LTTNG_LIST_EVENTS
:
2775 /* Setup lttng message with no payload */
2776 ret
= setup_lttng_msg(cmd_ctx
, 0);
2778 /* This label does not try to unlock the session */
2779 goto init_setup_error
;
2783 /* Commands that DO NOT need a session. */
2784 switch (cmd_ctx
->lsm
->cmd_type
) {
2785 case LTTNG_CREATE_SESSION
:
2786 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2787 case LTTNG_CREATE_SESSION_LIVE
:
2788 case LTTNG_CALIBRATE
:
2789 case LTTNG_LIST_SESSIONS
:
2790 case LTTNG_LIST_TRACEPOINTS
:
2791 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2792 need_tracing_session
= 0;
2795 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2797 * We keep the session list lock across _all_ commands
2798 * for now, because the per-session lock does not
2799 * handle teardown properly.
2801 session_lock_list();
2802 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2803 if (cmd_ctx
->session
== NULL
) {
2804 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2807 /* Acquire lock for the session */
2808 session_lock(cmd_ctx
->session
);
2818 * Check domain type for specific "pre-action".
2820 switch (cmd_ctx
->lsm
->domain
.type
) {
2821 case LTTNG_DOMAIN_KERNEL
:
2823 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2827 /* Kernel tracer check */
2828 if (kernel_tracer_fd
== -1) {
2829 /* Basically, load kernel tracer modules */
2830 ret
= init_kernel_tracer();
2836 /* Consumer is in an ERROR state. Report back to client */
2837 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2838 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2842 /* Need a session for kernel command */
2843 if (need_tracing_session
) {
2844 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2845 ret
= create_kernel_session(cmd_ctx
->session
);
2847 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2852 /* Start the kernel consumer daemon */
2853 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2854 if (kconsumer_data
.pid
== 0 &&
2855 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2856 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2857 ret
= start_consumerd(&kconsumer_data
);
2859 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2862 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2864 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2868 * The consumer was just spawned so we need to add the socket to
2869 * the consumer output of the session if exist.
2871 ret
= consumer_create_socket(&kconsumer_data
,
2872 cmd_ctx
->session
->kernel_session
->consumer
);
2879 case LTTNG_DOMAIN_JUL
:
2880 case LTTNG_DOMAIN_UST
:
2882 if (!ust_app_supported()) {
2883 ret
= LTTNG_ERR_NO_UST
;
2886 /* Consumer is in an ERROR state. Report back to client */
2887 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2888 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2892 if (need_tracing_session
) {
2893 /* Create UST session if none exist. */
2894 if (cmd_ctx
->session
->ust_session
== NULL
) {
2895 ret
= create_ust_session(cmd_ctx
->session
,
2896 &cmd_ctx
->lsm
->domain
);
2897 if (ret
!= LTTNG_OK
) {
2902 /* Start the UST consumer daemons */
2904 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2905 if (consumerd64_bin
[0] != '\0' &&
2906 ustconsumer64_data
.pid
== 0 &&
2907 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2908 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2909 ret
= start_consumerd(&ustconsumer64_data
);
2911 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2912 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2916 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2917 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2919 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2923 * Setup socket for consumer 64 bit. No need for atomic access
2924 * since it was set above and can ONLY be set in this thread.
2926 ret
= consumer_create_socket(&ustconsumer64_data
,
2927 cmd_ctx
->session
->ust_session
->consumer
);
2933 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
2934 if (consumerd32_bin
[0] != '\0' &&
2935 ustconsumer32_data
.pid
== 0 &&
2936 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2937 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2938 ret
= start_consumerd(&ustconsumer32_data
);
2940 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2941 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2945 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2946 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2948 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2952 * Setup socket for consumer 64 bit. No need for atomic access
2953 * since it was set above and can ONLY be set in this thread.
2955 ret
= consumer_create_socket(&ustconsumer32_data
,
2956 cmd_ctx
->session
->ust_session
->consumer
);
2968 /* Validate consumer daemon state when start/stop trace command */
2969 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2970 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2971 switch (cmd_ctx
->lsm
->domain
.type
) {
2972 case LTTNG_DOMAIN_JUL
:
2973 case LTTNG_DOMAIN_UST
:
2974 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2975 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2979 case LTTNG_DOMAIN_KERNEL
:
2980 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2981 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2989 * Check that the UID or GID match that of the tracing session.
2990 * The root user can interact with all sessions.
2992 if (need_tracing_session
) {
2993 if (!session_access_ok(cmd_ctx
->session
,
2994 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2995 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2996 ret
= LTTNG_ERR_EPERM
;
3002 * Send relayd information to consumer as soon as we have a domain and a
3005 if (cmd_ctx
->session
&& need_domain
) {
3007 * Setup relayd if not done yet. If the relayd information was already
3008 * sent to the consumer, this call will gracefully return.
3010 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3011 if (ret
!= LTTNG_OK
) {
3016 /* Process by command type */
3017 switch (cmd_ctx
->lsm
->cmd_type
) {
3018 case LTTNG_ADD_CONTEXT
:
3020 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3021 cmd_ctx
->lsm
->u
.context
.channel_name
,
3022 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
3025 case LTTNG_DISABLE_CHANNEL
:
3027 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3028 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3031 case LTTNG_DISABLE_EVENT
:
3033 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3034 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3035 cmd_ctx
->lsm
->u
.disable
.name
);
3038 case LTTNG_DISABLE_ALL_EVENT
:
3040 DBG("Disabling all events");
3042 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3043 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3046 case LTTNG_ENABLE_CHANNEL
:
3048 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3049 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3052 case LTTNG_ENABLE_EVENT
:
3054 struct lttng_event_exclusion
*exclusion
= NULL
;
3055 struct lttng_filter_bytecode
*bytecode
= NULL
;
3057 /* Handle exclusion events and receive it from the client. */
3058 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3059 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3061 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3062 (count
* LTTNG_SYMBOL_NAME_LEN
));
3064 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3068 DBG("Receiving var len exclusion event list from client ...");
3069 exclusion
->count
= count
;
3070 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3071 count
* LTTNG_SYMBOL_NAME_LEN
);
3073 DBG("Nothing recv() from client var len data... continuing");
3076 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3081 /* Handle filter and get bytecode from client. */
3082 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3083 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3085 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3086 ret
= LTTNG_ERR_FILTER_INVAL
;
3091 bytecode
= zmalloc(bytecode_len
);
3094 ret
= LTTNG_ERR_FILTER_NOMEM
;
3098 /* Receive var. len. data */
3099 DBG("Receiving var len filter's bytecode from client ...");
3100 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3102 DBG("Nothing recv() from client car len data... continuing");
3106 ret
= LTTNG_ERR_FILTER_INVAL
;
3110 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3113 ret
= LTTNG_ERR_FILTER_INVAL
;
3118 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3119 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3120 &cmd_ctx
->lsm
->u
.enable
.event
, bytecode
, exclusion
,
3121 kernel_poll_pipe
[1]);
3124 case LTTNG_ENABLE_ALL_EVENT
:
3126 DBG("Enabling all events");
3128 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3129 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3130 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, kernel_poll_pipe
[1]);
3133 case LTTNG_LIST_TRACEPOINTS
:
3135 struct lttng_event
*events
;
3138 session_lock_list();
3139 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3140 session_unlock_list();
3141 if (nb_events
< 0) {
3142 /* Return value is a negative lttng_error_code. */
3148 * Setup lttng message with payload size set to the event list size in
3149 * bytes and then copy list into the llm payload.
3151 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3157 /* Copy event list into message payload */
3158 memcpy(cmd_ctx
->llm
->payload
, events
,
3159 sizeof(struct lttng_event
) * nb_events
);
3166 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3168 struct lttng_event_field
*fields
;
3171 session_lock_list();
3172 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3174 session_unlock_list();
3175 if (nb_fields
< 0) {
3176 /* Return value is a negative lttng_error_code. */
3182 * Setup lttng message with payload size set to the event list size in
3183 * bytes and then copy list into the llm payload.
3185 ret
= setup_lttng_msg(cmd_ctx
,
3186 sizeof(struct lttng_event_field
) * nb_fields
);
3192 /* Copy event list into message payload */
3193 memcpy(cmd_ctx
->llm
->payload
, fields
,
3194 sizeof(struct lttng_event_field
) * nb_fields
);
3201 case LTTNG_SET_CONSUMER_URI
:
3204 struct lttng_uri
*uris
;
3206 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3207 len
= nb_uri
* sizeof(struct lttng_uri
);
3210 ret
= LTTNG_ERR_INVALID
;
3214 uris
= zmalloc(len
);
3216 ret
= LTTNG_ERR_FATAL
;
3220 /* Receive variable len data */
3221 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3222 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3224 DBG("No URIs received from client... continuing");
3226 ret
= LTTNG_ERR_SESSION_FAIL
;
3231 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3233 if (ret
!= LTTNG_OK
) {
3239 * XXX: 0 means that this URI should be applied on the session. Should
3240 * be a DOMAIN enuam.
3242 if (cmd_ctx
->lsm
->domain
.type
== 0) {
3243 /* Add the URI for the UST session if a consumer is present. */
3244 if (cmd_ctx
->session
->ust_session
&&
3245 cmd_ctx
->session
->ust_session
->consumer
) {
3246 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
3248 } else if (cmd_ctx
->session
->kernel_session
&&
3249 cmd_ctx
->session
->kernel_session
->consumer
) {
3250 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
3251 cmd_ctx
->session
, nb_uri
, uris
);
3259 case LTTNG_START_TRACE
:
3261 ret
= cmd_start_trace(cmd_ctx
->session
);
3264 case LTTNG_STOP_TRACE
:
3266 ret
= cmd_stop_trace(cmd_ctx
->session
);
3269 case LTTNG_CREATE_SESSION
:
3272 struct lttng_uri
*uris
= NULL
;
3274 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3275 len
= nb_uri
* sizeof(struct lttng_uri
);
3278 uris
= zmalloc(len
);
3280 ret
= LTTNG_ERR_FATAL
;
3284 /* Receive variable len data */
3285 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3286 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3288 DBG("No URIs received from client... continuing");
3290 ret
= LTTNG_ERR_SESSION_FAIL
;
3295 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3296 DBG("Creating session with ONE network URI is a bad call");
3297 ret
= LTTNG_ERR_SESSION_FAIL
;
3303 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3304 &cmd_ctx
->creds
, 0);
3310 case LTTNG_DESTROY_SESSION
:
3312 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3314 /* Set session to NULL so we do not unlock it after free. */
3315 cmd_ctx
->session
= NULL
;
3318 case LTTNG_LIST_DOMAINS
:
3321 struct lttng_domain
*domains
;
3323 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3325 /* Return value is a negative lttng_error_code. */
3330 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3336 /* Copy event list into message payload */
3337 memcpy(cmd_ctx
->llm
->payload
, domains
,
3338 nb_dom
* sizeof(struct lttng_domain
));
3345 case LTTNG_LIST_CHANNELS
:
3348 struct lttng_channel
*channels
= NULL
;
3350 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3351 cmd_ctx
->session
, &channels
);
3353 /* Return value is a negative lttng_error_code. */
3358 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3364 /* Copy event list into message payload */
3365 memcpy(cmd_ctx
->llm
->payload
, channels
,
3366 nb_chan
* sizeof(struct lttng_channel
));
3373 case LTTNG_LIST_EVENTS
:
3376 struct lttng_event
*events
= NULL
;
3378 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3379 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3381 /* Return value is a negative lttng_error_code. */
3386 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3392 /* Copy event list into message payload */
3393 memcpy(cmd_ctx
->llm
->payload
, events
,
3394 nb_event
* sizeof(struct lttng_event
));
3401 case LTTNG_LIST_SESSIONS
:
3403 unsigned int nr_sessions
;
3405 session_lock_list();
3406 nr_sessions
= lttng_sessions_count(
3407 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3408 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3410 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3412 session_unlock_list();
3416 /* Filled the session array */
3417 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3418 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3419 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3421 session_unlock_list();
3426 case LTTNG_CALIBRATE
:
3428 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3429 &cmd_ctx
->lsm
->u
.calibrate
);
3432 case LTTNG_REGISTER_CONSUMER
:
3434 struct consumer_data
*cdata
;
3436 switch (cmd_ctx
->lsm
->domain
.type
) {
3437 case LTTNG_DOMAIN_KERNEL
:
3438 cdata
= &kconsumer_data
;
3441 ret
= LTTNG_ERR_UND
;
3445 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3446 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3449 case LTTNG_DATA_PENDING
:
3451 ret
= cmd_data_pending(cmd_ctx
->session
);
3454 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3456 struct lttcomm_lttng_output_id reply
;
3458 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3459 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3460 if (ret
!= LTTNG_OK
) {
3464 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3469 /* Copy output list into message payload */
3470 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3474 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3476 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3477 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3480 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3483 struct lttng_snapshot_output
*outputs
= NULL
;
3485 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3486 if (nb_output
< 0) {
3491 ret
= setup_lttng_msg(cmd_ctx
,
3492 nb_output
* sizeof(struct lttng_snapshot_output
));
3499 /* Copy output list into message payload */
3500 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3501 nb_output
* sizeof(struct lttng_snapshot_output
));
3508 case LTTNG_SNAPSHOT_RECORD
:
3510 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3511 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3512 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3515 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3518 struct lttng_uri
*uris
= NULL
;
3520 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3521 len
= nb_uri
* sizeof(struct lttng_uri
);
3524 uris
= zmalloc(len
);
3526 ret
= LTTNG_ERR_FATAL
;
3530 /* Receive variable len data */
3531 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3532 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3534 DBG("No URIs received from client... continuing");
3536 ret
= LTTNG_ERR_SESSION_FAIL
;
3541 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3542 DBG("Creating session with ONE network URI is a bad call");
3543 ret
= LTTNG_ERR_SESSION_FAIL
;
3549 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3550 nb_uri
, &cmd_ctx
->creds
);
3554 case LTTNG_CREATE_SESSION_LIVE
:
3557 struct lttng_uri
*uris
= NULL
;
3559 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3560 len
= nb_uri
* sizeof(struct lttng_uri
);
3563 uris
= zmalloc(len
);
3565 ret
= LTTNG_ERR_FATAL
;
3569 /* Receive variable len data */
3570 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3571 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3573 DBG("No URIs received from client... continuing");
3575 ret
= LTTNG_ERR_SESSION_FAIL
;
3580 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3581 DBG("Creating session with ONE network URI is a bad call");
3582 ret
= LTTNG_ERR_SESSION_FAIL
;
3588 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3589 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3594 ret
= LTTNG_ERR_UND
;
3599 if (cmd_ctx
->llm
== NULL
) {
3600 DBG("Missing llm structure. Allocating one.");
3601 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3605 /* Set return code */
3606 cmd_ctx
->llm
->ret_code
= ret
;
3608 if (cmd_ctx
->session
) {
3609 session_unlock(cmd_ctx
->session
);
3611 if (need_tracing_session
) {
3612 session_unlock_list();
3619 * Thread managing health check socket.
3621 static void *thread_manage_health(void *data
)
3623 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3624 uint32_t revents
, nb_fd
;
3625 struct lttng_poll_event events
;
3626 struct health_comm_msg msg
;
3627 struct health_comm_reply reply
;
3629 DBG("[thread] Manage health check started");
3631 rcu_register_thread();
3633 /* We might hit an error path before this is created. */
3634 lttng_poll_init(&events
);
3636 /* Create unix socket */
3637 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3639 ERR("Unable to create health check Unix socket");
3645 /* lttng health client socket path permissions */
3646 ret
= chown(health_unix_sock_path
, 0,
3647 utils_get_group_id(tracing_group_name
));
3649 ERR("Unable to set group on %s", health_unix_sock_path
);
3655 ret
= chmod(health_unix_sock_path
,
3656 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3658 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3666 * Set the CLOEXEC flag. Return code is useless because either way, the
3669 (void) utils_set_fd_cloexec(sock
);
3671 ret
= lttcomm_listen_unix_sock(sock
);
3677 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3678 * more will be added to this poll set.
3680 ret
= sessiond_set_thread_pollset(&events
, 2);
3685 /* Add the application registration socket */
3686 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3691 lttng_sessiond_notify_ready();
3694 DBG("Health check ready");
3696 /* Inifinite blocking call, waiting for transmission */
3698 ret
= lttng_poll_wait(&events
, -1);
3701 * Restart interrupted system call.
3703 if (errno
== EINTR
) {
3711 for (i
= 0; i
< nb_fd
; i
++) {
3712 /* Fetch once the poll data */
3713 revents
= LTTNG_POLL_GETEV(&events
, i
);
3714 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3717 /* No activity for this FD (poll implementation). */
3721 /* Thread quit pipe has been closed. Killing thread. */
3722 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3728 /* Event on the registration socket */
3729 if (pollfd
== sock
) {
3730 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3731 ERR("Health socket poll error");
3737 new_sock
= lttcomm_accept_unix_sock(sock
);
3743 * Set the CLOEXEC flag. Return code is useless because either way, the
3746 (void) utils_set_fd_cloexec(new_sock
);
3748 DBG("Receiving data from client for health...");
3749 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3751 DBG("Nothing recv() from client... continuing");
3752 ret
= close(new_sock
);
3760 rcu_thread_online();
3762 memset(&reply
, 0, sizeof(reply
));
3763 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
3765 * health_check_state returns 0 if health is
3768 if (!health_check_state(health_sessiond
, i
)) {
3769 reply
.ret_code
|= 1ULL << i
;
3773 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
3775 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3777 ERR("Failed to send health data back to client");
3780 /* End of transmission */
3781 ret
= close(new_sock
);
3791 ERR("Health error occurred in %s", __func__
);
3793 DBG("Health check thread dying");
3794 unlink(health_unix_sock_path
);
3802 lttng_poll_clean(&events
);
3804 rcu_unregister_thread();
3809 * This thread manage all clients request using the unix client socket for
3812 static void *thread_manage_clients(void *data
)
3814 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3816 uint32_t revents
, nb_fd
;
3817 struct command_ctx
*cmd_ctx
= NULL
;
3818 struct lttng_poll_event events
;
3820 DBG("[thread] Manage client started");
3822 rcu_register_thread();
3824 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
3826 health_code_update();
3828 ret
= lttcomm_listen_unix_sock(client_sock
);
3834 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3835 * more will be added to this poll set.
3837 ret
= sessiond_set_thread_pollset(&events
, 2);
3839 goto error_create_poll
;
3842 /* Add the application registration socket */
3843 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3848 lttng_sessiond_notify_ready();
3850 /* This testpoint is after we signal readiness to the parent. */
3851 if (testpoint(sessiond_thread_manage_clients
)) {
3855 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
3859 health_code_update();
3862 DBG("Accepting client command ...");
3864 /* Inifinite blocking call, waiting for transmission */
3866 health_poll_entry();
3867 ret
= lttng_poll_wait(&events
, -1);
3871 * Restart interrupted system call.
3873 if (errno
== EINTR
) {
3881 for (i
= 0; i
< nb_fd
; i
++) {
3882 /* Fetch once the poll data */
3883 revents
= LTTNG_POLL_GETEV(&events
, i
);
3884 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3886 health_code_update();
3889 /* No activity for this FD (poll implementation). */
3893 /* Thread quit pipe has been closed. Killing thread. */
3894 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3900 /* Event on the registration socket */
3901 if (pollfd
== client_sock
) {
3902 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3903 ERR("Client socket poll error");
3909 DBG("Wait for client response");
3911 health_code_update();
3913 sock
= lttcomm_accept_unix_sock(client_sock
);
3919 * Set the CLOEXEC flag. Return code is useless because either way, the
3922 (void) utils_set_fd_cloexec(sock
);
3924 /* Set socket option for credentials retrieval */
3925 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3930 /* Allocate context command to process the client request */
3931 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3932 if (cmd_ctx
== NULL
) {
3933 PERROR("zmalloc cmd_ctx");
3937 /* Allocate data buffer for reception */
3938 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3939 if (cmd_ctx
->lsm
== NULL
) {
3940 PERROR("zmalloc cmd_ctx->lsm");
3944 cmd_ctx
->llm
= NULL
;
3945 cmd_ctx
->session
= NULL
;
3947 health_code_update();
3950 * Data is received from the lttng client. The struct
3951 * lttcomm_session_msg (lsm) contains the command and data request of
3954 DBG("Receiving data from client ...");
3955 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3956 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3958 DBG("Nothing recv() from client... continuing");
3964 clean_command_ctx(&cmd_ctx
);
3968 health_code_update();
3970 // TODO: Validate cmd_ctx including sanity check for
3971 // security purpose.
3973 rcu_thread_online();
3975 * This function dispatch the work to the kernel or userspace tracer
3976 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3977 * informations for the client. The command context struct contains
3978 * everything this function may needs.
3980 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3981 rcu_thread_offline();
3989 * TODO: Inform client somehow of the fatal error. At
3990 * this point, ret < 0 means that a zmalloc failed
3991 * (ENOMEM). Error detected but still accept
3992 * command, unless a socket error has been
3995 clean_command_ctx(&cmd_ctx
);
3999 health_code_update();
4001 DBG("Sending response (size: %d, retcode: %s)",
4002 cmd_ctx
->lttng_msg_size
,
4003 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
4004 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4006 ERR("Failed to send data back to client");
4009 /* End of transmission */
4016 clean_command_ctx(&cmd_ctx
);
4018 health_code_update();
4030 lttng_poll_clean(&events
);
4031 clean_command_ctx(&cmd_ctx
);
4035 unlink(client_unix_sock_path
);
4036 if (client_sock
>= 0) {
4037 ret
= close(client_sock
);
4045 ERR("Health error occurred in %s", __func__
);
4048 health_unregister(health_sessiond
);
4050 DBG("Client thread dying");
4052 rcu_unregister_thread();
4058 * usage function on stderr
4060 static void usage(void)
4062 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4063 fprintf(stderr
, " -h, --help Display this usage.\n");
4064 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4065 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4066 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4067 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4068 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4069 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4070 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4071 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4072 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4073 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4074 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4075 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4076 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4077 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
4078 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4079 fprintf(stderr
, " -V, --version Show version number.\n");
4080 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4081 fprintf(stderr
, " -q, --quiet No output at all.\n");
4082 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4083 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4084 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4085 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4086 fprintf(stderr
, " --jul-tcp-port JUL application registration TCP port\n");
4090 * daemon argument parsing
4092 static int parse_args(int argc
, char **argv
)
4096 static struct option long_options
[] = {
4097 { "client-sock", 1, 0, 'c' },
4098 { "apps-sock", 1, 0, 'a' },
4099 { "kconsumerd-cmd-sock", 1, 0, 'C' },
4100 { "kconsumerd-err-sock", 1, 0, 'E' },
4101 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
4102 { "ustconsumerd32-err-sock", 1, 0, 'H' },
4103 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
4104 { "ustconsumerd64-err-sock", 1, 0, 'F' },
4105 { "consumerd32-path", 1, 0, 'u' },
4106 { "consumerd32-libdir", 1, 0, 'U' },
4107 { "consumerd64-path", 1, 0, 't' },
4108 { "consumerd64-libdir", 1, 0, 'T' },
4109 { "daemonize", 0, 0, 'd' },
4110 { "sig-parent", 0, 0, 'S' },
4111 { "help", 0, 0, 'h' },
4112 { "group", 1, 0, 'g' },
4113 { "version", 0, 0, 'V' },
4114 { "quiet", 0, 0, 'q' },
4115 { "verbose", 0, 0, 'v' },
4116 { "verbose-consumer", 0, 0, 'Z' },
4117 { "no-kernel", 0, 0, 'N' },
4118 { "pidfile", 1, 0, 'p' },
4119 { "jul-tcp-port", 1, 0, 'J' },
4120 { "background", 0, 0, 'b' },
4125 int option_index
= 0;
4126 c
= getopt_long(argc
, argv
, "dhqvVSN" "a:c:g:s:C:E:D:F:Z:u:t:p:J:b",
4127 long_options
, &option_index
);
4134 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
4136 fprintf(stderr
, " with arg %s\n", optarg
);
4140 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4143 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4152 tracing_group_name
= optarg
;
4158 fprintf(stdout
, "%s\n", VERSION
);
4164 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4167 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4170 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4173 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4176 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4179 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
4185 lttng_opt_quiet
= 1;
4188 /* Verbose level can increase using multiple -v */
4189 lttng_opt_verbose
+= 1;
4192 opt_verbose_consumer
+= 1;
4195 consumerd32_bin
= optarg
;
4198 consumerd32_libdir
= optarg
;
4201 consumerd64_bin
= optarg
;
4204 consumerd64_libdir
= optarg
;
4207 opt_pidfile
= optarg
;
4209 case 'J': /* JUL TCP port. */
4214 v
= strtoul(optarg
, NULL
, 0);
4215 if (errno
!= 0 || !isdigit(optarg
[0])) {
4216 ERR("Wrong value in --jul-tcp-port parameter: %s", optarg
);
4219 if (v
== 0 || v
>= 65535) {
4220 ERR("Port overflow in --jul-tcp-port parameter: %s", optarg
);
4223 jul_tcp_port
= (uint32_t) v
;
4224 DBG3("JUL TCP port set to non default: %u", jul_tcp_port
);
4228 /* Unknown option or other error.
4229 * Error is printed by getopt, just return */
4238 * Creates the two needed socket by the daemon.
4239 * apps_sock - The communication socket for all UST apps.
4240 * client_sock - The communication of the cli tool (lttng).
4242 static int init_daemon_socket(void)
4247 old_umask
= umask(0);
4249 /* Create client tool unix socket */
4250 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4251 if (client_sock
< 0) {
4252 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4257 /* Set the cloexec flag */
4258 ret
= utils_set_fd_cloexec(client_sock
);
4260 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4261 "Continuing but note that the consumer daemon will have a "
4262 "reference to this socket on exec()", client_sock
);
4265 /* File permission MUST be 660 */
4266 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4268 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4273 /* Create the application unix socket */
4274 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4275 if (apps_sock
< 0) {
4276 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4281 /* Set the cloexec flag */
4282 ret
= utils_set_fd_cloexec(apps_sock
);
4284 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4285 "Continuing but note that the consumer daemon will have a "
4286 "reference to this socket on exec()", apps_sock
);
4289 /* File permission MUST be 666 */
4290 ret
= chmod(apps_unix_sock_path
,
4291 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4293 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4298 DBG3("Session daemon client socket %d and application socket %d created",
4299 client_sock
, apps_sock
);
4307 * Check if the global socket is available, and if a daemon is answering at the
4308 * other side. If yes, error is returned.
4310 static int check_existing_daemon(void)
4312 /* Is there anybody out there ? */
4313 if (lttng_session_daemon_alive()) {
4321 * Set the tracing group gid onto the client socket.
4323 * Race window between mkdir and chown is OK because we are going from more
4324 * permissive (root.root) to less permissive (root.tracing).
4326 static int set_permissions(char *rundir
)
4331 gid
= utils_get_group_id(tracing_group_name
);
4333 /* Set lttng run dir */
4334 ret
= chown(rundir
, 0, gid
);
4336 ERR("Unable to set group on %s", rundir
);
4341 * Ensure all applications and tracing group can search the run
4342 * dir. Allow everyone to read the directory, since it does not
4343 * buy us anything to hide its content.
4345 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4347 ERR("Unable to set permissions on %s", rundir
);
4351 /* lttng client socket path */
4352 ret
= chown(client_unix_sock_path
, 0, gid
);
4354 ERR("Unable to set group on %s", client_unix_sock_path
);
4358 /* kconsumer error socket path */
4359 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4361 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4365 /* 64-bit ustconsumer error socket path */
4366 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4368 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4372 /* 32-bit ustconsumer compat32 error socket path */
4373 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4375 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4379 DBG("All permissions are set");
4385 * Create the lttng run directory needed for all global sockets and pipe.
4387 static int create_lttng_rundir(const char *rundir
)
4391 DBG3("Creating LTTng run directory: %s", rundir
);
4393 ret
= mkdir(rundir
, S_IRWXU
);
4395 if (errno
!= EEXIST
) {
4396 ERR("Unable to create %s", rundir
);
4408 * Setup sockets and directory needed by the kconsumerd communication with the
4411 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4415 char path
[PATH_MAX
];
4417 switch (consumer_data
->type
) {
4418 case LTTNG_CONSUMER_KERNEL
:
4419 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4421 case LTTNG_CONSUMER64_UST
:
4422 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4424 case LTTNG_CONSUMER32_UST
:
4425 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4428 ERR("Consumer type unknown");
4433 DBG2("Creating consumer directory: %s", path
);
4435 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4437 if (errno
!= EEXIST
) {
4439 ERR("Failed to create %s", path
);
4445 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4447 ERR("Unable to set group on %s", path
);
4453 /* Create the kconsumerd error unix socket */
4454 consumer_data
->err_sock
=
4455 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4456 if (consumer_data
->err_sock
< 0) {
4457 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4463 * Set the CLOEXEC flag. Return code is useless because either way, the
4466 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4468 PERROR("utils_set_fd_cloexec");
4469 /* continue anyway */
4472 /* File permission MUST be 660 */
4473 ret
= chmod(consumer_data
->err_unix_sock_path
,
4474 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4476 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4486 * Signal handler for the daemon
4488 * Simply stop all worker threads, leaving main() return gracefully after
4489 * joining all threads and calling cleanup().
4491 static void sighandler(int sig
)
4495 DBG("SIGPIPE caught");
4498 DBG("SIGINT caught");
4502 DBG("SIGTERM caught");
4506 CMM_STORE_SHARED(recv_child_signal
, 1);
4514 * Setup signal handler for :
4515 * SIGINT, SIGTERM, SIGPIPE
4517 static int set_signal_handler(void)
4520 struct sigaction sa
;
4523 if ((ret
= sigemptyset(&sigset
)) < 0) {
4524 PERROR("sigemptyset");
4528 sa
.sa_handler
= sighandler
;
4529 sa
.sa_mask
= sigset
;
4531 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
4532 PERROR("sigaction");
4536 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
4537 PERROR("sigaction");
4541 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
4542 PERROR("sigaction");
4546 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
4547 PERROR("sigaction");
4551 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
4557 * Set open files limit to unlimited. This daemon can open a large number of
4558 * file descriptors in order to consumer multiple kernel traces.
4560 static void set_ulimit(void)
4565 /* The kernel does not allowed an infinite limit for open files */
4566 lim
.rlim_cur
= 65535;
4567 lim
.rlim_max
= 65535;
4569 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
4571 PERROR("failed to set open files limit");
4576 * Write pidfile using the rundir and opt_pidfile.
4578 static void write_pidfile(void)
4581 char pidfile_path
[PATH_MAX
];
4586 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4588 /* Build pidfile path from rundir and opt_pidfile. */
4589 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4590 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4592 PERROR("snprintf pidfile path");
4598 * Create pid file in rundir. Return value is of no importance. The
4599 * execution will continue even though we are not able to write the file.
4601 (void) utils_create_pid_file(getpid(), pidfile_path
);
4608 * Create lockfile using the rundir and return its fd.
4610 static int create_lockfile(void)
4613 char lockfile_path
[PATH_MAX
];
4615 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
4620 ret
= utils_create_lock_file(lockfile_path
);
4626 * Write JUL TCP port using the rundir.
4628 static void write_julport(void)
4631 char path
[PATH_MAX
];
4635 ret
= snprintf(path
, sizeof(path
), "%s/"
4636 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
, rundir
);
4638 PERROR("snprintf julport path");
4643 * Create TCP JUL port file in rundir. Return value is of no importance.
4644 * The execution will continue even though we are not able to write the
4647 (void) utils_create_pid_file(jul_tcp_port
, path
);
4656 int main(int argc
, char **argv
)
4660 const char *home_path
, *env_app_timeout
;
4662 init_kernel_workarounds();
4664 rcu_register_thread();
4666 if ((ret
= set_signal_handler()) < 0) {
4670 setup_consumerd_path();
4672 page_size
= sysconf(_SC_PAGESIZE
);
4673 if (page_size
< 0) {
4674 PERROR("sysconf _SC_PAGESIZE");
4675 page_size
= LONG_MAX
;
4676 WARN("Fallback page size to %ld", page_size
);
4679 /* Parse arguments */
4681 if ((ret
= parse_args(argc
, argv
)) < 0) {
4686 if (opt_daemon
|| opt_background
) {
4689 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
4696 * We are in the child. Make sure all other file descriptors are
4697 * closed, in case we are called with more opened file descriptors than
4698 * the standard ones.
4700 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4705 /* Create thread quit pipe */
4706 if ((ret
= init_thread_quit_pipe()) < 0) {
4710 /* Check if daemon is UID = 0 */
4711 is_root
= !getuid();
4714 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4720 /* Create global run dir with root access */
4721 ret
= create_lttng_rundir(rundir
);
4726 if (strlen(apps_unix_sock_path
) == 0) {
4727 snprintf(apps_unix_sock_path
, PATH_MAX
,
4728 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4731 if (strlen(client_unix_sock_path
) == 0) {
4732 snprintf(client_unix_sock_path
, PATH_MAX
,
4733 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4736 /* Set global SHM for ust */
4737 if (strlen(wait_shm_path
) == 0) {
4738 snprintf(wait_shm_path
, PATH_MAX
,
4739 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4742 if (strlen(health_unix_sock_path
) == 0) {
4743 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4744 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4747 /* Setup kernel consumerd path */
4748 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4749 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4750 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4751 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4753 DBG2("Kernel consumer err path: %s",
4754 kconsumer_data
.err_unix_sock_path
);
4755 DBG2("Kernel consumer cmd path: %s",
4756 kconsumer_data
.cmd_unix_sock_path
);
4758 home_path
= utils_get_home_dir();
4759 if (home_path
== NULL
) {
4760 /* TODO: Add --socket PATH option */
4761 ERR("Can't get HOME directory for sockets creation.");
4767 * Create rundir from home path. This will create something like
4770 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4776 ret
= create_lttng_rundir(rundir
);
4781 if (strlen(apps_unix_sock_path
) == 0) {
4782 snprintf(apps_unix_sock_path
, PATH_MAX
,
4783 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4786 /* Set the cli tool unix socket path */
4787 if (strlen(client_unix_sock_path
) == 0) {
4788 snprintf(client_unix_sock_path
, PATH_MAX
,
4789 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4792 /* Set global SHM for ust */
4793 if (strlen(wait_shm_path
) == 0) {
4794 snprintf(wait_shm_path
, PATH_MAX
,
4795 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4798 /* Set health check Unix path */
4799 if (strlen(health_unix_sock_path
) == 0) {
4800 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4801 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4805 lockfile_fd
= create_lockfile();
4806 if (lockfile_fd
< 0) {
4810 /* Set consumer initial state */
4811 kernel_consumerd_state
= CONSUMER_STOPPED
;
4812 ust_consumerd_state
= CONSUMER_STOPPED
;
4814 DBG("Client socket path %s", client_unix_sock_path
);
4815 DBG("Application socket path %s", apps_unix_sock_path
);
4816 DBG("Application wait path %s", wait_shm_path
);
4817 DBG("LTTng run directory path: %s", rundir
);
4819 /* 32 bits consumerd path setup */
4820 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4821 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4822 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4823 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4825 DBG2("UST consumer 32 bits err path: %s",
4826 ustconsumer32_data
.err_unix_sock_path
);
4827 DBG2("UST consumer 32 bits cmd path: %s",
4828 ustconsumer32_data
.cmd_unix_sock_path
);
4830 /* 64 bits consumerd path setup */
4831 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4832 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4833 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4834 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4836 DBG2("UST consumer 64 bits err path: %s",
4837 ustconsumer64_data
.err_unix_sock_path
);
4838 DBG2("UST consumer 64 bits cmd path: %s",
4839 ustconsumer64_data
.cmd_unix_sock_path
);
4842 * See if daemon already exist.
4844 if ((ret
= check_existing_daemon()) < 0) {
4845 ERR("Already running daemon.\n");
4847 * We do not goto exit because we must not cleanup()
4848 * because a daemon is already running.
4854 * Init UST app hash table. Alloc hash table before this point since
4855 * cleanup() can get called after that point.
4859 /* Initialize JUL domain subsystem. */
4860 if ((ret
= jul_init()) < 0) {
4861 /* ENOMEM at this point. */
4865 /* After this point, we can safely call cleanup() with "goto exit" */
4868 * These actions must be executed as root. We do that *after* setting up
4869 * the sockets path because we MUST make the check for another daemon using
4870 * those paths *before* trying to set the kernel consumer sockets and init
4874 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
4879 /* Setup kernel tracer */
4880 if (!opt_no_kernel
) {
4881 init_kernel_tracer();
4884 /* Set ulimit for open files */
4887 /* init lttng_fd tracking must be done after set_ulimit. */
4890 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
4895 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
4900 /* Setup the needed unix socket */
4901 if ((ret
= init_daemon_socket()) < 0) {
4905 /* Set credentials to socket */
4906 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
4910 /* Get parent pid if -S, --sig-parent is specified. */
4911 if (opt_sig_parent
) {
4915 /* Setup the kernel pipe for waking up the kernel thread */
4916 if (is_root
&& !opt_no_kernel
) {
4917 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
4922 /* Setup the thread ht_cleanup communication pipe. */
4923 if (utils_create_pipe_cloexec(ht_cleanup_pipe
) < 0) {
4927 /* Setup the thread apps communication pipe. */
4928 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
4932 /* Setup the thread apps notify communication pipe. */
4933 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
4937 /* Initialize global buffer per UID and PID registry. */
4938 buffer_reg_init_uid_registry();
4939 buffer_reg_init_pid_registry();
4941 /* Init UST command queue. */
4942 cds_wfq_init(&ust_cmd_queue
.queue
);
4945 * Get session list pointer. This pointer MUST NOT be free(). This list is
4946 * statically declared in session.c
4948 session_list_ptr
= session_get_list();
4950 /* Set up max poll set size */
4951 lttng_poll_set_max_size();
4955 /* Check for the application socket timeout env variable. */
4956 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
4957 if (env_app_timeout
) {
4958 app_socket_timeout
= atoi(env_app_timeout
);
4960 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
4966 /* Initialize communication library */
4968 /* This is to get the TCP timeout value. */
4969 lttcomm_inet_init();
4972 * Initialize the health check subsystem. This call should set the
4973 * appropriate time values.
4975 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
4976 if (!health_sessiond
) {
4977 PERROR("health_app_create error");
4978 goto exit_health_sessiond_cleanup
;
4981 /* Create thread to clean up RCU hash tables */
4982 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
4983 thread_ht_cleanup
, (void *) NULL
);
4985 PERROR("pthread_create ht_cleanup");
4986 goto exit_ht_cleanup
;
4989 /* Create health-check thread */
4990 ret
= pthread_create(&health_thread
, NULL
,
4991 thread_manage_health
, (void *) NULL
);
4993 PERROR("pthread_create health");
4997 /* Create thread to manage the client socket */
4998 ret
= pthread_create(&client_thread
, NULL
,
4999 thread_manage_clients
, (void *) NULL
);
5001 PERROR("pthread_create clients");
5005 /* Create thread to dispatch registration */
5006 ret
= pthread_create(&dispatch_thread
, NULL
,
5007 thread_dispatch_ust_registration
, (void *) NULL
);
5009 PERROR("pthread_create dispatch");
5013 /* Create thread to manage application registration. */
5014 ret
= pthread_create(®_apps_thread
, NULL
,
5015 thread_registration_apps
, (void *) NULL
);
5017 PERROR("pthread_create registration");
5021 /* Create thread to manage application socket */
5022 ret
= pthread_create(&apps_thread
, NULL
,
5023 thread_manage_apps
, (void *) NULL
);
5025 PERROR("pthread_create apps");
5029 /* Create thread to manage application notify socket */
5030 ret
= pthread_create(&apps_notify_thread
, NULL
,
5031 ust_thread_manage_notify
, (void *) NULL
);
5033 PERROR("pthread_create notify");
5034 goto exit_apps_notify
;
5037 /* Create JUL registration thread. */
5038 ret
= pthread_create(&jul_reg_thread
, NULL
,
5039 jul_thread_manage_registration
, (void *) NULL
);
5041 PERROR("pthread_create JUL");
5045 /* Don't start this thread if kernel tracing is not requested nor root */
5046 if (is_root
&& !opt_no_kernel
) {
5047 /* Create kernel thread to manage kernel event */
5048 ret
= pthread_create(&kernel_thread
, NULL
,
5049 thread_manage_kernel
, (void *) NULL
);
5051 PERROR("pthread_create kernel");
5055 ret
= pthread_join(kernel_thread
, &status
);
5057 PERROR("pthread_join");
5058 goto error
; /* join error, exit without cleanup */
5063 ret
= pthread_join(jul_reg_thread
, &status
);
5065 PERROR("pthread_join JUL");
5066 goto error
; /* join error, exit without cleanup */
5070 ret
= pthread_join(apps_notify_thread
, &status
);
5072 PERROR("pthread_join apps notify");
5073 goto error
; /* join error, exit without cleanup */
5077 ret
= pthread_join(apps_thread
, &status
);
5079 PERROR("pthread_join apps");
5080 goto error
; /* join error, exit without cleanup */
5085 ret
= pthread_join(reg_apps_thread
, &status
);
5087 PERROR("pthread_join");
5088 goto error
; /* join error, exit without cleanup */
5092 ret
= pthread_join(dispatch_thread
, &status
);
5094 PERROR("pthread_join");
5095 goto error
; /* join error, exit without cleanup */
5099 ret
= pthread_join(client_thread
, &status
);
5101 PERROR("pthread_join");
5102 goto error
; /* join error, exit without cleanup */
5105 ret
= join_consumer_thread(&kconsumer_data
);
5107 PERROR("join_consumer");
5108 goto error
; /* join error, exit without cleanup */
5111 ret
= join_consumer_thread(&ustconsumer32_data
);
5113 PERROR("join_consumer ust32");
5114 goto error
; /* join error, exit without cleanup */
5117 ret
= join_consumer_thread(&ustconsumer64_data
);
5119 PERROR("join_consumer ust64");
5120 goto error
; /* join error, exit without cleanup */
5124 ret
= pthread_join(health_thread
, &status
);
5126 PERROR("pthread_join health thread");
5127 goto error
; /* join error, exit without cleanup */
5131 ret
= pthread_join(ht_cleanup_thread
, &status
);
5133 PERROR("pthread_join ht cleanup thread");
5134 goto error
; /* join error, exit without cleanup */
5137 health_app_destroy(health_sessiond
);
5138 exit_health_sessiond_cleanup
:
5141 * cleanup() is called when no other thread is running.
5143 rcu_thread_online();
5145 rcu_thread_offline();
5146 rcu_unregister_thread();