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");
1064 /* Poll infinite value of time */
1066 health_poll_entry();
1067 ret
= lttng_poll_wait(&events
, -1);
1068 DBG("Thread kernel return from poll on %d fds",
1069 LTTNG_POLL_GETNB(&events
));
1073 * Restart interrupted system call.
1075 if (errno
== EINTR
) {
1079 } else if (ret
== 0) {
1080 /* Should not happen since timeout is infinite */
1081 ERR("Return value of poll is 0 with an infinite timeout.\n"
1082 "This should not have happened! Continuing...");
1088 for (i
= 0; i
< nb_fd
; i
++) {
1089 /* Fetch once the poll data */
1090 revents
= LTTNG_POLL_GETEV(&events
, i
);
1091 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1093 health_code_update();
1096 /* No activity for this FD (poll implementation). */
1100 /* Thread quit pipe has been closed. Killing thread. */
1101 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1107 /* Check for data on kernel pipe */
1108 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
1109 (void) lttng_read(kernel_poll_pipe
[0],
1112 * Ret value is useless here, if this pipe gets any actions an
1113 * update is required anyway.
1115 update_poll_flag
= 1;
1119 * New CPU detected by the kernel. Adding kernel stream to
1120 * kernel session and updating the kernel consumer
1122 if (revents
& LPOLLIN
) {
1123 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1129 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
1130 * and unregister kernel stream at this point.
1139 lttng_poll_clean(&events
);
1142 utils_close_pipe(kernel_poll_pipe
);
1143 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1146 ERR("Health error occurred in %s", __func__
);
1147 WARN("Kernel thread died unexpectedly. "
1148 "Kernel tracing can continue but CPU hotplug is disabled.");
1150 health_unregister(health_sessiond
);
1151 DBG("Kernel thread dying");
1156 * Signal pthread condition of the consumer data that the thread.
1158 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1160 pthread_mutex_lock(&data
->cond_mutex
);
1163 * The state is set before signaling. It can be any value, it's the waiter
1164 * job to correctly interpret this condition variable associated to the
1165 * consumer pthread_cond.
1167 * A value of 0 means that the corresponding thread of the consumer data
1168 * was not started. 1 indicates that the thread has started and is ready
1169 * for action. A negative value means that there was an error during the
1172 data
->consumer_thread_is_ready
= state
;
1173 (void) pthread_cond_signal(&data
->cond
);
1175 pthread_mutex_unlock(&data
->cond_mutex
);
1179 * This thread manage the consumer error sent back to the session daemon.
1181 static void *thread_manage_consumer(void *data
)
1183 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1184 uint32_t revents
, nb_fd
;
1185 enum lttcomm_return_code code
;
1186 struct lttng_poll_event events
;
1187 struct consumer_data
*consumer_data
= data
;
1189 DBG("[thread] Manage consumer started");
1191 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1193 health_code_update();
1196 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1197 * metadata_sock. Nothing more will be added to this poll set.
1199 ret
= sessiond_set_thread_pollset(&events
, 3);
1205 * The error socket here is already in a listening state which was done
1206 * just before spawning this thread to avoid a race between the consumer
1207 * daemon exec trying to connect and the listen() call.
1209 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1214 health_code_update();
1216 /* Infinite blocking call, waiting for transmission */
1218 health_poll_entry();
1220 if (testpoint(sessiond_thread_manage_consumer
)) {
1224 ret
= lttng_poll_wait(&events
, -1);
1228 * Restart interrupted system call.
1230 if (errno
== EINTR
) {
1238 for (i
= 0; i
< nb_fd
; i
++) {
1239 /* Fetch once the poll data */
1240 revents
= LTTNG_POLL_GETEV(&events
, i
);
1241 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1243 health_code_update();
1246 /* No activity for this FD (poll implementation). */
1250 /* Thread quit pipe has been closed. Killing thread. */
1251 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1257 /* Event on the registration socket */
1258 if (pollfd
== consumer_data
->err_sock
) {
1259 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1260 ERR("consumer err socket poll error");
1266 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1272 * Set the CLOEXEC flag. Return code is useless because either way, the
1275 (void) utils_set_fd_cloexec(sock
);
1277 health_code_update();
1279 DBG2("Receiving code from consumer err_sock");
1281 /* Getting status code from kconsumerd */
1282 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1283 sizeof(enum lttcomm_return_code
));
1288 health_code_update();
1289 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1290 /* Connect both socket, command and metadata. */
1291 consumer_data
->cmd_sock
=
1292 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1293 consumer_data
->metadata_fd
=
1294 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1295 if (consumer_data
->cmd_sock
< 0
1296 || consumer_data
->metadata_fd
< 0) {
1297 PERROR("consumer connect cmd socket");
1298 /* On error, signal condition and quit. */
1299 signal_consumer_condition(consumer_data
, -1);
1302 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1303 /* Create metadata socket lock. */
1304 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1305 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1306 PERROR("zmalloc pthread mutex");
1310 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1312 signal_consumer_condition(consumer_data
, 1);
1313 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1314 DBG("Consumer metadata socket ready (fd: %d)",
1315 consumer_data
->metadata_fd
);
1317 ERR("consumer error when waiting for SOCK_READY : %s",
1318 lttcomm_get_readable_code(-code
));
1322 /* Remove the consumerd error sock since we've established a connexion */
1323 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1328 /* Add new accepted error socket. */
1329 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1334 /* Add metadata socket that is successfully connected. */
1335 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1336 LPOLLIN
| LPOLLRDHUP
);
1341 health_code_update();
1343 /* Infinite blocking call, waiting for transmission */
1346 health_code_update();
1348 /* Exit the thread because the thread quit pipe has been triggered. */
1350 /* Not a health error. */
1355 health_poll_entry();
1356 ret
= lttng_poll_wait(&events
, -1);
1360 * Restart interrupted system call.
1362 if (errno
== EINTR
) {
1370 for (i
= 0; i
< nb_fd
; i
++) {
1371 /* Fetch once the poll data */
1372 revents
= LTTNG_POLL_GETEV(&events
, i
);
1373 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1375 health_code_update();
1378 /* No activity for this FD (poll implementation). */
1383 * Thread quit pipe has been triggered, flag that we should stop
1384 * but continue the current loop to handle potential data from
1387 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1389 if (pollfd
== sock
) {
1390 /* Event on the consumerd socket */
1391 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1392 ERR("consumer err socket second poll error");
1395 health_code_update();
1396 /* Wait for any kconsumerd error */
1397 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1398 sizeof(enum lttcomm_return_code
));
1400 ERR("consumer closed the command socket");
1404 ERR("consumer return code : %s",
1405 lttcomm_get_readable_code(-code
));
1408 } else if (pollfd
== consumer_data
->metadata_fd
) {
1409 /* UST metadata requests */
1410 ret
= ust_consumer_metadata_request(
1411 &consumer_data
->metadata_sock
);
1413 ERR("Handling metadata request");
1417 /* No need for an else branch all FDs are tested prior. */
1419 health_code_update();
1425 * We lock here because we are about to close the sockets and some other
1426 * thread might be using them so get exclusive access which will abort all
1427 * other consumer command by other threads.
1429 pthread_mutex_lock(&consumer_data
->lock
);
1431 /* Immediately set the consumerd state to stopped */
1432 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1433 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1434 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1435 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1436 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1438 /* Code flow error... */
1442 if (consumer_data
->err_sock
>= 0) {
1443 ret
= close(consumer_data
->err_sock
);
1447 consumer_data
->err_sock
= -1;
1449 if (consumer_data
->cmd_sock
>= 0) {
1450 ret
= close(consumer_data
->cmd_sock
);
1454 consumer_data
->cmd_sock
= -1;
1456 if (consumer_data
->metadata_sock
.fd_ptr
&&
1457 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1458 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1470 unlink(consumer_data
->err_unix_sock_path
);
1471 unlink(consumer_data
->cmd_unix_sock_path
);
1472 consumer_data
->pid
= 0;
1473 pthread_mutex_unlock(&consumer_data
->lock
);
1475 /* Cleanup metadata socket mutex. */
1476 if (consumer_data
->metadata_sock
.lock
) {
1477 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1478 free(consumer_data
->metadata_sock
.lock
);
1480 lttng_poll_clean(&events
);
1484 ERR("Health error occurred in %s", __func__
);
1486 health_unregister(health_sessiond
);
1487 DBG("consumer thread cleanup completed");
1493 * This thread manage application communication.
1495 static void *thread_manage_apps(void *data
)
1497 int i
, ret
, pollfd
, err
= -1;
1499 uint32_t revents
, nb_fd
;
1500 struct lttng_poll_event events
;
1502 DBG("[thread] Manage application started");
1504 rcu_register_thread();
1505 rcu_thread_online();
1507 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1509 if (testpoint(sessiond_thread_manage_apps
)) {
1510 goto error_testpoint
;
1513 health_code_update();
1515 ret
= sessiond_set_thread_pollset(&events
, 2);
1517 goto error_poll_create
;
1520 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1525 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1529 health_code_update();
1532 DBG("Apps thread polling");
1534 /* Inifinite blocking call, waiting for transmission */
1536 health_poll_entry();
1537 ret
= lttng_poll_wait(&events
, -1);
1538 DBG("Apps thread return from poll on %d fds",
1539 LTTNG_POLL_GETNB(&events
));
1543 * Restart interrupted system call.
1545 if (errno
== EINTR
) {
1553 for (i
= 0; i
< nb_fd
; i
++) {
1554 /* Fetch once the poll data */
1555 revents
= LTTNG_POLL_GETEV(&events
, i
);
1556 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1558 health_code_update();
1561 /* No activity for this FD (poll implementation). */
1565 /* Thread quit pipe has been closed. Killing thread. */
1566 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1572 /* Inspect the apps cmd pipe */
1573 if (pollfd
== apps_cmd_pipe
[0]) {
1574 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1575 ERR("Apps command pipe error");
1577 } else if (revents
& LPOLLIN
) {
1581 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1582 if (size_ret
< sizeof(sock
)) {
1583 PERROR("read apps cmd pipe");
1587 health_code_update();
1590 * We only monitor the error events of the socket. This
1591 * thread does not handle any incoming data from UST
1594 ret
= lttng_poll_add(&events
, sock
,
1595 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1600 DBG("Apps with sock %d added to poll set", sock
);
1604 * At this point, we know that a registered application made
1605 * the event at poll_wait.
1607 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1608 /* Removing from the poll set */
1609 ret
= lttng_poll_del(&events
, pollfd
);
1614 /* Socket closed on remote end. */
1615 ust_app_unregister(pollfd
);
1619 health_code_update();
1625 lttng_poll_clean(&events
);
1628 utils_close_pipe(apps_cmd_pipe
);
1629 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1632 * We don't clean the UST app hash table here since already registered
1633 * applications can still be controlled so let them be until the session
1634 * daemon dies or the applications stop.
1639 ERR("Health error occurred in %s", __func__
);
1641 health_unregister(health_sessiond
);
1642 DBG("Application communication apps thread cleanup complete");
1643 rcu_thread_offline();
1644 rcu_unregister_thread();
1649 * Send a socket to a thread This is called from the dispatch UST registration
1650 * thread once all sockets are set for the application.
1652 * The sock value can be invalid, we don't really care, the thread will handle
1653 * it and make the necessary cleanup if so.
1655 * On success, return 0 else a negative value being the errno message of the
1658 static int send_socket_to_thread(int fd
, int sock
)
1663 * It's possible that the FD is set as invalid with -1 concurrently just
1664 * before calling this function being a shutdown state of the thread.
1671 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1672 if (ret
< sizeof(sock
)) {
1673 PERROR("write apps pipe %d", fd
);
1680 /* All good. Don't send back the write positive ret value. */
1687 * Sanitize the wait queue of the dispatch registration thread meaning removing
1688 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1689 * notify socket is never received.
1691 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1693 int ret
, nb_fd
= 0, i
;
1694 unsigned int fd_added
= 0;
1695 struct lttng_poll_event events
;
1696 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1700 lttng_poll_init(&events
);
1702 /* Just skip everything for an empty queue. */
1703 if (!wait_queue
->count
) {
1707 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1712 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1713 &wait_queue
->head
, head
) {
1714 assert(wait_node
->app
);
1715 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1716 LPOLLHUP
| LPOLLERR
);
1729 * Poll but don't block so we can quickly identify the faulty events and
1730 * clean them afterwards from the wait queue.
1732 ret
= lttng_poll_wait(&events
, 0);
1738 for (i
= 0; i
< nb_fd
; i
++) {
1739 /* Get faulty FD. */
1740 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1741 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1744 /* No activity for this FD (poll implementation). */
1748 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1749 &wait_queue
->head
, head
) {
1750 if (pollfd
== wait_node
->app
->sock
&&
1751 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1752 cds_list_del(&wait_node
->head
);
1753 wait_queue
->count
--;
1754 ust_app_destroy(wait_node
->app
);
1762 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1766 lttng_poll_clean(&events
);
1770 lttng_poll_clean(&events
);
1772 ERR("Unable to sanitize wait queue");
1777 * Dispatch request from the registration threads to the application
1778 * communication thread.
1780 static void *thread_dispatch_ust_registration(void *data
)
1783 struct cds_wfcq_node
*node
;
1784 struct ust_command
*ust_cmd
= NULL
;
1785 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1786 struct ust_reg_wait_queue wait_queue
= {
1790 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1792 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1793 goto error_testpoint
;
1796 health_code_update();
1798 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1800 DBG("[thread] Dispatch UST command started");
1802 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1803 health_code_update();
1805 /* Atomically prepare the queue futex */
1806 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1809 struct ust_app
*app
= NULL
;
1813 * Make sure we don't have node(s) that have hung up before receiving
1814 * the notify socket. This is to clean the list in order to avoid
1815 * memory leaks from notify socket that are never seen.
1817 sanitize_wait_queue(&wait_queue
);
1819 health_code_update();
1820 /* Dequeue command for registration */
1821 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1823 DBG("Woken up but nothing in the UST command queue");
1824 /* Continue thread execution */
1828 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1830 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1831 " gid:%d sock:%d name:%s (version %d.%d)",
1832 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1833 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1834 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1835 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1837 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1838 wait_node
= zmalloc(sizeof(*wait_node
));
1840 PERROR("zmalloc wait_node dispatch");
1841 ret
= close(ust_cmd
->sock
);
1843 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1845 lttng_fd_put(LTTNG_FD_APPS
, 1);
1849 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1851 /* Create application object if socket is CMD. */
1852 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1854 if (!wait_node
->app
) {
1855 ret
= close(ust_cmd
->sock
);
1857 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1859 lttng_fd_put(LTTNG_FD_APPS
, 1);
1865 * Add application to the wait queue so we can set the notify
1866 * socket before putting this object in the global ht.
1868 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1873 * We have to continue here since we don't have the notify
1874 * socket and the application MUST be added to the hash table
1875 * only at that moment.
1880 * Look for the application in the local wait queue and set the
1881 * notify socket if found.
1883 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1884 &wait_queue
.head
, head
) {
1885 health_code_update();
1886 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
1887 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
1888 cds_list_del(&wait_node
->head
);
1890 app
= wait_node
->app
;
1892 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
1898 * With no application at this stage the received socket is
1899 * basically useless so close it before we free the cmd data
1900 * structure for good.
1903 ret
= close(ust_cmd
->sock
);
1905 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1907 lttng_fd_put(LTTNG_FD_APPS
, 1);
1914 * @session_lock_list
1916 * Lock the global session list so from the register up to the
1917 * registration done message, no thread can see the application
1918 * and change its state.
1920 session_lock_list();
1924 * Add application to the global hash table. This needs to be
1925 * done before the update to the UST registry can locate the
1930 /* Set app version. This call will print an error if needed. */
1931 (void) ust_app_version(app
);
1933 /* Send notify socket through the notify pipe. */
1934 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
1938 session_unlock_list();
1940 * No notify thread, stop the UST tracing. However, this is
1941 * not an internal error of the this thread thus setting
1942 * the health error code to a normal exit.
1949 * Update newly registered application with the tracing
1950 * registry info already enabled information.
1952 update_ust_app(app
->sock
);
1955 * Don't care about return value. Let the manage apps threads
1956 * handle app unregistration upon socket close.
1958 (void) ust_app_register_done(app
->sock
);
1961 * Even if the application socket has been closed, send the app
1962 * to the thread and unregistration will take place at that
1965 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
1968 session_unlock_list();
1970 * No apps. thread, stop the UST tracing. However, this is
1971 * not an internal error of the this thread thus setting
1972 * the health error code to a normal exit.
1979 session_unlock_list();
1981 } while (node
!= NULL
);
1983 health_poll_entry();
1984 /* Futex wait on queue. Blocking call on futex() */
1985 futex_nto1_wait(&ust_cmd_queue
.futex
);
1988 /* Normal exit, no error */
1992 /* Clean up wait queue. */
1993 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1994 &wait_queue
.head
, head
) {
1995 cds_list_del(&wait_node
->head
);
2001 DBG("Dispatch thread dying");
2004 ERR("Health error occurred in %s", __func__
);
2006 health_unregister(health_sessiond
);
2011 * This thread manage application registration.
2013 static void *thread_registration_apps(void *data
)
2015 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2016 uint32_t revents
, nb_fd
;
2017 struct lttng_poll_event events
;
2019 * Get allocated in this thread, enqueued to a global queue, dequeued and
2020 * freed in the manage apps thread.
2022 struct ust_command
*ust_cmd
= NULL
;
2024 DBG("[thread] Manage application registration started");
2026 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2028 if (testpoint(sessiond_thread_registration_apps
)) {
2029 goto error_testpoint
;
2032 ret
= lttcomm_listen_unix_sock(apps_sock
);
2038 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2039 * more will be added to this poll set.
2041 ret
= sessiond_set_thread_pollset(&events
, 2);
2043 goto error_create_poll
;
2046 /* Add the application registration socket */
2047 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2049 goto error_poll_add
;
2052 /* Notify all applications to register */
2053 ret
= notify_ust_apps(1);
2055 ERR("Failed to notify applications or create the wait shared memory.\n"
2056 "Execution continues but there might be problem for already\n"
2057 "running applications that wishes to register.");
2061 DBG("Accepting application registration");
2063 /* Inifinite blocking call, waiting for transmission */
2065 health_poll_entry();
2066 ret
= lttng_poll_wait(&events
, -1);
2070 * Restart interrupted system call.
2072 if (errno
== EINTR
) {
2080 for (i
= 0; i
< nb_fd
; i
++) {
2081 health_code_update();
2083 /* Fetch once the poll data */
2084 revents
= LTTNG_POLL_GETEV(&events
, i
);
2085 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2088 /* No activity for this FD (poll implementation). */
2092 /* Thread quit pipe has been closed. Killing thread. */
2093 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2099 /* Event on the registration socket */
2100 if (pollfd
== apps_sock
) {
2101 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2102 ERR("Register apps socket poll error");
2104 } else if (revents
& LPOLLIN
) {
2105 sock
= lttcomm_accept_unix_sock(apps_sock
);
2111 * Set socket timeout for both receiving and ending.
2112 * app_socket_timeout is in seconds, whereas
2113 * lttcomm_setsockopt_rcv_timeout and
2114 * lttcomm_setsockopt_snd_timeout expect msec as
2117 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2118 app_socket_timeout
* 1000);
2119 (void) lttcomm_setsockopt_snd_timeout(sock
,
2120 app_socket_timeout
* 1000);
2123 * Set the CLOEXEC flag. Return code is useless because
2124 * either way, the show must go on.
2126 (void) utils_set_fd_cloexec(sock
);
2128 /* Create UST registration command for enqueuing */
2129 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2130 if (ust_cmd
== NULL
) {
2131 PERROR("ust command zmalloc");
2136 * Using message-based transmissions to ensure we don't
2137 * have to deal with partially received messages.
2139 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2141 ERR("Exhausted file descriptors allowed for applications.");
2151 health_code_update();
2152 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2155 /* Close socket of the application. */
2160 lttng_fd_put(LTTNG_FD_APPS
, 1);
2164 health_code_update();
2166 ust_cmd
->sock
= sock
;
2169 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2170 " gid:%d sock:%d name:%s (version %d.%d)",
2171 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2172 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2173 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2174 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2177 * Lock free enqueue the registration request. The red pill
2178 * has been taken! This apps will be part of the *system*.
2180 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2183 * Wake the registration queue futex. Implicit memory
2184 * barrier with the exchange in cds_wfcq_enqueue.
2186 futex_nto1_wake(&ust_cmd_queue
.futex
);
2194 /* Notify that the registration thread is gone */
2197 if (apps_sock
>= 0) {
2198 ret
= close(apps_sock
);
2208 lttng_fd_put(LTTNG_FD_APPS
, 1);
2210 unlink(apps_unix_sock_path
);
2213 lttng_poll_clean(&events
);
2217 DBG("UST Registration thread cleanup complete");
2220 ERR("Health error occurred in %s", __func__
);
2222 health_unregister(health_sessiond
);
2228 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2229 * exec or it will fails.
2231 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2234 struct timespec timeout
;
2236 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2237 consumer_data
->consumer_thread_is_ready
= 0;
2239 /* Setup pthread condition */
2240 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2243 PERROR("pthread_condattr_init consumer data");
2248 * Set the monotonic clock in order to make sure we DO NOT jump in time
2249 * between the clock_gettime() call and the timedwait call. See bug #324
2250 * for a more details and how we noticed it.
2252 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2255 PERROR("pthread_condattr_setclock consumer data");
2259 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2262 PERROR("pthread_cond_init consumer data");
2266 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2270 PERROR("pthread_create consumer");
2275 /* We are about to wait on a pthread condition */
2276 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2278 /* Get time for sem_timedwait absolute timeout */
2279 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2281 * Set the timeout for the condition timed wait even if the clock gettime
2282 * call fails since we might loop on that call and we want to avoid to
2283 * increment the timeout too many times.
2285 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2288 * The following loop COULD be skipped in some conditions so this is why we
2289 * set ret to 0 in order to make sure at least one round of the loop is
2295 * Loop until the condition is reached or when a timeout is reached. Note
2296 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2297 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2298 * possible. This loop does not take any chances and works with both of
2301 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2302 if (clock_ret
< 0) {
2303 PERROR("clock_gettime spawn consumer");
2304 /* Infinite wait for the consumerd thread to be ready */
2305 ret
= pthread_cond_wait(&consumer_data
->cond
,
2306 &consumer_data
->cond_mutex
);
2308 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2309 &consumer_data
->cond_mutex
, &timeout
);
2313 /* Release the pthread condition */
2314 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2318 if (ret
== ETIMEDOUT
) {
2322 * Call has timed out so we kill the kconsumerd_thread and return
2325 ERR("Condition timed out. The consumer thread was never ready."
2327 pth_ret
= pthread_cancel(consumer_data
->thread
);
2329 PERROR("pthread_cancel consumer thread");
2332 PERROR("pthread_cond_wait failed consumer thread");
2334 /* Caller is expecting a negative value on failure. */
2339 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2340 if (consumer_data
->pid
== 0) {
2341 ERR("Consumerd did not start");
2342 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2345 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2354 * Join consumer thread
2356 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2360 /* Consumer pid must be a real one. */
2361 if (consumer_data
->pid
> 0) {
2363 ret
= kill(consumer_data
->pid
, SIGTERM
);
2365 PERROR("Error killing consumer daemon");
2368 return pthread_join(consumer_data
->thread
, &status
);
2375 * Fork and exec a consumer daemon (consumerd).
2377 * Return pid if successful else -1.
2379 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2383 const char *consumer_to_use
;
2384 const char *verbosity
;
2387 DBG("Spawning consumerd");
2394 if (opt_verbose_consumer
) {
2395 verbosity
= "--verbose";
2396 } else if (lttng_opt_quiet
) {
2397 verbosity
= "--quiet";
2402 switch (consumer_data
->type
) {
2403 case LTTNG_CONSUMER_KERNEL
:
2405 * Find out which consumerd to execute. We will first try the
2406 * 64-bit path, then the sessiond's installation directory, and
2407 * fallback on the 32-bit one,
2409 DBG3("Looking for a kernel consumer at these locations:");
2410 DBG3(" 1) %s", consumerd64_bin
);
2411 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2412 DBG3(" 3) %s", consumerd32_bin
);
2413 if (stat(consumerd64_bin
, &st
) == 0) {
2414 DBG3("Found location #1");
2415 consumer_to_use
= consumerd64_bin
;
2416 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2417 DBG3("Found location #2");
2418 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2419 } else if (stat(consumerd32_bin
, &st
) == 0) {
2420 DBG3("Found location #3");
2421 consumer_to_use
= consumerd32_bin
;
2423 DBG("Could not find any valid consumerd executable");
2427 DBG("Using kernel consumer at: %s", consumer_to_use
);
2428 ret
= execl(consumer_to_use
,
2429 "lttng-consumerd", verbosity
, "-k",
2430 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2431 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2432 "--group", tracing_group_name
,
2435 case LTTNG_CONSUMER64_UST
:
2437 char *tmpnew
= NULL
;
2439 if (consumerd64_libdir
[0] != '\0') {
2443 tmp
= getenv("LD_LIBRARY_PATH");
2447 tmplen
= strlen("LD_LIBRARY_PATH=")
2448 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2449 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2454 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2455 strcat(tmpnew
, consumerd64_libdir
);
2456 if (tmp
[0] != '\0') {
2457 strcat(tmpnew
, ":");
2458 strcat(tmpnew
, tmp
);
2460 ret
= putenv(tmpnew
);
2467 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2468 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2469 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2470 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2471 "--group", tracing_group_name
,
2473 if (consumerd64_libdir
[0] != '\0') {
2478 case LTTNG_CONSUMER32_UST
:
2480 char *tmpnew
= NULL
;
2482 if (consumerd32_libdir
[0] != '\0') {
2486 tmp
= getenv("LD_LIBRARY_PATH");
2490 tmplen
= strlen("LD_LIBRARY_PATH=")
2491 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2492 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2497 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2498 strcat(tmpnew
, consumerd32_libdir
);
2499 if (tmp
[0] != '\0') {
2500 strcat(tmpnew
, ":");
2501 strcat(tmpnew
, tmp
);
2503 ret
= putenv(tmpnew
);
2510 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2511 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2512 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2513 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2514 "--group", tracing_group_name
,
2516 if (consumerd32_libdir
[0] != '\0') {
2522 PERROR("unknown consumer type");
2526 PERROR("Consumer execl()");
2528 /* Reaching this point, we got a failure on our execl(). */
2530 } else if (pid
> 0) {
2533 PERROR("start consumer fork");
2541 * Spawn the consumerd daemon and session daemon thread.
2543 static int start_consumerd(struct consumer_data
*consumer_data
)
2548 * Set the listen() state on the socket since there is a possible race
2549 * between the exec() of the consumer daemon and this call if place in the
2550 * consumer thread. See bug #366 for more details.
2552 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2557 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2558 if (consumer_data
->pid
!= 0) {
2559 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2563 ret
= spawn_consumerd(consumer_data
);
2565 ERR("Spawning consumerd failed");
2566 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2570 /* Setting up the consumer_data pid */
2571 consumer_data
->pid
= ret
;
2572 DBG2("Consumer pid %d", consumer_data
->pid
);
2573 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2575 DBG2("Spawning consumer control thread");
2576 ret
= spawn_consumer_thread(consumer_data
);
2578 ERR("Fatal error spawning consumer control thread");
2586 /* Cleanup already created sockets on error. */
2587 if (consumer_data
->err_sock
>= 0) {
2590 err
= close(consumer_data
->err_sock
);
2592 PERROR("close consumer data error socket");
2599 * Setup necessary data for kernel tracer action.
2601 static int init_kernel_tracer(void)
2605 /* Modprobe lttng kernel modules */
2606 ret
= modprobe_lttng_control();
2611 /* Open debugfs lttng */
2612 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2613 if (kernel_tracer_fd
< 0) {
2614 DBG("Failed to open %s", module_proc_lttng
);
2619 /* Validate kernel version */
2620 ret
= kernel_validate_version(kernel_tracer_fd
);
2625 ret
= modprobe_lttng_data();
2630 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2634 modprobe_remove_lttng_control();
2635 ret
= close(kernel_tracer_fd
);
2639 kernel_tracer_fd
= -1;
2640 return LTTNG_ERR_KERN_VERSION
;
2643 ret
= close(kernel_tracer_fd
);
2649 modprobe_remove_lttng_control();
2652 WARN("No kernel tracer available");
2653 kernel_tracer_fd
= -1;
2655 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2657 return LTTNG_ERR_KERN_NA
;
2663 * Copy consumer output from the tracing session to the domain session. The
2664 * function also applies the right modification on a per domain basis for the
2665 * trace files destination directory.
2667 * Should *NOT* be called with RCU read-side lock held.
2669 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2672 const char *dir_name
;
2673 struct consumer_output
*consumer
;
2676 assert(session
->consumer
);
2679 case LTTNG_DOMAIN_KERNEL
:
2680 DBG3("Copying tracing session consumer output in kernel session");
2682 * XXX: We should audit the session creation and what this function
2683 * does "extra" in order to avoid a destroy since this function is used
2684 * in the domain session creation (kernel and ust) only. Same for UST
2687 if (session
->kernel_session
->consumer
) {
2688 consumer_destroy_output(session
->kernel_session
->consumer
);
2690 session
->kernel_session
->consumer
=
2691 consumer_copy_output(session
->consumer
);
2692 /* Ease our life a bit for the next part */
2693 consumer
= session
->kernel_session
->consumer
;
2694 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2696 case LTTNG_DOMAIN_JUL
:
2697 case LTTNG_DOMAIN_LOG4J
:
2698 case LTTNG_DOMAIN_PYTHON
:
2699 case LTTNG_DOMAIN_UST
:
2700 DBG3("Copying tracing session consumer output in UST session");
2701 if (session
->ust_session
->consumer
) {
2702 consumer_destroy_output(session
->ust_session
->consumer
);
2704 session
->ust_session
->consumer
=
2705 consumer_copy_output(session
->consumer
);
2706 /* Ease our life a bit for the next part */
2707 consumer
= session
->ust_session
->consumer
;
2708 dir_name
= DEFAULT_UST_TRACE_DIR
;
2711 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2715 /* Append correct directory to subdir */
2716 strncat(consumer
->subdir
, dir_name
,
2717 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2718 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2727 * Create an UST session and add it to the session ust list.
2729 * Should *NOT* be called with RCU read-side lock held.
2731 static int create_ust_session(struct ltt_session
*session
,
2732 struct lttng_domain
*domain
)
2735 struct ltt_ust_session
*lus
= NULL
;
2739 assert(session
->consumer
);
2741 switch (domain
->type
) {
2742 case LTTNG_DOMAIN_JUL
:
2743 case LTTNG_DOMAIN_LOG4J
:
2744 case LTTNG_DOMAIN_PYTHON
:
2745 case LTTNG_DOMAIN_UST
:
2748 ERR("Unknown UST domain on create session %d", domain
->type
);
2749 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2753 DBG("Creating UST session");
2755 lus
= trace_ust_create_session(session
->id
);
2757 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2761 lus
->uid
= session
->uid
;
2762 lus
->gid
= session
->gid
;
2763 lus
->output_traces
= session
->output_traces
;
2764 lus
->snapshot_mode
= session
->snapshot_mode
;
2765 lus
->live_timer_interval
= session
->live_timer
;
2766 session
->ust_session
= lus
;
2768 /* Copy session output to the newly created UST session */
2769 ret
= copy_session_consumer(domain
->type
, session
);
2770 if (ret
!= LTTNG_OK
) {
2778 session
->ust_session
= NULL
;
2783 * Create a kernel tracer session then create the default channel.
2785 static int create_kernel_session(struct ltt_session
*session
)
2789 DBG("Creating kernel session");
2791 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2793 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2797 /* Code flow safety */
2798 assert(session
->kernel_session
);
2800 /* Copy session output to the newly created Kernel session */
2801 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2802 if (ret
!= LTTNG_OK
) {
2806 /* Create directory(ies) on local filesystem. */
2807 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2808 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2809 ret
= run_as_mkdir_recursive(
2810 session
->kernel_session
->consumer
->dst
.trace_path
,
2811 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2813 if (ret
!= -EEXIST
) {
2814 ERR("Trace directory creation error");
2820 session
->kernel_session
->uid
= session
->uid
;
2821 session
->kernel_session
->gid
= session
->gid
;
2822 session
->kernel_session
->output_traces
= session
->output_traces
;
2823 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2828 trace_kernel_destroy_session(session
->kernel_session
);
2829 session
->kernel_session
= NULL
;
2834 * Count number of session permitted by uid/gid.
2836 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2839 struct ltt_session
*session
;
2841 DBG("Counting number of available session for UID %d GID %d",
2843 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2845 * Only list the sessions the user can control.
2847 if (!session_access_ok(session
, uid
, gid
)) {
2856 * Process the command requested by the lttng client within the command
2857 * context structure. This function make sure that the return structure (llm)
2858 * is set and ready for transmission before returning.
2860 * Return any error encountered or 0 for success.
2862 * "sock" is only used for special-case var. len data.
2864 * Should *NOT* be called with RCU read-side lock held.
2866 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
2870 int need_tracing_session
= 1;
2873 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
2877 switch (cmd_ctx
->lsm
->cmd_type
) {
2878 case LTTNG_CREATE_SESSION
:
2879 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2880 case LTTNG_CREATE_SESSION_LIVE
:
2881 case LTTNG_DESTROY_SESSION
:
2882 case LTTNG_LIST_SESSIONS
:
2883 case LTTNG_LIST_DOMAINS
:
2884 case LTTNG_START_TRACE
:
2885 case LTTNG_STOP_TRACE
:
2886 case LTTNG_DATA_PENDING
:
2887 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
2888 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
2889 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
2890 case LTTNG_SNAPSHOT_RECORD
:
2891 case LTTNG_SAVE_SESSION
:
2898 if (opt_no_kernel
&& need_domain
2899 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
2901 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
2903 ret
= LTTNG_ERR_KERN_NA
;
2908 /* Deny register consumer if we already have a spawned consumer. */
2909 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
2910 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
2911 if (kconsumer_data
.pid
> 0) {
2912 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2913 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2916 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
2920 * Check for command that don't needs to allocate a returned payload. We do
2921 * this here so we don't have to make the call for no payload at each
2924 switch(cmd_ctx
->lsm
->cmd_type
) {
2925 case LTTNG_LIST_SESSIONS
:
2926 case LTTNG_LIST_TRACEPOINTS
:
2927 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2928 case LTTNG_LIST_DOMAINS
:
2929 case LTTNG_LIST_CHANNELS
:
2930 case LTTNG_LIST_EVENTS
:
2931 case LTTNG_LIST_SYSCALLS
:
2934 /* Setup lttng message with no payload */
2935 ret
= setup_lttng_msg(cmd_ctx
, 0);
2937 /* This label does not try to unlock the session */
2938 goto init_setup_error
;
2942 /* Commands that DO NOT need a session. */
2943 switch (cmd_ctx
->lsm
->cmd_type
) {
2944 case LTTNG_CREATE_SESSION
:
2945 case LTTNG_CREATE_SESSION_SNAPSHOT
:
2946 case LTTNG_CREATE_SESSION_LIVE
:
2947 case LTTNG_CALIBRATE
:
2948 case LTTNG_LIST_SESSIONS
:
2949 case LTTNG_LIST_TRACEPOINTS
:
2950 case LTTNG_LIST_SYSCALLS
:
2951 case LTTNG_LIST_TRACEPOINT_FIELDS
:
2952 case LTTNG_SAVE_SESSION
:
2953 need_tracing_session
= 0;
2956 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
2958 * We keep the session list lock across _all_ commands
2959 * for now, because the per-session lock does not
2960 * handle teardown properly.
2962 session_lock_list();
2963 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
2964 if (cmd_ctx
->session
== NULL
) {
2965 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
2968 /* Acquire lock for the session */
2969 session_lock(cmd_ctx
->session
);
2975 * Commands that need a valid session but should NOT create one if none
2976 * exists. Instead of creating one and destroying it when the command is
2977 * handled, process that right before so we save some round trip in useless
2980 switch (cmd_ctx
->lsm
->cmd_type
) {
2981 case LTTNG_DISABLE_CHANNEL
:
2982 case LTTNG_DISABLE_EVENT
:
2983 switch (cmd_ctx
->lsm
->domain
.type
) {
2984 case LTTNG_DOMAIN_KERNEL
:
2985 if (!cmd_ctx
->session
->kernel_session
) {
2986 ret
= LTTNG_ERR_NO_CHANNEL
;
2990 case LTTNG_DOMAIN_JUL
:
2991 case LTTNG_DOMAIN_LOG4J
:
2992 case LTTNG_DOMAIN_PYTHON
:
2993 case LTTNG_DOMAIN_UST
:
2994 if (!cmd_ctx
->session
->ust_session
) {
2995 ret
= LTTNG_ERR_NO_CHANNEL
;
3000 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3012 * Check domain type for specific "pre-action".
3014 switch (cmd_ctx
->lsm
->domain
.type
) {
3015 case LTTNG_DOMAIN_KERNEL
:
3017 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3021 /* Kernel tracer check */
3022 if (kernel_tracer_fd
== -1) {
3023 /* Basically, load kernel tracer modules */
3024 ret
= init_kernel_tracer();
3030 /* Consumer is in an ERROR state. Report back to client */
3031 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3032 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3036 /* Need a session for kernel command */
3037 if (need_tracing_session
) {
3038 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3039 ret
= create_kernel_session(cmd_ctx
->session
);
3041 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3046 /* Start the kernel consumer daemon */
3047 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3048 if (kconsumer_data
.pid
== 0 &&
3049 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3050 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3051 ret
= start_consumerd(&kconsumer_data
);
3053 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3056 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3058 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3062 * The consumer was just spawned so we need to add the socket to
3063 * the consumer output of the session if exist.
3065 ret
= consumer_create_socket(&kconsumer_data
,
3066 cmd_ctx
->session
->kernel_session
->consumer
);
3073 case LTTNG_DOMAIN_JUL
:
3074 case LTTNG_DOMAIN_LOG4J
:
3075 case LTTNG_DOMAIN_PYTHON
:
3076 case LTTNG_DOMAIN_UST
:
3078 if (!ust_app_supported()) {
3079 ret
= LTTNG_ERR_NO_UST
;
3082 /* Consumer is in an ERROR state. Report back to client */
3083 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3084 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3088 if (need_tracing_session
) {
3089 /* Create UST session if none exist. */
3090 if (cmd_ctx
->session
->ust_session
== NULL
) {
3091 ret
= create_ust_session(cmd_ctx
->session
,
3092 &cmd_ctx
->lsm
->domain
);
3093 if (ret
!= LTTNG_OK
) {
3098 /* Start the UST consumer daemons */
3100 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3101 if (consumerd64_bin
[0] != '\0' &&
3102 ustconsumer64_data
.pid
== 0 &&
3103 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3104 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3105 ret
= start_consumerd(&ustconsumer64_data
);
3107 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3108 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3112 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3113 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3115 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3119 * Setup socket for consumer 64 bit. No need for atomic access
3120 * since it was set above and can ONLY be set in this thread.
3122 ret
= consumer_create_socket(&ustconsumer64_data
,
3123 cmd_ctx
->session
->ust_session
->consumer
);
3129 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3130 if (consumerd32_bin
[0] != '\0' &&
3131 ustconsumer32_data
.pid
== 0 &&
3132 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3133 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3134 ret
= start_consumerd(&ustconsumer32_data
);
3136 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3137 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3141 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3142 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3144 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3148 * Setup socket for consumer 64 bit. No need for atomic access
3149 * since it was set above and can ONLY be set in this thread.
3151 ret
= consumer_create_socket(&ustconsumer32_data
,
3152 cmd_ctx
->session
->ust_session
->consumer
);
3164 /* Validate consumer daemon state when start/stop trace command */
3165 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3166 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3167 switch (cmd_ctx
->lsm
->domain
.type
) {
3168 case LTTNG_DOMAIN_JUL
:
3169 case LTTNG_DOMAIN_LOG4J
:
3170 case LTTNG_DOMAIN_PYTHON
:
3171 case LTTNG_DOMAIN_UST
:
3172 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3173 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3177 case LTTNG_DOMAIN_KERNEL
:
3178 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3179 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3187 * Check that the UID or GID match that of the tracing session.
3188 * The root user can interact with all sessions.
3190 if (need_tracing_session
) {
3191 if (!session_access_ok(cmd_ctx
->session
,
3192 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3193 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3194 ret
= LTTNG_ERR_EPERM
;
3200 * Send relayd information to consumer as soon as we have a domain and a
3203 if (cmd_ctx
->session
&& need_domain
) {
3205 * Setup relayd if not done yet. If the relayd information was already
3206 * sent to the consumer, this call will gracefully return.
3208 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3209 if (ret
!= LTTNG_OK
) {
3214 /* Process by command type */
3215 switch (cmd_ctx
->lsm
->cmd_type
) {
3216 case LTTNG_ADD_CONTEXT
:
3218 ret
= cmd_add_context(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3219 cmd_ctx
->lsm
->u
.context
.channel_name
,
3220 &cmd_ctx
->lsm
->u
.context
.ctx
, kernel_poll_pipe
[1]);
3223 case LTTNG_DISABLE_CHANNEL
:
3225 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3226 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3229 case LTTNG_DISABLE_EVENT
:
3231 /* FIXME: passing packed structure to non-packed pointer */
3232 /* TODO: handle filter */
3233 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3234 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3235 &cmd_ctx
->lsm
->u
.disable
.event
);
3238 case LTTNG_ENABLE_CHANNEL
:
3240 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3241 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3244 case LTTNG_ENABLE_EVENT
:
3246 struct lttng_event_exclusion
*exclusion
= NULL
;
3247 struct lttng_filter_bytecode
*bytecode
= NULL
;
3248 char *filter_expression
= NULL
;
3250 /* Handle exclusion events and receive it from the client. */
3251 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3252 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3254 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3255 (count
* LTTNG_SYMBOL_NAME_LEN
));
3257 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3261 DBG("Receiving var len exclusion event list from client ...");
3262 exclusion
->count
= count
;
3263 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3264 count
* LTTNG_SYMBOL_NAME_LEN
);
3266 DBG("Nothing recv() from client var len data... continuing");
3269 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3274 /* Get filter expression from client. */
3275 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3276 size_t expression_len
=
3277 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3279 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3280 ret
= LTTNG_ERR_FILTER_INVAL
;
3285 filter_expression
= zmalloc(expression_len
);
3286 if (!filter_expression
) {
3288 ret
= LTTNG_ERR_FILTER_NOMEM
;
3292 /* Receive var. len. data */
3293 DBG("Receiving var len filter's expression from client ...");
3294 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3297 DBG("Nothing recv() from client car len data... continuing");
3299 free(filter_expression
);
3301 ret
= LTTNG_ERR_FILTER_INVAL
;
3306 /* Handle filter and get bytecode from client. */
3307 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3308 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3310 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3311 ret
= LTTNG_ERR_FILTER_INVAL
;
3312 free(filter_expression
);
3317 bytecode
= zmalloc(bytecode_len
);
3319 free(filter_expression
);
3321 ret
= LTTNG_ERR_FILTER_NOMEM
;
3325 /* Receive var. len. data */
3326 DBG("Receiving var len filter's bytecode from client ...");
3327 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3329 DBG("Nothing recv() from client car len data... continuing");
3331 free(filter_expression
);
3334 ret
= LTTNG_ERR_FILTER_INVAL
;
3338 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3339 free(filter_expression
);
3342 ret
= LTTNG_ERR_FILTER_INVAL
;
3347 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3348 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3349 &cmd_ctx
->lsm
->u
.enable
.event
,
3350 filter_expression
, bytecode
, exclusion
,
3351 kernel_poll_pipe
[1]);
3354 case LTTNG_LIST_TRACEPOINTS
:
3356 struct lttng_event
*events
;
3359 session_lock_list();
3360 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3361 session_unlock_list();
3362 if (nb_events
< 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
, sizeof(struct lttng_event
) * nb_events
);
3378 /* Copy event list into message payload */
3379 memcpy(cmd_ctx
->llm
->payload
, events
,
3380 sizeof(struct lttng_event
) * nb_events
);
3387 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3389 struct lttng_event_field
*fields
;
3392 session_lock_list();
3393 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3395 session_unlock_list();
3396 if (nb_fields
< 0) {
3397 /* Return value is a negative lttng_error_code. */
3403 * Setup lttng message with payload size set to the event list size in
3404 * bytes and then copy list into the llm payload.
3406 ret
= setup_lttng_msg(cmd_ctx
,
3407 sizeof(struct lttng_event_field
) * nb_fields
);
3413 /* Copy event list into message payload */
3414 memcpy(cmd_ctx
->llm
->payload
, fields
,
3415 sizeof(struct lttng_event_field
) * nb_fields
);
3422 case LTTNG_LIST_SYSCALLS
:
3424 struct lttng_event
*events
;
3427 nb_events
= cmd_list_syscalls(&events
);
3428 if (nb_events
< 0) {
3429 /* Return value is a negative lttng_error_code. */
3435 * Setup lttng message with payload size set to the event list size in
3436 * bytes and then copy list into the llm payload.
3438 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
3444 /* Copy event list into message payload */
3445 memcpy(cmd_ctx
->llm
->payload
, events
,
3446 sizeof(struct lttng_event
) * nb_events
);
3453 case LTTNG_SET_CONSUMER_URI
:
3456 struct lttng_uri
*uris
;
3458 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3459 len
= nb_uri
* sizeof(struct lttng_uri
);
3462 ret
= LTTNG_ERR_INVALID
;
3466 uris
= zmalloc(len
);
3468 ret
= LTTNG_ERR_FATAL
;
3472 /* Receive variable len data */
3473 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3474 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3476 DBG("No URIs received from client... continuing");
3478 ret
= LTTNG_ERR_SESSION_FAIL
;
3483 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3485 if (ret
!= LTTNG_OK
) {
3492 case LTTNG_START_TRACE
:
3494 ret
= cmd_start_trace(cmd_ctx
->session
);
3497 case LTTNG_STOP_TRACE
:
3499 ret
= cmd_stop_trace(cmd_ctx
->session
);
3502 case LTTNG_CREATE_SESSION
:
3505 struct lttng_uri
*uris
= NULL
;
3507 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3508 len
= nb_uri
* sizeof(struct lttng_uri
);
3511 uris
= zmalloc(len
);
3513 ret
= LTTNG_ERR_FATAL
;
3517 /* Receive variable len data */
3518 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3519 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3521 DBG("No URIs received from client... continuing");
3523 ret
= LTTNG_ERR_SESSION_FAIL
;
3528 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3529 DBG("Creating session with ONE network URI is a bad call");
3530 ret
= LTTNG_ERR_SESSION_FAIL
;
3536 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3537 &cmd_ctx
->creds
, 0);
3543 case LTTNG_DESTROY_SESSION
:
3545 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3547 /* Set session to NULL so we do not unlock it after free. */
3548 cmd_ctx
->session
= NULL
;
3551 case LTTNG_LIST_DOMAINS
:
3554 struct lttng_domain
*domains
= NULL
;
3556 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3558 /* Return value is a negative lttng_error_code. */
3563 ret
= setup_lttng_msg(cmd_ctx
, nb_dom
* sizeof(struct lttng_domain
));
3569 /* Copy event list into message payload */
3570 memcpy(cmd_ctx
->llm
->payload
, domains
,
3571 nb_dom
* sizeof(struct lttng_domain
));
3578 case LTTNG_LIST_CHANNELS
:
3581 struct lttng_channel
*channels
= NULL
;
3583 nb_chan
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3584 cmd_ctx
->session
, &channels
);
3586 /* Return value is a negative lttng_error_code. */
3591 ret
= setup_lttng_msg(cmd_ctx
, nb_chan
* sizeof(struct lttng_channel
));
3597 /* Copy event list into message payload */
3598 memcpy(cmd_ctx
->llm
->payload
, channels
,
3599 nb_chan
* sizeof(struct lttng_channel
));
3606 case LTTNG_LIST_EVENTS
:
3609 struct lttng_event
*events
= NULL
;
3611 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
, cmd_ctx
->session
,
3612 cmd_ctx
->lsm
->u
.list
.channel_name
, &events
);
3614 /* Return value is a negative lttng_error_code. */
3619 ret
= setup_lttng_msg(cmd_ctx
, nb_event
* sizeof(struct lttng_event
));
3625 /* Copy event list into message payload */
3626 memcpy(cmd_ctx
->llm
->payload
, events
,
3627 nb_event
* sizeof(struct lttng_event
));
3634 case LTTNG_LIST_SESSIONS
:
3636 unsigned int nr_sessions
;
3638 session_lock_list();
3639 nr_sessions
= lttng_sessions_count(
3640 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3641 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3643 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * nr_sessions
);
3645 session_unlock_list();
3649 /* Filled the session array */
3650 cmd_list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
),
3651 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3652 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3654 session_unlock_list();
3659 case LTTNG_CALIBRATE
:
3661 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3662 &cmd_ctx
->lsm
->u
.calibrate
);
3665 case LTTNG_REGISTER_CONSUMER
:
3667 struct consumer_data
*cdata
;
3669 switch (cmd_ctx
->lsm
->domain
.type
) {
3670 case LTTNG_DOMAIN_KERNEL
:
3671 cdata
= &kconsumer_data
;
3674 ret
= LTTNG_ERR_UND
;
3678 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3679 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3682 case LTTNG_DATA_PENDING
:
3684 ret
= cmd_data_pending(cmd_ctx
->session
);
3687 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3689 struct lttcomm_lttng_output_id reply
;
3691 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
3692 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
3693 if (ret
!= LTTNG_OK
) {
3697 ret
= setup_lttng_msg(cmd_ctx
, sizeof(reply
));
3702 /* Copy output list into message payload */
3703 memcpy(cmd_ctx
->llm
->payload
, &reply
, sizeof(reply
));
3707 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3709 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
3710 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
3713 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3716 struct lttng_snapshot_output
*outputs
= NULL
;
3718 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
3719 if (nb_output
< 0) {
3724 ret
= setup_lttng_msg(cmd_ctx
,
3725 nb_output
* sizeof(struct lttng_snapshot_output
));
3732 /* Copy output list into message payload */
3733 memcpy(cmd_ctx
->llm
->payload
, outputs
,
3734 nb_output
* sizeof(struct lttng_snapshot_output
));
3741 case LTTNG_SNAPSHOT_RECORD
:
3743 ret
= cmd_snapshot_record(cmd_ctx
->session
,
3744 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
3745 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
3748 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3751 struct lttng_uri
*uris
= NULL
;
3753 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3754 len
= nb_uri
* sizeof(struct lttng_uri
);
3757 uris
= zmalloc(len
);
3759 ret
= LTTNG_ERR_FATAL
;
3763 /* Receive variable len data */
3764 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3765 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3767 DBG("No URIs received from client... continuing");
3769 ret
= LTTNG_ERR_SESSION_FAIL
;
3774 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3775 DBG("Creating session with ONE network URI is a bad call");
3776 ret
= LTTNG_ERR_SESSION_FAIL
;
3782 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
3783 nb_uri
, &cmd_ctx
->creds
);
3787 case LTTNG_CREATE_SESSION_LIVE
:
3790 struct lttng_uri
*uris
= NULL
;
3792 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3793 len
= nb_uri
* sizeof(struct lttng_uri
);
3796 uris
= zmalloc(len
);
3798 ret
= LTTNG_ERR_FATAL
;
3802 /* Receive variable len data */
3803 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3804 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3806 DBG("No URIs received from client... continuing");
3808 ret
= LTTNG_ERR_SESSION_FAIL
;
3813 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3814 DBG("Creating session with ONE network URI is a bad call");
3815 ret
= LTTNG_ERR_SESSION_FAIL
;
3821 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
3822 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
3826 case LTTNG_SAVE_SESSION
:
3828 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
3833 ret
= LTTNG_ERR_UND
;
3838 if (cmd_ctx
->llm
== NULL
) {
3839 DBG("Missing llm structure. Allocating one.");
3840 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
3844 /* Set return code */
3845 cmd_ctx
->llm
->ret_code
= ret
;
3847 if (cmd_ctx
->session
) {
3848 session_unlock(cmd_ctx
->session
);
3850 if (need_tracing_session
) {
3851 session_unlock_list();
3858 * Thread managing health check socket.
3860 static void *thread_manage_health(void *data
)
3862 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
3863 uint32_t revents
, nb_fd
;
3864 struct lttng_poll_event events
;
3865 struct health_comm_msg msg
;
3866 struct health_comm_reply reply
;
3868 DBG("[thread] Manage health check started");
3870 rcu_register_thread();
3872 /* We might hit an error path before this is created. */
3873 lttng_poll_init(&events
);
3875 /* Create unix socket */
3876 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
3878 ERR("Unable to create health check Unix socket");
3884 /* lttng health client socket path permissions */
3885 ret
= chown(health_unix_sock_path
, 0,
3886 utils_get_group_id(tracing_group_name
));
3888 ERR("Unable to set group on %s", health_unix_sock_path
);
3894 ret
= chmod(health_unix_sock_path
,
3895 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3897 ERR("Unable to set permissions on %s", health_unix_sock_path
);
3905 * Set the CLOEXEC flag. Return code is useless because either way, the
3908 (void) utils_set_fd_cloexec(sock
);
3910 ret
= lttcomm_listen_unix_sock(sock
);
3916 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3917 * more will be added to this poll set.
3919 ret
= sessiond_set_thread_pollset(&events
, 2);
3924 /* Add the application registration socket */
3925 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
3930 sessiond_notify_ready();
3933 DBG("Health check ready");
3935 /* Inifinite blocking call, waiting for transmission */
3937 ret
= lttng_poll_wait(&events
, -1);
3940 * Restart interrupted system call.
3942 if (errno
== EINTR
) {
3950 for (i
= 0; i
< nb_fd
; i
++) {
3951 /* Fetch once the poll data */
3952 revents
= LTTNG_POLL_GETEV(&events
, i
);
3953 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3956 /* No activity for this FD (poll implementation). */
3960 /* Thread quit pipe has been closed. Killing thread. */
3961 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
3967 /* Event on the registration socket */
3968 if (pollfd
== sock
) {
3969 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
3970 ERR("Health socket poll error");
3976 new_sock
= lttcomm_accept_unix_sock(sock
);
3982 * Set the CLOEXEC flag. Return code is useless because either way, the
3985 (void) utils_set_fd_cloexec(new_sock
);
3987 DBG("Receiving data from client for health...");
3988 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
3990 DBG("Nothing recv() from client... continuing");
3991 ret
= close(new_sock
);
3999 rcu_thread_online();
4001 memset(&reply
, 0, sizeof(reply
));
4002 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4004 * health_check_state returns 0 if health is
4007 if (!health_check_state(health_sessiond
, i
)) {
4008 reply
.ret_code
|= 1ULL << i
;
4012 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4014 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4016 ERR("Failed to send health data back to client");
4019 /* End of transmission */
4020 ret
= close(new_sock
);
4030 ERR("Health error occurred in %s", __func__
);
4032 DBG("Health check thread dying");
4033 unlink(health_unix_sock_path
);
4041 lttng_poll_clean(&events
);
4043 rcu_unregister_thread();
4048 * This thread manage all clients request using the unix client socket for
4051 static void *thread_manage_clients(void *data
)
4053 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4055 uint32_t revents
, nb_fd
;
4056 struct command_ctx
*cmd_ctx
= NULL
;
4057 struct lttng_poll_event events
;
4059 DBG("[thread] Manage client started");
4061 rcu_register_thread();
4063 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4065 health_code_update();
4067 ret
= lttcomm_listen_unix_sock(client_sock
);
4073 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4074 * more will be added to this poll set.
4076 ret
= sessiond_set_thread_pollset(&events
, 2);
4078 goto error_create_poll
;
4081 /* Add the application registration socket */
4082 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4087 sessiond_notify_ready();
4088 ret
= sem_post(&load_info
->message_thread_ready
);
4090 PERROR("sem_post message_thread_ready");
4094 /* This testpoint is after we signal readiness to the parent. */
4095 if (testpoint(sessiond_thread_manage_clients
)) {
4099 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4103 health_code_update();
4106 DBG("Accepting client command ...");
4108 /* Inifinite blocking call, waiting for transmission */
4110 health_poll_entry();
4111 ret
= lttng_poll_wait(&events
, -1);
4115 * Restart interrupted system call.
4117 if (errno
== EINTR
) {
4125 for (i
= 0; i
< nb_fd
; i
++) {
4126 /* Fetch once the poll data */
4127 revents
= LTTNG_POLL_GETEV(&events
, i
);
4128 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4130 health_code_update();
4133 /* No activity for this FD (poll implementation). */
4137 /* Thread quit pipe has been closed. Killing thread. */
4138 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4144 /* Event on the registration socket */
4145 if (pollfd
== client_sock
) {
4146 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4147 ERR("Client socket poll error");
4153 DBG("Wait for client response");
4155 health_code_update();
4157 sock
= lttcomm_accept_unix_sock(client_sock
);
4163 * Set the CLOEXEC flag. Return code is useless because either way, the
4166 (void) utils_set_fd_cloexec(sock
);
4168 /* Set socket option for credentials retrieval */
4169 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4174 /* Allocate context command to process the client request */
4175 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4176 if (cmd_ctx
== NULL
) {
4177 PERROR("zmalloc cmd_ctx");
4181 /* Allocate data buffer for reception */
4182 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4183 if (cmd_ctx
->lsm
== NULL
) {
4184 PERROR("zmalloc cmd_ctx->lsm");
4188 cmd_ctx
->llm
= NULL
;
4189 cmd_ctx
->session
= NULL
;
4191 health_code_update();
4194 * Data is received from the lttng client. The struct
4195 * lttcomm_session_msg (lsm) contains the command and data request of
4198 DBG("Receiving data from client ...");
4199 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4200 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4202 DBG("Nothing recv() from client... continuing");
4208 clean_command_ctx(&cmd_ctx
);
4212 health_code_update();
4214 // TODO: Validate cmd_ctx including sanity check for
4215 // security purpose.
4217 rcu_thread_online();
4219 * This function dispatch the work to the kernel or userspace tracer
4220 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4221 * informations for the client. The command context struct contains
4222 * everything this function may needs.
4224 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4225 rcu_thread_offline();
4233 * TODO: Inform client somehow of the fatal error. At
4234 * this point, ret < 0 means that a zmalloc failed
4235 * (ENOMEM). Error detected but still accept
4236 * command, unless a socket error has been
4239 clean_command_ctx(&cmd_ctx
);
4243 health_code_update();
4245 DBG("Sending response (size: %d, retcode: %s)",
4246 cmd_ctx
->lttng_msg_size
,
4247 lttng_strerror(-cmd_ctx
->llm
->ret_code
));
4248 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4250 ERR("Failed to send data back to client");
4253 /* End of transmission */
4260 clean_command_ctx(&cmd_ctx
);
4262 health_code_update();
4274 lttng_poll_clean(&events
);
4275 clean_command_ctx(&cmd_ctx
);
4279 unlink(client_unix_sock_path
);
4280 if (client_sock
>= 0) {
4281 ret
= close(client_sock
);
4289 ERR("Health error occurred in %s", __func__
);
4292 health_unregister(health_sessiond
);
4294 DBG("Client thread dying");
4296 rcu_unregister_thread();
4299 * Since we are creating the consumer threads, we own them, so we need
4300 * to join them before our thread exits.
4302 ret
= join_consumer_thread(&kconsumer_data
);
4305 PERROR("join_consumer");
4308 ret
= join_consumer_thread(&ustconsumer32_data
);
4311 PERROR("join_consumer ust32");
4314 ret
= join_consumer_thread(&ustconsumer64_data
);
4317 PERROR("join_consumer ust64");
4324 * usage function on stderr
4326 static void usage(void)
4328 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
4329 fprintf(stderr
, " -h, --help Display this usage.\n");
4330 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
4331 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4332 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4333 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4334 fprintf(stderr
, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4335 fprintf(stderr
, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4336 fprintf(stderr
, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4337 fprintf(stderr
, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4338 fprintf(stderr
, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4339 fprintf(stderr
, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4340 fprintf(stderr
, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4341 fprintf(stderr
, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4342 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
4343 fprintf(stderr
, " -b, --background Start as a daemon, keeping console open.\n");
4344 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4345 fprintf(stderr
, " -V, --version Show version number.\n");
4346 fprintf(stderr
, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4347 fprintf(stderr
, " -q, --quiet No output at all.\n");
4348 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4349 fprintf(stderr
, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4350 fprintf(stderr
, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4351 fprintf(stderr
, " --no-kernel Disable kernel tracer\n");
4352 fprintf(stderr
, " --agent-tcp-port Agent registration TCP port\n");
4353 fprintf(stderr
, " -f --config PATH Load daemon configuration file\n");
4354 fprintf(stderr
, " -l --load PATH Load session configuration\n");
4355 fprintf(stderr
, " --kmod-probes Specify kernel module probes to load\n");
4356 fprintf(stderr
, " --extra-kmod-probes Specify extra kernel module probes to load\n");
4360 * Take an option from the getopt output and set it in the right variable to be
4363 * Return 0 on success else a negative value.
4365 static int set_option(int opt
, const char *arg
, const char *optname
)
4369 if (arg
&& arg
[0] == '\0') {
4371 * This only happens if the value is read from daemon config
4372 * file. This means the option requires an argument and the
4373 * configuration file contains a line such as:
4382 fprintf(stderr
, "option %s", optname
);
4384 fprintf(stderr
, " with arg %s\n", arg
);
4388 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4391 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4401 * If the override option is set, the pointer points to a
4402 * *non* const thus freeing it even though the variable type is
4405 if (tracing_group_name_override
) {
4406 free((void *) tracing_group_name
);
4408 tracing_group_name
= strdup(arg
);
4409 if (!tracing_group_name
) {
4413 tracing_group_name_override
= 1;
4419 fprintf(stdout
, "%s\n", VERSION
);
4425 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4428 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4431 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4434 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4437 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4440 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4446 lttng_opt_quiet
= 1;
4449 /* Verbose level can increase using multiple -v */
4451 /* Value obtained from config file */
4452 lttng_opt_verbose
= config_parse_value(arg
);
4454 /* -v used on command line */
4455 lttng_opt_verbose
++;
4457 /* Clamp value to [0, 3] */
4458 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4459 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4463 opt_verbose_consumer
= config_parse_value(arg
);
4465 opt_verbose_consumer
+= 1;
4469 if (consumerd32_bin_override
) {
4470 free((void *) consumerd32_bin
);
4472 consumerd32_bin
= strdup(arg
);
4473 if (!consumerd32_bin
) {
4477 consumerd32_bin_override
= 1;
4480 if (consumerd32_libdir_override
) {
4481 free((void *) consumerd32_libdir
);
4483 consumerd32_libdir
= strdup(arg
);
4484 if (!consumerd32_libdir
) {
4488 consumerd32_libdir_override
= 1;
4491 if (consumerd64_bin_override
) {
4492 free((void *) consumerd64_bin
);
4494 consumerd64_bin
= strdup(arg
);
4495 if (!consumerd64_bin
) {
4499 consumerd64_bin_override
= 1;
4502 if (consumerd64_libdir_override
) {
4503 free((void *) consumerd64_libdir
);
4505 consumerd64_libdir
= strdup(arg
);
4506 if (!consumerd64_libdir
) {
4510 consumerd64_libdir_override
= 1;
4514 opt_pidfile
= strdup(arg
);
4520 case 'J': /* Agent TCP port. */
4525 v
= strtoul(arg
, NULL
, 0);
4526 if (errno
!= 0 || !isdigit(arg
[0])) {
4527 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4530 if (v
== 0 || v
>= 65535) {
4531 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4534 agent_tcp_port
= (uint32_t) v
;
4535 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
4539 free(opt_load_session_path
);
4540 opt_load_session_path
= strdup(arg
);
4541 if (!opt_load_session_path
) {
4546 case 'P': /* probe modules list */
4547 free(kmod_probes_list
);
4548 kmod_probes_list
= strdup(arg
);
4549 if (!kmod_probes_list
) {
4555 free(kmod_extra_probes_list
);
4556 kmod_extra_probes_list
= strdup(arg
);
4557 if (!kmod_extra_probes_list
) {
4563 /* This is handled in set_options() thus silent break. */
4566 /* Unknown option or other error.
4567 * Error is printed by getopt, just return */
4572 if (ret
== -EINVAL
) {
4573 const char *opt_name
= "unknown";
4576 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
4578 if (opt
== long_options
[i
].val
) {
4579 opt_name
= long_options
[i
].name
;
4584 WARN("Invalid argument provided for option \"%s\", using default value.",
4592 * config_entry_handler_cb used to handle options read from a config file.
4593 * See config_entry_handler_cb comment in common/config/config.h for the
4594 * return value conventions.
4596 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
4600 if (!entry
|| !entry
->name
|| !entry
->value
) {
4605 /* Check if the option is to be ignored */
4606 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
4607 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
4612 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
4615 /* Ignore if not fully matched. */
4616 if (strcmp(entry
->name
, long_options
[i
].name
)) {
4621 * If the option takes no argument on the command line, we have to
4622 * check if the value is "true". We support non-zero numeric values,
4625 if (!long_options
[i
].has_arg
) {
4626 ret
= config_parse_value(entry
->value
);
4629 WARN("Invalid configuration value \"%s\" for option %s",
4630 entry
->value
, entry
->name
);
4632 /* False, skip boolean config option. */
4637 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
4641 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
4648 * daemon configuration loading and argument parsing
4650 static int set_options(int argc
, char **argv
)
4652 int ret
= 0, c
= 0, option_index
= 0;
4653 int orig_optopt
= optopt
, orig_optind
= optind
;
4655 const char *config_path
= NULL
;
4657 optstring
= utils_generate_optstring(long_options
,
4658 sizeof(long_options
) / sizeof(struct option
));
4664 /* Check for the --config option */
4665 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
4666 &option_index
)) != -1) {
4670 } else if (c
!= 'f') {
4671 /* if not equal to --config option. */
4675 config_path
= utils_expand_path(optarg
);
4677 ERR("Failed to resolve path: %s", optarg
);
4681 ret
= config_get_section_entries(config_path
, config_section_name
,
4682 config_entry_handler
, NULL
);
4685 ERR("Invalid configuration option at line %i", ret
);
4691 /* Reset getopt's global state */
4692 optopt
= orig_optopt
;
4693 optind
= orig_optind
;
4695 c
= getopt_long(argc
, argv
, optstring
, long_options
, &option_index
);
4700 ret
= set_option(c
, optarg
, long_options
[option_index
].name
);
4712 * Creates the two needed socket by the daemon.
4713 * apps_sock - The communication socket for all UST apps.
4714 * client_sock - The communication of the cli tool (lttng).
4716 static int init_daemon_socket(void)
4721 old_umask
= umask(0);
4723 /* Create client tool unix socket */
4724 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
4725 if (client_sock
< 0) {
4726 ERR("Create unix sock failed: %s", client_unix_sock_path
);
4731 /* Set the cloexec flag */
4732 ret
= utils_set_fd_cloexec(client_sock
);
4734 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4735 "Continuing but note that the consumer daemon will have a "
4736 "reference to this socket on exec()", client_sock
);
4739 /* File permission MUST be 660 */
4740 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4742 ERR("Set file permissions failed: %s", client_unix_sock_path
);
4747 /* Create the application unix socket */
4748 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
4749 if (apps_sock
< 0) {
4750 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
4755 /* Set the cloexec flag */
4756 ret
= utils_set_fd_cloexec(apps_sock
);
4758 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4759 "Continuing but note that the consumer daemon will have a "
4760 "reference to this socket on exec()", apps_sock
);
4763 /* File permission MUST be 666 */
4764 ret
= chmod(apps_unix_sock_path
,
4765 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
4767 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
4772 DBG3("Session daemon client socket %d and application socket %d created",
4773 client_sock
, apps_sock
);
4781 * Check if the global socket is available, and if a daemon is answering at the
4782 * other side. If yes, error is returned.
4784 static int check_existing_daemon(void)
4786 /* Is there anybody out there ? */
4787 if (lttng_session_daemon_alive()) {
4795 * Set the tracing group gid onto the client socket.
4797 * Race window between mkdir and chown is OK because we are going from more
4798 * permissive (root.root) to less permissive (root.tracing).
4800 static int set_permissions(char *rundir
)
4805 gid
= utils_get_group_id(tracing_group_name
);
4807 /* Set lttng run dir */
4808 ret
= chown(rundir
, 0, gid
);
4810 ERR("Unable to set group on %s", rundir
);
4815 * Ensure all applications and tracing group can search the run
4816 * dir. Allow everyone to read the directory, since it does not
4817 * buy us anything to hide its content.
4819 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
4821 ERR("Unable to set permissions on %s", rundir
);
4825 /* lttng client socket path */
4826 ret
= chown(client_unix_sock_path
, 0, gid
);
4828 ERR("Unable to set group on %s", client_unix_sock_path
);
4832 /* kconsumer error socket path */
4833 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
4835 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
4839 /* 64-bit ustconsumer error socket path */
4840 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
4842 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
4846 /* 32-bit ustconsumer compat32 error socket path */
4847 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
4849 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
4853 DBG("All permissions are set");
4859 * Create the lttng run directory needed for all global sockets and pipe.
4861 static int create_lttng_rundir(const char *rundir
)
4865 DBG3("Creating LTTng run directory: %s", rundir
);
4867 ret
= mkdir(rundir
, S_IRWXU
);
4869 if (errno
!= EEXIST
) {
4870 ERR("Unable to create %s", rundir
);
4882 * Setup sockets and directory needed by the kconsumerd communication with the
4885 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
4889 char path
[PATH_MAX
];
4891 switch (consumer_data
->type
) {
4892 case LTTNG_CONSUMER_KERNEL
:
4893 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
4895 case LTTNG_CONSUMER64_UST
:
4896 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
4898 case LTTNG_CONSUMER32_UST
:
4899 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
4902 ERR("Consumer type unknown");
4907 DBG2("Creating consumer directory: %s", path
);
4909 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
4911 if (errno
!= EEXIST
) {
4913 ERR("Failed to create %s", path
);
4919 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
4921 ERR("Unable to set group on %s", path
);
4927 /* Create the kconsumerd error unix socket */
4928 consumer_data
->err_sock
=
4929 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
4930 if (consumer_data
->err_sock
< 0) {
4931 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
4937 * Set the CLOEXEC flag. Return code is useless because either way, the
4940 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
4942 PERROR("utils_set_fd_cloexec");
4943 /* continue anyway */
4946 /* File permission MUST be 660 */
4947 ret
= chmod(consumer_data
->err_unix_sock_path
,
4948 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4950 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
4960 * Signal handler for the daemon
4962 * Simply stop all worker threads, leaving main() return gracefully after
4963 * joining all threads and calling cleanup().
4965 static void sighandler(int sig
)
4969 DBG("SIGPIPE caught");
4972 DBG("SIGINT caught");
4976 DBG("SIGTERM caught");
4980 CMM_STORE_SHARED(recv_child_signal
, 1);
4988 * Setup signal handler for :
4989 * SIGINT, SIGTERM, SIGPIPE
4991 static int set_signal_handler(void)
4994 struct sigaction sa
;
4997 if ((ret
= sigemptyset(&sigset
)) < 0) {
4998 PERROR("sigemptyset");
5002 sa
.sa_handler
= sighandler
;
5003 sa
.sa_mask
= sigset
;
5005 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5006 PERROR("sigaction");
5010 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5011 PERROR("sigaction");
5015 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5016 PERROR("sigaction");
5020 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5021 PERROR("sigaction");
5025 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5031 * Set open files limit to unlimited. This daemon can open a large number of
5032 * file descriptors in order to consumer multiple kernel traces.
5034 static void set_ulimit(void)
5039 /* The kernel does not allowed an infinite limit for open files */
5040 lim
.rlim_cur
= 65535;
5041 lim
.rlim_max
= 65535;
5043 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5045 PERROR("failed to set open files limit");
5050 * Write pidfile using the rundir and opt_pidfile.
5052 static int write_pidfile(void)
5055 char pidfile_path
[PATH_MAX
];
5060 strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
));
5062 /* Build pidfile path from rundir and opt_pidfile. */
5063 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
5064 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
5066 PERROR("snprintf pidfile path");
5072 * Create pid file in rundir.
5074 ret
= utils_create_pid_file(getpid(), pidfile_path
);
5080 * Create lockfile using the rundir and return its fd.
5082 static int create_lockfile(void)
5085 char lockfile_path
[PATH_MAX
];
5087 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
5092 ret
= utils_create_lock_file(lockfile_path
);
5098 * Write agent TCP port using the rundir.
5100 static int write_agent_port(void)
5103 char path
[PATH_MAX
];
5107 ret
= snprintf(path
, sizeof(path
), "%s/"
5108 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
5110 PERROR("snprintf agent port path");
5115 * Create TCP agent port file in rundir.
5117 ret
= utils_create_pid_file(agent_tcp_port
, path
);
5126 int main(int argc
, char **argv
)
5128 int ret
= 0, retval
= 0;
5130 const char *home_path
, *env_app_timeout
;
5132 init_kernel_workarounds();
5134 rcu_register_thread();
5136 if (set_signal_handler()) {
5138 goto exit_set_signal_handler
;
5141 setup_consumerd_path();
5143 page_size
= sysconf(_SC_PAGESIZE
);
5144 if (page_size
< 0) {
5145 PERROR("sysconf _SC_PAGESIZE");
5146 page_size
= LONG_MAX
;
5147 WARN("Fallback page size to %ld", page_size
);
5151 * Parse arguments and load the daemon configuration file.
5153 * We have an exit_options exit path to free memory reserved by
5154 * set_options. This is needed because the rest of sessiond_cleanup()
5155 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5156 * depends on set_options.
5159 if (set_options(argc
, argv
)) {
5165 if (opt_daemon
|| opt_background
) {
5168 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5176 * We are in the child. Make sure all other file descriptors are
5177 * closed, in case we are called with more opened file
5178 * descriptors than the standard ones.
5180 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5186 * Starting from here, we can create threads. This needs to be after
5187 * lttng_daemonize due to RCU.
5191 * Initialize the health check subsystem. This call should set the
5192 * appropriate time values.
5194 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5195 if (!health_sessiond
) {
5196 PERROR("health_app_create error");
5198 goto exit_health_sessiond_cleanup
;
5201 if (init_ht_cleanup_quit_pipe()) {
5203 goto exit_ht_cleanup_quit_pipe
;
5206 /* Setup the thread ht_cleanup communication pipe. */
5207 if (utils_create_pipe_cloexec(ht_cleanup_pipe
)) {
5209 goto exit_ht_cleanup_pipe
;
5212 /* Set up max poll set size */
5213 if (lttng_poll_set_max_size()) {
5215 goto exit_set_max_size
;
5218 /* Create thread to clean up RCU hash tables */
5219 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5220 thread_ht_cleanup
, (void *) NULL
);
5223 PERROR("pthread_create ht_cleanup");
5225 goto exit_ht_cleanup
;
5228 /* Create thread quit pipe */
5229 if (init_thread_quit_pipe()) {
5231 goto exit_init_data
;
5234 /* Check if daemon is UID = 0 */
5235 is_root
= !getuid();
5238 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5241 goto exit_init_data
;
5244 /* Create global run dir with root access */
5245 if (create_lttng_rundir(rundir
)) {
5247 goto exit_init_data
;
5250 if (strlen(apps_unix_sock_path
) == 0) {
5251 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5252 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5255 goto exit_init_data
;
5259 if (strlen(client_unix_sock_path
) == 0) {
5260 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5261 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5264 goto exit_init_data
;
5268 /* Set global SHM for ust */
5269 if (strlen(wait_shm_path
) == 0) {
5270 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5271 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5274 goto exit_init_data
;
5278 if (strlen(health_unix_sock_path
) == 0) {
5279 ret
= snprintf(health_unix_sock_path
,
5280 sizeof(health_unix_sock_path
),
5281 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5284 goto exit_init_data
;
5288 /* Setup kernel consumerd path */
5289 ret
= snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5290 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5293 goto exit_init_data
;
5295 ret
= snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5296 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5299 goto exit_init_data
;
5302 DBG2("Kernel consumer err path: %s",
5303 kconsumer_data
.err_unix_sock_path
);
5304 DBG2("Kernel consumer cmd path: %s",
5305 kconsumer_data
.cmd_unix_sock_path
);
5307 home_path
= utils_get_home_dir();
5308 if (home_path
== NULL
) {
5309 /* TODO: Add --socket PATH option */
5310 ERR("Can't get HOME directory for sockets creation.");
5312 goto exit_init_data
;
5316 * Create rundir from home path. This will create something like
5319 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5322 goto exit_init_data
;
5325 if (create_lttng_rundir(rundir
)) {
5327 goto exit_init_data
;
5330 if (strlen(apps_unix_sock_path
) == 0) {
5331 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5332 DEFAULT_HOME_APPS_UNIX_SOCK
,
5336 goto exit_init_data
;
5340 /* Set the cli tool unix socket path */
5341 if (strlen(client_unix_sock_path
) == 0) {
5342 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5343 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
5347 goto exit_init_data
;
5351 /* Set global SHM for ust */
5352 if (strlen(wait_shm_path
) == 0) {
5353 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5354 DEFAULT_HOME_APPS_WAIT_SHM_PATH
,
5358 goto exit_init_data
;
5362 /* Set health check Unix path */
5363 if (strlen(health_unix_sock_path
) == 0) {
5364 ret
= snprintf(health_unix_sock_path
,
5365 sizeof(health_unix_sock_path
),
5366 DEFAULT_HOME_HEALTH_UNIX_SOCK
,
5370 goto exit_init_data
;
5375 lockfile_fd
= create_lockfile();
5376 if (lockfile_fd
< 0) {
5378 goto exit_init_data
;
5381 /* Set consumer initial state */
5382 kernel_consumerd_state
= CONSUMER_STOPPED
;
5383 ust_consumerd_state
= CONSUMER_STOPPED
;
5385 DBG("Client socket path %s", client_unix_sock_path
);
5386 DBG("Application socket path %s", apps_unix_sock_path
);
5387 DBG("Application wait path %s", wait_shm_path
);
5388 DBG("LTTng run directory path: %s", rundir
);
5390 /* 32 bits consumerd path setup */
5391 ret
= snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5392 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5394 PERROR("snprintf 32-bit consumer error socket path");
5396 goto exit_init_data
;
5398 ret
= snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5399 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5401 PERROR("snprintf 32-bit consumer command socket path");
5403 goto exit_init_data
;
5406 DBG2("UST consumer 32 bits err path: %s",
5407 ustconsumer32_data
.err_unix_sock_path
);
5408 DBG2("UST consumer 32 bits cmd path: %s",
5409 ustconsumer32_data
.cmd_unix_sock_path
);
5411 /* 64 bits consumerd path setup */
5412 ret
= snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5413 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5415 PERROR("snprintf 64-bit consumer error socket path");
5417 goto exit_init_data
;
5419 ret
= snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5420 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5422 PERROR("snprintf 64-bit consumer command socket path");
5424 goto exit_init_data
;
5427 DBG2("UST consumer 64 bits err path: %s",
5428 ustconsumer64_data
.err_unix_sock_path
);
5429 DBG2("UST consumer 64 bits cmd path: %s",
5430 ustconsumer64_data
.cmd_unix_sock_path
);
5433 * See if daemon already exist.
5435 if (check_existing_daemon()) {
5436 ERR("Already running daemon.\n");
5438 * We do not goto exit because we must not cleanup()
5439 * because a daemon is already running.
5442 goto exit_init_data
;
5446 * Init UST app hash table. Alloc hash table before this point since
5447 * cleanup() can get called after that point.
5449 if (ust_app_ht_alloc()) {
5451 goto exit_init_data
;
5454 /* Initialize agent domain subsystem. */
5455 if (agent_setup()) {
5456 /* ENOMEM at this point. */
5458 goto exit_init_data
;
5462 * These actions must be executed as root. We do that *after* setting up
5463 * the sockets path because we MUST make the check for another daemon using
5464 * those paths *before* trying to set the kernel consumer sockets and init
5468 if (set_consumer_sockets(&kconsumer_data
, rundir
)) {
5470 goto exit_init_data
;
5473 /* Setup kernel tracer */
5474 if (!opt_no_kernel
) {
5475 init_kernel_tracer();
5476 if (kernel_tracer_fd
>= 0) {
5477 ret
= syscall_init_table();
5479 ERR("Unable to populate syscall table. "
5480 "Syscall tracing won't work "
5481 "for this session daemon.");
5486 /* Set ulimit for open files */
5489 /* init lttng_fd tracking must be done after set_ulimit. */
5492 if (set_consumer_sockets(&ustconsumer64_data
, rundir
)) {
5494 goto exit_init_data
;
5497 if (set_consumer_sockets(&ustconsumer32_data
, rundir
)) {
5499 goto exit_init_data
;
5502 /* Setup the needed unix socket */
5503 if (init_daemon_socket()) {
5505 goto exit_init_data
;
5508 /* Set credentials to socket */
5509 if (is_root
&& set_permissions(rundir
)) {
5511 goto exit_init_data
;
5514 /* Get parent pid if -S, --sig-parent is specified. */
5515 if (opt_sig_parent
) {
5519 /* Setup the kernel pipe for waking up the kernel thread */
5520 if (is_root
&& !opt_no_kernel
) {
5521 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5523 goto exit_init_data
;
5527 /* Setup the thread apps communication pipe. */
5528 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5530 goto exit_init_data
;
5533 /* Setup the thread apps notify communication pipe. */
5534 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5536 goto exit_init_data
;
5539 /* Initialize global buffer per UID and PID registry. */
5540 buffer_reg_init_uid_registry();
5541 buffer_reg_init_pid_registry();
5543 /* Init UST command queue. */
5544 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
5547 * Get session list pointer. This pointer MUST NOT be free'd. This list
5548 * is statically declared in session.c
5550 session_list_ptr
= session_get_list();
5554 /* Check for the application socket timeout env variable. */
5555 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
5556 if (env_app_timeout
) {
5557 app_socket_timeout
= atoi(env_app_timeout
);
5559 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
5562 ret
= write_pidfile();
5564 ERR("Error in write_pidfile");
5566 goto exit_init_data
;
5568 ret
= write_agent_port();
5570 ERR("Error in write_agent_port");
5572 goto exit_init_data
;
5575 /* Initialize communication library */
5577 /* Initialize TCP timeout values */
5578 lttcomm_inet_init();
5580 if (load_session_init_data(&load_info
) < 0) {
5582 goto exit_init_data
;
5584 load_info
->path
= opt_load_session_path
;
5586 /* Create health-check thread */
5587 ret
= pthread_create(&health_thread
, NULL
,
5588 thread_manage_health
, (void *) NULL
);
5591 PERROR("pthread_create health");
5596 /* Create thread to manage the client socket */
5597 ret
= pthread_create(&client_thread
, NULL
,
5598 thread_manage_clients
, (void *) NULL
);
5601 PERROR("pthread_create clients");
5606 /* Create thread to dispatch registration */
5607 ret
= pthread_create(&dispatch_thread
, NULL
,
5608 thread_dispatch_ust_registration
, (void *) NULL
);
5611 PERROR("pthread_create dispatch");
5616 /* Create thread to manage application registration. */
5617 ret
= pthread_create(®_apps_thread
, NULL
,
5618 thread_registration_apps
, (void *) NULL
);
5621 PERROR("pthread_create registration");
5626 /* Create thread to manage application socket */
5627 ret
= pthread_create(&apps_thread
, NULL
,
5628 thread_manage_apps
, (void *) NULL
);
5631 PERROR("pthread_create apps");
5636 /* Create thread to manage application notify socket */
5637 ret
= pthread_create(&apps_notify_thread
, NULL
,
5638 ust_thread_manage_notify
, (void *) NULL
);
5641 PERROR("pthread_create notify");
5643 goto exit_apps_notify
;
5646 /* Create agent registration thread. */
5647 ret
= pthread_create(&agent_reg_thread
, NULL
,
5648 agent_thread_manage_registration
, (void *) NULL
);
5651 PERROR("pthread_create agent");
5653 goto exit_agent_reg
;
5656 /* Don't start this thread if kernel tracing is not requested nor root */
5657 if (is_root
&& !opt_no_kernel
) {
5658 /* Create kernel thread to manage kernel event */
5659 ret
= pthread_create(&kernel_thread
, NULL
,
5660 thread_manage_kernel
, (void *) NULL
);
5663 PERROR("pthread_create kernel");
5669 /* Create session loading thread. */
5670 ret
= pthread_create(&load_session_thread
, NULL
, thread_load_session
,
5674 PERROR("pthread_create load_session_thread");
5676 goto exit_load_session
;
5680 * This is where we start awaiting program completion (e.g. through
5681 * signal that asks threads to teardown).
5684 ret
= pthread_join(load_session_thread
, &status
);
5687 PERROR("pthread_join load_session_thread");
5692 if (is_root
&& !opt_no_kernel
) {
5693 ret
= pthread_join(kernel_thread
, &status
);
5696 PERROR("pthread_join");
5702 ret
= pthread_join(agent_reg_thread
, &status
);
5705 PERROR("pthread_join agent");
5710 ret
= pthread_join(apps_notify_thread
, &status
);
5713 PERROR("pthread_join apps notify");
5718 ret
= pthread_join(apps_thread
, &status
);
5721 PERROR("pthread_join apps");
5726 ret
= pthread_join(reg_apps_thread
, &status
);
5729 PERROR("pthread_join");
5734 ret
= pthread_join(dispatch_thread
, &status
);
5737 PERROR("pthread_join");
5742 ret
= pthread_join(client_thread
, &status
);
5745 PERROR("pthread_join");
5750 ret
= pthread_join(health_thread
, &status
);
5753 PERROR("pthread_join health thread");
5760 * sessiond_cleanup() is called when no other thread is running, except
5761 * the ht_cleanup thread, which is needed to destroy the hash tables.
5763 rcu_thread_online();
5765 rcu_thread_offline();
5766 rcu_unregister_thread();
5768 ret
= notify_thread_pipe(ht_cleanup_quit_pipe
[1]);
5770 ERR("write error on ht_cleanup quit pipe");
5774 ret
= pthread_join(ht_cleanup_thread
, &status
);
5777 PERROR("pthread_join ht cleanup thread");
5783 utils_close_pipe(ht_cleanup_pipe
);
5784 exit_ht_cleanup_pipe
:
5787 * Close the ht_cleanup quit pipe.
5789 utils_close_pipe(ht_cleanup_quit_pipe
);
5790 exit_ht_cleanup_quit_pipe
:
5792 health_app_destroy(health_sessiond
);
5793 exit_health_sessiond_cleanup
:
5796 sessiond_cleanup_options();
5798 exit_set_signal_handler
: