2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/defaults.h>
45 #include <common/kernel-consumer/kernel-consumer.h>
46 #include <common/futex.h>
47 #include <common/relayd/relayd.h>
48 #include <common/utils.h>
49 #include <common/daemonize.h>
50 #include <common/config/config.h>
52 #include "lttng-sessiond.h"
53 #include "buffer-registry.h"
60 #include "kernel-consumer.h"
64 #include "ust-consumer.h"
67 #include "health-sessiond.h"
68 #include "testpoint.h"
69 #include "ust-thread.h"
70 #include "jul-thread.h"
72 #define CONSUMERD_FILE "lttng-consumerd"
75 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
76 static int tracing_group_name_override
;
77 static char *opt_pidfile
;
78 static int opt_sig_parent
;
79 static int opt_verbose_consumer
;
80 static int opt_daemon
, opt_background
;
81 static int opt_no_kernel
;
82 static int is_root
; /* Set to 1 if the daemon is running as root */
83 static pid_t ppid
; /* Parent PID for --sig-parent option */
84 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
87 /* Set to 1 when a SIGUSR1 signal is received. */
88 static int recv_child_signal
;
91 * Consumer daemon specific control data. Every value not initialized here is
92 * set to 0 by the static definition.
94 static struct consumer_data kconsumer_data
= {
95 .type
= LTTNG_CONSUMER_KERNEL
,
96 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
97 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
100 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
101 .lock
= PTHREAD_MUTEX_INITIALIZER
,
102 .cond
= PTHREAD_COND_INITIALIZER
,
103 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
105 static struct consumer_data ustconsumer64_data
= {
106 .type
= LTTNG_CONSUMER64_UST
,
107 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
108 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
111 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
112 .lock
= PTHREAD_MUTEX_INITIALIZER
,
113 .cond
= PTHREAD_COND_INITIALIZER
,
114 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
116 static struct consumer_data ustconsumer32_data
= {
117 .type
= LTTNG_CONSUMER32_UST
,
118 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
119 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
122 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
123 .lock
= PTHREAD_MUTEX_INITIALIZER
,
124 .cond
= PTHREAD_COND_INITIALIZER
,
125 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
128 /* Command line options */
129 static const struct option long_options
[] = {
130 { "client-sock", 1, 0, 'c' },
131 { "apps-sock", 1, 0, 'a' },
132 { "kconsumerd-cmd-sock", 1, 0, 'C' },
133 { "kconsumerd-err-sock", 1, 0, 'E' },
134 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
135 { "ustconsumerd32-err-sock", 1, 0, 'H' },
136 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
137 { "ustconsumerd64-err-sock", 1, 0, 'F' },
138 { "consumerd32-path", 1, 0, 'u' },
139 { "consumerd32-libdir", 1, 0, 'U' },
140 { "consumerd64-path", 1, 0, 't' },
141 { "consumerd64-libdir", 1, 0, 'T' },
142 { "daemonize", 0, 0, 'd' },
143 { "background", 0, 0, 'b' },
144 { "sig-parent", 0, 0, 'S' },
145 { "help", 0, 0, 'h' },
146 { "group", 1, 0, 'g' },
147 { "version", 0, 0, 'V' },
148 { "quiet", 0, 0, 'q' },
149 { "verbose", 0, 0, 'v' },
150 { "verbose-consumer", 0, 0, 'Z' },
151 { "no-kernel", 0, 0, 'N' },
152 { "pidfile", 1, 0, 'p' },
153 { "jul-tcp-port", 1, 0, 'J' },
154 { "config", 1, 0, 'f' },
158 /* Command line options to ignore from configuration file */
159 static const char *config_ignore_options
[] = { "help", "version", "config" };
161 /* Shared between threads */
162 static int dispatch_thread_exit
;
164 /* Global application Unix socket path */
165 static char apps_unix_sock_path
[PATH_MAX
];
166 /* Global client Unix socket path */
167 static char client_unix_sock_path
[PATH_MAX
];
168 /* global wait shm path for UST */
169 static char wait_shm_path
[PATH_MAX
];
170 /* Global health check unix path */
171 static char health_unix_sock_path
[PATH_MAX
];
173 /* Sockets and FDs */
174 static int client_sock
= -1;
175 static int apps_sock
= -1;
176 int kernel_tracer_fd
= -1;
177 static int kernel_poll_pipe
[2] = { -1, -1 };
180 * Quit pipe for all threads. This permits a single cancellation point
181 * for all threads when receiving an event on the pipe.
183 static int thread_quit_pipe
[2] = { -1, -1 };
186 * This pipe is used to inform the thread managing application communication
187 * that a command is queued and ready to be processed.
189 static int apps_cmd_pipe
[2] = { -1, -1 };
191 int apps_cmd_notify_pipe
[2] = { -1, -1 };
193 /* Pthread, Mutexes and Semaphores */
194 static pthread_t apps_thread
;
195 static pthread_t apps_notify_thread
;
196 static pthread_t reg_apps_thread
;
197 static pthread_t client_thread
;
198 static pthread_t kernel_thread
;
199 static pthread_t dispatch_thread
;
200 static pthread_t health_thread
;
201 static pthread_t ht_cleanup_thread
;
202 static pthread_t jul_reg_thread
;
205 * UST registration command queue. This queue is tied with a futex and uses a N
206 * wakers / 1 waiter implemented and detailed in futex.c/.h
208 * The thread_manage_apps and thread_dispatch_ust_registration interact with
209 * this queue and the wait/wake scheme.
211 static struct ust_cmd_queue ust_cmd_queue
;
214 * Pointer initialized before thread creation.
216 * This points to the tracing session list containing the session count and a
217 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
218 * MUST NOT be taken if you call a public function in session.c.
220 * The lock is nested inside the structure: session_list_ptr->lock. Please use
221 * session_lock_list and session_unlock_list for lock acquisition.
223 static struct ltt_session_list
*session_list_ptr
;
225 int ust_consumerd64_fd
= -1;
226 int ust_consumerd32_fd
= -1;
228 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
229 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
230 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
231 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
232 static int consumerd32_bin_override
;
233 static int consumerd64_bin_override
;
234 static int consumerd32_libdir_override
;
235 static int consumerd64_libdir_override
;
237 static const char *module_proc_lttng
= "/proc/lttng";
240 * Consumer daemon state which is changed when spawning it, killing it or in
241 * case of a fatal error.
243 enum consumerd_state
{
244 CONSUMER_STARTED
= 1,
245 CONSUMER_STOPPED
= 2,
250 * This consumer daemon state is used to validate if a client command will be
251 * able to reach the consumer. If not, the client is informed. For instance,
252 * doing a "lttng start" when the consumer state is set to ERROR will return an
253 * error to the client.
255 * The following example shows a possible race condition of this scheme:
257 * consumer thread error happens
259 * client cmd checks state -> still OK
260 * consumer thread exit, sets error
261 * client cmd try to talk to consumer
264 * However, since the consumer is a different daemon, we have no way of making
265 * sure the command will reach it safely even with this state flag. This is why
266 * we consider that up to the state validation during command processing, the
267 * command is safe. After that, we can not guarantee the correctness of the
268 * client request vis-a-vis the consumer.
270 static enum consumerd_state ust_consumerd_state
;
271 static enum consumerd_state kernel_consumerd_state
;
274 * Socket timeout for receiving and sending in seconds.
276 static int app_socket_timeout
;
278 /* Set in main() with the current page size. */
281 /* Application health monitoring */
282 struct health_app
*health_sessiond
;
284 /* JUL TCP port for registration. Used by the JUL thread. */
285 unsigned int jul_tcp_port
= DEFAULT_JUL_TCP_PORT
;
287 const char * const config_section_name
= "sessiond";
290 * Whether sessiond is ready for commands/health check requests.
291 * NR_LTTNG_SESSIOND_READY must match the number of calls to
292 * lttng_sessiond_notify_ready().
294 #define NR_LTTNG_SESSIOND_READY 2
295 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
297 /* Notify parents that we are ready for cmd and health check */
299 void lttng_sessiond_notify_ready(void)
301 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
303 * Notify parent pid that we are ready to accept command
304 * for client side. This ppid is the one from the
305 * external process that spawned us.
307 if (opt_sig_parent
) {
312 * Notify the parent of the fork() process that we are
315 if (opt_daemon
|| opt_background
) {
316 kill(child_ppid
, SIGUSR1
);
322 void setup_consumerd_path(void)
324 const char *bin
, *libdir
;
327 * Allow INSTALL_BIN_PATH to be used as a target path for the
328 * native architecture size consumer if CONFIG_CONSUMER*_PATH
329 * has not been defined.
331 #if (CAA_BITS_PER_LONG == 32)
332 if (!consumerd32_bin
[0]) {
333 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
335 if (!consumerd32_libdir
[0]) {
336 consumerd32_libdir
= INSTALL_LIB_PATH
;
338 #elif (CAA_BITS_PER_LONG == 64)
339 if (!consumerd64_bin
[0]) {
340 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
342 if (!consumerd64_libdir
[0]) {
343 consumerd64_libdir
= INSTALL_LIB_PATH
;
346 #error "Unknown bitness"
350 * runtime env. var. overrides the build default.
352 bin
= getenv("LTTNG_CONSUMERD32_BIN");
354 consumerd32_bin
= bin
;
356 bin
= getenv("LTTNG_CONSUMERD64_BIN");
358 consumerd64_bin
= bin
;
360 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
362 consumerd32_libdir
= libdir
;
364 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
366 consumerd64_libdir
= libdir
;
371 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
373 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
379 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
385 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
397 * Check if the thread quit pipe was triggered.
399 * Return 1 if it was triggered else 0;
401 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
403 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
411 * Init thread quit pipe.
413 * Return -1 on error or 0 if all pipes are created.
415 static int init_thread_quit_pipe(void)
419 ret
= pipe(thread_quit_pipe
);
421 PERROR("thread quit pipe");
425 for (i
= 0; i
< 2; i
++) {
426 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
438 * Stop all threads by closing the thread quit pipe.
440 static void stop_threads(void)
444 /* Stopping all threads */
445 DBG("Terminating all threads");
446 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
448 ERR("write error on thread quit pipe");
451 /* Dispatch thread */
452 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
453 futex_nto1_wake(&ust_cmd_queue
.futex
);
457 * Close every consumer sockets.
459 static void close_consumer_sockets(void)
463 if (kconsumer_data
.err_sock
>= 0) {
464 ret
= close(kconsumer_data
.err_sock
);
466 PERROR("kernel consumer err_sock close");
469 if (ustconsumer32_data
.err_sock
>= 0) {
470 ret
= close(ustconsumer32_data
.err_sock
);
472 PERROR("UST consumerd32 err_sock close");
475 if (ustconsumer64_data
.err_sock
>= 0) {
476 ret
= close(ustconsumer64_data
.err_sock
);
478 PERROR("UST consumerd64 err_sock close");
481 if (kconsumer_data
.cmd_sock
>= 0) {
482 ret
= close(kconsumer_data
.cmd_sock
);
484 PERROR("kernel consumer cmd_sock close");
487 if (ustconsumer32_data
.cmd_sock
>= 0) {
488 ret
= close(ustconsumer32_data
.cmd_sock
);
490 PERROR("UST consumerd32 cmd_sock close");
493 if (ustconsumer64_data
.cmd_sock
>= 0) {
494 ret
= close(ustconsumer64_data
.cmd_sock
);
496 PERROR("UST consumerd64 cmd_sock close");
504 static void cleanup(void)
507 struct ltt_session
*sess
, *stmp
;
513 * Close the thread quit pipe. It has already done its job,
514 * since we are now called.
516 utils_close_pipe(thread_quit_pipe
);
519 * If opt_pidfile is undefined, the default file will be wiped when
520 * removing the rundir.
523 ret
= remove(opt_pidfile
);
525 PERROR("remove pidfile %s", opt_pidfile
);
529 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
532 snprintf(path
, PATH_MAX
,
534 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
535 DBG("Removing %s", path
);
538 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
539 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
);
540 DBG("Removing %s", path
);
544 snprintf(path
, PATH_MAX
,
545 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
547 DBG("Removing %s", path
);
550 snprintf(path
, PATH_MAX
,
551 DEFAULT_KCONSUMERD_PATH
,
553 DBG("Removing directory %s", path
);
556 /* ust consumerd 32 */
557 snprintf(path
, PATH_MAX
,
558 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
560 DBG("Removing %s", path
);
563 snprintf(path
, PATH_MAX
,
564 DEFAULT_USTCONSUMERD32_PATH
,
566 DBG("Removing directory %s", path
);
569 /* ust consumerd 64 */
570 snprintf(path
, PATH_MAX
,
571 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
573 DBG("Removing %s", path
);
576 snprintf(path
, PATH_MAX
,
577 DEFAULT_USTCONSUMERD64_PATH
,
579 DBG("Removing directory %s", path
);
583 * We do NOT rmdir rundir because there are other processes
584 * using it, for instance lttng-relayd, which can start in
585 * parallel with this teardown.
590 DBG("Cleaning up all sessions");
592 /* Destroy session list mutex */
593 if (session_list_ptr
!= NULL
) {
594 pthread_mutex_destroy(&session_list_ptr
->lock
);
596 /* Cleanup ALL session */
597 cds_list_for_each_entry_safe(sess
, stmp
,
598 &session_list_ptr
->head
, list
) {
599 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
603 DBG("Closing all UST sockets");
604 ust_app_clean_list();
605 buffer_reg_destroy_registries();
607 if (is_root
&& !opt_no_kernel
) {
608 DBG2("Closing kernel fd");
609 if (kernel_tracer_fd
>= 0) {
610 ret
= close(kernel_tracer_fd
);
615 DBG("Unloading kernel modules");
616 modprobe_remove_lttng_all();
619 close_consumer_sockets();
622 * If the override option is set, the pointer points to a *non* const thus
623 * freeing it even though the variable type is set to const.
625 if (tracing_group_name_override
) {
626 free((void *) tracing_group_name
);
628 if (consumerd32_bin_override
) {
629 free((void *) consumerd32_bin
);
631 if (consumerd64_bin_override
) {
632 free((void *) consumerd64_bin
);
634 if (consumerd32_libdir_override
) {
635 free((void *) consumerd32_libdir
);
637 if (consumerd64_libdir_override
) {
638 free((void *) consumerd64_libdir
);
646 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
647 "Matthew, BEET driven development works!%c[%dm",
648 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
653 * Send data on a unix socket using the liblttsessiondcomm API.
655 * Return lttcomm error code.
657 static int send_unix_sock(int sock
, void *buf
, size_t len
)
659 /* Check valid length */
664 return lttcomm_send_unix_sock(sock
, buf
, len
);
668 * Free memory of a command context structure.
670 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
672 DBG("Clean command context structure");
674 if ((*cmd_ctx
)->llm
) {
675 free((*cmd_ctx
)->llm
);
677 if ((*cmd_ctx
)->lsm
) {
678 free((*cmd_ctx
)->lsm
);
686 * Notify UST applications using the shm mmap futex.
688 static int notify_ust_apps(int active
)
692 DBG("Notifying applications of session daemon state: %d", active
);
694 /* See shm.c for this call implying mmap, shm and futex calls */
695 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
696 if (wait_shm_mmap
== NULL
) {
700 /* Wake waiting process */
701 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
703 /* Apps notified successfully */
711 * Setup the outgoing data buffer for the response (llm) by allocating the
712 * right amount of memory and copying the original information from the lsm
715 * Return total size of the buffer pointed by buf.
717 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
723 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
724 if (cmd_ctx
->llm
== NULL
) {
730 /* Copy common data */
731 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
732 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
734 cmd_ctx
->llm
->data_size
= size
;
735 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
744 * Update the kernel poll set of all channel fd available over all tracing
745 * session. Add the wakeup pipe at the end of the set.
747 static int update_kernel_poll(struct lttng_poll_event
*events
)
750 struct ltt_session
*session
;
751 struct ltt_kernel_channel
*channel
;
753 DBG("Updating kernel poll set");
756 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
757 session_lock(session
);
758 if (session
->kernel_session
== NULL
) {
759 session_unlock(session
);
763 cds_list_for_each_entry(channel
,
764 &session
->kernel_session
->channel_list
.head
, list
) {
765 /* Add channel fd to the kernel poll set */
766 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
768 session_unlock(session
);
771 DBG("Channel fd %d added to kernel set", channel
->fd
);
773 session_unlock(session
);
775 session_unlock_list();
780 session_unlock_list();
785 * Find the channel fd from 'fd' over all tracing session. When found, check
786 * for new channel stream and send those stream fds to the kernel consumer.
788 * Useful for CPU hotplug feature.
790 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
793 struct ltt_session
*session
;
794 struct ltt_kernel_session
*ksess
;
795 struct ltt_kernel_channel
*channel
;
797 DBG("Updating kernel streams for channel fd %d", fd
);
800 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
801 session_lock(session
);
802 if (session
->kernel_session
== NULL
) {
803 session_unlock(session
);
806 ksess
= session
->kernel_session
;
808 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
809 if (channel
->fd
== fd
) {
810 DBG("Channel found, updating kernel streams");
811 ret
= kernel_open_channel_stream(channel
);
815 /* Update the stream global counter */
816 ksess
->stream_count_global
+= ret
;
819 * Have we already sent fds to the consumer? If yes, it means
820 * that tracing is started so it is safe to send our updated
823 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
824 struct lttng_ht_iter iter
;
825 struct consumer_socket
*socket
;
828 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
829 &iter
.iter
, socket
, node
.node
) {
830 pthread_mutex_lock(socket
->lock
);
831 ret
= kernel_consumer_send_channel_stream(socket
,
833 session
->output_traces
? 1 : 0);
834 pthread_mutex_unlock(socket
->lock
);
845 session_unlock(session
);
847 session_unlock_list();
851 session_unlock(session
);
852 session_unlock_list();
857 * For each tracing session, update newly registered apps. The session list
858 * lock MUST be acquired before calling this.
860 static void update_ust_app(int app_sock
)
862 struct ltt_session
*sess
, *stmp
;
864 /* Consumer is in an ERROR state. Stop any application update. */
865 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
866 /* Stop the update process since the consumer is dead. */
870 /* For all tracing session(s) */
871 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
873 if (sess
->ust_session
) {
874 ust_app_global_update(sess
->ust_session
, app_sock
);
876 session_unlock(sess
);
881 * This thread manage event coming from the kernel.
883 * Features supported in this thread:
886 static void *thread_manage_kernel(void *data
)
888 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
889 uint32_t revents
, nb_fd
;
891 struct lttng_poll_event events
;
893 DBG("[thread] Thread manage kernel started");
895 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
898 * This first step of the while is to clean this structure which could free
899 * non NULL pointers so initialize it before the loop.
901 lttng_poll_init(&events
);
903 if (testpoint(sessiond_thread_manage_kernel
)) {
904 goto error_testpoint
;
907 health_code_update();
909 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
910 goto error_testpoint
;
914 health_code_update();
916 if (update_poll_flag
== 1) {
917 /* Clean events object. We are about to populate it again. */
918 lttng_poll_clean(&events
);
920 ret
= sessiond_set_thread_pollset(&events
, 2);
922 goto error_poll_create
;
925 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
930 /* This will add the available kernel channel if any. */
931 ret
= update_kernel_poll(&events
);
935 update_poll_flag
= 0;
938 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
940 /* Poll infinite value of time */
943 ret
= lttng_poll_wait(&events
, -1);
947 * Restart interrupted system call.
949 if (errno
== EINTR
) {
953 } else if (ret
== 0) {
954 /* Should not happen since timeout is infinite */
955 ERR("Return value of poll is 0 with an infinite timeout.\n"
956 "This should not have happened! Continuing...");
962 for (i
= 0; i
< nb_fd
; i
++) {
963 /* Fetch once the poll data */
964 revents
= LTTNG_POLL_GETEV(&events
, i
);
965 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
967 health_code_update();
969 /* Thread quit pipe has been closed. Killing thread. */
970 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
976 /* Check for data on kernel pipe */
977 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
978 (void) lttng_read(kernel_poll_pipe
[0],
981 * Ret value is useless here, if this pipe gets any actions an
982 * update is required anyway.
984 update_poll_flag
= 1;
988 * New CPU detected by the kernel. Adding kernel stream to
989 * kernel session and updating the kernel consumer
991 if (revents
& LPOLLIN
) {
992 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
998 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
999 * and unregister kernel stream at this point.
1008 lttng_poll_clean(&events
);
1011 utils_close_pipe(kernel_poll_pipe
);
1012 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1015 ERR("Health error occurred in %s", __func__
);
1016 WARN("Kernel thread died unexpectedly. "
1017 "Kernel tracing can continue but CPU hotplug is disabled.");
1019 health_unregister(health_sessiond
);
1020 DBG("Kernel thread dying");
1025 * Signal pthread condition of the consumer data that the thread.
1027 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1029 pthread_mutex_lock(&data
->cond_mutex
);
1032 * The state is set before signaling. It can be any value, it's the waiter
1033 * job to correctly interpret this condition variable associated to the
1034 * consumer pthread_cond.
1036 * A value of 0 means that the corresponding thread of the consumer data
1037 * was not started. 1 indicates that the thread has started and is ready
1038 * for action. A negative value means that there was an error during the
1041 data
->consumer_thread_is_ready
= state
;
1042 (void) pthread_cond_signal(&data
->cond
);
1044 pthread_mutex_unlock(&data
->cond_mutex
);
1048 * This thread manage the consumer error sent back to the session daemon.
1050 static void *thread_manage_consumer(void *data
)
1052 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1053 uint32_t revents
, nb_fd
;
1054 enum lttcomm_return_code code
;
1055 struct lttng_poll_event events
;
1056 struct consumer_data
*consumer_data
= data
;
1058 DBG("[thread] Manage consumer started");
1060 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1062 health_code_update();
1065 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1066 * metadata_sock. Nothing more will be added to this poll set.
1068 ret
= sessiond_set_thread_pollset(&events
, 3);
1074 * The error socket here is already in a listening state which was done
1075 * just before spawning this thread to avoid a race between the consumer
1076 * daemon exec trying to connect and the listen() call.
1078 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1083 health_code_update();
1085 /* Infinite blocking call, waiting for transmission */
1087 health_poll_entry();
1089 if (testpoint(sessiond_thread_manage_consumer
)) {
1093 ret
= lttng_poll_wait(&events
, -1);
1097 * Restart interrupted system call.
1099 if (errno
== EINTR
) {
1107 for (i
= 0; i
< nb_fd
; i
++) {
1108 /* Fetch once the poll data */
1109 revents
= LTTNG_POLL_GETEV(&events
, i
);
1110 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1112 health_code_update();
1114 /* Thread quit pipe has been closed. Killing thread. */
1115 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1121 /* Event on the registration socket */
1122 if (pollfd
== consumer_data
->err_sock
) {
1123 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1124 ERR("consumer err socket poll error");
1130 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1136 * Set the CLOEXEC flag. Return code is useless because either way, the
1139 (void) utils_set_fd_cloexec(sock
);
1141 health_code_update();
1143 DBG2("Receiving code from consumer err_sock");
1145 /* Getting status code from kconsumerd */
1146 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1147 sizeof(enum lttcomm_return_code
));
1152 health_code_update();
1154 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1155 /* Connect both socket, command and metadata. */
1156 consumer_data
->cmd_sock
=
1157 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1158 consumer_data
->metadata_fd
=
1159 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1160 if (consumer_data
->cmd_sock
< 0
1161 || consumer_data
->metadata_fd
< 0) {
1162 PERROR("consumer connect cmd socket");
1163 /* On error, signal condition and quit. */
1164 signal_consumer_condition(consumer_data
, -1);
1167 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1168 /* Create metadata socket lock. */
1169 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1170 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1171 PERROR("zmalloc pthread mutex");
1175 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1177 signal_consumer_condition(consumer_data
, 1);
1178 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1179 DBG("Consumer metadata socket ready (fd: %d)",
1180 consumer_data
->metadata_fd
);
1182 ERR("consumer error when waiting for SOCK_READY : %s",
1183 lttcomm_get_readable_code(-code
));
1187 /* Remove the consumerd error sock since we've established a connexion */
1188 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1193 /* Add new accepted error socket. */
1194 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1199 /* Add metadata socket that is successfully connected. */
1200 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1201 LPOLLIN
| LPOLLRDHUP
);
1206 health_code_update();
1208 /* Infinite blocking call, waiting for transmission */
1211 health_poll_entry();
1212 ret
= lttng_poll_wait(&events
, -1);
1216 * Restart interrupted system call.
1218 if (errno
== EINTR
) {
1226 for (i
= 0; i
< nb_fd
; i
++) {
1227 /* Fetch once the poll data */
1228 revents
= LTTNG_POLL_GETEV(&events
, i
);
1229 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1231 health_code_update();
1233 /* Thread quit pipe has been closed. Killing thread. */
1234 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1240 if (pollfd
== sock
) {
1241 /* Event on the consumerd socket */
1242 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1243 ERR("consumer err socket second poll error");
1246 health_code_update();
1247 /* Wait for any kconsumerd error */
1248 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1249 sizeof(enum lttcomm_return_code
));
1251 ERR("consumer closed the command socket");
1255 ERR("consumer return code : %s",
1256 lttcomm_get_readable_code(-code
));
1259 } else if (pollfd
== consumer_data
->metadata_fd
) {
1260 /* UST metadata requests */
1261 ret
= ust_consumer_metadata_request(
1262 &consumer_data
->metadata_sock
);
1264 ERR("Handling metadata request");
1269 ERR("Unknown pollfd");
1273 health_code_update();
1279 * We lock here because we are about to close the sockets and some other
1280 * thread might be using them so get exclusive access which will abort all
1281 * other consumer command by other threads.
1283 pthread_mutex_lock(&consumer_data
->lock
);
1285 /* Immediately set the consumerd state to stopped */
1286 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1287 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1288 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1289 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1290 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1292 /* Code flow error... */
1296 if (consumer_data
->err_sock
>= 0) {
1297 ret
= close(consumer_data
->err_sock
);
1301 consumer_data
->err_sock
= -1;
1303 if (consumer_data
->cmd_sock
>= 0) {
1304 ret
= close(consumer_data
->cmd_sock
);
1308 consumer_data
->cmd_sock
= -1;
1310 if (*consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1311 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1324 unlink(consumer_data
->err_unix_sock_path
);
1325 unlink(consumer_data
->cmd_unix_sock_path
);
1326 consumer_data
->pid
= 0;
1327 pthread_mutex_unlock(&consumer_data
->lock
);
1329 /* Cleanup metadata socket mutex. */
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();
1411 /* Thread quit pipe has been closed. Killing thread. */
1412 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1418 /* Inspect the apps cmd pipe */
1419 if (pollfd
== apps_cmd_pipe
[0]) {
1420 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1421 ERR("Apps command pipe error");
1423 } else if (revents
& LPOLLIN
) {
1427 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1428 if (size_ret
< sizeof(sock
)) {
1429 PERROR("read apps cmd pipe");
1433 health_code_update();
1436 * We only monitor the error events of the socket. This
1437 * thread does not handle any incoming data from UST
1440 ret
= lttng_poll_add(&events
, sock
,
1441 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1446 DBG("Apps with sock %d added to poll set", sock
);
1450 * At this point, we know that a registered application made
1451 * the event at poll_wait.
1453 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1454 /* Removing from the poll set */
1455 ret
= lttng_poll_del(&events
, pollfd
);
1460 /* Socket closed on remote end. */
1461 ust_app_unregister(pollfd
);
1465 health_code_update();
1471 lttng_poll_clean(&events
);
1474 utils_close_pipe(apps_cmd_pipe
);
1475 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1478 * We don't clean the UST app hash table here since already registered
1479 * applications can still be controlled so let them be until the session
1480 * daemon dies or the applications stop.
1485 ERR("Health error occurred in %s", __func__
);
1487 health_unregister(health_sessiond
);
1488 DBG("Application communication apps thread cleanup complete");
1489 rcu_thread_offline();
1490 rcu_unregister_thread();
1495 * Send a socket to a thread This is called from the dispatch UST registration
1496 * thread once all sockets are set for the application.
1498 * The sock value can be invalid, we don't really care, the thread will handle
1499 * it and make the necessary cleanup if so.
1501 * On success, return 0 else a negative value being the errno message of the
1504 static int send_socket_to_thread(int fd
, int sock
)
1509 * It's possible that the FD is set as invalid with -1 concurrently just
1510 * before calling this function being a shutdown state of the thread.
1517 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1518 if (ret
< sizeof(sock
)) {
1519 PERROR("write apps pipe %d", fd
);
1526 /* All good. Don't send back the write positive ret value. */
1533 * Sanitize the wait queue of the dispatch registration thread meaning removing
1534 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1535 * notify socket is never received.
1537 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1539 int ret
, nb_fd
= 0, i
;
1540 unsigned int fd_added
= 0;
1541 struct lttng_poll_event events
;
1542 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1546 lttng_poll_init(&events
);
1548 /* Just skip everything for an empty queue. */
1549 if (!wait_queue
->count
) {
1553 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1558 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1559 &wait_queue
->head
, head
) {
1560 assert(wait_node
->app
);
1561 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1562 LPOLLHUP
| LPOLLERR
);
1575 * Poll but don't block so we can quickly identify the faulty events and
1576 * clean them afterwards from the wait queue.
1578 ret
= lttng_poll_wait(&events
, 0);
1584 for (i
= 0; i
< nb_fd
; i
++) {
1585 /* Get faulty FD. */
1586 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1587 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1589 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1590 &wait_queue
->head
, head
) {
1591 if (pollfd
== wait_node
->app
->sock
&&
1592 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1593 cds_list_del(&wait_node
->head
);
1594 wait_queue
->count
--;
1595 ust_app_destroy(wait_node
->app
);
1603 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1607 lttng_poll_clean(&events
);
1611 lttng_poll_clean(&events
);
1613 ERR("Unable to sanitize wait queue");
1618 * Dispatch request from the registration threads to the application
1619 * communication thread.
1621 static void *thread_dispatch_ust_registration(void *data
)
1624 struct cds_wfq_node
*node
;
1625 struct ust_command
*ust_cmd
= NULL
;
1626 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1627 struct ust_reg_wait_queue wait_queue
= {
1631 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1633 health_code_update();
1635 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1637 DBG("[thread] Dispatch UST command started");
1639 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1640 health_code_update();
1642 /* Atomically prepare the queue futex */
1643 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1646 struct ust_app
*app
= NULL
;
1650 * Make sure we don't have node(s) that have hung up before receiving
1651 * the notify socket. This is to clean the list in order to avoid
1652 * memory leaks from notify socket that are never seen.
1654 sanitize_wait_queue(&wait_queue
);
1656 health_code_update();
1657 /* Dequeue command for registration */
1658 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1660 DBG("Woken up but nothing in the UST command queue");
1661 /* Continue thread execution */
1665 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1667 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1668 " gid:%d sock:%d name:%s (version %d.%d)",
1669 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1670 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1671 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1672 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1674 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1675 wait_node
= zmalloc(sizeof(*wait_node
));
1677 PERROR("zmalloc wait_node dispatch");
1678 ret
= close(ust_cmd
->sock
);
1680 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1682 lttng_fd_put(LTTNG_FD_APPS
, 1);
1686 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1688 /* Create application object if socket is CMD. */
1689 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1691 if (!wait_node
->app
) {
1692 ret
= close(ust_cmd
->sock
);
1694 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1696 lttng_fd_put(LTTNG_FD_APPS
, 1);
1702 * Add application to the wait queue so we can set the notify
1703 * socket before putting this object in the global ht.
1705 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1710 * We have to continue here since we don't have the notify
1711 * socket and the application MUST be added to the hash table
1712 * only at that moment.
1717 * Look for the application in the local wait queue and set the
1718 * notify socket if found.
1720 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1721 &wait_queue
.head
, head
) {
1722 health_code_update();
1723 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1724 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1725 cds_list_del(&wait_node
->head
);
1727 app
= wait_node
->app
;
1729 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1735 * With no application at this stage the received socket is
1736 * basically useless so close it before we free the cmd data
1737 * structure for good.
1740 ret
= close(ust_cmd
->sock
);
1742 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1744 lttng_fd_put(LTTNG_FD_APPS
, 1);
1751 * @session_lock_list
1753 * Lock the global session list so from the register up to the
1754 * registration done message, no thread can see the application
1755 * and change its state.
1757 session_lock_list();
1761 * Add application to the global hash table. This needs to be
1762 * done before the update to the UST registry can locate the
1767 /* Set app version. This call will print an error if needed. */
1768 (void) ust_app_version(app
);
1770 /* Send notify socket through the notify pipe. */
1771 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1775 session_unlock_list();
1777 * No notify thread, stop the UST tracing. However, this is
1778 * not an internal error of the this thread thus setting
1779 * the health error code to a normal exit.
1786 * Update newly registered application with the tracing
1787 * registry info already enabled information.
1789 update_ust_app(app
->sock
);
1792 * Don't care about return value. Let the manage apps threads
1793 * handle app unregistration upon socket close.
1795 (void) ust_app_register_done(app
->sock
);
1798 * Even if the application socket has been closed, send the app
1799 * to the thread and unregistration will take place at that
1802 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1805 session_unlock_list();
1807 * No apps. thread, stop the UST tracing. However, this is
1808 * not an internal error of the this thread thus setting
1809 * the health error code to a normal exit.
1816 session_unlock_list();
1818 } while (node
!= NULL
);
1820 health_poll_entry();
1821 /* Futex wait on queue. Blocking call on futex() */
1822 futex_nto1_wait(&ust_cmd_queue
.futex
);
1825 /* Normal exit, no error */
1829 /* Clean up wait queue. */
1830 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1831 &wait_queue
.head
, head
) {
1832 cds_list_del(&wait_node
->head
);
1837 DBG("Dispatch thread dying");
1840 ERR("Health error occurred in %s", __func__
);
1842 health_unregister(health_sessiond
);
1847 * This thread manage application registration.
1849 static void *thread_registration_apps(void *data
)
1851 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1852 uint32_t revents
, nb_fd
;
1853 struct lttng_poll_event events
;
1855 * Get allocated in this thread, enqueued to a global queue, dequeued and
1856 * freed in the manage apps thread.
1858 struct ust_command
*ust_cmd
= NULL
;
1860 DBG("[thread] Manage application registration started");
1862 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1864 if (testpoint(sessiond_thread_registration_apps
)) {
1865 goto error_testpoint
;
1868 ret
= lttcomm_listen_unix_sock(apps_sock
);
1874 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1875 * more will be added to this poll set.
1877 ret
= sessiond_set_thread_pollset(&events
, 2);
1879 goto error_create_poll
;
1882 /* Add the application registration socket */
1883 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1885 goto error_poll_add
;
1888 /* Notify all applications to register */
1889 ret
= notify_ust_apps(1);
1891 ERR("Failed to notify applications or create the wait shared memory.\n"
1892 "Execution continues but there might be problem for already\n"
1893 "running applications that wishes to register.");
1897 DBG("Accepting application registration");
1899 /* Inifinite blocking call, waiting for transmission */
1901 health_poll_entry();
1902 ret
= lttng_poll_wait(&events
, -1);
1906 * Restart interrupted system call.
1908 if (errno
== EINTR
) {
1916 for (i
= 0; i
< nb_fd
; i
++) {
1917 health_code_update();
1919 /* Fetch once the poll data */
1920 revents
= LTTNG_POLL_GETEV(&events
, i
);
1921 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1923 /* Thread quit pipe has been closed. Killing thread. */
1924 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1930 /* Event on the registration socket */
1931 if (pollfd
== apps_sock
) {
1932 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1933 ERR("Register apps socket poll error");
1935 } else if (revents
& LPOLLIN
) {
1936 sock
= lttcomm_accept_unix_sock(apps_sock
);
1942 * Set socket timeout for both receiving and ending.
1943 * app_socket_timeout is in seconds, whereas
1944 * lttcomm_setsockopt_rcv_timeout and
1945 * lttcomm_setsockopt_snd_timeout expect msec as
1948 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1949 app_socket_timeout
* 1000);
1950 (void) lttcomm_setsockopt_snd_timeout(sock
,
1951 app_socket_timeout
* 1000);
1954 * Set the CLOEXEC flag. Return code is useless because
1955 * either way, the show must go on.
1957 (void) utils_set_fd_cloexec(sock
);
1959 /* Create UST registration command for enqueuing */
1960 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1961 if (ust_cmd
== NULL
) {
1962 PERROR("ust command zmalloc");
1967 * Using message-based transmissions to ensure we don't
1968 * have to deal with partially received messages.
1970 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1972 ERR("Exhausted file descriptors allowed for applications.");
1982 health_code_update();
1983 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
1986 /* Close socket of the application. */
1991 lttng_fd_put(LTTNG_FD_APPS
, 1);
1995 health_code_update();
1997 ust_cmd
->sock
= sock
;
2000 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2001 " gid:%d sock:%d name:%s (version %d.%d)",
2002 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2003 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2004 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2005 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2008 * Lock free enqueue the registration request. The red pill
2009 * has been taken! This apps will be part of the *system*.
2011 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
2014 * Wake the registration queue futex. Implicit memory
2015 * barrier with the exchange in cds_wfq_enqueue.
2017 futex_nto1_wake(&ust_cmd_queue
.futex
);
2027 ERR("Health error occurred in %s", __func__
);
2030 /* Notify that the registration thread is gone */
2033 if (apps_sock
>= 0) {
2034 ret
= close(apps_sock
);
2044 lttng_fd_put(LTTNG_FD_APPS
, 1);
2046 unlink(apps_unix_sock_path
);
2049 lttng_poll_clean(&events
);
2053 DBG("UST Registration thread cleanup complete");
2054 health_unregister(health_sessiond
);
2060 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2061 * exec or it will fails.
2063 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2066 struct timespec timeout
;
2068 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2069 consumer_data
->consumer_thread_is_ready
= 0;
2071 /* Setup pthread condition */
2072 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2075 PERROR("pthread_condattr_init consumer data");
2080 * Set the monotonic clock in order to make sure we DO NOT jump in time
2081 * between the clock_gettime() call and the timedwait call. See bug #324
2082 * for a more details and how we noticed it.
2084 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2087 PERROR("pthread_condattr_setclock consumer data");
2091 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2094 PERROR("pthread_cond_init consumer data");
2098 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2101 PERROR("pthread_create consumer");
2106 /* We are about to wait on a pthread condition */
2107 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2109 /* Get time for sem_timedwait absolute timeout */
2110 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2112 * Set the timeout for the condition timed wait even if the clock gettime
2113 * call fails since we might loop on that call and we want to avoid to
2114 * increment the timeout too many times.
2116 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2119 * The following loop COULD be skipped in some conditions so this is why we
2120 * set ret to 0 in order to make sure at least one round of the loop is
2126 * Loop until the condition is reached or when a timeout is reached. Note
2127 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2128 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2129 * possible. This loop does not take any chances and works with both of
2132 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2133 if (clock_ret
< 0) {
2134 PERROR("clock_gettime spawn consumer");
2135 /* Infinite wait for the consumerd thread to be ready */
2136 ret
= pthread_cond_wait(&consumer_data
->cond
,
2137 &consumer_data
->cond_mutex
);
2139 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2140 &consumer_data
->cond_mutex
, &timeout
);
2144 /* Release the pthread condition */
2145 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2149 if (ret
== ETIMEDOUT
) {
2151 * Call has timed out so we kill the kconsumerd_thread and return
2154 ERR("Condition timed out. The consumer thread was never ready."
2156 ret
= pthread_cancel(consumer_data
->thread
);
2158 PERROR("pthread_cancel consumer thread");
2161 PERROR("pthread_cond_wait failed consumer thread");
2166 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2167 if (consumer_data
->pid
== 0) {
2168 ERR("Consumerd did not start");
2169 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2172 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2181 * Join consumer thread
2183 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2187 /* Consumer pid must be a real one. */
2188 if (consumer_data
->pid
> 0) {
2190 ret
= kill(consumer_data
->pid
, SIGTERM
);
2192 ERR("Error killing consumer daemon");
2195 return pthread_join(consumer_data
->thread
, &status
);
2202 * Fork and exec a consumer daemon (consumerd).
2204 * Return pid if successful else -1.
2206 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2210 const char *consumer_to_use
;
2211 const char *verbosity
;
2214 DBG("Spawning consumerd");
2221 if (opt_verbose_consumer
) {
2222 verbosity
= "--verbose";
2224 verbosity
= "--quiet";
2226 switch (consumer_data
->type
) {
2227 case LTTNG_CONSUMER_KERNEL
:
2229 * Find out which consumerd to execute. We will first try the
2230 * 64-bit path, then the sessiond's installation directory, and
2231 * fallback on the 32-bit one,
2233 DBG3("Looking for a kernel consumer at these locations:");
2234 DBG3(" 1) %s", consumerd64_bin
);
2235 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2236 DBG3(" 3) %s", consumerd32_bin
);
2237 if (stat(consumerd64_bin
, &st
) == 0) {
2238 DBG3("Found location #1");
2239 consumer_to_use
= consumerd64_bin
;
2240 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2241 DBG3("Found location #2");
2242 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2243 } else if (stat(consumerd32_bin
, &st
) == 0) {
2244 DBG3("Found location #3");
2245 consumer_to_use
= consumerd32_bin
;
2247 DBG("Could not find any valid consumerd executable");
2250 DBG("Using kernel consumer at: %s", consumer_to_use
);
2251 execl(consumer_to_use
,
2252 "lttng-consumerd", verbosity
, "-k",
2253 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2254 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2255 "--group", tracing_group_name
,
2258 case LTTNG_CONSUMER64_UST
:
2260 char *tmpnew
= NULL
;
2262 if (consumerd64_libdir
[0] != '\0') {
2266 tmp
= getenv("LD_LIBRARY_PATH");
2270 tmplen
= strlen("LD_LIBRARY_PATH=")
2271 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2272 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2277 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2278 strcat(tmpnew
, consumerd64_libdir
);
2279 if (tmp
[0] != '\0') {
2280 strcat(tmpnew
, ":");
2281 strcat(tmpnew
, tmp
);
2283 ret
= putenv(tmpnew
);
2290 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2291 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2292 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2293 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2294 "--group", tracing_group_name
,
2296 if (consumerd64_libdir
[0] != '\0') {
2304 case LTTNG_CONSUMER32_UST
:
2306 char *tmpnew
= NULL
;
2308 if (consumerd32_libdir
[0] != '\0') {
2312 tmp
= getenv("LD_LIBRARY_PATH");
2316 tmplen
= strlen("LD_LIBRARY_PATH=")
2317 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2318 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2323 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2324 strcat(tmpnew
, consumerd32_libdir
);
2325 if (tmp
[0] != '\0') {
2326 strcat(tmpnew
, ":");
2327 strcat(tmpnew
, tmp
);
2329 ret
= putenv(tmpnew
);
2336 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2337 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2338 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2339 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2340 "--group", tracing_group_name
,
2342 if (consumerd32_libdir
[0] != '\0') {
2351 PERROR("unknown consumer type");
2355 PERROR("kernel start consumer exec");
2358 } else if (pid
> 0) {
2361 PERROR("start consumer fork");
2369 * Spawn the consumerd daemon and session daemon thread.
2371 static int start_consumerd(struct consumer_data
*consumer_data
)
2376 * Set the listen() state on the socket since there is a possible race
2377 * between the exec() of the consumer daemon and this call if place in the
2378 * consumer thread. See bug #366 for more details.
2380 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2385 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2386 if (consumer_data
->pid
!= 0) {
2387 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2391 ret
= spawn_consumerd(consumer_data
);
2393 ERR("Spawning consumerd failed");
2394 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2398 /* Setting up the consumer_data pid */
2399 consumer_data
->pid
= ret
;
2400 DBG2("Consumer pid %d", consumer_data
->pid
);
2401 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2403 DBG2("Spawning consumer control thread");
2404 ret
= spawn_consumer_thread(consumer_data
);
2406 ERR("Fatal error spawning consumer control thread");
2414 /* Cleanup already created sockets on error. */
2415 if (consumer_data
->err_sock
>= 0) {
2418 err
= close(consumer_data
->err_sock
);
2420 PERROR("close consumer data error socket");
2427 * Setup necessary data for kernel tracer action.
2429 static int init_kernel_tracer(void)
2433 /* Modprobe lttng kernel modules */
2434 ret
= modprobe_lttng_control();
2439 /* Open debugfs lttng */
2440 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2441 if (kernel_tracer_fd
< 0) {
2442 DBG("Failed to open %s", module_proc_lttng
);
2447 /* Validate kernel version */
2448 ret
= kernel_validate_version(kernel_tracer_fd
);
2453 ret
= modprobe_lttng_data();
2458 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2462 modprobe_remove_lttng_control();
2463 ret
= close(kernel_tracer_fd
);
2467 kernel_tracer_fd
= -1;
2468 return LTTNG_ERR_KERN_VERSION
;
2471 ret
= close(kernel_tracer_fd
);
2477 modprobe_remove_lttng_control();
2480 WARN("No kernel tracer available");
2481 kernel_tracer_fd
= -1;
2483 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2485 return LTTNG_ERR_KERN_NA
;
2491 * Copy consumer output from the tracing session to the domain session. The
2492 * function also applies the right modification on a per domain basis for the
2493 * trace files destination directory.
2495 * Should *NOT* be called with RCU read-side lock held.
2497 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2500 const char *dir_name
;
2501 struct consumer_output
*consumer
;
2504 assert(session
->consumer
);
2507 case LTTNG_DOMAIN_KERNEL
:
2508 DBG3("Copying tracing session consumer output in kernel session");
2510 * XXX: We should audit the session creation and what this function
2511 * does "extra" in order to avoid a destroy since this function is used
2512 * in the domain session creation (kernel and ust) only. Same for UST
2515 if (session
->kernel_session
->consumer
) {
2516 consumer_destroy_output(session
->kernel_session
->consumer
);
2518 session
->kernel_session
->consumer
=
2519 consumer_copy_output(session
->consumer
);
2520 /* Ease our life a bit for the next part */
2521 consumer
= session
->kernel_session
->consumer
;
2522 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2524 case LTTNG_DOMAIN_JUL
:
2525 case LTTNG_DOMAIN_UST
:
2526 DBG3("Copying tracing session consumer output in UST session");
2527 if (session
->ust_session
->consumer
) {
2528 consumer_destroy_output(session
->ust_session
->consumer
);
2530 session
->ust_session
->consumer
=
2531 consumer_copy_output(session
->consumer
);
2532 /* Ease our life a bit for the next part */
2533 consumer
= session
->ust_session
->consumer
;
2534 dir_name
= DEFAULT_UST_TRACE_DIR
;
2537 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2541 /* Append correct directory to subdir */
2542 strncat(consumer
->subdir
, dir_name
,
2543 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2544 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2553 * Create an UST session and add it to the session ust list.
2555 * Should *NOT* be called with RCU read-side lock held.
2557 static int create_ust_session(struct ltt_session
*session
,
2558 struct lttng_domain
*domain
)
2561 struct ltt_ust_session
*lus
= NULL
;
2565 assert(session
->consumer
);
2567 switch (domain
->type
) {
2568 case LTTNG_DOMAIN_JUL
:
2569 case LTTNG_DOMAIN_UST
:
2572 ERR("Unknown UST domain on create session %d", domain
->type
);
2573 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2577 DBG("Creating UST session");
2579 lus
= trace_ust_create_session(session
->id
);
2581 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2585 lus
->uid
= session
->uid
;
2586 lus
->gid
= session
->gid
;
2587 lus
->output_traces
= session
->output_traces
;
2588 lus
->snapshot_mode
= session
->snapshot_mode
;
2589 lus
->live_timer_interval
= session
->live_timer
;
2590 session
->ust_session
= lus
;
2592 /* Copy session output to the newly created UST session */
2593 ret
= copy_session_consumer(domain
->type
, session
);
2594 if (ret
!= LTTNG_OK
) {
2602 session
->ust_session
= NULL
;
2607 * Create a kernel tracer session then create the default channel.
2609 static int create_kernel_session(struct ltt_session
*session
)
2613 DBG("Creating kernel session");
2615 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2617 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2621 /* Code flow safety */
2622 assert(session
->kernel_session
);
2624 /* Copy session output to the newly created Kernel session */
2625 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2626 if (ret
!= LTTNG_OK
) {
2630 /* Create directory(ies) on local filesystem. */
2631 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2632 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2633 ret
= run_as_mkdir_recursive(
2634 session
->kernel_session
->consumer
->dst
.trace_path
,
2635 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2637 if (ret
!= -EEXIST
) {
2638 ERR("Trace directory creation error");
2644 session
->kernel_session
->uid
= session
->uid
;
2645 session
->kernel_session
->gid
= session
->gid
;
2646 session
->kernel_session
->output_traces
= session
->output_traces
;
2647 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2652 trace_kernel_destroy_session(session
->kernel_session
);
2653 session
->kernel_session
= NULL
;
2658 * Count number of session permitted by uid/gid.
2660 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2663 struct ltt_session
*session
;
2665 DBG("Counting number of available session for UID %d GID %d",
2667 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2669 * Only list the sessions the user can control.
2671 if (!session_access_ok(session
, uid
, gid
)) {
2680 * Process the command requested by the lttng client within the command
2681 * context structure. This function make sure that the return structure (llm)
2682 * is set and ready for transmission before returning.
2684 * Return any error encountered or 0 for success.
2686 * "sock" is only used for special-case var. len data.
2688 * Should *NOT* be called with RCU read-side lock held.
2690 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2694 int need_tracing_session
= 1;
2697 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2701 switch (cmd_ctx
->lsm
->cmd_type
) {
2702 case LTTNG_CREATE_SESSION
:
2703 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2704 case LTTNG_CREATE_SESSION_LIVE
:
2705 case LTTNG_DESTROY_SESSION
:
2706 case LTTNG_LIST_SESSIONS
:
2707 case LTTNG_LIST_DOMAINS
:
2708 case LTTNG_START_TRACE
:
2709 case LTTNG_STOP_TRACE
:
2710 case LTTNG_DATA_PENDING
:
2711 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2712 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2713 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2714 case LTTNG_SNAPSHOT_RECORD
:
2721 if (opt_no_kernel
&& need_domain
2722 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2724 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2726 ret
= LTTNG_ERR_KERN_NA
;
2731 /* Deny register consumer if we already have a spawned consumer. */
2732 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2733 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2734 if (kconsumer_data
.pid
> 0) {
2735 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2736 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2739 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2743 * Check for command that don't needs to allocate a returned payload. We do
2744 * this here so we don't have to make the call for no payload at each
2747 switch(cmd_ctx
->lsm
->cmd_type
) {
2748 case LTTNG_LIST_SESSIONS
:
2749 case LTTNG_LIST_TRACEPOINTS
:
2750 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2751 case LTTNG_LIST_DOMAINS
:
2752 case LTTNG_LIST_CHANNELS
:
2753 case LTTNG_LIST_EVENTS
:
2756 /* Setup lttng message with no payload */
2757 ret
= setup_lttng_msg(cmd_ctx
, 0);
2759 /* This label does not try to unlock the session */
2760 goto init_setup_error
;
2764 /* Commands that DO NOT need a session. */
2765 switch (cmd_ctx
->lsm
->cmd_type
) {
2766 case LTTNG_CREATE_SESSION
:
2767 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2768 case LTTNG_CREATE_SESSION_LIVE
:
2769 case LTTNG_CALIBRATE
:
2770 case LTTNG_LIST_SESSIONS
:
2771 case LTTNG_LIST_TRACEPOINTS
:
2772 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2773 need_tracing_session
= 0;
2776 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2778 * We keep the session list lock across _all_ commands
2779 * for now, because the per-session lock does not
2780 * handle teardown properly.
2782 session_lock_list();
2783 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2784 if (cmd_ctx
->session
== NULL
) {
2785 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2788 /* Acquire lock for the session */
2789 session_lock(cmd_ctx
->session
);
2799 * Check domain type for specific "pre-action".
2801 switch (cmd_ctx
->lsm
->domain
.type
) {
2802 case LTTNG_DOMAIN_KERNEL
:
2804 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2808 /* Kernel tracer check */
2809 if (kernel_tracer_fd
== -1) {
2810 /* Basically, load kernel tracer modules */
2811 ret
= init_kernel_tracer();
2817 /* Consumer is in an ERROR state. Report back to client */
2818 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2819 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2823 /* Need a session for kernel command */
2824 if (need_tracing_session
) {
2825 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2826 ret
= create_kernel_session(cmd_ctx
->session
);
2828 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2833 /* Start the kernel consumer daemon */
2834 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2835 if (kconsumer_data
.pid
== 0 &&
2836 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2837 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2838 ret
= start_consumerd(&kconsumer_data
);
2840 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2843 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2845 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2849 * The consumer was just spawned so we need to add the socket to
2850 * the consumer output of the session if exist.
2852 ret
= consumer_create_socket(&kconsumer_data
,
2853 cmd_ctx
->session
->kernel_session
->consumer
);
2860 case LTTNG_DOMAIN_JUL
:
2861 case LTTNG_DOMAIN_UST
:
2863 if (!ust_app_supported()) {
2864 ret
= LTTNG_ERR_NO_UST
;
2867 /* Consumer is in an ERROR state. Report back to client */
2868 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2869 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2873 if (need_tracing_session
) {
2874 /* Create UST session if none exist. */
2875 if (cmd_ctx
->session
->ust_session
== NULL
) {
2876 ret
= create_ust_session(cmd_ctx
->session
,
2877 &cmd_ctx
->lsm
->domain
);
2878 if (ret
!= LTTNG_OK
) {
2883 /* Start the UST consumer daemons */
2885 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2886 if (consumerd64_bin
[0] != '\0' &&
2887 ustconsumer64_data
.pid
== 0 &&
2888 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2889 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2890 ret
= start_consumerd(&ustconsumer64_data
);
2892 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2893 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2897 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2898 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2900 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2904 * Setup socket for consumer 64 bit. No need for atomic access
2905 * since it was set above and can ONLY be set in this thread.
2907 ret
= consumer_create_socket(&ustconsumer64_data
,
2908 cmd_ctx
->session
->ust_session
->consumer
);
2914 if (consumerd32_bin
[0] != '\0' &&
2915 ustconsumer32_data
.pid
== 0 &&
2916 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2917 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2918 ret
= start_consumerd(&ustconsumer32_data
);
2920 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2921 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2925 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2926 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2928 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2932 * Setup socket for consumer 64 bit. No need for atomic access
2933 * since it was set above and can ONLY be set in this thread.
2935 ret
= consumer_create_socket(&ustconsumer32_data
,
2936 cmd_ctx
->session
->ust_session
->consumer
);
2948 /* Validate consumer daemon state when start/stop trace command */
2949 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2950 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2951 switch (cmd_ctx
->lsm
->domain
.type
) {
2952 case LTTNG_DOMAIN_JUL
:
2953 case LTTNG_DOMAIN_UST
:
2954 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2955 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2959 case LTTNG_DOMAIN_KERNEL
:
2960 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2961 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2969 * Check that the UID or GID match that of the tracing session.
2970 * The root user can interact with all sessions.
2972 if (need_tracing_session
) {
2973 if (!session_access_ok(cmd_ctx
->session
,
2974 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
2975 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
2976 ret
= LTTNG_ERR_EPERM
;
2982 * Send relayd information to consumer as soon as we have a domain and a
2985 if (cmd_ctx
->session
&& need_domain
) {
2987 * Setup relayd if not done yet. If the relayd information was already
2988 * sent to the consumer, this call will gracefully return.
2990 ret
= cmd_setup_relayd(cmd_ctx
->session
);
2991 if (ret
!= LTTNG_OK
) {
2996 /* Process by command type */
2997 switch (cmd_ctx
->lsm
->cmd_type
) {
2998 case LTTNG_ADD_CONTEXT
:
3000 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3001 cmd_ctx
->lsm
->u
.context
.channel_name
,
3002 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
3005 case LTTNG_DISABLE_CHANNEL
:
3007 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3008 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3011 case LTTNG_DISABLE_EVENT
:
3013 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3014 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3015 cmd_ctx
->lsm
->u
.disable
.name
);
3018 case LTTNG_DISABLE_ALL_EVENT
:
3020 DBG("Disabling all events");
3022 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3023 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3026 case LTTNG_ENABLE_CHANNEL
:
3028 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3029 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3032 case LTTNG_ENABLE_EVENT
:
3034 struct lttng_event_exclusion
*exclusion
= NULL
;
3035 struct lttng_filter_bytecode
*bytecode
= NULL
;
3037 /* Handle exclusion events and receive it from the client. */
3038 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3039 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3041 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3042 (count
* LTTNG_SYMBOL_NAME_LEN
));
3044 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3048 DBG("Receiving var len exclusion event list from client ...");
3049 exclusion
->count
= count
;
3050 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3051 count
* LTTNG_SYMBOL_NAME_LEN
);
3053 DBG("Nothing recv() from client var len data... continuing");
3056 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3061 /* Handle filter and get bytecode from client. */
3062 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3063 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3065 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3066 ret
= LTTNG_ERR_FILTER_INVAL
;
3071 bytecode
= zmalloc(bytecode_len
);
3074 ret
= LTTNG_ERR_FILTER_NOMEM
;
3078 /* Receive var. len. data */
3079 DBG("Receiving var len filter's bytecode from client ...");
3080 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3082 DBG("Nothing recv() from client car len data... continuing");
3086 ret
= LTTNG_ERR_FILTER_INVAL
;
3090 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3093 ret
= LTTNG_ERR_FILTER_INVAL
;
3098 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3099 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3100 &cmd_ctx
->lsm
->u
.enable
.event
, bytecode
, exclusion
,
3101 kernel_poll_pipe
[1]);
3104 case LTTNG_ENABLE_ALL_EVENT
:
3106 DBG("Enabling all events");
3108 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3109 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3110 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, kernel_poll_pipe
[1]);
3113 case LTTNG_LIST_TRACEPOINTS
:
3115 struct lttng_event
*events
;
3118 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3119 if (nb_events
< 0) {
3120 /* Return value is a negative lttng_error_code. */
3126 * Setup lttng message with payload size set to the event list size in
3127 * bytes and then copy list into the llm payload.
3129 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3135 /* Copy event list into message payload */
3136 memcpy(cmd_ctx
->llm
->payload
, events
,
3137 sizeof(struct lttng_event
) * nb_events
);
3144 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3146 struct lttng_event_field
*fields
;
3149 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3151 if (nb_fields
< 0) {
3152 /* Return value is a negative lttng_error_code. */
3158 * Setup lttng message with payload size set to the event list size in
3159 * bytes and then copy list into the llm payload.
3161 ret
= setup_lttng_msg(cmd_ctx
,
3162 sizeof(struct lttng_event_field
) * nb_fields
);
3168 /* Copy event list into message payload */
3169 memcpy(cmd_ctx
->llm
->payload
, fields
,
3170 sizeof(struct lttng_event_field
) * nb_fields
);
3177 case LTTNG_SET_CONSUMER_URI
:
3180 struct lttng_uri
*uris
;
3182 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3183 len
= nb_uri
* sizeof(struct lttng_uri
);
3186 ret
= LTTNG_ERR_INVALID
;
3190 uris
= zmalloc(len
);
3192 ret
= LTTNG_ERR_FATAL
;
3196 /* Receive variable len data */
3197 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3198 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3200 DBG("No URIs received from client... continuing");
3202 ret
= LTTNG_ERR_SESSION_FAIL
;
3207 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3209 if (ret
!= LTTNG_OK
) {
3215 * XXX: 0 means that this URI should be applied on the session. Should
3216 * be a DOMAIN enuam.
3218 if (cmd_ctx
->lsm
->domain
.type
== 0) {
3219 /* Add the URI for the UST session if a consumer is present. */
3220 if (cmd_ctx
->session
->ust_session
&&
3221 cmd_ctx
->session
->ust_session
->consumer
) {
3222 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
3224 } else if (cmd_ctx
->session
->kernel_session
&&
3225 cmd_ctx
->session
->kernel_session
->consumer
) {
3226 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
3227 cmd_ctx
->session
, nb_uri
, uris
);
3235 case LTTNG_START_TRACE
:
3237 ret
= cmd_start_trace(cmd_ctx
->session
);
3240 case LTTNG_STOP_TRACE
:
3242 ret
= cmd_stop_trace(cmd_ctx
->session
);
3245 case LTTNG_CREATE_SESSION
:
3248 struct lttng_uri
*uris
= NULL
;
3250 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3251 len
= nb_uri
* sizeof(struct lttng_uri
);
3254 uris
= zmalloc(len
);
3256 ret
= LTTNG_ERR_FATAL
;
3260 /* Receive variable len data */
3261 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3262 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3264 DBG("No URIs received from client... continuing");
3266 ret
= LTTNG_ERR_SESSION_FAIL
;
3271 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3272 DBG("Creating session with ONE network URI is a bad call");
3273 ret
= LTTNG_ERR_SESSION_FAIL
;
3279 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3280 &cmd_ctx
->creds
, 0);
3286 case LTTNG_DESTROY_SESSION
:
3288 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3290 /* Set session to NULL so we do not unlock it after free. */
3291 cmd_ctx
->session
= NULL
;
3294 case LTTNG_LIST_DOMAINS
:
3297 struct lttng_domain
*domains
;
3299 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3301 /* Return value is a negative lttng_error_code. */
3306 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3312 /* Copy event list into message payload */
3313 memcpy(cmd_ctx
->llm
->payload
, domains
,
3314 nb_dom
* sizeof(struct lttng_domain
));
3321 case LTTNG_LIST_CHANNELS
:
3324 struct lttng_channel
*channels
;
3326 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3327 cmd_ctx
->session
, &channels
);
3329 /* Return value is a negative lttng_error_code. */
3334 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3340 /* Copy event list into message payload */
3341 memcpy(cmd_ctx
->llm
->payload
, channels
,
3342 nb_chan
* sizeof(struct lttng_channel
));
3349 case LTTNG_LIST_EVENTS
:
3352 struct lttng_event
*events
= NULL
;
3354 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3355 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3357 /* Return value is a negative lttng_error_code. */
3362 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3368 /* Copy event list into message payload */
3369 memcpy(cmd_ctx
->llm
->payload
, events
,
3370 nb_event
* sizeof(struct lttng_event
));
3377 case LTTNG_LIST_SESSIONS
:
3379 unsigned int nr_sessions
;
3381 session_lock_list();
3382 nr_sessions
= lttng_sessions_count(
3383 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3384 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3386 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3388 session_unlock_list();
3392 /* Filled the session array */
3393 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3394 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3395 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3397 session_unlock_list();
3402 case LTTNG_CALIBRATE
:
3404 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3405 &cmd_ctx
->lsm
->u
.calibrate
);
3408 case LTTNG_REGISTER_CONSUMER
:
3410 struct consumer_data
*cdata
;
3412 switch (cmd_ctx
->lsm
->domain
.type
) {
3413 case LTTNG_DOMAIN_KERNEL
:
3414 cdata
= &kconsumer_data
;
3417 ret
= LTTNG_ERR_UND
;
3421 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3422 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3425 case LTTNG_DATA_PENDING
:
3427 ret
= cmd_data_pending(cmd_ctx
->session
);
3430 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3432 struct lttcomm_lttng_output_id reply
;
3434 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3435 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3436 if (ret
!= LTTNG_OK
) {
3440 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3445 /* Copy output list into message payload */
3446 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3450 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3452 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3453 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3456 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3459 struct lttng_snapshot_output
*outputs
= NULL
;
3461 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3462 if (nb_output
< 0) {
3467 ret
= setup_lttng_msg(cmd_ctx
,
3468 nb_output
* sizeof(struct lttng_snapshot_output
));
3475 /* Copy output list into message payload */
3476 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3477 nb_output
* sizeof(struct lttng_snapshot_output
));
3484 case LTTNG_SNAPSHOT_RECORD
:
3486 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3487 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3488 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3491 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3494 struct lttng_uri
*uris
= NULL
;
3496 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3497 len
= nb_uri
* sizeof(struct lttng_uri
);
3500 uris
= zmalloc(len
);
3502 ret
= LTTNG_ERR_FATAL
;
3506 /* Receive variable len data */
3507 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3508 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3510 DBG("No URIs received from client... continuing");
3512 ret
= LTTNG_ERR_SESSION_FAIL
;
3517 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3518 DBG("Creating session with ONE network URI is a bad call");
3519 ret
= LTTNG_ERR_SESSION_FAIL
;
3525 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3526 nb_uri
, &cmd_ctx
->creds
);
3530 case LTTNG_CREATE_SESSION_LIVE
:
3533 struct lttng_uri
*uris
= NULL
;
3535 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3536 len
= nb_uri
* sizeof(struct lttng_uri
);
3539 uris
= zmalloc(len
);
3541 ret
= LTTNG_ERR_FATAL
;
3545 /* Receive variable len data */
3546 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3547 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3549 DBG("No URIs received from client... continuing");
3551 ret
= LTTNG_ERR_SESSION_FAIL
;
3556 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3557 DBG("Creating session with ONE network URI is a bad call");
3558 ret
= LTTNG_ERR_SESSION_FAIL
;
3564 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3565 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3570 ret
= LTTNG_ERR_UND
;
3575 if (cmd_ctx
->llm
== NULL
) {
3576 DBG("Missing llm structure. Allocating one.");
3577 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3581 /* Set return code */
3582 cmd_ctx
->llm
->ret_code
= ret
;
3584 if (cmd_ctx
->session
) {
3585 session_unlock(cmd_ctx
->session
);
3587 if (need_tracing_session
) {
3588 session_unlock_list();
3595 * Thread managing health check socket.
3597 static void *thread_manage_health(void *data
)
3599 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3600 uint32_t revents
, nb_fd
;
3601 struct lttng_poll_event events
;
3602 struct health_comm_msg msg
;
3603 struct health_comm_reply reply
;
3605 DBG("[thread] Manage health check started");
3607 rcu_register_thread();
3609 /* We might hit an error path before this is created. */
3610 lttng_poll_init(&events
);
3612 /* Create unix socket */
3613 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3615 ERR("Unable to create health check Unix socket");
3621 /* lttng health client socket path permissions */
3622 ret
= chown(health_unix_sock_path
, 0,
3623 utils_get_group_id(tracing_group_name
));
3625 ERR("Unable to set group on %s", health_unix_sock_path
);
3631 ret
= chmod(health_unix_sock_path
,
3632 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3634 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3642 * Set the CLOEXEC flag. Return code is useless because either way, the
3645 (void) utils_set_fd_cloexec(sock
);
3647 ret
= lttcomm_listen_unix_sock(sock
);
3653 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3654 * more will be added to this poll set.
3656 ret
= sessiond_set_thread_pollset(&events
, 2);
3661 /* Add the application registration socket */
3662 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3667 lttng_sessiond_notify_ready();
3670 DBG("Health check ready");
3672 /* Inifinite blocking call, waiting for transmission */
3674 ret
= lttng_poll_wait(&events
, -1);
3677 * Restart interrupted system call.
3679 if (errno
== EINTR
) {
3687 for (i
= 0; i
< nb_fd
; i
++) {
3688 /* Fetch once the poll data */
3689 revents
= LTTNG_POLL_GETEV(&events
, i
);
3690 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3692 /* Thread quit pipe has been closed. Killing thread. */
3693 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3699 /* Event on the registration socket */
3700 if (pollfd
== sock
) {
3701 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3702 ERR("Health socket poll error");
3708 new_sock
= lttcomm_accept_unix_sock(sock
);
3714 * Set the CLOEXEC flag. Return code is useless because either way, the
3717 (void) utils_set_fd_cloexec(new_sock
);
3719 DBG("Receiving data from client for health...");
3720 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3722 DBG("Nothing recv() from client... continuing");
3723 ret
= close(new_sock
);
3731 rcu_thread_online();
3734 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
3736 * health_check_state returns 0 if health is
3739 if (!health_check_state(health_sessiond
, i
)) {
3740 reply
.ret_code
|= 1ULL << i
;
3744 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
3746 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3748 ERR("Failed to send health data back to client");
3751 /* End of transmission */
3752 ret
= close(new_sock
);
3762 ERR("Health error occurred in %s", __func__
);
3764 DBG("Health check thread dying");
3765 unlink(health_unix_sock_path
);
3773 lttng_poll_clean(&events
);
3775 rcu_unregister_thread();
3780 * This thread manage all clients request using the unix client socket for
3783 static void *thread_manage_clients(void *data
)
3785 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3787 uint32_t revents
, nb_fd
;
3788 struct command_ctx
*cmd_ctx
= NULL
;
3789 struct lttng_poll_event events
;
3791 DBG("[thread] Manage client started");
3793 rcu_register_thread();
3795 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
3797 health_code_update();
3799 ret
= lttcomm_listen_unix_sock(client_sock
);
3805 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3806 * more will be added to this poll set.
3808 ret
= sessiond_set_thread_pollset(&events
, 2);
3810 goto error_create_poll
;
3813 /* Add the application registration socket */
3814 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3819 lttng_sessiond_notify_ready();
3821 /* This testpoint is after we signal readiness to the parent. */
3822 if (testpoint(sessiond_thread_manage_clients
)) {
3826 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
3830 health_code_update();
3833 DBG("Accepting client command ...");
3835 /* Inifinite blocking call, waiting for transmission */
3837 health_poll_entry();
3838 ret
= lttng_poll_wait(&events
, -1);
3842 * Restart interrupted system call.
3844 if (errno
== EINTR
) {
3852 for (i
= 0; i
< nb_fd
; i
++) {
3853 /* Fetch once the poll data */
3854 revents
= LTTNG_POLL_GETEV(&events
, i
);
3855 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3857 health_code_update();
3859 /* Thread quit pipe has been closed. Killing thread. */
3860 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3866 /* Event on the registration socket */
3867 if (pollfd
== client_sock
) {
3868 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3869 ERR("Client socket poll error");
3875 DBG("Wait for client response");
3877 health_code_update();
3879 sock
= lttcomm_accept_unix_sock(client_sock
);
3885 * Set the CLOEXEC flag. Return code is useless because either way, the
3888 (void) utils_set_fd_cloexec(sock
);
3890 /* Set socket option for credentials retrieval */
3891 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3896 /* Allocate context command to process the client request */
3897 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3898 if (cmd_ctx
== NULL
) {
3899 PERROR("zmalloc cmd_ctx");
3903 /* Allocate data buffer for reception */
3904 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3905 if (cmd_ctx
->lsm
== NULL
) {
3906 PERROR("zmalloc cmd_ctx->lsm");
3910 cmd_ctx
->llm
= NULL
;
3911 cmd_ctx
->session
= NULL
;
3913 health_code_update();
3916 * Data is received from the lttng client. The struct
3917 * lttcomm_session_msg (lsm) contains the command and data request of
3920 DBG("Receiving data from client ...");
3921 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
3922 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
3924 DBG("Nothing recv() from client... continuing");
3930 clean_command_ctx(&cmd_ctx
);
3934 health_code_update();
3936 // TODO: Validate cmd_ctx including sanity check for
3937 // security purpose.
3939 rcu_thread_online();
3941 * This function dispatch the work to the kernel or userspace tracer
3942 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3943 * informations for the client. The command context struct contains
3944 * everything this function may needs.
3946 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
3947 rcu_thread_offline();
3955 * TODO: Inform client somehow of the fatal error. At
3956 * this point, ret < 0 means that a zmalloc failed
3957 * (ENOMEM). Error detected but still accept
3958 * command, unless a socket error has been
3961 clean_command_ctx(&cmd_ctx
);
3965 health_code_update();
3967 DBG("Sending response (size: %d, retcode: %s)",
3968 cmd_ctx
->lttng_msg_size
,
3969 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
3970 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
3972 ERR("Failed to send data back to client");
3975 /* End of transmission */
3982 clean_command_ctx(&cmd_ctx
);
3984 health_code_update();
3996 lttng_poll_clean(&events
);
3997 clean_command_ctx(&cmd_ctx
);
4001 unlink(client_unix_sock_path
);
4002 if (client_sock
>= 0) {
4003 ret
= close(client_sock
);
4011 ERR("Health error occurred in %s", __func__
);
4014 health_unregister(health_sessiond
);
4016 DBG("Client thread dying");
4018 rcu_unregister_thread();
4024 * usage function on stderr
4026 static void usage(void)
4028 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4029 fprintf(stderr
, " -h, --help Display this usage.\n");
4030 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4031 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4032 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4033 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4034 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4035 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4036 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4037 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4038 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4039 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4040 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4041 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4042 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4043 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
4044 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4045 fprintf(stderr
, " -V, --version Show version number.\n");
4046 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4047 fprintf(stderr
, " -q, --quiet No output at all.\n");
4048 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4049 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4050 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4051 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4052 fprintf(stderr
, " --jul-tcp-port JUL application registration TCP port\n");
4053 fprintf(stderr
, " -f --config Load daemon configuration file\n");
4057 * Take an option from the getopt output and set it in the right variable to be
4060 * Return 0 on success else a negative value.
4062 static int set_option(int opt
, const char *arg
, const char *optname
)
4068 fprintf(stderr
, "option %s", optname
);
4070 fprintf(stderr
, " with arg %s\n", arg
);
4074 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4077 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4086 tracing_group_name
= strdup(arg
);
4092 fprintf(stdout
, "%s\n", VERSION
);
4098 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4101 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4104 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4107 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4110 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4113 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4119 lttng_opt_quiet
= 1;
4122 /* Verbose level can increase using multiple -v */
4124 lttng_opt_verbose
= config_parse_value(arg
);
4126 lttng_opt_verbose
+= 1;
4131 opt_verbose_consumer
= config_parse_value(arg
);
4133 opt_verbose_consumer
+= 1;
4137 consumerd32_bin
= strdup(arg
);
4138 consumerd32_bin_override
= 1;
4141 consumerd32_libdir
= strdup(arg
);
4142 consumerd32_libdir_override
= 1;
4145 consumerd64_bin
= strdup(arg
);
4146 consumerd64_bin_override
= 1;
4149 consumerd64_libdir
= strdup(arg
);
4150 consumerd64_libdir_override
= 1;
4153 opt_pidfile
= strdup(arg
);
4155 case 'J': /* JUL TCP port. */
4160 v
= strtoul(arg
, NULL
, 0);
4161 if (errno
!= 0 || !isdigit(arg
[0])) {
4162 ERR("Wrong value in --jul-tcp-port parameter: %s", arg
);
4165 if (v
== 0 || v
>= 65535) {
4166 ERR("Port overflow in --jul-tcp-port parameter: %s", arg
);
4169 jul_tcp_port
= (uint32_t) v
;
4170 DBG3("JUL TCP port set to non default: %u", jul_tcp_port
);
4174 /* Unknown option or other error.
4175 * Error is printed by getopt, just return */
4183 * config_entry_handler_cb used to handle options read from a config file.
4184 * See config_entry_handler_cb comment in common/config/config.h for the
4185 * return value conventions.
4187 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4191 if (!entry
|| !entry
->name
|| !entry
->value
) {
4196 /* Check if the option is to be ignored */
4197 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4198 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4203 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4206 /* Ignore if not fully matched. */
4207 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4212 * If the option takes no argument on the command line, we have to
4213 * check if the value is "true". We support non-zero numeric values,
4216 if (!long_options
[i
].has_arg
) {
4217 ret
= config_parse_value(entry
->value
);
4220 WARN("Invalid configuration value \"%s\" for option %s",
4221 entry
->value
, entry
->name
);
4223 /* False, skip boolean config option. */
4228 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4232 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4239 * daemon configuration loading and argument parsing
4241 static int set_options(int argc
, char **argv
)
4243 int ret
= 0, c
= 0, option_index
= 0;
4244 int orig_optopt
= optopt
, orig_optind
= optind
;
4246 const char *config_path
= NULL
;
4248 optstring
= utils_generate_optstring(long_options
,
4249 sizeof(long_options
) / sizeof(struct option
));
4255 /* Check for the --config option */
4256 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4257 &option_index
)) != -1) {
4261 } else if (c
!= 'f') {
4262 /* if not equal to --config option. */
4266 config_path
= utils_expand_path(optarg
);
4268 ERR("Failed to resolve path: %s", optarg
);
4272 ret
= config_get_section_entries(config_path
, config_section_name
,
4273 config_entry_handler
, NULL
);
4276 ERR("Invalid configuration option at line %i", ret
);
4282 /* Reset getopt's global state */
4283 optopt
= orig_optopt
;
4284 optind
= orig_optind
;
4286 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
4291 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
4303 * Creates the two needed socket by the daemon.
4304 * apps_sock - The communication socket for all UST apps.
4305 * client_sock - The communication of the cli tool (lttng).
4307 static int init_daemon_socket(void)
4312 old_umask
= umask(0);
4314 /* Create client tool unix socket */
4315 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4316 if (client_sock
< 0) {
4317 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4322 /* Set the cloexec flag */
4323 ret
= utils_set_fd_cloexec(client_sock
);
4325 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4326 "Continuing but note that the consumer daemon will have a "
4327 "reference to this socket on exec()", client_sock
);
4330 /* File permission MUST be 660 */
4331 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4333 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4338 /* Create the application unix socket */
4339 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4340 if (apps_sock
< 0) {
4341 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4346 /* Set the cloexec flag */
4347 ret
= utils_set_fd_cloexec(apps_sock
);
4349 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4350 "Continuing but note that the consumer daemon will have a "
4351 "reference to this socket on exec()", apps_sock
);
4354 /* File permission MUST be 666 */
4355 ret
= chmod(apps_unix_sock_path
,
4356 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4358 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4363 DBG3("Session daemon client socket %d and application socket %d created",
4364 client_sock
, apps_sock
);
4372 * Check if the global socket is available, and if a daemon is answering at the
4373 * other side. If yes, error is returned.
4375 static int check_existing_daemon(void)
4377 /* Is there anybody out there ? */
4378 if (lttng_session_daemon_alive()) {
4386 * Set the tracing group gid onto the client socket.
4388 * Race window between mkdir and chown is OK because we are going from more
4389 * permissive (root.root) to less permissive (root.tracing).
4391 static int set_permissions(char *rundir
)
4396 gid
= utils_get_group_id(tracing_group_name
);
4398 /* Set lttng run dir */
4399 ret
= chown(rundir
, 0, gid
);
4401 ERR("Unable to set group on %s", rundir
);
4406 * Ensure all applications and tracing group can search the run
4407 * dir. Allow everyone to read the directory, since it does not
4408 * buy us anything to hide its content.
4410 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4412 ERR("Unable to set permissions on %s", rundir
);
4416 /* lttng client socket path */
4417 ret
= chown(client_unix_sock_path
, 0, gid
);
4419 ERR("Unable to set group on %s", client_unix_sock_path
);
4423 /* kconsumer error socket path */
4424 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4426 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4430 /* 64-bit ustconsumer error socket path */
4431 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4433 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4437 /* 32-bit ustconsumer compat32 error socket path */
4438 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4440 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4444 DBG("All permissions are set");
4450 * Create the lttng run directory needed for all global sockets and pipe.
4452 static int create_lttng_rundir(const char *rundir
)
4456 DBG3("Creating LTTng run directory: %s", rundir
);
4458 ret
= mkdir(rundir
, S_IRWXU
);
4460 if (errno
!= EEXIST
) {
4461 ERR("Unable to create %s", rundir
);
4473 * Setup sockets and directory needed by the kconsumerd communication with the
4476 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4480 char path
[PATH_MAX
];
4482 switch (consumer_data
->type
) {
4483 case LTTNG_CONSUMER_KERNEL
:
4484 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4486 case LTTNG_CONSUMER64_UST
:
4487 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4489 case LTTNG_CONSUMER32_UST
:
4490 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4493 ERR("Consumer type unknown");
4498 DBG2("Creating consumer directory: %s", path
);
4500 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4502 if (errno
!= EEXIST
) {
4504 ERR("Failed to create %s", path
);
4510 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4512 ERR("Unable to set group on %s", path
);
4518 /* Create the kconsumerd error unix socket */
4519 consumer_data
->err_sock
=
4520 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4521 if (consumer_data
->err_sock
< 0) {
4522 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4528 * Set the CLOEXEC flag. Return code is useless because either way, the
4531 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4533 PERROR("utils_set_fd_cloexec");
4534 /* continue anyway */
4537 /* File permission MUST be 660 */
4538 ret
= chmod(consumer_data
->err_unix_sock_path
,
4539 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4541 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4551 * Signal handler for the daemon
4553 * Simply stop all worker threads, leaving main() return gracefully after
4554 * joining all threads and calling cleanup().
4556 static void sighandler(int sig
)
4560 DBG("SIGPIPE caught");
4563 DBG("SIGINT caught");
4567 DBG("SIGTERM caught");
4571 CMM_STORE_SHARED(recv_child_signal
, 1);
4579 * Setup signal handler for :
4580 * SIGINT, SIGTERM, SIGPIPE
4582 static int set_signal_handler(void)
4585 struct sigaction sa
;
4588 if ((ret
= sigemptyset(&sigset
)) < 0) {
4589 PERROR("sigemptyset");
4593 sa
.sa_handler
= sighandler
;
4594 sa
.sa_mask
= sigset
;
4596 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
4597 PERROR("sigaction");
4601 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
4602 PERROR("sigaction");
4606 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
4607 PERROR("sigaction");
4611 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
4612 PERROR("sigaction");
4616 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
4622 * Set open files limit to unlimited. This daemon can open a large number of
4623 * file descriptors in order to consumer multiple kernel traces.
4625 static void set_ulimit(void)
4630 /* The kernel does not allowed an infinite limit for open files */
4631 lim
.rlim_cur
= 65535;
4632 lim
.rlim_max
= 65535;
4634 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
4636 PERROR("failed to set open files limit");
4641 * Write pidfile using the rundir and opt_pidfile.
4643 static void write_pidfile(void)
4646 char pidfile_path
[PATH_MAX
];
4651 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4653 /* Build pidfile path from rundir and opt_pidfile. */
4654 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4655 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4657 PERROR("snprintf pidfile path");
4663 * Create pid file in rundir. Return value is of no importance. The
4664 * execution will continue even though we are not able to write the file.
4666 (void) utils_create_pid_file(getpid(), pidfile_path
);
4673 * Write JUL TCP port using the rundir.
4675 static void write_julport(void)
4678 char path
[PATH_MAX
];
4682 ret
= snprintf(path
, sizeof(path
), "%s/"
4683 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
, rundir
);
4685 PERROR("snprintf julport path");
4690 * Create TCP JUL port file in rundir. Return value is of no importance.
4691 * The execution will continue even though we are not able to write the
4694 (void) utils_create_pid_file(jul_tcp_port
, path
);
4703 int main(int argc
, char **argv
)
4707 const char *home_path
, *env_app_timeout
;
4709 init_kernel_workarounds();
4711 rcu_register_thread();
4713 if ((ret
= set_signal_handler()) < 0) {
4717 setup_consumerd_path();
4719 page_size
= sysconf(_SC_PAGESIZE
);
4720 if (page_size
< 0) {
4721 PERROR("sysconf _SC_PAGESIZE");
4722 page_size
= LONG_MAX
;
4723 WARN("Fallback page size to %ld", page_size
);
4726 /* Parse arguments and load the daemon configuration file */
4728 if ((ret
= set_options(argc
, argv
)) < 0) {
4733 if (opt_daemon
|| opt_background
) {
4736 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
4743 * We are in the child. Make sure all other file descriptors are
4744 * closed, in case we are called with more opened file descriptors than
4745 * the standard ones.
4747 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4752 /* Create thread quit pipe */
4753 if ((ret
= init_thread_quit_pipe()) < 0) {
4757 /* Check if daemon is UID = 0 */
4758 is_root
= !getuid();
4761 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4763 /* Create global run dir with root access */
4764 ret
= create_lttng_rundir(rundir
);
4769 if (strlen(apps_unix_sock_path
) == 0) {
4770 snprintf(apps_unix_sock_path
, PATH_MAX
,
4771 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4774 if (strlen(client_unix_sock_path
) == 0) {
4775 snprintf(client_unix_sock_path
, PATH_MAX
,
4776 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4779 /* Set global SHM for ust */
4780 if (strlen(wait_shm_path
) == 0) {
4781 snprintf(wait_shm_path
, PATH_MAX
,
4782 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4785 if (strlen(health_unix_sock_path
) == 0) {
4786 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4787 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4790 /* Setup kernel consumerd path */
4791 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4792 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4793 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4794 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4796 DBG2("Kernel consumer err path: %s",
4797 kconsumer_data
.err_unix_sock_path
);
4798 DBG2("Kernel consumer cmd path: %s",
4799 kconsumer_data
.cmd_unix_sock_path
);
4801 home_path
= utils_get_home_dir();
4802 if (home_path
== NULL
) {
4803 /* TODO: Add --socket PATH option */
4804 ERR("Can't get HOME directory for sockets creation.");
4810 * Create rundir from home path. This will create something like
4813 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4819 ret
= create_lttng_rundir(rundir
);
4824 if (strlen(apps_unix_sock_path
) == 0) {
4825 snprintf(apps_unix_sock_path
, PATH_MAX
,
4826 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4829 /* Set the cli tool unix socket path */
4830 if (strlen(client_unix_sock_path
) == 0) {
4831 snprintf(client_unix_sock_path
, PATH_MAX
,
4832 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4835 /* Set global SHM for ust */
4836 if (strlen(wait_shm_path
) == 0) {
4837 snprintf(wait_shm_path
, PATH_MAX
,
4838 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4841 /* Set health check Unix path */
4842 if (strlen(health_unix_sock_path
) == 0) {
4843 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4844 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4848 /* Set consumer initial state */
4849 kernel_consumerd_state
= CONSUMER_STOPPED
;
4850 ust_consumerd_state
= CONSUMER_STOPPED
;
4852 DBG("Client socket path %s", client_unix_sock_path
);
4853 DBG("Application socket path %s", apps_unix_sock_path
);
4854 DBG("Application wait path %s", wait_shm_path
);
4855 DBG("LTTng run directory path: %s", rundir
);
4857 /* 32 bits consumerd path setup */
4858 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4859 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4860 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4861 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4863 DBG2("UST consumer 32 bits err path: %s",
4864 ustconsumer32_data
.err_unix_sock_path
);
4865 DBG2("UST consumer 32 bits cmd path: %s",
4866 ustconsumer32_data
.cmd_unix_sock_path
);
4868 /* 64 bits consumerd path setup */
4869 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4870 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4871 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4872 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4874 DBG2("UST consumer 64 bits err path: %s",
4875 ustconsumer64_data
.err_unix_sock_path
);
4876 DBG2("UST consumer 64 bits cmd path: %s",
4877 ustconsumer64_data
.cmd_unix_sock_path
);
4880 * See if daemon already exist.
4882 if ((ret
= check_existing_daemon()) < 0) {
4883 ERR("Already running daemon.\n");
4885 * We do not goto exit because we must not cleanup()
4886 * because a daemon is already running.
4892 * Init UST app hash table. Alloc hash table before this point since
4893 * cleanup() can get called after that point.
4897 /* Initialize JUL domain subsystem. */
4898 if ((ret
= jul_init()) < 0) {
4899 /* ENOMEM at this point. */
4903 /* After this point, we can safely call cleanup() with "goto exit" */
4906 * These actions must be executed as root. We do that *after* setting up
4907 * the sockets path because we MUST make the check for another daemon using
4908 * those paths *before* trying to set the kernel consumer sockets and init
4912 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
4917 /* Setup kernel tracer */
4918 if (!opt_no_kernel
) {
4919 init_kernel_tracer();
4922 /* Set ulimit for open files */
4925 /* init lttng_fd tracking must be done after set_ulimit. */
4928 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
4933 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
4938 /* Setup the needed unix socket */
4939 if ((ret
= init_daemon_socket()) < 0) {
4943 /* Set credentials to socket */
4944 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
4948 /* Get parent pid if -S, --sig-parent is specified. */
4949 if (opt_sig_parent
) {
4953 /* Setup the kernel pipe for waking up the kernel thread */
4954 if (is_root
&& !opt_no_kernel
) {
4955 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
4960 /* Setup the thread ht_cleanup communication pipe. */
4961 if (utils_create_pipe_cloexec(ht_cleanup_pipe
) < 0) {
4965 /* Setup the thread apps communication pipe. */
4966 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
4970 /* Setup the thread apps notify communication pipe. */
4971 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
4975 /* Initialize global buffer per UID and PID registry. */
4976 buffer_reg_init_uid_registry();
4977 buffer_reg_init_pid_registry();
4979 /* Init UST command queue. */
4980 cds_wfq_init(&ust_cmd_queue
.queue
);
4983 * Get session list pointer. This pointer MUST NOT be free(). This list is
4984 * statically declared in session.c
4986 session_list_ptr
= session_get_list();
4988 /* Set up max poll set size */
4989 lttng_poll_set_max_size();
4993 /* Check for the application socket timeout env variable. */
4994 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
4995 if (env_app_timeout
) {
4996 app_socket_timeout
= atoi(env_app_timeout
);
4998 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5004 /* Initialize communication library */
5006 /* This is to get the TCP timeout value. */
5007 lttcomm_inet_init();
5010 * Initialize the health check subsystem. This call should set the
5011 * appropriate time values.
5013 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5014 if (!health_sessiond
) {
5015 PERROR("health_app_create error");
5016 goto exit_health_sessiond_cleanup
;
5019 /* Create thread to clean up RCU hash tables */
5020 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5021 thread_ht_cleanup
, (void *) NULL
);
5023 PERROR("pthread_create ht_cleanup");
5024 goto exit_ht_cleanup
;
5027 /* Create health-check thread */
5028 ret
= pthread_create(&health_thread
, NULL
,
5029 thread_manage_health
, (void *) NULL
);
5031 PERROR("pthread_create health");
5035 /* Create thread to manage the client socket */
5036 ret
= pthread_create(&client_thread
, NULL
,
5037 thread_manage_clients
, (void *) NULL
);
5039 PERROR("pthread_create clients");
5043 /* Create thread to dispatch registration */
5044 ret
= pthread_create(&dispatch_thread
, NULL
,
5045 thread_dispatch_ust_registration
, (void *) NULL
);
5047 PERROR("pthread_create dispatch");
5051 /* Create thread to manage application registration. */
5052 ret
= pthread_create(®_apps_thread
, NULL
,
5053 thread_registration_apps
, (void *) NULL
);
5055 PERROR("pthread_create registration");
5059 /* Create thread to manage application socket */
5060 ret
= pthread_create(&apps_thread
, NULL
,
5061 thread_manage_apps
, (void *) NULL
);
5063 PERROR("pthread_create apps");
5067 /* Create thread to manage application notify socket */
5068 ret
= pthread_create(&apps_notify_thread
, NULL
,
5069 ust_thread_manage_notify
, (void *) NULL
);
5071 PERROR("pthread_create apps");
5072 goto exit_apps_notify
;
5075 /* Create JUL registration thread. */
5076 ret
= pthread_create(&jul_reg_thread
, NULL
,
5077 jul_thread_manage_registration
, (void *) NULL
);
5079 PERROR("pthread_create apps");
5083 /* Don't start this thread if kernel tracing is not requested nor root */
5084 if (is_root
&& !opt_no_kernel
) {
5085 /* Create kernel thread to manage kernel event */
5086 ret
= pthread_create(&kernel_thread
, NULL
,
5087 thread_manage_kernel
, (void *) NULL
);
5089 PERROR("pthread_create kernel");
5093 ret
= pthread_join(kernel_thread
, &status
);
5095 PERROR("pthread_join");
5096 goto error
; /* join error, exit without cleanup */
5101 ret
= pthread_join(jul_reg_thread
, &status
);
5103 PERROR("pthread_join JUL");
5104 goto error
; /* join error, exit without cleanup */
5108 ret
= pthread_join(apps_notify_thread
, &status
);
5110 PERROR("pthread_join apps notify");
5111 goto error
; /* join error, exit without cleanup */
5115 ret
= pthread_join(apps_thread
, &status
);
5117 PERROR("pthread_join apps");
5118 goto error
; /* join error, exit without cleanup */
5123 ret
= pthread_join(reg_apps_thread
, &status
);
5125 PERROR("pthread_join");
5126 goto error
; /* join error, exit without cleanup */
5130 ret
= pthread_join(dispatch_thread
, &status
);
5132 PERROR("pthread_join");
5133 goto error
; /* join error, exit without cleanup */
5137 ret
= pthread_join(client_thread
, &status
);
5139 PERROR("pthread_join");
5140 goto error
; /* join error, exit without cleanup */
5143 ret
= join_consumer_thread(&kconsumer_data
);
5145 PERROR("join_consumer");
5146 goto error
; /* join error, exit without cleanup */
5149 ret
= join_consumer_thread(&ustconsumer32_data
);
5151 PERROR("join_consumer ust32");
5152 goto error
; /* join error, exit without cleanup */
5155 ret
= join_consumer_thread(&ustconsumer64_data
);
5157 PERROR("join_consumer ust64");
5158 goto error
; /* join error, exit without cleanup */
5162 ret
= pthread_join(health_thread
, &status
);
5164 PERROR("pthread_join health thread");
5165 goto error
; /* join error, exit without cleanup */
5169 ret
= pthread_join(ht_cleanup_thread
, &status
);
5171 PERROR("pthread_join ht cleanup thread");
5172 goto error
; /* join error, exit without cleanup */
5175 health_app_destroy(health_sessiond
);
5176 exit_health_sessiond_cleanup
:
5179 * cleanup() is called when no other thread is running.
5181 rcu_thread_online();
5183 rcu_thread_offline();
5184 rcu_unregister_thread();