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 #include "load-session-thread.h"
74 #define CONSUMERD_FILE "lttng-consumerd"
77 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
78 static int tracing_group_name_override
;
79 static char *opt_pidfile
;
80 static int opt_sig_parent
;
81 static int opt_verbose_consumer
;
82 static int opt_daemon
, opt_background
;
83 static int opt_no_kernel
;
84 static char *opt_load_session_path
;
85 static pid_t ppid
; /* Parent PID for --sig-parent option */
86 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
89 /* Set to 1 when a SIGUSR1 signal is received. */
90 static int recv_child_signal
;
93 * Consumer daemon specific control data. Every value not initialized here is
94 * set to 0 by the static definition.
96 static struct consumer_data kconsumer_data
= {
97 .type
= LTTNG_CONSUMER_KERNEL
,
98 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
99 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
102 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
103 .lock
= PTHREAD_MUTEX_INITIALIZER
,
104 .cond
= PTHREAD_COND_INITIALIZER
,
105 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
107 static struct consumer_data ustconsumer64_data
= {
108 .type
= LTTNG_CONSUMER64_UST
,
109 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
110 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
113 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
114 .lock
= PTHREAD_MUTEX_INITIALIZER
,
115 .cond
= PTHREAD_COND_INITIALIZER
,
116 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
118 static struct consumer_data ustconsumer32_data
= {
119 .type
= LTTNG_CONSUMER32_UST
,
120 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
121 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
124 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
125 .lock
= PTHREAD_MUTEX_INITIALIZER
,
126 .cond
= PTHREAD_COND_INITIALIZER
,
127 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
130 /* Command line options */
131 static const struct option long_options
[] = {
132 { "client-sock", 1, 0, 'c' },
133 { "apps-sock", 1, 0, 'a' },
134 { "kconsumerd-cmd-sock", 1, 0, 'C' },
135 { "kconsumerd-err-sock", 1, 0, 'E' },
136 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
137 { "ustconsumerd32-err-sock", 1, 0, 'H' },
138 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
139 { "ustconsumerd64-err-sock", 1, 0, 'F' },
140 { "consumerd32-path", 1, 0, 'u' },
141 { "consumerd32-libdir", 1, 0, 'U' },
142 { "consumerd64-path", 1, 0, 't' },
143 { "consumerd64-libdir", 1, 0, 'T' },
144 { "daemonize", 0, 0, 'd' },
145 { "background", 0, 0, 'b' },
146 { "sig-parent", 0, 0, 'S' },
147 { "help", 0, 0, 'h' },
148 { "group", 1, 0, 'g' },
149 { "version", 0, 0, 'V' },
150 { "quiet", 0, 0, 'q' },
151 { "verbose", 0, 0, 'v' },
152 { "verbose-consumer", 0, 0, 'Z' },
153 { "no-kernel", 0, 0, 'N' },
154 { "pidfile", 1, 0, 'p' },
155 { "jul-tcp-port", 1, 0, 'J' },
156 { "config", 1, 0, 'f' },
157 { "load", 1, 0, 'l' },
161 /* Command line options to ignore from configuration file */
162 static const char *config_ignore_options
[] = { "help", "version", "config" };
164 /* Shared between threads */
165 static int dispatch_thread_exit
;
167 /* Global application Unix socket path */
168 static char apps_unix_sock_path
[PATH_MAX
];
169 /* Global client Unix socket path */
170 static char client_unix_sock_path
[PATH_MAX
];
171 /* global wait shm path for UST */
172 static char wait_shm_path
[PATH_MAX
];
173 /* Global health check unix path */
174 static char health_unix_sock_path
[PATH_MAX
];
176 /* Sockets and FDs */
177 static int client_sock
= -1;
178 static int apps_sock
= -1;
179 int kernel_tracer_fd
= -1;
180 static int kernel_poll_pipe
[2] = { -1, -1 };
183 * Quit pipe for all threads. This permits a single cancellation point
184 * for all threads when receiving an event on the pipe.
186 static int thread_quit_pipe
[2] = { -1, -1 };
189 * This pipe is used to inform the thread managing application communication
190 * that a command is queued and ready to be processed.
192 static int apps_cmd_pipe
[2] = { -1, -1 };
194 int apps_cmd_notify_pipe
[2] = { -1, -1 };
196 /* Pthread, Mutexes and Semaphores */
197 static pthread_t apps_thread
;
198 static pthread_t apps_notify_thread
;
199 static pthread_t reg_apps_thread
;
200 static pthread_t client_thread
;
201 static pthread_t kernel_thread
;
202 static pthread_t dispatch_thread
;
203 static pthread_t health_thread
;
204 static pthread_t ht_cleanup_thread
;
205 static pthread_t jul_reg_thread
;
206 static pthread_t load_session_thread
;
209 * UST registration command queue. This queue is tied with a futex and uses a N
210 * wakers / 1 waiter implemented and detailed in futex.c/.h
212 * The thread_registration_apps and thread_dispatch_ust_registration uses this
213 * queue along with the wait/wake scheme. The thread_manage_apps receives down
214 * the line new application socket and monitors it for any I/O error or clean
215 * close that triggers an unregistration of the application.
217 static struct ust_cmd_queue ust_cmd_queue
;
220 * Pointer initialized before thread creation.
222 * This points to the tracing session list containing the session count and a
223 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
224 * MUST NOT be taken if you call a public function in session.c.
226 * The lock is nested inside the structure: session_list_ptr->lock. Please use
227 * session_lock_list and session_unlock_list for lock acquisition.
229 static struct ltt_session_list
*session_list_ptr
;
231 int ust_consumerd64_fd
= -1;
232 int ust_consumerd32_fd
= -1;
234 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
235 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
236 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
237 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
238 static int consumerd32_bin_override
;
239 static int consumerd64_bin_override
;
240 static int consumerd32_libdir_override
;
241 static int consumerd64_libdir_override
;
243 static const char *module_proc_lttng
= "/proc/lttng";
246 * Consumer daemon state which is changed when spawning it, killing it or in
247 * case of a fatal error.
249 enum consumerd_state
{
250 CONSUMER_STARTED
= 1,
251 CONSUMER_STOPPED
= 2,
256 * This consumer daemon state is used to validate if a client command will be
257 * able to reach the consumer. If not, the client is informed. For instance,
258 * doing a "lttng start" when the consumer state is set to ERROR will return an
259 * error to the client.
261 * The following example shows a possible race condition of this scheme:
263 * consumer thread error happens
265 * client cmd checks state -> still OK
266 * consumer thread exit, sets error
267 * client cmd try to talk to consumer
270 * However, since the consumer is a different daemon, we have no way of making
271 * sure the command will reach it safely even with this state flag. This is why
272 * we consider that up to the state validation during command processing, the
273 * command is safe. After that, we can not guarantee the correctness of the
274 * client request vis-a-vis the consumer.
276 static enum consumerd_state ust_consumerd_state
;
277 static enum consumerd_state kernel_consumerd_state
;
280 * Socket timeout for receiving and sending in seconds.
282 static int app_socket_timeout
;
284 /* Set in main() with the current page size. */
287 /* Application health monitoring */
288 struct health_app
*health_sessiond
;
290 /* JUL TCP port for registration. Used by the JUL thread. */
291 unsigned int jul_tcp_port
= DEFAULT_JUL_TCP_PORT
;
293 /* Am I root or not. */
294 int is_root
; /* Set to 1 if the daemon is running as root */
296 const char * const config_section_name
= "sessiond";
298 /* Load session thread information to operate. */
299 struct load_session_thread_data
*load_info
;
302 * Whether sessiond is ready for commands/health check requests.
303 * NR_LTTNG_SESSIOND_READY must match the number of calls to
304 * sessiond_notify_ready().
306 #define NR_LTTNG_SESSIOND_READY 3
307 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
309 /* Notify parents that we are ready for cmd and health check */
311 void sessiond_notify_ready(void)
313 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
315 * Notify parent pid that we are ready to accept command
316 * for client side. This ppid is the one from the
317 * external process that spawned us.
319 if (opt_sig_parent
) {
324 * Notify the parent of the fork() process that we are
327 if (opt_daemon
|| opt_background
) {
328 kill(child_ppid
, SIGUSR1
);
334 void setup_consumerd_path(void)
336 const char *bin
, *libdir
;
339 * Allow INSTALL_BIN_PATH to be used as a target path for the
340 * native architecture size consumer if CONFIG_CONSUMER*_PATH
341 * has not been defined.
343 #if (CAA_BITS_PER_LONG == 32)
344 if (!consumerd32_bin
[0]) {
345 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
347 if (!consumerd32_libdir
[0]) {
348 consumerd32_libdir
= INSTALL_LIB_PATH
;
350 #elif (CAA_BITS_PER_LONG == 64)
351 if (!consumerd64_bin
[0]) {
352 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
354 if (!consumerd64_libdir
[0]) {
355 consumerd64_libdir
= INSTALL_LIB_PATH
;
358 #error "Unknown bitness"
362 * runtime env. var. overrides the build default.
364 bin
= getenv("LTTNG_CONSUMERD32_BIN");
366 consumerd32_bin
= bin
;
368 bin
= getenv("LTTNG_CONSUMERD64_BIN");
370 consumerd64_bin
= bin
;
372 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
374 consumerd32_libdir
= libdir
;
376 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
378 consumerd64_libdir
= libdir
;
383 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
385 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
391 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
397 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
| LPOLLERR
);
409 * Check if the thread quit pipe was triggered.
411 * Return 1 if it was triggered else 0;
413 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
415 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
423 * Init thread quit pipe.
425 * Return -1 on error or 0 if all pipes are created.
427 static int init_thread_quit_pipe(void)
431 ret
= pipe(thread_quit_pipe
);
433 PERROR("thread quit pipe");
437 for (i
= 0; i
< 2; i
++) {
438 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
450 * Stop all threads by closing the thread quit pipe.
452 static void stop_threads(void)
456 /* Stopping all threads */
457 DBG("Terminating all threads");
458 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
460 ERR("write error on thread quit pipe");
463 /* Dispatch thread */
464 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
465 futex_nto1_wake(&ust_cmd_queue
.futex
);
469 * Close every consumer sockets.
471 static void close_consumer_sockets(void)
475 if (kconsumer_data
.err_sock
>= 0) {
476 ret
= close(kconsumer_data
.err_sock
);
478 PERROR("kernel consumer err_sock close");
481 if (ustconsumer32_data
.err_sock
>= 0) {
482 ret
= close(ustconsumer32_data
.err_sock
);
484 PERROR("UST consumerd32 err_sock close");
487 if (ustconsumer64_data
.err_sock
>= 0) {
488 ret
= close(ustconsumer64_data
.err_sock
);
490 PERROR("UST consumerd64 err_sock close");
493 if (kconsumer_data
.cmd_sock
>= 0) {
494 ret
= close(kconsumer_data
.cmd_sock
);
496 PERROR("kernel consumer cmd_sock close");
499 if (ustconsumer32_data
.cmd_sock
>= 0) {
500 ret
= close(ustconsumer32_data
.cmd_sock
);
502 PERROR("UST consumerd32 cmd_sock close");
505 if (ustconsumer64_data
.cmd_sock
>= 0) {
506 ret
= close(ustconsumer64_data
.cmd_sock
);
508 PERROR("UST consumerd64 cmd_sock close");
516 static void cleanup(void)
519 struct ltt_session
*sess
, *stmp
;
525 * Close the thread quit pipe. It has already done its job,
526 * since we are now called.
528 utils_close_pipe(thread_quit_pipe
);
531 * If opt_pidfile is undefined, the default file will be wiped when
532 * removing the rundir.
535 ret
= remove(opt_pidfile
);
537 PERROR("remove pidfile %s", opt_pidfile
);
541 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
544 snprintf(path
, PATH_MAX
,
546 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
547 DBG("Removing %s", path
);
550 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
551 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
);
552 DBG("Removing %s", path
);
556 snprintf(path
, PATH_MAX
,
557 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
559 DBG("Removing %s", path
);
562 snprintf(path
, PATH_MAX
,
563 DEFAULT_KCONSUMERD_PATH
,
565 DBG("Removing directory %s", path
);
568 /* ust consumerd 32 */
569 snprintf(path
, PATH_MAX
,
570 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
572 DBG("Removing %s", path
);
575 snprintf(path
, PATH_MAX
,
576 DEFAULT_USTCONSUMERD32_PATH
,
578 DBG("Removing directory %s", path
);
581 /* ust consumerd 64 */
582 snprintf(path
, PATH_MAX
,
583 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
585 DBG("Removing %s", path
);
588 snprintf(path
, PATH_MAX
,
589 DEFAULT_USTCONSUMERD64_PATH
,
591 DBG("Removing directory %s", path
);
595 * We do NOT rmdir rundir because there are other processes
596 * using it, for instance lttng-relayd, which can start in
597 * parallel with this teardown.
602 DBG("Cleaning up all sessions");
604 /* Destroy session list mutex */
605 if (session_list_ptr
!= NULL
) {
606 pthread_mutex_destroy(&session_list_ptr
->lock
);
608 /* Cleanup ALL session */
609 cds_list_for_each_entry_safe(sess
, stmp
,
610 &session_list_ptr
->head
, list
) {
611 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
615 DBG("Closing all UST sockets");
616 ust_app_clean_list();
617 buffer_reg_destroy_registries();
619 if (is_root
&& !opt_no_kernel
) {
620 DBG2("Closing kernel fd");
621 if (kernel_tracer_fd
>= 0) {
622 ret
= close(kernel_tracer_fd
);
627 DBG("Unloading kernel modules");
628 modprobe_remove_lttng_all();
631 close_consumer_sockets();
634 * If the override option is set, the pointer points to a *non* const thus
635 * freeing it even though the variable type is set to const.
637 if (tracing_group_name_override
) {
638 free((void *) tracing_group_name
);
640 if (consumerd32_bin_override
) {
641 free((void *) consumerd32_bin
);
643 if (consumerd64_bin_override
) {
644 free((void *) consumerd64_bin
);
646 if (consumerd32_libdir_override
) {
647 free((void *) consumerd32_libdir
);
649 if (consumerd64_libdir_override
) {
650 free((void *) consumerd64_libdir
);
657 if (opt_load_session_path
) {
658 free(opt_load_session_path
);
662 load_session_destroy_data(load_info
);
667 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
668 "Matthew, BEET driven development works!%c[%dm",
669 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
674 * Send data on a unix socket using the liblttsessiondcomm API.
676 * Return lttcomm error code.
678 static int send_unix_sock(int sock
, void *buf
, size_t len
)
680 /* Check valid length */
685 return lttcomm_send_unix_sock(sock
, buf
, len
);
689 * Free memory of a command context structure.
691 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
693 DBG("Clean command context structure");
695 if ((*cmd_ctx
)->llm
) {
696 free((*cmd_ctx
)->llm
);
698 if ((*cmd_ctx
)->lsm
) {
699 free((*cmd_ctx
)->lsm
);
707 * Notify UST applications using the shm mmap futex.
709 static int notify_ust_apps(int active
)
713 DBG("Notifying applications of session daemon state: %d", active
);
715 /* See shm.c for this call implying mmap, shm and futex calls */
716 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
717 if (wait_shm_mmap
== NULL
) {
721 /* Wake waiting process */
722 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
724 /* Apps notified successfully */
732 * Setup the outgoing data buffer for the response (llm) by allocating the
733 * right amount of memory and copying the original information from the lsm
736 * Return total size of the buffer pointed by buf.
738 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
744 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
745 if (cmd_ctx
->llm
== NULL
) {
751 /* Copy common data */
752 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
753 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
755 cmd_ctx
->llm
->data_size
= size
;
756 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
765 * Update the kernel poll set of all channel fd available over all tracing
766 * session. Add the wakeup pipe at the end of the set.
768 static int update_kernel_poll(struct lttng_poll_event
*events
)
771 struct ltt_session
*session
;
772 struct ltt_kernel_channel
*channel
;
774 DBG("Updating kernel poll set");
777 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
778 session_lock(session
);
779 if (session
->kernel_session
== NULL
) {
780 session_unlock(session
);
784 cds_list_for_each_entry(channel
,
785 &session
->kernel_session
->channel_list
.head
, list
) {
786 /* Add channel fd to the kernel poll set */
787 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
789 session_unlock(session
);
792 DBG("Channel fd %d added to kernel set", channel
->fd
);
794 session_unlock(session
);
796 session_unlock_list();
801 session_unlock_list();
806 * Find the channel fd from 'fd' over all tracing session. When found, check
807 * for new channel stream and send those stream fds to the kernel consumer.
809 * Useful for CPU hotplug feature.
811 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
814 struct ltt_session
*session
;
815 struct ltt_kernel_session
*ksess
;
816 struct ltt_kernel_channel
*channel
;
818 DBG("Updating kernel streams for channel fd %d", fd
);
821 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
822 session_lock(session
);
823 if (session
->kernel_session
== NULL
) {
824 session_unlock(session
);
827 ksess
= session
->kernel_session
;
829 cds_list_for_each_entry(channel
, &ksess
->channel_list
.head
, list
) {
830 if (channel
->fd
== fd
) {
831 DBG("Channel found, updating kernel streams");
832 ret
= kernel_open_channel_stream(channel
);
836 /* Update the stream global counter */
837 ksess
->stream_count_global
+= ret
;
840 * Have we already sent fds to the consumer? If yes, it means
841 * that tracing is started so it is safe to send our updated
844 if (ksess
->consumer_fds_sent
== 1 && ksess
->consumer
!= NULL
) {
845 struct lttng_ht_iter iter
;
846 struct consumer_socket
*socket
;
849 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
850 &iter
.iter
, socket
, node
.node
) {
851 pthread_mutex_lock(socket
->lock
);
852 ret
= kernel_consumer_send_channel_stream(socket
,
854 session
->output_traces
? 1 : 0);
855 pthread_mutex_unlock(socket
->lock
);
866 session_unlock(session
);
868 session_unlock_list();
872 session_unlock(session
);
873 session_unlock_list();
878 * For each tracing session, update newly registered apps. The session list
879 * lock MUST be acquired before calling this.
881 static void update_ust_app(int app_sock
)
883 struct ltt_session
*sess
, *stmp
;
885 /* Consumer is in an ERROR state. Stop any application update. */
886 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
887 /* Stop the update process since the consumer is dead. */
891 /* For all tracing session(s) */
892 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
894 if (sess
->ust_session
) {
895 ust_app_global_update(sess
->ust_session
, app_sock
);
897 session_unlock(sess
);
902 * This thread manage event coming from the kernel.
904 * Features supported in this thread:
907 static void *thread_manage_kernel(void *data
)
909 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
910 uint32_t revents
, nb_fd
;
912 struct lttng_poll_event events
;
914 DBG("[thread] Thread manage kernel started");
916 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
919 * This first step of the while is to clean this structure which could free
920 * non NULL pointers so initialize it before the loop.
922 lttng_poll_init(&events
);
924 if (testpoint(sessiond_thread_manage_kernel
)) {
925 goto error_testpoint
;
928 health_code_update();
930 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
931 goto error_testpoint
;
935 health_code_update();
937 if (update_poll_flag
== 1) {
938 /* Clean events object. We are about to populate it again. */
939 lttng_poll_clean(&events
);
941 ret
= sessiond_set_thread_pollset(&events
, 2);
943 goto error_poll_create
;
946 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
951 /* This will add the available kernel channel if any. */
952 ret
= update_kernel_poll(&events
);
956 update_poll_flag
= 0;
959 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
961 /* Poll infinite value of time */
964 ret
= lttng_poll_wait(&events
, -1);
968 * Restart interrupted system call.
970 if (errno
== EINTR
) {
974 } else if (ret
== 0) {
975 /* Should not happen since timeout is infinite */
976 ERR("Return value of poll is 0 with an infinite timeout.\n"
977 "This should not have happened! Continuing...");
983 for (i
= 0; i
< nb_fd
; i
++) {
984 /* Fetch once the poll data */
985 revents
= LTTNG_POLL_GETEV(&events
, i
);
986 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
988 health_code_update();
990 /* Thread quit pipe has been closed. Killing thread. */
991 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
997 /* Check for data on kernel pipe */
998 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
999 (void) lttng_read(kernel_poll_pipe
[0],
1002 * Ret value is useless here, if this pipe gets any actions an
1003 * update is required anyway.
1005 update_poll_flag
= 1;
1009 * New CPU detected by the kernel. Adding kernel stream to
1010 * kernel session and updating the kernel consumer
1012 if (revents
& LPOLLIN
) {
1013 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1019 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
1020 * and unregister kernel stream at this point.
1029 lttng_poll_clean(&events
);
1032 utils_close_pipe(kernel_poll_pipe
);
1033 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1036 ERR("Health error occurred in %s", __func__
);
1037 WARN("Kernel thread died unexpectedly. "
1038 "Kernel tracing can continue but CPU hotplug is disabled.");
1040 health_unregister(health_sessiond
);
1041 DBG("Kernel thread dying");
1046 * Signal pthread condition of the consumer data that the thread.
1048 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1050 pthread_mutex_lock(&data
->cond_mutex
);
1053 * The state is set before signaling. It can be any value, it's the waiter
1054 * job to correctly interpret this condition variable associated to the
1055 * consumer pthread_cond.
1057 * A value of 0 means that the corresponding thread of the consumer data
1058 * was not started. 1 indicates that the thread has started and is ready
1059 * for action. A negative value means that there was an error during the
1062 data
->consumer_thread_is_ready
= state
;
1063 (void) pthread_cond_signal(&data
->cond
);
1065 pthread_mutex_unlock(&data
->cond_mutex
);
1069 * This thread manage the consumer error sent back to the session daemon.
1071 static void *thread_manage_consumer(void *data
)
1073 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1074 uint32_t revents
, nb_fd
;
1075 enum lttcomm_return_code code
;
1076 struct lttng_poll_event events
;
1077 struct consumer_data
*consumer_data
= data
;
1079 DBG("[thread] Manage consumer started");
1081 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1083 health_code_update();
1086 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1087 * metadata_sock. Nothing more will be added to this poll set.
1089 ret
= sessiond_set_thread_pollset(&events
, 3);
1095 * The error socket here is already in a listening state which was done
1096 * just before spawning this thread to avoid a race between the consumer
1097 * daemon exec trying to connect and the listen() call.
1099 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1104 health_code_update();
1106 /* Infinite blocking call, waiting for transmission */
1108 health_poll_entry();
1110 if (testpoint(sessiond_thread_manage_consumer
)) {
1114 ret
= lttng_poll_wait(&events
, -1);
1118 * Restart interrupted system call.
1120 if (errno
== EINTR
) {
1128 for (i
= 0; i
< nb_fd
; i
++) {
1129 /* Fetch once the poll data */
1130 revents
= LTTNG_POLL_GETEV(&events
, i
);
1131 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1133 health_code_update();
1135 /* Thread quit pipe has been closed. Killing thread. */
1136 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1142 /* Event on the registration socket */
1143 if (pollfd
== consumer_data
->err_sock
) {
1144 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1145 ERR("consumer err socket poll error");
1151 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1157 * Set the CLOEXEC flag. Return code is useless because either way, the
1160 (void) utils_set_fd_cloexec(sock
);
1162 health_code_update();
1164 DBG2("Receiving code from consumer err_sock");
1166 /* Getting status code from kconsumerd */
1167 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1168 sizeof(enum lttcomm_return_code
));
1173 health_code_update();
1174 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1175 /* Connect both socket, command and metadata. */
1176 consumer_data
->cmd_sock
=
1177 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1178 consumer_data
->metadata_fd
=
1179 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1180 if (consumer_data
->cmd_sock
< 0
1181 || consumer_data
->metadata_fd
< 0) {
1182 PERROR("consumer connect cmd socket");
1183 /* On error, signal condition and quit. */
1184 signal_consumer_condition(consumer_data
, -1);
1187 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1188 /* Create metadata socket lock. */
1189 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1190 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1191 PERROR("zmalloc pthread mutex");
1195 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1197 signal_consumer_condition(consumer_data
, 1);
1198 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1199 DBG("Consumer metadata socket ready (fd: %d)",
1200 consumer_data
->metadata_fd
);
1202 ERR("consumer error when waiting for SOCK_READY : %s",
1203 lttcomm_get_readable_code(-code
));
1207 /* Remove the consumerd error sock since we've established a connexion */
1208 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1213 /* Add new accepted error socket. */
1214 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1219 /* Add metadata socket that is successfully connected. */
1220 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1221 LPOLLIN
| LPOLLRDHUP
);
1226 health_code_update();
1228 /* Infinite blocking call, waiting for transmission */
1231 health_poll_entry();
1232 ret
= lttng_poll_wait(&events
, -1);
1236 * Restart interrupted system call.
1238 if (errno
== EINTR
) {
1246 for (i
= 0; i
< nb_fd
; i
++) {
1247 /* Fetch once the poll data */
1248 revents
= LTTNG_POLL_GETEV(&events
, i
);
1249 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1251 health_code_update();
1253 /* Thread quit pipe has been closed. Killing thread. */
1254 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1260 if (pollfd
== sock
) {
1261 /* Event on the consumerd socket */
1262 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1263 ERR("consumer err socket second poll error");
1266 health_code_update();
1267 /* Wait for any kconsumerd error */
1268 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1269 sizeof(enum lttcomm_return_code
));
1271 ERR("consumer closed the command socket");
1275 ERR("consumer return code : %s",
1276 lttcomm_get_readable_code(-code
));
1279 } else if (pollfd
== consumer_data
->metadata_fd
) {
1280 /* UST metadata requests */
1281 ret
= ust_consumer_metadata_request(
1282 &consumer_data
->metadata_sock
);
1284 ERR("Handling metadata request");
1289 ERR("Unknown pollfd");
1293 health_code_update();
1299 * We lock here because we are about to close the sockets and some other
1300 * thread might be using them so get exclusive access which will abort all
1301 * other consumer command by other threads.
1303 pthread_mutex_lock(&consumer_data
->lock
);
1305 /* Immediately set the consumerd state to stopped */
1306 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1307 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1308 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1309 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1310 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1312 /* Code flow error... */
1316 if (consumer_data
->err_sock
>= 0) {
1317 ret
= close(consumer_data
->err_sock
);
1321 consumer_data
->err_sock
= -1;
1323 if (consumer_data
->cmd_sock
>= 0) {
1324 ret
= close(consumer_data
->cmd_sock
);
1328 consumer_data
->cmd_sock
= -1;
1330 if (consumer_data
->metadata_sock
.fd_ptr
&&
1331 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1332 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1344 unlink(consumer_data
->err_unix_sock_path
);
1345 unlink(consumer_data
->cmd_unix_sock_path
);
1346 consumer_data
->pid
= 0;
1347 pthread_mutex_unlock(&consumer_data
->lock
);
1349 /* Cleanup metadata socket mutex. */
1350 if (consumer_data
->metadata_sock
.lock
) {
1351 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1352 free(consumer_data
->metadata_sock
.lock
);
1354 lttng_poll_clean(&events
);
1358 ERR("Health error occurred in %s", __func__
);
1360 health_unregister(health_sessiond
);
1361 DBG("consumer thread cleanup completed");
1367 * This thread manage application communication.
1369 static void *thread_manage_apps(void *data
)
1371 int i
, ret
, pollfd
, err
= -1;
1373 uint32_t revents
, nb_fd
;
1374 struct lttng_poll_event events
;
1376 DBG("[thread] Manage application started");
1378 rcu_register_thread();
1379 rcu_thread_online();
1381 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1383 if (testpoint(sessiond_thread_manage_apps
)) {
1384 goto error_testpoint
;
1387 health_code_update();
1389 ret
= sessiond_set_thread_pollset(&events
, 2);
1391 goto error_poll_create
;
1394 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1399 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1403 health_code_update();
1406 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1408 /* Inifinite blocking call, waiting for transmission */
1410 health_poll_entry();
1411 ret
= lttng_poll_wait(&events
, -1);
1415 * Restart interrupted system call.
1417 if (errno
== EINTR
) {
1425 for (i
= 0; i
< nb_fd
; i
++) {
1426 /* Fetch once the poll data */
1427 revents
= LTTNG_POLL_GETEV(&events
, i
);
1428 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1430 health_code_update();
1432 /* Thread quit pipe has been closed. Killing thread. */
1433 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1439 /* Inspect the apps cmd pipe */
1440 if (pollfd
== apps_cmd_pipe
[0]) {
1441 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1442 ERR("Apps command pipe error");
1444 } else if (revents
& LPOLLIN
) {
1448 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1449 if (size_ret
< sizeof(sock
)) {
1450 PERROR("read apps cmd pipe");
1454 health_code_update();
1457 * We only monitor the error events of the socket. This
1458 * thread does not handle any incoming data from UST
1461 ret
= lttng_poll_add(&events
, sock
,
1462 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1467 DBG("Apps with sock %d added to poll set", sock
);
1471 * At this point, we know that a registered application made
1472 * the event at poll_wait.
1474 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1475 /* Removing from the poll set */
1476 ret
= lttng_poll_del(&events
, pollfd
);
1481 /* Socket closed on remote end. */
1482 ust_app_unregister(pollfd
);
1486 health_code_update();
1492 lttng_poll_clean(&events
);
1495 utils_close_pipe(apps_cmd_pipe
);
1496 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1499 * We don't clean the UST app hash table here since already registered
1500 * applications can still be controlled so let them be until the session
1501 * daemon dies or the applications stop.
1506 ERR("Health error occurred in %s", __func__
);
1508 health_unregister(health_sessiond
);
1509 DBG("Application communication apps thread cleanup complete");
1510 rcu_thread_offline();
1511 rcu_unregister_thread();
1516 * Send a socket to a thread This is called from the dispatch UST registration
1517 * thread once all sockets are set for the application.
1519 * The sock value can be invalid, we don't really care, the thread will handle
1520 * it and make the necessary cleanup if so.
1522 * On success, return 0 else a negative value being the errno message of the
1525 static int send_socket_to_thread(int fd
, int sock
)
1530 * It's possible that the FD is set as invalid with -1 concurrently just
1531 * before calling this function being a shutdown state of the thread.
1538 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1539 if (ret
< sizeof(sock
)) {
1540 PERROR("write apps pipe %d", fd
);
1547 /* All good. Don't send back the write positive ret value. */
1554 * Sanitize the wait queue of the dispatch registration thread meaning removing
1555 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1556 * notify socket is never received.
1558 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1560 int ret
, nb_fd
= 0, i
;
1561 unsigned int fd_added
= 0;
1562 struct lttng_poll_event events
;
1563 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1567 lttng_poll_init(&events
);
1569 /* Just skip everything for an empty queue. */
1570 if (!wait_queue
->count
) {
1574 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1579 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1580 &wait_queue
->head
, head
) {
1581 assert(wait_node
->app
);
1582 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1583 LPOLLHUP
| LPOLLERR
);
1596 * Poll but don't block so we can quickly identify the faulty events and
1597 * clean them afterwards from the wait queue.
1599 ret
= lttng_poll_wait(&events
, 0);
1605 for (i
= 0; i
< nb_fd
; i
++) {
1606 /* Get faulty FD. */
1607 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1608 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1610 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1611 &wait_queue
->head
, head
) {
1612 if (pollfd
== wait_node
->app
->sock
&&
1613 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1614 cds_list_del(&wait_node
->head
);
1615 wait_queue
->count
--;
1616 ust_app_destroy(wait_node
->app
);
1624 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1628 lttng_poll_clean(&events
);
1632 lttng_poll_clean(&events
);
1634 ERR("Unable to sanitize wait queue");
1639 * Dispatch request from the registration threads to the application
1640 * communication thread.
1642 static void *thread_dispatch_ust_registration(void *data
)
1645 struct cds_wfq_node
*node
;
1646 struct ust_command
*ust_cmd
= NULL
;
1647 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1648 struct ust_reg_wait_queue wait_queue
= {
1652 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1654 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1655 goto error_testpoint
;
1658 health_code_update();
1660 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1662 DBG("[thread] Dispatch UST command started");
1664 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1665 health_code_update();
1667 /* Atomically prepare the queue futex */
1668 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1671 struct ust_app
*app
= NULL
;
1675 * Make sure we don't have node(s) that have hung up before receiving
1676 * the notify socket. This is to clean the list in order to avoid
1677 * memory leaks from notify socket that are never seen.
1679 sanitize_wait_queue(&wait_queue
);
1681 health_code_update();
1682 /* Dequeue command for registration */
1683 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1685 DBG("Woken up but nothing in the UST command queue");
1686 /* Continue thread execution */
1690 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1692 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1693 " gid:%d sock:%d name:%s (version %d.%d)",
1694 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1695 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1696 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1697 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1699 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1700 wait_node
= zmalloc(sizeof(*wait_node
));
1702 PERROR("zmalloc wait_node dispatch");
1703 ret
= close(ust_cmd
->sock
);
1705 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1707 lttng_fd_put(LTTNG_FD_APPS
, 1);
1711 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1713 /* Create application object if socket is CMD. */
1714 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1716 if (!wait_node
->app
) {
1717 ret
= close(ust_cmd
->sock
);
1719 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1721 lttng_fd_put(LTTNG_FD_APPS
, 1);
1727 * Add application to the wait queue so we can set the notify
1728 * socket before putting this object in the global ht.
1730 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1735 * We have to continue here since we don't have the notify
1736 * socket and the application MUST be added to the hash table
1737 * only at that moment.
1742 * Look for the application in the local wait queue and set the
1743 * notify socket if found.
1745 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1746 &wait_queue
.head
, head
) {
1747 health_code_update();
1748 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1749 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1750 cds_list_del(&wait_node
->head
);
1752 app
= wait_node
->app
;
1754 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1760 * With no application at this stage the received socket is
1761 * basically useless so close it before we free the cmd data
1762 * structure for good.
1765 ret
= close(ust_cmd
->sock
);
1767 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1769 lttng_fd_put(LTTNG_FD_APPS
, 1);
1776 * @session_lock_list
1778 * Lock the global session list so from the register up to the
1779 * registration done message, no thread can see the application
1780 * and change its state.
1782 session_lock_list();
1786 * Add application to the global hash table. This needs to be
1787 * done before the update to the UST registry can locate the
1792 /* Set app version. This call will print an error if needed. */
1793 (void) ust_app_version(app
);
1795 /* Send notify socket through the notify pipe. */
1796 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1800 session_unlock_list();
1802 * No notify thread, stop the UST tracing. However, this is
1803 * not an internal error of the this thread thus setting
1804 * the health error code to a normal exit.
1811 * Update newly registered application with the tracing
1812 * registry info already enabled information.
1814 update_ust_app(app
->sock
);
1817 * Don't care about return value. Let the manage apps threads
1818 * handle app unregistration upon socket close.
1820 (void) ust_app_register_done(app
->sock
);
1823 * Even if the application socket has been closed, send the app
1824 * to the thread and unregistration will take place at that
1827 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1830 session_unlock_list();
1832 * No apps. thread, stop the UST tracing. However, this is
1833 * not an internal error of the this thread thus setting
1834 * the health error code to a normal exit.
1841 session_unlock_list();
1843 } while (node
!= NULL
);
1845 health_poll_entry();
1846 /* Futex wait on queue. Blocking call on futex() */
1847 futex_nto1_wait(&ust_cmd_queue
.futex
);
1850 /* Normal exit, no error */
1854 /* Clean up wait queue. */
1855 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1856 &wait_queue
.head
, head
) {
1857 cds_list_del(&wait_node
->head
);
1863 DBG("Dispatch thread dying");
1866 ERR("Health error occurred in %s", __func__
);
1868 health_unregister(health_sessiond
);
1873 * This thread manage application registration.
1875 static void *thread_registration_apps(void *data
)
1877 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1878 uint32_t revents
, nb_fd
;
1879 struct lttng_poll_event events
;
1881 * Get allocated in this thread, enqueued to a global queue, dequeued and
1882 * freed in the manage apps thread.
1884 struct ust_command
*ust_cmd
= NULL
;
1886 DBG("[thread] Manage application registration started");
1888 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1890 if (testpoint(sessiond_thread_registration_apps
)) {
1891 goto error_testpoint
;
1894 ret
= lttcomm_listen_unix_sock(apps_sock
);
1900 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1901 * more will be added to this poll set.
1903 ret
= sessiond_set_thread_pollset(&events
, 2);
1905 goto error_create_poll
;
1908 /* Add the application registration socket */
1909 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1911 goto error_poll_add
;
1914 /* Notify all applications to register */
1915 ret
= notify_ust_apps(1);
1917 ERR("Failed to notify applications or create the wait shared memory.\n"
1918 "Execution continues but there might be problem for already\n"
1919 "running applications that wishes to register.");
1923 DBG("Accepting application registration");
1925 /* Inifinite blocking call, waiting for transmission */
1927 health_poll_entry();
1928 ret
= lttng_poll_wait(&events
, -1);
1932 * Restart interrupted system call.
1934 if (errno
== EINTR
) {
1942 for (i
= 0; i
< nb_fd
; i
++) {
1943 health_code_update();
1945 /* Fetch once the poll data */
1946 revents
= LTTNG_POLL_GETEV(&events
, i
);
1947 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1949 /* Thread quit pipe has been closed. Killing thread. */
1950 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1956 /* Event on the registration socket */
1957 if (pollfd
== apps_sock
) {
1958 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1959 ERR("Register apps socket poll error");
1961 } else if (revents
& LPOLLIN
) {
1962 sock
= lttcomm_accept_unix_sock(apps_sock
);
1968 * Set socket timeout for both receiving and ending.
1969 * app_socket_timeout is in seconds, whereas
1970 * lttcomm_setsockopt_rcv_timeout and
1971 * lttcomm_setsockopt_snd_timeout expect msec as
1974 (void) lttcomm_setsockopt_rcv_timeout(sock
,
1975 app_socket_timeout
* 1000);
1976 (void) lttcomm_setsockopt_snd_timeout(sock
,
1977 app_socket_timeout
* 1000);
1980 * Set the CLOEXEC flag. Return code is useless because
1981 * either way, the show must go on.
1983 (void) utils_set_fd_cloexec(sock
);
1985 /* Create UST registration command for enqueuing */
1986 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1987 if (ust_cmd
== NULL
) {
1988 PERROR("ust command zmalloc");
1993 * Using message-based transmissions to ensure we don't
1994 * have to deal with partially received messages.
1996 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1998 ERR("Exhausted file descriptors allowed for applications.");
2008 health_code_update();
2009 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2012 /* Close socket of the application. */
2017 lttng_fd_put(LTTNG_FD_APPS
, 1);
2021 health_code_update();
2023 ust_cmd
->sock
= sock
;
2026 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2027 " gid:%d sock:%d name:%s (version %d.%d)",
2028 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2029 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2030 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2031 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2034 * Lock free enqueue the registration request. The red pill
2035 * has been taken! This apps will be part of the *system*.
2037 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
2040 * Wake the registration queue futex. Implicit memory
2041 * barrier with the exchange in cds_wfq_enqueue.
2043 futex_nto1_wake(&ust_cmd_queue
.futex
);
2051 /* Notify that the registration thread is gone */
2054 if (apps_sock
>= 0) {
2055 ret
= close(apps_sock
);
2065 lttng_fd_put(LTTNG_FD_APPS
, 1);
2067 unlink(apps_unix_sock_path
);
2070 lttng_poll_clean(&events
);
2074 DBG("UST Registration thread cleanup complete");
2077 ERR("Health error occurred in %s", __func__
);
2079 health_unregister(health_sessiond
);
2085 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2086 * exec or it will fails.
2088 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2091 struct timespec timeout
;
2093 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2094 consumer_data
->consumer_thread_is_ready
= 0;
2096 /* Setup pthread condition */
2097 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2100 PERROR("pthread_condattr_init consumer data");
2105 * Set the monotonic clock in order to make sure we DO NOT jump in time
2106 * between the clock_gettime() call and the timedwait call. See bug #324
2107 * for a more details and how we noticed it.
2109 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2112 PERROR("pthread_condattr_setclock consumer data");
2116 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2119 PERROR("pthread_cond_init consumer data");
2123 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2126 PERROR("pthread_create consumer");
2131 /* We are about to wait on a pthread condition */
2132 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2134 /* Get time for sem_timedwait absolute timeout */
2135 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2137 * Set the timeout for the condition timed wait even if the clock gettime
2138 * call fails since we might loop on that call and we want to avoid to
2139 * increment the timeout too many times.
2141 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2144 * The following loop COULD be skipped in some conditions so this is why we
2145 * set ret to 0 in order to make sure at least one round of the loop is
2151 * Loop until the condition is reached or when a timeout is reached. Note
2152 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2153 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2154 * possible. This loop does not take any chances and works with both of
2157 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2158 if (clock_ret
< 0) {
2159 PERROR("clock_gettime spawn consumer");
2160 /* Infinite wait for the consumerd thread to be ready */
2161 ret
= pthread_cond_wait(&consumer_data
->cond
,
2162 &consumer_data
->cond_mutex
);
2164 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2165 &consumer_data
->cond_mutex
, &timeout
);
2169 /* Release the pthread condition */
2170 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2174 if (ret
== ETIMEDOUT
) {
2178 * Call has timed out so we kill the kconsumerd_thread and return
2181 ERR("Condition timed out. The consumer thread was never ready."
2183 pth_ret
= pthread_cancel(consumer_data
->thread
);
2185 PERROR("pthread_cancel consumer thread");
2188 PERROR("pthread_cond_wait failed consumer thread");
2190 /* Caller is expecting a negative value on failure. */
2195 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2196 if (consumer_data
->pid
== 0) {
2197 ERR("Consumerd did not start");
2198 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2201 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2210 * Join consumer thread
2212 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2216 /* Consumer pid must be a real one. */
2217 if (consumer_data
->pid
> 0) {
2219 ret
= kill(consumer_data
->pid
, SIGTERM
);
2221 ERR("Error killing consumer daemon");
2224 return pthread_join(consumer_data
->thread
, &status
);
2231 * Fork and exec a consumer daemon (consumerd).
2233 * Return pid if successful else -1.
2235 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2239 const char *consumer_to_use
;
2240 const char *verbosity
;
2243 DBG("Spawning consumerd");
2250 if (opt_verbose_consumer
) {
2251 verbosity
= "--verbose";
2252 } else if (lttng_opt_quiet
) {
2253 verbosity
= "--quiet";
2258 switch (consumer_data
->type
) {
2259 case LTTNG_CONSUMER_KERNEL
:
2261 * Find out which consumerd to execute. We will first try the
2262 * 64-bit path, then the sessiond's installation directory, and
2263 * fallback on the 32-bit one,
2265 DBG3("Looking for a kernel consumer at these locations:");
2266 DBG3(" 1) %s", consumerd64_bin
);
2267 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2268 DBG3(" 3) %s", consumerd32_bin
);
2269 if (stat(consumerd64_bin
, &st
) == 0) {
2270 DBG3("Found location #1");
2271 consumer_to_use
= consumerd64_bin
;
2272 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2273 DBG3("Found location #2");
2274 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2275 } else if (stat(consumerd32_bin
, &st
) == 0) {
2276 DBG3("Found location #3");
2277 consumer_to_use
= consumerd32_bin
;
2279 DBG("Could not find any valid consumerd executable");
2283 DBG("Using kernel consumer at: %s", consumer_to_use
);
2284 ret
= execl(consumer_to_use
,
2285 "lttng-consumerd", verbosity
, "-k",
2286 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2287 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2288 "--group", tracing_group_name
,
2291 case LTTNG_CONSUMER64_UST
:
2293 char *tmpnew
= NULL
;
2295 if (consumerd64_libdir
[0] != '\0') {
2299 tmp
= getenv("LD_LIBRARY_PATH");
2303 tmplen
= strlen("LD_LIBRARY_PATH=")
2304 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2305 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2310 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2311 strcat(tmpnew
, consumerd64_libdir
);
2312 if (tmp
[0] != '\0') {
2313 strcat(tmpnew
, ":");
2314 strcat(tmpnew
, tmp
);
2316 ret
= putenv(tmpnew
);
2323 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2324 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2325 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2326 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2327 "--group", tracing_group_name
,
2329 if (consumerd64_libdir
[0] != '\0') {
2334 case LTTNG_CONSUMER32_UST
:
2336 char *tmpnew
= NULL
;
2338 if (consumerd32_libdir
[0] != '\0') {
2342 tmp
= getenv("LD_LIBRARY_PATH");
2346 tmplen
= strlen("LD_LIBRARY_PATH=")
2347 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2348 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2353 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2354 strcat(tmpnew
, consumerd32_libdir
);
2355 if (tmp
[0] != '\0') {
2356 strcat(tmpnew
, ":");
2357 strcat(tmpnew
, tmp
);
2359 ret
= putenv(tmpnew
);
2366 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2367 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2368 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2369 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2370 "--group", tracing_group_name
,
2372 if (consumerd32_libdir
[0] != '\0') {
2378 PERROR("unknown consumer type");
2382 PERROR("Consumer execl()");
2384 /* Reaching this point, we got a failure on our execl(). */
2386 } else if (pid
> 0) {
2389 PERROR("start consumer fork");
2397 * Spawn the consumerd daemon and session daemon thread.
2399 static int start_consumerd(struct consumer_data
*consumer_data
)
2404 * Set the listen() state on the socket since there is a possible race
2405 * between the exec() of the consumer daemon and this call if place in the
2406 * consumer thread. See bug #366 for more details.
2408 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2413 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2414 if (consumer_data
->pid
!= 0) {
2415 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2419 ret
= spawn_consumerd(consumer_data
);
2421 ERR("Spawning consumerd failed");
2422 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2426 /* Setting up the consumer_data pid */
2427 consumer_data
->pid
= ret
;
2428 DBG2("Consumer pid %d", consumer_data
->pid
);
2429 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2431 DBG2("Spawning consumer control thread");
2432 ret
= spawn_consumer_thread(consumer_data
);
2434 ERR("Fatal error spawning consumer control thread");
2442 /* Cleanup already created sockets on error. */
2443 if (consumer_data
->err_sock
>= 0) {
2446 err
= close(consumer_data
->err_sock
);
2448 PERROR("close consumer data error socket");
2455 * Setup necessary data for kernel tracer action.
2457 static int init_kernel_tracer(void)
2461 /* Modprobe lttng kernel modules */
2462 ret
= modprobe_lttng_control();
2467 /* Open debugfs lttng */
2468 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2469 if (kernel_tracer_fd
< 0) {
2470 DBG("Failed to open %s", module_proc_lttng
);
2475 /* Validate kernel version */
2476 ret
= kernel_validate_version(kernel_tracer_fd
);
2481 ret
= modprobe_lttng_data();
2486 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2490 modprobe_remove_lttng_control();
2491 ret
= close(kernel_tracer_fd
);
2495 kernel_tracer_fd
= -1;
2496 return LTTNG_ERR_KERN_VERSION
;
2499 ret
= close(kernel_tracer_fd
);
2505 modprobe_remove_lttng_control();
2508 WARN("No kernel tracer available");
2509 kernel_tracer_fd
= -1;
2511 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2513 return LTTNG_ERR_KERN_NA
;
2519 * Copy consumer output from the tracing session to the domain session. The
2520 * function also applies the right modification on a per domain basis for the
2521 * trace files destination directory.
2523 * Should *NOT* be called with RCU read-side lock held.
2525 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2528 const char *dir_name
;
2529 struct consumer_output
*consumer
;
2532 assert(session
->consumer
);
2535 case LTTNG_DOMAIN_KERNEL
:
2536 DBG3("Copying tracing session consumer output in kernel session");
2538 * XXX: We should audit the session creation and what this function
2539 * does "extra" in order to avoid a destroy since this function is used
2540 * in the domain session creation (kernel and ust) only. Same for UST
2543 if (session
->kernel_session
->consumer
) {
2544 consumer_destroy_output(session
->kernel_session
->consumer
);
2546 session
->kernel_session
->consumer
=
2547 consumer_copy_output(session
->consumer
);
2548 /* Ease our life a bit for the next part */
2549 consumer
= session
->kernel_session
->consumer
;
2550 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2552 case LTTNG_DOMAIN_JUL
:
2553 case LTTNG_DOMAIN_UST
:
2554 DBG3("Copying tracing session consumer output in UST session");
2555 if (session
->ust_session
->consumer
) {
2556 consumer_destroy_output(session
->ust_session
->consumer
);
2558 session
->ust_session
->consumer
=
2559 consumer_copy_output(session
->consumer
);
2560 /* Ease our life a bit for the next part */
2561 consumer
= session
->ust_session
->consumer
;
2562 dir_name
= DEFAULT_UST_TRACE_DIR
;
2565 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2569 /* Append correct directory to subdir */
2570 strncat(consumer
->subdir
, dir_name
,
2571 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2572 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2581 * Create an UST session and add it to the session ust list.
2583 * Should *NOT* be called with RCU read-side lock held.
2585 static int create_ust_session(struct ltt_session
*session
,
2586 struct lttng_domain
*domain
)
2589 struct ltt_ust_session
*lus
= NULL
;
2593 assert(session
->consumer
);
2595 switch (domain
->type
) {
2596 case LTTNG_DOMAIN_JUL
:
2597 case LTTNG_DOMAIN_UST
:
2600 ERR("Unknown UST domain on create session %d", domain
->type
);
2601 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2605 DBG("Creating UST session");
2607 lus
= trace_ust_create_session(session
->id
);
2609 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2613 lus
->uid
= session
->uid
;
2614 lus
->gid
= session
->gid
;
2615 lus
->output_traces
= session
->output_traces
;
2616 lus
->snapshot_mode
= session
->snapshot_mode
;
2617 lus
->live_timer_interval
= session
->live_timer
;
2618 session
->ust_session
= lus
;
2620 /* Copy session output to the newly created UST session */
2621 ret
= copy_session_consumer(domain
->type
, session
);
2622 if (ret
!= LTTNG_OK
) {
2630 session
->ust_session
= NULL
;
2635 * Create a kernel tracer session then create the default channel.
2637 static int create_kernel_session(struct ltt_session
*session
)
2641 DBG("Creating kernel session");
2643 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2645 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2649 /* Code flow safety */
2650 assert(session
->kernel_session
);
2652 /* Copy session output to the newly created Kernel session */
2653 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2654 if (ret
!= LTTNG_OK
) {
2658 /* Create directory(ies) on local filesystem. */
2659 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2660 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2661 ret
= run_as_mkdir_recursive(
2662 session
->kernel_session
->consumer
->dst
.trace_path
,
2663 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2665 if (ret
!= -EEXIST
) {
2666 ERR("Trace directory creation error");
2672 session
->kernel_session
->uid
= session
->uid
;
2673 session
->kernel_session
->gid
= session
->gid
;
2674 session
->kernel_session
->output_traces
= session
->output_traces
;
2675 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2680 trace_kernel_destroy_session(session
->kernel_session
);
2681 session
->kernel_session
= NULL
;
2686 * Count number of session permitted by uid/gid.
2688 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2691 struct ltt_session
*session
;
2693 DBG("Counting number of available session for UID %d GID %d",
2695 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2697 * Only list the sessions the user can control.
2699 if (!session_access_ok(session
, uid
, gid
)) {
2708 * Process the command requested by the lttng client within the command
2709 * context structure. This function make sure that the return structure (llm)
2710 * is set and ready for transmission before returning.
2712 * Return any error encountered or 0 for success.
2714 * "sock" is only used for special-case var. len data.
2716 * Should *NOT* be called with RCU read-side lock held.
2718 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2722 int need_tracing_session
= 1;
2725 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2729 switch (cmd_ctx
->lsm
->cmd_type
) {
2730 case LTTNG_CREATE_SESSION
:
2731 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2732 case LTTNG_CREATE_SESSION_LIVE
:
2733 case LTTNG_DESTROY_SESSION
:
2734 case LTTNG_LIST_SESSIONS
:
2735 case LTTNG_LIST_DOMAINS
:
2736 case LTTNG_START_TRACE
:
2737 case LTTNG_STOP_TRACE
:
2738 case LTTNG_DATA_PENDING
:
2739 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2740 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2741 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2742 case LTTNG_SNAPSHOT_RECORD
:
2743 case LTTNG_SAVE_SESSION
:
2750 if (opt_no_kernel
&& need_domain
2751 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2753 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2755 ret
= LTTNG_ERR_KERN_NA
;
2760 /* Deny register consumer if we already have a spawned consumer. */
2761 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2762 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2763 if (kconsumer_data
.pid
> 0) {
2764 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2765 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2768 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2772 * Check for command that don't needs to allocate a returned payload. We do
2773 * this here so we don't have to make the call for no payload at each
2776 switch(cmd_ctx
->lsm
->cmd_type
) {
2777 case LTTNG_LIST_SESSIONS
:
2778 case LTTNG_LIST_TRACEPOINTS
:
2779 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2780 case LTTNG_LIST_DOMAINS
:
2781 case LTTNG_LIST_CHANNELS
:
2782 case LTTNG_LIST_EVENTS
:
2785 /* Setup lttng message with no payload */
2786 ret
= setup_lttng_msg(cmd_ctx
, 0);
2788 /* This label does not try to unlock the session */
2789 goto init_setup_error
;
2793 /* Commands that DO NOT need a session. */
2794 switch (cmd_ctx
->lsm
->cmd_type
) {
2795 case LTTNG_CREATE_SESSION
:
2796 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2797 case LTTNG_CREATE_SESSION_LIVE
:
2798 case LTTNG_CALIBRATE
:
2799 case LTTNG_LIST_SESSIONS
:
2800 case LTTNG_LIST_TRACEPOINTS
:
2801 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2802 case LTTNG_SAVE_SESSION
:
2803 need_tracing_session
= 0;
2806 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2808 * We keep the session list lock across _all_ commands
2809 * for now, because the per-session lock does not
2810 * handle teardown properly.
2812 session_lock_list();
2813 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2814 if (cmd_ctx
->session
== NULL
) {
2815 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2818 /* Acquire lock for the session */
2819 session_lock(cmd_ctx
->session
);
2829 * Check domain type for specific "pre-action".
2831 switch (cmd_ctx
->lsm
->domain
.type
) {
2832 case LTTNG_DOMAIN_KERNEL
:
2834 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2838 /* Kernel tracer check */
2839 if (kernel_tracer_fd
== -1) {
2840 /* Basically, load kernel tracer modules */
2841 ret
= init_kernel_tracer();
2847 /* Consumer is in an ERROR state. Report back to client */
2848 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2849 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2853 /* Need a session for kernel command */
2854 if (need_tracing_session
) {
2855 if (cmd_ctx
->session
->kernel_session
== NULL
) {
2856 ret
= create_kernel_session(cmd_ctx
->session
);
2858 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2863 /* Start the kernel consumer daemon */
2864 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2865 if (kconsumer_data
.pid
== 0 &&
2866 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2867 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2868 ret
= start_consumerd(&kconsumer_data
);
2870 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2873 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
2875 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2879 * The consumer was just spawned so we need to add the socket to
2880 * the consumer output of the session if exist.
2882 ret
= consumer_create_socket(&kconsumer_data
,
2883 cmd_ctx
->session
->kernel_session
->consumer
);
2890 case LTTNG_DOMAIN_JUL
:
2891 case LTTNG_DOMAIN_UST
:
2893 if (!ust_app_supported()) {
2894 ret
= LTTNG_ERR_NO_UST
;
2897 /* Consumer is in an ERROR state. Report back to client */
2898 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
2899 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2903 if (need_tracing_session
) {
2904 /* Create UST session if none exist. */
2905 if (cmd_ctx
->session
->ust_session
== NULL
) {
2906 ret
= create_ust_session(cmd_ctx
->session
,
2907 &cmd_ctx
->lsm
->domain
);
2908 if (ret
!= LTTNG_OK
) {
2913 /* Start the UST consumer daemons */
2915 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
2916 if (consumerd64_bin
[0] != '\0' &&
2917 ustconsumer64_data
.pid
== 0 &&
2918 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2919 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2920 ret
= start_consumerd(&ustconsumer64_data
);
2922 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
2923 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
2927 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
2928 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2930 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
2934 * Setup socket for consumer 64 bit. No need for atomic access
2935 * since it was set above and can ONLY be set in this thread.
2937 ret
= consumer_create_socket(&ustconsumer64_data
,
2938 cmd_ctx
->session
->ust_session
->consumer
);
2944 if (consumerd32_bin
[0] != '\0' &&
2945 ustconsumer32_data
.pid
== 0 &&
2946 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
2947 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2948 ret
= start_consumerd(&ustconsumer32_data
);
2950 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
2951 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
2955 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
2956 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
2958 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
2962 * Setup socket for consumer 64 bit. No need for atomic access
2963 * since it was set above and can ONLY be set in this thread.
2965 ret
= consumer_create_socket(&ustconsumer32_data
,
2966 cmd_ctx
->session
->ust_session
->consumer
);
2978 /* Validate consumer daemon state when start/stop trace command */
2979 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
2980 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
2981 switch (cmd_ctx
->lsm
->domain
.type
) {
2982 case LTTNG_DOMAIN_JUL
:
2983 case LTTNG_DOMAIN_UST
:
2984 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
2985 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
2989 case LTTNG_DOMAIN_KERNEL
:
2990 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
2991 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
2999 * Check that the UID or GID match that of the tracing session.
3000 * The root user can interact with all sessions.
3002 if (need_tracing_session
) {
3003 if (!session_access_ok(cmd_ctx
->session
,
3004 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3005 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3006 ret
= LTTNG_ERR_EPERM
;
3012 * Send relayd information to consumer as soon as we have a domain and a
3015 if (cmd_ctx
->session
&& need_domain
) {
3017 * Setup relayd if not done yet. If the relayd information was already
3018 * sent to the consumer, this call will gracefully return.
3020 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3021 if (ret
!= LTTNG_OK
) {
3026 /* Process by command type */
3027 switch (cmd_ctx
->lsm
->cmd_type
) {
3028 case LTTNG_ADD_CONTEXT
:
3030 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3031 cmd_ctx
->lsm
->u
.context
.channel_name
,
3032 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
3035 case LTTNG_DISABLE_CHANNEL
:
3037 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3038 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3041 case LTTNG_DISABLE_EVENT
:
3043 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3044 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3045 cmd_ctx
->lsm
->u
.disable
.name
);
3048 case LTTNG_DISABLE_ALL_EVENT
:
3050 DBG("Disabling all events");
3052 ret
= cmd_disable_event_all(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3053 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3056 case LTTNG_ENABLE_CHANNEL
:
3058 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3059 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3062 case LTTNG_ENABLE_EVENT
:
3064 struct lttng_event_exclusion
*exclusion
= NULL
;
3065 struct lttng_filter_bytecode
*bytecode
= NULL
;
3066 char *filter_expression
= NULL
;
3068 /* Handle exclusion events and receive it from the client. */
3069 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3070 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3072 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3073 (count
* LTTNG_SYMBOL_NAME_LEN
));
3075 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3079 DBG("Receiving var len exclusion event list from client ...");
3080 exclusion
->count
= count
;
3081 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3082 count
* LTTNG_SYMBOL_NAME_LEN
);
3084 DBG("Nothing recv() from client var len data... continuing");
3087 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3092 /* Get filter expression from client. */
3093 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3094 size_t expression_len
=
3095 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3097 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3098 ret
= LTTNG_ERR_FILTER_INVAL
;
3103 filter_expression
= zmalloc(expression_len
);
3104 if (!filter_expression
) {
3106 ret
= LTTNG_ERR_FILTER_NOMEM
;
3110 /* Receive var. len. data */
3111 DBG("Receiving var len filter's expression from client ...");
3112 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3115 DBG("Nothing recv() from client car len data... continuing");
3117 free(filter_expression
);
3119 ret
= LTTNG_ERR_FILTER_INVAL
;
3124 /* Handle filter and get bytecode from client. */
3125 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3126 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3128 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3129 ret
= LTTNG_ERR_FILTER_INVAL
;
3134 bytecode
= zmalloc(bytecode_len
);
3137 ret
= LTTNG_ERR_FILTER_NOMEM
;
3141 /* Receive var. len. data */
3142 DBG("Receiving var len filter's bytecode from client ...");
3143 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3145 DBG("Nothing recv() from client car len data... continuing");
3149 ret
= LTTNG_ERR_FILTER_INVAL
;
3153 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3156 ret
= LTTNG_ERR_FILTER_INVAL
;
3161 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3162 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3163 &cmd_ctx
->lsm
->u
.enable
.event
,
3164 filter_expression
, bytecode
, exclusion
,
3165 kernel_poll_pipe
[1]);
3168 case LTTNG_ENABLE_ALL_EVENT
:
3170 DBG("Enabling all events");
3172 ret
= cmd_enable_event_all(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3173 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3174 cmd_ctx
->lsm
->u
.enable
.event
.type
, NULL
, NULL
,
3175 kernel_poll_pipe
[1]);
3178 case LTTNG_LIST_TRACEPOINTS
:
3180 struct lttng_event
*events
;
3183 session_lock_list();
3184 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3185 session_unlock_list();
3186 if (nb_events
< 0) {
3187 /* Return value is a negative lttng_error_code. */
3193 * Setup lttng message with payload size set to the event list size in
3194 * bytes and then copy list into the llm payload.
3196 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3202 /* Copy event list into message payload */
3203 memcpy(cmd_ctx
->llm
->payload
, events
,
3204 sizeof(struct lttng_event
) * nb_events
);
3211 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3213 struct lttng_event_field
*fields
;
3216 session_lock_list();
3217 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3219 session_unlock_list();
3220 if (nb_fields
< 0) {
3221 /* Return value is a negative lttng_error_code. */
3227 * Setup lttng message with payload size set to the event list size in
3228 * bytes and then copy list into the llm payload.
3230 ret
= setup_lttng_msg(cmd_ctx
,
3231 sizeof(struct lttng_event_field
) * nb_fields
);
3237 /* Copy event list into message payload */
3238 memcpy(cmd_ctx
->llm
->payload
, fields
,
3239 sizeof(struct lttng_event_field
) * nb_fields
);
3246 case LTTNG_SET_CONSUMER_URI
:
3249 struct lttng_uri
*uris
;
3251 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3252 len
= nb_uri
* sizeof(struct lttng_uri
);
3255 ret
= LTTNG_ERR_INVALID
;
3259 uris
= zmalloc(len
);
3261 ret
= LTTNG_ERR_FATAL
;
3265 /* Receive variable len data */
3266 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3267 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3269 DBG("No URIs received from client... continuing");
3271 ret
= LTTNG_ERR_SESSION_FAIL
;
3276 ret
= cmd_set_consumer_uri(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3278 if (ret
!= LTTNG_OK
) {
3284 * XXX: 0 means that this URI should be applied on the session. Should
3285 * be a DOMAIN enuam.
3287 if (cmd_ctx
->lsm
->domain
.type
== 0) {
3288 /* Add the URI for the UST session if a consumer is present. */
3289 if (cmd_ctx
->session
->ust_session
&&
3290 cmd_ctx
->session
->ust_session
->consumer
) {
3291 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_UST
, cmd_ctx
->session
,
3293 } else if (cmd_ctx
->session
->kernel_session
&&
3294 cmd_ctx
->session
->kernel_session
->consumer
) {
3295 ret
= cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL
,
3296 cmd_ctx
->session
, nb_uri
, uris
);
3304 case LTTNG_START_TRACE
:
3306 ret
= cmd_start_trace(cmd_ctx
->session
);
3309 case LTTNG_STOP_TRACE
:
3311 ret
= cmd_stop_trace(cmd_ctx
->session
);
3314 case LTTNG_CREATE_SESSION
:
3317 struct lttng_uri
*uris
= NULL
;
3319 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3320 len
= nb_uri
* sizeof(struct lttng_uri
);
3323 uris
= zmalloc(len
);
3325 ret
= LTTNG_ERR_FATAL
;
3329 /* Receive variable len data */
3330 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3331 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3333 DBG("No URIs received from client... continuing");
3335 ret
= LTTNG_ERR_SESSION_FAIL
;
3340 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3341 DBG("Creating session with ONE network URI is a bad call");
3342 ret
= LTTNG_ERR_SESSION_FAIL
;
3348 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3349 &cmd_ctx
->creds
, 0);
3355 case LTTNG_DESTROY_SESSION
:
3357 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3359 /* Set session to NULL so we do not unlock it after free. */
3360 cmd_ctx
->session
= NULL
;
3363 case LTTNG_LIST_DOMAINS
:
3366 struct lttng_domain
*domains
;
3368 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3370 /* Return value is a negative lttng_error_code. */
3375 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3381 /* Copy event list into message payload */
3382 memcpy(cmd_ctx
->llm
->payload
, domains
,
3383 nb_dom
* sizeof(struct lttng_domain
));
3390 case LTTNG_LIST_CHANNELS
:
3393 struct lttng_channel
*channels
;
3395 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3396 cmd_ctx
->session
, &channels
);
3398 /* Return value is a negative lttng_error_code. */
3403 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3409 /* Copy event list into message payload */
3410 memcpy(cmd_ctx
->llm
->payload
, channels
,
3411 nb_chan
* sizeof(struct lttng_channel
));
3418 case LTTNG_LIST_EVENTS
:
3421 struct lttng_event
*events
= NULL
;
3423 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3424 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3426 /* Return value is a negative lttng_error_code. */
3431 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3437 /* Copy event list into message payload */
3438 memcpy(cmd_ctx
->llm
->payload
, events
,
3439 nb_event
* sizeof(struct lttng_event
));
3446 case LTTNG_LIST_SESSIONS
:
3448 unsigned int nr_sessions
;
3450 session_lock_list();
3451 nr_sessions
= lttng_sessions_count(
3452 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3453 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3455 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3457 session_unlock_list();
3461 /* Filled the session array */
3462 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3463 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3464 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3466 session_unlock_list();
3471 case LTTNG_CALIBRATE
:
3473 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3474 &cmd_ctx
->lsm
->u
.calibrate
);
3477 case LTTNG_REGISTER_CONSUMER
:
3479 struct consumer_data
*cdata
;
3481 switch (cmd_ctx
->lsm
->domain
.type
) {
3482 case LTTNG_DOMAIN_KERNEL
:
3483 cdata
= &kconsumer_data
;
3486 ret
= LTTNG_ERR_UND
;
3490 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3491 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3494 case LTTNG_DATA_PENDING
:
3496 ret
= cmd_data_pending(cmd_ctx
->session
);
3499 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3501 struct lttcomm_lttng_output_id reply
;
3503 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3504 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3505 if (ret
!= LTTNG_OK
) {
3509 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3514 /* Copy output list into message payload */
3515 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3519 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3521 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3522 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3525 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3528 struct lttng_snapshot_output
*outputs
= NULL
;
3530 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3531 if (nb_output
< 0) {
3536 ret
= setup_lttng_msg(cmd_ctx
,
3537 nb_output
* sizeof(struct lttng_snapshot_output
));
3544 /* Copy output list into message payload */
3545 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3546 nb_output
* sizeof(struct lttng_snapshot_output
));
3553 case LTTNG_SNAPSHOT_RECORD
:
3555 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3556 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3557 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3560 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3563 struct lttng_uri
*uris
= NULL
;
3565 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3566 len
= nb_uri
* sizeof(struct lttng_uri
);
3569 uris
= zmalloc(len
);
3571 ret
= LTTNG_ERR_FATAL
;
3575 /* Receive variable len data */
3576 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3577 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3579 DBG("No URIs received from client... continuing");
3581 ret
= LTTNG_ERR_SESSION_FAIL
;
3586 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3587 DBG("Creating session with ONE network URI is a bad call");
3588 ret
= LTTNG_ERR_SESSION_FAIL
;
3594 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3595 nb_uri
, &cmd_ctx
->creds
);
3599 case LTTNG_CREATE_SESSION_LIVE
:
3602 struct lttng_uri
*uris
= NULL
;
3604 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3605 len
= nb_uri
* sizeof(struct lttng_uri
);
3608 uris
= zmalloc(len
);
3610 ret
= LTTNG_ERR_FATAL
;
3614 /* Receive variable len data */
3615 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3616 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3618 DBG("No URIs received from client... continuing");
3620 ret
= LTTNG_ERR_SESSION_FAIL
;
3625 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3626 DBG("Creating session with ONE network URI is a bad call");
3627 ret
= LTTNG_ERR_SESSION_FAIL
;
3633 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3634 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3638 case LTTNG_SAVE_SESSION
:
3640 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
3645 ret
= LTTNG_ERR_UND
;
3650 if (cmd_ctx
->llm
== NULL
) {
3651 DBG("Missing llm structure. Allocating one.");
3652 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3656 /* Set return code */
3657 cmd_ctx
->llm
->ret_code
= ret
;
3659 if (cmd_ctx
->session
) {
3660 session_unlock(cmd_ctx
->session
);
3662 if (need_tracing_session
) {
3663 session_unlock_list();
3670 * Thread managing health check socket.
3672 static void *thread_manage_health(void *data
)
3674 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3675 uint32_t revents
, nb_fd
;
3676 struct lttng_poll_event events
;
3677 struct health_comm_msg msg
;
3678 struct health_comm_reply reply
;
3680 DBG("[thread] Manage health check started");
3682 rcu_register_thread();
3684 /* We might hit an error path before this is created. */
3685 lttng_poll_init(&events
);
3687 /* Create unix socket */
3688 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3690 ERR("Unable to create health check Unix socket");
3696 /* lttng health client socket path permissions */
3697 ret
= chown(health_unix_sock_path
, 0,
3698 utils_get_group_id(tracing_group_name
));
3700 ERR("Unable to set group on %s", health_unix_sock_path
);
3706 ret
= chmod(health_unix_sock_path
,
3707 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3709 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3717 * Set the CLOEXEC flag. Return code is useless because either way, the
3720 (void) utils_set_fd_cloexec(sock
);
3722 ret
= lttcomm_listen_unix_sock(sock
);
3728 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3729 * more will be added to this poll set.
3731 ret
= sessiond_set_thread_pollset(&events
, 2);
3736 /* Add the application registration socket */
3737 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3742 sessiond_notify_ready();
3745 DBG("Health check ready");
3747 /* Inifinite blocking call, waiting for transmission */
3749 ret
= lttng_poll_wait(&events
, -1);
3752 * Restart interrupted system call.
3754 if (errno
== EINTR
) {
3762 for (i
= 0; i
< nb_fd
; i
++) {
3763 /* Fetch once the poll data */
3764 revents
= LTTNG_POLL_GETEV(&events
, i
);
3765 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3767 /* Thread quit pipe has been closed. Killing thread. */
3768 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3774 /* Event on the registration socket */
3775 if (pollfd
== sock
) {
3776 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3777 ERR("Health socket poll error");
3783 new_sock
= lttcomm_accept_unix_sock(sock
);
3789 * Set the CLOEXEC flag. Return code is useless because either way, the
3792 (void) utils_set_fd_cloexec(new_sock
);
3794 DBG("Receiving data from client for health...");
3795 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3797 DBG("Nothing recv() from client... continuing");
3798 ret
= close(new_sock
);
3806 rcu_thread_online();
3808 memset(&reply
, 0, sizeof(reply
));
3809 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
3811 * health_check_state returns 0 if health is
3814 if (!health_check_state(health_sessiond
, i
)) {
3815 reply
.ret_code
|= 1ULL << i
;
3819 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
3821 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3823 ERR("Failed to send health data back to client");
3826 /* End of transmission */
3827 ret
= close(new_sock
);
3837 ERR("Health error occurred in %s", __func__
);
3839 DBG("Health check thread dying");
3840 unlink(health_unix_sock_path
);
3848 lttng_poll_clean(&events
);
3850 rcu_unregister_thread();
3855 * This thread manage all clients request using the unix client socket for
3858 static void *thread_manage_clients(void *data
)
3860 int sock
= -1, ret
, i
, pollfd
, err
= -1;
3862 uint32_t revents
, nb_fd
;
3863 struct command_ctx
*cmd_ctx
= NULL
;
3864 struct lttng_poll_event events
;
3866 DBG("[thread] Manage client started");
3868 rcu_register_thread();
3870 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
3872 health_code_update();
3874 ret
= lttcomm_listen_unix_sock(client_sock
);
3880 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3881 * more will be added to this poll set.
3883 ret
= sessiond_set_thread_pollset(&events
, 2);
3885 goto error_create_poll
;
3888 /* Add the application registration socket */
3889 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
3894 sessiond_notify_ready();
3895 ret
= sem_post(&load_info
->message_thread_ready
);
3897 PERROR("sem_post message_thread_ready");
3901 /* This testpoint is after we signal readiness to the parent. */
3902 if (testpoint(sessiond_thread_manage_clients
)) {
3906 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
3910 health_code_update();
3913 DBG("Accepting client command ...");
3915 /* Inifinite blocking call, waiting for transmission */
3917 health_poll_entry();
3918 ret
= lttng_poll_wait(&events
, -1);
3922 * Restart interrupted system call.
3924 if (errno
== EINTR
) {
3932 for (i
= 0; i
< nb_fd
; i
++) {
3933 /* Fetch once the poll data */
3934 revents
= LTTNG_POLL_GETEV(&events
, i
);
3935 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3937 health_code_update();
3939 /* Thread quit pipe has been closed. Killing thread. */
3940 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3946 /* Event on the registration socket */
3947 if (pollfd
== client_sock
) {
3948 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3949 ERR("Client socket poll error");
3955 DBG("Wait for client response");
3957 health_code_update();
3959 sock
= lttcomm_accept_unix_sock(client_sock
);
3965 * Set the CLOEXEC flag. Return code is useless because either way, the
3968 (void) utils_set_fd_cloexec(sock
);
3970 /* Set socket option for credentials retrieval */
3971 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
3976 /* Allocate context command to process the client request */
3977 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
3978 if (cmd_ctx
== NULL
) {
3979 PERROR("zmalloc cmd_ctx");
3983 /* Allocate data buffer for reception */
3984 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
3985 if (cmd_ctx
->lsm
== NULL
) {
3986 PERROR("zmalloc cmd_ctx->lsm");
3990 cmd_ctx
->llm
= NULL
;
3991 cmd_ctx
->session
= NULL
;
3993 health_code_update();
3996 * Data is received from the lttng client. The struct
3997 * lttcomm_session_msg (lsm) contains the command and data request of
4000 DBG("Receiving data from client ...");
4001 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4002 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4004 DBG("Nothing recv() from client... continuing");
4010 clean_command_ctx(&cmd_ctx
);
4014 health_code_update();
4016 // TODO: Validate cmd_ctx including sanity check for
4017 // security purpose.
4019 rcu_thread_online();
4021 * This function dispatch the work to the kernel or userspace tracer
4022 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4023 * informations for the client. The command context struct contains
4024 * everything this function may needs.
4026 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4027 rcu_thread_offline();
4035 * TODO: Inform client somehow of the fatal error. At
4036 * this point, ret < 0 means that a zmalloc failed
4037 * (ENOMEM). Error detected but still accept
4038 * command, unless a socket error has been
4041 clean_command_ctx(&cmd_ctx
);
4045 health_code_update();
4047 DBG("Sending response (size: %d, retcode: %s)",
4048 cmd_ctx
->lttng_msg_size
,
4049 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
4050 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4052 ERR("Failed to send data back to client");
4055 /* End of transmission */
4062 clean_command_ctx(&cmd_ctx
);
4064 health_code_update();
4076 lttng_poll_clean(&events
);
4077 clean_command_ctx(&cmd_ctx
);
4081 unlink(client_unix_sock_path
);
4082 if (client_sock
>= 0) {
4083 ret
= close(client_sock
);
4091 ERR("Health error occurred in %s", __func__
);
4094 health_unregister(health_sessiond
);
4096 DBG("Client thread dying");
4098 rcu_unregister_thread();
4104 * usage function on stderr
4106 static void usage(void)
4108 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4109 fprintf(stderr
, " -h, --help Display this usage.\n");
4110 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4111 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4112 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4113 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4114 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4115 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4116 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4117 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4118 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4119 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4120 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4121 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4122 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4123 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
4124 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4125 fprintf(stderr
, " -V, --version Show version number.\n");
4126 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4127 fprintf(stderr
, " -q, --quiet No output at all.\n");
4128 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4129 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4130 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4131 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4132 fprintf(stderr
, " --jul-tcp-port JUL application registration TCP port\n");
4133 fprintf(stderr
, " -f --config Load daemon configuration file\n");
4134 fprintf(stderr
, " -l --load PATH Load session configuration\n");
4138 * Take an option from the getopt output and set it in the right variable to be
4141 * Return 0 on success else a negative value.
4143 static int set_option(int opt
, const char *arg
, const char *optname
)
4149 fprintf(stderr
, "option %s", optname
);
4151 fprintf(stderr
, " with arg %s\n", arg
);
4155 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4158 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4167 tracing_group_name
= strdup(arg
);
4173 fprintf(stdout
, "%s\n", VERSION
);
4179 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4182 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4185 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4188 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4191 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4194 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4200 lttng_opt_quiet
= 1;
4203 /* Verbose level can increase using multiple -v */
4205 lttng_opt_verbose
= config_parse_value(arg
);
4207 lttng_opt_verbose
+= 1;
4212 opt_verbose_consumer
= config_parse_value(arg
);
4214 opt_verbose_consumer
+= 1;
4218 consumerd32_bin
= strdup(arg
);
4219 consumerd32_bin_override
= 1;
4222 consumerd32_libdir
= strdup(arg
);
4223 consumerd32_libdir_override
= 1;
4226 consumerd64_bin
= strdup(arg
);
4227 consumerd64_bin_override
= 1;
4230 consumerd64_libdir
= strdup(arg
);
4231 consumerd64_libdir_override
= 1;
4234 opt_pidfile
= strdup(arg
);
4236 case 'J': /* JUL TCP port. */
4241 v
= strtoul(arg
, NULL
, 0);
4242 if (errno
!= 0 || !isdigit(arg
[0])) {
4243 ERR("Wrong value in --jul-tcp-port parameter: %s", arg
);
4246 if (v
== 0 || v
>= 65535) {
4247 ERR("Port overflow in --jul-tcp-port parameter: %s", arg
);
4250 jul_tcp_port
= (uint32_t) v
;
4251 DBG3("JUL TCP port set to non default: %u", jul_tcp_port
);
4255 opt_load_session_path
= strdup(arg
);
4256 if (!opt_load_session_path
) {
4262 /* This is handled in set_options() thus silent break. */
4265 /* Unknown option or other error.
4266 * Error is printed by getopt, just return */
4274 * config_entry_handler_cb used to handle options read from a config file.
4275 * See config_entry_handler_cb comment in common/config/config.h for the
4276 * return value conventions.
4278 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4282 if (!entry
|| !entry
->name
|| !entry
->value
) {
4287 /* Check if the option is to be ignored */
4288 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4289 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4294 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4297 /* Ignore if not fully matched. */
4298 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4303 * If the option takes no argument on the command line, we have to
4304 * check if the value is "true". We support non-zero numeric values,
4307 if (!long_options
[i
].has_arg
) {
4308 ret
= config_parse_value(entry
->value
);
4311 WARN("Invalid configuration value \"%s\" for option %s",
4312 entry
->value
, entry
->name
);
4314 /* False, skip boolean config option. */
4319 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4323 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4330 * daemon configuration loading and argument parsing
4332 static int set_options(int argc
, char **argv
)
4334 int ret
= 0, c
= 0, option_index
= 0;
4335 int orig_optopt
= optopt
, orig_optind
= optind
;
4337 const char *config_path
= NULL
;
4339 optstring
= utils_generate_optstring(long_options
,
4340 sizeof(long_options
) / sizeof(struct option
));
4346 /* Check for the --config option */
4347 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4348 &option_index
)) != -1) {
4352 } else if (c
!= 'f') {
4353 /* if not equal to --config option. */
4357 config_path
= utils_expand_path(optarg
);
4359 ERR("Failed to resolve path: %s", optarg
);
4363 ret
= config_get_section_entries(config_path
, config_section_name
,
4364 config_entry_handler
, NULL
);
4367 ERR("Invalid configuration option at line %i", ret
);
4373 /* Reset getopt's global state */
4374 optopt
= orig_optopt
;
4375 optind
= orig_optind
;
4377 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
4382 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
4394 * Creates the two needed socket by the daemon.
4395 * apps_sock - The communication socket for all UST apps.
4396 * client_sock - The communication of the cli tool (lttng).
4398 static int init_daemon_socket(void)
4403 old_umask
= umask(0);
4405 /* Create client tool unix socket */
4406 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4407 if (client_sock
< 0) {
4408 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4413 /* Set the cloexec flag */
4414 ret
= utils_set_fd_cloexec(client_sock
);
4416 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4417 "Continuing but note that the consumer daemon will have a "
4418 "reference to this socket on exec()", client_sock
);
4421 /* File permission MUST be 660 */
4422 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4424 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4429 /* Create the application unix socket */
4430 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4431 if (apps_sock
< 0) {
4432 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4437 /* Set the cloexec flag */
4438 ret
= utils_set_fd_cloexec(apps_sock
);
4440 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4441 "Continuing but note that the consumer daemon will have a "
4442 "reference to this socket on exec()", apps_sock
);
4445 /* File permission MUST be 666 */
4446 ret
= chmod(apps_unix_sock_path
,
4447 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4449 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4454 DBG3("Session daemon client socket %d and application socket %d created",
4455 client_sock
, apps_sock
);
4463 * Check if the global socket is available, and if a daemon is answering at the
4464 * other side. If yes, error is returned.
4466 static int check_existing_daemon(void)
4468 /* Is there anybody out there ? */
4469 if (lttng_session_daemon_alive()) {
4477 * Set the tracing group gid onto the client socket.
4479 * Race window between mkdir and chown is OK because we are going from more
4480 * permissive (root.root) to less permissive (root.tracing).
4482 static int set_permissions(char *rundir
)
4487 gid
= utils_get_group_id(tracing_group_name
);
4489 /* Set lttng run dir */
4490 ret
= chown(rundir
, 0, gid
);
4492 ERR("Unable to set group on %s", rundir
);
4497 * Ensure all applications and tracing group can search the run
4498 * dir. Allow everyone to read the directory, since it does not
4499 * buy us anything to hide its content.
4501 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4503 ERR("Unable to set permissions on %s", rundir
);
4507 /* lttng client socket path */
4508 ret
= chown(client_unix_sock_path
, 0, gid
);
4510 ERR("Unable to set group on %s", client_unix_sock_path
);
4514 /* kconsumer error socket path */
4515 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4517 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4521 /* 64-bit ustconsumer error socket path */
4522 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4524 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4528 /* 32-bit ustconsumer compat32 error socket path */
4529 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4531 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4535 DBG("All permissions are set");
4541 * Create the lttng run directory needed for all global sockets and pipe.
4543 static int create_lttng_rundir(const char *rundir
)
4547 DBG3("Creating LTTng run directory: %s", rundir
);
4549 ret
= mkdir(rundir
, S_IRWXU
);
4551 if (errno
!= EEXIST
) {
4552 ERR("Unable to create %s", rundir
);
4564 * Setup sockets and directory needed by the kconsumerd communication with the
4567 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4571 char path
[PATH_MAX
];
4573 switch (consumer_data
->type
) {
4574 case LTTNG_CONSUMER_KERNEL
:
4575 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4577 case LTTNG_CONSUMER64_UST
:
4578 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4580 case LTTNG_CONSUMER32_UST
:
4581 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4584 ERR("Consumer type unknown");
4589 DBG2("Creating consumer directory: %s", path
);
4591 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4593 if (errno
!= EEXIST
) {
4595 ERR("Failed to create %s", path
);
4601 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4603 ERR("Unable to set group on %s", path
);
4609 /* Create the kconsumerd error unix socket */
4610 consumer_data
->err_sock
=
4611 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4612 if (consumer_data
->err_sock
< 0) {
4613 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4619 * Set the CLOEXEC flag. Return code is useless because either way, the
4622 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4624 PERROR("utils_set_fd_cloexec");
4625 /* continue anyway */
4628 /* File permission MUST be 660 */
4629 ret
= chmod(consumer_data
->err_unix_sock_path
,
4630 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4632 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4642 * Signal handler for the daemon
4644 * Simply stop all worker threads, leaving main() return gracefully after
4645 * joining all threads and calling cleanup().
4647 static void sighandler(int sig
)
4651 DBG("SIGPIPE caught");
4654 DBG("SIGINT caught");
4658 DBG("SIGTERM caught");
4662 CMM_STORE_SHARED(recv_child_signal
, 1);
4670 * Setup signal handler for :
4671 * SIGINT, SIGTERM, SIGPIPE
4673 static int set_signal_handler(void)
4676 struct sigaction sa
;
4679 if ((ret
= sigemptyset(&sigset
)) < 0) {
4680 PERROR("sigemptyset");
4684 sa
.sa_handler
= sighandler
;
4685 sa
.sa_mask
= sigset
;
4687 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
4688 PERROR("sigaction");
4692 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
4693 PERROR("sigaction");
4697 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
4698 PERROR("sigaction");
4702 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
4703 PERROR("sigaction");
4707 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
4713 * Set open files limit to unlimited. This daemon can open a large number of
4714 * file descriptors in order to consumer multiple kernel traces.
4716 static void set_ulimit(void)
4721 /* The kernel does not allowed an infinite limit for open files */
4722 lim
.rlim_cur
= 65535;
4723 lim
.rlim_max
= 65535;
4725 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
4727 PERROR("failed to set open files limit");
4732 * Write pidfile using the rundir and opt_pidfile.
4734 static void write_pidfile(void)
4737 char pidfile_path
[PATH_MAX
];
4742 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
4744 /* Build pidfile path from rundir and opt_pidfile. */
4745 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
4746 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
4748 PERROR("snprintf pidfile path");
4754 * Create pid file in rundir. Return value is of no importance. The
4755 * execution will continue even though we are not able to write the file.
4757 (void) utils_create_pid_file(getpid(), pidfile_path
);
4764 * Write JUL TCP port using the rundir.
4766 static void write_julport(void)
4769 char path
[PATH_MAX
];
4773 ret
= snprintf(path
, sizeof(path
), "%s/"
4774 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE
, rundir
);
4776 PERROR("snprintf julport path");
4781 * Create TCP JUL port file in rundir. Return value is of no importance.
4782 * The execution will continue even though we are not able to write the
4785 (void) utils_create_pid_file(jul_tcp_port
, path
);
4792 * Start the load session thread and dettach from it so the main thread can
4793 * continue. This does not return a value since whatever the outcome, the main
4794 * thread will continue.
4796 static void start_load_session_thread(void)
4800 /* Create session loading thread. */
4801 ret
= pthread_create(&load_session_thread
, NULL
, thread_load_session
,
4804 PERROR("pthread_create load_session_thread");
4808 ret
= pthread_detach(load_session_thread
);
4810 PERROR("pthread_detach load_session_thread");
4813 /* Everything went well so don't cleanup anything. */
4816 /* The cleanup() function will destroy the load_info data. */
4823 int main(int argc
, char **argv
)
4827 const char *home_path
, *env_app_timeout
;
4829 init_kernel_workarounds();
4831 rcu_register_thread();
4833 if ((ret
= set_signal_handler()) < 0) {
4837 setup_consumerd_path();
4839 page_size
= sysconf(_SC_PAGESIZE
);
4840 if (page_size
< 0) {
4841 PERROR("sysconf _SC_PAGESIZE");
4842 page_size
= LONG_MAX
;
4843 WARN("Fallback page size to %ld", page_size
);
4846 /* Parse arguments and load the daemon configuration file */
4848 if ((ret
= set_options(argc
, argv
)) < 0) {
4853 if (opt_daemon
|| opt_background
) {
4856 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
4863 * We are in the child. Make sure all other file descriptors are
4864 * closed, in case we are called with more opened file descriptors than
4865 * the standard ones.
4867 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
4872 /* Create thread quit pipe */
4873 if ((ret
= init_thread_quit_pipe()) < 0) {
4877 /* Check if daemon is UID = 0 */
4878 is_root
= !getuid();
4881 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
4883 /* Create global run dir with root access */
4884 ret
= create_lttng_rundir(rundir
);
4889 if (strlen(apps_unix_sock_path
) == 0) {
4890 snprintf(apps_unix_sock_path
, PATH_MAX
,
4891 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
4894 if (strlen(client_unix_sock_path
) == 0) {
4895 snprintf(client_unix_sock_path
, PATH_MAX
,
4896 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
4899 /* Set global SHM for ust */
4900 if (strlen(wait_shm_path
) == 0) {
4901 snprintf(wait_shm_path
, PATH_MAX
,
4902 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
4905 if (strlen(health_unix_sock_path
) == 0) {
4906 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4907 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
4910 /* Setup kernel consumerd path */
4911 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
4912 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
4913 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
4914 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
4916 DBG2("Kernel consumer err path: %s",
4917 kconsumer_data
.err_unix_sock_path
);
4918 DBG2("Kernel consumer cmd path: %s",
4919 kconsumer_data
.cmd_unix_sock_path
);
4921 home_path
= utils_get_home_dir();
4922 if (home_path
== NULL
) {
4923 /* TODO: Add --socket PATH option */
4924 ERR("Can't get HOME directory for sockets creation.");
4930 * Create rundir from home path. This will create something like
4933 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
4939 ret
= create_lttng_rundir(rundir
);
4944 if (strlen(apps_unix_sock_path
) == 0) {
4945 snprintf(apps_unix_sock_path
, PATH_MAX
,
4946 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
4949 /* Set the cli tool unix socket path */
4950 if (strlen(client_unix_sock_path
) == 0) {
4951 snprintf(client_unix_sock_path
, PATH_MAX
,
4952 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
4955 /* Set global SHM for ust */
4956 if (strlen(wait_shm_path
) == 0) {
4957 snprintf(wait_shm_path
, PATH_MAX
,
4958 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
4961 /* Set health check Unix path */
4962 if (strlen(health_unix_sock_path
) == 0) {
4963 snprintf(health_unix_sock_path
, sizeof(health_unix_sock_path
),
4964 DEFAULT_HOME_HEALTH_UNIX_SOCK
, home_path
);
4968 /* Set consumer initial state */
4969 kernel_consumerd_state
= CONSUMER_STOPPED
;
4970 ust_consumerd_state
= CONSUMER_STOPPED
;
4972 DBG("Client socket path %s", client_unix_sock_path
);
4973 DBG("Application socket path %s", apps_unix_sock_path
);
4974 DBG("Application wait path %s", wait_shm_path
);
4975 DBG("LTTng run directory path: %s", rundir
);
4977 /* 32 bits consumerd path setup */
4978 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
4979 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
4980 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
4981 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
4983 DBG2("UST consumer 32 bits err path: %s",
4984 ustconsumer32_data
.err_unix_sock_path
);
4985 DBG2("UST consumer 32 bits cmd path: %s",
4986 ustconsumer32_data
.cmd_unix_sock_path
);
4988 /* 64 bits consumerd path setup */
4989 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
4990 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
4991 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
4992 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
4994 DBG2("UST consumer 64 bits err path: %s",
4995 ustconsumer64_data
.err_unix_sock_path
);
4996 DBG2("UST consumer 64 bits cmd path: %s",
4997 ustconsumer64_data
.cmd_unix_sock_path
);
5000 * See if daemon already exist.
5002 if ((ret
= check_existing_daemon()) < 0) {
5003 ERR("Already running daemon.\n");
5005 * We do not goto exit because we must not cleanup()
5006 * because a daemon is already running.
5012 * Init UST app hash table. Alloc hash table before this point since
5013 * cleanup() can get called after that point.
5017 /* Initialize JUL domain subsystem. */
5018 if ((ret
= jul_init()) < 0) {
5019 /* ENOMEM at this point. */
5023 /* After this point, we can safely call cleanup() with "goto exit" */
5026 * These actions must be executed as root. We do that *after* setting up
5027 * the sockets path because we MUST make the check for another daemon using
5028 * those paths *before* trying to set the kernel consumer sockets and init
5032 ret
= set_consumer_sockets(&kconsumer_data
, rundir
);
5037 /* Setup kernel tracer */
5038 if (!opt_no_kernel
) {
5039 init_kernel_tracer();
5042 /* Set ulimit for open files */
5045 /* init lttng_fd tracking must be done after set_ulimit. */
5048 ret
= set_consumer_sockets(&ustconsumer64_data
, rundir
);
5053 ret
= set_consumer_sockets(&ustconsumer32_data
, rundir
);
5058 /* Setup the needed unix socket */
5059 if ((ret
= init_daemon_socket()) < 0) {
5063 /* Set credentials to socket */
5064 if (is_root
&& ((ret
= set_permissions(rundir
)) < 0)) {
5068 /* Get parent pid if -S, --sig-parent is specified. */
5069 if (opt_sig_parent
) {
5073 /* Setup the kernel pipe for waking up the kernel thread */
5074 if (is_root
&& !opt_no_kernel
) {
5075 if ((ret
= utils_create_pipe_cloexec(kernel_poll_pipe
)) < 0) {
5080 /* Setup the thread ht_cleanup communication pipe. */
5081 if (utils_create_pipe_cloexec(ht_cleanup_pipe
) < 0) {
5085 /* Setup the thread apps communication pipe. */
5086 if ((ret
= utils_create_pipe_cloexec(apps_cmd_pipe
)) < 0) {
5090 /* Setup the thread apps notify communication pipe. */
5091 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
) < 0) {
5095 /* Initialize global buffer per UID and PID registry. */
5096 buffer_reg_init_uid_registry();
5097 buffer_reg_init_pid_registry();
5099 /* Init UST command queue. */
5100 cds_wfq_init(&ust_cmd_queue
.queue
);
5103 * Get session list pointer. This pointer MUST NOT be free(). This list is
5104 * statically declared in session.c
5106 session_list_ptr
= session_get_list();
5108 /* Set up max poll set size */
5109 lttng_poll_set_max_size();
5113 /* Check for the application socket timeout env variable. */
5114 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5115 if (env_app_timeout
) {
5116 app_socket_timeout
= atoi(env_app_timeout
);
5118 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5124 /* Initialize communication library */
5126 /* This is to get the TCP timeout value. */
5127 lttcomm_inet_init();
5129 if (load_session_init_data(&load_info
) < 0) {
5132 load_info
->path
= opt_load_session_path
;
5135 * Initialize the health check subsystem. This call should set the
5136 * appropriate time values.
5138 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5139 if (!health_sessiond
) {
5140 PERROR("health_app_create error");
5141 goto exit_health_sessiond_cleanup
;
5144 /* Create thread to clean up RCU hash tables */
5145 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5146 thread_ht_cleanup
, (void *) NULL
);
5148 PERROR("pthread_create ht_cleanup");
5149 goto exit_ht_cleanup
;
5152 /* Create health-check thread */
5153 ret
= pthread_create(&health_thread
, NULL
,
5154 thread_manage_health
, (void *) NULL
);
5156 PERROR("pthread_create health");
5160 /* Create thread to manage the client socket */
5161 ret
= pthread_create(&client_thread
, NULL
,
5162 thread_manage_clients
, (void *) NULL
);
5164 PERROR("pthread_create clients");
5168 /* Create thread to dispatch registration */
5169 ret
= pthread_create(&dispatch_thread
, NULL
,
5170 thread_dispatch_ust_registration
, (void *) NULL
);
5172 PERROR("pthread_create dispatch");
5176 /* Create thread to manage application registration. */
5177 ret
= pthread_create(®_apps_thread
, NULL
,
5178 thread_registration_apps
, (void *) NULL
);
5180 PERROR("pthread_create registration");
5184 /* Create thread to manage application socket */
5185 ret
= pthread_create(&apps_thread
, NULL
,
5186 thread_manage_apps
, (void *) NULL
);
5188 PERROR("pthread_create apps");
5192 /* Create thread to manage application notify socket */
5193 ret
= pthread_create(&apps_notify_thread
, NULL
,
5194 ust_thread_manage_notify
, (void *) NULL
);
5196 PERROR("pthread_create notify");
5197 goto exit_apps_notify
;
5200 /* Create JUL registration thread. */
5201 ret
= pthread_create(&jul_reg_thread
, NULL
,
5202 jul_thread_manage_registration
, (void *) NULL
);
5204 PERROR("pthread_create JUL");
5208 /* Don't start this thread if kernel tracing is not requested nor root */
5209 if (is_root
&& !opt_no_kernel
) {
5210 /* Create kernel thread to manage kernel event */
5211 ret
= pthread_create(&kernel_thread
, NULL
,
5212 thread_manage_kernel
, (void *) NULL
);
5214 PERROR("pthread_create kernel");
5219 /* Load possible session(s). */
5220 start_load_session_thread();
5222 if (is_root
&& !opt_no_kernel
) {
5223 ret
= pthread_join(kernel_thread
, &status
);
5225 PERROR("pthread_join");
5226 goto error
; /* join error, exit without cleanup */
5231 ret
= pthread_join(jul_reg_thread
, &status
);
5233 PERROR("pthread_join JUL");
5234 goto error
; /* join error, exit without cleanup */
5238 ret
= pthread_join(apps_notify_thread
, &status
);
5240 PERROR("pthread_join apps notify");
5241 goto error
; /* join error, exit without cleanup */
5245 ret
= pthread_join(apps_thread
, &status
);
5247 PERROR("pthread_join apps");
5248 goto error
; /* join error, exit without cleanup */
5253 ret
= pthread_join(reg_apps_thread
, &status
);
5255 PERROR("pthread_join");
5256 goto error
; /* join error, exit without cleanup */
5260 ret
= pthread_join(dispatch_thread
, &status
);
5262 PERROR("pthread_join");
5263 goto error
; /* join error, exit without cleanup */
5267 ret
= pthread_join(client_thread
, &status
);
5269 PERROR("pthread_join");
5270 goto error
; /* join error, exit without cleanup */
5273 ret
= join_consumer_thread(&kconsumer_data
);
5275 PERROR("join_consumer");
5276 goto error
; /* join error, exit without cleanup */
5279 ret
= join_consumer_thread(&ustconsumer32_data
);
5281 PERROR("join_consumer ust32");
5282 goto error
; /* join error, exit without cleanup */
5285 ret
= join_consumer_thread(&ustconsumer64_data
);
5287 PERROR("join_consumer ust64");
5288 goto error
; /* join error, exit without cleanup */
5292 ret
= pthread_join(health_thread
, &status
);
5294 PERROR("pthread_join health thread");
5295 goto error
; /* join error, exit without cleanup */
5299 ret
= pthread_join(ht_cleanup_thread
, &status
);
5301 PERROR("pthread_join ht cleanup thread");
5302 goto error
; /* join error, exit without cleanup */
5305 health_app_destroy(health_sessiond
);
5306 exit_health_sessiond_cleanup
:
5309 * cleanup() is called when no other thread is running.
5311 rcu_thread_online();
5313 rcu_thread_offline();
5314 rcu_unregister_thread();