2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/uatomic.h>
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/compat/getenv.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/session-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"
74 #include "notification-thread.h"
75 #include "notification-thread-commands.h"
76 #include "rotation-thread.h"
79 #include "ht-cleanup.h"
81 #define CONSUMERD_FILE "lttng-consumerd"
83 static const char *help_msg
=
84 #ifdef LTTNG_EMBED_HELP
85 #include <lttng-sessiond.8.h>
92 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
93 static int tracing_group_name_override
;
94 static char *opt_pidfile
;
95 static int opt_sig_parent
;
96 static int opt_verbose_consumer
;
97 static int opt_daemon
, opt_background
;
98 static int opt_no_kernel
;
99 static char *opt_load_session_path
;
100 static pid_t ppid
; /* Parent PID for --sig-parent option */
101 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
103 static int lockfile_fd
= -1;
105 /* Set to 1 when a SIGUSR1 signal is received. */
106 static int recv_child_signal
;
109 * Consumer daemon specific control data. Every value not initialized here is
110 * set to 0 by the static definition.
112 static struct consumer_data kconsumer_data
= {
113 .type
= LTTNG_CONSUMER_KERNEL
,
114 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
115 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
118 .channel_monitor_pipe
= -1,
119 .channel_rotate_pipe
= -1,
120 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
121 .lock
= PTHREAD_MUTEX_INITIALIZER
,
122 .cond
= PTHREAD_COND_INITIALIZER
,
123 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
125 static struct consumer_data ustconsumer64_data
= {
126 .type
= LTTNG_CONSUMER64_UST
,
127 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
128 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
131 .channel_monitor_pipe
= -1,
132 .channel_rotate_pipe
= -1,
133 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
134 .lock
= PTHREAD_MUTEX_INITIALIZER
,
135 .cond
= PTHREAD_COND_INITIALIZER
,
136 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
138 static struct consumer_data ustconsumer32_data
= {
139 .type
= LTTNG_CONSUMER32_UST
,
140 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
141 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
144 .channel_monitor_pipe
= -1,
145 .channel_rotate_pipe
= -1,
146 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
147 .lock
= PTHREAD_MUTEX_INITIALIZER
,
148 .cond
= PTHREAD_COND_INITIALIZER
,
149 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
152 /* Command line options */
153 static const struct option long_options
[] = {
154 { "client-sock", required_argument
, 0, 'c' },
155 { "apps-sock", required_argument
, 0, 'a' },
156 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
157 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
158 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
159 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
160 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
161 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
162 { "consumerd32-path", required_argument
, 0, '\0' },
163 { "consumerd32-libdir", required_argument
, 0, '\0' },
164 { "consumerd64-path", required_argument
, 0, '\0' },
165 { "consumerd64-libdir", required_argument
, 0, '\0' },
166 { "daemonize", no_argument
, 0, 'd' },
167 { "background", no_argument
, 0, 'b' },
168 { "sig-parent", no_argument
, 0, 'S' },
169 { "help", no_argument
, 0, 'h' },
170 { "group", required_argument
, 0, 'g' },
171 { "version", no_argument
, 0, 'V' },
172 { "quiet", no_argument
, 0, 'q' },
173 { "verbose", no_argument
, 0, 'v' },
174 { "verbose-consumer", no_argument
, 0, '\0' },
175 { "no-kernel", no_argument
, 0, '\0' },
176 { "pidfile", required_argument
, 0, 'p' },
177 { "agent-tcp-port", required_argument
, 0, '\0' },
178 { "config", required_argument
, 0, 'f' },
179 { "load", required_argument
, 0, 'l' },
180 { "kmod-probes", required_argument
, 0, '\0' },
181 { "extra-kmod-probes", required_argument
, 0, '\0' },
185 /* Command line options to ignore from configuration file */
186 static const char *config_ignore_options
[] = { "help", "version", "config" };
188 /* Shared between threads */
189 static int dispatch_thread_exit
;
191 /* Global application Unix socket path */
192 static char apps_unix_sock_path
[PATH_MAX
];
193 /* Global client Unix socket path */
194 static char client_unix_sock_path
[PATH_MAX
];
195 /* global wait shm path for UST */
196 static char wait_shm_path
[PATH_MAX
];
197 /* Global health check unix path */
198 static char health_unix_sock_path
[PATH_MAX
];
200 /* Sockets and FDs */
201 static int client_sock
= -1;
202 static int apps_sock
= -1;
203 int kernel_tracer_fd
= -1;
204 static int kernel_poll_pipe
[2] = { -1, -1 };
207 * Quit pipe for all threads. This permits a single cancellation point
208 * for all threads when receiving an event on the pipe.
210 static int thread_quit_pipe
[2] = { -1, -1 };
213 * This pipe is used to inform the thread managing application communication
214 * that a command is queued and ready to be processed.
216 static int apps_cmd_pipe
[2] = { -1, -1 };
218 int apps_cmd_notify_pipe
[2] = { -1, -1 };
220 /* Pthread, Mutexes and Semaphores */
221 static pthread_t apps_thread
;
222 static pthread_t apps_notify_thread
;
223 static pthread_t reg_apps_thread
;
224 static pthread_t client_thread
;
225 static pthread_t kernel_thread
;
226 static pthread_t dispatch_thread
;
227 static pthread_t health_thread
;
228 static pthread_t ht_cleanup_thread
;
229 static pthread_t agent_reg_thread
;
230 static pthread_t load_session_thread
;
231 static pthread_t notification_thread
;
232 static pthread_t rotation_thread
;
235 * UST registration command queue. This queue is tied with a futex and uses a N
236 * wakers / 1 waiter implemented and detailed in futex.c/.h
238 * The thread_registration_apps and thread_dispatch_ust_registration uses this
239 * queue along with the wait/wake scheme. The thread_manage_apps receives down
240 * the line new application socket and monitors it for any I/O error or clean
241 * close that triggers an unregistration of the application.
243 static struct ust_cmd_queue ust_cmd_queue
;
246 * Pointer initialized before thread creation.
248 * This points to the tracing session list containing the session count and a
249 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
250 * MUST NOT be taken if you call a public function in session.c.
252 * The lock is nested inside the structure: session_list_ptr->lock. Please use
253 * session_lock_list and session_unlock_list for lock acquisition.
255 static struct ltt_session_list
*session_list_ptr
;
257 int ust_consumerd64_fd
= -1;
258 int ust_consumerd32_fd
= -1;
260 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
261 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
262 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
263 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
264 static int consumerd32_bin_override
;
265 static int consumerd64_bin_override
;
266 static int consumerd32_libdir_override
;
267 static int consumerd64_libdir_override
;
269 static const char *module_proc_lttng
= "/proc/lttng";
272 * Consumer daemon state which is changed when spawning it, killing it or in
273 * case of a fatal error.
275 enum consumerd_state
{
276 CONSUMER_STARTED
= 1,
277 CONSUMER_STOPPED
= 2,
282 * This consumer daemon state is used to validate if a client command will be
283 * able to reach the consumer. If not, the client is informed. For instance,
284 * doing a "lttng start" when the consumer state is set to ERROR will return an
285 * error to the client.
287 * The following example shows a possible race condition of this scheme:
289 * consumer thread error happens
291 * client cmd checks state -> still OK
292 * consumer thread exit, sets error
293 * client cmd try to talk to consumer
296 * However, since the consumer is a different daemon, we have no way of making
297 * sure the command will reach it safely even with this state flag. This is why
298 * we consider that up to the state validation during command processing, the
299 * command is safe. After that, we can not guarantee the correctness of the
300 * client request vis-a-vis the consumer.
302 static enum consumerd_state ust_consumerd_state
;
303 static enum consumerd_state kernel_consumerd_state
;
306 * Socket timeout for receiving and sending in seconds.
308 static int app_socket_timeout
;
310 /* Set in main() with the current page size. */
313 /* Application health monitoring */
314 struct health_app
*health_sessiond
;
316 /* Agent TCP port for registration. Used by the agent thread. */
317 unsigned int agent_tcp_port
= DEFAULT_AGENT_TCP_PORT
;
319 /* Am I root or not. */
320 int is_root
; /* Set to 1 if the daemon is running as root */
322 const char * const config_section_name
= "sessiond";
324 /* Load session thread information to operate. */
325 struct load_session_thread_data
*load_info
;
327 /* Notification thread handle. */
328 struct notification_thread_handle
*notification_thread_handle
;
330 /* Rotation thread handle. */
331 struct rotation_thread_handle
*rotation_thread_handle
;
333 /* Global hash tables */
334 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
337 * Whether sessiond is ready for commands/notification channel/health check
339 * NR_LTTNG_SESSIOND_READY must match the number of calls to
340 * sessiond_notify_ready().
342 #define NR_LTTNG_SESSIOND_READY 5
343 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
345 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
347 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) ? 1 : 0;
350 /* Notify parents that we are ready for cmd and health check */
352 void sessiond_notify_ready(void)
354 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
356 * Notify parent pid that we are ready to accept command
357 * for client side. This ppid is the one from the
358 * external process that spawned us.
360 if (opt_sig_parent
) {
365 * Notify the parent of the fork() process that we are
368 if (opt_daemon
|| opt_background
) {
369 kill(child_ppid
, SIGUSR1
);
375 void setup_consumerd_path(void)
377 const char *bin
, *libdir
;
380 * Allow INSTALL_BIN_PATH to be used as a target path for the
381 * native architecture size consumer if CONFIG_CONSUMER*_PATH
382 * has not been defined.
384 #if (CAA_BITS_PER_LONG == 32)
385 if (!consumerd32_bin
[0]) {
386 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
388 if (!consumerd32_libdir
[0]) {
389 consumerd32_libdir
= INSTALL_LIB_PATH
;
391 #elif (CAA_BITS_PER_LONG == 64)
392 if (!consumerd64_bin
[0]) {
393 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
395 if (!consumerd64_libdir
[0]) {
396 consumerd64_libdir
= INSTALL_LIB_PATH
;
399 #error "Unknown bitness"
403 * runtime env. var. overrides the build default.
405 bin
= lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
407 consumerd32_bin
= bin
;
409 bin
= lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
411 consumerd64_bin
= bin
;
413 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
415 consumerd32_libdir
= libdir
;
417 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
419 consumerd64_libdir
= libdir
;
424 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
431 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
437 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
449 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
451 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
453 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
457 * Init thread quit pipe.
459 * Return -1 on error or 0 if all pipes are created.
461 static int __init_thread_quit_pipe(int *a_pipe
)
467 PERROR("thread quit pipe");
471 for (i
= 0; i
< 2; i
++) {
472 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
483 static int init_thread_quit_pipe(void)
485 return __init_thread_quit_pipe(thread_quit_pipe
);
489 * Stop all threads by closing the thread quit pipe.
491 static void stop_threads(void)
495 /* Stopping all threads */
496 DBG("Terminating all threads");
497 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
499 ERR("write error on thread quit pipe");
502 /* Dispatch thread */
503 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
504 futex_nto1_wake(&ust_cmd_queue
.futex
);
508 * Close every consumer sockets.
510 static void close_consumer_sockets(void)
514 if (kconsumer_data
.err_sock
>= 0) {
515 ret
= close(kconsumer_data
.err_sock
);
517 PERROR("kernel consumer err_sock close");
520 if (ustconsumer32_data
.err_sock
>= 0) {
521 ret
= close(ustconsumer32_data
.err_sock
);
523 PERROR("UST consumerd32 err_sock close");
526 if (ustconsumer64_data
.err_sock
>= 0) {
527 ret
= close(ustconsumer64_data
.err_sock
);
529 PERROR("UST consumerd64 err_sock close");
532 if (kconsumer_data
.cmd_sock
>= 0) {
533 ret
= close(kconsumer_data
.cmd_sock
);
535 PERROR("kernel consumer cmd_sock close");
538 if (ustconsumer32_data
.cmd_sock
>= 0) {
539 ret
= close(ustconsumer32_data
.cmd_sock
);
541 PERROR("UST consumerd32 cmd_sock close");
544 if (ustconsumer64_data
.cmd_sock
>= 0) {
545 ret
= close(ustconsumer64_data
.cmd_sock
);
547 PERROR("UST consumerd64 cmd_sock close");
550 if (kconsumer_data
.channel_monitor_pipe
>= 0) {
551 ret
= close(kconsumer_data
.channel_monitor_pipe
);
553 PERROR("kernel consumer channel monitor pipe close");
556 if (ustconsumer32_data
.channel_monitor_pipe
>= 0) {
557 ret
= close(ustconsumer32_data
.channel_monitor_pipe
);
559 PERROR("UST consumerd32 channel monitor pipe close");
562 if (ustconsumer64_data
.channel_monitor_pipe
>= 0) {
563 ret
= close(ustconsumer64_data
.channel_monitor_pipe
);
565 PERROR("UST consumerd64 channel monitor pipe close");
568 if (kconsumer_data
.channel_rotate_pipe
>= 0) {
569 ret
= close(kconsumer_data
.channel_rotate_pipe
);
571 PERROR("kernel consumer channel rotate pipe close");
574 if (ustconsumer32_data
.channel_rotate_pipe
>= 0) {
575 ret
= close(ustconsumer32_data
.channel_rotate_pipe
);
577 PERROR("UST consumerd32 channel rotate pipe close");
580 if (ustconsumer64_data
.channel_rotate_pipe
>= 0) {
581 ret
= close(ustconsumer64_data
.channel_rotate_pipe
);
583 PERROR("UST consumerd64 channel rotate pipe close");
589 * Generate the full lock file path using the rundir.
591 * Return the snprintf() return value thus a negative value is an error.
593 static int generate_lock_file_path(char *path
, size_t len
)
600 /* Build lockfile path from rundir. */
601 ret
= snprintf(path
, len
, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE
, rundir
);
603 PERROR("snprintf lockfile path");
610 * Wait on consumer process termination.
612 * Need to be called with the consumer data lock held or from a context
613 * ensuring no concurrent access to data (e.g: cleanup).
615 static void wait_consumer(struct consumer_data
*consumer_data
)
620 if (consumer_data
->pid
<= 0) {
624 DBG("Waiting for complete teardown of consumerd (PID: %d)",
626 ret
= waitpid(consumer_data
->pid
, &status
, 0);
628 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
629 } else if (!WIFEXITED(status
)) {
630 ERR("consumerd termination with error: %d",
633 consumer_data
->pid
= 0;
637 * Cleanup the session daemon's data structures.
639 static void sessiond_cleanup(void)
642 struct ltt_session
*sess
, *stmp
;
645 DBG("Cleanup sessiond");
648 * Close the thread quit pipe. It has already done its job,
649 * since we are now called.
651 utils_close_pipe(thread_quit_pipe
);
654 * If opt_pidfile is undefined, the default file will be wiped when
655 * removing the rundir.
658 ret
= remove(opt_pidfile
);
660 PERROR("remove pidfile %s", opt_pidfile
);
664 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
667 snprintf(path
, PATH_MAX
,
669 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
670 DBG("Removing %s", path
);
673 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
674 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
675 DBG("Removing %s", path
);
679 snprintf(path
, PATH_MAX
,
680 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
682 DBG("Removing %s", path
);
685 snprintf(path
, PATH_MAX
,
686 DEFAULT_KCONSUMERD_PATH
,
688 DBG("Removing directory %s", path
);
691 /* ust consumerd 32 */
692 snprintf(path
, PATH_MAX
,
693 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
695 DBG("Removing %s", path
);
698 snprintf(path
, PATH_MAX
,
699 DEFAULT_USTCONSUMERD32_PATH
,
701 DBG("Removing directory %s", path
);
704 /* ust consumerd 64 */
705 snprintf(path
, PATH_MAX
,
706 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
708 DBG("Removing %s", path
);
711 snprintf(path
, PATH_MAX
,
712 DEFAULT_USTCONSUMERD64_PATH
,
714 DBG("Removing directory %s", path
);
717 DBG("Cleaning up all sessions");
719 /* Destroy session list mutex */
720 if (session_list_ptr
!= NULL
) {
721 pthread_mutex_destroy(&session_list_ptr
->lock
);
723 /* Cleanup ALL session */
724 cds_list_for_each_entry_safe(sess
, stmp
,
725 &session_list_ptr
->head
, list
) {
726 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
730 wait_consumer(&kconsumer_data
);
731 wait_consumer(&ustconsumer64_data
);
732 wait_consumer(&ustconsumer32_data
);
734 DBG("Cleaning up all agent apps");
735 agent_app_ht_clean();
737 DBG("Closing all UST sockets");
738 ust_app_clean_list();
739 buffer_reg_destroy_registries();
741 if (is_root
&& !opt_no_kernel
) {
742 DBG2("Closing kernel fd");
743 if (kernel_tracer_fd
>= 0) {
744 ret
= close(kernel_tracer_fd
);
749 DBG("Unloading kernel modules");
750 modprobe_remove_lttng_all();
754 close_consumer_sockets();
757 load_session_destroy_data(load_info
);
762 * Cleanup lock file by deleting it and finaly closing it which will
763 * release the file system lock.
765 if (lockfile_fd
>= 0) {
766 char lockfile_path
[PATH_MAX
];
768 ret
= generate_lock_file_path(lockfile_path
,
769 sizeof(lockfile_path
));
771 ret
= remove(lockfile_path
);
773 PERROR("remove lock file");
775 ret
= close(lockfile_fd
);
777 PERROR("close lock file");
783 * We do NOT rmdir rundir because there are other processes
784 * using it, for instance lttng-relayd, which can start in
785 * parallel with this teardown.
792 * Cleanup the daemon's option data structures.
794 static void sessiond_cleanup_options(void)
796 DBG("Cleaning up options");
799 * If the override option is set, the pointer points to a *non* const
800 * thus freeing it even though the variable type is set to const.
802 if (tracing_group_name_override
) {
803 free((void *) tracing_group_name
);
805 if (consumerd32_bin_override
) {
806 free((void *) consumerd32_bin
);
808 if (consumerd64_bin_override
) {
809 free((void *) consumerd64_bin
);
811 if (consumerd32_libdir_override
) {
812 free((void *) consumerd32_libdir
);
814 if (consumerd64_libdir_override
) {
815 free((void *) consumerd64_libdir
);
819 free(opt_load_session_path
);
820 free(kmod_probes_list
);
821 free(kmod_extra_probes_list
);
823 run_as_destroy_worker();
827 * Send data on a unix socket using the liblttsessiondcomm API.
829 * Return lttcomm error code.
831 static int send_unix_sock(int sock
, void *buf
, size_t len
)
833 /* Check valid length */
838 return lttcomm_send_unix_sock(sock
, buf
, len
);
842 * Free memory of a command context structure.
844 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
846 DBG("Clean command context structure");
848 if ((*cmd_ctx
)->llm
) {
849 free((*cmd_ctx
)->llm
);
851 if ((*cmd_ctx
)->lsm
) {
852 free((*cmd_ctx
)->lsm
);
860 * Notify UST applications using the shm mmap futex.
862 static int notify_ust_apps(int active
)
866 DBG("Notifying applications of session daemon state: %d", active
);
868 /* See shm.c for this call implying mmap, shm and futex calls */
869 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
870 if (wait_shm_mmap
== NULL
) {
874 /* Wake waiting process */
875 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
877 /* Apps notified successfully */
885 * Setup the outgoing data buffer for the response (llm) by allocating the
886 * right amount of memory and copying the original information from the lsm
889 * Return 0 on success, negative value on error.
891 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
892 const void *payload_buf
, size_t payload_len
,
893 const void *cmd_header_buf
, size_t cmd_header_len
)
896 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
897 const size_t cmd_header_offset
= header_len
;
898 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
899 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
901 cmd_ctx
->llm
= zmalloc(total_msg_size
);
903 if (cmd_ctx
->llm
== NULL
) {
909 /* Copy common data */
910 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
911 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
912 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
913 cmd_ctx
->llm
->data_size
= payload_len
;
914 cmd_ctx
->lttng_msg_size
= total_msg_size
;
916 /* Copy command header */
917 if (cmd_header_len
) {
918 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
924 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
933 * Version of setup_lttng_msg() without command header.
935 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
936 void *payload_buf
, size_t payload_len
)
938 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
941 * Update the kernel poll set of all channel fd available over all tracing
942 * session. Add the wakeup pipe at the end of the set.
944 static int update_kernel_poll(struct lttng_poll_event
*events
)
947 struct ltt_session
*session
;
948 struct ltt_kernel_channel
*channel
;
950 DBG("Updating kernel poll set");
953 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
954 session_lock(session
);
955 if (session
->kernel_session
== NULL
) {
956 session_unlock(session
);
960 cds_list_for_each_entry(channel
,
961 &session
->kernel_session
->channel_list
.head
, list
) {
962 /* Add channel fd to the kernel poll set */
963 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
965 session_unlock(session
);
968 DBG("Channel fd %d added to kernel set", channel
->fd
);
970 session_unlock(session
);
972 session_unlock_list();
977 session_unlock_list();
982 * Find the channel fd from 'fd' over all tracing session. When found, check
983 * for new channel stream and send those stream fds to the kernel consumer.
985 * Useful for CPU hotplug feature.
987 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
990 struct ltt_session
*session
;
991 struct ltt_kernel_session
*ksess
;
992 struct ltt_kernel_channel
*channel
;
994 DBG("Updating kernel streams for channel fd %d", fd
);
997 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
998 session_lock(session
);
999 if (session
->kernel_session
== NULL
) {
1000 session_unlock(session
);
1003 ksess
= session
->kernel_session
;
1005 cds_list_for_each_entry(channel
,
1006 &ksess
->channel_list
.head
, list
) {
1007 struct lttng_ht_iter iter
;
1008 struct consumer_socket
*socket
;
1010 if (channel
->fd
!= fd
) {
1013 DBG("Channel found, updating kernel streams");
1014 ret
= kernel_open_channel_stream(channel
);
1018 /* Update the stream global counter */
1019 ksess
->stream_count_global
+= ret
;
1022 * Have we already sent fds to the consumer? If yes, it
1023 * means that tracing is started so it is safe to send
1024 * our updated stream fds.
1026 if (ksess
->consumer_fds_sent
!= 1
1027 || ksess
->consumer
== NULL
) {
1033 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
1034 &iter
.iter
, socket
, node
.node
) {
1035 pthread_mutex_lock(socket
->lock
);
1036 ret
= kernel_consumer_send_channel_stream(socket
,
1038 session
->output_traces
? 1 : 0);
1039 pthread_mutex_unlock(socket
->lock
);
1047 session_unlock(session
);
1049 session_unlock_list();
1053 session_unlock(session
);
1054 session_unlock_list();
1059 * For each tracing session, update newly registered apps. The session list
1060 * lock MUST be acquired before calling this.
1062 static void update_ust_app(int app_sock
)
1064 struct ltt_session
*sess
, *stmp
;
1066 /* Consumer is in an ERROR state. Stop any application update. */
1067 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
1068 /* Stop the update process since the consumer is dead. */
1072 /* For all tracing session(s) */
1073 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
1074 struct ust_app
*app
;
1077 if (!sess
->ust_session
) {
1078 goto unlock_session
;
1082 assert(app_sock
>= 0);
1083 app
= ust_app_find_by_sock(app_sock
);
1086 * Application can be unregistered before so
1087 * this is possible hence simply stopping the
1090 DBG3("UST app update failed to find app sock %d",
1094 ust_app_global_update(sess
->ust_session
, app
);
1098 session_unlock(sess
);
1103 * This thread manage event coming from the kernel.
1105 * Features supported in this thread:
1108 static void *thread_manage_kernel(void *data
)
1110 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
1111 uint32_t revents
, nb_fd
;
1113 struct lttng_poll_event events
;
1115 DBG("[thread] Thread manage kernel started");
1117 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
1120 * This first step of the while is to clean this structure which could free
1121 * non NULL pointers so initialize it before the loop.
1123 lttng_poll_init(&events
);
1125 if (testpoint(sessiond_thread_manage_kernel
)) {
1126 goto error_testpoint
;
1129 health_code_update();
1131 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1132 goto error_testpoint
;
1136 health_code_update();
1138 if (update_poll_flag
== 1) {
1139 /* Clean events object. We are about to populate it again. */
1140 lttng_poll_clean(&events
);
1142 ret
= sessiond_set_thread_pollset(&events
, 2);
1144 goto error_poll_create
;
1147 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1152 /* This will add the available kernel channel if any. */
1153 ret
= update_kernel_poll(&events
);
1157 update_poll_flag
= 0;
1160 DBG("Thread kernel polling");
1162 /* Poll infinite value of time */
1164 health_poll_entry();
1165 ret
= lttng_poll_wait(&events
, -1);
1166 DBG("Thread kernel return from poll on %d fds",
1167 LTTNG_POLL_GETNB(&events
));
1171 * Restart interrupted system call.
1173 if (errno
== EINTR
) {
1177 } else if (ret
== 0) {
1178 /* Should not happen since timeout is infinite */
1179 ERR("Return value of poll is 0 with an infinite timeout.\n"
1180 "This should not have happened! Continuing...");
1186 for (i
= 0; i
< nb_fd
; i
++) {
1187 /* Fetch once the poll data */
1188 revents
= LTTNG_POLL_GETEV(&events
, i
);
1189 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1191 health_code_update();
1194 /* No activity for this FD (poll implementation). */
1198 /* Thread quit pipe has been closed. Killing thread. */
1199 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1205 /* Check for data on kernel pipe */
1206 if (revents
& LPOLLIN
) {
1207 if (pollfd
== kernel_poll_pipe
[0]) {
1208 (void) lttng_read(kernel_poll_pipe
[0],
1211 * Ret value is useless here, if this pipe gets any actions an
1212 * update is required anyway.
1214 update_poll_flag
= 1;
1218 * New CPU detected by the kernel. Adding kernel stream to
1219 * kernel session and updating the kernel consumer
1221 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1227 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1228 update_poll_flag
= 1;
1231 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1239 lttng_poll_clean(&events
);
1242 utils_close_pipe(kernel_poll_pipe
);
1243 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1246 ERR("Health error occurred in %s", __func__
);
1247 WARN("Kernel thread died unexpectedly. "
1248 "Kernel tracing can continue but CPU hotplug is disabled.");
1250 health_unregister(health_sessiond
);
1251 DBG("Kernel thread dying");
1256 * Signal pthread condition of the consumer data that the thread.
1258 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1260 pthread_mutex_lock(&data
->cond_mutex
);
1263 * The state is set before signaling. It can be any value, it's the waiter
1264 * job to correctly interpret this condition variable associated to the
1265 * consumer pthread_cond.
1267 * A value of 0 means that the corresponding thread of the consumer data
1268 * was not started. 1 indicates that the thread has started and is ready
1269 * for action. A negative value means that there was an error during the
1272 data
->consumer_thread_is_ready
= state
;
1273 (void) pthread_cond_signal(&data
->cond
);
1275 pthread_mutex_unlock(&data
->cond_mutex
);
1279 * This thread manage the consumer error sent back to the session daemon.
1281 static void *thread_manage_consumer(void *data
)
1283 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1284 uint32_t revents
, nb_fd
;
1285 enum lttcomm_return_code code
;
1286 struct lttng_poll_event events
;
1287 struct consumer_data
*consumer_data
= data
;
1288 struct consumer_socket
*cmd_socket_wrapper
= NULL
;
1290 DBG("[thread] Manage consumer started");
1292 rcu_register_thread();
1293 rcu_thread_online();
1295 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1297 health_code_update();
1300 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1301 * metadata_sock. Nothing more will be added to this poll set.
1303 ret
= sessiond_set_thread_pollset(&events
, 3);
1309 * The error socket here is already in a listening state which was done
1310 * just before spawning this thread to avoid a race between the consumer
1311 * daemon exec trying to connect and the listen() call.
1313 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1318 health_code_update();
1320 /* Infinite blocking call, waiting for transmission */
1322 health_poll_entry();
1324 if (testpoint(sessiond_thread_manage_consumer
)) {
1328 ret
= lttng_poll_wait(&events
, -1);
1332 * Restart interrupted system call.
1334 if (errno
== EINTR
) {
1342 for (i
= 0; i
< nb_fd
; i
++) {
1343 /* Fetch once the poll data */
1344 revents
= LTTNG_POLL_GETEV(&events
, i
);
1345 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1347 health_code_update();
1350 /* No activity for this FD (poll implementation). */
1354 /* Thread quit pipe has been closed. Killing thread. */
1355 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1361 /* Event on the registration socket */
1362 if (pollfd
== consumer_data
->err_sock
) {
1363 if (revents
& LPOLLIN
) {
1365 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1366 ERR("consumer err socket poll error");
1369 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1375 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1381 * Set the CLOEXEC flag. Return code is useless because either way, the
1384 (void) utils_set_fd_cloexec(sock
);
1386 health_code_update();
1388 DBG2("Receiving code from consumer err_sock");
1390 /* Getting status code from kconsumerd */
1391 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1392 sizeof(enum lttcomm_return_code
));
1397 health_code_update();
1398 if (code
!= LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1399 ERR("consumer error when waiting for SOCK_READY : %s",
1400 lttcomm_get_readable_code(-code
));
1404 /* Connect both command and metadata sockets. */
1405 consumer_data
->cmd_sock
=
1406 lttcomm_connect_unix_sock(
1407 consumer_data
->cmd_unix_sock_path
);
1408 consumer_data
->metadata_fd
=
1409 lttcomm_connect_unix_sock(
1410 consumer_data
->cmd_unix_sock_path
);
1411 if (consumer_data
->cmd_sock
< 0 || consumer_data
->metadata_fd
< 0) {
1412 PERROR("consumer connect cmd socket");
1413 /* On error, signal condition and quit. */
1414 signal_consumer_condition(consumer_data
, -1);
1418 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1420 /* Create metadata socket lock. */
1421 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1422 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1423 PERROR("zmalloc pthread mutex");
1426 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1428 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1429 DBG("Consumer metadata socket ready (fd: %d)",
1430 consumer_data
->metadata_fd
);
1433 * Remove the consumerd error sock since we've established a connection.
1435 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1440 /* Add new accepted error socket. */
1441 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1446 /* Add metadata socket that is successfully connected. */
1447 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1448 LPOLLIN
| LPOLLRDHUP
);
1453 health_code_update();
1456 * Transfer the write-end of the channel monitoring and rotate pipe
1457 * to the consumer by issuing a SET_CHANNEL_MONITOR_PIPE and
1458 * SET_CHANNEL_ROTATE_PIPE commands.
1460 cmd_socket_wrapper
= consumer_allocate_socket(&consumer_data
->cmd_sock
);
1461 if (!cmd_socket_wrapper
) {
1465 ret
= consumer_send_channel_monitor_pipe(cmd_socket_wrapper
,
1466 consumer_data
->channel_monitor_pipe
);
1471 ret
= consumer_send_channel_rotate_pipe(cmd_socket_wrapper
,
1472 consumer_data
->channel_rotate_pipe
);
1477 /* Discard the socket wrapper as it is no longer needed. */
1478 consumer_destroy_socket(cmd_socket_wrapper
);
1479 cmd_socket_wrapper
= NULL
;
1481 /* The thread is completely initialized, signal that it is ready. */
1482 signal_consumer_condition(consumer_data
, 1);
1484 /* Infinite blocking call, waiting for transmission */
1487 health_code_update();
1489 /* Exit the thread because the thread quit pipe has been triggered. */
1491 /* Not a health error. */
1496 health_poll_entry();
1497 ret
= lttng_poll_wait(&events
, -1);
1501 * Restart interrupted system call.
1503 if (errno
== EINTR
) {
1511 for (i
= 0; i
< nb_fd
; i
++) {
1512 /* Fetch once the poll data */
1513 revents
= LTTNG_POLL_GETEV(&events
, i
);
1514 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1516 health_code_update();
1519 /* No activity for this FD (poll implementation). */
1524 * Thread quit pipe has been triggered, flag that we should stop
1525 * but continue the current loop to handle potential data from
1528 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1530 if (pollfd
== sock
) {
1531 /* Event on the consumerd socket */
1532 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1533 && !(revents
& LPOLLIN
)) {
1534 ERR("consumer err socket second poll error");
1537 health_code_update();
1538 /* Wait for any kconsumerd error */
1539 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1540 sizeof(enum lttcomm_return_code
));
1542 ERR("consumer closed the command socket");
1546 ERR("consumer return code : %s",
1547 lttcomm_get_readable_code(-code
));
1550 } else if (pollfd
== consumer_data
->metadata_fd
) {
1551 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1552 && !(revents
& LPOLLIN
)) {
1553 ERR("consumer err metadata socket second poll error");
1556 /* UST metadata requests */
1557 ret
= ust_consumer_metadata_request(
1558 &consumer_data
->metadata_sock
);
1560 ERR("Handling metadata request");
1564 /* No need for an else branch all FDs are tested prior. */
1566 health_code_update();
1572 * We lock here because we are about to close the sockets and some other
1573 * thread might be using them so get exclusive access which will abort all
1574 * other consumer command by other threads.
1576 pthread_mutex_lock(&consumer_data
->lock
);
1578 /* Immediately set the consumerd state to stopped */
1579 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1580 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1581 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1582 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1583 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1585 /* Code flow error... */
1589 if (consumer_data
->err_sock
>= 0) {
1590 ret
= close(consumer_data
->err_sock
);
1594 consumer_data
->err_sock
= -1;
1596 if (consumer_data
->cmd_sock
>= 0) {
1597 ret
= close(consumer_data
->cmd_sock
);
1601 consumer_data
->cmd_sock
= -1;
1603 if (consumer_data
->metadata_sock
.fd_ptr
&&
1604 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1605 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1617 unlink(consumer_data
->err_unix_sock_path
);
1618 unlink(consumer_data
->cmd_unix_sock_path
);
1619 pthread_mutex_unlock(&consumer_data
->lock
);
1621 /* Cleanup metadata socket mutex. */
1622 if (consumer_data
->metadata_sock
.lock
) {
1623 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1624 free(consumer_data
->metadata_sock
.lock
);
1626 lttng_poll_clean(&events
);
1628 if (cmd_socket_wrapper
) {
1629 consumer_destroy_socket(cmd_socket_wrapper
);
1634 ERR("Health error occurred in %s", __func__
);
1636 health_unregister(health_sessiond
);
1637 DBG("consumer thread cleanup completed");
1639 rcu_thread_offline();
1640 rcu_unregister_thread();
1646 * This thread manage application communication.
1648 static void *thread_manage_apps(void *data
)
1650 int i
, ret
, pollfd
, err
= -1;
1652 uint32_t revents
, nb_fd
;
1653 struct lttng_poll_event events
;
1655 DBG("[thread] Manage application started");
1657 rcu_register_thread();
1658 rcu_thread_online();
1660 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1662 if (testpoint(sessiond_thread_manage_apps
)) {
1663 goto error_testpoint
;
1666 health_code_update();
1668 ret
= sessiond_set_thread_pollset(&events
, 2);
1670 goto error_poll_create
;
1673 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1678 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1682 health_code_update();
1685 DBG("Apps thread polling");
1687 /* Inifinite blocking call, waiting for transmission */
1689 health_poll_entry();
1690 ret
= lttng_poll_wait(&events
, -1);
1691 DBG("Apps thread return from poll on %d fds",
1692 LTTNG_POLL_GETNB(&events
));
1696 * Restart interrupted system call.
1698 if (errno
== EINTR
) {
1706 for (i
= 0; i
< nb_fd
; i
++) {
1707 /* Fetch once the poll data */
1708 revents
= LTTNG_POLL_GETEV(&events
, i
);
1709 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1711 health_code_update();
1714 /* No activity for this FD (poll implementation). */
1718 /* Thread quit pipe has been closed. Killing thread. */
1719 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1725 /* Inspect the apps cmd pipe */
1726 if (pollfd
== apps_cmd_pipe
[0]) {
1727 if (revents
& LPOLLIN
) {
1731 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1732 if (size_ret
< sizeof(sock
)) {
1733 PERROR("read apps cmd pipe");
1737 health_code_update();
1740 * Since this is a command socket (write then read),
1741 * we only monitor the error events of the socket.
1743 ret
= lttng_poll_add(&events
, sock
,
1744 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1749 DBG("Apps with sock %d added to poll set", sock
);
1750 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1751 ERR("Apps command pipe error");
1754 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1759 * At this point, we know that a registered application made
1760 * the event at poll_wait.
1762 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1763 /* Removing from the poll set */
1764 ret
= lttng_poll_del(&events
, pollfd
);
1769 /* Socket closed on remote end. */
1770 ust_app_unregister(pollfd
);
1772 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1777 health_code_update();
1783 lttng_poll_clean(&events
);
1786 utils_close_pipe(apps_cmd_pipe
);
1787 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1790 * We don't clean the UST app hash table here since already registered
1791 * applications can still be controlled so let them be until the session
1792 * daemon dies or the applications stop.
1797 ERR("Health error occurred in %s", __func__
);
1799 health_unregister(health_sessiond
);
1800 DBG("Application communication apps thread cleanup complete");
1801 rcu_thread_offline();
1802 rcu_unregister_thread();
1807 * Send a socket to a thread This is called from the dispatch UST registration
1808 * thread once all sockets are set for the application.
1810 * The sock value can be invalid, we don't really care, the thread will handle
1811 * it and make the necessary cleanup if so.
1813 * On success, return 0 else a negative value being the errno message of the
1816 static int send_socket_to_thread(int fd
, int sock
)
1821 * It's possible that the FD is set as invalid with -1 concurrently just
1822 * before calling this function being a shutdown state of the thread.
1829 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1830 if (ret
< sizeof(sock
)) {
1831 PERROR("write apps pipe %d", fd
);
1838 /* All good. Don't send back the write positive ret value. */
1845 * Sanitize the wait queue of the dispatch registration thread meaning removing
1846 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1847 * notify socket is never received.
1849 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1851 int ret
, nb_fd
= 0, i
;
1852 unsigned int fd_added
= 0;
1853 struct lttng_poll_event events
;
1854 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1858 lttng_poll_init(&events
);
1860 /* Just skip everything for an empty queue. */
1861 if (!wait_queue
->count
) {
1865 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1870 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1871 &wait_queue
->head
, head
) {
1872 assert(wait_node
->app
);
1873 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1874 LPOLLHUP
| LPOLLERR
);
1887 * Poll but don't block so we can quickly identify the faulty events and
1888 * clean them afterwards from the wait queue.
1890 ret
= lttng_poll_wait(&events
, 0);
1896 for (i
= 0; i
< nb_fd
; i
++) {
1897 /* Get faulty FD. */
1898 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1899 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1902 /* No activity for this FD (poll implementation). */
1906 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1907 &wait_queue
->head
, head
) {
1908 if (pollfd
== wait_node
->app
->sock
&&
1909 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1910 cds_list_del(&wait_node
->head
);
1911 wait_queue
->count
--;
1912 ust_app_destroy(wait_node
->app
);
1915 * Silence warning of use-after-free in
1916 * cds_list_for_each_entry_safe which uses
1917 * __typeof__(*wait_node).
1922 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1929 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1933 lttng_poll_clean(&events
);
1937 lttng_poll_clean(&events
);
1939 ERR("Unable to sanitize wait queue");
1944 * Dispatch request from the registration threads to the application
1945 * communication thread.
1947 static void *thread_dispatch_ust_registration(void *data
)
1950 struct cds_wfcq_node
*node
;
1951 struct ust_command
*ust_cmd
= NULL
;
1952 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1953 struct ust_reg_wait_queue wait_queue
= {
1957 rcu_register_thread();
1959 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1961 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1962 goto error_testpoint
;
1965 health_code_update();
1967 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1969 DBG("[thread] Dispatch UST command started");
1972 health_code_update();
1974 /* Atomically prepare the queue futex */
1975 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1977 if (CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1982 struct ust_app
*app
= NULL
;
1986 * Make sure we don't have node(s) that have hung up before receiving
1987 * the notify socket. This is to clean the list in order to avoid
1988 * memory leaks from notify socket that are never seen.
1990 sanitize_wait_queue(&wait_queue
);
1992 health_code_update();
1993 /* Dequeue command for registration */
1994 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1996 DBG("Woken up but nothing in the UST command queue");
1997 /* Continue thread execution */
2001 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2003 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
2004 " gid:%d sock:%d name:%s (version %d.%d)",
2005 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2006 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2007 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2008 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2010 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
2011 wait_node
= zmalloc(sizeof(*wait_node
));
2013 PERROR("zmalloc wait_node dispatch");
2014 ret
= close(ust_cmd
->sock
);
2016 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
2018 lttng_fd_put(LTTNG_FD_APPS
, 1);
2022 CDS_INIT_LIST_HEAD(&wait_node
->head
);
2024 /* Create application object if socket is CMD. */
2025 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
2027 if (!wait_node
->app
) {
2028 ret
= close(ust_cmd
->sock
);
2030 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
2032 lttng_fd_put(LTTNG_FD_APPS
, 1);
2038 * Add application to the wait queue so we can set the notify
2039 * socket before putting this object in the global ht.
2041 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
2046 * We have to continue here since we don't have the notify
2047 * socket and the application MUST be added to the hash table
2048 * only at that moment.
2053 * Look for the application in the local wait queue and set the
2054 * notify socket if found.
2056 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2057 &wait_queue
.head
, head
) {
2058 health_code_update();
2059 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
2060 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
2061 cds_list_del(&wait_node
->head
);
2063 app
= wait_node
->app
;
2065 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
2071 * With no application at this stage the received socket is
2072 * basically useless so close it before we free the cmd data
2073 * structure for good.
2076 ret
= close(ust_cmd
->sock
);
2078 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
2080 lttng_fd_put(LTTNG_FD_APPS
, 1);
2087 * @session_lock_list
2089 * Lock the global session list so from the register up to the
2090 * registration done message, no thread can see the application
2091 * and change its state.
2093 session_lock_list();
2097 * Add application to the global hash table. This needs to be
2098 * done before the update to the UST registry can locate the
2103 /* Set app version. This call will print an error if needed. */
2104 (void) ust_app_version(app
);
2106 /* Send notify socket through the notify pipe. */
2107 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
2111 session_unlock_list();
2113 * No notify thread, stop the UST tracing. However, this is
2114 * not an internal error of the this thread thus setting
2115 * the health error code to a normal exit.
2122 * Update newly registered application with the tracing
2123 * registry info already enabled information.
2125 update_ust_app(app
->sock
);
2128 * Don't care about return value. Let the manage apps threads
2129 * handle app unregistration upon socket close.
2131 (void) ust_app_register_done(app
);
2134 * Even if the application socket has been closed, send the app
2135 * to the thread and unregistration will take place at that
2138 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2141 session_unlock_list();
2143 * No apps. thread, stop the UST tracing. However, this is
2144 * not an internal error of the this thread thus setting
2145 * the health error code to a normal exit.
2152 session_unlock_list();
2154 } while (node
!= NULL
);
2156 health_poll_entry();
2157 /* Futex wait on queue. Blocking call on futex() */
2158 futex_nto1_wait(&ust_cmd_queue
.futex
);
2161 /* Normal exit, no error */
2165 /* Clean up wait queue. */
2166 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2167 &wait_queue
.head
, head
) {
2168 cds_list_del(&wait_node
->head
);
2173 /* Empty command queue. */
2175 /* Dequeue command for registration */
2176 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2180 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2181 ret
= close(ust_cmd
->sock
);
2183 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2185 lttng_fd_put(LTTNG_FD_APPS
, 1);
2190 DBG("Dispatch thread dying");
2193 ERR("Health error occurred in %s", __func__
);
2195 health_unregister(health_sessiond
);
2196 rcu_unregister_thread();
2201 * This thread manage application registration.
2203 static void *thread_registration_apps(void *data
)
2205 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2206 uint32_t revents
, nb_fd
;
2207 struct lttng_poll_event events
;
2209 * Get allocated in this thread, enqueued to a global queue, dequeued and
2210 * freed in the manage apps thread.
2212 struct ust_command
*ust_cmd
= NULL
;
2214 DBG("[thread] Manage application registration started");
2216 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2218 if (testpoint(sessiond_thread_registration_apps
)) {
2219 goto error_testpoint
;
2222 ret
= lttcomm_listen_unix_sock(apps_sock
);
2228 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2229 * more will be added to this poll set.
2231 ret
= sessiond_set_thread_pollset(&events
, 2);
2233 goto error_create_poll
;
2236 /* Add the application registration socket */
2237 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2239 goto error_poll_add
;
2242 /* Notify all applications to register */
2243 ret
= notify_ust_apps(1);
2245 ERR("Failed to notify applications or create the wait shared memory.\n"
2246 "Execution continues but there might be problem for already\n"
2247 "running applications that wishes to register.");
2251 DBG("Accepting application registration");
2253 /* Inifinite blocking call, waiting for transmission */
2255 health_poll_entry();
2256 ret
= lttng_poll_wait(&events
, -1);
2260 * Restart interrupted system call.
2262 if (errno
== EINTR
) {
2270 for (i
= 0; i
< nb_fd
; i
++) {
2271 health_code_update();
2273 /* Fetch once the poll data */
2274 revents
= LTTNG_POLL_GETEV(&events
, i
);
2275 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2278 /* No activity for this FD (poll implementation). */
2282 /* Thread quit pipe has been closed. Killing thread. */
2283 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2289 /* Event on the registration socket */
2290 if (pollfd
== apps_sock
) {
2291 if (revents
& LPOLLIN
) {
2292 sock
= lttcomm_accept_unix_sock(apps_sock
);
2298 * Set socket timeout for both receiving and ending.
2299 * app_socket_timeout is in seconds, whereas
2300 * lttcomm_setsockopt_rcv_timeout and
2301 * lttcomm_setsockopt_snd_timeout expect msec as
2304 if (app_socket_timeout
>= 0) {
2305 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2306 app_socket_timeout
* 1000);
2307 (void) lttcomm_setsockopt_snd_timeout(sock
,
2308 app_socket_timeout
* 1000);
2312 * Set the CLOEXEC flag. Return code is useless because
2313 * either way, the show must go on.
2315 (void) utils_set_fd_cloexec(sock
);
2317 /* Create UST registration command for enqueuing */
2318 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2319 if (ust_cmd
== NULL
) {
2320 PERROR("ust command zmalloc");
2329 * Using message-based transmissions to ensure we don't
2330 * have to deal with partially received messages.
2332 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2334 ERR("Exhausted file descriptors allowed for applications.");
2344 health_code_update();
2345 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2348 /* Close socket of the application. */
2353 lttng_fd_put(LTTNG_FD_APPS
, 1);
2357 health_code_update();
2359 ust_cmd
->sock
= sock
;
2362 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2363 " gid:%d sock:%d name:%s (version %d.%d)",
2364 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2365 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2366 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2367 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2370 * Lock free enqueue the registration request. The red pill
2371 * has been taken! This apps will be part of the *system*.
2373 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2376 * Wake the registration queue futex. Implicit memory
2377 * barrier with the exchange in cds_wfcq_enqueue.
2379 futex_nto1_wake(&ust_cmd_queue
.futex
);
2380 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2381 ERR("Register apps socket poll error");
2384 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2393 /* Notify that the registration thread is gone */
2396 if (apps_sock
>= 0) {
2397 ret
= close(apps_sock
);
2407 lttng_fd_put(LTTNG_FD_APPS
, 1);
2409 unlink(apps_unix_sock_path
);
2412 lttng_poll_clean(&events
);
2416 DBG("UST Registration thread cleanup complete");
2419 ERR("Health error occurred in %s", __func__
);
2421 health_unregister(health_sessiond
);
2427 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2428 * exec or it will fails.
2430 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2433 struct timespec timeout
;
2436 * Make sure we set the readiness flag to 0 because we are NOT ready.
2437 * This access to consumer_thread_is_ready does not need to be
2438 * protected by consumer_data.cond_mutex (yet) since the consumer
2439 * management thread has not been started at this point.
2441 consumer_data
->consumer_thread_is_ready
= 0;
2443 /* Setup pthread condition */
2444 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2447 PERROR("pthread_condattr_init consumer data");
2452 * Set the monotonic clock in order to make sure we DO NOT jump in time
2453 * between the clock_gettime() call and the timedwait call. See bug #324
2454 * for a more details and how we noticed it.
2456 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2459 PERROR("pthread_condattr_setclock consumer data");
2463 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2466 PERROR("pthread_cond_init consumer data");
2470 ret
= pthread_create(&consumer_data
->thread
, default_pthread_attr(),
2471 thread_manage_consumer
, consumer_data
);
2474 PERROR("pthread_create consumer");
2479 /* We are about to wait on a pthread condition */
2480 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2482 /* Get time for sem_timedwait absolute timeout */
2483 clock_ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2485 * Set the timeout for the condition timed wait even if the clock gettime
2486 * call fails since we might loop on that call and we want to avoid to
2487 * increment the timeout too many times.
2489 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2492 * The following loop COULD be skipped in some conditions so this is why we
2493 * set ret to 0 in order to make sure at least one round of the loop is
2499 * Loop until the condition is reached or when a timeout is reached. Note
2500 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2501 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2502 * possible. This loop does not take any chances and works with both of
2505 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2506 if (clock_ret
< 0) {
2507 PERROR("clock_gettime spawn consumer");
2508 /* Infinite wait for the consumerd thread to be ready */
2509 ret
= pthread_cond_wait(&consumer_data
->cond
,
2510 &consumer_data
->cond_mutex
);
2512 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2513 &consumer_data
->cond_mutex
, &timeout
);
2517 /* Release the pthread condition */
2518 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2522 if (ret
== ETIMEDOUT
) {
2526 * Call has timed out so we kill the kconsumerd_thread and return
2529 ERR("Condition timed out. The consumer thread was never ready."
2531 pth_ret
= pthread_cancel(consumer_data
->thread
);
2533 PERROR("pthread_cancel consumer thread");
2536 PERROR("pthread_cond_wait failed consumer thread");
2538 /* Caller is expecting a negative value on failure. */
2543 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2544 if (consumer_data
->pid
== 0) {
2545 ERR("Consumerd did not start");
2546 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2549 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2558 * Join consumer thread
2560 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2564 /* Consumer pid must be a real one. */
2565 if (consumer_data
->pid
> 0) {
2567 ret
= kill(consumer_data
->pid
, SIGTERM
);
2569 PERROR("Error killing consumer daemon");
2572 return pthread_join(consumer_data
->thread
, &status
);
2579 * Fork and exec a consumer daemon (consumerd).
2581 * Return pid if successful else -1.
2583 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2587 const char *consumer_to_use
;
2588 const char *verbosity
;
2591 DBG("Spawning consumerd");
2598 if (opt_verbose_consumer
) {
2599 verbosity
= "--verbose";
2600 } else if (lttng_opt_quiet
) {
2601 verbosity
= "--quiet";
2606 switch (consumer_data
->type
) {
2607 case LTTNG_CONSUMER_KERNEL
:
2609 * Find out which consumerd to execute. We will first try the
2610 * 64-bit path, then the sessiond's installation directory, and
2611 * fallback on the 32-bit one,
2613 DBG3("Looking for a kernel consumer at these locations:");
2614 DBG3(" 1) %s", consumerd64_bin
);
2615 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2616 DBG3(" 3) %s", consumerd32_bin
);
2617 if (stat(consumerd64_bin
, &st
) == 0) {
2618 DBG3("Found location #1");
2619 consumer_to_use
= consumerd64_bin
;
2620 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2621 DBG3("Found location #2");
2622 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2623 } else if (stat(consumerd32_bin
, &st
) == 0) {
2624 DBG3("Found location #3");
2625 consumer_to_use
= consumerd32_bin
;
2627 DBG("Could not find any valid consumerd executable");
2631 DBG("Using kernel consumer at: %s", consumer_to_use
);
2632 (void) execl(consumer_to_use
,
2633 "lttng-consumerd", verbosity
, "-k",
2634 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2635 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2636 "--group", tracing_group_name
,
2639 case LTTNG_CONSUMER64_UST
:
2641 char *tmpnew
= NULL
;
2643 if (consumerd64_libdir
[0] != '\0') {
2647 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2651 tmplen
= strlen("LD_LIBRARY_PATH=")
2652 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2653 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2658 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2659 strcat(tmpnew
, consumerd64_libdir
);
2660 if (tmp
[0] != '\0') {
2661 strcat(tmpnew
, ":");
2662 strcat(tmpnew
, tmp
);
2664 ret
= putenv(tmpnew
);
2671 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2672 (void) execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2673 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2674 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2675 "--group", tracing_group_name
,
2677 if (consumerd64_libdir
[0] != '\0') {
2682 case LTTNG_CONSUMER32_UST
:
2684 char *tmpnew
= NULL
;
2686 if (consumerd32_libdir
[0] != '\0') {
2690 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2694 tmplen
= strlen("LD_LIBRARY_PATH=")
2695 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2696 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2701 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2702 strcat(tmpnew
, consumerd32_libdir
);
2703 if (tmp
[0] != '\0') {
2704 strcat(tmpnew
, ":");
2705 strcat(tmpnew
, tmp
);
2707 ret
= putenv(tmpnew
);
2714 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2715 (void) execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2716 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2717 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2718 "--group", tracing_group_name
,
2720 if (consumerd32_libdir
[0] != '\0') {
2726 PERROR("unknown consumer type");
2730 PERROR("Consumer execl()");
2732 /* Reaching this point, we got a failure on our execl(). */
2734 } else if (pid
> 0) {
2737 PERROR("start consumer fork");
2745 * Spawn the consumerd daemon and session daemon thread.
2747 static int start_consumerd(struct consumer_data
*consumer_data
)
2752 * Set the listen() state on the socket since there is a possible race
2753 * between the exec() of the consumer daemon and this call if place in the
2754 * consumer thread. See bug #366 for more details.
2756 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2761 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2762 if (consumer_data
->pid
!= 0) {
2763 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2767 ret
= spawn_consumerd(consumer_data
);
2769 ERR("Spawning consumerd failed");
2770 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2774 /* Setting up the consumer_data pid */
2775 consumer_data
->pid
= ret
;
2776 DBG2("Consumer pid %d", consumer_data
->pid
);
2777 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2779 DBG2("Spawning consumer control thread");
2780 ret
= spawn_consumer_thread(consumer_data
);
2782 ERR("Fatal error spawning consumer control thread");
2790 /* Cleanup already created sockets on error. */
2791 if (consumer_data
->err_sock
>= 0) {
2794 err
= close(consumer_data
->err_sock
);
2796 PERROR("close consumer data error socket");
2803 * Setup necessary data for kernel tracer action.
2805 static int init_kernel_tracer(void)
2809 /* Modprobe lttng kernel modules */
2810 ret
= modprobe_lttng_control();
2815 /* Open debugfs lttng */
2816 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2817 if (kernel_tracer_fd
< 0) {
2818 DBG("Failed to open %s", module_proc_lttng
);
2822 /* Validate kernel version */
2823 ret
= kernel_validate_version(kernel_tracer_fd
);
2828 ret
= modprobe_lttng_data();
2833 ret
= kernel_supports_ring_buffer_snapshot_sample_positions(
2840 WARN("Kernel tracer does not support buffer monitoring. "
2841 "The monitoring timer of channels in the kernel domain "
2842 "will be set to 0 (disabled).");
2845 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2849 modprobe_remove_lttng_control();
2850 ret
= close(kernel_tracer_fd
);
2854 kernel_tracer_fd
= -1;
2855 return LTTNG_ERR_KERN_VERSION
;
2858 ret
= close(kernel_tracer_fd
);
2864 modprobe_remove_lttng_control();
2867 WARN("No kernel tracer available");
2868 kernel_tracer_fd
= -1;
2870 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2872 return LTTNG_ERR_KERN_NA
;
2878 * Copy consumer output from the tracing session to the domain session. The
2879 * function also applies the right modification on a per domain basis for the
2880 * trace files destination directory.
2882 * Should *NOT* be called with RCU read-side lock held.
2884 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2887 const char *dir_name
;
2888 struct consumer_output
*consumer
;
2891 assert(session
->consumer
);
2894 case LTTNG_DOMAIN_KERNEL
:
2895 DBG3("Copying tracing session consumer output in kernel session");
2897 * XXX: We should audit the session creation and what this function
2898 * does "extra" in order to avoid a destroy since this function is used
2899 * in the domain session creation (kernel and ust) only. Same for UST
2902 if (session
->kernel_session
->consumer
) {
2903 consumer_output_put(session
->kernel_session
->consumer
);
2905 session
->kernel_session
->consumer
=
2906 consumer_copy_output(session
->consumer
);
2907 /* Ease our life a bit for the next part */
2908 consumer
= session
->kernel_session
->consumer
;
2909 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2911 case LTTNG_DOMAIN_JUL
:
2912 case LTTNG_DOMAIN_LOG4J
:
2913 case LTTNG_DOMAIN_PYTHON
:
2914 case LTTNG_DOMAIN_UST
:
2915 DBG3("Copying tracing session consumer output in UST session");
2916 if (session
->ust_session
->consumer
) {
2917 consumer_output_put(session
->ust_session
->consumer
);
2919 session
->ust_session
->consumer
=
2920 consumer_copy_output(session
->consumer
);
2921 /* Ease our life a bit for the next part */
2922 consumer
= session
->ust_session
->consumer
;
2923 dir_name
= DEFAULT_UST_TRACE_DIR
;
2926 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2930 /* Append correct directory to subdir */
2931 strncat(consumer
->subdir
, dir_name
,
2932 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2933 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2942 * Create an UST session and add it to the session ust list.
2944 * Should *NOT* be called with RCU read-side lock held.
2946 static int create_ust_session(struct ltt_session
*session
,
2947 struct lttng_domain
*domain
)
2950 struct ltt_ust_session
*lus
= NULL
;
2954 assert(session
->consumer
);
2956 switch (domain
->type
) {
2957 case LTTNG_DOMAIN_JUL
:
2958 case LTTNG_DOMAIN_LOG4J
:
2959 case LTTNG_DOMAIN_PYTHON
:
2960 case LTTNG_DOMAIN_UST
:
2963 ERR("Unknown UST domain on create session %d", domain
->type
);
2964 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2968 DBG("Creating UST session");
2970 lus
= trace_ust_create_session(session
->id
);
2972 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2976 lus
->uid
= session
->uid
;
2977 lus
->gid
= session
->gid
;
2978 lus
->output_traces
= session
->output_traces
;
2979 lus
->snapshot_mode
= session
->snapshot_mode
;
2980 lus
->live_timer_interval
= session
->live_timer
;
2981 session
->ust_session
= lus
;
2982 if (session
->shm_path
[0]) {
2983 strncpy(lus
->root_shm_path
, session
->shm_path
,
2984 sizeof(lus
->root_shm_path
));
2985 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2986 strncpy(lus
->shm_path
, session
->shm_path
,
2987 sizeof(lus
->shm_path
));
2988 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2989 strncat(lus
->shm_path
, "/ust",
2990 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2992 /* Copy session output to the newly created UST session */
2993 ret
= copy_session_consumer(domain
->type
, session
);
2994 if (ret
!= LTTNG_OK
) {
3002 session
->ust_session
= NULL
;
3007 * Create a kernel tracer session then create the default channel.
3009 static int create_kernel_session(struct ltt_session
*session
)
3013 DBG("Creating kernel session");
3015 ret
= kernel_create_session(session
, kernel_tracer_fd
);
3017 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3021 /* Code flow safety */
3022 assert(session
->kernel_session
);
3024 /* Copy session output to the newly created Kernel session */
3025 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
3026 if (ret
!= LTTNG_OK
) {
3030 /* Create directory(ies) on local filesystem. */
3031 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
3032 strlen(session
->kernel_session
->consumer
->dst
.session_root_path
) > 0) {
3033 ret
= run_as_mkdir_recursive(
3034 session
->kernel_session
->consumer
->dst
.session_root_path
,
3035 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
3037 if (errno
!= EEXIST
) {
3038 ERR("Trace directory creation error");
3044 session
->kernel_session
->uid
= session
->uid
;
3045 session
->kernel_session
->gid
= session
->gid
;
3046 session
->kernel_session
->output_traces
= session
->output_traces
;
3047 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
3052 trace_kernel_destroy_session(session
->kernel_session
);
3053 session
->kernel_session
= NULL
;
3058 * Count number of session permitted by uid/gid.
3060 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
3063 struct ltt_session
*session
;
3065 DBG("Counting number of available session for UID %d GID %d",
3067 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
3069 * Only list the sessions the user can control.
3071 if (!session_access_ok(session
, uid
, gid
)) {
3080 * Process the command requested by the lttng client within the command
3081 * context structure. This function make sure that the return structure (llm)
3082 * is set and ready for transmission before returning.
3084 * Return any error encountered or 0 for success.
3086 * "sock" is only used for special-case var. len data.
3088 * Should *NOT* be called with RCU read-side lock held.
3090 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
3094 int need_tracing_session
= 1;
3097 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
3099 assert(!rcu_read_ongoing());
3103 switch (cmd_ctx
->lsm
->cmd_type
) {
3104 case LTTNG_CREATE_SESSION
:
3105 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3106 case LTTNG_CREATE_SESSION_LIVE
:
3107 case LTTNG_DESTROY_SESSION
:
3108 case LTTNG_LIST_SESSIONS
:
3109 case LTTNG_LIST_DOMAINS
:
3110 case LTTNG_START_TRACE
:
3111 case LTTNG_STOP_TRACE
:
3112 case LTTNG_DATA_PENDING
:
3113 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3114 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3115 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3116 case LTTNG_SNAPSHOT_RECORD
:
3117 case LTTNG_SAVE_SESSION
:
3118 case LTTNG_SET_SESSION_SHM_PATH
:
3119 case LTTNG_REGENERATE_METADATA
:
3120 case LTTNG_REGENERATE_STATEDUMP
:
3121 case LTTNG_REGISTER_TRIGGER
:
3122 case LTTNG_UNREGISTER_TRIGGER
:
3123 case LTTNG_ROTATE_SESSION
:
3124 case LTTNG_ROTATE_PENDING
:
3131 if (opt_no_kernel
&& need_domain
3132 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3134 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3136 ret
= LTTNG_ERR_KERN_NA
;
3141 /* Deny register consumer if we already have a spawned consumer. */
3142 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3143 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3144 if (kconsumer_data
.pid
> 0) {
3145 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3146 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3149 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3153 * Check for command that don't needs to allocate a returned payload. We do
3154 * this here so we don't have to make the call for no payload at each
3157 switch(cmd_ctx
->lsm
->cmd_type
) {
3158 case LTTNG_LIST_SESSIONS
:
3159 case LTTNG_LIST_TRACEPOINTS
:
3160 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3161 case LTTNG_LIST_DOMAINS
:
3162 case LTTNG_LIST_CHANNELS
:
3163 case LTTNG_LIST_EVENTS
:
3164 case LTTNG_LIST_SYSCALLS
:
3165 case LTTNG_LIST_TRACKER_PIDS
:
3166 case LTTNG_DATA_PENDING
:
3167 case LTTNG_ROTATE_SESSION
:
3168 case LTTNG_ROTATE_PENDING
:
3171 /* Setup lttng message with no payload */
3172 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3174 /* This label does not try to unlock the session */
3175 goto init_setup_error
;
3179 /* Commands that DO NOT need a session. */
3180 switch (cmd_ctx
->lsm
->cmd_type
) {
3181 case LTTNG_CREATE_SESSION
:
3182 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3183 case LTTNG_CREATE_SESSION_LIVE
:
3184 case LTTNG_LIST_SESSIONS
:
3185 case LTTNG_LIST_TRACEPOINTS
:
3186 case LTTNG_LIST_SYSCALLS
:
3187 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3188 case LTTNG_SAVE_SESSION
:
3189 case LTTNG_REGISTER_TRIGGER
:
3190 case LTTNG_UNREGISTER_TRIGGER
:
3191 need_tracing_session
= 0;
3194 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3196 * We keep the session list lock across _all_ commands
3197 * for now, because the per-session lock does not
3198 * handle teardown properly.
3200 session_lock_list();
3201 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3202 if (cmd_ctx
->session
== NULL
) {
3203 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3206 /* Acquire lock for the session */
3207 session_lock(cmd_ctx
->session
);
3213 * Commands that need a valid session but should NOT create one if none
3214 * exists. Instead of creating one and destroying it when the command is
3215 * handled, process that right before so we save some round trip in useless
3218 switch (cmd_ctx
->lsm
->cmd_type
) {
3219 case LTTNG_DISABLE_CHANNEL
:
3220 case LTTNG_DISABLE_EVENT
:
3221 switch (cmd_ctx
->lsm
->domain
.type
) {
3222 case LTTNG_DOMAIN_KERNEL
:
3223 if (!cmd_ctx
->session
->kernel_session
) {
3224 ret
= LTTNG_ERR_NO_CHANNEL
;
3228 case LTTNG_DOMAIN_JUL
:
3229 case LTTNG_DOMAIN_LOG4J
:
3230 case LTTNG_DOMAIN_PYTHON
:
3231 case LTTNG_DOMAIN_UST
:
3232 if (!cmd_ctx
->session
->ust_session
) {
3233 ret
= LTTNG_ERR_NO_CHANNEL
;
3238 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3250 * Check domain type for specific "pre-action".
3252 switch (cmd_ctx
->lsm
->domain
.type
) {
3253 case LTTNG_DOMAIN_KERNEL
:
3255 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3259 /* Kernel tracer check */
3260 if (kernel_tracer_fd
== -1) {
3261 /* Basically, load kernel tracer modules */
3262 ret
= init_kernel_tracer();
3268 /* Consumer is in an ERROR state. Report back to client */
3269 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3270 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3274 /* Need a session for kernel command */
3275 if (need_tracing_session
) {
3276 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3277 ret
= create_kernel_session(cmd_ctx
->session
);
3279 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3284 /* Start the kernel consumer daemon */
3285 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3286 if (kconsumer_data
.pid
== 0 &&
3287 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3288 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3289 ret
= start_consumerd(&kconsumer_data
);
3291 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3294 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3296 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3300 * The consumer was just spawned so we need to add the socket to
3301 * the consumer output of the session if exist.
3303 ret
= consumer_create_socket(&kconsumer_data
,
3304 cmd_ctx
->session
->kernel_session
->consumer
);
3311 case LTTNG_DOMAIN_JUL
:
3312 case LTTNG_DOMAIN_LOG4J
:
3313 case LTTNG_DOMAIN_PYTHON
:
3314 case LTTNG_DOMAIN_UST
:
3316 if (!ust_app_supported()) {
3317 ret
= LTTNG_ERR_NO_UST
;
3320 /* Consumer is in an ERROR state. Report back to client */
3321 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3322 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3326 if (need_tracing_session
) {
3327 /* Create UST session if none exist. */
3328 if (cmd_ctx
->session
->ust_session
== NULL
) {
3329 ret
= create_ust_session(cmd_ctx
->session
,
3330 &cmd_ctx
->lsm
->domain
);
3331 if (ret
!= LTTNG_OK
) {
3336 /* Start the UST consumer daemons */
3338 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3339 if (consumerd64_bin
[0] != '\0' &&
3340 ustconsumer64_data
.pid
== 0 &&
3341 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3342 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3343 ret
= start_consumerd(&ustconsumer64_data
);
3345 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3346 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3350 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3351 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3353 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3357 * Setup socket for consumer 64 bit. No need for atomic access
3358 * since it was set above and can ONLY be set in this thread.
3360 ret
= consumer_create_socket(&ustconsumer64_data
,
3361 cmd_ctx
->session
->ust_session
->consumer
);
3367 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3368 if (consumerd32_bin
[0] != '\0' &&
3369 ustconsumer32_data
.pid
== 0 &&
3370 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3371 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3372 ret
= start_consumerd(&ustconsumer32_data
);
3374 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3375 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3379 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3380 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3382 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3386 * Setup socket for consumer 64 bit. No need for atomic access
3387 * since it was set above and can ONLY be set in this thread.
3389 ret
= consumer_create_socket(&ustconsumer32_data
,
3390 cmd_ctx
->session
->ust_session
->consumer
);
3402 /* Validate consumer daemon state when start/stop trace command */
3403 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3404 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3405 switch (cmd_ctx
->lsm
->domain
.type
) {
3406 case LTTNG_DOMAIN_NONE
:
3408 case LTTNG_DOMAIN_JUL
:
3409 case LTTNG_DOMAIN_LOG4J
:
3410 case LTTNG_DOMAIN_PYTHON
:
3411 case LTTNG_DOMAIN_UST
:
3412 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3413 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3417 case LTTNG_DOMAIN_KERNEL
:
3418 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3419 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3424 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3430 * Check that the UID or GID match that of the tracing session.
3431 * The root user can interact with all sessions.
3433 if (need_tracing_session
) {
3434 if (!session_access_ok(cmd_ctx
->session
,
3435 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3436 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3437 ret
= LTTNG_ERR_EPERM
;
3443 * Send relayd information to consumer as soon as we have a domain and a
3446 if (cmd_ctx
->session
&& need_domain
) {
3448 * Setup relayd if not done yet. If the relayd information was already
3449 * sent to the consumer, this call will gracefully return.
3451 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3452 if (ret
!= LTTNG_OK
) {
3457 /* Process by command type */
3458 switch (cmd_ctx
->lsm
->cmd_type
) {
3459 case LTTNG_ADD_CONTEXT
:
3462 * An LTTNG_ADD_CONTEXT command might have a supplementary
3463 * payload if the context being added is an application context.
3465 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3466 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3467 char *provider_name
= NULL
, *context_name
= NULL
;
3468 size_t provider_name_len
=
3469 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3470 size_t context_name_len
=
3471 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3473 if (provider_name_len
== 0 || context_name_len
== 0) {
3475 * Application provider and context names MUST
3478 ret
= -LTTNG_ERR_INVALID
;
3482 provider_name
= zmalloc(provider_name_len
+ 1);
3483 if (!provider_name
) {
3484 ret
= -LTTNG_ERR_NOMEM
;
3487 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3490 context_name
= zmalloc(context_name_len
+ 1);
3491 if (!context_name
) {
3492 ret
= -LTTNG_ERR_NOMEM
;
3493 goto error_add_context
;
3495 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3498 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3501 goto error_add_context
;
3504 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3507 goto error_add_context
;
3512 * cmd_add_context assumes ownership of the provider and context
3515 ret
= cmd_add_context(cmd_ctx
->session
,
3516 cmd_ctx
->lsm
->domain
.type
,
3517 cmd_ctx
->lsm
->u
.context
.channel_name
,
3518 &cmd_ctx
->lsm
->u
.context
.ctx
,
3519 kernel_poll_pipe
[1]);
3521 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3522 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3524 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3525 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3531 case LTTNG_DISABLE_CHANNEL
:
3533 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3534 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3537 case LTTNG_DISABLE_EVENT
:
3541 * FIXME: handle filter; for now we just receive the filter's
3542 * bytecode along with the filter expression which are sent by
3543 * liblttng-ctl and discard them.
3545 * This fixes an issue where the client may block while sending
3546 * the filter payload and encounter an error because the session
3547 * daemon closes the socket without ever handling this data.
3549 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3550 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3553 char data
[LTTNG_FILTER_MAX_LEN
];
3555 DBG("Discarding disable event command payload of size %zu", count
);
3557 ret
= lttcomm_recv_unix_sock(sock
, data
,
3558 count
> sizeof(data
) ? sizeof(data
) : count
);
3563 count
-= (size_t) ret
;
3566 /* FIXME: passing packed structure to non-packed pointer */
3567 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3568 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3569 &cmd_ctx
->lsm
->u
.disable
.event
);
3572 case LTTNG_ENABLE_CHANNEL
:
3574 cmd_ctx
->lsm
->u
.channel
.chan
.attr
.extended
.ptr
=
3575 (struct lttng_channel_extended
*) &cmd_ctx
->lsm
->u
.channel
.extended
;
3576 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3577 &cmd_ctx
->lsm
->u
.channel
.chan
,
3578 kernel_poll_pipe
[1]);
3581 case LTTNG_TRACK_PID
:
3583 ret
= cmd_track_pid(cmd_ctx
->session
,
3584 cmd_ctx
->lsm
->domain
.type
,
3585 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3588 case LTTNG_UNTRACK_PID
:
3590 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3591 cmd_ctx
->lsm
->domain
.type
,
3592 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3595 case LTTNG_ENABLE_EVENT
:
3597 struct lttng_event_exclusion
*exclusion
= NULL
;
3598 struct lttng_filter_bytecode
*bytecode
= NULL
;
3599 char *filter_expression
= NULL
;
3601 /* Handle exclusion events and receive it from the client. */
3602 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3603 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3605 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3606 (count
* LTTNG_SYMBOL_NAME_LEN
));
3608 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3612 DBG("Receiving var len exclusion event list from client ...");
3613 exclusion
->count
= count
;
3614 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3615 count
* LTTNG_SYMBOL_NAME_LEN
);
3617 DBG("Nothing recv() from client var len data... continuing");
3620 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3625 /* Get filter expression from client. */
3626 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3627 size_t expression_len
=
3628 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3630 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3631 ret
= LTTNG_ERR_FILTER_INVAL
;
3636 filter_expression
= zmalloc(expression_len
);
3637 if (!filter_expression
) {
3639 ret
= LTTNG_ERR_FILTER_NOMEM
;
3643 /* Receive var. len. data */
3644 DBG("Receiving var len filter's expression from client ...");
3645 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3648 DBG("Nothing recv() from client car len data... continuing");
3650 free(filter_expression
);
3652 ret
= LTTNG_ERR_FILTER_INVAL
;
3657 /* Handle filter and get bytecode from client. */
3658 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3659 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3661 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3662 ret
= LTTNG_ERR_FILTER_INVAL
;
3663 free(filter_expression
);
3668 bytecode
= zmalloc(bytecode_len
);
3670 free(filter_expression
);
3672 ret
= LTTNG_ERR_FILTER_NOMEM
;
3676 /* Receive var. len. data */
3677 DBG("Receiving var len filter's bytecode from client ...");
3678 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3680 DBG("Nothing recv() from client car len data... continuing");
3682 free(filter_expression
);
3685 ret
= LTTNG_ERR_FILTER_INVAL
;
3689 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3690 free(filter_expression
);
3693 ret
= LTTNG_ERR_FILTER_INVAL
;
3698 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3699 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3700 &cmd_ctx
->lsm
->u
.enable
.event
,
3701 filter_expression
, bytecode
, exclusion
,
3702 kernel_poll_pipe
[1]);
3705 case LTTNG_LIST_TRACEPOINTS
:
3707 struct lttng_event
*events
;
3710 session_lock_list();
3711 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3712 session_unlock_list();
3713 if (nb_events
< 0) {
3714 /* Return value is a negative lttng_error_code. */
3720 * Setup lttng message with payload size set to the event list size in
3721 * bytes and then copy list into the llm payload.
3723 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3724 sizeof(struct lttng_event
) * nb_events
);
3734 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3736 struct lttng_event_field
*fields
;
3739 session_lock_list();
3740 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3742 session_unlock_list();
3743 if (nb_fields
< 0) {
3744 /* Return value is a negative lttng_error_code. */
3750 * Setup lttng message with payload size set to the event list size in
3751 * bytes and then copy list into the llm payload.
3753 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3754 sizeof(struct lttng_event_field
) * nb_fields
);
3764 case LTTNG_LIST_SYSCALLS
:
3766 struct lttng_event
*events
;
3769 nb_events
= cmd_list_syscalls(&events
);
3770 if (nb_events
< 0) {
3771 /* Return value is a negative lttng_error_code. */
3777 * Setup lttng message with payload size set to the event list size in
3778 * bytes and then copy list into the llm payload.
3780 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3781 sizeof(struct lttng_event
) * nb_events
);
3791 case LTTNG_LIST_TRACKER_PIDS
:
3793 int32_t *pids
= NULL
;
3796 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3797 cmd_ctx
->lsm
->domain
.type
, &pids
);
3799 /* Return value is a negative lttng_error_code. */
3805 * Setup lttng message with payload size set to the event list size in
3806 * bytes and then copy list into the llm payload.
3808 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pids
,
3809 sizeof(int32_t) * nr_pids
);
3819 case LTTNG_SET_CONSUMER_URI
:
3822 struct lttng_uri
*uris
;
3824 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3825 len
= nb_uri
* sizeof(struct lttng_uri
);
3828 ret
= LTTNG_ERR_INVALID
;
3832 uris
= zmalloc(len
);
3834 ret
= LTTNG_ERR_FATAL
;
3838 /* Receive variable len data */
3839 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3840 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3842 DBG("No URIs received from client... continuing");
3844 ret
= LTTNG_ERR_SESSION_FAIL
;
3849 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3851 if (ret
!= LTTNG_OK
) {
3858 case LTTNG_START_TRACE
:
3860 ret
= cmd_start_trace(cmd_ctx
->session
);
3863 case LTTNG_STOP_TRACE
:
3865 ret
= cmd_stop_trace(cmd_ctx
->session
);
3868 case LTTNG_CREATE_SESSION
:
3871 struct lttng_uri
*uris
= NULL
;
3873 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3874 len
= nb_uri
* sizeof(struct lttng_uri
);
3877 uris
= zmalloc(len
);
3879 ret
= LTTNG_ERR_FATAL
;
3883 /* Receive variable len data */
3884 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3885 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3887 DBG("No URIs received from client... continuing");
3889 ret
= LTTNG_ERR_SESSION_FAIL
;
3894 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3895 DBG("Creating session with ONE network URI is a bad call");
3896 ret
= LTTNG_ERR_SESSION_FAIL
;
3902 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3903 &cmd_ctx
->creds
, 0);
3909 case LTTNG_DESTROY_SESSION
:
3911 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3913 /* Set session to NULL so we do not unlock it after free. */
3914 cmd_ctx
->session
= NULL
;
3917 case LTTNG_LIST_DOMAINS
:
3920 struct lttng_domain
*domains
= NULL
;
3922 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3924 /* Return value is a negative lttng_error_code. */
3929 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3930 nb_dom
* sizeof(struct lttng_domain
));
3940 case LTTNG_LIST_CHANNELS
:
3942 ssize_t payload_size
;
3943 struct lttng_channel
*channels
= NULL
;
3945 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3946 cmd_ctx
->session
, &channels
);
3947 if (payload_size
< 0) {
3948 /* Return value is a negative lttng_error_code. */
3949 ret
= -payload_size
;
3953 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3964 case LTTNG_LIST_EVENTS
:
3967 struct lttng_event
*events
= NULL
;
3968 struct lttcomm_event_command_header cmd_header
;
3971 memset(&cmd_header
, 0, sizeof(cmd_header
));
3972 /* Extended infos are included at the end of events */
3973 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3974 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3975 &events
, &total_size
);
3978 /* Return value is a negative lttng_error_code. */
3983 cmd_header
.nb_events
= nb_event
;
3984 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3985 &cmd_header
, sizeof(cmd_header
));
3995 case LTTNG_LIST_SESSIONS
:
3997 unsigned int nr_sessions
;
3998 void *sessions_payload
;
4001 session_lock_list();
4002 nr_sessions
= lttng_sessions_count(
4003 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4004 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4005 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
4006 sessions_payload
= zmalloc(payload_len
);
4008 if (!sessions_payload
) {
4009 session_unlock_list();
4014 cmd_list_lttng_sessions(sessions_payload
,
4015 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
4016 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
4017 session_unlock_list();
4019 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
4021 free(sessions_payload
);
4030 case LTTNG_REGISTER_CONSUMER
:
4032 struct consumer_data
*cdata
;
4034 switch (cmd_ctx
->lsm
->domain
.type
) {
4035 case LTTNG_DOMAIN_KERNEL
:
4036 cdata
= &kconsumer_data
;
4039 ret
= LTTNG_ERR_UND
;
4043 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
4044 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
4047 case LTTNG_DATA_PENDING
:
4050 uint8_t pending_ret_byte
;
4052 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
4057 * This function may returns 0 or 1 to indicate whether or not
4058 * there is data pending. In case of error, it should return an
4059 * LTTNG_ERR code. However, some code paths may still return
4060 * a nondescript error code, which we handle by returning an
4063 if (pending_ret
== 0 || pending_ret
== 1) {
4065 * ret will be set to LTTNG_OK at the end of
4068 } else if (pending_ret
< 0) {
4069 ret
= LTTNG_ERR_UNK
;
4076 pending_ret_byte
= (uint8_t) pending_ret
;
4078 /* 1 byte to return whether or not data is pending */
4079 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
4080 &pending_ret_byte
, 1);
4089 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
4091 struct lttcomm_lttng_output_id reply
;
4093 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
4094 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
4095 if (ret
!= LTTNG_OK
) {
4099 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
4105 /* Copy output list into message payload */
4109 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
4111 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
4112 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
4115 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
4118 struct lttng_snapshot_output
*outputs
= NULL
;
4120 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
4121 if (nb_output
< 0) {
4126 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4127 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4128 nb_output
* sizeof(struct lttng_snapshot_output
));
4138 case LTTNG_SNAPSHOT_RECORD
:
4140 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4141 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4142 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4145 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4148 struct lttng_uri
*uris
= NULL
;
4150 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4151 len
= nb_uri
* sizeof(struct lttng_uri
);
4154 uris
= zmalloc(len
);
4156 ret
= LTTNG_ERR_FATAL
;
4160 /* Receive variable len data */
4161 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4162 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4164 DBG("No URIs received from client... continuing");
4166 ret
= LTTNG_ERR_SESSION_FAIL
;
4171 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4172 DBG("Creating session with ONE network URI is a bad call");
4173 ret
= LTTNG_ERR_SESSION_FAIL
;
4179 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4180 nb_uri
, &cmd_ctx
->creds
);
4184 case LTTNG_CREATE_SESSION_LIVE
:
4187 struct lttng_uri
*uris
= NULL
;
4189 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4190 len
= nb_uri
* sizeof(struct lttng_uri
);
4193 uris
= zmalloc(len
);
4195 ret
= LTTNG_ERR_FATAL
;
4199 /* Receive variable len data */
4200 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4201 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4203 DBG("No URIs received from client... continuing");
4205 ret
= LTTNG_ERR_SESSION_FAIL
;
4210 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4211 DBG("Creating session with ONE network URI is a bad call");
4212 ret
= LTTNG_ERR_SESSION_FAIL
;
4218 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4219 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4223 case LTTNG_SAVE_SESSION
:
4225 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4229 case LTTNG_SET_SESSION_SHM_PATH
:
4231 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4232 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4235 case LTTNG_REGENERATE_METADATA
:
4237 ret
= cmd_regenerate_metadata(cmd_ctx
->session
);
4240 case LTTNG_REGENERATE_STATEDUMP
:
4242 ret
= cmd_regenerate_statedump(cmd_ctx
->session
);
4245 case LTTNG_REGISTER_TRIGGER
:
4247 ret
= cmd_register_trigger(cmd_ctx
, sock
,
4248 notification_thread_handle
);
4251 case LTTNG_UNREGISTER_TRIGGER
:
4253 ret
= cmd_unregister_trigger(cmd_ctx
, sock
,
4254 notification_thread_handle
);
4257 case LTTNG_ROTATE_SESSION
:
4259 struct lttng_rotate_session_return
*rotate_return
= NULL
;
4261 ret
= cmd_rotate_session(cmd_ctx
->session
, &rotate_return
);
4264 fprintf(stderr
, "cmd ret: %d\n", ret
);
4268 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, rotate_return
,
4269 sizeof(struct lttng_rotate_session_return
));
4270 free(rotate_return
);
4279 case LTTNG_ROTATE_PENDING
:
4281 struct lttng_rotate_pending_return
*pending_return
= NULL
;
4283 ret
= cmd_rotate_pending(cmd_ctx
->session
, &pending_return
,
4284 cmd_ctx
->lsm
->u
.rotate_pending
.rotate_id
);
4290 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, pending_return
,
4291 sizeof(struct lttng_rotate_session_handle
));
4292 free(pending_return
);
4302 ret
= LTTNG_ERR_UND
;
4307 if (cmd_ctx
->llm
== NULL
) {
4308 DBG("Missing llm structure. Allocating one.");
4309 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4313 /* Set return code */
4314 cmd_ctx
->llm
->ret_code
= ret
;
4315 fprintf(stderr
, "llm ret: %d\n", ret
);
4317 if (cmd_ctx
->session
) {
4318 session_unlock(cmd_ctx
->session
);
4320 if (need_tracing_session
) {
4321 session_unlock_list();
4324 assert(!rcu_read_ongoing());
4329 * Thread managing health check socket.
4331 static void *thread_manage_health(void *data
)
4333 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4334 uint32_t revents
, nb_fd
;
4335 struct lttng_poll_event events
;
4336 struct health_comm_msg msg
;
4337 struct health_comm_reply reply
;
4339 DBG("[thread] Manage health check started");
4341 rcu_register_thread();
4343 /* We might hit an error path before this is created. */
4344 lttng_poll_init(&events
);
4346 /* Create unix socket */
4347 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
4349 ERR("Unable to create health check Unix socket");
4354 /* lttng health client socket path permissions */
4355 ret
= chown(health_unix_sock_path
, 0,
4356 utils_get_group_id(tracing_group_name
));
4358 ERR("Unable to set group on %s", health_unix_sock_path
);
4363 ret
= chmod(health_unix_sock_path
,
4364 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4366 ERR("Unable to set permissions on %s", health_unix_sock_path
);
4373 * Set the CLOEXEC flag. Return code is useless because either way, the
4376 (void) utils_set_fd_cloexec(sock
);
4378 ret
= lttcomm_listen_unix_sock(sock
);
4384 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4385 * more will be added to this poll set.
4387 ret
= sessiond_set_thread_pollset(&events
, 2);
4392 /* Add the application registration socket */
4393 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4398 sessiond_notify_ready();
4401 DBG("Health check ready");
4403 /* Inifinite blocking call, waiting for transmission */
4405 ret
= lttng_poll_wait(&events
, -1);
4408 * Restart interrupted system call.
4410 if (errno
== EINTR
) {
4418 for (i
= 0; i
< nb_fd
; i
++) {
4419 /* Fetch once the poll data */
4420 revents
= LTTNG_POLL_GETEV(&events
, i
);
4421 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4424 /* No activity for this FD (poll implementation). */
4428 /* Thread quit pipe has been closed. Killing thread. */
4429 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4435 /* Event on the registration socket */
4436 if (pollfd
== sock
) {
4437 if (revents
& LPOLLIN
) {
4439 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4440 ERR("Health socket poll error");
4443 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4449 new_sock
= lttcomm_accept_unix_sock(sock
);
4455 * Set the CLOEXEC flag. Return code is useless because either way, the
4458 (void) utils_set_fd_cloexec(new_sock
);
4460 DBG("Receiving data from client for health...");
4461 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4463 DBG("Nothing recv() from client... continuing");
4464 ret
= close(new_sock
);
4471 rcu_thread_online();
4473 memset(&reply
, 0, sizeof(reply
));
4474 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4476 * health_check_state returns 0 if health is
4479 if (!health_check_state(health_sessiond
, i
)) {
4480 reply
.ret_code
|= 1ULL << i
;
4484 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4486 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4488 ERR("Failed to send health data back to client");
4491 /* End of transmission */
4492 ret
= close(new_sock
);
4501 ERR("Health error occurred in %s", __func__
);
4503 DBG("Health check thread dying");
4504 unlink(health_unix_sock_path
);
4512 lttng_poll_clean(&events
);
4514 rcu_unregister_thread();
4519 * This thread manage all clients request using the unix client socket for
4522 static void *thread_manage_clients(void *data
)
4524 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4526 uint32_t revents
, nb_fd
;
4527 struct command_ctx
*cmd_ctx
= NULL
;
4528 struct lttng_poll_event events
;
4530 DBG("[thread] Manage client started");
4532 rcu_register_thread();
4534 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4536 health_code_update();
4538 ret
= lttcomm_listen_unix_sock(client_sock
);
4544 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4545 * more will be added to this poll set.
4547 ret
= sessiond_set_thread_pollset(&events
, 2);
4549 goto error_create_poll
;
4552 /* Add the application registration socket */
4553 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4558 sessiond_notify_ready();
4559 ret
= sem_post(&load_info
->message_thread_ready
);
4561 PERROR("sem_post message_thread_ready");
4565 /* This testpoint is after we signal readiness to the parent. */
4566 if (testpoint(sessiond_thread_manage_clients
)) {
4570 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4574 health_code_update();
4577 DBG("Accepting client command ...");
4579 /* Inifinite blocking call, waiting for transmission */
4581 health_poll_entry();
4582 ret
= lttng_poll_wait(&events
, -1);
4586 * Restart interrupted system call.
4588 if (errno
== EINTR
) {
4596 for (i
= 0; i
< nb_fd
; i
++) {
4597 /* Fetch once the poll data */
4598 revents
= LTTNG_POLL_GETEV(&events
, i
);
4599 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4601 health_code_update();
4604 /* No activity for this FD (poll implementation). */
4608 /* Thread quit pipe has been closed. Killing thread. */
4609 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4615 /* Event on the registration socket */
4616 if (pollfd
== client_sock
) {
4617 if (revents
& LPOLLIN
) {
4619 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4620 ERR("Client socket poll error");
4623 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4629 DBG("Wait for client response");
4631 health_code_update();
4633 sock
= lttcomm_accept_unix_sock(client_sock
);
4639 * Set the CLOEXEC flag. Return code is useless because either way, the
4642 (void) utils_set_fd_cloexec(sock
);
4644 /* Set socket option for credentials retrieval */
4645 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4650 /* Allocate context command to process the client request */
4651 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4652 if (cmd_ctx
== NULL
) {
4653 PERROR("zmalloc cmd_ctx");
4657 /* Allocate data buffer for reception */
4658 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4659 if (cmd_ctx
->lsm
== NULL
) {
4660 PERROR("zmalloc cmd_ctx->lsm");
4664 cmd_ctx
->llm
= NULL
;
4665 cmd_ctx
->session
= NULL
;
4667 health_code_update();
4670 * Data is received from the lttng client. The struct
4671 * lttcomm_session_msg (lsm) contains the command and data request of
4674 DBG("Receiving data from client ...");
4675 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4676 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4678 DBG("Nothing recv() from client... continuing");
4684 clean_command_ctx(&cmd_ctx
);
4688 health_code_update();
4690 // TODO: Validate cmd_ctx including sanity check for
4691 // security purpose.
4693 rcu_thread_online();
4695 * This function dispatch the work to the kernel or userspace tracer
4696 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4697 * informations for the client. The command context struct contains
4698 * everything this function may needs.
4700 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4701 rcu_thread_offline();
4709 * TODO: Inform client somehow of the fatal error. At
4710 * this point, ret < 0 means that a zmalloc failed
4711 * (ENOMEM). Error detected but still accept
4712 * command, unless a socket error has been
4715 clean_command_ctx(&cmd_ctx
);
4719 health_code_update();
4721 DBG("Sending response (size: %d, retcode: %s (%d))",
4722 cmd_ctx
->lttng_msg_size
,
4723 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4724 cmd_ctx
->llm
->ret_code
);
4725 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4727 ERR("Failed to send data back to client");
4730 /* End of transmission */
4737 clean_command_ctx(&cmd_ctx
);
4739 health_code_update();
4751 lttng_poll_clean(&events
);
4752 clean_command_ctx(&cmd_ctx
);
4756 unlink(client_unix_sock_path
);
4757 if (client_sock
>= 0) {
4758 ret
= close(client_sock
);
4766 ERR("Health error occurred in %s", __func__
);
4769 health_unregister(health_sessiond
);
4771 DBG("Client thread dying");
4773 rcu_unregister_thread();
4776 * Since we are creating the consumer threads, we own them, so we need
4777 * to join them before our thread exits.
4779 ret
= join_consumer_thread(&kconsumer_data
);
4782 PERROR("join_consumer");
4785 ret
= join_consumer_thread(&ustconsumer32_data
);
4788 PERROR("join_consumer ust32");
4791 ret
= join_consumer_thread(&ustconsumer64_data
);
4794 PERROR("join_consumer ust64");
4799 static int string_match(const char *str1
, const char *str2
)
4801 return (str1
&& str2
) && !strcmp(str1
, str2
);
4805 * Take an option from the getopt output and set it in the right variable to be
4808 * Return 0 on success else a negative value.
4810 static int set_option(int opt
, const char *arg
, const char *optname
)
4814 if (string_match(optname
, "client-sock") || opt
== 'c') {
4815 if (!arg
|| *arg
== '\0') {
4819 if (lttng_is_setuid_setgid()) {
4820 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4821 "-c, --client-sock");
4823 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4825 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4826 if (!arg
|| *arg
== '\0') {
4830 if (lttng_is_setuid_setgid()) {
4831 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4834 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4836 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4838 } else if (string_match(optname
, "background") || opt
== 'b') {
4840 } else if (string_match(optname
, "group") || opt
== 'g') {
4841 if (!arg
|| *arg
== '\0') {
4845 if (lttng_is_setuid_setgid()) {
4846 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4850 * If the override option is set, the pointer points to a
4851 * *non* const thus freeing it even though the variable type is
4854 if (tracing_group_name_override
) {
4855 free((void *) tracing_group_name
);
4857 tracing_group_name
= strdup(arg
);
4858 if (!tracing_group_name
) {
4862 tracing_group_name_override
= 1;
4864 } else if (string_match(optname
, "help") || opt
== 'h') {
4865 ret
= utils_show_help(8, "lttng-sessiond", help_msg
);
4867 ERR("Cannot show --help for `lttng-sessiond`");
4870 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4871 } else if (string_match(optname
, "version") || opt
== 'V') {
4872 fprintf(stdout
, "%s\n", VERSION
);
4874 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4876 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4877 if (!arg
|| *arg
== '\0') {
4881 if (lttng_is_setuid_setgid()) {
4882 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4883 "--kconsumerd-err-sock");
4885 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4887 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4888 if (!arg
|| *arg
== '\0') {
4892 if (lttng_is_setuid_setgid()) {
4893 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4894 "--kconsumerd-cmd-sock");
4896 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4898 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4899 if (!arg
|| *arg
== '\0') {
4903 if (lttng_is_setuid_setgid()) {
4904 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4905 "--ustconsumerd64-err-sock");
4907 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4909 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4910 if (!arg
|| *arg
== '\0') {
4914 if (lttng_is_setuid_setgid()) {
4915 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4916 "--ustconsumerd64-cmd-sock");
4918 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4920 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4921 if (!arg
|| *arg
== '\0') {
4925 if (lttng_is_setuid_setgid()) {
4926 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4927 "--ustconsumerd32-err-sock");
4929 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4931 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4932 if (!arg
|| *arg
== '\0') {
4936 if (lttng_is_setuid_setgid()) {
4937 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4938 "--ustconsumerd32-cmd-sock");
4940 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4942 } else if (string_match(optname
, "no-kernel")) {
4944 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4945 lttng_opt_quiet
= 1;
4946 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4947 /* Verbose level can increase using multiple -v */
4949 /* Value obtained from config file */
4950 lttng_opt_verbose
= config_parse_value(arg
);
4952 /* -v used on command line */
4953 lttng_opt_verbose
++;
4955 /* Clamp value to [0, 3] */
4956 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4957 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4958 } else if (string_match(optname
, "verbose-consumer")) {
4960 opt_verbose_consumer
= config_parse_value(arg
);
4962 opt_verbose_consumer
++;
4964 } else if (string_match(optname
, "consumerd32-path")) {
4965 if (!arg
|| *arg
== '\0') {
4969 if (lttng_is_setuid_setgid()) {
4970 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4971 "--consumerd32-path");
4973 if (consumerd32_bin_override
) {
4974 free((void *) consumerd32_bin
);
4976 consumerd32_bin
= strdup(arg
);
4977 if (!consumerd32_bin
) {
4981 consumerd32_bin_override
= 1;
4983 } else if (string_match(optname
, "consumerd32-libdir")) {
4984 if (!arg
|| *arg
== '\0') {
4988 if (lttng_is_setuid_setgid()) {
4989 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4990 "--consumerd32-libdir");
4992 if (consumerd32_libdir_override
) {
4993 free((void *) consumerd32_libdir
);
4995 consumerd32_libdir
= strdup(arg
);
4996 if (!consumerd32_libdir
) {
5000 consumerd32_libdir_override
= 1;
5002 } else if (string_match(optname
, "consumerd64-path")) {
5003 if (!arg
|| *arg
== '\0') {
5007 if (lttng_is_setuid_setgid()) {
5008 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5009 "--consumerd64-path");
5011 if (consumerd64_bin_override
) {
5012 free((void *) consumerd64_bin
);
5014 consumerd64_bin
= strdup(arg
);
5015 if (!consumerd64_bin
) {
5019 consumerd64_bin_override
= 1;
5021 } else if (string_match(optname
, "consumerd64-libdir")) {
5022 if (!arg
|| *arg
== '\0') {
5026 if (lttng_is_setuid_setgid()) {
5027 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5028 "--consumerd64-libdir");
5030 if (consumerd64_libdir_override
) {
5031 free((void *) consumerd64_libdir
);
5033 consumerd64_libdir
= strdup(arg
);
5034 if (!consumerd64_libdir
) {
5038 consumerd64_libdir_override
= 1;
5040 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
5041 if (!arg
|| *arg
== '\0') {
5045 if (lttng_is_setuid_setgid()) {
5046 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5050 opt_pidfile
= strdup(arg
);
5056 } else if (string_match(optname
, "agent-tcp-port")) {
5057 if (!arg
|| *arg
== '\0') {
5061 if (lttng_is_setuid_setgid()) {
5062 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5063 "--agent-tcp-port");
5068 v
= strtoul(arg
, NULL
, 0);
5069 if (errno
!= 0 || !isdigit(arg
[0])) {
5070 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
5073 if (v
== 0 || v
>= 65535) {
5074 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
5077 agent_tcp_port
= (uint32_t) v
;
5078 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
5080 } else if (string_match(optname
, "load") || opt
== 'l') {
5081 if (!arg
|| *arg
== '\0') {
5085 if (lttng_is_setuid_setgid()) {
5086 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5089 free(opt_load_session_path
);
5090 opt_load_session_path
= strdup(arg
);
5091 if (!opt_load_session_path
) {
5096 } else if (string_match(optname
, "kmod-probes")) {
5097 if (!arg
|| *arg
== '\0') {
5101 if (lttng_is_setuid_setgid()) {
5102 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5105 free(kmod_probes_list
);
5106 kmod_probes_list
= strdup(arg
);
5107 if (!kmod_probes_list
) {
5112 } else if (string_match(optname
, "extra-kmod-probes")) {
5113 if (!arg
|| *arg
== '\0') {
5117 if (lttng_is_setuid_setgid()) {
5118 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5119 "--extra-kmod-probes");
5121 free(kmod_extra_probes_list
);
5122 kmod_extra_probes_list
= strdup(arg
);
5123 if (!kmod_extra_probes_list
) {
5128 } else if (string_match(optname
, "config") || opt
== 'f') {
5129 /* This is handled in set_options() thus silent skip. */
5132 /* Unknown option or other error.
5133 * Error is printed by getopt, just return */
5138 if (ret
== -EINVAL
) {
5139 const char *opt_name
= "unknown";
5142 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5144 if (opt
== long_options
[i
].val
) {
5145 opt_name
= long_options
[i
].name
;
5150 WARN("Invalid argument provided for option \"%s\", using default value.",
5158 * config_entry_handler_cb used to handle options read from a config file.
5159 * See config_entry_handler_cb comment in common/config/session-config.h for the
5160 * return value conventions.
5162 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5166 if (!entry
|| !entry
->name
|| !entry
->value
) {
5171 /* Check if the option is to be ignored */
5172 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5173 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5178 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5181 /* Ignore if not fully matched. */
5182 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5187 * If the option takes no argument on the command line, we have to
5188 * check if the value is "true". We support non-zero numeric values,
5191 if (!long_options
[i
].has_arg
) {
5192 ret
= config_parse_value(entry
->value
);
5195 WARN("Invalid configuration value \"%s\" for option %s",
5196 entry
->value
, entry
->name
);
5198 /* False, skip boolean config option. */
5203 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5207 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5214 * daemon configuration loading and argument parsing
5216 static int set_options(int argc
, char **argv
)
5218 int ret
= 0, c
= 0, option_index
= 0;
5219 int orig_optopt
= optopt
, orig_optind
= optind
;
5221 const char *config_path
= NULL
;
5223 optstring
= utils_generate_optstring(long_options
,
5224 sizeof(long_options
) / sizeof(struct option
));
5230 /* Check for the --config option */
5231 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5232 &option_index
)) != -1) {
5236 } else if (c
!= 'f') {
5237 /* if not equal to --config option. */
5241 if (lttng_is_setuid_setgid()) {
5242 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5245 config_path
= utils_expand_path(optarg
);
5247 ERR("Failed to resolve path: %s", optarg
);
5252 ret
= config_get_section_entries(config_path
, config_section_name
,
5253 config_entry_handler
, NULL
);
5256 ERR("Invalid configuration option at line %i", ret
);
5262 /* Reset getopt's global state */
5263 optopt
= orig_optopt
;
5264 optind
= orig_optind
;
5268 * getopt_long() will not set option_index if it encounters a
5271 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5278 * Pass NULL as the long option name if popt left the index
5281 ret
= set_option(c
, optarg
,
5282 option_index
< 0 ? NULL
:
5283 long_options
[option_index
].name
);
5295 * Creates the two needed socket by the daemon.
5296 * apps_sock - The communication socket for all UST apps.
5297 * client_sock - The communication of the cli tool (lttng).
5299 static int init_daemon_socket(void)
5304 old_umask
= umask(0);
5306 /* Create client tool unix socket */
5307 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5308 if (client_sock
< 0) {
5309 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5314 /* Set the cloexec flag */
5315 ret
= utils_set_fd_cloexec(client_sock
);
5317 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5318 "Continuing but note that the consumer daemon will have a "
5319 "reference to this socket on exec()", client_sock
);
5322 /* File permission MUST be 660 */
5323 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5325 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5330 /* Create the application unix socket */
5331 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5332 if (apps_sock
< 0) {
5333 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5338 /* Set the cloexec flag */
5339 ret
= utils_set_fd_cloexec(apps_sock
);
5341 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5342 "Continuing but note that the consumer daemon will have a "
5343 "reference to this socket on exec()", apps_sock
);
5346 /* File permission MUST be 666 */
5347 ret
= chmod(apps_unix_sock_path
,
5348 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5350 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5355 DBG3("Session daemon client socket %d and application socket %d created",
5356 client_sock
, apps_sock
);
5364 * Check if the global socket is available, and if a daemon is answering at the
5365 * other side. If yes, error is returned.
5367 static int check_existing_daemon(void)
5369 /* Is there anybody out there ? */
5370 if (lttng_session_daemon_alive()) {
5378 * Set the tracing group gid onto the client socket.
5380 * Race window between mkdir and chown is OK because we are going from more
5381 * permissive (root.root) to less permissive (root.tracing).
5383 static int set_permissions(char *rundir
)
5388 gid
= utils_get_group_id(tracing_group_name
);
5390 /* Set lttng run dir */
5391 ret
= chown(rundir
, 0, gid
);
5393 ERR("Unable to set group on %s", rundir
);
5398 * Ensure all applications and tracing group can search the run
5399 * dir. Allow everyone to read the directory, since it does not
5400 * buy us anything to hide its content.
5402 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5404 ERR("Unable to set permissions on %s", rundir
);
5408 /* lttng client socket path */
5409 ret
= chown(client_unix_sock_path
, 0, gid
);
5411 ERR("Unable to set group on %s", client_unix_sock_path
);
5415 /* kconsumer error socket path */
5416 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5418 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5422 /* 64-bit ustconsumer error socket path */
5423 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5425 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5429 /* 32-bit ustconsumer compat32 error socket path */
5430 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5432 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5436 DBG("All permissions are set");
5442 * Create the lttng run directory needed for all global sockets and pipe.
5444 static int create_lttng_rundir(const char *rundir
)
5448 DBG3("Creating LTTng run directory: %s", rundir
);
5450 ret
= mkdir(rundir
, S_IRWXU
);
5452 if (errno
!= EEXIST
) {
5453 ERR("Unable to create %s", rundir
);
5465 * Setup sockets and directory needed by the kconsumerd communication with the
5468 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5472 char path
[PATH_MAX
];
5474 switch (consumer_data
->type
) {
5475 case LTTNG_CONSUMER_KERNEL
:
5476 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5478 case LTTNG_CONSUMER64_UST
:
5479 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5481 case LTTNG_CONSUMER32_UST
:
5482 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5485 ERR("Consumer type unknown");
5490 DBG2("Creating consumer directory: %s", path
);
5492 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5493 if (ret
< 0 && errno
!= EEXIST
) {
5495 ERR("Failed to create %s", path
);
5499 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
5501 ERR("Unable to set group on %s", path
);
5507 /* Create the kconsumerd error unix socket */
5508 consumer_data
->err_sock
=
5509 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5510 if (consumer_data
->err_sock
< 0) {
5511 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5517 * Set the CLOEXEC flag. Return code is useless because either way, the
5520 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5522 PERROR("utils_set_fd_cloexec");
5523 /* continue anyway */
5526 /* File permission MUST be 660 */
5527 ret
= chmod(consumer_data
->err_unix_sock_path
,
5528 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5530 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5540 * Signal handler for the daemon
5542 * Simply stop all worker threads, leaving main() return gracefully after
5543 * joining all threads and calling cleanup().
5545 static void sighandler(int sig
)
5549 DBG("SIGINT caught");
5553 DBG("SIGTERM caught");
5557 CMM_STORE_SHARED(recv_child_signal
, 1);
5565 * Setup signal handler for :
5566 * SIGINT, SIGTERM, SIGPIPE
5568 static int set_signal_handler(void)
5571 struct sigaction sa
;
5574 if ((ret
= sigemptyset(&sigset
)) < 0) {
5575 PERROR("sigemptyset");
5579 sa
.sa_mask
= sigset
;
5582 sa
.sa_handler
= sighandler
;
5583 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5584 PERROR("sigaction");
5588 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5589 PERROR("sigaction");
5593 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5594 PERROR("sigaction");
5598 sa
.sa_handler
= SIG_IGN
;
5599 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5600 PERROR("sigaction");
5604 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5610 * Set open files limit to unlimited. This daemon can open a large number of
5611 * file descriptors in order to consume multiple kernel traces.
5613 static void set_ulimit(void)
5618 /* The kernel does not allow an infinite limit for open files */
5619 lim
.rlim_cur
= 65535;
5620 lim
.rlim_max
= 65535;
5622 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5624 PERROR("failed to set open files limit");
5629 * Write pidfile using the rundir and opt_pidfile.
5631 static int write_pidfile(void)
5634 char pidfile_path
[PATH_MAX
];
5639 if (lttng_strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
))) {
5644 /* Build pidfile path from rundir and opt_pidfile. */
5645 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
5646 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
5648 PERROR("snprintf pidfile path");
5654 * Create pid file in rundir.
5656 ret
= utils_create_pid_file(getpid(), pidfile_path
);
5662 * Create lockfile using the rundir and return its fd.
5664 static int create_lockfile(void)
5667 char lockfile_path
[PATH_MAX
];
5669 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
5674 ret
= utils_create_lock_file(lockfile_path
);
5680 * Write agent TCP port using the rundir.
5682 static int write_agent_port(void)
5685 char path
[PATH_MAX
];
5689 ret
= snprintf(path
, sizeof(path
), "%s/"
5690 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
5692 PERROR("snprintf agent port path");
5697 * Create TCP agent port file in rundir.
5699 ret
= utils_create_pid_file(agent_tcp_port
, path
);
5708 int main(int argc
, char **argv
)
5710 int ret
= 0, retval
= 0;
5712 const char *home_path
, *env_app_timeout
;
5713 struct lttng_pipe
*ust32_channel_monitor_pipe
= NULL
,
5714 *ust64_channel_monitor_pipe
= NULL
,
5715 *kernel_channel_monitor_pipe
= NULL
;
5716 bool notification_thread_running
= false;
5717 struct lttng_pipe
*ust32_channel_rotate_pipe
= NULL
,
5718 *ust64_channel_rotate_pipe
= NULL
,
5719 *kernel_channel_rotate_pipe
= NULL
;
5721 init_kernel_workarounds();
5723 rcu_register_thread();
5725 if (set_signal_handler()) {
5727 goto exit_set_signal_handler
;
5730 setup_consumerd_path();
5732 page_size
= sysconf(_SC_PAGESIZE
);
5733 if (page_size
< 0) {
5734 PERROR("sysconf _SC_PAGESIZE");
5735 page_size
= LONG_MAX
;
5736 WARN("Fallback page size to %ld", page_size
);
5740 * Parse arguments and load the daemon configuration file.
5742 * We have an exit_options exit path to free memory reserved by
5743 * set_options. This is needed because the rest of sessiond_cleanup()
5744 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5745 * depends on set_options.
5748 if (set_options(argc
, argv
)) {
5754 if (opt_daemon
|| opt_background
) {
5757 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5765 * We are in the child. Make sure all other file descriptors are
5766 * closed, in case we are called with more opened file
5767 * descriptors than the standard ones.
5769 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5774 if (run_as_create_worker(argv
[0]) < 0) {
5775 goto exit_create_run_as_worker_cleanup
;
5779 * Starting from here, we can create threads. This needs to be after
5780 * lttng_daemonize due to RCU.
5784 * Initialize the health check subsystem. This call should set the
5785 * appropriate time values.
5787 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5788 if (!health_sessiond
) {
5789 PERROR("health_app_create error");
5791 goto exit_health_sessiond_cleanup
;
5794 /* Create thread to clean up RCU hash tables */
5795 if (init_ht_cleanup_thread(&ht_cleanup_thread
)) {
5797 goto exit_ht_cleanup
;
5800 /* Create thread quit pipe */
5801 if (init_thread_quit_pipe()) {
5803 goto exit_init_data
;
5806 /* Check if daemon is UID = 0 */
5807 is_root
= !getuid();
5810 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5813 goto exit_init_data
;
5816 /* Create global run dir with root access */
5817 if (create_lttng_rundir(rundir
)) {
5819 goto exit_init_data
;
5822 if (strlen(apps_unix_sock_path
) == 0) {
5823 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5824 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5827 goto exit_init_data
;
5831 if (strlen(client_unix_sock_path
) == 0) {
5832 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5833 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5836 goto exit_init_data
;
5840 /* Set global SHM for ust */
5841 if (strlen(wait_shm_path
) == 0) {
5842 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5843 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5846 goto exit_init_data
;
5850 if (strlen(health_unix_sock_path
) == 0) {
5851 ret
= snprintf(health_unix_sock_path
,
5852 sizeof(health_unix_sock_path
),
5853 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5856 goto exit_init_data
;
5860 /* Setup kernel consumerd path */
5861 ret
= snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5862 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5865 goto exit_init_data
;
5867 ret
= snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5868 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5871 goto exit_init_data
;
5874 DBG2("Kernel consumer err path: %s",
5875 kconsumer_data
.err_unix_sock_path
);
5876 DBG2("Kernel consumer cmd path: %s",
5877 kconsumer_data
.cmd_unix_sock_path
);
5878 kernel_channel_monitor_pipe
= lttng_pipe_open(0);
5879 if (!kernel_channel_monitor_pipe
) {
5880 ERR("Failed to create kernel consumer channel monitor pipe");
5882 goto exit_init_data
;
5884 kconsumer_data
.channel_monitor_pipe
=
5885 lttng_pipe_release_writefd(
5886 kernel_channel_monitor_pipe
);
5887 if (kconsumer_data
.channel_monitor_pipe
< 0) {
5889 goto exit_init_data
;
5891 kernel_channel_rotate_pipe
= lttng_pipe_open(0);
5892 if (!kernel_channel_rotate_pipe
) {
5893 ERR("Failed to create kernel consumer channel rotate pipe");
5895 goto exit_init_data
;
5897 kconsumer_data
.channel_rotate_pipe
=
5898 lttng_pipe_release_writefd(
5899 kernel_channel_rotate_pipe
);
5900 if (kconsumer_data
.channel_rotate_pipe
< 0) {
5902 goto exit_init_data
;
5905 home_path
= utils_get_home_dir();
5906 if (home_path
== NULL
) {
5907 /* TODO: Add --socket PATH option */
5908 ERR("Can't get HOME directory for sockets creation.");
5910 goto exit_init_data
;
5914 * Create rundir from home path. This will create something like
5917 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5920 goto exit_init_data
;
5923 if (create_lttng_rundir(rundir
)) {
5925 goto exit_init_data
;
5928 if (strlen(apps_unix_sock_path
) == 0) {
5929 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5930 DEFAULT_HOME_APPS_UNIX_SOCK
,
5934 goto exit_init_data
;
5938 /* Set the cli tool unix socket path */
5939 if (strlen(client_unix_sock_path
) == 0) {
5940 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5941 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
5945 goto exit_init_data
;
5949 /* Set global SHM for ust */
5950 if (strlen(wait_shm_path
) == 0) {
5951 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5952 DEFAULT_HOME_APPS_WAIT_SHM_PATH
,
5956 goto exit_init_data
;
5960 /* Set health check Unix path */
5961 if (strlen(health_unix_sock_path
) == 0) {
5962 ret
= snprintf(health_unix_sock_path
,
5963 sizeof(health_unix_sock_path
),
5964 DEFAULT_HOME_HEALTH_UNIX_SOCK
,
5968 goto exit_init_data
;
5973 lockfile_fd
= create_lockfile();
5974 if (lockfile_fd
< 0) {
5976 goto exit_init_data
;
5979 /* Set consumer initial state */
5980 kernel_consumerd_state
= CONSUMER_STOPPED
;
5981 ust_consumerd_state
= CONSUMER_STOPPED
;
5983 DBG("Client socket path %s", client_unix_sock_path
);
5984 DBG("Application socket path %s", apps_unix_sock_path
);
5985 DBG("Application wait path %s", wait_shm_path
);
5986 DBG("LTTng run directory path: %s", rundir
);
5988 /* 32 bits consumerd path setup */
5989 ret
= snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5990 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5992 PERROR("snprintf 32-bit consumer error socket path");
5994 goto exit_init_data
;
5996 ret
= snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5997 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5999 PERROR("snprintf 32-bit consumer command socket path");
6001 goto exit_init_data
;
6004 DBG2("UST consumer 32 bits err path: %s",
6005 ustconsumer32_data
.err_unix_sock_path
);
6006 DBG2("UST consumer 32 bits cmd path: %s",
6007 ustconsumer32_data
.cmd_unix_sock_path
);
6008 ust32_channel_monitor_pipe
= lttng_pipe_open(0);
6009 if (!ust32_channel_monitor_pipe
) {
6010 ERR("Failed to create 32-bit user space consumer channel monitor pipe");
6012 goto exit_init_data
;
6014 ustconsumer32_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6015 ust32_channel_monitor_pipe
);
6016 if (ustconsumer32_data
.channel_monitor_pipe
< 0) {
6018 goto exit_init_data
;
6020 ust32_channel_rotate_pipe
= lttng_pipe_open(0);
6021 if (!ust32_channel_rotate_pipe
) {
6022 ERR("Failed to create 32-bit user space consumer channel rotate pipe");
6024 goto exit_init_data
;
6026 ustconsumer32_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6027 ust32_channel_rotate_pipe
);
6028 if (ustconsumer32_data
.channel_rotate_pipe
< 0) {
6030 goto exit_init_data
;
6033 /* 64 bits consumerd path setup */
6034 ret
= snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
6035 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
6037 PERROR("snprintf 64-bit consumer error socket path");
6039 goto exit_init_data
;
6041 ret
= snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
6042 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
6044 PERROR("snprintf 64-bit consumer command socket path");
6046 goto exit_init_data
;
6049 DBG2("UST consumer 64 bits err path: %s",
6050 ustconsumer64_data
.err_unix_sock_path
);
6051 DBG2("UST consumer 64 bits cmd path: %s",
6052 ustconsumer64_data
.cmd_unix_sock_path
);
6053 ust64_channel_monitor_pipe
= lttng_pipe_open(0);
6054 if (!ust64_channel_monitor_pipe
) {
6055 ERR("Failed to create 64-bit user space consumer channel monitor pipe");
6057 goto exit_init_data
;
6059 ustconsumer64_data
.channel_monitor_pipe
= lttng_pipe_release_writefd(
6060 ust64_channel_monitor_pipe
);
6061 if (ustconsumer64_data
.channel_monitor_pipe
< 0) {
6063 goto exit_init_data
;
6065 ust64_channel_rotate_pipe
= lttng_pipe_open(0);
6066 if (!ust64_channel_rotate_pipe
) {
6067 ERR("Failed to create 64-bit user space consumer channel rotate pipe");
6069 goto exit_init_data
;
6071 ustconsumer64_data
.channel_rotate_pipe
= lttng_pipe_release_writefd(
6072 ust64_channel_rotate_pipe
);
6073 if (ustconsumer64_data
.channel_rotate_pipe
< 0) {
6075 goto exit_init_data
;
6079 * See if daemon already exist.
6081 if (check_existing_daemon()) {
6082 ERR("Already running daemon.\n");
6084 * We do not goto exit because we must not cleanup()
6085 * because a daemon is already running.
6088 goto exit_init_data
;
6092 * Init UST app hash table. Alloc hash table before this point since
6093 * cleanup() can get called after that point.
6095 if (ust_app_ht_alloc()) {
6096 ERR("Failed to allocate UST app hash table");
6098 goto exit_init_data
;
6102 * Initialize agent app hash table. We allocate the hash table here
6103 * since cleanup() can get called after this point.
6105 if (agent_app_ht_alloc()) {
6106 ERR("Failed to allocate Agent app hash table");
6108 goto exit_init_data
;
6112 * These actions must be executed as root. We do that *after* setting up
6113 * the sockets path because we MUST make the check for another daemon using
6114 * those paths *before* trying to set the kernel consumer sockets and init
6118 if (set_consumer_sockets(&kconsumer_data
, rundir
)) {
6120 goto exit_init_data
;
6123 /* Setup kernel tracer */
6124 if (!opt_no_kernel
) {
6125 init_kernel_tracer();
6126 if (kernel_tracer_fd
>= 0) {
6127 ret
= syscall_init_table();
6129 ERR("Unable to populate syscall table. "
6130 "Syscall tracing won't work "
6131 "for this session daemon.");
6136 /* Set ulimit for open files */
6139 /* init lttng_fd tracking must be done after set_ulimit. */
6142 if (set_consumer_sockets(&ustconsumer64_data
, rundir
)) {
6144 goto exit_init_data
;
6147 if (set_consumer_sockets(&ustconsumer32_data
, rundir
)) {
6149 goto exit_init_data
;
6152 /* Setup the needed unix socket */
6153 if (init_daemon_socket()) {
6155 goto exit_init_data
;
6158 /* Set credentials to socket */
6159 if (is_root
&& set_permissions(rundir
)) {
6161 goto exit_init_data
;
6164 /* Get parent pid if -S, --sig-parent is specified. */
6165 if (opt_sig_parent
) {
6169 /* Setup the kernel pipe for waking up the kernel thread */
6170 if (is_root
&& !opt_no_kernel
) {
6171 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
6173 goto exit_init_data
;
6177 /* Setup the thread apps communication pipe. */
6178 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
6180 goto exit_init_data
;
6183 /* Setup the thread apps notify communication pipe. */
6184 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
6186 goto exit_init_data
;
6189 /* Initialize global buffer per UID and PID registry. */
6190 buffer_reg_init_uid_registry();
6191 buffer_reg_init_pid_registry();
6193 /* Init UST command queue. */
6194 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6197 * Get session list pointer. This pointer MUST NOT be free'd. This list
6198 * is statically declared in session.c
6200 session_list_ptr
= session_get_list();
6204 /* Check for the application socket timeout env variable. */
6205 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6206 if (env_app_timeout
) {
6207 app_socket_timeout
= atoi(env_app_timeout
);
6209 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6212 ret
= write_pidfile();
6214 ERR("Error in write_pidfile");
6216 goto exit_init_data
;
6218 ret
= write_agent_port();
6220 ERR("Error in write_agent_port");
6222 goto exit_init_data
;
6225 /* Initialize communication library */
6227 /* Initialize TCP timeout values */
6228 lttcomm_inet_init();
6230 if (load_session_init_data(&load_info
) < 0) {
6232 goto exit_init_data
;
6234 load_info
->path
= opt_load_session_path
;
6236 /* Create health-check thread. */
6237 ret
= pthread_create(&health_thread
, default_pthread_attr(),
6238 thread_manage_health
, (void *) NULL
);
6241 PERROR("pthread_create health");
6246 /* notification_thread_data acquires the pipes' read side. */
6247 notification_thread_handle
= notification_thread_handle_create(
6248 ust32_channel_monitor_pipe
,
6249 ust64_channel_monitor_pipe
,
6250 kernel_channel_monitor_pipe
);
6251 if (!notification_thread_handle
) {
6253 ERR("Failed to create notification thread shared data");
6255 goto exit_notification
;
6258 /* Create notification thread. */
6259 ret
= pthread_create(¬ification_thread
, default_pthread_attr(),
6260 thread_notification
, notification_thread_handle
);
6263 PERROR("pthread_create notification");
6266 goto exit_notification
;
6268 notification_thread_running
= true;
6270 /* rotation_thread_data acquires the pipes' read side. */
6271 rotation_thread_handle
= rotation_thread_handle_create(
6272 ust32_channel_rotate_pipe
,
6273 ust64_channel_rotate_pipe
,
6274 kernel_channel_rotate_pipe
,
6275 thread_quit_pipe
[0]);
6276 if (!rotation_thread_handle
) {
6278 ERR("Failed to create rotation thread shared data");
6283 /* Create rotation thread. */
6284 ret
= pthread_create(&rotation_thread
, default_pthread_attr(),
6285 thread_rotation
, rotation_thread_handle
);
6288 PERROR("pthread_create rotation");
6294 /* Create thread to manage the client socket */
6295 ret
= pthread_create(&client_thread
, default_pthread_attr(),
6296 thread_manage_clients
, (void *) NULL
);
6299 PERROR("pthread_create clients");
6305 /* Create thread to dispatch registration */
6306 ret
= pthread_create(&dispatch_thread
, default_pthread_attr(),
6307 thread_dispatch_ust_registration
, (void *) NULL
);
6310 PERROR("pthread_create dispatch");
6316 /* Create thread to manage application registration. */
6317 ret
= pthread_create(®_apps_thread
, default_pthread_attr(),
6318 thread_registration_apps
, (void *) NULL
);
6321 PERROR("pthread_create registration");
6327 /* Create thread to manage application socket */
6328 ret
= pthread_create(&apps_thread
, default_pthread_attr(),
6329 thread_manage_apps
, (void *) NULL
);
6332 PERROR("pthread_create apps");
6338 /* Create thread to manage application notify socket */
6339 ret
= pthread_create(&apps_notify_thread
, default_pthread_attr(),
6340 ust_thread_manage_notify
, (void *) NULL
);
6343 PERROR("pthread_create notify");
6346 goto exit_apps_notify
;
6349 /* Create agent registration thread. */
6350 ret
= pthread_create(&agent_reg_thread
, default_pthread_attr(),
6351 agent_thread_manage_registration
, (void *) NULL
);
6354 PERROR("pthread_create agent");
6357 goto exit_agent_reg
;
6360 /* Don't start this thread if kernel tracing is not requested nor root */
6361 if (is_root
&& !opt_no_kernel
) {
6362 /* Create kernel thread to manage kernel event */
6363 ret
= pthread_create(&kernel_thread
, default_pthread_attr(),
6364 thread_manage_kernel
, (void *) NULL
);
6367 PERROR("pthread_create kernel");
6374 /* Create session loading thread. */
6375 ret
= pthread_create(&load_session_thread
, default_pthread_attr(),
6376 thread_load_session
, load_info
);
6379 PERROR("pthread_create load_session_thread");
6382 goto exit_load_session
;
6386 * This is where we start awaiting program completion (e.g. through
6387 * signal that asks threads to teardown).
6390 ret
= pthread_join(load_session_thread
, &status
);
6393 PERROR("pthread_join load_session_thread");
6398 if (is_root
&& !opt_no_kernel
) {
6399 ret
= pthread_join(kernel_thread
, &status
);
6402 PERROR("pthread_join");
6408 ret
= pthread_join(agent_reg_thread
, &status
);
6411 PERROR("pthread_join agent");
6416 ret
= pthread_join(apps_notify_thread
, &status
);
6419 PERROR("pthread_join apps notify");
6424 ret
= pthread_join(apps_thread
, &status
);
6427 PERROR("pthread_join apps");
6432 ret
= pthread_join(reg_apps_thread
, &status
);
6435 PERROR("pthread_join");
6441 * Join dispatch thread after joining reg_apps_thread to ensure
6442 * we don't leak applications in the queue.
6444 ret
= pthread_join(dispatch_thread
, &status
);
6447 PERROR("pthread_join");
6452 ret
= pthread_join(client_thread
, &status
);
6455 PERROR("pthread_join");
6462 ret
= pthread_join(health_thread
, &status
);
6465 PERROR("pthread_join health thread");
6472 * Wait for all pending call_rcu work to complete before tearing
6473 * down data structures. call_rcu worker may be trying to
6474 * perform lookups in those structures.
6478 * sessiond_cleanup() is called when no other thread is running, except
6479 * the ht_cleanup thread, which is needed to destroy the hash tables.
6481 rcu_thread_online();
6485 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6486 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6487 * the queue is empty before shutting down the clean-up thread.
6492 * The teardown of the notification system is performed after the
6493 * session daemon's teardown in order to allow it to be notified
6494 * of the active session and channels at the moment of the teardown.
6496 if (notification_thread_handle
) {
6497 if (notification_thread_running
) {
6498 notification_thread_command_quit(
6499 notification_thread_handle
);
6500 ret
= pthread_join(notification_thread
, &status
);
6503 PERROR("pthread_join notification thread");
6507 notification_thread_handle_destroy(notification_thread_handle
);
6510 if (rotation_thread_handle
) {
6511 rotation_thread_handle_destroy(rotation_thread_handle
);
6514 ret
= pthread_join(rotation_thread
, &status
);
6517 PERROR("pthread_join rotation thread");
6521 rcu_thread_offline();
6522 rcu_unregister_thread();
6524 ret
= fini_ht_cleanup_thread(&ht_cleanup_thread
);
6528 lttng_pipe_destroy(ust32_channel_monitor_pipe
);
6529 lttng_pipe_destroy(ust64_channel_monitor_pipe
);
6530 lttng_pipe_destroy(kernel_channel_monitor_pipe
);
6531 lttng_pipe_destroy(ust32_channel_rotate_pipe
);
6532 lttng_pipe_destroy(ust64_channel_rotate_pipe
);
6533 lttng_pipe_destroy(kernel_channel_rotate_pipe
);
6536 health_app_destroy(health_sessiond
);
6537 exit_health_sessiond_cleanup
:
6538 exit_create_run_as_worker_cleanup
:
6541 sessiond_cleanup_options();
6543 exit_set_signal_handler
: