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.
33 #include <sys/mount.h>
34 #include <sys/resource.h>
35 #include <sys/socket.h>
37 #include <sys/types.h>
39 #include <urcu/uatomic.h>
43 #include <common/common.h>
44 #include <common/compat/socket.h>
45 #include <common/defaults.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/futex.h>
48 #include <common/relayd/relayd.h>
49 #include <common/utils.h>
50 #include <common/daemonize.h>
51 #include <common/config/config.h>
53 #include "lttng-sessiond.h"
54 #include "buffer-registry.h"
61 #include "kernel-consumer.h"
65 #include "ust-consumer.h"
68 #include "health-sessiond.h"
69 #include "testpoint.h"
70 #include "ust-thread.h"
71 #include "agent-thread.h"
73 #include "load-session-thread.h"
76 #define CONSUMERD_FILE "lttng-consumerd"
79 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
80 static int tracing_group_name_override
;
81 static char *opt_pidfile
;
82 static int opt_sig_parent
;
83 static int opt_verbose_consumer
;
84 static int opt_daemon
, opt_background
;
85 static int opt_no_kernel
;
86 static char *opt_load_session_path
;
87 static pid_t ppid
; /* Parent PID for --sig-parent option */
88 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
90 static int lockfile_fd
= -1;
92 /* Set to 1 when a SIGUSR1 signal is received. */
93 static int recv_child_signal
;
96 * Consumer daemon specific control data. Every value not initialized here is
97 * set to 0 by the static definition.
99 static struct consumer_data kconsumer_data
= {
100 .type
= LTTNG_CONSUMER_KERNEL
,
101 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
102 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
105 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
106 .lock
= PTHREAD_MUTEX_INITIALIZER
,
107 .cond
= PTHREAD_COND_INITIALIZER
,
108 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
110 static struct consumer_data ustconsumer64_data
= {
111 .type
= LTTNG_CONSUMER64_UST
,
112 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
113 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
116 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
117 .lock
= PTHREAD_MUTEX_INITIALIZER
,
118 .cond
= PTHREAD_COND_INITIALIZER
,
119 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
121 static struct consumer_data ustconsumer32_data
= {
122 .type
= LTTNG_CONSUMER32_UST
,
123 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
124 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
127 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
128 .lock
= PTHREAD_MUTEX_INITIALIZER
,
129 .cond
= PTHREAD_COND_INITIALIZER
,
130 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
133 /* Command line options */
134 static const struct option long_options
[] = {
135 { "client-sock", 1, 0, 'c' },
136 { "apps-sock", 1, 0, 'a' },
137 { "kconsumerd-cmd-sock", 1, 0, 'C' },
138 { "kconsumerd-err-sock", 1, 0, 'E' },
139 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
140 { "ustconsumerd32-err-sock", 1, 0, 'H' },
141 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
142 { "ustconsumerd64-err-sock", 1, 0, 'F' },
143 { "consumerd32-path", 1, 0, 'u' },
144 { "consumerd32-libdir", 1, 0, 'U' },
145 { "consumerd64-path", 1, 0, 't' },
146 { "consumerd64-libdir", 1, 0, 'T' },
147 { "daemonize", 0, 0, 'd' },
148 { "background", 0, 0, 'b' },
149 { "sig-parent", 0, 0, 'S' },
150 { "help", 0, 0, 'h' },
151 { "group", 1, 0, 'g' },
152 { "version", 0, 0, 'V' },
153 { "quiet", 0, 0, 'q' },
154 { "verbose", 0, 0, 'v' },
155 { "verbose-consumer", 0, 0, 'Z' },
156 { "no-kernel", 0, 0, 'N' },
157 { "pidfile", 1, 0, 'p' },
158 { "agent-tcp-port", 1, 0, 'J' },
159 { "config", 1, 0, 'f' },
160 { "load", 1, 0, 'l' },
161 { "kmod-probes", 1, 0, 'P' },
162 { "extra-kmod-probes", 1, 0, 'e' },
166 /* Command line options to ignore from configuration file */
167 static const char *config_ignore_options
[] = { "help", "version", "config" };
169 /* Shared between threads */
170 static int dispatch_thread_exit
;
172 /* Global application Unix socket path */
173 static char apps_unix_sock_path
[PATH_MAX
];
174 /* Global client Unix socket path */
175 static char client_unix_sock_path
[PATH_MAX
];
176 /* global wait shm path for UST */
177 static char wait_shm_path
[PATH_MAX
];
178 /* Global health check unix path */
179 static char health_unix_sock_path
[PATH_MAX
];
181 /* Sockets and FDs */
182 static int client_sock
= -1;
183 static int apps_sock
= -1;
184 int kernel_tracer_fd
= -1;
185 static int kernel_poll_pipe
[2] = { -1, -1 };
188 * Quit pipe for all threads. This permits a single cancellation point
189 * for all threads when receiving an event on the pipe.
191 static int thread_quit_pipe
[2] = { -1, -1 };
192 static int ht_cleanup_quit_pipe
[2] = { -1, -1 };
195 * This pipe is used to inform the thread managing application communication
196 * that a command is queued and ready to be processed.
198 static int apps_cmd_pipe
[2] = { -1, -1 };
200 int apps_cmd_notify_pipe
[2] = { -1, -1 };
202 /* Pthread, Mutexes and Semaphores */
203 static pthread_t apps_thread
;
204 static pthread_t apps_notify_thread
;
205 static pthread_t reg_apps_thread
;
206 static pthread_t client_thread
;
207 static pthread_t kernel_thread
;
208 static pthread_t dispatch_thread
;
209 static pthread_t health_thread
;
210 static pthread_t ht_cleanup_thread
;
211 static pthread_t agent_reg_thread
;
212 static pthread_t load_session_thread
;
215 * UST registration command queue. This queue is tied with a futex and uses a N
216 * wakers / 1 waiter implemented and detailed in futex.c/.h
218 * The thread_registration_apps and thread_dispatch_ust_registration uses this
219 * queue along with the wait/wake scheme. The thread_manage_apps receives down
220 * the line new application socket and monitors it for any I/O error or clean
221 * close that triggers an unregistration of the application.
223 static struct ust_cmd_queue ust_cmd_queue
;
226 * Pointer initialized before thread creation.
228 * This points to the tracing session list containing the session count and a
229 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
230 * MUST NOT be taken if you call a public function in session.c.
232 * The lock is nested inside the structure: session_list_ptr->lock. Please use
233 * session_lock_list and session_unlock_list for lock acquisition.
235 static struct ltt_session_list
*session_list_ptr
;
237 int ust_consumerd64_fd
= -1;
238 int ust_consumerd32_fd
= -1;
240 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
241 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
242 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
243 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
244 static int consumerd32_bin_override
;
245 static int consumerd64_bin_override
;
246 static int consumerd32_libdir_override
;
247 static int consumerd64_libdir_override
;
249 static const char *module_proc_lttng
= "/proc/lttng";
252 * Consumer daemon state which is changed when spawning it, killing it or in
253 * case of a fatal error.
255 enum consumerd_state
{
256 CONSUMER_STARTED
= 1,
257 CONSUMER_STOPPED
= 2,
262 * This consumer daemon state is used to validate if a client command will be
263 * able to reach the consumer. If not, the client is informed. For instance,
264 * doing a "lttng start" when the consumer state is set to ERROR will return an
265 * error to the client.
267 * The following example shows a possible race condition of this scheme:
269 * consumer thread error happens
271 * client cmd checks state -> still OK
272 * consumer thread exit, sets error
273 * client cmd try to talk to consumer
276 * However, since the consumer is a different daemon, we have no way of making
277 * sure the command will reach it safely even with this state flag. This is why
278 * we consider that up to the state validation during command processing, the
279 * command is safe. After that, we can not guarantee the correctness of the
280 * client request vis-a-vis the consumer.
282 static enum consumerd_state ust_consumerd_state
;
283 static enum consumerd_state kernel_consumerd_state
;
286 * Socket timeout for receiving and sending in seconds.
288 static int app_socket_timeout
;
290 /* Set in main() with the current page size. */
293 /* Application health monitoring */
294 struct health_app
*health_sessiond
;
296 /* Agent TCP port for registration. Used by the agent thread. */
297 unsigned int agent_tcp_port
= DEFAULT_AGENT_TCP_PORT
;
299 /* Am I root or not. */
300 int is_root
; /* Set to 1 if the daemon is running as root */
302 const char * const config_section_name
= "sessiond";
304 /* Load session thread information to operate. */
305 struct load_session_thread_data
*load_info
;
308 * Whether sessiond is ready for commands/health check requests.
309 * NR_LTTNG_SESSIOND_READY must match the number of calls to
310 * sessiond_notify_ready().
312 #define NR_LTTNG_SESSIOND_READY 3
313 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
315 /* Notify parents that we are ready for cmd and health check */
317 void sessiond_notify_ready(void)
319 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
321 * Notify parent pid that we are ready to accept command
322 * for client side. This ppid is the one from the
323 * external process that spawned us.
325 if (opt_sig_parent
) {
330 * Notify the parent of the fork() process that we are
333 if (opt_daemon
|| opt_background
) {
334 kill(child_ppid
, SIGUSR1
);
340 void setup_consumerd_path(void)
342 const char *bin
, *libdir
;
345 * Allow INSTALL_BIN_PATH to be used as a target path for the
346 * native architecture size consumer if CONFIG_CONSUMER*_PATH
347 * has not been defined.
349 #if (CAA_BITS_PER_LONG == 32)
350 if (!consumerd32_bin
[0]) {
351 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
353 if (!consumerd32_libdir
[0]) {
354 consumerd32_libdir
= INSTALL_LIB_PATH
;
356 #elif (CAA_BITS_PER_LONG == 64)
357 if (!consumerd64_bin
[0]) {
358 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
360 if (!consumerd64_libdir
[0]) {
361 consumerd64_libdir
= INSTALL_LIB_PATH
;
364 #error "Unknown bitness"
368 * runtime env. var. overrides the build default.
370 bin
= getenv("LTTNG_CONSUMERD32_BIN");
372 consumerd32_bin
= bin
;
374 bin
= getenv("LTTNG_CONSUMERD64_BIN");
376 consumerd64_bin
= bin
;
378 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
380 consumerd32_libdir
= libdir
;
382 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
384 consumerd64_libdir
= libdir
;
389 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
396 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
402 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
414 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
416 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
418 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
422 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
424 int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event
*events
,
427 return __sessiond_set_thread_pollset(events
, size
,
428 ht_cleanup_quit_pipe
);
432 int __sessiond_check_thread_quit_pipe(int fd
, uint32_t events
, int a_pipe
)
434 if (fd
== a_pipe
&& (events
& LPOLLIN
)) {
441 * Check if the thread quit pipe was triggered.
443 * Return 1 if it was triggered else 0;
445 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
447 return __sessiond_check_thread_quit_pipe(fd
, events
,
448 thread_quit_pipe
[0]);
452 * Check if the ht_cleanup thread quit pipe was triggered.
454 * Return 1 if it was triggered else 0;
456 int sessiond_check_ht_cleanup_quit(int fd
, uint32_t events
)
458 return __sessiond_check_thread_quit_pipe(fd
, events
,
459 ht_cleanup_quit_pipe
[0]);
463 * Init thread quit pipe.
465 * Return -1 on error or 0 if all pipes are created.
467 static int __init_thread_quit_pipe(int *a_pipe
)
473 PERROR("thread quit pipe");
477 for (i
= 0; i
< 2; i
++) {
478 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
489 static int init_thread_quit_pipe(void)
491 return __init_thread_quit_pipe(thread_quit_pipe
);
494 static int init_ht_cleanup_quit_pipe(void)
496 return __init_thread_quit_pipe(ht_cleanup_quit_pipe
);
500 * Stop all threads by closing the thread quit pipe.
502 static void stop_threads(void)
506 /* Stopping all threads */
507 DBG("Terminating all threads");
508 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
510 ERR("write error on thread quit pipe");
513 /* Dispatch thread */
514 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
515 futex_nto1_wake(&ust_cmd_queue
.futex
);
519 * Close every consumer sockets.
521 static void close_consumer_sockets(void)
525 if (kconsumer_data
.err_sock
>= 0) {
526 ret
= close(kconsumer_data
.err_sock
);
528 PERROR("kernel consumer err_sock close");
531 if (ustconsumer32_data
.err_sock
>= 0) {
532 ret
= close(ustconsumer32_data
.err_sock
);
534 PERROR("UST consumerd32 err_sock close");
537 if (ustconsumer64_data
.err_sock
>= 0) {
538 ret
= close(ustconsumer64_data
.err_sock
);
540 PERROR("UST consumerd64 err_sock close");
543 if (kconsumer_data
.cmd_sock
>= 0) {
544 ret
= close(kconsumer_data
.cmd_sock
);
546 PERROR("kernel consumer cmd_sock close");
549 if (ustconsumer32_data
.cmd_sock
>= 0) {
550 ret
= close(ustconsumer32_data
.cmd_sock
);
552 PERROR("UST consumerd32 cmd_sock close");
555 if (ustconsumer64_data
.cmd_sock
>= 0) {
556 ret
= close(ustconsumer64_data
.cmd_sock
);
558 PERROR("UST consumerd64 cmd_sock close");
564 * Generate the full lock file path using the rundir.
566 * Return the snprintf() return value thus a negative value is an error.
568 static int generate_lock_file_path(char *path
, size_t len
)
575 /* Build lockfile path from rundir. */
576 ret
= snprintf(path
, len
, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE
, rundir
);
578 PERROR("snprintf lockfile path");
585 * Cleanup the session daemon's data structures.
587 static void sessiond_cleanup(void)
590 struct ltt_session
*sess
, *stmp
;
593 DBG("Cleanup sessiond");
596 * Close the thread quit pipe. It has already done its job,
597 * since we are now called.
599 utils_close_pipe(thread_quit_pipe
);
602 * If opt_pidfile is undefined, the default file will be wiped when
603 * removing the rundir.
606 ret
= remove(opt_pidfile
);
608 PERROR("remove pidfile %s", opt_pidfile
);
612 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
615 snprintf(path
, PATH_MAX
,
617 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
618 DBG("Removing %s", path
);
621 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
622 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
623 DBG("Removing %s", path
);
627 snprintf(path
, PATH_MAX
,
628 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
630 DBG("Removing %s", path
);
633 snprintf(path
, PATH_MAX
,
634 DEFAULT_KCONSUMERD_PATH
,
636 DBG("Removing directory %s", path
);
639 /* ust consumerd 32 */
640 snprintf(path
, PATH_MAX
,
641 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
643 DBG("Removing %s", path
);
646 snprintf(path
, PATH_MAX
,
647 DEFAULT_USTCONSUMERD32_PATH
,
649 DBG("Removing directory %s", path
);
652 /* ust consumerd 64 */
653 snprintf(path
, PATH_MAX
,
654 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
656 DBG("Removing %s", path
);
659 snprintf(path
, PATH_MAX
,
660 DEFAULT_USTCONSUMERD64_PATH
,
662 DBG("Removing directory %s", path
);
665 DBG("Cleaning up all sessions");
667 /* Destroy session list mutex */
668 if (session_list_ptr
!= NULL
) {
669 pthread_mutex_destroy(&session_list_ptr
->lock
);
671 /* Cleanup ALL session */
672 cds_list_for_each_entry_safe(sess
, stmp
,
673 &session_list_ptr
->head
, list
) {
674 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
678 DBG("Closing all UST sockets");
679 ust_app_clean_list();
680 buffer_reg_destroy_registries();
682 if (is_root
&& !opt_no_kernel
) {
683 DBG2("Closing kernel fd");
684 if (kernel_tracer_fd
>= 0) {
685 ret
= close(kernel_tracer_fd
);
690 DBG("Unloading kernel modules");
691 modprobe_remove_lttng_all();
695 close_consumer_sockets();
698 load_session_destroy_data(load_info
);
703 * Cleanup lock file by deleting it and finaly closing it which will
704 * release the file system lock.
706 if (lockfile_fd
>= 0) {
707 char lockfile_path
[PATH_MAX
];
709 ret
= generate_lock_file_path(lockfile_path
,
710 sizeof(lockfile_path
));
712 ret
= remove(lockfile_path
);
714 PERROR("remove lock file");
716 ret
= close(lockfile_fd
);
718 PERROR("close lock file");
724 * We do NOT rmdir rundir because there are other processes
725 * using it, for instance lttng-relayd, which can start in
726 * parallel with this teardown.
733 * Cleanup the daemon's option data structures.
735 static void sessiond_cleanup_options(void)
737 DBG("Cleaning up options");
740 * If the override option is set, the pointer points to a *non* const
741 * thus freeing it even though the variable type is set to const.
743 if (tracing_group_name_override
) {
744 free((void *) tracing_group_name
);
746 if (consumerd32_bin_override
) {
747 free((void *) consumerd32_bin
);
749 if (consumerd64_bin_override
) {
750 free((void *) consumerd64_bin
);
752 if (consumerd32_libdir_override
) {
753 free((void *) consumerd32_libdir
);
755 if (consumerd64_libdir_override
) {
756 free((void *) consumerd64_libdir
);
760 free(opt_load_session_path
);
761 free(kmod_probes_list
);
762 free(kmod_extra_probes_list
);
765 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
766 "Matthew, BEET driven development works!%c[%dm",
767 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
772 * Send data on a unix socket using the liblttsessiondcomm API.
774 * Return lttcomm error code.
776 static int send_unix_sock(int sock
, void *buf
, size_t len
)
778 /* Check valid length */
783 return lttcomm_send_unix_sock(sock
, buf
, len
);
787 * Free memory of a command context structure.
789 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
791 DBG("Clean command context structure");
793 if ((*cmd_ctx
)->llm
) {
794 free((*cmd_ctx
)->llm
);
796 if ((*cmd_ctx
)->lsm
) {
797 free((*cmd_ctx
)->lsm
);
805 * Notify UST applications using the shm mmap futex.
807 static int notify_ust_apps(int active
)
811 DBG("Notifying applications of session daemon state: %d", active
);
813 /* See shm.c for this call implying mmap, shm and futex calls */
814 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
815 if (wait_shm_mmap
== NULL
) {
819 /* Wake waiting process */
820 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
822 /* Apps notified successfully */
830 * Setup the outgoing data buffer for the response (llm) by allocating the
831 * right amount of memory and copying the original information from the lsm
834 * Return total size of the buffer pointed by buf.
836 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
842 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
843 if (cmd_ctx
->llm
== NULL
) {
849 /* Copy common data */
850 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
851 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
853 cmd_ctx
->llm
->data_size
= size
;
854 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
863 * Update the kernel poll set of all channel fd available over all tracing
864 * session. Add the wakeup pipe at the end of the set.
866 static int update_kernel_poll(struct lttng_poll_event
*events
)
869 struct ltt_session
*session
;
870 struct ltt_kernel_channel
*channel
;
872 DBG("Updating kernel poll set");
875 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
876 session_lock(session
);
877 if (session
->kernel_session
== NULL
) {
878 session_unlock(session
);
882 cds_list_for_each_entry(channel
,
883 &session
->kernel_session
->channel_list
.head
, list
) {
884 /* Add channel fd to the kernel poll set */
885 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
887 session_unlock(session
);
890 DBG("Channel fd %d added to kernel set", channel
->fd
);
892 session_unlock(session
);
894 session_unlock_list();
899 session_unlock_list();
904 * Find the channel fd from 'fd' over all tracing session. When found, check
905 * for new channel stream and send those stream fds to the kernel consumer.
907 * Useful for CPU hotplug feature.
909 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
912 struct ltt_session
*session
;
913 struct ltt_kernel_session
*ksess
;
914 struct ltt_kernel_channel
*channel
;
916 DBG("Updating kernel streams for channel fd %d", fd
);
919 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
920 session_lock(session
);
921 if (session
->kernel_session
== NULL
) {
922 session_unlock(session
);
925 ksess
= session
->kernel_session
;
927 cds_list_for_each_entry(channel
,
928 &ksess
->channel_list
.head
, list
) {
929 struct lttng_ht_iter iter
;
930 struct consumer_socket
*socket
;
932 if (channel
->fd
!= fd
) {
935 DBG("Channel found, updating kernel streams");
936 ret
= kernel_open_channel_stream(channel
);
940 /* Update the stream global counter */
941 ksess
->stream_count_global
+= ret
;
944 * Have we already sent fds to the consumer? If yes, it
945 * means that tracing is started so it is safe to send
946 * our updated stream fds.
948 if (ksess
->consumer_fds_sent
!= 1
949 || ksess
->consumer
== NULL
) {
955 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
956 &iter
.iter
, socket
, node
.node
) {
957 pthread_mutex_lock(socket
->lock
);
958 ret
= kernel_consumer_send_channel_stream(socket
,
960 session
->output_traces
? 1 : 0);
961 pthread_mutex_unlock(socket
->lock
);
969 session_unlock(session
);
971 session_unlock_list();
975 session_unlock(session
);
976 session_unlock_list();
981 * For each tracing session, update newly registered apps. The session list
982 * lock MUST be acquired before calling this.
984 static void update_ust_app(int app_sock
)
986 struct ltt_session
*sess
, *stmp
;
988 /* Consumer is in an ERROR state. Stop any application update. */
989 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
990 /* Stop the update process since the consumer is dead. */
994 /* For all tracing session(s) */
995 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
997 if (sess
->ust_session
) {
998 ust_app_global_update(sess
->ust_session
, app_sock
);
1000 session_unlock(sess
);
1005 * This thread manage event coming from the kernel.
1007 * Features supported in this thread:
1010 static void *thread_manage_kernel(void *data
)
1012 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
1013 uint32_t revents
, nb_fd
;
1015 struct lttng_poll_event events
;
1017 DBG("[thread] Thread manage kernel started");
1019 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
1022 * This first step of the while is to clean this structure which could free
1023 * non NULL pointers so initialize it before the loop.
1025 lttng_poll_init(&events
);
1027 if (testpoint(sessiond_thread_manage_kernel
)) {
1028 goto error_testpoint
;
1031 health_code_update();
1033 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1034 goto error_testpoint
;
1038 health_code_update();
1040 if (update_poll_flag
== 1) {
1041 /* Clean events object. We are about to populate it again. */
1042 lttng_poll_clean(&events
);
1044 ret
= sessiond_set_thread_pollset(&events
, 2);
1046 goto error_poll_create
;
1049 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1054 /* This will add the available kernel channel if any. */
1055 ret
= update_kernel_poll(&events
);
1059 update_poll_flag
= 0;
1062 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events
));
1064 /* Poll infinite value of time */
1066 health_poll_entry();
1067 ret
= lttng_poll_wait(&events
, -1);
1071 * Restart interrupted system call.
1073 if (errno
== EINTR
) {
1077 } else if (ret
== 0) {
1078 /* Should not happen since timeout is infinite */
1079 ERR("Return value of poll is 0 with an infinite timeout.\n"
1080 "This should not have happened! Continuing...");
1086 for (i
= 0; i
< nb_fd
; i
++) {
1087 /* Fetch once the poll data */
1088 revents
= LTTNG_POLL_GETEV(&events
, i
);
1089 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1091 health_code_update();
1093 /* Thread quit pipe has been closed. Killing thread. */
1094 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1100 /* Check for data on kernel pipe */
1101 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
1102 (void) lttng_read(kernel_poll_pipe
[0],
1105 * Ret value is useless here, if this pipe gets any actions an
1106 * update is required anyway.
1108 update_poll_flag
= 1;
1112 * New CPU detected by the kernel. Adding kernel stream to
1113 * kernel session and updating the kernel consumer
1115 if (revents
& LPOLLIN
) {
1116 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1122 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
1123 * and unregister kernel stream at this point.
1132 lttng_poll_clean(&events
);
1135 utils_close_pipe(kernel_poll_pipe
);
1136 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1139 ERR("Health error occurred in %s", __func__
);
1140 WARN("Kernel thread died unexpectedly. "
1141 "Kernel tracing can continue but CPU hotplug is disabled.");
1143 health_unregister(health_sessiond
);
1144 DBG("Kernel thread dying");
1149 * Signal pthread condition of the consumer data that the thread.
1151 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1153 pthread_mutex_lock(&data
->cond_mutex
);
1156 * The state is set before signaling. It can be any value, it's the waiter
1157 * job to correctly interpret this condition variable associated to the
1158 * consumer pthread_cond.
1160 * A value of 0 means that the corresponding thread of the consumer data
1161 * was not started. 1 indicates that the thread has started and is ready
1162 * for action. A negative value means that there was an error during the
1165 data
->consumer_thread_is_ready
= state
;
1166 (void) pthread_cond_signal(&data
->cond
);
1168 pthread_mutex_unlock(&data
->cond_mutex
);
1172 * This thread manage the consumer error sent back to the session daemon.
1174 static void *thread_manage_consumer(void *data
)
1176 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1177 uint32_t revents
, nb_fd
;
1178 enum lttcomm_return_code code
;
1179 struct lttng_poll_event events
;
1180 struct consumer_data
*consumer_data
= data
;
1182 DBG("[thread] Manage consumer started");
1184 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1186 health_code_update();
1189 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1190 * metadata_sock. Nothing more will be added to this poll set.
1192 ret
= sessiond_set_thread_pollset(&events
, 3);
1198 * The error socket here is already in a listening state which was done
1199 * just before spawning this thread to avoid a race between the consumer
1200 * daemon exec trying to connect and the listen() call.
1202 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1207 health_code_update();
1209 /* Infinite blocking call, waiting for transmission */
1211 health_poll_entry();
1213 if (testpoint(sessiond_thread_manage_consumer
)) {
1217 ret
= lttng_poll_wait(&events
, -1);
1221 * Restart interrupted system call.
1223 if (errno
== EINTR
) {
1231 for (i
= 0; i
< nb_fd
; i
++) {
1232 /* Fetch once the poll data */
1233 revents
= LTTNG_POLL_GETEV(&events
, i
);
1234 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1236 health_code_update();
1238 /* Thread quit pipe has been closed. Killing thread. */
1239 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1245 /* Event on the registration socket */
1246 if (pollfd
== consumer_data
->err_sock
) {
1247 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1248 ERR("consumer err socket poll error");
1254 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1260 * Set the CLOEXEC flag. Return code is useless because either way, the
1263 (void) utils_set_fd_cloexec(sock
);
1265 health_code_update();
1267 DBG2("Receiving code from consumer err_sock");
1269 /* Getting status code from kconsumerd */
1270 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1271 sizeof(enum lttcomm_return_code
));
1276 health_code_update();
1277 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1278 /* Connect both socket, command and metadata. */
1279 consumer_data
->cmd_sock
=
1280 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1281 consumer_data
->metadata_fd
=
1282 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1283 if (consumer_data
->cmd_sock
< 0
1284 || consumer_data
->metadata_fd
< 0) {
1285 PERROR("consumer connect cmd socket");
1286 /* On error, signal condition and quit. */
1287 signal_consumer_condition(consumer_data
, -1);
1290 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1291 /* Create metadata socket lock. */
1292 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1293 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1294 PERROR("zmalloc pthread mutex");
1298 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1300 signal_consumer_condition(consumer_data
, 1);
1301 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1302 DBG("Consumer metadata socket ready (fd: %d)",
1303 consumer_data
->metadata_fd
);
1305 ERR("consumer error when waiting for SOCK_READY : %s",
1306 lttcomm_get_readable_code(-code
));
1310 /* Remove the consumerd error sock since we've established a connexion */
1311 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1316 /* Add new accepted error socket. */
1317 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1322 /* Add metadata socket that is successfully connected. */
1323 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1324 LPOLLIN
| LPOLLRDHUP
);
1329 health_code_update();
1331 /* Infinite blocking call, waiting for transmission */
1334 health_code_update();
1336 /* Exit the thread because the thread quit pipe has been triggered. */
1338 /* Not a health error. */
1343 health_poll_entry();
1344 ret
= lttng_poll_wait(&events
, -1);
1348 * Restart interrupted system call.
1350 if (errno
== EINTR
) {
1358 for (i
= 0; i
< nb_fd
; i
++) {
1359 /* Fetch once the poll data */
1360 revents
= LTTNG_POLL_GETEV(&events
, i
);
1361 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1363 health_code_update();
1366 * Thread quit pipe has been triggered, flag that we should stop
1367 * but continue the current loop to handle potential data from
1370 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1372 if (pollfd
== sock
) {
1373 /* Event on the consumerd socket */
1374 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1375 ERR("consumer err socket second poll error");
1378 health_code_update();
1379 /* Wait for any kconsumerd error */
1380 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1381 sizeof(enum lttcomm_return_code
));
1383 ERR("consumer closed the command socket");
1387 ERR("consumer return code : %s",
1388 lttcomm_get_readable_code(-code
));
1391 } else if (pollfd
== consumer_data
->metadata_fd
) {
1392 /* UST metadata requests */
1393 ret
= ust_consumer_metadata_request(
1394 &consumer_data
->metadata_sock
);
1396 ERR("Handling metadata request");
1400 /* No need for an else branch all FDs are tested prior. */
1402 health_code_update();
1408 * We lock here because we are about to close the sockets and some other
1409 * thread might be using them so get exclusive access which will abort all
1410 * other consumer command by other threads.
1412 pthread_mutex_lock(&consumer_data
->lock
);
1414 /* Immediately set the consumerd state to stopped */
1415 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1416 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1417 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1418 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1419 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1421 /* Code flow error... */
1425 if (consumer_data
->err_sock
>= 0) {
1426 ret
= close(consumer_data
->err_sock
);
1430 consumer_data
->err_sock
= -1;
1432 if (consumer_data
->cmd_sock
>= 0) {
1433 ret
= close(consumer_data
->cmd_sock
);
1437 consumer_data
->cmd_sock
= -1;
1439 if (consumer_data
->metadata_sock
.fd_ptr
&&
1440 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1441 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1453 unlink(consumer_data
->err_unix_sock_path
);
1454 unlink(consumer_data
->cmd_unix_sock_path
);
1455 consumer_data
->pid
= 0;
1456 pthread_mutex_unlock(&consumer_data
->lock
);
1458 /* Cleanup metadata socket mutex. */
1459 if (consumer_data
->metadata_sock
.lock
) {
1460 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1461 free(consumer_data
->metadata_sock
.lock
);
1463 lttng_poll_clean(&events
);
1467 ERR("Health error occurred in %s", __func__
);
1469 health_unregister(health_sessiond
);
1470 DBG("consumer thread cleanup completed");
1476 * This thread manage application communication.
1478 static void *thread_manage_apps(void *data
)
1480 int i
, ret
, pollfd
, err
= -1;
1482 uint32_t revents
, nb_fd
;
1483 struct lttng_poll_event events
;
1485 DBG("[thread] Manage application started");
1487 rcu_register_thread();
1488 rcu_thread_online();
1490 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1492 if (testpoint(sessiond_thread_manage_apps
)) {
1493 goto error_testpoint
;
1496 health_code_update();
1498 ret
= sessiond_set_thread_pollset(&events
, 2);
1500 goto error_poll_create
;
1503 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1508 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1512 health_code_update();
1515 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events
));
1517 /* Inifinite blocking call, waiting for transmission */
1519 health_poll_entry();
1520 ret
= lttng_poll_wait(&events
, -1);
1524 * Restart interrupted system call.
1526 if (errno
== EINTR
) {
1534 for (i
= 0; i
< nb_fd
; i
++) {
1535 /* Fetch once the poll data */
1536 revents
= LTTNG_POLL_GETEV(&events
, i
);
1537 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1539 health_code_update();
1541 /* Thread quit pipe has been closed. Killing thread. */
1542 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1548 /* Inspect the apps cmd pipe */
1549 if (pollfd
== apps_cmd_pipe
[0]) {
1550 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1551 ERR("Apps command pipe error");
1553 } else if (revents
& LPOLLIN
) {
1557 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1558 if (size_ret
< sizeof(sock
)) {
1559 PERROR("read apps cmd pipe");
1563 health_code_update();
1566 * We only monitor the error events of the socket. This
1567 * thread does not handle any incoming data from UST
1570 ret
= lttng_poll_add(&events
, sock
,
1571 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1576 DBG("Apps with sock %d added to poll set", sock
);
1580 * At this point, we know that a registered application made
1581 * the event at poll_wait.
1583 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1584 /* Removing from the poll set */
1585 ret
= lttng_poll_del(&events
, pollfd
);
1590 /* Socket closed on remote end. */
1591 ust_app_unregister(pollfd
);
1595 health_code_update();
1601 lttng_poll_clean(&events
);
1604 utils_close_pipe(apps_cmd_pipe
);
1605 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1608 * We don't clean the UST app hash table here since already registered
1609 * applications can still be controlled so let them be until the session
1610 * daemon dies or the applications stop.
1615 ERR("Health error occurred in %s", __func__
);
1617 health_unregister(health_sessiond
);
1618 DBG("Application communication apps thread cleanup complete");
1619 rcu_thread_offline();
1620 rcu_unregister_thread();
1625 * Send a socket to a thread This is called from the dispatch UST registration
1626 * thread once all sockets are set for the application.
1628 * The sock value can be invalid, we don't really care, the thread will handle
1629 * it and make the necessary cleanup if so.
1631 * On success, return 0 else a negative value being the errno message of the
1634 static int send_socket_to_thread(int fd
, int sock
)
1639 * It's possible that the FD is set as invalid with -1 concurrently just
1640 * before calling this function being a shutdown state of the thread.
1647 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1648 if (ret
< sizeof(sock
)) {
1649 PERROR("write apps pipe %d", fd
);
1656 /* All good. Don't send back the write positive ret value. */
1663 * Sanitize the wait queue of the dispatch registration thread meaning removing
1664 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1665 * notify socket is never received.
1667 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1669 int ret
, nb_fd
= 0, i
;
1670 unsigned int fd_added
= 0;
1671 struct lttng_poll_event events
;
1672 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1676 lttng_poll_init(&events
);
1678 /* Just skip everything for an empty queue. */
1679 if (!wait_queue
->count
) {
1683 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1688 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1689 &wait_queue
->head
, head
) {
1690 assert(wait_node
->app
);
1691 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1692 LPOLLHUP
| LPOLLERR
);
1705 * Poll but don't block so we can quickly identify the faulty events and
1706 * clean them afterwards from the wait queue.
1708 ret
= lttng_poll_wait(&events
, 0);
1714 for (i
= 0; i
< nb_fd
; i
++) {
1715 /* Get faulty FD. */
1716 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1717 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1719 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1720 &wait_queue
->head
, head
) {
1721 if (pollfd
== wait_node
->app
->sock
&&
1722 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1723 cds_list_del(&wait_node
->head
);
1724 wait_queue
->count
--;
1725 ust_app_destroy(wait_node
->app
);
1733 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1737 lttng_poll_clean(&events
);
1741 lttng_poll_clean(&events
);
1743 ERR("Unable to sanitize wait queue");
1748 * Dispatch request from the registration threads to the application
1749 * communication thread.
1751 static void *thread_dispatch_ust_registration(void *data
)
1754 struct cds_wfcq_node
*node
;
1755 struct ust_command
*ust_cmd
= NULL
;
1756 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1757 struct ust_reg_wait_queue wait_queue
= {
1761 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1763 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1764 goto error_testpoint
;
1767 health_code_update();
1769 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1771 DBG("[thread] Dispatch UST command started");
1773 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1774 health_code_update();
1776 /* Atomically prepare the queue futex */
1777 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1780 struct ust_app
*app
= NULL
;
1784 * Make sure we don't have node(s) that have hung up before receiving
1785 * the notify socket. This is to clean the list in order to avoid
1786 * memory leaks from notify socket that are never seen.
1788 sanitize_wait_queue(&wait_queue
);
1790 health_code_update();
1791 /* Dequeue command for registration */
1792 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1794 DBG("Woken up but nothing in the UST command queue");
1795 /* Continue thread execution */
1799 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1801 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1802 " gid:%d sock:%d name:%s (version %d.%d)",
1803 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1804 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1805 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1806 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1808 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1809 wait_node
= zmalloc(sizeof(*wait_node
));
1811 PERROR("zmalloc wait_node dispatch");
1812 ret
= close(ust_cmd
->sock
);
1814 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1816 lttng_fd_put(LTTNG_FD_APPS
, 1);
1820 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1822 /* Create application object if socket is CMD. */
1823 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1825 if (!wait_node
->app
) {
1826 ret
= close(ust_cmd
->sock
);
1828 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1830 lttng_fd_put(LTTNG_FD_APPS
, 1);
1836 * Add application to the wait queue so we can set the notify
1837 * socket before putting this object in the global ht.
1839 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1844 * We have to continue here since we don't have the notify
1845 * socket and the application MUST be added to the hash table
1846 * only at that moment.
1851 * Look for the application in the local wait queue and set the
1852 * notify socket if found.
1854 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1855 &wait_queue
.head
, head
) {
1856 health_code_update();
1857 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1858 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1859 cds_list_del(&wait_node
->head
);
1861 app
= wait_node
->app
;
1863 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1869 * With no application at this stage the received socket is
1870 * basically useless so close it before we free the cmd data
1871 * structure for good.
1874 ret
= close(ust_cmd
->sock
);
1876 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1878 lttng_fd_put(LTTNG_FD_APPS
, 1);
1885 * @session_lock_list
1887 * Lock the global session list so from the register up to the
1888 * registration done message, no thread can see the application
1889 * and change its state.
1891 session_lock_list();
1895 * Add application to the global hash table. This needs to be
1896 * done before the update to the UST registry can locate the
1901 /* Set app version. This call will print an error if needed. */
1902 (void) ust_app_version(app
);
1904 /* Send notify socket through the notify pipe. */
1905 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1909 session_unlock_list();
1911 * No notify thread, stop the UST tracing. However, this is
1912 * not an internal error of the this thread thus setting
1913 * the health error code to a normal exit.
1920 * Update newly registered application with the tracing
1921 * registry info already enabled information.
1923 update_ust_app(app
->sock
);
1926 * Don't care about return value. Let the manage apps threads
1927 * handle app unregistration upon socket close.
1929 (void) ust_app_register_done(app
->sock
);
1932 * Even if the application socket has been closed, send the app
1933 * to the thread and unregistration will take place at that
1936 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1939 session_unlock_list();
1941 * No apps. thread, stop the UST tracing. However, this is
1942 * not an internal error of the this thread thus setting
1943 * the health error code to a normal exit.
1950 session_unlock_list();
1952 } while (node
!= NULL
);
1954 health_poll_entry();
1955 /* Futex wait on queue. Blocking call on futex() */
1956 futex_nto1_wait(&ust_cmd_queue
.futex
);
1959 /* Normal exit, no error */
1963 /* Clean up wait queue. */
1964 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1965 &wait_queue
.head
, head
) {
1966 cds_list_del(&wait_node
->head
);
1972 DBG("Dispatch thread dying");
1975 ERR("Health error occurred in %s", __func__
);
1977 health_unregister(health_sessiond
);
1982 * This thread manage application registration.
1984 static void *thread_registration_apps(void *data
)
1986 int sock
= -1, i
, ret
, pollfd
, err
= -1;
1987 uint32_t revents
, nb_fd
;
1988 struct lttng_poll_event events
;
1990 * Get allocated in this thread, enqueued to a global queue, dequeued and
1991 * freed in the manage apps thread.
1993 struct ust_command
*ust_cmd
= NULL
;
1995 DBG("[thread] Manage application registration started");
1997 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
1999 if (testpoint(sessiond_thread_registration_apps
)) {
2000 goto error_testpoint
;
2003 ret
= lttcomm_listen_unix_sock(apps_sock
);
2009 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2010 * more will be added to this poll set.
2012 ret
= sessiond_set_thread_pollset(&events
, 2);
2014 goto error_create_poll
;
2017 /* Add the application registration socket */
2018 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2020 goto error_poll_add
;
2023 /* Notify all applications to register */
2024 ret
= notify_ust_apps(1);
2026 ERR("Failed to notify applications or create the wait shared memory.\n"
2027 "Execution continues but there might be problem for already\n"
2028 "running applications that wishes to register.");
2032 DBG("Accepting application registration");
2034 /* Inifinite blocking call, waiting for transmission */
2036 health_poll_entry();
2037 ret
= lttng_poll_wait(&events
, -1);
2041 * Restart interrupted system call.
2043 if (errno
== EINTR
) {
2051 for (i
= 0; i
< nb_fd
; i
++) {
2052 health_code_update();
2054 /* Fetch once the poll data */
2055 revents
= LTTNG_POLL_GETEV(&events
, i
);
2056 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2058 /* Thread quit pipe has been closed. Killing thread. */
2059 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2065 /* Event on the registration socket */
2066 if (pollfd
== apps_sock
) {
2067 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2068 ERR("Register apps socket poll error");
2070 } else if (revents
& LPOLLIN
) {
2071 sock
= lttcomm_accept_unix_sock(apps_sock
);
2077 * Set socket timeout for both receiving and ending.
2078 * app_socket_timeout is in seconds, whereas
2079 * lttcomm_setsockopt_rcv_timeout and
2080 * lttcomm_setsockopt_snd_timeout expect msec as
2083 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2084 app_socket_timeout
* 1000);
2085 (void) lttcomm_setsockopt_snd_timeout(sock
,
2086 app_socket_timeout
* 1000);
2089 * Set the CLOEXEC flag. Return code is useless because
2090 * either way, the show must go on.
2092 (void) utils_set_fd_cloexec(sock
);
2094 /* Create UST registration command for enqueuing */
2095 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2096 if (ust_cmd
== NULL
) {
2097 PERROR("ust command zmalloc");
2102 * Using message-based transmissions to ensure we don't
2103 * have to deal with partially received messages.
2105 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2107 ERR("Exhausted file descriptors allowed for applications.");
2117 health_code_update();
2118 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2121 /* Close socket of the application. */
2126 lttng_fd_put(LTTNG_FD_APPS
, 1);
2130 health_code_update();
2132 ust_cmd
->sock
= sock
;
2135 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2136 " gid:%d sock:%d name:%s (version %d.%d)",
2137 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2138 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2139 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2140 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2143 * Lock free enqueue the registration request. The red pill
2144 * has been taken! This apps will be part of the *system*.
2146 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2149 * Wake the registration queue futex. Implicit memory
2150 * barrier with the exchange in cds_wfcq_enqueue.
2152 futex_nto1_wake(&ust_cmd_queue
.futex
);
2160 /* Notify that the registration thread is gone */
2163 if (apps_sock
>= 0) {
2164 ret
= close(apps_sock
);
2174 lttng_fd_put(LTTNG_FD_APPS
, 1);
2176 unlink(apps_unix_sock_path
);
2179 lttng_poll_clean(&events
);
2183 DBG("UST Registration thread cleanup complete");
2186 ERR("Health error occurred in %s", __func__
);
2188 health_unregister(health_sessiond
);
2194 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2195 * exec or it will fails.
2197 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2200 struct timespec timeout
;
2202 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2203 consumer_data
->consumer_thread_is_ready
= 0;
2205 /* Setup pthread condition */
2206 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2209 PERROR("pthread_condattr_init consumer data");
2214 * Set the monotonic clock in order to make sure we DO NOT jump in time
2215 * between the clock_gettime() call and the timedwait call. See bug #324
2216 * for a more details and how we noticed it.
2218 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2221 PERROR("pthread_condattr_setclock consumer data");
2225 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2228 PERROR("pthread_cond_init consumer data");
2232 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2236 PERROR("pthread_create consumer");
2241 /* We are about to wait on a pthread condition */
2242 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2244 /* Get time for sem_timedwait absolute timeout */
2245 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2247 * Set the timeout for the condition timed wait even if the clock gettime
2248 * call fails since we might loop on that call and we want to avoid to
2249 * increment the timeout too many times.
2251 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2254 * The following loop COULD be skipped in some conditions so this is why we
2255 * set ret to 0 in order to make sure at least one round of the loop is
2261 * Loop until the condition is reached or when a timeout is reached. Note
2262 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2263 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2264 * possible. This loop does not take any chances and works with both of
2267 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2268 if (clock_ret
< 0) {
2269 PERROR("clock_gettime spawn consumer");
2270 /* Infinite wait for the consumerd thread to be ready */
2271 ret
= pthread_cond_wait(&consumer_data
->cond
,
2272 &consumer_data
->cond_mutex
);
2274 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2275 &consumer_data
->cond_mutex
, &timeout
);
2279 /* Release the pthread condition */
2280 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2284 if (ret
== ETIMEDOUT
) {
2288 * Call has timed out so we kill the kconsumerd_thread and return
2291 ERR("Condition timed out. The consumer thread was never ready."
2293 pth_ret
= pthread_cancel(consumer_data
->thread
);
2295 PERROR("pthread_cancel consumer thread");
2298 PERROR("pthread_cond_wait failed consumer thread");
2300 /* Caller is expecting a negative value on failure. */
2305 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2306 if (consumer_data
->pid
== 0) {
2307 ERR("Consumerd did not start");
2308 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2311 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2320 * Join consumer thread
2322 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2326 /* Consumer pid must be a real one. */
2327 if (consumer_data
->pid
> 0) {
2329 ret
= kill(consumer_data
->pid
, SIGTERM
);
2331 PERROR("Error killing consumer daemon");
2334 return pthread_join(consumer_data
->thread
, &status
);
2341 * Fork and exec a consumer daemon (consumerd).
2343 * Return pid if successful else -1.
2345 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2349 const char *consumer_to_use
;
2350 const char *verbosity
;
2353 DBG("Spawning consumerd");
2360 if (opt_verbose_consumer
) {
2361 verbosity
= "--verbose";
2362 } else if (lttng_opt_quiet
) {
2363 verbosity
= "--quiet";
2368 switch (consumer_data
->type
) {
2369 case LTTNG_CONSUMER_KERNEL
:
2371 * Find out which consumerd to execute. We will first try the
2372 * 64-bit path, then the sessiond's installation directory, and
2373 * fallback on the 32-bit one,
2375 DBG3("Looking for a kernel consumer at these locations:");
2376 DBG3(" 1) %s", consumerd64_bin
);
2377 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2378 DBG3(" 3) %s", consumerd32_bin
);
2379 if (stat(consumerd64_bin
, &st
) == 0) {
2380 DBG3("Found location #1");
2381 consumer_to_use
= consumerd64_bin
;
2382 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2383 DBG3("Found location #2");
2384 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2385 } else if (stat(consumerd32_bin
, &st
) == 0) {
2386 DBG3("Found location #3");
2387 consumer_to_use
= consumerd32_bin
;
2389 DBG("Could not find any valid consumerd executable");
2393 DBG("Using kernel consumer at: %s", consumer_to_use
);
2394 ret
= execl(consumer_to_use
,
2395 "lttng-consumerd", verbosity
, "-k",
2396 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2397 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2398 "--group", tracing_group_name
,
2401 case LTTNG_CONSUMER64_UST
:
2403 char *tmpnew
= NULL
;
2405 if (consumerd64_libdir
[0] != '\0') {
2409 tmp
= getenv("LD_LIBRARY_PATH");
2413 tmplen
= strlen("LD_LIBRARY_PATH=")
2414 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2415 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2420 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2421 strcat(tmpnew
, consumerd64_libdir
);
2422 if (tmp
[0] != '\0') {
2423 strcat(tmpnew
, ":");
2424 strcat(tmpnew
, tmp
);
2426 ret
= putenv(tmpnew
);
2433 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2434 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2435 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2436 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2437 "--group", tracing_group_name
,
2439 if (consumerd64_libdir
[0] != '\0') {
2444 case LTTNG_CONSUMER32_UST
:
2446 char *tmpnew
= NULL
;
2448 if (consumerd32_libdir
[0] != '\0') {
2452 tmp
= getenv("LD_LIBRARY_PATH");
2456 tmplen
= strlen("LD_LIBRARY_PATH=")
2457 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2458 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2463 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2464 strcat(tmpnew
, consumerd32_libdir
);
2465 if (tmp
[0] != '\0') {
2466 strcat(tmpnew
, ":");
2467 strcat(tmpnew
, tmp
);
2469 ret
= putenv(tmpnew
);
2476 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2477 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2478 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2479 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2480 "--group", tracing_group_name
,
2482 if (consumerd32_libdir
[0] != '\0') {
2488 PERROR("unknown consumer type");
2492 PERROR("Consumer execl()");
2494 /* Reaching this point, we got a failure on our execl(). */
2496 } else if (pid
> 0) {
2499 PERROR("start consumer fork");
2507 * Spawn the consumerd daemon and session daemon thread.
2509 static int start_consumerd(struct consumer_data
*consumer_data
)
2514 * Set the listen() state on the socket since there is a possible race
2515 * between the exec() of the consumer daemon and this call if place in the
2516 * consumer thread. See bug #366 for more details.
2518 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2523 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2524 if (consumer_data
->pid
!= 0) {
2525 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2529 ret
= spawn_consumerd(consumer_data
);
2531 ERR("Spawning consumerd failed");
2532 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2536 /* Setting up the consumer_data pid */
2537 consumer_data
->pid
= ret
;
2538 DBG2("Consumer pid %d", consumer_data
->pid
);
2539 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2541 DBG2("Spawning consumer control thread");
2542 ret
= spawn_consumer_thread(consumer_data
);
2544 ERR("Fatal error spawning consumer control thread");
2552 /* Cleanup already created sockets on error. */
2553 if (consumer_data
->err_sock
>= 0) {
2556 err
= close(consumer_data
->err_sock
);
2558 PERROR("close consumer data error socket");
2565 * Setup necessary data for kernel tracer action.
2567 static int init_kernel_tracer(void)
2571 /* Modprobe lttng kernel modules */
2572 ret
= modprobe_lttng_control();
2577 /* Open debugfs lttng */
2578 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2579 if (kernel_tracer_fd
< 0) {
2580 DBG("Failed to open %s", module_proc_lttng
);
2585 /* Validate kernel version */
2586 ret
= kernel_validate_version(kernel_tracer_fd
);
2591 ret
= modprobe_lttng_data();
2596 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2600 modprobe_remove_lttng_control();
2601 ret
= close(kernel_tracer_fd
);
2605 kernel_tracer_fd
= -1;
2606 return LTTNG_ERR_KERN_VERSION
;
2609 ret
= close(kernel_tracer_fd
);
2615 modprobe_remove_lttng_control();
2618 WARN("No kernel tracer available");
2619 kernel_tracer_fd
= -1;
2621 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2623 return LTTNG_ERR_KERN_NA
;
2629 * Copy consumer output from the tracing session to the domain session. The
2630 * function also applies the right modification on a per domain basis for the
2631 * trace files destination directory.
2633 * Should *NOT* be called with RCU read-side lock held.
2635 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2638 const char *dir_name
;
2639 struct consumer_output
*consumer
;
2642 assert(session
->consumer
);
2645 case LTTNG_DOMAIN_KERNEL
:
2646 DBG3("Copying tracing session consumer output in kernel session");
2648 * XXX: We should audit the session creation and what this function
2649 * does "extra" in order to avoid a destroy since this function is used
2650 * in the domain session creation (kernel and ust) only. Same for UST
2653 if (session
->kernel_session
->consumer
) {
2654 consumer_destroy_output(session
->kernel_session
->consumer
);
2656 session
->kernel_session
->consumer
=
2657 consumer_copy_output(session
->consumer
);
2658 /* Ease our life a bit for the next part */
2659 consumer
= session
->kernel_session
->consumer
;
2660 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2662 case LTTNG_DOMAIN_JUL
:
2663 case LTTNG_DOMAIN_LOG4J
:
2664 case LTTNG_DOMAIN_PYTHON
:
2665 case LTTNG_DOMAIN_UST
:
2666 DBG3("Copying tracing session consumer output in UST session");
2667 if (session
->ust_session
->consumer
) {
2668 consumer_destroy_output(session
->ust_session
->consumer
);
2670 session
->ust_session
->consumer
=
2671 consumer_copy_output(session
->consumer
);
2672 /* Ease our life a bit for the next part */
2673 consumer
= session
->ust_session
->consumer
;
2674 dir_name
= DEFAULT_UST_TRACE_DIR
;
2677 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2681 /* Append correct directory to subdir */
2682 strncat(consumer
->subdir
, dir_name
,
2683 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2684 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2693 * Create an UST session and add it to the session ust list.
2695 * Should *NOT* be called with RCU read-side lock held.
2697 static int create_ust_session(struct ltt_session
*session
,
2698 struct lttng_domain
*domain
)
2701 struct ltt_ust_session
*lus
= NULL
;
2705 assert(session
->consumer
);
2707 switch (domain
->type
) {
2708 case LTTNG_DOMAIN_JUL
:
2709 case LTTNG_DOMAIN_LOG4J
:
2710 case LTTNG_DOMAIN_PYTHON
:
2711 case LTTNG_DOMAIN_UST
:
2714 ERR("Unknown UST domain on create session %d", domain
->type
);
2715 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2719 DBG("Creating UST session");
2721 lus
= trace_ust_create_session(session
->id
);
2723 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2727 lus
->uid
= session
->uid
;
2728 lus
->gid
= session
->gid
;
2729 lus
->output_traces
= session
->output_traces
;
2730 lus
->snapshot_mode
= session
->snapshot_mode
;
2731 lus
->live_timer_interval
= session
->live_timer
;
2732 session
->ust_session
= lus
;
2734 /* Copy session output to the newly created UST session */
2735 ret
= copy_session_consumer(domain
->type
, session
);
2736 if (ret
!= LTTNG_OK
) {
2744 session
->ust_session
= NULL
;
2749 * Create a kernel tracer session then create the default channel.
2751 static int create_kernel_session(struct ltt_session
*session
)
2755 DBG("Creating kernel session");
2757 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2759 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2763 /* Code flow safety */
2764 assert(session
->kernel_session
);
2766 /* Copy session output to the newly created Kernel session */
2767 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2768 if (ret
!= LTTNG_OK
) {
2772 /* Create directory(ies) on local filesystem. */
2773 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2774 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2775 ret
= run_as_mkdir_recursive(
2776 session
->kernel_session
->consumer
->dst
.trace_path
,
2777 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2779 if (ret
!= -EEXIST
) {
2780 ERR("Trace directory creation error");
2786 session
->kernel_session
->uid
= session
->uid
;
2787 session
->kernel_session
->gid
= session
->gid
;
2788 session
->kernel_session
->output_traces
= session
->output_traces
;
2789 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2794 trace_kernel_destroy_session(session
->kernel_session
);
2795 session
->kernel_session
= NULL
;
2800 * Count number of session permitted by uid/gid.
2802 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2805 struct ltt_session
*session
;
2807 DBG("Counting number of available session for UID %d GID %d",
2809 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2811 * Only list the sessions the user can control.
2813 if (!session_access_ok(session
, uid
, gid
)) {
2822 * Process the command requested by the lttng client within the command
2823 * context structure. This function make sure that the return structure (llm)
2824 * is set and ready for transmission before returning.
2826 * Return any error encountered or 0 for success.
2828 * "sock" is only used for special-case var. len data.
2830 * Should *NOT* be called with RCU read-side lock held.
2832 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2836 int need_tracing_session
= 1;
2839 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2843 switch (cmd_ctx
->lsm
->cmd_type
) {
2844 case LTTNG_CREATE_SESSION
:
2845 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2846 case LTTNG_CREATE_SESSION_LIVE
:
2847 case LTTNG_DESTROY_SESSION
:
2848 case LTTNG_LIST_SESSIONS
:
2849 case LTTNG_LIST_DOMAINS
:
2850 case LTTNG_START_TRACE
:
2851 case LTTNG_STOP_TRACE
:
2852 case LTTNG_DATA_PENDING
:
2853 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2854 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2855 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2856 case LTTNG_SNAPSHOT_RECORD
:
2857 case LTTNG_SAVE_SESSION
:
2864 if (opt_no_kernel
&& need_domain
2865 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2867 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2869 ret
= LTTNG_ERR_KERN_NA
;
2874 /* Deny register consumer if we already have a spawned consumer. */
2875 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2876 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2877 if (kconsumer_data
.pid
> 0) {
2878 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2879 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2882 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2886 * Check for command that don't needs to allocate a returned payload. We do
2887 * this here so we don't have to make the call for no payload at each
2890 switch(cmd_ctx
->lsm
->cmd_type
) {
2891 case LTTNG_LIST_SESSIONS
:
2892 case LTTNG_LIST_TRACEPOINTS
:
2893 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2894 case LTTNG_LIST_DOMAINS
:
2895 case LTTNG_LIST_CHANNELS
:
2896 case LTTNG_LIST_EVENTS
:
2897 case LTTNG_LIST_SYSCALLS
:
2900 /* Setup lttng message with no payload */
2901 ret
= setup_lttng_msg(cmd_ctx
, 0);
2903 /* This label does not try to unlock the session */
2904 goto init_setup_error
;
2908 /* Commands that DO NOT need a session. */
2909 switch (cmd_ctx
->lsm
->cmd_type
) {
2910 case LTTNG_CREATE_SESSION
:
2911 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2912 case LTTNG_CREATE_SESSION_LIVE
:
2913 case LTTNG_CALIBRATE
:
2914 case LTTNG_LIST_SESSIONS
:
2915 case LTTNG_LIST_TRACEPOINTS
:
2916 case LTTNG_LIST_SYSCALLS
:
2917 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2918 case LTTNG_SAVE_SESSION
:
2919 need_tracing_session
= 0;
2922 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2924 * We keep the session list lock across _all_ commands
2925 * for now, because the per-session lock does not
2926 * handle teardown properly.
2928 session_lock_list();
2929 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2930 if (cmd_ctx
->session
== NULL
) {
2931 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2934 /* Acquire lock for the session */
2935 session_lock(cmd_ctx
->session
);
2941 * Commands that need a valid session but should NOT create one if none
2942 * exists. Instead of creating one and destroying it when the command is
2943 * handled, process that right before so we save some round trip in useless
2946 switch (cmd_ctx
->lsm
->cmd_type
) {
2947 case LTTNG_DISABLE_CHANNEL
:
2948 case LTTNG_DISABLE_EVENT
:
2949 switch (cmd_ctx
->lsm
->domain
.type
) {
2950 case LTTNG_DOMAIN_KERNEL
:
2951 if (!cmd_ctx
->session
->kernel_session
) {
2952 ret
= LTTNG_ERR_NO_CHANNEL
;
2956 case LTTNG_DOMAIN_JUL
:
2957 case LTTNG_DOMAIN_LOG4J
:
2958 case LTTNG_DOMAIN_PYTHON
:
2959 case LTTNG_DOMAIN_UST
:
2960 if (!cmd_ctx
->session
->ust_session
) {
2961 ret
= LTTNG_ERR_NO_CHANNEL
;
2966 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2978 * Check domain type for specific "pre-action".
2980 switch (cmd_ctx
->lsm
->domain
.type
) {
2981 case LTTNG_DOMAIN_KERNEL
:
2983 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2987 /* Kernel tracer check */
2988 if (kernel_tracer_fd
== -1) {
2989 /* Basically, load kernel tracer modules */
2990 ret
= init_kernel_tracer();
2996 /* Consumer is in an ERROR state. Report back to client */
2997 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
2998 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3002 /* Need a session for kernel command */
3003 if (need_tracing_session
) {
3004 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3005 ret
= create_kernel_session(cmd_ctx
->session
);
3007 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3012 /* Start the kernel consumer daemon */
3013 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3014 if (kconsumer_data
.pid
== 0 &&
3015 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3016 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3017 ret
= start_consumerd(&kconsumer_data
);
3019 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3022 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3024 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3028 * The consumer was just spawned so we need to add the socket to
3029 * the consumer output of the session if exist.
3031 ret
= consumer_create_socket(&kconsumer_data
,
3032 cmd_ctx
->session
->kernel_session
->consumer
);
3039 case LTTNG_DOMAIN_JUL
:
3040 case LTTNG_DOMAIN_LOG4J
:
3041 case LTTNG_DOMAIN_PYTHON
:
3042 case LTTNG_DOMAIN_UST
:
3044 if (!ust_app_supported()) {
3045 ret
= LTTNG_ERR_NO_UST
;
3048 /* Consumer is in an ERROR state. Report back to client */
3049 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3050 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3054 if (need_tracing_session
) {
3055 /* Create UST session if none exist. */
3056 if (cmd_ctx
->session
->ust_session
== NULL
) {
3057 ret
= create_ust_session(cmd_ctx
->session
,
3058 &cmd_ctx
->lsm
->domain
);
3059 if (ret
!= LTTNG_OK
) {
3064 /* Start the UST consumer daemons */
3066 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3067 if (consumerd64_bin
[0] != '\0' &&
3068 ustconsumer64_data
.pid
== 0 &&
3069 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3070 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3071 ret
= start_consumerd(&ustconsumer64_data
);
3073 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3074 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3078 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3079 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3081 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3085 * Setup socket for consumer 64 bit. No need for atomic access
3086 * since it was set above and can ONLY be set in this thread.
3088 ret
= consumer_create_socket(&ustconsumer64_data
,
3089 cmd_ctx
->session
->ust_session
->consumer
);
3095 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3096 if (consumerd32_bin
[0] != '\0' &&
3097 ustconsumer32_data
.pid
== 0 &&
3098 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3099 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3100 ret
= start_consumerd(&ustconsumer32_data
);
3102 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3103 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3107 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3108 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3110 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3114 * Setup socket for consumer 64 bit. No need for atomic access
3115 * since it was set above and can ONLY be set in this thread.
3117 ret
= consumer_create_socket(&ustconsumer32_data
,
3118 cmd_ctx
->session
->ust_session
->consumer
);
3130 /* Validate consumer daemon state when start/stop trace command */
3131 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3132 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3133 switch (cmd_ctx
->lsm
->domain
.type
) {
3134 case LTTNG_DOMAIN_JUL
:
3135 case LTTNG_DOMAIN_LOG4J
:
3136 case LTTNG_DOMAIN_PYTHON
:
3137 case LTTNG_DOMAIN_UST
:
3138 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3139 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3143 case LTTNG_DOMAIN_KERNEL
:
3144 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3145 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3153 * Check that the UID or GID match that of the tracing session.
3154 * The root user can interact with all sessions.
3156 if (need_tracing_session
) {
3157 if (!session_access_ok(cmd_ctx
->session
,
3158 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3159 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3160 ret
= LTTNG_ERR_EPERM
;
3166 * Send relayd information to consumer as soon as we have a domain and a
3169 if (cmd_ctx
->session
&& need_domain
) {
3171 * Setup relayd if not done yet. If the relayd information was already
3172 * sent to the consumer, this call will gracefully return.
3174 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3175 if (ret
!= LTTNG_OK
) {
3180 /* Process by command type */
3181 switch (cmd_ctx
->lsm
->cmd_type
) {
3182 case LTTNG_ADD_CONTEXT
:
3184 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3185 cmd_ctx
->lsm
->u
.context
.channel_name
,
3186 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
3189 case LTTNG_DISABLE_CHANNEL
:
3191 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3192 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3195 case LTTNG_DISABLE_EVENT
:
3197 /* FIXME: passing packed structure to non-packed pointer */
3198 /* TODO: handle filter */
3199 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3200 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3201 &cmd_ctx
->lsm
->u
.disable
.event
);
3204 case LTTNG_ENABLE_CHANNEL
:
3206 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3207 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3210 case LTTNG_ENABLE_EVENT
:
3212 struct lttng_event_exclusion
*exclusion
= NULL
;
3213 struct lttng_filter_bytecode
*bytecode
= NULL
;
3214 char *filter_expression
= NULL
;
3216 /* Handle exclusion events and receive it from the client. */
3217 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3218 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3220 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3221 (count
* LTTNG_SYMBOL_NAME_LEN
));
3223 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3227 DBG("Receiving var len exclusion event list from client ...");
3228 exclusion
->count
= count
;
3229 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3230 count
* LTTNG_SYMBOL_NAME_LEN
);
3232 DBG("Nothing recv() from client var len data... continuing");
3235 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3240 /* Get filter expression from client. */
3241 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3242 size_t expression_len
=
3243 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3245 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3246 ret
= LTTNG_ERR_FILTER_INVAL
;
3251 filter_expression
= zmalloc(expression_len
);
3252 if (!filter_expression
) {
3254 ret
= LTTNG_ERR_FILTER_NOMEM
;
3258 /* Receive var. len. data */
3259 DBG("Receiving var len filter's expression from client ...");
3260 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3263 DBG("Nothing recv() from client car len data... continuing");
3265 free(filter_expression
);
3267 ret
= LTTNG_ERR_FILTER_INVAL
;
3272 /* Handle filter and get bytecode from client. */
3273 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3274 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3276 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3277 ret
= LTTNG_ERR_FILTER_INVAL
;
3278 free(filter_expression
);
3283 bytecode
= zmalloc(bytecode_len
);
3285 free(filter_expression
);
3287 ret
= LTTNG_ERR_FILTER_NOMEM
;
3291 /* Receive var. len. data */
3292 DBG("Receiving var len filter's bytecode from client ...");
3293 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3295 DBG("Nothing recv() from client car len data... continuing");
3297 free(filter_expression
);
3300 ret
= LTTNG_ERR_FILTER_INVAL
;
3304 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3305 free(filter_expression
);
3308 ret
= LTTNG_ERR_FILTER_INVAL
;
3313 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3314 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3315 &cmd_ctx
->lsm
->u
.enable
.event
,
3316 filter_expression
, bytecode
, exclusion
,
3317 kernel_poll_pipe
[1]);
3320 case LTTNG_LIST_TRACEPOINTS
:
3322 struct lttng_event
*events
;
3325 session_lock_list();
3326 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3327 session_unlock_list();
3328 if (nb_events
< 0) {
3329 /* Return value is a negative lttng_error_code. */
3335 * Setup lttng message with payload size set to the event list size in
3336 * bytes and then copy list into the llm payload.
3338 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3344 /* Copy event list into message payload */
3345 memcpy(cmd_ctx
->llm
->payload
, events
,
3346 sizeof(struct lttng_event
) * nb_events
);
3353 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3355 struct lttng_event_field
*fields
;
3358 session_lock_list();
3359 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3361 session_unlock_list();
3362 if (nb_fields
< 0) {
3363 /* Return value is a negative lttng_error_code. */
3369 * Setup lttng message with payload size set to the event list size in
3370 * bytes and then copy list into the llm payload.
3372 ret
= setup_lttng_msg(cmd_ctx
,
3373 sizeof(struct lttng_event_field
) * nb_fields
);
3379 /* Copy event list into message payload */
3380 memcpy(cmd_ctx
->llm
->payload
, fields
,
3381 sizeof(struct lttng_event_field
) * nb_fields
);
3388 case LTTNG_LIST_SYSCALLS
:
3390 struct lttng_event
*events
;
3393 nb_events
= cmd_list_syscalls(&events
);
3394 if (nb_events
< 0) {
3395 /* Return value is a negative lttng_error_code. */
3401 * Setup lttng message with payload size set to the event list size in
3402 * bytes and then copy list into the llm payload.
3404 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3410 /* Copy event list into message payload */
3411 memcpy(cmd_ctx
->llm
->payload
, events
,
3412 sizeof(struct lttng_event
) * nb_events
);
3419 case LTTNG_SET_CONSUMER_URI
:
3422 struct lttng_uri
*uris
;
3424 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3425 len
= nb_uri
* sizeof(struct lttng_uri
);
3428 ret
= LTTNG_ERR_INVALID
;
3432 uris
= zmalloc(len
);
3434 ret
= LTTNG_ERR_FATAL
;
3438 /* Receive variable len data */
3439 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3440 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3442 DBG("No URIs received from client... continuing");
3444 ret
= LTTNG_ERR_SESSION_FAIL
;
3449 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3451 if (ret
!= LTTNG_OK
) {
3458 case LTTNG_START_TRACE
:
3460 ret
= cmd_start_trace(cmd_ctx
->session
);
3463 case LTTNG_STOP_TRACE
:
3465 ret
= cmd_stop_trace(cmd_ctx
->session
);
3468 case LTTNG_CREATE_SESSION
:
3471 struct lttng_uri
*uris
= NULL
;
3473 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3474 len
= nb_uri
* sizeof(struct lttng_uri
);
3477 uris
= zmalloc(len
);
3479 ret
= LTTNG_ERR_FATAL
;
3483 /* Receive variable len data */
3484 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3485 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3487 DBG("No URIs received from client... continuing");
3489 ret
= LTTNG_ERR_SESSION_FAIL
;
3494 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3495 DBG("Creating session with ONE network URI is a bad call");
3496 ret
= LTTNG_ERR_SESSION_FAIL
;
3502 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3503 &cmd_ctx
->creds
, 0);
3509 case LTTNG_DESTROY_SESSION
:
3511 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3513 /* Set session to NULL so we do not unlock it after free. */
3514 cmd_ctx
->session
= NULL
;
3517 case LTTNG_LIST_DOMAINS
:
3520 struct lttng_domain
*domains
= NULL
;
3522 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3524 /* Return value is a negative lttng_error_code. */
3529 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3535 /* Copy event list into message payload */
3536 memcpy(cmd_ctx
->llm
->payload
, domains
,
3537 nb_dom
* sizeof(struct lttng_domain
));
3544 case LTTNG_LIST_CHANNELS
:
3547 struct lttng_channel
*channels
= NULL
;
3549 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3550 cmd_ctx
->session
, &channels
);
3552 /* Return value is a negative lttng_error_code. */
3557 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3563 /* Copy event list into message payload */
3564 memcpy(cmd_ctx
->llm
->payload
, channels
,
3565 nb_chan
* sizeof(struct lttng_channel
));
3572 case LTTNG_LIST_EVENTS
:
3575 struct lttng_event
*events
= NULL
;
3577 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3578 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3580 /* Return value is a negative lttng_error_code. */
3585 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3591 /* Copy event list into message payload */
3592 memcpy(cmd_ctx
->llm
->payload
, events
,
3593 nb_event
* sizeof(struct lttng_event
));
3600 case LTTNG_LIST_SESSIONS
:
3602 unsigned int nr_sessions
;
3604 session_lock_list();
3605 nr_sessions
= lttng_sessions_count(
3606 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3607 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3609 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3611 session_unlock_list();
3615 /* Filled the session array */
3616 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3617 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3618 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3620 session_unlock_list();
3625 case LTTNG_CALIBRATE
:
3627 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3628 &cmd_ctx
->lsm
->u
.calibrate
);
3631 case LTTNG_REGISTER_CONSUMER
:
3633 struct consumer_data
*cdata
;
3635 switch (cmd_ctx
->lsm
->domain
.type
) {
3636 case LTTNG_DOMAIN_KERNEL
:
3637 cdata
= &kconsumer_data
;
3640 ret
= LTTNG_ERR_UND
;
3644 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3645 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3648 case LTTNG_DATA_PENDING
:
3650 ret
= cmd_data_pending(cmd_ctx
->session
);
3653 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3655 struct lttcomm_lttng_output_id reply
;
3657 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3658 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3659 if (ret
!= LTTNG_OK
) {
3663 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3668 /* Copy output list into message payload */
3669 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3673 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3675 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3676 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3679 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3682 struct lttng_snapshot_output
*outputs
= NULL
;
3684 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3685 if (nb_output
< 0) {
3690 ret
= setup_lttng_msg(cmd_ctx
,
3691 nb_output
* sizeof(struct lttng_snapshot_output
));
3698 /* Copy output list into message payload */
3699 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3700 nb_output
* sizeof(struct lttng_snapshot_output
));
3707 case LTTNG_SNAPSHOT_RECORD
:
3709 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3710 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3711 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3714 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3717 struct lttng_uri
*uris
= NULL
;
3719 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3720 len
= nb_uri
* sizeof(struct lttng_uri
);
3723 uris
= zmalloc(len
);
3725 ret
= LTTNG_ERR_FATAL
;
3729 /* Receive variable len data */
3730 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3731 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3733 DBG("No URIs received from client... continuing");
3735 ret
= LTTNG_ERR_SESSION_FAIL
;
3740 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3741 DBG("Creating session with ONE network URI is a bad call");
3742 ret
= LTTNG_ERR_SESSION_FAIL
;
3748 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3749 nb_uri
, &cmd_ctx
->creds
);
3753 case LTTNG_CREATE_SESSION_LIVE
:
3756 struct lttng_uri
*uris
= NULL
;
3758 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3759 len
= nb_uri
* sizeof(struct lttng_uri
);
3762 uris
= zmalloc(len
);
3764 ret
= LTTNG_ERR_FATAL
;
3768 /* Receive variable len data */
3769 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3770 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3772 DBG("No URIs received from client... continuing");
3774 ret
= LTTNG_ERR_SESSION_FAIL
;
3779 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3780 DBG("Creating session with ONE network URI is a bad call");
3781 ret
= LTTNG_ERR_SESSION_FAIL
;
3787 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3788 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3792 case LTTNG_SAVE_SESSION
:
3794 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
3799 ret
= LTTNG_ERR_UND
;
3804 if (cmd_ctx
->llm
== NULL
) {
3805 DBG("Missing llm structure. Allocating one.");
3806 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3810 /* Set return code */
3811 cmd_ctx
->llm
->ret_code
= ret
;
3813 if (cmd_ctx
->session
) {
3814 session_unlock(cmd_ctx
->session
);
3816 if (need_tracing_session
) {
3817 session_unlock_list();
3824 * Thread managing health check socket.
3826 static void *thread_manage_health(void *data
)
3828 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3829 uint32_t revents
, nb_fd
;
3830 struct lttng_poll_event events
;
3831 struct health_comm_msg msg
;
3832 struct health_comm_reply reply
;
3834 DBG("[thread] Manage health check started");
3836 rcu_register_thread();
3838 /* We might hit an error path before this is created. */
3839 lttng_poll_init(&events
);
3841 /* Create unix socket */
3842 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3844 ERR("Unable to create health check Unix socket");
3850 /* lttng health client socket path permissions */
3851 ret
= chown(health_unix_sock_path
, 0,
3852 utils_get_group_id(tracing_group_name
));
3854 ERR("Unable to set group on %s", health_unix_sock_path
);
3860 ret
= chmod(health_unix_sock_path
,
3861 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3863 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3871 * Set the CLOEXEC flag. Return code is useless because either way, the
3874 (void) utils_set_fd_cloexec(sock
);
3876 ret
= lttcomm_listen_unix_sock(sock
);
3882 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3883 * more will be added to this poll set.
3885 ret
= sessiond_set_thread_pollset(&events
, 2);
3890 /* Add the application registration socket */
3891 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3896 sessiond_notify_ready();
3899 DBG("Health check ready");
3901 /* Inifinite blocking call, waiting for transmission */
3903 ret
= lttng_poll_wait(&events
, -1);
3906 * Restart interrupted system call.
3908 if (errno
== EINTR
) {
3916 for (i
= 0; i
< nb_fd
; i
++) {
3917 /* Fetch once the poll data */
3918 revents
= LTTNG_POLL_GETEV(&events
, i
);
3919 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3921 /* Thread quit pipe has been closed. Killing thread. */
3922 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3928 /* Event on the registration socket */
3929 if (pollfd
== sock
) {
3930 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3931 ERR("Health socket poll error");
3937 new_sock
= lttcomm_accept_unix_sock(sock
);
3943 * Set the CLOEXEC flag. Return code is useless because either way, the
3946 (void) utils_set_fd_cloexec(new_sock
);
3948 DBG("Receiving data from client for health...");
3949 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3951 DBG("Nothing recv() from client... continuing");
3952 ret
= close(new_sock
);
3960 rcu_thread_online();
3962 memset(&reply
, 0, sizeof(reply
));
3963 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
3965 * health_check_state returns 0 if health is
3968 if (!health_check_state(health_sessiond
, i
)) {
3969 reply
.ret_code
|= 1ULL << i
;
3973 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
3975 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
3977 ERR("Failed to send health data back to client");
3980 /* End of transmission */
3981 ret
= close(new_sock
);
3991 ERR("Health error occurred in %s", __func__
);
3993 DBG("Health check thread dying");
3994 unlink(health_unix_sock_path
);
4002 lttng_poll_clean(&events
);
4004 rcu_unregister_thread();
4009 * This thread manage all clients request using the unix client socket for
4012 static void *thread_manage_clients(void *data
)
4014 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4016 uint32_t revents
, nb_fd
;
4017 struct command_ctx
*cmd_ctx
= NULL
;
4018 struct lttng_poll_event events
;
4020 DBG("[thread] Manage client started");
4022 rcu_register_thread();
4024 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4026 health_code_update();
4028 ret
= lttcomm_listen_unix_sock(client_sock
);
4034 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4035 * more will be added to this poll set.
4037 ret
= sessiond_set_thread_pollset(&events
, 2);
4039 goto error_create_poll
;
4042 /* Add the application registration socket */
4043 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4048 sessiond_notify_ready();
4049 ret
= sem_post(&load_info
->message_thread_ready
);
4051 PERROR("sem_post message_thread_ready");
4055 /* This testpoint is after we signal readiness to the parent. */
4056 if (testpoint(sessiond_thread_manage_clients
)) {
4060 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4064 health_code_update();
4067 DBG("Accepting client command ...");
4069 /* Inifinite blocking call, waiting for transmission */
4071 health_poll_entry();
4072 ret
= lttng_poll_wait(&events
, -1);
4076 * Restart interrupted system call.
4078 if (errno
== EINTR
) {
4086 for (i
= 0; i
< nb_fd
; i
++) {
4087 /* Fetch once the poll data */
4088 revents
= LTTNG_POLL_GETEV(&events
, i
);
4089 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4091 health_code_update();
4093 /* Thread quit pipe has been closed. Killing thread. */
4094 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4100 /* Event on the registration socket */
4101 if (pollfd
== client_sock
) {
4102 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4103 ERR("Client socket poll error");
4109 DBG("Wait for client response");
4111 health_code_update();
4113 sock
= lttcomm_accept_unix_sock(client_sock
);
4119 * Set the CLOEXEC flag. Return code is useless because either way, the
4122 (void) utils_set_fd_cloexec(sock
);
4124 /* Set socket option for credentials retrieval */
4125 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4130 /* Allocate context command to process the client request */
4131 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4132 if (cmd_ctx
== NULL
) {
4133 PERROR("zmalloc cmd_ctx");
4137 /* Allocate data buffer for reception */
4138 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4139 if (cmd_ctx
->lsm
== NULL
) {
4140 PERROR("zmalloc cmd_ctx->lsm");
4144 cmd_ctx
->llm
= NULL
;
4145 cmd_ctx
->session
= NULL
;
4147 health_code_update();
4150 * Data is received from the lttng client. The struct
4151 * lttcomm_session_msg (lsm) contains the command and data request of
4154 DBG("Receiving data from client ...");
4155 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4156 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4158 DBG("Nothing recv() from client... continuing");
4164 clean_command_ctx(&cmd_ctx
);
4168 health_code_update();
4170 // TODO: Validate cmd_ctx including sanity check for
4171 // security purpose.
4173 rcu_thread_online();
4175 * This function dispatch the work to the kernel or userspace tracer
4176 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4177 * informations for the client. The command context struct contains
4178 * everything this function may needs.
4180 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4181 rcu_thread_offline();
4189 * TODO: Inform client somehow of the fatal error. At
4190 * this point, ret < 0 means that a zmalloc failed
4191 * (ENOMEM). Error detected but still accept
4192 * command, unless a socket error has been
4195 clean_command_ctx(&cmd_ctx
);
4199 health_code_update();
4201 DBG("Sending response (size: %d, retcode: %s)",
4202 cmd_ctx
->lttng_msg_size
,
4203 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
4204 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4206 ERR("Failed to send data back to client");
4209 /* End of transmission */
4216 clean_command_ctx(&cmd_ctx
);
4218 health_code_update();
4230 lttng_poll_clean(&events
);
4231 clean_command_ctx(&cmd_ctx
);
4235 unlink(client_unix_sock_path
);
4236 if (client_sock
>= 0) {
4237 ret
= close(client_sock
);
4245 ERR("Health error occurred in %s", __func__
);
4248 health_unregister(health_sessiond
);
4250 DBG("Client thread dying");
4252 rcu_unregister_thread();
4255 * Since we are creating the consumer threads, we own them, so we need
4256 * to join them before our thread exits.
4258 ret
= join_consumer_thread(&kconsumer_data
);
4261 PERROR("join_consumer");
4264 ret
= join_consumer_thread(&ustconsumer32_data
);
4267 PERROR("join_consumer ust32");
4270 ret
= join_consumer_thread(&ustconsumer64_data
);
4273 PERROR("join_consumer ust64");
4280 * usage function on stderr
4282 static void usage(void)
4284 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4285 fprintf(stderr
, " -h, --help Display this usage.\n");
4286 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4287 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4288 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4289 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4290 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4291 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4292 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4293 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4294 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4295 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4296 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4297 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4298 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4299 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
4300 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4301 fprintf(stderr
, " -V, --version Show version number.\n");
4302 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4303 fprintf(stderr
, " -q, --quiet No output at all.\n");
4304 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4305 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4306 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4307 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4308 fprintf(stderr
, " --agent-tcp-port Agent registration TCP port\n");
4309 fprintf(stderr
, " -f --config PATH Load daemon configuration file\n");
4310 fprintf(stderr
, " -l --load PATH Load session configuration\n");
4311 fprintf(stderr
, " --kmod-probes Specify kernel module probes to load\n");
4312 fprintf(stderr
, " --extra-kmod-probes Specify extra kernel module probes to load\n");
4316 * Take an option from the getopt output and set it in the right variable to be
4319 * Return 0 on success else a negative value.
4321 static int set_option(int opt
, const char *arg
, const char *optname
)
4325 if (arg
&& arg
[0] == '\0') {
4327 * This only happens if the value is read from daemon config
4328 * file. This means the option requires an argument and the
4329 * configuration file contains a line such as:
4338 fprintf(stderr
, "option %s", optname
);
4340 fprintf(stderr
, " with arg %s\n", arg
);
4344 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4347 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4357 * If the override option is set, the pointer points to a
4358 * *non* const thus freeing it even though the variable type is
4361 if (tracing_group_name_override
) {
4362 free((void *) tracing_group_name
);
4364 tracing_group_name
= strdup(arg
);
4365 if (!tracing_group_name
) {
4369 tracing_group_name_override
= 1;
4375 fprintf(stdout
, "%s\n", VERSION
);
4381 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4384 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4387 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4390 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4393 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4396 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4402 lttng_opt_quiet
= 1;
4405 /* Verbose level can increase using multiple -v */
4407 /* Value obtained from config file */
4408 lttng_opt_verbose
= config_parse_value(arg
);
4410 /* -v used on command line */
4411 lttng_opt_verbose
++;
4413 /* Clamp value to [0, 3] */
4414 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4415 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4419 opt_verbose_consumer
= config_parse_value(arg
);
4421 opt_verbose_consumer
+= 1;
4425 if (consumerd32_bin_override
) {
4426 free((void *) consumerd32_bin
);
4428 consumerd32_bin
= strdup(arg
);
4429 if (!consumerd32_bin
) {
4433 consumerd32_bin_override
= 1;
4436 if (consumerd32_libdir_override
) {
4437 free((void *) consumerd32_libdir
);
4439 consumerd32_libdir
= strdup(arg
);
4440 if (!consumerd32_libdir
) {
4444 consumerd32_libdir_override
= 1;
4447 if (consumerd64_bin_override
) {
4448 free((void *) consumerd64_bin
);
4450 consumerd64_bin
= strdup(arg
);
4451 if (!consumerd64_bin
) {
4455 consumerd64_bin_override
= 1;
4458 if (consumerd64_libdir_override
) {
4459 free((void *) consumerd64_libdir
);
4461 consumerd64_libdir
= strdup(arg
);
4462 if (!consumerd64_libdir
) {
4466 consumerd64_libdir_override
= 1;
4470 opt_pidfile
= strdup(arg
);
4476 case 'J': /* Agent TCP port. */
4481 v
= strtoul(arg
, NULL
, 0);
4482 if (errno
!= 0 || !isdigit(arg
[0])) {
4483 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4486 if (v
== 0 || v
>= 65535) {
4487 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4490 agent_tcp_port
= (uint32_t) v
;
4491 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
4495 free(opt_load_session_path
);
4496 opt_load_session_path
= strdup(arg
);
4497 if (!opt_load_session_path
) {
4502 case 'P': /* probe modules list */
4503 free(kmod_probes_list
);
4504 kmod_probes_list
= strdup(arg
);
4505 if (!kmod_probes_list
) {
4511 free(kmod_extra_probes_list
);
4512 kmod_extra_probes_list
= strdup(arg
);
4513 if (!kmod_extra_probes_list
) {
4519 /* This is handled in set_options() thus silent break. */
4522 /* Unknown option or other error.
4523 * Error is printed by getopt, just return */
4528 if (ret
== -EINVAL
) {
4529 const char *opt_name
= "unknown";
4532 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4534 if (opt
== long_options
[i
].val
) {
4535 opt_name
= long_options
[i
].name
;
4540 WARN("Invalid argument provided for option \"%s\", using default value.",
4548 * config_entry_handler_cb used to handle options read from a config file.
4549 * See config_entry_handler_cb comment in common/config/config.h for the
4550 * return value conventions.
4552 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4556 if (!entry
|| !entry
->name
|| !entry
->value
) {
4561 /* Check if the option is to be ignored */
4562 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4563 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4568 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4571 /* Ignore if not fully matched. */
4572 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4577 * If the option takes no argument on the command line, we have to
4578 * check if the value is "true". We support non-zero numeric values,
4581 if (!long_options
[i
].has_arg
) {
4582 ret
= config_parse_value(entry
->value
);
4585 WARN("Invalid configuration value \"%s\" for option %s",
4586 entry
->value
, entry
->name
);
4588 /* False, skip boolean config option. */
4593 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4597 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4604 * daemon configuration loading and argument parsing
4606 static int set_options(int argc
, char **argv
)
4608 int ret
= 0, c
= 0, option_index
= 0;
4609 int orig_optopt
= optopt
, orig_optind
= optind
;
4611 const char *config_path
= NULL
;
4613 optstring
= utils_generate_optstring(long_options
,
4614 sizeof(long_options
) / sizeof(struct option
));
4620 /* Check for the --config option */
4621 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4622 &option_index
)) != -1) {
4626 } else if (c
!= 'f') {
4627 /* if not equal to --config option. */
4631 config_path
= utils_expand_path(optarg
);
4633 ERR("Failed to resolve path: %s", optarg
);
4637 ret
= config_get_section_entries(config_path
, config_section_name
,
4638 config_entry_handler
, NULL
);
4641 ERR("Invalid configuration option at line %i", ret
);
4647 /* Reset getopt's global state */
4648 optopt
= orig_optopt
;
4649 optind
= orig_optind
;
4651 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
4656 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
4668 * Creates the two needed socket by the daemon.
4669 * apps_sock - The communication socket for all UST apps.
4670 * client_sock - The communication of the cli tool (lttng).
4672 static int init_daemon_socket(void)
4677 old_umask
= umask(0);
4679 /* Create client tool unix socket */
4680 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4681 if (client_sock
< 0) {
4682 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4687 /* Set the cloexec flag */
4688 ret
= utils_set_fd_cloexec(client_sock
);
4690 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4691 "Continuing but note that the consumer daemon will have a "
4692 "reference to this socket on exec()", client_sock
);
4695 /* File permission MUST be 660 */
4696 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4698 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4703 /* Create the application unix socket */
4704 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4705 if (apps_sock
< 0) {
4706 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4711 /* Set the cloexec flag */
4712 ret
= utils_set_fd_cloexec(apps_sock
);
4714 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4715 "Continuing but note that the consumer daemon will have a "
4716 "reference to this socket on exec()", apps_sock
);
4719 /* File permission MUST be 666 */
4720 ret
= chmod(apps_unix_sock_path
,
4721 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4723 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4728 DBG3("Session daemon client socket %d and application socket %d created",
4729 client_sock
, apps_sock
);
4737 * Check if the global socket is available, and if a daemon is answering at the
4738 * other side. If yes, error is returned.
4740 static int check_existing_daemon(void)
4742 /* Is there anybody out there ? */
4743 if (lttng_session_daemon_alive()) {
4751 * Set the tracing group gid onto the client socket.
4753 * Race window between mkdir and chown is OK because we are going from more
4754 * permissive (root.root) to less permissive (root.tracing).
4756 static int set_permissions(char *rundir
)
4761 gid
= utils_get_group_id(tracing_group_name
);
4763 /* Set lttng run dir */
4764 ret
= chown(rundir
, 0, gid
);
4766 ERR("Unable to set group on %s", rundir
);
4771 * Ensure all applications and tracing group can search the run
4772 * dir. Allow everyone to read the directory, since it does not
4773 * buy us anything to hide its content.
4775 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4777 ERR("Unable to set permissions on %s", rundir
);
4781 /* lttng client socket path */
4782 ret
= chown(client_unix_sock_path
, 0, gid
);
4784 ERR("Unable to set group on %s", client_unix_sock_path
);
4788 /* kconsumer error socket path */
4789 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4791 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4795 /* 64-bit ustconsumer error socket path */
4796 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4798 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4802 /* 32-bit ustconsumer compat32 error socket path */
4803 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4805 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4809 DBG("All permissions are set");
4815 * Create the lttng run directory needed for all global sockets and pipe.
4817 static int create_lttng_rundir(const char *rundir
)
4821 DBG3("Creating LTTng run directory: %s", rundir
);
4823 ret
= mkdir(rundir
, S_IRWXU
);
4825 if (errno
!= EEXIST
) {
4826 ERR("Unable to create %s", rundir
);
4838 * Setup sockets and directory needed by the kconsumerd communication with the
4841 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4845 char path
[PATH_MAX
];
4847 switch (consumer_data
->type
) {
4848 case LTTNG_CONSUMER_KERNEL
:
4849 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4851 case LTTNG_CONSUMER64_UST
:
4852 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4854 case LTTNG_CONSUMER32_UST
:
4855 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4858 ERR("Consumer type unknown");
4863 DBG2("Creating consumer directory: %s", path
);
4865 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4867 if (errno
!= EEXIST
) {
4869 ERR("Failed to create %s", path
);
4875 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4877 ERR("Unable to set group on %s", path
);
4883 /* Create the kconsumerd error unix socket */
4884 consumer_data
->err_sock
=
4885 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4886 if (consumer_data
->err_sock
< 0) {
4887 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4893 * Set the CLOEXEC flag. Return code is useless because either way, the
4896 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4898 PERROR("utils_set_fd_cloexec");
4899 /* continue anyway */
4902 /* File permission MUST be 660 */
4903 ret
= chmod(consumer_data
->err_unix_sock_path
,
4904 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4906 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4916 * Signal handler for the daemon
4918 * Simply stop all worker threads, leaving main() return gracefully after
4919 * joining all threads and calling cleanup().
4921 static void sighandler(int sig
)
4925 DBG("SIGPIPE caught");
4928 DBG("SIGINT caught");
4932 DBG("SIGTERM caught");
4936 CMM_STORE_SHARED(recv_child_signal
, 1);
4944 * Setup signal handler for :
4945 * SIGINT, SIGTERM, SIGPIPE
4947 static int set_signal_handler(void)
4950 struct sigaction sa
;
4953 if ((ret
= sigemptyset(&sigset
)) < 0) {
4954 PERROR("sigemptyset");
4958 sa
.sa_handler
= sighandler
;
4959 sa
.sa_mask
= sigset
;
4961 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
4962 PERROR("sigaction");
4966 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
4967 PERROR("sigaction");
4971 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
4972 PERROR("sigaction");
4976 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
4977 PERROR("sigaction");
4981 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
4987 * Set open files limit to unlimited. This daemon can open a large number of
4988 * file descriptors in order to consumer multiple kernel traces.
4990 static void set_ulimit(void)
4995 /* The kernel does not allowed an infinite limit for open files */
4996 lim
.rlim_cur
= 65535;
4997 lim
.rlim_max
= 65535;
4999 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5001 PERROR("failed to set open files limit");
5006 * Write pidfile using the rundir and opt_pidfile.
5008 static int write_pidfile(void)
5011 char pidfile_path
[PATH_MAX
];
5016 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
5018 /* Build pidfile path from rundir and opt_pidfile. */
5019 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
5020 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
5022 PERROR("snprintf pidfile path");
5028 * Create pid file in rundir.
5030 ret
= utils_create_pid_file(getpid(), pidfile_path
);
5036 * Create lockfile using the rundir and return its fd.
5038 static int create_lockfile(void)
5041 char lockfile_path
[PATH_MAX
];
5043 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
5048 ret
= utils_create_lock_file(lockfile_path
);
5054 * Write agent TCP port using the rundir.
5056 static int write_agent_port(void)
5059 char path
[PATH_MAX
];
5063 ret
= snprintf(path
, sizeof(path
), "%s/"
5064 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
5066 PERROR("snprintf agent port path");
5071 * Create TCP agent port file in rundir.
5073 ret
= utils_create_pid_file(agent_tcp_port
, path
);
5082 int main(int argc
, char **argv
)
5084 int ret
= 0, retval
= 0;
5086 const char *home_path
, *env_app_timeout
;
5088 init_kernel_workarounds();
5090 rcu_register_thread();
5092 if (set_signal_handler()) {
5094 goto exit_set_signal_handler
;
5097 setup_consumerd_path();
5099 page_size
= sysconf(_SC_PAGESIZE
);
5100 if (page_size
< 0) {
5101 PERROR("sysconf _SC_PAGESIZE");
5102 page_size
= LONG_MAX
;
5103 WARN("Fallback page size to %ld", page_size
);
5107 * Parse arguments and load the daemon configuration file.
5109 * We have an exit_options exit path to free memory reserved by
5110 * set_options. This is needed because the rest of sessiond_cleanup()
5111 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5112 * depends on set_options.
5115 if (set_options(argc
, argv
)) {
5121 if (opt_daemon
|| opt_background
) {
5124 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5132 * We are in the child. Make sure all other file descriptors are
5133 * closed, in case we are called with more opened file
5134 * descriptors than the standard ones.
5136 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5142 * Starting from here, we can create threads. This needs to be after
5143 * lttng_daemonize due to RCU.
5147 * Initialize the health check subsystem. This call should set the
5148 * appropriate time values.
5150 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5151 if (!health_sessiond
) {
5152 PERROR("health_app_create error");
5154 goto exit_health_sessiond_cleanup
;
5157 if (init_ht_cleanup_quit_pipe()) {
5159 goto exit_ht_cleanup_quit_pipe
;
5162 /* Setup the thread ht_cleanup communication pipe. */
5163 if (utils_create_pipe_cloexec(ht_cleanup_pipe
)) {
5165 goto exit_ht_cleanup_pipe
;
5168 /* Create thread to clean up RCU hash tables */
5169 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5170 thread_ht_cleanup
, (void *) NULL
);
5173 PERROR("pthread_create ht_cleanup");
5175 goto exit_ht_cleanup
;
5178 /* Create thread quit pipe */
5179 if (init_thread_quit_pipe()) {
5181 goto exit_init_data
;
5184 /* Check if daemon is UID = 0 */
5185 is_root
= !getuid();
5188 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5191 goto exit_init_data
;
5194 /* Create global run dir with root access */
5195 if (create_lttng_rundir(rundir
)) {
5197 goto exit_init_data
;
5200 if (strlen(apps_unix_sock_path
) == 0) {
5201 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5202 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5205 goto exit_init_data
;
5209 if (strlen(client_unix_sock_path
) == 0) {
5210 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5211 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5214 goto exit_init_data
;
5218 /* Set global SHM for ust */
5219 if (strlen(wait_shm_path
) == 0) {
5220 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5221 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5224 goto exit_init_data
;
5228 if (strlen(health_unix_sock_path
) == 0) {
5229 ret
= snprintf(health_unix_sock_path
,
5230 sizeof(health_unix_sock_path
),
5231 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5234 goto exit_init_data
;
5238 /* Setup kernel consumerd path */
5239 ret
= snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5240 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5243 goto exit_init_data
;
5245 ret
= snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5246 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5249 goto exit_init_data
;
5252 DBG2("Kernel consumer err path: %s",
5253 kconsumer_data
.err_unix_sock_path
);
5254 DBG2("Kernel consumer cmd path: %s",
5255 kconsumer_data
.cmd_unix_sock_path
);
5257 home_path
= utils_get_home_dir();
5258 if (home_path
== NULL
) {
5259 /* TODO: Add --socket PATH option */
5260 ERR("Can't get HOME directory for sockets creation.");
5262 goto exit_init_data
;
5266 * Create rundir from home path. This will create something like
5269 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5272 goto exit_init_data
;
5275 if (create_lttng_rundir(rundir
)) {
5277 goto exit_init_data
;
5280 if (strlen(apps_unix_sock_path
) == 0) {
5281 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5282 DEFAULT_HOME_APPS_UNIX_SOCK
,
5286 goto exit_init_data
;
5290 /* Set the cli tool unix socket path */
5291 if (strlen(client_unix_sock_path
) == 0) {
5292 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5293 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
5297 goto exit_init_data
;
5301 /* Set global SHM for ust */
5302 if (strlen(wait_shm_path
) == 0) {
5303 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5304 DEFAULT_HOME_APPS_WAIT_SHM_PATH
,
5308 goto exit_init_data
;
5312 /* Set health check Unix path */
5313 if (strlen(health_unix_sock_path
) == 0) {
5314 ret
= snprintf(health_unix_sock_path
,
5315 sizeof(health_unix_sock_path
),
5316 DEFAULT_HOME_HEALTH_UNIX_SOCK
,
5320 goto exit_init_data
;
5325 lockfile_fd
= create_lockfile();
5326 if (lockfile_fd
< 0) {
5328 goto exit_init_data
;
5331 /* Set consumer initial state */
5332 kernel_consumerd_state
= CONSUMER_STOPPED
;
5333 ust_consumerd_state
= CONSUMER_STOPPED
;
5335 DBG("Client socket path %s", client_unix_sock_path
);
5336 DBG("Application socket path %s", apps_unix_sock_path
);
5337 DBG("Application wait path %s", wait_shm_path
);
5338 DBG("LTTng run directory path: %s", rundir
);
5340 /* 32 bits consumerd path setup */
5341 ret
= snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5342 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5344 PERROR("snprintf 32-bit consumer error socket path");
5346 goto exit_init_data
;
5348 ret
= snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5349 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5351 PERROR("snprintf 32-bit consumer command socket path");
5353 goto exit_init_data
;
5356 DBG2("UST consumer 32 bits err path: %s",
5357 ustconsumer32_data
.err_unix_sock_path
);
5358 DBG2("UST consumer 32 bits cmd path: %s",
5359 ustconsumer32_data
.cmd_unix_sock_path
);
5361 /* 64 bits consumerd path setup */
5362 ret
= snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5363 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5365 PERROR("snprintf 64-bit consumer error socket path");
5367 goto exit_init_data
;
5369 ret
= snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5370 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5372 PERROR("snprintf 64-bit consumer command socket path");
5374 goto exit_init_data
;
5377 DBG2("UST consumer 64 bits err path: %s",
5378 ustconsumer64_data
.err_unix_sock_path
);
5379 DBG2("UST consumer 64 bits cmd path: %s",
5380 ustconsumer64_data
.cmd_unix_sock_path
);
5383 * See if daemon already exist.
5385 if (check_existing_daemon()) {
5386 ERR("Already running daemon.\n");
5388 * We do not goto exit because we must not cleanup()
5389 * because a daemon is already running.
5392 goto exit_init_data
;
5396 * Init UST app hash table. Alloc hash table before this point since
5397 * cleanup() can get called after that point.
5399 if (ust_app_ht_alloc()) {
5401 goto exit_init_data
;
5404 /* Initialize agent domain subsystem. */
5405 if (agent_setup()) {
5406 /* ENOMEM at this point. */
5408 goto exit_init_data
;
5412 * These actions must be executed as root. We do that *after* setting up
5413 * the sockets path because we MUST make the check for another daemon using
5414 * those paths *before* trying to set the kernel consumer sockets and init
5418 if (set_consumer_sockets(&kconsumer_data
, rundir
)) {
5420 goto exit_init_data
;
5423 /* Setup kernel tracer */
5424 if (!opt_no_kernel
) {
5425 init_kernel_tracer();
5426 if (kernel_tracer_fd
>= 0) {
5427 ret
= syscall_init_table();
5429 ERR("Unable to populate syscall table. "
5430 "Syscall tracing won't work "
5431 "for this session daemon.");
5436 /* Set ulimit for open files */
5439 /* init lttng_fd tracking must be done after set_ulimit. */
5442 if (set_consumer_sockets(&ustconsumer64_data
, rundir
)) {
5444 goto exit_init_data
;
5447 if (set_consumer_sockets(&ustconsumer32_data
, rundir
)) {
5449 goto exit_init_data
;
5452 /* Setup the needed unix socket */
5453 if (init_daemon_socket()) {
5455 goto exit_init_data
;
5458 /* Set credentials to socket */
5459 if (is_root
&& set_permissions(rundir
)) {
5461 goto exit_init_data
;
5464 /* Get parent pid if -S, --sig-parent is specified. */
5465 if (opt_sig_parent
) {
5469 /* Setup the kernel pipe for waking up the kernel thread */
5470 if (is_root
&& !opt_no_kernel
) {
5471 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5473 goto exit_init_data
;
5477 /* Setup the thread apps communication pipe. */
5478 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5480 goto exit_init_data
;
5483 /* Setup the thread apps notify communication pipe. */
5484 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5486 goto exit_init_data
;
5489 /* Initialize global buffer per UID and PID registry. */
5490 buffer_reg_init_uid_registry();
5491 buffer_reg_init_pid_registry();
5493 /* Init UST command queue. */
5494 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5497 * Get session list pointer. This pointer MUST NOT be free'd. This list
5498 * is statically declared in session.c
5500 session_list_ptr
= session_get_list();
5502 /* Set up max poll set size */
5503 lttng_poll_set_max_size();
5507 /* Check for the application socket timeout env variable. */
5508 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5509 if (env_app_timeout
) {
5510 app_socket_timeout
= atoi(env_app_timeout
);
5512 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5515 ret
= write_pidfile();
5517 ERR("Error in write_pidfile");
5519 goto exit_init_data
;
5521 ret
= write_agent_port();
5523 ERR("Error in write_agent_port");
5525 goto exit_init_data
;
5528 /* Initialize communication library */
5530 /* Initialize TCP timeout values */
5531 lttcomm_inet_init();
5533 if (load_session_init_data(&load_info
) < 0) {
5535 goto exit_init_data
;
5537 load_info
->path
= opt_load_session_path
;
5539 /* Create health-check thread */
5540 ret
= pthread_create(&health_thread
, NULL
,
5541 thread_manage_health
, (void *) NULL
);
5544 PERROR("pthread_create health");
5549 /* Create thread to manage the client socket */
5550 ret
= pthread_create(&client_thread
, NULL
,
5551 thread_manage_clients
, (void *) NULL
);
5554 PERROR("pthread_create clients");
5559 /* Create thread to dispatch registration */
5560 ret
= pthread_create(&dispatch_thread
, NULL
,
5561 thread_dispatch_ust_registration
, (void *) NULL
);
5564 PERROR("pthread_create dispatch");
5569 /* Create thread to manage application registration. */
5570 ret
= pthread_create(®_apps_thread
, NULL
,
5571 thread_registration_apps
, (void *) NULL
);
5574 PERROR("pthread_create registration");
5579 /* Create thread to manage application socket */
5580 ret
= pthread_create(&apps_thread
, NULL
,
5581 thread_manage_apps
, (void *) NULL
);
5584 PERROR("pthread_create apps");
5589 /* Create thread to manage application notify socket */
5590 ret
= pthread_create(&apps_notify_thread
, NULL
,
5591 ust_thread_manage_notify
, (void *) NULL
);
5594 PERROR("pthread_create notify");
5596 goto exit_apps_notify
;
5599 /* Create agent registration thread. */
5600 ret
= pthread_create(&agent_reg_thread
, NULL
,
5601 agent_thread_manage_registration
, (void *) NULL
);
5604 PERROR("pthread_create agent");
5606 goto exit_agent_reg
;
5609 /* Don't start this thread if kernel tracing is not requested nor root */
5610 if (is_root
&& !opt_no_kernel
) {
5611 /* Create kernel thread to manage kernel event */
5612 ret
= pthread_create(&kernel_thread
, NULL
,
5613 thread_manage_kernel
, (void *) NULL
);
5616 PERROR("pthread_create kernel");
5622 /* Create session loading thread. */
5623 ret
= pthread_create(&load_session_thread
, NULL
, thread_load_session
,
5627 PERROR("pthread_create load_session_thread");
5629 goto exit_load_session
;
5633 * This is where we start awaiting program completion (e.g. through
5634 * signal that asks threads to teardown).
5637 ret
= pthread_join(load_session_thread
, &status
);
5640 PERROR("pthread_join load_session_thread");
5645 if (is_root
&& !opt_no_kernel
) {
5646 ret
= pthread_join(kernel_thread
, &status
);
5649 PERROR("pthread_join");
5655 ret
= pthread_join(agent_reg_thread
, &status
);
5658 PERROR("pthread_join agent");
5663 ret
= pthread_join(apps_notify_thread
, &status
);
5666 PERROR("pthread_join apps notify");
5671 ret
= pthread_join(apps_thread
, &status
);
5674 PERROR("pthread_join apps");
5679 ret
= pthread_join(reg_apps_thread
, &status
);
5682 PERROR("pthread_join");
5687 ret
= pthread_join(dispatch_thread
, &status
);
5690 PERROR("pthread_join");
5695 ret
= pthread_join(client_thread
, &status
);
5698 PERROR("pthread_join");
5703 ret
= pthread_join(health_thread
, &status
);
5706 PERROR("pthread_join health thread");
5713 * sessiond_cleanup() is called when no other thread is running, except
5714 * the ht_cleanup thread, which is needed to destroy the hash tables.
5716 rcu_thread_online();
5718 rcu_thread_offline();
5719 rcu_unregister_thread();
5721 ret
= notify_thread_pipe(ht_cleanup_quit_pipe
[1]);
5723 ERR("write error on ht_cleanup quit pipe");
5727 ret
= pthread_join(ht_cleanup_thread
, &status
);
5730 PERROR("pthread_join ht cleanup thread");
5735 utils_close_pipe(ht_cleanup_pipe
);
5736 exit_ht_cleanup_pipe
:
5739 * Close the ht_cleanup quit pipe.
5741 utils_close_pipe(ht_cleanup_quit_pipe
);
5742 exit_ht_cleanup_quit_pipe
:
5744 health_app_destroy(health_sessiond
);
5745 exit_health_sessiond_cleanup
:
5748 sessiond_cleanup_options();
5750 exit_set_signal_handler
: