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"
77 #define CONSUMERD_FILE "lttng-consumerd"
80 static const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
81 static int tracing_group_name_override
;
82 static char *opt_pidfile
;
83 static int opt_sig_parent
;
84 static int opt_verbose_consumer
;
85 static int opt_daemon
, opt_background
;
86 static int opt_no_kernel
;
87 static char *opt_load_session_path
;
88 static pid_t ppid
; /* Parent PID for --sig-parent option */
89 static pid_t child_ppid
; /* Internal parent PID use with daemonize. */
91 static int lockfile_fd
= -1;
93 /* Set to 1 when a SIGUSR1 signal is received. */
94 static int recv_child_signal
;
97 * Consumer daemon specific control data. Every value not initialized here is
98 * set to 0 by the static definition.
100 static struct consumer_data kconsumer_data
= {
101 .type
= LTTNG_CONSUMER_KERNEL
,
102 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
103 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
106 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
107 .lock
= PTHREAD_MUTEX_INITIALIZER
,
108 .cond
= PTHREAD_COND_INITIALIZER
,
109 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
111 static struct consumer_data ustconsumer64_data
= {
112 .type
= LTTNG_CONSUMER64_UST
,
113 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
114 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
117 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
118 .lock
= PTHREAD_MUTEX_INITIALIZER
,
119 .cond
= PTHREAD_COND_INITIALIZER
,
120 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
122 static struct consumer_data ustconsumer32_data
= {
123 .type
= LTTNG_CONSUMER32_UST
,
124 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
125 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
128 .pid_mutex
= PTHREAD_MUTEX_INITIALIZER
,
129 .lock
= PTHREAD_MUTEX_INITIALIZER
,
130 .cond
= PTHREAD_COND_INITIALIZER
,
131 .cond_mutex
= PTHREAD_MUTEX_INITIALIZER
,
134 /* Command line options */
135 static const struct option long_options
[] = {
136 { "client-sock", required_argument
, 0, 'c' },
137 { "apps-sock", required_argument
, 0, 'a' },
138 { "kconsumerd-cmd-sock", required_argument
, 0, '\0' },
139 { "kconsumerd-err-sock", required_argument
, 0, '\0' },
140 { "ustconsumerd32-cmd-sock", required_argument
, 0, '\0' },
141 { "ustconsumerd32-err-sock", required_argument
, 0, '\0' },
142 { "ustconsumerd64-cmd-sock", required_argument
, 0, '\0' },
143 { "ustconsumerd64-err-sock", required_argument
, 0, '\0' },
144 { "consumerd32-path", required_argument
, 0, '\0' },
145 { "consumerd32-libdir", required_argument
, 0, '\0' },
146 { "consumerd64-path", required_argument
, 0, '\0' },
147 { "consumerd64-libdir", required_argument
, 0, '\0' },
148 { "daemonize", no_argument
, 0, 'd' },
149 { "background", no_argument
, 0, 'b' },
150 { "sig-parent", no_argument
, 0, 'S' },
151 { "help", no_argument
, 0, 'h' },
152 { "group", required_argument
, 0, 'g' },
153 { "version", no_argument
, 0, 'V' },
154 { "quiet", no_argument
, 0, 'q' },
155 { "verbose", no_argument
, 0, 'v' },
156 { "verbose-consumer", no_argument
, 0, '\0' },
157 { "no-kernel", no_argument
, 0, '\0' },
158 { "pidfile", required_argument
, 0, 'p' },
159 { "agent-tcp-port", required_argument
, 0, '\0' },
160 { "config", required_argument
, 0, 'f' },
161 { "load", required_argument
, 0, 'l' },
162 { "kmod-probes", required_argument
, 0, '\0' },
163 { "extra-kmod-probes", required_argument
, 0, '\0' },
167 /* Command line options to ignore from configuration file */
168 static const char *config_ignore_options
[] = { "help", "version", "config" };
170 /* Shared between threads */
171 static int dispatch_thread_exit
;
173 /* Global application Unix socket path */
174 static char apps_unix_sock_path
[PATH_MAX
];
175 /* Global client Unix socket path */
176 static char client_unix_sock_path
[PATH_MAX
];
177 /* global wait shm path for UST */
178 static char wait_shm_path
[PATH_MAX
];
179 /* Global health check unix path */
180 static char health_unix_sock_path
[PATH_MAX
];
182 /* Sockets and FDs */
183 static int client_sock
= -1;
184 static int apps_sock
= -1;
185 int kernel_tracer_fd
= -1;
186 static int kernel_poll_pipe
[2] = { -1, -1 };
189 * Quit pipe for all threads. This permits a single cancellation point
190 * for all threads when receiving an event on the pipe.
192 static int thread_quit_pipe
[2] = { -1, -1 };
193 static int ht_cleanup_quit_pipe
[2] = { -1, -1 };
196 * This pipe is used to inform the thread managing application communication
197 * that a command is queued and ready to be processed.
199 static int apps_cmd_pipe
[2] = { -1, -1 };
201 int apps_cmd_notify_pipe
[2] = { -1, -1 };
203 /* Pthread, Mutexes and Semaphores */
204 static pthread_t apps_thread
;
205 static pthread_t apps_notify_thread
;
206 static pthread_t reg_apps_thread
;
207 static pthread_t client_thread
;
208 static pthread_t kernel_thread
;
209 static pthread_t dispatch_thread
;
210 static pthread_t health_thread
;
211 static pthread_t ht_cleanup_thread
;
212 static pthread_t agent_reg_thread
;
213 static pthread_t load_session_thread
;
216 * UST registration command queue. This queue is tied with a futex and uses a N
217 * wakers / 1 waiter implemented and detailed in futex.c/.h
219 * The thread_registration_apps and thread_dispatch_ust_registration uses this
220 * queue along with the wait/wake scheme. The thread_manage_apps receives down
221 * the line new application socket and monitors it for any I/O error or clean
222 * close that triggers an unregistration of the application.
224 static struct ust_cmd_queue ust_cmd_queue
;
227 * Pointer initialized before thread creation.
229 * This points to the tracing session list containing the session count and a
230 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
231 * MUST NOT be taken if you call a public function in session.c.
233 * The lock is nested inside the structure: session_list_ptr->lock. Please use
234 * session_lock_list and session_unlock_list for lock acquisition.
236 static struct ltt_session_list
*session_list_ptr
;
238 int ust_consumerd64_fd
= -1;
239 int ust_consumerd32_fd
= -1;
241 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
242 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
243 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
244 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
245 static int consumerd32_bin_override
;
246 static int consumerd64_bin_override
;
247 static int consumerd32_libdir_override
;
248 static int consumerd64_libdir_override
;
250 static const char *module_proc_lttng
= "/proc/lttng";
253 * Consumer daemon state which is changed when spawning it, killing it or in
254 * case of a fatal error.
256 enum consumerd_state
{
257 CONSUMER_STARTED
= 1,
258 CONSUMER_STOPPED
= 2,
263 * This consumer daemon state is used to validate if a client command will be
264 * able to reach the consumer. If not, the client is informed. For instance,
265 * doing a "lttng start" when the consumer state is set to ERROR will return an
266 * error to the client.
268 * The following example shows a possible race condition of this scheme:
270 * consumer thread error happens
272 * client cmd checks state -> still OK
273 * consumer thread exit, sets error
274 * client cmd try to talk to consumer
277 * However, since the consumer is a different daemon, we have no way of making
278 * sure the command will reach it safely even with this state flag. This is why
279 * we consider that up to the state validation during command processing, the
280 * command is safe. After that, we can not guarantee the correctness of the
281 * client request vis-a-vis the consumer.
283 static enum consumerd_state ust_consumerd_state
;
284 static enum consumerd_state kernel_consumerd_state
;
287 * Socket timeout for receiving and sending in seconds.
289 static int app_socket_timeout
;
291 /* Set in main() with the current page size. */
294 /* Application health monitoring */
295 struct health_app
*health_sessiond
;
297 /* Agent TCP port for registration. Used by the agent thread. */
298 unsigned int agent_tcp_port
= DEFAULT_AGENT_TCP_PORT
;
300 /* Am I root or not. */
301 int is_root
; /* Set to 1 if the daemon is running as root */
303 const char * const config_section_name
= "sessiond";
305 /* Load session thread information to operate. */
306 struct load_session_thread_data
*load_info
;
308 /* Global hash tables */
309 struct lttng_ht
*agent_apps_ht_by_sock
= NULL
;
312 * Whether sessiond is ready for commands/health check requests.
313 * NR_LTTNG_SESSIOND_READY must match the number of calls to
314 * sessiond_notify_ready().
316 #define NR_LTTNG_SESSIOND_READY 3
317 int lttng_sessiond_ready
= NR_LTTNG_SESSIOND_READY
;
319 /* Notify parents that we are ready for cmd and health check */
321 void sessiond_notify_ready(void)
323 if (uatomic_sub_return(<tng_sessiond_ready
, 1) == 0) {
325 * Notify parent pid that we are ready to accept command
326 * for client side. This ppid is the one from the
327 * external process that spawned us.
329 if (opt_sig_parent
) {
334 * Notify the parent of the fork() process that we are
337 if (opt_daemon
|| opt_background
) {
338 kill(child_ppid
, SIGUSR1
);
344 void setup_consumerd_path(void)
346 const char *bin
, *libdir
;
349 * Allow INSTALL_BIN_PATH to be used as a target path for the
350 * native architecture size consumer if CONFIG_CONSUMER*_PATH
351 * has not been defined.
353 #if (CAA_BITS_PER_LONG == 32)
354 if (!consumerd32_bin
[0]) {
355 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
357 if (!consumerd32_libdir
[0]) {
358 consumerd32_libdir
= INSTALL_LIB_PATH
;
360 #elif (CAA_BITS_PER_LONG == 64)
361 if (!consumerd64_bin
[0]) {
362 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
364 if (!consumerd64_libdir
[0]) {
365 consumerd64_libdir
= INSTALL_LIB_PATH
;
368 #error "Unknown bitness"
372 * runtime env. var. overrides the build default.
374 bin
= lttng_secure_getenv("LTTNG_CONSUMERD32_BIN");
376 consumerd32_bin
= bin
;
378 bin
= lttng_secure_getenv("LTTNG_CONSUMERD64_BIN");
380 consumerd64_bin
= bin
;
382 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD32_LIBDIR");
384 consumerd32_libdir
= libdir
;
386 libdir
= lttng_secure_getenv("LTTNG_CONSUMERD64_LIBDIR");
388 consumerd64_libdir
= libdir
;
393 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
400 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
406 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
418 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
420 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
422 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);
426 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
428 int sessiond_set_ht_cleanup_thread_pollset(struct lttng_poll_event
*events
,
431 return __sessiond_set_thread_pollset(events
, size
,
432 ht_cleanup_quit_pipe
);
436 int __sessiond_check_thread_quit_pipe(int fd
, uint32_t events
, int a_pipe
)
438 if (fd
== a_pipe
&& (events
& LPOLLIN
)) {
445 * Check if the thread quit pipe was triggered.
447 * Return 1 if it was triggered else 0;
449 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
451 return __sessiond_check_thread_quit_pipe(fd
, events
,
452 thread_quit_pipe
[0]);
456 * Check if the ht_cleanup thread quit pipe was triggered.
458 * Return 1 if it was triggered else 0;
460 int sessiond_check_ht_cleanup_quit(int fd
, uint32_t events
)
462 return __sessiond_check_thread_quit_pipe(fd
, events
,
463 ht_cleanup_quit_pipe
[0]);
467 * Init thread quit pipe.
469 * Return -1 on error or 0 if all pipes are created.
471 static int __init_thread_quit_pipe(int *a_pipe
)
477 PERROR("thread quit pipe");
481 for (i
= 0; i
< 2; i
++) {
482 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
493 static int init_thread_quit_pipe(void)
495 return __init_thread_quit_pipe(thread_quit_pipe
);
498 static int init_ht_cleanup_quit_pipe(void)
500 return __init_thread_quit_pipe(ht_cleanup_quit_pipe
);
504 * Stop all threads by closing the thread quit pipe.
506 static void stop_threads(void)
510 /* Stopping all threads */
511 DBG("Terminating all threads");
512 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
514 ERR("write error on thread quit pipe");
517 /* Dispatch thread */
518 CMM_STORE_SHARED(dispatch_thread_exit
, 1);
519 futex_nto1_wake(&ust_cmd_queue
.futex
);
523 * Close every consumer sockets.
525 static void close_consumer_sockets(void)
529 if (kconsumer_data
.err_sock
>= 0) {
530 ret
= close(kconsumer_data
.err_sock
);
532 PERROR("kernel consumer err_sock close");
535 if (ustconsumer32_data
.err_sock
>= 0) {
536 ret
= close(ustconsumer32_data
.err_sock
);
538 PERROR("UST consumerd32 err_sock close");
541 if (ustconsumer64_data
.err_sock
>= 0) {
542 ret
= close(ustconsumer64_data
.err_sock
);
544 PERROR("UST consumerd64 err_sock close");
547 if (kconsumer_data
.cmd_sock
>= 0) {
548 ret
= close(kconsumer_data
.cmd_sock
);
550 PERROR("kernel consumer cmd_sock close");
553 if (ustconsumer32_data
.cmd_sock
>= 0) {
554 ret
= close(ustconsumer32_data
.cmd_sock
);
556 PERROR("UST consumerd32 cmd_sock close");
559 if (ustconsumer64_data
.cmd_sock
>= 0) {
560 ret
= close(ustconsumer64_data
.cmd_sock
);
562 PERROR("UST consumerd64 cmd_sock close");
568 * Generate the full lock file path using the rundir.
570 * Return the snprintf() return value thus a negative value is an error.
572 static int generate_lock_file_path(char *path
, size_t len
)
579 /* Build lockfile path from rundir. */
580 ret
= snprintf(path
, len
, "%s/" DEFAULT_LTTNG_SESSIOND_LOCKFILE
, rundir
);
582 PERROR("snprintf lockfile path");
589 * Wait on consumer process termination.
591 * Need to be called with the consumer data lock held or from a context
592 * ensuring no concurrent access to data (e.g: cleanup).
594 static void wait_consumer(struct consumer_data
*consumer_data
)
599 if (consumer_data
->pid
<= 0) {
603 DBG("Waiting for complete teardown of consumerd (PID: %d)",
605 ret
= waitpid(consumer_data
->pid
, &status
, 0);
607 PERROR("consumerd waitpid pid: %d", consumer_data
->pid
)
609 if (!WIFEXITED(status
)) {
610 ERR("consumerd termination with error: %d",
613 consumer_data
->pid
= 0;
617 * Cleanup the session daemon's data structures.
619 static void sessiond_cleanup(void)
622 struct ltt_session
*sess
, *stmp
;
625 DBG("Cleanup sessiond");
628 * Close the thread quit pipe. It has already done its job,
629 * since we are now called.
631 utils_close_pipe(thread_quit_pipe
);
634 * If opt_pidfile is undefined, the default file will be wiped when
635 * removing the rundir.
638 ret
= remove(opt_pidfile
);
640 PERROR("remove pidfile %s", opt_pidfile
);
644 DBG("Removing sessiond and consumerd content of directory %s", rundir
);
647 snprintf(path
, PATH_MAX
,
649 rundir
, DEFAULT_LTTNG_SESSIOND_PIDFILE
);
650 DBG("Removing %s", path
);
653 snprintf(path
, PATH_MAX
, "%s/%s", rundir
,
654 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
);
655 DBG("Removing %s", path
);
659 snprintf(path
, PATH_MAX
,
660 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
662 DBG("Removing %s", path
);
665 snprintf(path
, PATH_MAX
,
666 DEFAULT_KCONSUMERD_PATH
,
668 DBG("Removing directory %s", path
);
671 /* ust consumerd 32 */
672 snprintf(path
, PATH_MAX
,
673 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
675 DBG("Removing %s", path
);
678 snprintf(path
, PATH_MAX
,
679 DEFAULT_USTCONSUMERD32_PATH
,
681 DBG("Removing directory %s", path
);
684 /* ust consumerd 64 */
685 snprintf(path
, PATH_MAX
,
686 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
688 DBG("Removing %s", path
);
691 snprintf(path
, PATH_MAX
,
692 DEFAULT_USTCONSUMERD64_PATH
,
694 DBG("Removing directory %s", path
);
697 DBG("Cleaning up all sessions");
699 /* Destroy session list mutex */
700 if (session_list_ptr
!= NULL
) {
701 pthread_mutex_destroy(&session_list_ptr
->lock
);
703 /* Cleanup ALL session */
704 cds_list_for_each_entry_safe(sess
, stmp
,
705 &session_list_ptr
->head
, list
) {
706 cmd_destroy_session(sess
, kernel_poll_pipe
[1]);
710 wait_consumer(&kconsumer_data
);
711 wait_consumer(&ustconsumer64_data
);
712 wait_consumer(&ustconsumer32_data
);
714 DBG("Cleaning up all agent apps");
715 agent_app_ht_clean();
717 DBG("Closing all UST sockets");
718 ust_app_clean_list();
719 buffer_reg_destroy_registries();
721 if (is_root
&& !opt_no_kernel
) {
722 DBG2("Closing kernel fd");
723 if (kernel_tracer_fd
>= 0) {
724 ret
= close(kernel_tracer_fd
);
729 DBG("Unloading kernel modules");
730 modprobe_remove_lttng_all();
734 close_consumer_sockets();
737 load_session_destroy_data(load_info
);
742 * Cleanup lock file by deleting it and finaly closing it which will
743 * release the file system lock.
745 if (lockfile_fd
>= 0) {
746 char lockfile_path
[PATH_MAX
];
748 ret
= generate_lock_file_path(lockfile_path
,
749 sizeof(lockfile_path
));
751 ret
= remove(lockfile_path
);
753 PERROR("remove lock file");
755 ret
= close(lockfile_fd
);
757 PERROR("close lock file");
763 * We do NOT rmdir rundir because there are other processes
764 * using it, for instance lttng-relayd, which can start in
765 * parallel with this teardown.
772 * Cleanup the daemon's option data structures.
774 static void sessiond_cleanup_options(void)
776 DBG("Cleaning up options");
779 * If the override option is set, the pointer points to a *non* const
780 * thus freeing it even though the variable type is set to const.
782 if (tracing_group_name_override
) {
783 free((void *) tracing_group_name
);
785 if (consumerd32_bin_override
) {
786 free((void *) consumerd32_bin
);
788 if (consumerd64_bin_override
) {
789 free((void *) consumerd64_bin
);
791 if (consumerd32_libdir_override
) {
792 free((void *) consumerd32_libdir
);
794 if (consumerd64_libdir_override
) {
795 free((void *) consumerd64_libdir
);
799 free(opt_load_session_path
);
800 free(kmod_probes_list
);
801 free(kmod_extra_probes_list
);
803 run_as_destroy_worker();
806 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
807 "Matthew, BEET driven development works!%c[%dm",
808 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
813 * Send data on a unix socket using the liblttsessiondcomm API.
815 * Return lttcomm error code.
817 static int send_unix_sock(int sock
, void *buf
, size_t len
)
819 /* Check valid length */
824 return lttcomm_send_unix_sock(sock
, buf
, len
);
828 * Free memory of a command context structure.
830 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
832 DBG("Clean command context structure");
834 if ((*cmd_ctx
)->llm
) {
835 free((*cmd_ctx
)->llm
);
837 if ((*cmd_ctx
)->lsm
) {
838 free((*cmd_ctx
)->lsm
);
846 * Notify UST applications using the shm mmap futex.
848 static int notify_ust_apps(int active
)
852 DBG("Notifying applications of session daemon state: %d", active
);
854 /* See shm.c for this call implying mmap, shm and futex calls */
855 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
856 if (wait_shm_mmap
== NULL
) {
860 /* Wake waiting process */
861 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
863 /* Apps notified successfully */
871 * Setup the outgoing data buffer for the response (llm) by allocating the
872 * right amount of memory and copying the original information from the lsm
875 * Return 0 on success, negative value on error.
877 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
,
878 const void *payload_buf
, size_t payload_len
,
879 const void *cmd_header_buf
, size_t cmd_header_len
)
882 const size_t header_len
= sizeof(struct lttcomm_lttng_msg
);
883 const size_t cmd_header_offset
= header_len
;
884 const size_t payload_offset
= cmd_header_offset
+ cmd_header_len
;
885 const size_t total_msg_size
= header_len
+ cmd_header_len
+ payload_len
;
887 cmd_ctx
->llm
= zmalloc(total_msg_size
);
889 if (cmd_ctx
->llm
== NULL
) {
895 /* Copy common data */
896 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
897 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
898 cmd_ctx
->llm
->cmd_header_size
= cmd_header_len
;
899 cmd_ctx
->llm
->data_size
= payload_len
;
900 cmd_ctx
->lttng_msg_size
= total_msg_size
;
902 /* Copy command header */
903 if (cmd_header_len
) {
904 memcpy(((uint8_t *) cmd_ctx
->llm
) + cmd_header_offset
, cmd_header_buf
,
910 memcpy(((uint8_t *) cmd_ctx
->llm
) + payload_offset
, payload_buf
,
919 * Version of setup_lttng_msg() without command header.
921 static int setup_lttng_msg_no_cmd_header(struct command_ctx
*cmd_ctx
,
922 void *payload_buf
, size_t payload_len
)
924 return setup_lttng_msg(cmd_ctx
, payload_buf
, payload_len
, NULL
, 0);
927 * Update the kernel poll set of all channel fd available over all tracing
928 * session. Add the wakeup pipe at the end of the set.
930 static int update_kernel_poll(struct lttng_poll_event
*events
)
933 struct ltt_session
*session
;
934 struct ltt_kernel_channel
*channel
;
936 DBG("Updating kernel poll set");
939 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
940 session_lock(session
);
941 if (session
->kernel_session
== NULL
) {
942 session_unlock(session
);
946 cds_list_for_each_entry(channel
,
947 &session
->kernel_session
->channel_list
.head
, list
) {
948 /* Add channel fd to the kernel poll set */
949 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
951 session_unlock(session
);
954 DBG("Channel fd %d added to kernel set", channel
->fd
);
956 session_unlock(session
);
958 session_unlock_list();
963 session_unlock_list();
968 * Find the channel fd from 'fd' over all tracing session. When found, check
969 * for new channel stream and send those stream fds to the kernel consumer.
971 * Useful for CPU hotplug feature.
973 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
976 struct ltt_session
*session
;
977 struct ltt_kernel_session
*ksess
;
978 struct ltt_kernel_channel
*channel
;
980 DBG("Updating kernel streams for channel fd %d", fd
);
983 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
984 session_lock(session
);
985 if (session
->kernel_session
== NULL
) {
986 session_unlock(session
);
989 ksess
= session
->kernel_session
;
991 cds_list_for_each_entry(channel
,
992 &ksess
->channel_list
.head
, list
) {
993 struct lttng_ht_iter iter
;
994 struct consumer_socket
*socket
;
996 if (channel
->fd
!= fd
) {
999 DBG("Channel found, updating kernel streams");
1000 ret
= kernel_open_channel_stream(channel
);
1004 /* Update the stream global counter */
1005 ksess
->stream_count_global
+= ret
;
1008 * Have we already sent fds to the consumer? If yes, it
1009 * means that tracing is started so it is safe to send
1010 * our updated stream fds.
1012 if (ksess
->consumer_fds_sent
!= 1
1013 || ksess
->consumer
== NULL
) {
1019 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
,
1020 &iter
.iter
, socket
, node
.node
) {
1021 pthread_mutex_lock(socket
->lock
);
1022 ret
= kernel_consumer_send_channel_stream(socket
,
1024 session
->output_traces
? 1 : 0);
1025 pthread_mutex_unlock(socket
->lock
);
1033 session_unlock(session
);
1035 session_unlock_list();
1039 session_unlock(session
);
1040 session_unlock_list();
1045 * For each tracing session, update newly registered apps. The session list
1046 * lock MUST be acquired before calling this.
1048 static void update_ust_app(int app_sock
)
1050 struct ltt_session
*sess
, *stmp
;
1052 /* Consumer is in an ERROR state. Stop any application update. */
1053 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
1054 /* Stop the update process since the consumer is dead. */
1058 /* For all tracing session(s) */
1059 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
1060 struct ust_app
*app
;
1063 if (!sess
->ust_session
) {
1064 goto unlock_session
;
1068 assert(app_sock
>= 0);
1069 app
= ust_app_find_by_sock(app_sock
);
1072 * Application can be unregistered before so
1073 * this is possible hence simply stopping the
1076 DBG3("UST app update failed to find app sock %d",
1080 ust_app_global_update(sess
->ust_session
, app
);
1084 session_unlock(sess
);
1089 * This thread manage event coming from the kernel.
1091 * Features supported in this thread:
1094 static void *thread_manage_kernel(void *data
)
1096 int ret
, i
, pollfd
, update_poll_flag
= 1, err
= -1;
1097 uint32_t revents
, nb_fd
;
1099 struct lttng_poll_event events
;
1101 DBG("[thread] Thread manage kernel started");
1103 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_KERNEL
);
1106 * This first step of the while is to clean this structure which could free
1107 * non NULL pointers so initialize it before the loop.
1109 lttng_poll_init(&events
);
1111 if (testpoint(sessiond_thread_manage_kernel
)) {
1112 goto error_testpoint
;
1115 health_code_update();
1117 if (testpoint(sessiond_thread_manage_kernel_before_loop
)) {
1118 goto error_testpoint
;
1122 health_code_update();
1124 if (update_poll_flag
== 1) {
1125 /* Clean events object. We are about to populate it again. */
1126 lttng_poll_clean(&events
);
1128 ret
= sessiond_set_thread_pollset(&events
, 2);
1130 goto error_poll_create
;
1133 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
1138 /* This will add the available kernel channel if any. */
1139 ret
= update_kernel_poll(&events
);
1143 update_poll_flag
= 0;
1146 DBG("Thread kernel polling");
1148 /* Poll infinite value of time */
1150 health_poll_entry();
1151 ret
= lttng_poll_wait(&events
, -1);
1152 DBG("Thread kernel return from poll on %d fds",
1153 LTTNG_POLL_GETNB(&events
));
1157 * Restart interrupted system call.
1159 if (errno
== EINTR
) {
1163 } else if (ret
== 0) {
1164 /* Should not happen since timeout is infinite */
1165 ERR("Return value of poll is 0 with an infinite timeout.\n"
1166 "This should not have happened! Continuing...");
1172 for (i
= 0; i
< nb_fd
; i
++) {
1173 /* Fetch once the poll data */
1174 revents
= LTTNG_POLL_GETEV(&events
, i
);
1175 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1177 health_code_update();
1180 /* No activity for this FD (poll implementation). */
1184 /* Thread quit pipe has been closed. Killing thread. */
1185 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1191 /* Check for data on kernel pipe */
1192 if (revents
& LPOLLIN
) {
1193 if (pollfd
== kernel_poll_pipe
[0]) {
1194 (void) lttng_read(kernel_poll_pipe
[0],
1197 * Ret value is useless here, if this pipe gets any actions an
1198 * update is required anyway.
1200 update_poll_flag
= 1;
1204 * New CPU detected by the kernel. Adding kernel stream to
1205 * kernel session and updating the kernel consumer
1207 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
1213 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1214 update_poll_flag
= 1;
1217 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1225 lttng_poll_clean(&events
);
1228 utils_close_pipe(kernel_poll_pipe
);
1229 kernel_poll_pipe
[0] = kernel_poll_pipe
[1] = -1;
1232 ERR("Health error occurred in %s", __func__
);
1233 WARN("Kernel thread died unexpectedly. "
1234 "Kernel tracing can continue but CPU hotplug is disabled.");
1236 health_unregister(health_sessiond
);
1237 DBG("Kernel thread dying");
1242 * Signal pthread condition of the consumer data that the thread.
1244 static void signal_consumer_condition(struct consumer_data
*data
, int state
)
1246 pthread_mutex_lock(&data
->cond_mutex
);
1249 * The state is set before signaling. It can be any value, it's the waiter
1250 * job to correctly interpret this condition variable associated to the
1251 * consumer pthread_cond.
1253 * A value of 0 means that the corresponding thread of the consumer data
1254 * was not started. 1 indicates that the thread has started and is ready
1255 * for action. A negative value means that there was an error during the
1258 data
->consumer_thread_is_ready
= state
;
1259 (void) pthread_cond_signal(&data
->cond
);
1261 pthread_mutex_unlock(&data
->cond_mutex
);
1265 * This thread manage the consumer error sent back to the session daemon.
1267 static void *thread_manage_consumer(void *data
)
1269 int sock
= -1, i
, ret
, pollfd
, err
= -1, should_quit
= 0;
1270 uint32_t revents
, nb_fd
;
1271 enum lttcomm_return_code code
;
1272 struct lttng_poll_event events
;
1273 struct consumer_data
*consumer_data
= data
;
1275 DBG("[thread] Manage consumer started");
1277 rcu_register_thread();
1278 rcu_thread_online();
1280 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CONSUMER
);
1282 health_code_update();
1285 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1286 * metadata_sock. Nothing more will be added to this poll set.
1288 ret
= sessiond_set_thread_pollset(&events
, 3);
1294 * The error socket here is already in a listening state which was done
1295 * just before spawning this thread to avoid a race between the consumer
1296 * daemon exec trying to connect and the listen() call.
1298 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
1303 health_code_update();
1305 /* Infinite blocking call, waiting for transmission */
1307 health_poll_entry();
1309 if (testpoint(sessiond_thread_manage_consumer
)) {
1313 ret
= lttng_poll_wait(&events
, -1);
1317 * Restart interrupted system call.
1319 if (errno
== EINTR
) {
1327 for (i
= 0; i
< nb_fd
; i
++) {
1328 /* Fetch once the poll data */
1329 revents
= LTTNG_POLL_GETEV(&events
, i
);
1330 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1332 health_code_update();
1335 /* No activity for this FD (poll implementation). */
1339 /* Thread quit pipe has been closed. Killing thread. */
1340 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1346 /* Event on the registration socket */
1347 if (pollfd
== consumer_data
->err_sock
) {
1348 if (revents
& LPOLLIN
) {
1350 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1351 ERR("consumer err socket poll error");
1354 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1360 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
1366 * Set the CLOEXEC flag. Return code is useless because either way, the
1369 (void) utils_set_fd_cloexec(sock
);
1371 health_code_update();
1373 DBG2("Receiving code from consumer err_sock");
1375 /* Getting status code from kconsumerd */
1376 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1377 sizeof(enum lttcomm_return_code
));
1382 health_code_update();
1383 if (code
== LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
) {
1384 /* Connect both socket, command and metadata. */
1385 consumer_data
->cmd_sock
=
1386 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1387 consumer_data
->metadata_fd
=
1388 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
1389 if (consumer_data
->cmd_sock
< 0
1390 || consumer_data
->metadata_fd
< 0) {
1391 PERROR("consumer connect cmd socket");
1392 /* On error, signal condition and quit. */
1393 signal_consumer_condition(consumer_data
, -1);
1396 consumer_data
->metadata_sock
.fd_ptr
= &consumer_data
->metadata_fd
;
1397 /* Create metadata socket lock. */
1398 consumer_data
->metadata_sock
.lock
= zmalloc(sizeof(pthread_mutex_t
));
1399 if (consumer_data
->metadata_sock
.lock
== NULL
) {
1400 PERROR("zmalloc pthread mutex");
1404 pthread_mutex_init(consumer_data
->metadata_sock
.lock
, NULL
);
1406 signal_consumer_condition(consumer_data
, 1);
1407 DBG("Consumer command socket ready (fd: %d", consumer_data
->cmd_sock
);
1408 DBG("Consumer metadata socket ready (fd: %d)",
1409 consumer_data
->metadata_fd
);
1411 ERR("consumer error when waiting for SOCK_READY : %s",
1412 lttcomm_get_readable_code(-code
));
1416 /* Remove the consumerd error sock since we've established a connexion */
1417 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
1422 /* Add new accepted error socket. */
1423 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
1428 /* Add metadata socket that is successfully connected. */
1429 ret
= lttng_poll_add(&events
, consumer_data
->metadata_fd
,
1430 LPOLLIN
| LPOLLRDHUP
);
1435 health_code_update();
1437 /* Infinite blocking call, waiting for transmission */
1440 health_code_update();
1442 /* Exit the thread because the thread quit pipe has been triggered. */
1444 /* Not a health error. */
1449 health_poll_entry();
1450 ret
= lttng_poll_wait(&events
, -1);
1454 * Restart interrupted system call.
1456 if (errno
== EINTR
) {
1464 for (i
= 0; i
< nb_fd
; i
++) {
1465 /* Fetch once the poll data */
1466 revents
= LTTNG_POLL_GETEV(&events
, i
);
1467 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1469 health_code_update();
1472 /* No activity for this FD (poll implementation). */
1477 * Thread quit pipe has been triggered, flag that we should stop
1478 * but continue the current loop to handle potential data from
1481 should_quit
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1483 if (pollfd
== sock
) {
1484 /* Event on the consumerd socket */
1485 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1486 && !(revents
& LPOLLIN
)) {
1487 ERR("consumer err socket second poll error");
1490 health_code_update();
1491 /* Wait for any kconsumerd error */
1492 ret
= lttcomm_recv_unix_sock(sock
, &code
,
1493 sizeof(enum lttcomm_return_code
));
1495 ERR("consumer closed the command socket");
1499 ERR("consumer return code : %s",
1500 lttcomm_get_readable_code(-code
));
1503 } else if (pollfd
== consumer_data
->metadata_fd
) {
1504 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)
1505 && !(revents
& LPOLLIN
)) {
1506 ERR("consumer err metadata socket second poll error");
1509 /* UST metadata requests */
1510 ret
= ust_consumer_metadata_request(
1511 &consumer_data
->metadata_sock
);
1513 ERR("Handling metadata request");
1517 /* No need for an else branch all FDs are tested prior. */
1519 health_code_update();
1525 * We lock here because we are about to close the sockets and some other
1526 * thread might be using them so get exclusive access which will abort all
1527 * other consumer command by other threads.
1529 pthread_mutex_lock(&consumer_data
->lock
);
1531 /* Immediately set the consumerd state to stopped */
1532 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
1533 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
1534 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
1535 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
1536 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
1538 /* Code flow error... */
1542 if (consumer_data
->err_sock
>= 0) {
1543 ret
= close(consumer_data
->err_sock
);
1547 consumer_data
->err_sock
= -1;
1549 if (consumer_data
->cmd_sock
>= 0) {
1550 ret
= close(consumer_data
->cmd_sock
);
1554 consumer_data
->cmd_sock
= -1;
1556 if (consumer_data
->metadata_sock
.fd_ptr
&&
1557 *consumer_data
->metadata_sock
.fd_ptr
>= 0) {
1558 ret
= close(*consumer_data
->metadata_sock
.fd_ptr
);
1570 unlink(consumer_data
->err_unix_sock_path
);
1571 unlink(consumer_data
->cmd_unix_sock_path
);
1572 pthread_mutex_unlock(&consumer_data
->lock
);
1574 /* Cleanup metadata socket mutex. */
1575 if (consumer_data
->metadata_sock
.lock
) {
1576 pthread_mutex_destroy(consumer_data
->metadata_sock
.lock
);
1577 free(consumer_data
->metadata_sock
.lock
);
1579 lttng_poll_clean(&events
);
1583 ERR("Health error occurred in %s", __func__
);
1585 health_unregister(health_sessiond
);
1586 DBG("consumer thread cleanup completed");
1588 rcu_thread_offline();
1589 rcu_unregister_thread();
1595 * This thread manage application communication.
1597 static void *thread_manage_apps(void *data
)
1599 int i
, ret
, pollfd
, err
= -1;
1601 uint32_t revents
, nb_fd
;
1602 struct lttng_poll_event events
;
1604 DBG("[thread] Manage application started");
1606 rcu_register_thread();
1607 rcu_thread_online();
1609 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_MANAGE
);
1611 if (testpoint(sessiond_thread_manage_apps
)) {
1612 goto error_testpoint
;
1615 health_code_update();
1617 ret
= sessiond_set_thread_pollset(&events
, 2);
1619 goto error_poll_create
;
1622 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1627 if (testpoint(sessiond_thread_manage_apps_before_loop
)) {
1631 health_code_update();
1634 DBG("Apps thread polling");
1636 /* Inifinite blocking call, waiting for transmission */
1638 health_poll_entry();
1639 ret
= lttng_poll_wait(&events
, -1);
1640 DBG("Apps thread return from poll on %d fds",
1641 LTTNG_POLL_GETNB(&events
));
1645 * Restart interrupted system call.
1647 if (errno
== EINTR
) {
1655 for (i
= 0; i
< nb_fd
; i
++) {
1656 /* Fetch once the poll data */
1657 revents
= LTTNG_POLL_GETEV(&events
, i
);
1658 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1660 health_code_update();
1663 /* No activity for this FD (poll implementation). */
1667 /* Thread quit pipe has been closed. Killing thread. */
1668 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
1674 /* Inspect the apps cmd pipe */
1675 if (pollfd
== apps_cmd_pipe
[0]) {
1676 if (revents
& LPOLLIN
) {
1680 size_ret
= lttng_read(apps_cmd_pipe
[0], &sock
, sizeof(sock
));
1681 if (size_ret
< sizeof(sock
)) {
1682 PERROR("read apps cmd pipe");
1686 health_code_update();
1689 * Since this is a command socket (write then read),
1690 * we only monitor the error events of the socket.
1692 ret
= lttng_poll_add(&events
, sock
,
1693 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
1698 DBG("Apps with sock %d added to poll set", sock
);
1699 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1700 ERR("Apps command pipe error");
1703 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
1708 * At this point, we know that a registered application made
1709 * the event at poll_wait.
1711 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1712 /* Removing from the poll set */
1713 ret
= lttng_poll_del(&events
, pollfd
);
1718 /* Socket closed on remote end. */
1719 ust_app_unregister(pollfd
);
1721 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1726 health_code_update();
1732 lttng_poll_clean(&events
);
1735 utils_close_pipe(apps_cmd_pipe
);
1736 apps_cmd_pipe
[0] = apps_cmd_pipe
[1] = -1;
1739 * We don't clean the UST app hash table here since already registered
1740 * applications can still be controlled so let them be until the session
1741 * daemon dies or the applications stop.
1746 ERR("Health error occurred in %s", __func__
);
1748 health_unregister(health_sessiond
);
1749 DBG("Application communication apps thread cleanup complete");
1750 rcu_thread_offline();
1751 rcu_unregister_thread();
1756 * Send a socket to a thread This is called from the dispatch UST registration
1757 * thread once all sockets are set for the application.
1759 * The sock value can be invalid, we don't really care, the thread will handle
1760 * it and make the necessary cleanup if so.
1762 * On success, return 0 else a negative value being the errno message of the
1765 static int send_socket_to_thread(int fd
, int sock
)
1770 * It's possible that the FD is set as invalid with -1 concurrently just
1771 * before calling this function being a shutdown state of the thread.
1778 ret
= lttng_write(fd
, &sock
, sizeof(sock
));
1779 if (ret
< sizeof(sock
)) {
1780 PERROR("write apps pipe %d", fd
);
1787 /* All good. Don't send back the write positive ret value. */
1794 * Sanitize the wait queue of the dispatch registration thread meaning removing
1795 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1796 * notify socket is never received.
1798 static void sanitize_wait_queue(struct ust_reg_wait_queue
*wait_queue
)
1800 int ret
, nb_fd
= 0, i
;
1801 unsigned int fd_added
= 0;
1802 struct lttng_poll_event events
;
1803 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1807 lttng_poll_init(&events
);
1809 /* Just skip everything for an empty queue. */
1810 if (!wait_queue
->count
) {
1814 ret
= lttng_poll_create(&events
, wait_queue
->count
, LTTNG_CLOEXEC
);
1819 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1820 &wait_queue
->head
, head
) {
1821 assert(wait_node
->app
);
1822 ret
= lttng_poll_add(&events
, wait_node
->app
->sock
,
1823 LPOLLHUP
| LPOLLERR
);
1836 * Poll but don't block so we can quickly identify the faulty events and
1837 * clean them afterwards from the wait queue.
1839 ret
= lttng_poll_wait(&events
, 0);
1845 for (i
= 0; i
< nb_fd
; i
++) {
1846 /* Get faulty FD. */
1847 uint32_t revents
= LTTNG_POLL_GETEV(&events
, i
);
1848 int pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1851 /* No activity for this FD (poll implementation). */
1855 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
1856 &wait_queue
->head
, head
) {
1857 if (pollfd
== wait_node
->app
->sock
&&
1858 (revents
& (LPOLLHUP
| LPOLLERR
))) {
1859 cds_list_del(&wait_node
->head
);
1860 wait_queue
->count
--;
1861 ust_app_destroy(wait_node
->app
);
1864 * Silence warning of use-after-free in
1865 * cds_list_for_each_entry_safe which uses
1866 * __typeof__(*wait_node).
1871 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
1878 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd
);
1882 lttng_poll_clean(&events
);
1886 lttng_poll_clean(&events
);
1888 ERR("Unable to sanitize wait queue");
1893 * Dispatch request from the registration threads to the application
1894 * communication thread.
1896 static void *thread_dispatch_ust_registration(void *data
)
1899 struct cds_wfcq_node
*node
;
1900 struct ust_command
*ust_cmd
= NULL
;
1901 struct ust_reg_wait_node
*wait_node
= NULL
, *tmp_wait_node
;
1902 struct ust_reg_wait_queue wait_queue
= {
1906 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH
);
1908 if (testpoint(sessiond_thread_app_reg_dispatch
)) {
1909 goto error_testpoint
;
1912 health_code_update();
1914 CDS_INIT_LIST_HEAD(&wait_queue
.head
);
1916 DBG("[thread] Dispatch UST command started");
1918 while (!CMM_LOAD_SHARED(dispatch_thread_exit
)) {
1919 health_code_update();
1921 /* Atomically prepare the queue futex */
1922 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1925 struct ust_app
*app
= NULL
;
1929 * Make sure we don't have node(s) that have hung up before receiving
1930 * the notify socket. This is to clean the list in order to avoid
1931 * memory leaks from notify socket that are never seen.
1933 sanitize_wait_queue(&wait_queue
);
1935 health_code_update();
1936 /* Dequeue command for registration */
1937 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
1939 DBG("Woken up but nothing in the UST command queue");
1940 /* Continue thread execution */
1944 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1946 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1947 " gid:%d sock:%d name:%s (version %d.%d)",
1948 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1949 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1950 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1951 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1953 if (ust_cmd
->reg_msg
.type
== USTCTL_SOCKET_CMD
) {
1954 wait_node
= zmalloc(sizeof(*wait_node
));
1956 PERROR("zmalloc wait_node dispatch");
1957 ret
= close(ust_cmd
->sock
);
1959 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1961 lttng_fd_put(LTTNG_FD_APPS
, 1);
1965 CDS_INIT_LIST_HEAD(&wait_node
->head
);
1967 /* Create application object if socket is CMD. */
1968 wait_node
->app
= ust_app_create(&ust_cmd
->reg_msg
,
1970 if (!wait_node
->app
) {
1971 ret
= close(ust_cmd
->sock
);
1973 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
1975 lttng_fd_put(LTTNG_FD_APPS
, 1);
1981 * Add application to the wait queue so we can set the notify
1982 * socket before putting this object in the global ht.
1984 cds_list_add(&wait_node
->head
, &wait_queue
.head
);
1989 * We have to continue here since we don't have the notify
1990 * socket and the application MUST be added to the hash table
1991 * only at that moment.
1996 * Look for the application in the local wait queue and set the
1997 * notify socket if found.
1999 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2000 &wait_queue
.head
, head
) {
2001 health_code_update();
2002 if (wait_node
->app
->pid
== ust_cmd
->reg_msg
.pid
) {
2003 wait_node
->app
->notify_sock
= ust_cmd
->sock
;
2004 cds_list_del(&wait_node
->head
);
2006 app
= wait_node
->app
;
2008 DBG3("UST app notify socket %d is set", ust_cmd
->sock
);
2014 * With no application at this stage the received socket is
2015 * basically useless so close it before we free the cmd data
2016 * structure for good.
2019 ret
= close(ust_cmd
->sock
);
2021 PERROR("close ust sock dispatch %d", ust_cmd
->sock
);
2023 lttng_fd_put(LTTNG_FD_APPS
, 1);
2030 * @session_lock_list
2032 * Lock the global session list so from the register up to the
2033 * registration done message, no thread can see the application
2034 * and change its state.
2036 session_lock_list();
2040 * Add application to the global hash table. This needs to be
2041 * done before the update to the UST registry can locate the
2046 /* Set app version. This call will print an error if needed. */
2047 (void) ust_app_version(app
);
2049 /* Send notify socket through the notify pipe. */
2050 ret
= send_socket_to_thread(apps_cmd_notify_pipe
[1],
2054 session_unlock_list();
2056 * No notify thread, stop the UST tracing. However, this is
2057 * not an internal error of the this thread thus setting
2058 * the health error code to a normal exit.
2065 * Update newly registered application with the tracing
2066 * registry info already enabled information.
2068 update_ust_app(app
->sock
);
2071 * Don't care about return value. Let the manage apps threads
2072 * handle app unregistration upon socket close.
2074 (void) ust_app_register_done(app
);
2077 * Even if the application socket has been closed, send the app
2078 * to the thread and unregistration will take place at that
2081 ret
= send_socket_to_thread(apps_cmd_pipe
[1], app
->sock
);
2084 session_unlock_list();
2086 * No apps. thread, stop the UST tracing. However, this is
2087 * not an internal error of the this thread thus setting
2088 * the health error code to a normal exit.
2095 session_unlock_list();
2097 } while (node
!= NULL
);
2099 health_poll_entry();
2100 /* Futex wait on queue. Blocking call on futex() */
2101 futex_nto1_wait(&ust_cmd_queue
.futex
);
2104 /* Normal exit, no error */
2108 /* Clean up wait queue. */
2109 cds_list_for_each_entry_safe(wait_node
, tmp_wait_node
,
2110 &wait_queue
.head
, head
) {
2111 cds_list_del(&wait_node
->head
);
2116 /* Empty command queue. */
2118 /* Dequeue command for registration */
2119 node
= cds_wfcq_dequeue_blocking(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
2123 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
2124 ret
= close(ust_cmd
->sock
);
2126 PERROR("close ust sock exit dispatch %d", ust_cmd
->sock
);
2128 lttng_fd_put(LTTNG_FD_APPS
, 1);
2133 DBG("Dispatch thread dying");
2136 ERR("Health error occurred in %s", __func__
);
2138 health_unregister(health_sessiond
);
2143 * This thread manage application registration.
2145 static void *thread_registration_apps(void *data
)
2147 int sock
= -1, i
, ret
, pollfd
, err
= -1;
2148 uint32_t revents
, nb_fd
;
2149 struct lttng_poll_event events
;
2151 * Get allocated in this thread, enqueued to a global queue, dequeued and
2152 * freed in the manage apps thread.
2154 struct ust_command
*ust_cmd
= NULL
;
2156 DBG("[thread] Manage application registration started");
2158 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_APP_REG
);
2160 if (testpoint(sessiond_thread_registration_apps
)) {
2161 goto error_testpoint
;
2164 ret
= lttcomm_listen_unix_sock(apps_sock
);
2170 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
2171 * more will be added to this poll set.
2173 ret
= sessiond_set_thread_pollset(&events
, 2);
2175 goto error_create_poll
;
2178 /* Add the application registration socket */
2179 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
2181 goto error_poll_add
;
2184 /* Notify all applications to register */
2185 ret
= notify_ust_apps(1);
2187 ERR("Failed to notify applications or create the wait shared memory.\n"
2188 "Execution continues but there might be problem for already\n"
2189 "running applications that wishes to register.");
2193 DBG("Accepting application registration");
2195 /* Inifinite blocking call, waiting for transmission */
2197 health_poll_entry();
2198 ret
= lttng_poll_wait(&events
, -1);
2202 * Restart interrupted system call.
2204 if (errno
== EINTR
) {
2212 for (i
= 0; i
< nb_fd
; i
++) {
2213 health_code_update();
2215 /* Fetch once the poll data */
2216 revents
= LTTNG_POLL_GETEV(&events
, i
);
2217 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2220 /* No activity for this FD (poll implementation). */
2224 /* Thread quit pipe has been closed. Killing thread. */
2225 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
2231 /* Event on the registration socket */
2232 if (pollfd
== apps_sock
) {
2233 if (revents
& LPOLLIN
) {
2234 sock
= lttcomm_accept_unix_sock(apps_sock
);
2240 * Set socket timeout for both receiving and ending.
2241 * app_socket_timeout is in seconds, whereas
2242 * lttcomm_setsockopt_rcv_timeout and
2243 * lttcomm_setsockopt_snd_timeout expect msec as
2246 if (app_socket_timeout
>= 0) {
2247 (void) lttcomm_setsockopt_rcv_timeout(sock
,
2248 app_socket_timeout
* 1000);
2249 (void) lttcomm_setsockopt_snd_timeout(sock
,
2250 app_socket_timeout
* 1000);
2254 * Set the CLOEXEC flag. Return code is useless because
2255 * either way, the show must go on.
2257 (void) utils_set_fd_cloexec(sock
);
2259 /* Create UST registration command for enqueuing */
2260 ust_cmd
= zmalloc(sizeof(struct ust_command
));
2261 if (ust_cmd
== NULL
) {
2262 PERROR("ust command zmalloc");
2271 * Using message-based transmissions to ensure we don't
2272 * have to deal with partially received messages.
2274 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2276 ERR("Exhausted file descriptors allowed for applications.");
2286 health_code_update();
2287 ret
= ust_app_recv_registration(sock
, &ust_cmd
->reg_msg
);
2290 /* Close socket of the application. */
2295 lttng_fd_put(LTTNG_FD_APPS
, 1);
2299 health_code_update();
2301 ust_cmd
->sock
= sock
;
2304 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2305 " gid:%d sock:%d name:%s (version %d.%d)",
2306 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
2307 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
2308 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
2309 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
2312 * Lock free enqueue the registration request. The red pill
2313 * has been taken! This apps will be part of the *system*.
2315 cds_wfcq_enqueue(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
, &ust_cmd
->node
);
2318 * Wake the registration queue futex. Implicit memory
2319 * barrier with the exchange in cds_wfcq_enqueue.
2321 futex_nto1_wake(&ust_cmd_queue
.futex
);
2322 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2323 ERR("Register apps socket poll error");
2326 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2335 /* Notify that the registration thread is gone */
2338 if (apps_sock
>= 0) {
2339 ret
= close(apps_sock
);
2349 lttng_fd_put(LTTNG_FD_APPS
, 1);
2351 unlink(apps_unix_sock_path
);
2354 lttng_poll_clean(&events
);
2358 DBG("UST Registration thread cleanup complete");
2361 ERR("Health error occurred in %s", __func__
);
2363 health_unregister(health_sessiond
);
2369 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2370 * exec or it will fails.
2372 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
2375 struct timespec timeout
;
2377 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2378 consumer_data
->consumer_thread_is_ready
= 0;
2380 /* Setup pthread condition */
2381 ret
= pthread_condattr_init(&consumer_data
->condattr
);
2384 PERROR("pthread_condattr_init consumer data");
2389 * Set the monotonic clock in order to make sure we DO NOT jump in time
2390 * between the clock_gettime() call and the timedwait call. See bug #324
2391 * for a more details and how we noticed it.
2393 ret
= pthread_condattr_setclock(&consumer_data
->condattr
, CLOCK_MONOTONIC
);
2396 PERROR("pthread_condattr_setclock consumer data");
2400 ret
= pthread_cond_init(&consumer_data
->cond
, &consumer_data
->condattr
);
2403 PERROR("pthread_cond_init consumer data");
2407 ret
= pthread_create(&consumer_data
->thread
, NULL
, thread_manage_consumer
,
2411 PERROR("pthread_create consumer");
2416 /* We are about to wait on a pthread condition */
2417 pthread_mutex_lock(&consumer_data
->cond_mutex
);
2419 /* Get time for sem_timedwait absolute timeout */
2420 clock_ret
= clock_gettime(CLOCK_MONOTONIC
, &timeout
);
2422 * Set the timeout for the condition timed wait even if the clock gettime
2423 * call fails since we might loop on that call and we want to avoid to
2424 * increment the timeout too many times.
2426 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
2429 * The following loop COULD be skipped in some conditions so this is why we
2430 * set ret to 0 in order to make sure at least one round of the loop is
2436 * Loop until the condition is reached or when a timeout is reached. Note
2437 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2438 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2439 * possible. This loop does not take any chances and works with both of
2442 while (!consumer_data
->consumer_thread_is_ready
&& ret
!= ETIMEDOUT
) {
2443 if (clock_ret
< 0) {
2444 PERROR("clock_gettime spawn consumer");
2445 /* Infinite wait for the consumerd thread to be ready */
2446 ret
= pthread_cond_wait(&consumer_data
->cond
,
2447 &consumer_data
->cond_mutex
);
2449 ret
= pthread_cond_timedwait(&consumer_data
->cond
,
2450 &consumer_data
->cond_mutex
, &timeout
);
2454 /* Release the pthread condition */
2455 pthread_mutex_unlock(&consumer_data
->cond_mutex
);
2459 if (ret
== ETIMEDOUT
) {
2463 * Call has timed out so we kill the kconsumerd_thread and return
2466 ERR("Condition timed out. The consumer thread was never ready."
2468 pth_ret
= pthread_cancel(consumer_data
->thread
);
2470 PERROR("pthread_cancel consumer thread");
2473 PERROR("pthread_cond_wait failed consumer thread");
2475 /* Caller is expecting a negative value on failure. */
2480 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2481 if (consumer_data
->pid
== 0) {
2482 ERR("Consumerd did not start");
2483 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2486 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2495 * Join consumer thread
2497 static int join_consumer_thread(struct consumer_data
*consumer_data
)
2501 /* Consumer pid must be a real one. */
2502 if (consumer_data
->pid
> 0) {
2504 ret
= kill(consumer_data
->pid
, SIGTERM
);
2506 PERROR("Error killing consumer daemon");
2509 return pthread_join(consumer_data
->thread
, &status
);
2516 * Fork and exec a consumer daemon (consumerd).
2518 * Return pid if successful else -1.
2520 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
2524 const char *consumer_to_use
;
2525 const char *verbosity
;
2528 DBG("Spawning consumerd");
2535 if (opt_verbose_consumer
) {
2536 verbosity
= "--verbose";
2537 } else if (lttng_opt_quiet
) {
2538 verbosity
= "--quiet";
2543 switch (consumer_data
->type
) {
2544 case LTTNG_CONSUMER_KERNEL
:
2546 * Find out which consumerd to execute. We will first try the
2547 * 64-bit path, then the sessiond's installation directory, and
2548 * fallback on the 32-bit one,
2550 DBG3("Looking for a kernel consumer at these locations:");
2551 DBG3(" 1) %s", consumerd64_bin
);
2552 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
2553 DBG3(" 3) %s", consumerd32_bin
);
2554 if (stat(consumerd64_bin
, &st
) == 0) {
2555 DBG3("Found location #1");
2556 consumer_to_use
= consumerd64_bin
;
2557 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
2558 DBG3("Found location #2");
2559 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
2560 } else if (stat(consumerd32_bin
, &st
) == 0) {
2561 DBG3("Found location #3");
2562 consumer_to_use
= consumerd32_bin
;
2564 DBG("Could not find any valid consumerd executable");
2568 DBG("Using kernel consumer at: %s", consumer_to_use
);
2569 ret
= execl(consumer_to_use
,
2570 "lttng-consumerd", verbosity
, "-k",
2571 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2572 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2573 "--group", tracing_group_name
,
2576 case LTTNG_CONSUMER64_UST
:
2578 char *tmpnew
= NULL
;
2580 if (consumerd64_libdir
[0] != '\0') {
2584 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2588 tmplen
= strlen("LD_LIBRARY_PATH=")
2589 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
2590 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2595 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2596 strcat(tmpnew
, consumerd64_libdir
);
2597 if (tmp
[0] != '\0') {
2598 strcat(tmpnew
, ":");
2599 strcat(tmpnew
, tmp
);
2601 ret
= putenv(tmpnew
);
2608 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
2609 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
2610 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2611 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2612 "--group", tracing_group_name
,
2614 if (consumerd64_libdir
[0] != '\0') {
2619 case LTTNG_CONSUMER32_UST
:
2621 char *tmpnew
= NULL
;
2623 if (consumerd32_libdir
[0] != '\0') {
2627 tmp
= lttng_secure_getenv("LD_LIBRARY_PATH");
2631 tmplen
= strlen("LD_LIBRARY_PATH=")
2632 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
2633 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
2638 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
2639 strcat(tmpnew
, consumerd32_libdir
);
2640 if (tmp
[0] != '\0') {
2641 strcat(tmpnew
, ":");
2642 strcat(tmpnew
, tmp
);
2644 ret
= putenv(tmpnew
);
2651 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
2652 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
2653 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
2654 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
2655 "--group", tracing_group_name
,
2657 if (consumerd32_libdir
[0] != '\0') {
2663 PERROR("unknown consumer type");
2667 PERROR("Consumer execl()");
2669 /* Reaching this point, we got a failure on our execl(). */
2671 } else if (pid
> 0) {
2674 PERROR("start consumer fork");
2682 * Spawn the consumerd daemon and session daemon thread.
2684 static int start_consumerd(struct consumer_data
*consumer_data
)
2689 * Set the listen() state on the socket since there is a possible race
2690 * between the exec() of the consumer daemon and this call if place in the
2691 * consumer thread. See bug #366 for more details.
2693 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
2698 pthread_mutex_lock(&consumer_data
->pid_mutex
);
2699 if (consumer_data
->pid
!= 0) {
2700 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2704 ret
= spawn_consumerd(consumer_data
);
2706 ERR("Spawning consumerd failed");
2707 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2711 /* Setting up the consumer_data pid */
2712 consumer_data
->pid
= ret
;
2713 DBG2("Consumer pid %d", consumer_data
->pid
);
2714 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
2716 DBG2("Spawning consumer control thread");
2717 ret
= spawn_consumer_thread(consumer_data
);
2719 ERR("Fatal error spawning consumer control thread");
2727 /* Cleanup already created sockets on error. */
2728 if (consumer_data
->err_sock
>= 0) {
2731 err
= close(consumer_data
->err_sock
);
2733 PERROR("close consumer data error socket");
2740 * Setup necessary data for kernel tracer action.
2742 static int init_kernel_tracer(void)
2746 /* Modprobe lttng kernel modules */
2747 ret
= modprobe_lttng_control();
2752 /* Open debugfs lttng */
2753 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
2754 if (kernel_tracer_fd
< 0) {
2755 DBG("Failed to open %s", module_proc_lttng
);
2760 /* Validate kernel version */
2761 ret
= kernel_validate_version(kernel_tracer_fd
);
2766 ret
= modprobe_lttng_data();
2771 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2775 modprobe_remove_lttng_control();
2776 ret
= close(kernel_tracer_fd
);
2780 kernel_tracer_fd
= -1;
2781 return LTTNG_ERR_KERN_VERSION
;
2784 ret
= close(kernel_tracer_fd
);
2790 modprobe_remove_lttng_control();
2793 WARN("No kernel tracer available");
2794 kernel_tracer_fd
= -1;
2796 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2798 return LTTNG_ERR_KERN_NA
;
2804 * Copy consumer output from the tracing session to the domain session. The
2805 * function also applies the right modification on a per domain basis for the
2806 * trace files destination directory.
2808 * Should *NOT* be called with RCU read-side lock held.
2810 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2813 const char *dir_name
;
2814 struct consumer_output
*consumer
;
2817 assert(session
->consumer
);
2820 case LTTNG_DOMAIN_KERNEL
:
2821 DBG3("Copying tracing session consumer output in kernel session");
2823 * XXX: We should audit the session creation and what this function
2824 * does "extra" in order to avoid a destroy since this function is used
2825 * in the domain session creation (kernel and ust) only. Same for UST
2828 if (session
->kernel_session
->consumer
) {
2829 consumer_output_put(session
->kernel_session
->consumer
);
2831 session
->kernel_session
->consumer
=
2832 consumer_copy_output(session
->consumer
);
2833 /* Ease our life a bit for the next part */
2834 consumer
= session
->kernel_session
->consumer
;
2835 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2837 case LTTNG_DOMAIN_JUL
:
2838 case LTTNG_DOMAIN_LOG4J
:
2839 case LTTNG_DOMAIN_PYTHON
:
2840 case LTTNG_DOMAIN_UST
:
2841 DBG3("Copying tracing session consumer output in UST session");
2842 if (session
->ust_session
->consumer
) {
2843 consumer_output_put(session
->ust_session
->consumer
);
2845 session
->ust_session
->consumer
=
2846 consumer_copy_output(session
->consumer
);
2847 /* Ease our life a bit for the next part */
2848 consumer
= session
->ust_session
->consumer
;
2849 dir_name
= DEFAULT_UST_TRACE_DIR
;
2852 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2856 /* Append correct directory to subdir */
2857 strncat(consumer
->subdir
, dir_name
,
2858 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
2859 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2868 * Create an UST session and add it to the session ust list.
2870 * Should *NOT* be called with RCU read-side lock held.
2872 static int create_ust_session(struct ltt_session
*session
,
2873 struct lttng_domain
*domain
)
2876 struct ltt_ust_session
*lus
= NULL
;
2880 assert(session
->consumer
);
2882 switch (domain
->type
) {
2883 case LTTNG_DOMAIN_JUL
:
2884 case LTTNG_DOMAIN_LOG4J
:
2885 case LTTNG_DOMAIN_PYTHON
:
2886 case LTTNG_DOMAIN_UST
:
2889 ERR("Unknown UST domain on create session %d", domain
->type
);
2890 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2894 DBG("Creating UST session");
2896 lus
= trace_ust_create_session(session
->id
);
2898 ret
= LTTNG_ERR_UST_SESS_FAIL
;
2902 lus
->uid
= session
->uid
;
2903 lus
->gid
= session
->gid
;
2904 lus
->output_traces
= session
->output_traces
;
2905 lus
->snapshot_mode
= session
->snapshot_mode
;
2906 lus
->live_timer_interval
= session
->live_timer
;
2907 session
->ust_session
= lus
;
2908 if (session
->shm_path
[0]) {
2909 strncpy(lus
->root_shm_path
, session
->shm_path
,
2910 sizeof(lus
->root_shm_path
));
2911 lus
->root_shm_path
[sizeof(lus
->root_shm_path
) - 1] = '\0';
2912 strncpy(lus
->shm_path
, session
->shm_path
,
2913 sizeof(lus
->shm_path
));
2914 lus
->shm_path
[sizeof(lus
->shm_path
) - 1] = '\0';
2915 strncat(lus
->shm_path
, "/ust",
2916 sizeof(lus
->shm_path
) - strlen(lus
->shm_path
) - 1);
2918 /* Copy session output to the newly created UST session */
2919 ret
= copy_session_consumer(domain
->type
, session
);
2920 if (ret
!= LTTNG_OK
) {
2928 session
->ust_session
= NULL
;
2933 * Create a kernel tracer session then create the default channel.
2935 static int create_kernel_session(struct ltt_session
*session
)
2939 DBG("Creating kernel session");
2941 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2943 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
2947 /* Code flow safety */
2948 assert(session
->kernel_session
);
2950 /* Copy session output to the newly created Kernel session */
2951 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2952 if (ret
!= LTTNG_OK
) {
2956 /* Create directory(ies) on local filesystem. */
2957 if (session
->kernel_session
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2958 strlen(session
->kernel_session
->consumer
->dst
.trace_path
) > 0) {
2959 ret
= run_as_mkdir_recursive(
2960 session
->kernel_session
->consumer
->dst
.trace_path
,
2961 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2963 if (errno
!= EEXIST
) {
2964 ERR("Trace directory creation error");
2970 session
->kernel_session
->uid
= session
->uid
;
2971 session
->kernel_session
->gid
= session
->gid
;
2972 session
->kernel_session
->output_traces
= session
->output_traces
;
2973 session
->kernel_session
->snapshot_mode
= session
->snapshot_mode
;
2978 trace_kernel_destroy_session(session
->kernel_session
);
2979 session
->kernel_session
= NULL
;
2984 * Count number of session permitted by uid/gid.
2986 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2989 struct ltt_session
*session
;
2991 DBG("Counting number of available session for UID %d GID %d",
2993 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2995 * Only list the sessions the user can control.
2997 if (!session_access_ok(session
, uid
, gid
)) {
3006 * Process the command requested by the lttng client within the command
3007 * context structure. This function make sure that the return structure (llm)
3008 * is set and ready for transmission before returning.
3010 * Return any error encountered or 0 for success.
3012 * "sock" is only used for special-case var. len data.
3014 * Should *NOT* be called with RCU read-side lock held.
3016 static int process_client_msg(struct command_ctx
*cmd_ctx
, int sock
,
3020 int need_tracing_session
= 1;
3023 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
3025 assert(!rcu_read_ongoing());
3029 switch (cmd_ctx
->lsm
->cmd_type
) {
3030 case LTTNG_CREATE_SESSION
:
3031 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3032 case LTTNG_CREATE_SESSION_LIVE
:
3033 case LTTNG_DESTROY_SESSION
:
3034 case LTTNG_LIST_SESSIONS
:
3035 case LTTNG_LIST_DOMAINS
:
3036 case LTTNG_START_TRACE
:
3037 case LTTNG_STOP_TRACE
:
3038 case LTTNG_DATA_PENDING
:
3039 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
3040 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
3041 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
3042 case LTTNG_SNAPSHOT_RECORD
:
3043 case LTTNG_SAVE_SESSION
:
3044 case LTTNG_SET_SESSION_SHM_PATH
:
3045 case LTTNG_METADATA_REGENERATE
:
3052 if (opt_no_kernel
&& need_domain
3053 && cmd_ctx
->lsm
->domain
.type
== LTTNG_DOMAIN_KERNEL
) {
3055 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3057 ret
= LTTNG_ERR_KERN_NA
;
3062 /* Deny register consumer if we already have a spawned consumer. */
3063 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_REGISTER_CONSUMER
) {
3064 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3065 if (kconsumer_data
.pid
> 0) {
3066 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3067 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3070 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3074 * Check for command that don't needs to allocate a returned payload. We do
3075 * this here so we don't have to make the call for no payload at each
3078 switch(cmd_ctx
->lsm
->cmd_type
) {
3079 case LTTNG_LIST_SESSIONS
:
3080 case LTTNG_LIST_TRACEPOINTS
:
3081 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3082 case LTTNG_LIST_DOMAINS
:
3083 case LTTNG_LIST_CHANNELS
:
3084 case LTTNG_LIST_EVENTS
:
3085 case LTTNG_LIST_SYSCALLS
:
3086 case LTTNG_LIST_TRACKER_PIDS
:
3087 case LTTNG_DATA_PENDING
:
3090 /* Setup lttng message with no payload */
3091 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0);
3093 /* This label does not try to unlock the session */
3094 goto init_setup_error
;
3098 /* Commands that DO NOT need a session. */
3099 switch (cmd_ctx
->lsm
->cmd_type
) {
3100 case LTTNG_CREATE_SESSION
:
3101 case LTTNG_CREATE_SESSION_SNAPSHOT
:
3102 case LTTNG_CREATE_SESSION_LIVE
:
3103 case LTTNG_CALIBRATE
:
3104 case LTTNG_LIST_SESSIONS
:
3105 case LTTNG_LIST_TRACEPOINTS
:
3106 case LTTNG_LIST_SYSCALLS
:
3107 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3108 case LTTNG_SAVE_SESSION
:
3109 need_tracing_session
= 0;
3112 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
3114 * We keep the session list lock across _all_ commands
3115 * for now, because the per-session lock does not
3116 * handle teardown properly.
3118 session_lock_list();
3119 cmd_ctx
->session
= session_find_by_name(cmd_ctx
->lsm
->session
.name
);
3120 if (cmd_ctx
->session
== NULL
) {
3121 ret
= LTTNG_ERR_SESS_NOT_FOUND
;
3124 /* Acquire lock for the session */
3125 session_lock(cmd_ctx
->session
);
3131 * Commands that need a valid session but should NOT create one if none
3132 * exists. Instead of creating one and destroying it when the command is
3133 * handled, process that right before so we save some round trip in useless
3136 switch (cmd_ctx
->lsm
->cmd_type
) {
3137 case LTTNG_DISABLE_CHANNEL
:
3138 case LTTNG_DISABLE_EVENT
:
3139 switch (cmd_ctx
->lsm
->domain
.type
) {
3140 case LTTNG_DOMAIN_KERNEL
:
3141 if (!cmd_ctx
->session
->kernel_session
) {
3142 ret
= LTTNG_ERR_NO_CHANNEL
;
3146 case LTTNG_DOMAIN_JUL
:
3147 case LTTNG_DOMAIN_LOG4J
:
3148 case LTTNG_DOMAIN_PYTHON
:
3149 case LTTNG_DOMAIN_UST
:
3150 if (!cmd_ctx
->session
->ust_session
) {
3151 ret
= LTTNG_ERR_NO_CHANNEL
;
3156 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3168 * Check domain type for specific "pre-action".
3170 switch (cmd_ctx
->lsm
->domain
.type
) {
3171 case LTTNG_DOMAIN_KERNEL
:
3173 ret
= LTTNG_ERR_NEED_ROOT_SESSIOND
;
3177 /* Kernel tracer check */
3178 if (kernel_tracer_fd
== -1) {
3179 /* Basically, load kernel tracer modules */
3180 ret
= init_kernel_tracer();
3186 /* Consumer is in an ERROR state. Report back to client */
3187 if (uatomic_read(&kernel_consumerd_state
) == CONSUMER_ERROR
) {
3188 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3192 /* Need a session for kernel command */
3193 if (need_tracing_session
) {
3194 if (cmd_ctx
->session
->kernel_session
== NULL
) {
3195 ret
= create_kernel_session(cmd_ctx
->session
);
3197 ret
= LTTNG_ERR_KERN_SESS_FAIL
;
3202 /* Start the kernel consumer daemon */
3203 pthread_mutex_lock(&kconsumer_data
.pid_mutex
);
3204 if (kconsumer_data
.pid
== 0 &&
3205 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3206 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3207 ret
= start_consumerd(&kconsumer_data
);
3209 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3212 uatomic_set(&kernel_consumerd_state
, CONSUMER_STARTED
);
3214 pthread_mutex_unlock(&kconsumer_data
.pid_mutex
);
3218 * The consumer was just spawned so we need to add the socket to
3219 * the consumer output of the session if exist.
3221 ret
= consumer_create_socket(&kconsumer_data
,
3222 cmd_ctx
->session
->kernel_session
->consumer
);
3229 case LTTNG_DOMAIN_JUL
:
3230 case LTTNG_DOMAIN_LOG4J
:
3231 case LTTNG_DOMAIN_PYTHON
:
3232 case LTTNG_DOMAIN_UST
:
3234 if (!ust_app_supported()) {
3235 ret
= LTTNG_ERR_NO_UST
;
3238 /* Consumer is in an ERROR state. Report back to client */
3239 if (uatomic_read(&ust_consumerd_state
) == CONSUMER_ERROR
) {
3240 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3244 if (need_tracing_session
) {
3245 /* Create UST session if none exist. */
3246 if (cmd_ctx
->session
->ust_session
== NULL
) {
3247 ret
= create_ust_session(cmd_ctx
->session
,
3248 &cmd_ctx
->lsm
->domain
);
3249 if (ret
!= LTTNG_OK
) {
3254 /* Start the UST consumer daemons */
3256 pthread_mutex_lock(&ustconsumer64_data
.pid_mutex
);
3257 if (consumerd64_bin
[0] != '\0' &&
3258 ustconsumer64_data
.pid
== 0 &&
3259 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3260 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3261 ret
= start_consumerd(&ustconsumer64_data
);
3263 ret
= LTTNG_ERR_UST_CONSUMER64_FAIL
;
3264 uatomic_set(&ust_consumerd64_fd
, -EINVAL
);
3268 uatomic_set(&ust_consumerd64_fd
, ustconsumer64_data
.cmd_sock
);
3269 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3271 pthread_mutex_unlock(&ustconsumer64_data
.pid_mutex
);
3275 * Setup socket for consumer 64 bit. No need for atomic access
3276 * since it was set above and can ONLY be set in this thread.
3278 ret
= consumer_create_socket(&ustconsumer64_data
,
3279 cmd_ctx
->session
->ust_session
->consumer
);
3285 pthread_mutex_lock(&ustconsumer32_data
.pid_mutex
);
3286 if (consumerd32_bin
[0] != '\0' &&
3287 ustconsumer32_data
.pid
== 0 &&
3288 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
3289 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3290 ret
= start_consumerd(&ustconsumer32_data
);
3292 ret
= LTTNG_ERR_UST_CONSUMER32_FAIL
;
3293 uatomic_set(&ust_consumerd32_fd
, -EINVAL
);
3297 uatomic_set(&ust_consumerd32_fd
, ustconsumer32_data
.cmd_sock
);
3298 uatomic_set(&ust_consumerd_state
, CONSUMER_STARTED
);
3300 pthread_mutex_unlock(&ustconsumer32_data
.pid_mutex
);
3304 * Setup socket for consumer 64 bit. No need for atomic access
3305 * since it was set above and can ONLY be set in this thread.
3307 ret
= consumer_create_socket(&ustconsumer32_data
,
3308 cmd_ctx
->session
->ust_session
->consumer
);
3320 /* Validate consumer daemon state when start/stop trace command */
3321 if (cmd_ctx
->lsm
->cmd_type
== LTTNG_START_TRACE
||
3322 cmd_ctx
->lsm
->cmd_type
== LTTNG_STOP_TRACE
) {
3323 switch (cmd_ctx
->lsm
->domain
.type
) {
3324 case LTTNG_DOMAIN_NONE
:
3326 case LTTNG_DOMAIN_JUL
:
3327 case LTTNG_DOMAIN_LOG4J
:
3328 case LTTNG_DOMAIN_PYTHON
:
3329 case LTTNG_DOMAIN_UST
:
3330 if (uatomic_read(&ust_consumerd_state
) != CONSUMER_STARTED
) {
3331 ret
= LTTNG_ERR_NO_USTCONSUMERD
;
3335 case LTTNG_DOMAIN_KERNEL
:
3336 if (uatomic_read(&kernel_consumerd_state
) != CONSUMER_STARTED
) {
3337 ret
= LTTNG_ERR_NO_KERNCONSUMERD
;
3342 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
3348 * Check that the UID or GID match that of the tracing session.
3349 * The root user can interact with all sessions.
3351 if (need_tracing_session
) {
3352 if (!session_access_ok(cmd_ctx
->session
,
3353 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3354 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
))) {
3355 ret
= LTTNG_ERR_EPERM
;
3361 * Send relayd information to consumer as soon as we have a domain and a
3364 if (cmd_ctx
->session
&& need_domain
) {
3366 * Setup relayd if not done yet. If the relayd information was already
3367 * sent to the consumer, this call will gracefully return.
3369 ret
= cmd_setup_relayd(cmd_ctx
->session
);
3370 if (ret
!= LTTNG_OK
) {
3375 /* Process by command type */
3376 switch (cmd_ctx
->lsm
->cmd_type
) {
3377 case LTTNG_ADD_CONTEXT
:
3380 * An LTTNG_ADD_CONTEXT command might have a supplementary
3381 * payload if the context being added is an application context.
3383 if (cmd_ctx
->lsm
->u
.context
.ctx
.ctx
==
3384 LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
3385 char *provider_name
= NULL
, *context_name
= NULL
;
3386 size_t provider_name_len
=
3387 cmd_ctx
->lsm
->u
.context
.provider_name_len
;
3388 size_t context_name_len
=
3389 cmd_ctx
->lsm
->u
.context
.context_name_len
;
3391 if (provider_name_len
== 0 || context_name_len
== 0) {
3393 * Application provider and context names MUST
3396 ret
= -LTTNG_ERR_INVALID
;
3400 provider_name
= zmalloc(provider_name_len
+ 1);
3401 if (!provider_name
) {
3402 ret
= -LTTNG_ERR_NOMEM
;
3405 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
=
3408 context_name
= zmalloc(context_name_len
+ 1);
3409 if (!context_name
) {
3410 ret
= -LTTNG_ERR_NOMEM
;
3411 goto error_add_context
;
3413 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
=
3416 ret
= lttcomm_recv_unix_sock(sock
, provider_name
,
3419 goto error_add_context
;
3422 ret
= lttcomm_recv_unix_sock(sock
, context_name
,
3425 goto error_add_context
;
3430 * cmd_add_context assumes ownership of the provider and context
3433 ret
= cmd_add_context(cmd_ctx
->session
,
3434 cmd_ctx
->lsm
->domain
.type
,
3435 cmd_ctx
->lsm
->u
.context
.channel_name
,
3436 &cmd_ctx
->lsm
->u
.context
.ctx
,
3437 kernel_poll_pipe
[1]);
3439 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
= NULL
;
3440 cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
= NULL
;
3442 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.provider_name
);
3443 free(cmd_ctx
->lsm
->u
.context
.ctx
.u
.app_ctx
.ctx_name
);
3449 case LTTNG_DISABLE_CHANNEL
:
3451 ret
= cmd_disable_channel(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3452 cmd_ctx
->lsm
->u
.disable
.channel_name
);
3455 case LTTNG_DISABLE_EVENT
:
3459 * FIXME: handle filter; for now we just receive the filter's
3460 * bytecode along with the filter expression which are sent by
3461 * liblttng-ctl and discard them.
3463 * This fixes an issue where the client may block while sending
3464 * the filter payload and encounter an error because the session
3465 * daemon closes the socket without ever handling this data.
3467 size_t count
= cmd_ctx
->lsm
->u
.disable
.expression_len
+
3468 cmd_ctx
->lsm
->u
.disable
.bytecode_len
;
3471 char data
[LTTNG_FILTER_MAX_LEN
];
3473 DBG("Discarding disable event command payload of size %zu", count
);
3475 ret
= lttcomm_recv_unix_sock(sock
, data
,
3476 count
> sizeof(data
) ? sizeof(data
) : count
);
3481 count
-= (size_t) ret
;
3484 /* FIXME: passing packed structure to non-packed pointer */
3485 ret
= cmd_disable_event(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3486 cmd_ctx
->lsm
->u
.disable
.channel_name
,
3487 &cmd_ctx
->lsm
->u
.disable
.event
);
3490 case LTTNG_ENABLE_CHANNEL
:
3492 ret
= cmd_enable_channel(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3493 &cmd_ctx
->lsm
->u
.channel
.chan
, kernel_poll_pipe
[1]);
3496 case LTTNG_TRACK_PID
:
3498 ret
= cmd_track_pid(cmd_ctx
->session
,
3499 cmd_ctx
->lsm
->domain
.type
,
3500 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3503 case LTTNG_UNTRACK_PID
:
3505 ret
= cmd_untrack_pid(cmd_ctx
->session
,
3506 cmd_ctx
->lsm
->domain
.type
,
3507 cmd_ctx
->lsm
->u
.pid_tracker
.pid
);
3510 case LTTNG_ENABLE_EVENT
:
3512 struct lttng_event_exclusion
*exclusion
= NULL
;
3513 struct lttng_filter_bytecode
*bytecode
= NULL
;
3514 char *filter_expression
= NULL
;
3516 /* Handle exclusion events and receive it from the client. */
3517 if (cmd_ctx
->lsm
->u
.enable
.exclusion_count
> 0) {
3518 size_t count
= cmd_ctx
->lsm
->u
.enable
.exclusion_count
;
3520 exclusion
= zmalloc(sizeof(struct lttng_event_exclusion
) +
3521 (count
* LTTNG_SYMBOL_NAME_LEN
));
3523 ret
= LTTNG_ERR_EXCLUSION_NOMEM
;
3527 DBG("Receiving var len exclusion event list from client ...");
3528 exclusion
->count
= count
;
3529 ret
= lttcomm_recv_unix_sock(sock
, exclusion
->names
,
3530 count
* LTTNG_SYMBOL_NAME_LEN
);
3532 DBG("Nothing recv() from client var len data... continuing");
3535 ret
= LTTNG_ERR_EXCLUSION_INVAL
;
3540 /* Get filter expression from client. */
3541 if (cmd_ctx
->lsm
->u
.enable
.expression_len
> 0) {
3542 size_t expression_len
=
3543 cmd_ctx
->lsm
->u
.enable
.expression_len
;
3545 if (expression_len
> LTTNG_FILTER_MAX_LEN
) {
3546 ret
= LTTNG_ERR_FILTER_INVAL
;
3551 filter_expression
= zmalloc(expression_len
);
3552 if (!filter_expression
) {
3554 ret
= LTTNG_ERR_FILTER_NOMEM
;
3558 /* Receive var. len. data */
3559 DBG("Receiving var len filter's expression from client ...");
3560 ret
= lttcomm_recv_unix_sock(sock
, filter_expression
,
3563 DBG("Nothing recv() from client car len data... continuing");
3565 free(filter_expression
);
3567 ret
= LTTNG_ERR_FILTER_INVAL
;
3572 /* Handle filter and get bytecode from client. */
3573 if (cmd_ctx
->lsm
->u
.enable
.bytecode_len
> 0) {
3574 size_t bytecode_len
= cmd_ctx
->lsm
->u
.enable
.bytecode_len
;
3576 if (bytecode_len
> LTTNG_FILTER_MAX_LEN
) {
3577 ret
= LTTNG_ERR_FILTER_INVAL
;
3578 free(filter_expression
);
3583 bytecode
= zmalloc(bytecode_len
);
3585 free(filter_expression
);
3587 ret
= LTTNG_ERR_FILTER_NOMEM
;
3591 /* Receive var. len. data */
3592 DBG("Receiving var len filter's bytecode from client ...");
3593 ret
= lttcomm_recv_unix_sock(sock
, bytecode
, bytecode_len
);
3595 DBG("Nothing recv() from client car len data... continuing");
3597 free(filter_expression
);
3600 ret
= LTTNG_ERR_FILTER_INVAL
;
3604 if ((bytecode
->len
+ sizeof(*bytecode
)) != bytecode_len
) {
3605 free(filter_expression
);
3608 ret
= LTTNG_ERR_FILTER_INVAL
;
3613 ret
= cmd_enable_event(cmd_ctx
->session
, &cmd_ctx
->lsm
->domain
,
3614 cmd_ctx
->lsm
->u
.enable
.channel_name
,
3615 &cmd_ctx
->lsm
->u
.enable
.event
,
3616 filter_expression
, bytecode
, exclusion
,
3617 kernel_poll_pipe
[1]);
3620 case LTTNG_LIST_TRACEPOINTS
:
3622 struct lttng_event
*events
;
3625 session_lock_list();
3626 nb_events
= cmd_list_tracepoints(cmd_ctx
->lsm
->domain
.type
, &events
);
3627 session_unlock_list();
3628 if (nb_events
< 0) {
3629 /* Return value is a negative lttng_error_code. */
3635 * Setup lttng message with payload size set to the event list size in
3636 * bytes and then copy list into the llm payload.
3638 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3639 sizeof(struct lttng_event
) * nb_events
);
3649 case LTTNG_LIST_TRACEPOINT_FIELDS
:
3651 struct lttng_event_field
*fields
;
3654 session_lock_list();
3655 nb_fields
= cmd_list_tracepoint_fields(cmd_ctx
->lsm
->domain
.type
,
3657 session_unlock_list();
3658 if (nb_fields
< 0) {
3659 /* Return value is a negative lttng_error_code. */
3665 * Setup lttng message with payload size set to the event list size in
3666 * bytes and then copy list into the llm payload.
3668 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, fields
,
3669 sizeof(struct lttng_event_field
) * nb_fields
);
3679 case LTTNG_LIST_SYSCALLS
:
3681 struct lttng_event
*events
;
3684 nb_events
= cmd_list_syscalls(&events
);
3685 if (nb_events
< 0) {
3686 /* Return value is a negative lttng_error_code. */
3692 * Setup lttng message with payload size set to the event list size in
3693 * bytes and then copy list into the llm payload.
3695 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, events
,
3696 sizeof(struct lttng_event
) * nb_events
);
3706 case LTTNG_LIST_TRACKER_PIDS
:
3708 int32_t *pids
= NULL
;
3711 nr_pids
= cmd_list_tracker_pids(cmd_ctx
->session
,
3712 cmd_ctx
->lsm
->domain
.type
, &pids
);
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
, pids
,
3724 sizeof(int32_t) * nr_pids
);
3734 case LTTNG_SET_CONSUMER_URI
:
3737 struct lttng_uri
*uris
;
3739 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3740 len
= nb_uri
* sizeof(struct lttng_uri
);
3743 ret
= LTTNG_ERR_INVALID
;
3747 uris
= zmalloc(len
);
3749 ret
= LTTNG_ERR_FATAL
;
3753 /* Receive variable len data */
3754 DBG("Receiving %zu URI(s) from client ...", nb_uri
);
3755 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3757 DBG("No URIs received from client... continuing");
3759 ret
= LTTNG_ERR_SESSION_FAIL
;
3764 ret
= cmd_set_consumer_uri(cmd_ctx
->session
, nb_uri
, uris
);
3766 if (ret
!= LTTNG_OK
) {
3773 case LTTNG_START_TRACE
:
3775 ret
= cmd_start_trace(cmd_ctx
->session
);
3778 case LTTNG_STOP_TRACE
:
3780 ret
= cmd_stop_trace(cmd_ctx
->session
);
3783 case LTTNG_CREATE_SESSION
:
3786 struct lttng_uri
*uris
= NULL
;
3788 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
3789 len
= nb_uri
* sizeof(struct lttng_uri
);
3792 uris
= zmalloc(len
);
3794 ret
= LTTNG_ERR_FATAL
;
3798 /* Receive variable len data */
3799 DBG("Waiting for %zu URIs from client ...", nb_uri
);
3800 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
3802 DBG("No URIs received from client... continuing");
3804 ret
= LTTNG_ERR_SESSION_FAIL
;
3809 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
3810 DBG("Creating session with ONE network URI is a bad call");
3811 ret
= LTTNG_ERR_SESSION_FAIL
;
3817 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
, nb_uri
,
3818 &cmd_ctx
->creds
, 0);
3824 case LTTNG_DESTROY_SESSION
:
3826 ret
= cmd_destroy_session(cmd_ctx
->session
, kernel_poll_pipe
[1]);
3828 /* Set session to NULL so we do not unlock it after free. */
3829 cmd_ctx
->session
= NULL
;
3832 case LTTNG_LIST_DOMAINS
:
3835 struct lttng_domain
*domains
= NULL
;
3837 nb_dom
= cmd_list_domains(cmd_ctx
->session
, &domains
);
3839 /* Return value is a negative lttng_error_code. */
3844 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, domains
,
3845 nb_dom
* sizeof(struct lttng_domain
));
3855 case LTTNG_LIST_CHANNELS
:
3857 ssize_t payload_size
;
3858 struct lttng_channel
*channels
= NULL
;
3860 payload_size
= cmd_list_channels(cmd_ctx
->lsm
->domain
.type
,
3861 cmd_ctx
->session
, &channels
);
3862 if (payload_size
< 0) {
3863 /* Return value is a negative lttng_error_code. */
3864 ret
= -payload_size
;
3868 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, channels
,
3879 case LTTNG_LIST_EVENTS
:
3882 struct lttng_event
*events
= NULL
;
3883 struct lttcomm_event_command_header cmd_header
;
3886 memset(&cmd_header
, 0, sizeof(cmd_header
));
3887 /* Extended infos are included at the end of events */
3888 nb_event
= cmd_list_events(cmd_ctx
->lsm
->domain
.type
,
3889 cmd_ctx
->session
, cmd_ctx
->lsm
->u
.list
.channel_name
,
3890 &events
, &total_size
);
3893 /* Return value is a negative lttng_error_code. */
3898 cmd_header
.nb_events
= nb_event
;
3899 ret
= setup_lttng_msg(cmd_ctx
, events
, total_size
,
3900 &cmd_header
, sizeof(cmd_header
));
3910 case LTTNG_LIST_SESSIONS
:
3912 unsigned int nr_sessions
;
3913 void *sessions_payload
;
3916 session_lock_list();
3917 nr_sessions
= lttng_sessions_count(
3918 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3919 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3920 payload_len
= sizeof(struct lttng_session
) * nr_sessions
;
3921 sessions_payload
= zmalloc(payload_len
);
3923 if (!sessions_payload
) {
3924 session_unlock_list();
3929 cmd_list_lttng_sessions(sessions_payload
,
3930 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx
->creds
),
3931 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx
->creds
));
3932 session_unlock_list();
3934 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, sessions_payload
,
3936 free(sessions_payload
);
3945 case LTTNG_CALIBRATE
:
3947 ret
= cmd_calibrate(cmd_ctx
->lsm
->domain
.type
,
3948 &cmd_ctx
->lsm
->u
.calibrate
);
3951 case LTTNG_REGISTER_CONSUMER
:
3953 struct consumer_data
*cdata
;
3955 switch (cmd_ctx
->lsm
->domain
.type
) {
3956 case LTTNG_DOMAIN_KERNEL
:
3957 cdata
= &kconsumer_data
;
3960 ret
= LTTNG_ERR_UND
;
3964 ret
= cmd_register_consumer(cmd_ctx
->session
, cmd_ctx
->lsm
->domain
.type
,
3965 cmd_ctx
->lsm
->u
.reg
.path
, cdata
);
3968 case LTTNG_DATA_PENDING
:
3971 uint8_t pending_ret_byte
;
3973 pending_ret
= cmd_data_pending(cmd_ctx
->session
);
3978 * This function may returns 0 or 1 to indicate whether or not
3979 * there is data pending. In case of error, it should return an
3980 * LTTNG_ERR code. However, some code paths may still return
3981 * a nondescript error code, which we handle by returning an
3984 if (pending_ret
== 0 || pending_ret
== 1) {
3986 * ret will be set to LTTNG_OK at the end of
3989 } else if (pending_ret
< 0) {
3990 ret
= LTTNG_ERR_UNK
;
3997 pending_ret_byte
= (uint8_t) pending_ret
;
3999 /* 1 byte to return whether or not data is pending */
4000 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
,
4001 &pending_ret_byte
, 1);
4010 case LTTNG_SNAPSHOT_ADD_OUTPUT
:
4012 struct lttcomm_lttng_output_id reply
;
4014 ret
= cmd_snapshot_add_output(cmd_ctx
->session
,
4015 &cmd_ctx
->lsm
->u
.snapshot_output
.output
, &reply
.id
);
4016 if (ret
!= LTTNG_OK
) {
4020 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, &reply
,
4026 /* Copy output list into message payload */
4030 case LTTNG_SNAPSHOT_DEL_OUTPUT
:
4032 ret
= cmd_snapshot_del_output(cmd_ctx
->session
,
4033 &cmd_ctx
->lsm
->u
.snapshot_output
.output
);
4036 case LTTNG_SNAPSHOT_LIST_OUTPUT
:
4039 struct lttng_snapshot_output
*outputs
= NULL
;
4041 nb_output
= cmd_snapshot_list_outputs(cmd_ctx
->session
, &outputs
);
4042 if (nb_output
< 0) {
4047 assert((nb_output
> 0 && outputs
) || nb_output
== 0);
4048 ret
= setup_lttng_msg_no_cmd_header(cmd_ctx
, outputs
,
4049 nb_output
* sizeof(struct lttng_snapshot_output
));
4059 case LTTNG_SNAPSHOT_RECORD
:
4061 ret
= cmd_snapshot_record(cmd_ctx
->session
,
4062 &cmd_ctx
->lsm
->u
.snapshot_record
.output
,
4063 cmd_ctx
->lsm
->u
.snapshot_record
.wait
);
4066 case LTTNG_CREATE_SESSION_SNAPSHOT
:
4069 struct lttng_uri
*uris
= NULL
;
4071 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4072 len
= nb_uri
* sizeof(struct lttng_uri
);
4075 uris
= zmalloc(len
);
4077 ret
= LTTNG_ERR_FATAL
;
4081 /* Receive variable len data */
4082 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4083 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4085 DBG("No URIs received from client... continuing");
4087 ret
= LTTNG_ERR_SESSION_FAIL
;
4092 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4093 DBG("Creating session with ONE network URI is a bad call");
4094 ret
= LTTNG_ERR_SESSION_FAIL
;
4100 ret
= cmd_create_session_snapshot(cmd_ctx
->lsm
->session
.name
, uris
,
4101 nb_uri
, &cmd_ctx
->creds
);
4105 case LTTNG_CREATE_SESSION_LIVE
:
4108 struct lttng_uri
*uris
= NULL
;
4110 nb_uri
= cmd_ctx
->lsm
->u
.uri
.size
;
4111 len
= nb_uri
* sizeof(struct lttng_uri
);
4114 uris
= zmalloc(len
);
4116 ret
= LTTNG_ERR_FATAL
;
4120 /* Receive variable len data */
4121 DBG("Waiting for %zu URIs from client ...", nb_uri
);
4122 ret
= lttcomm_recv_unix_sock(sock
, uris
, len
);
4124 DBG("No URIs received from client... continuing");
4126 ret
= LTTNG_ERR_SESSION_FAIL
;
4131 if (nb_uri
== 1 && uris
[0].dtype
!= LTTNG_DST_PATH
) {
4132 DBG("Creating session with ONE network URI is a bad call");
4133 ret
= LTTNG_ERR_SESSION_FAIL
;
4139 ret
= cmd_create_session_uri(cmd_ctx
->lsm
->session
.name
, uris
,
4140 nb_uri
, &cmd_ctx
->creds
, cmd_ctx
->lsm
->u
.session_live
.timer_interval
);
4144 case LTTNG_SAVE_SESSION
:
4146 ret
= cmd_save_sessions(&cmd_ctx
->lsm
->u
.save_session
.attr
,
4150 case LTTNG_SET_SESSION_SHM_PATH
:
4152 ret
= cmd_set_session_shm_path(cmd_ctx
->session
,
4153 cmd_ctx
->lsm
->u
.set_shm_path
.shm_path
);
4156 case LTTNG_METADATA_REGENERATE
:
4158 ret
= cmd_metadata_regenerate(cmd_ctx
->session
);
4162 ret
= LTTNG_ERR_UND
;
4167 if (cmd_ctx
->llm
== NULL
) {
4168 DBG("Missing llm structure. Allocating one.");
4169 if (setup_lttng_msg_no_cmd_header(cmd_ctx
, NULL
, 0) < 0) {
4173 /* Set return code */
4174 cmd_ctx
->llm
->ret_code
= ret
;
4176 if (cmd_ctx
->session
) {
4177 session_unlock(cmd_ctx
->session
);
4179 if (need_tracing_session
) {
4180 session_unlock_list();
4183 assert(!rcu_read_ongoing());
4188 * Thread managing health check socket.
4190 static void *thread_manage_health(void *data
)
4192 int sock
= -1, new_sock
= -1, ret
, i
, pollfd
, err
= -1;
4193 uint32_t revents
, nb_fd
;
4194 struct lttng_poll_event events
;
4195 struct health_comm_msg msg
;
4196 struct health_comm_reply reply
;
4198 DBG("[thread] Manage health check started");
4200 rcu_register_thread();
4202 /* We might hit an error path before this is created. */
4203 lttng_poll_init(&events
);
4205 /* Create unix socket */
4206 sock
= lttcomm_create_unix_sock(health_unix_sock_path
);
4208 ERR("Unable to create health check Unix socket");
4214 /* lttng health client socket path permissions */
4215 ret
= chown(health_unix_sock_path
, 0,
4216 utils_get_group_id(tracing_group_name
));
4218 ERR("Unable to set group on %s", health_unix_sock_path
);
4224 ret
= chmod(health_unix_sock_path
,
4225 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
4227 ERR("Unable to set permissions on %s", health_unix_sock_path
);
4235 * Set the CLOEXEC flag. Return code is useless because either way, the
4238 (void) utils_set_fd_cloexec(sock
);
4240 ret
= lttcomm_listen_unix_sock(sock
);
4246 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4247 * more will be added to this poll set.
4249 ret
= sessiond_set_thread_pollset(&events
, 2);
4254 /* Add the application registration socket */
4255 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLPRI
);
4260 sessiond_notify_ready();
4263 DBG("Health check ready");
4265 /* Inifinite blocking call, waiting for transmission */
4267 ret
= lttng_poll_wait(&events
, -1);
4270 * Restart interrupted system call.
4272 if (errno
== EINTR
) {
4280 for (i
= 0; i
< nb_fd
; i
++) {
4281 /* Fetch once the poll data */
4282 revents
= LTTNG_POLL_GETEV(&events
, i
);
4283 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4286 /* No activity for this FD (poll implementation). */
4290 /* Thread quit pipe has been closed. Killing thread. */
4291 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4297 /* Event on the registration socket */
4298 if (pollfd
== sock
) {
4299 if (revents
& LPOLLIN
) {
4301 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4302 ERR("Health socket poll error");
4305 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4311 new_sock
= lttcomm_accept_unix_sock(sock
);
4317 * Set the CLOEXEC flag. Return code is useless because either way, the
4320 (void) utils_set_fd_cloexec(new_sock
);
4322 DBG("Receiving data from client for health...");
4323 ret
= lttcomm_recv_unix_sock(new_sock
, (void *)&msg
, sizeof(msg
));
4325 DBG("Nothing recv() from client... continuing");
4326 ret
= close(new_sock
);
4334 rcu_thread_online();
4336 memset(&reply
, 0, sizeof(reply
));
4337 for (i
= 0; i
< NR_HEALTH_SESSIOND_TYPES
; i
++) {
4339 * health_check_state returns 0 if health is
4342 if (!health_check_state(health_sessiond
, i
)) {
4343 reply
.ret_code
|= 1ULL << i
;
4347 DBG2("Health check return value %" PRIx64
, reply
.ret_code
);
4349 ret
= send_unix_sock(new_sock
, (void *) &reply
, sizeof(reply
));
4351 ERR("Failed to send health data back to client");
4354 /* End of transmission */
4355 ret
= close(new_sock
);
4365 ERR("Health error occurred in %s", __func__
);
4367 DBG("Health check thread dying");
4368 unlink(health_unix_sock_path
);
4376 lttng_poll_clean(&events
);
4378 rcu_unregister_thread();
4383 * This thread manage all clients request using the unix client socket for
4386 static void *thread_manage_clients(void *data
)
4388 int sock
= -1, ret
, i
, pollfd
, err
= -1;
4390 uint32_t revents
, nb_fd
;
4391 struct command_ctx
*cmd_ctx
= NULL
;
4392 struct lttng_poll_event events
;
4394 DBG("[thread] Manage client started");
4396 rcu_register_thread();
4398 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_CMD
);
4400 health_code_update();
4402 ret
= lttcomm_listen_unix_sock(client_sock
);
4408 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
4409 * more will be added to this poll set.
4411 ret
= sessiond_set_thread_pollset(&events
, 2);
4413 goto error_create_poll
;
4416 /* Add the application registration socket */
4417 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
4422 sessiond_notify_ready();
4423 ret
= sem_post(&load_info
->message_thread_ready
);
4425 PERROR("sem_post message_thread_ready");
4429 /* This testpoint is after we signal readiness to the parent. */
4430 if (testpoint(sessiond_thread_manage_clients
)) {
4434 if (testpoint(sessiond_thread_manage_clients_before_loop
)) {
4438 health_code_update();
4441 DBG("Accepting client command ...");
4443 /* Inifinite blocking call, waiting for transmission */
4445 health_poll_entry();
4446 ret
= lttng_poll_wait(&events
, -1);
4450 * Restart interrupted system call.
4452 if (errno
== EINTR
) {
4460 for (i
= 0; i
< nb_fd
; i
++) {
4461 /* Fetch once the poll data */
4462 revents
= LTTNG_POLL_GETEV(&events
, i
);
4463 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
4465 health_code_update();
4468 /* No activity for this FD (poll implementation). */
4472 /* Thread quit pipe has been closed. Killing thread. */
4473 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
4479 /* Event on the registration socket */
4480 if (pollfd
== client_sock
) {
4481 if (revents
& LPOLLIN
) {
4483 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
4484 ERR("Client socket poll error");
4487 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
4493 DBG("Wait for client response");
4495 health_code_update();
4497 sock
= lttcomm_accept_unix_sock(client_sock
);
4503 * Set the CLOEXEC flag. Return code is useless because either way, the
4506 (void) utils_set_fd_cloexec(sock
);
4508 /* Set socket option for credentials retrieval */
4509 ret
= lttcomm_setsockopt_creds_unix_sock(sock
);
4514 /* Allocate context command to process the client request */
4515 cmd_ctx
= zmalloc(sizeof(struct command_ctx
));
4516 if (cmd_ctx
== NULL
) {
4517 PERROR("zmalloc cmd_ctx");
4521 /* Allocate data buffer for reception */
4522 cmd_ctx
->lsm
= zmalloc(sizeof(struct lttcomm_session_msg
));
4523 if (cmd_ctx
->lsm
== NULL
) {
4524 PERROR("zmalloc cmd_ctx->lsm");
4528 cmd_ctx
->llm
= NULL
;
4529 cmd_ctx
->session
= NULL
;
4531 health_code_update();
4534 * Data is received from the lttng client. The struct
4535 * lttcomm_session_msg (lsm) contains the command and data request of
4538 DBG("Receiving data from client ...");
4539 ret
= lttcomm_recv_creds_unix_sock(sock
, cmd_ctx
->lsm
,
4540 sizeof(struct lttcomm_session_msg
), &cmd_ctx
->creds
);
4542 DBG("Nothing recv() from client... continuing");
4548 clean_command_ctx(&cmd_ctx
);
4552 health_code_update();
4554 // TODO: Validate cmd_ctx including sanity check for
4555 // security purpose.
4557 rcu_thread_online();
4559 * This function dispatch the work to the kernel or userspace tracer
4560 * libs and fill the lttcomm_lttng_msg data structure of all the needed
4561 * informations for the client. The command context struct contains
4562 * everything this function may needs.
4564 ret
= process_client_msg(cmd_ctx
, sock
, &sock_error
);
4565 rcu_thread_offline();
4573 * TODO: Inform client somehow of the fatal error. At
4574 * this point, ret < 0 means that a zmalloc failed
4575 * (ENOMEM). Error detected but still accept
4576 * command, unless a socket error has been
4579 clean_command_ctx(&cmd_ctx
);
4583 health_code_update();
4585 DBG("Sending response (size: %d, retcode: %s (%d))",
4586 cmd_ctx
->lttng_msg_size
,
4587 lttng_strerror(-cmd_ctx
->llm
->ret_code
),
4588 cmd_ctx
->llm
->ret_code
);
4589 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
4591 ERR("Failed to send data back to client");
4594 /* End of transmission */
4601 clean_command_ctx(&cmd_ctx
);
4603 health_code_update();
4615 lttng_poll_clean(&events
);
4616 clean_command_ctx(&cmd_ctx
);
4620 unlink(client_unix_sock_path
);
4621 if (client_sock
>= 0) {
4622 ret
= close(client_sock
);
4630 ERR("Health error occurred in %s", __func__
);
4633 health_unregister(health_sessiond
);
4635 DBG("Client thread dying");
4637 rcu_unregister_thread();
4640 * Since we are creating the consumer threads, we own them, so we need
4641 * to join them before our thread exits.
4643 ret
= join_consumer_thread(&kconsumer_data
);
4646 PERROR("join_consumer");
4649 ret
= join_consumer_thread(&ustconsumer32_data
);
4652 PERROR("join_consumer ust32");
4655 ret
= join_consumer_thread(&ustconsumer64_data
);
4658 PERROR("join_consumer ust64");
4663 static int string_match(const char *str1
, const char *str2
)
4665 return (str1
&& str2
) && !strcmp(str1
, str2
);
4669 * Take an option from the getopt output and set it in the right variable to be
4672 * Return 0 on success else a negative value.
4674 static int set_option(int opt
, const char *arg
, const char *optname
)
4678 if (string_match(optname
, "client-sock") || opt
== 'c') {
4679 if (!arg
|| *arg
== '\0') {
4683 if (lttng_is_setuid_setgid()) {
4684 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4685 "-c, --client-sock");
4687 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", arg
);
4689 } else if (string_match(optname
, "apps-sock") || opt
== 'a') {
4690 if (!arg
|| *arg
== '\0') {
4694 if (lttng_is_setuid_setgid()) {
4695 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4698 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", arg
);
4700 } else if (string_match(optname
, "daemonize") || opt
== 'd') {
4702 } else if (string_match(optname
, "background") || opt
== 'b') {
4704 } else if (string_match(optname
, "group") || opt
== 'g') {
4705 if (!arg
|| *arg
== '\0') {
4709 if (lttng_is_setuid_setgid()) {
4710 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4714 * If the override option is set, the pointer points to a
4715 * *non* const thus freeing it even though the variable type is
4718 if (tracing_group_name_override
) {
4719 free((void *) tracing_group_name
);
4721 tracing_group_name
= strdup(arg
);
4722 if (!tracing_group_name
) {
4726 tracing_group_name_override
= 1;
4728 } else if (string_match(optname
, "help") || opt
== 'h') {
4729 ret
= utils_show_man_page(8, "lttng-sessiond");
4731 ERR("Cannot view man page lttng-sessiond(8)");
4734 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
4735 } else if (string_match(optname
, "version") || opt
== 'V') {
4736 fprintf(stdout
, "%s\n", VERSION
);
4738 } else if (string_match(optname
, "sig-parent") || opt
== 'S') {
4740 } else if (string_match(optname
, "kconsumerd-err-sock")) {
4741 if (!arg
|| *arg
== '\0') {
4745 if (lttng_is_setuid_setgid()) {
4746 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4747 "--kconsumerd-err-sock");
4749 snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4751 } else if (string_match(optname
, "kconsumerd-cmd-sock")) {
4752 if (!arg
|| *arg
== '\0') {
4756 if (lttng_is_setuid_setgid()) {
4757 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4758 "--kconsumerd-cmd-sock");
4760 snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4762 } else if (string_match(optname
, "ustconsumerd64-err-sock")) {
4763 if (!arg
|| *arg
== '\0') {
4767 if (lttng_is_setuid_setgid()) {
4768 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4769 "--ustconsumerd64-err-sock");
4771 snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4773 } else if (string_match(optname
, "ustconsumerd64-cmd-sock")) {
4774 if (!arg
|| *arg
== '\0') {
4778 if (lttng_is_setuid_setgid()) {
4779 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4780 "--ustconsumerd64-cmd-sock");
4782 snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4784 } else if (string_match(optname
, "ustconsumerd32-err-sock")) {
4785 if (!arg
|| *arg
== '\0') {
4789 if (lttng_is_setuid_setgid()) {
4790 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4791 "--ustconsumerd32-err-sock");
4793 snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
, "%s", arg
);
4795 } else if (string_match(optname
, "ustconsumerd32-cmd-sock")) {
4796 if (!arg
|| *arg
== '\0') {
4800 if (lttng_is_setuid_setgid()) {
4801 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4802 "--ustconsumerd32-cmd-sock");
4804 snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
, "%s", arg
);
4806 } else if (string_match(optname
, "no-kernel")) {
4808 } else if (string_match(optname
, "quiet") || opt
== 'q') {
4809 lttng_opt_quiet
= 1;
4810 } else if (string_match(optname
, "verbose") || opt
== 'v') {
4811 /* Verbose level can increase using multiple -v */
4813 /* Value obtained from config file */
4814 lttng_opt_verbose
= config_parse_value(arg
);
4816 /* -v used on command line */
4817 lttng_opt_verbose
++;
4819 /* Clamp value to [0, 3] */
4820 lttng_opt_verbose
= lttng_opt_verbose
< 0 ? 0 :
4821 (lttng_opt_verbose
<= 3 ? lttng_opt_verbose
: 3);
4822 } else if (string_match(optname
, "verbose-consumer")) {
4824 opt_verbose_consumer
= config_parse_value(arg
);
4826 opt_verbose_consumer
++;
4828 } else if (string_match(optname
, "consumerd32-path")) {
4829 if (!arg
|| *arg
== '\0') {
4833 if (lttng_is_setuid_setgid()) {
4834 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4835 "--consumerd32-path");
4837 if (consumerd32_bin_override
) {
4838 free((void *) consumerd32_bin
);
4840 consumerd32_bin
= strdup(arg
);
4841 if (!consumerd32_bin
) {
4845 consumerd32_bin_override
= 1;
4847 } else if (string_match(optname
, "consumerd32-libdir")) {
4848 if (!arg
|| *arg
== '\0') {
4852 if (lttng_is_setuid_setgid()) {
4853 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4854 "--consumerd32-libdir");
4856 if (consumerd32_libdir_override
) {
4857 free((void *) consumerd32_libdir
);
4859 consumerd32_libdir
= strdup(arg
);
4860 if (!consumerd32_libdir
) {
4864 consumerd32_libdir_override
= 1;
4866 } else if (string_match(optname
, "consumerd64-path")) {
4867 if (!arg
|| *arg
== '\0') {
4871 if (lttng_is_setuid_setgid()) {
4872 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4873 "--consumerd64-path");
4875 if (consumerd64_bin_override
) {
4876 free((void *) consumerd64_bin
);
4878 consumerd64_bin
= strdup(arg
);
4879 if (!consumerd64_bin
) {
4883 consumerd64_bin_override
= 1;
4885 } else if (string_match(optname
, "consumerd64-libdir")) {
4886 if (!arg
|| *arg
== '\0') {
4890 if (lttng_is_setuid_setgid()) {
4891 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4892 "--consumerd64-libdir");
4894 if (consumerd64_libdir_override
) {
4895 free((void *) consumerd64_libdir
);
4897 consumerd64_libdir
= strdup(arg
);
4898 if (!consumerd64_libdir
) {
4902 consumerd64_libdir_override
= 1;
4904 } else if (string_match(optname
, "pidfile") || opt
== 'p') {
4905 if (!arg
|| *arg
== '\0') {
4909 if (lttng_is_setuid_setgid()) {
4910 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4914 opt_pidfile
= strdup(arg
);
4920 } else if (string_match(optname
, "agent-tcp-port")) {
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 "--agent-tcp-port");
4936 v
= strtoul(arg
, NULL
, 0);
4937 if (errno
!= 0 || !isdigit(arg
[0])) {
4938 ERR("Wrong value in --agent-tcp-port parameter: %s", arg
);
4941 if (v
== 0 || v
>= 65535) {
4942 ERR("Port overflow in --agent-tcp-port parameter: %s", arg
);
4945 agent_tcp_port
= (uint32_t) v
;
4946 DBG3("Agent TCP port set to non default: %u", agent_tcp_port
);
4948 } else if (string_match(optname
, "load") || opt
== 'l') {
4949 if (!arg
|| *arg
== '\0') {
4953 if (lttng_is_setuid_setgid()) {
4954 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4957 free(opt_load_session_path
);
4958 opt_load_session_path
= strdup(arg
);
4959 if (!opt_load_session_path
) {
4964 } else if (string_match(optname
, "kmod-probes")) {
4965 if (!arg
|| *arg
== '\0') {
4969 if (lttng_is_setuid_setgid()) {
4970 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4973 free(kmod_probes_list
);
4974 kmod_probes_list
= strdup(arg
);
4975 if (!kmod_probes_list
) {
4980 } else if (string_match(optname
, "extra-kmod-probes")) {
4981 if (!arg
|| *arg
== '\0') {
4985 if (lttng_is_setuid_setgid()) {
4986 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
4987 "--extra-kmod-probes");
4989 free(kmod_extra_probes_list
);
4990 kmod_extra_probes_list
= strdup(arg
);
4991 if (!kmod_extra_probes_list
) {
4996 } else if (string_match(optname
, "config") || opt
== 'f') {
4997 /* This is handled in set_options() thus silent skip. */
5000 /* Unknown option or other error.
5001 * Error is printed by getopt, just return */
5006 if (ret
== -EINVAL
) {
5007 const char *opt_name
= "unknown";
5010 for (i
= 0; i
< sizeof(long_options
) / sizeof(struct option
);
5012 if (opt
== long_options
[i
].val
) {
5013 opt_name
= long_options
[i
].name
;
5018 WARN("Invalid argument provided for option \"%s\", using default value.",
5026 * config_entry_handler_cb used to handle options read from a config file.
5027 * See config_entry_handler_cb comment in common/config/session-config.h for the
5028 * return value conventions.
5030 static int config_entry_handler(const struct config_entry
*entry
, void *unused
)
5034 if (!entry
|| !entry
->name
|| !entry
->value
) {
5039 /* Check if the option is to be ignored */
5040 for (i
= 0; i
< sizeof(config_ignore_options
) / sizeof(char *); i
++) {
5041 if (!strcmp(entry
->name
, config_ignore_options
[i
])) {
5046 for (i
= 0; i
< (sizeof(long_options
) / sizeof(struct option
)) - 1;
5049 /* Ignore if not fully matched. */
5050 if (strcmp(entry
->name
, long_options
[i
].name
)) {
5055 * If the option takes no argument on the command line, we have to
5056 * check if the value is "true". We support non-zero numeric values,
5059 if (!long_options
[i
].has_arg
) {
5060 ret
= config_parse_value(entry
->value
);
5063 WARN("Invalid configuration value \"%s\" for option %s",
5064 entry
->value
, entry
->name
);
5066 /* False, skip boolean config option. */
5071 ret
= set_option(long_options
[i
].val
, entry
->value
, entry
->name
);
5075 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry
->name
);
5082 * daemon configuration loading and argument parsing
5084 static int set_options(int argc
, char **argv
)
5086 int ret
= 0, c
= 0, option_index
= 0;
5087 int orig_optopt
= optopt
, orig_optind
= optind
;
5089 const char *config_path
= NULL
;
5091 optstring
= utils_generate_optstring(long_options
,
5092 sizeof(long_options
) / sizeof(struct option
));
5098 /* Check for the --config option */
5099 while ((c
= getopt_long(argc
, argv
, optstring
, long_options
,
5100 &option_index
)) != -1) {
5104 } else if (c
!= 'f') {
5105 /* if not equal to --config option. */
5109 if (lttng_is_setuid_setgid()) {
5110 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
5113 config_path
= utils_expand_path(optarg
);
5115 ERR("Failed to resolve path: %s", optarg
);
5120 ret
= config_get_section_entries(config_path
, config_section_name
,
5121 config_entry_handler
, NULL
);
5124 ERR("Invalid configuration option at line %i", ret
);
5130 /* Reset getopt's global state */
5131 optopt
= orig_optopt
;
5132 optind
= orig_optind
;
5136 * getopt_long() will not set option_index if it encounters a
5139 c
= getopt_long(argc
, argv
, optstring
, long_options
,
5146 * Pass NULL as the long option name if popt left the index
5149 ret
= set_option(c
, optarg
,
5150 option_index
< 0 ? NULL
:
5151 long_options
[option_index
].name
);
5163 * Creates the two needed socket by the daemon.
5164 * apps_sock - The communication socket for all UST apps.
5165 * client_sock - The communication of the cli tool (lttng).
5167 static int init_daemon_socket(void)
5172 old_umask
= umask(0);
5174 /* Create client tool unix socket */
5175 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
5176 if (client_sock
< 0) {
5177 ERR("Create unix sock failed: %s", client_unix_sock_path
);
5182 /* Set the cloexec flag */
5183 ret
= utils_set_fd_cloexec(client_sock
);
5185 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
5186 "Continuing but note that the consumer daemon will have a "
5187 "reference to this socket on exec()", client_sock
);
5190 /* File permission MUST be 660 */
5191 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5193 ERR("Set file permissions failed: %s", client_unix_sock_path
);
5198 /* Create the application unix socket */
5199 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
5200 if (apps_sock
< 0) {
5201 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
5206 /* Set the cloexec flag */
5207 ret
= utils_set_fd_cloexec(apps_sock
);
5209 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
5210 "Continuing but note that the consumer daemon will have a "
5211 "reference to this socket on exec()", apps_sock
);
5214 /* File permission MUST be 666 */
5215 ret
= chmod(apps_unix_sock_path
,
5216 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
5218 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
5223 DBG3("Session daemon client socket %d and application socket %d created",
5224 client_sock
, apps_sock
);
5232 * Check if the global socket is available, and if a daemon is answering at the
5233 * other side. If yes, error is returned.
5235 static int check_existing_daemon(void)
5237 /* Is there anybody out there ? */
5238 if (lttng_session_daemon_alive()) {
5246 * Set the tracing group gid onto the client socket.
5248 * Race window between mkdir and chown is OK because we are going from more
5249 * permissive (root.root) to less permissive (root.tracing).
5251 static int set_permissions(char *rundir
)
5256 gid
= utils_get_group_id(tracing_group_name
);
5258 /* Set lttng run dir */
5259 ret
= chown(rundir
, 0, gid
);
5261 ERR("Unable to set group on %s", rundir
);
5266 * Ensure all applications and tracing group can search the run
5267 * dir. Allow everyone to read the directory, since it does not
5268 * buy us anything to hide its content.
5270 ret
= chmod(rundir
, S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
5272 ERR("Unable to set permissions on %s", rundir
);
5276 /* lttng client socket path */
5277 ret
= chown(client_unix_sock_path
, 0, gid
);
5279 ERR("Unable to set group on %s", client_unix_sock_path
);
5283 /* kconsumer error socket path */
5284 ret
= chown(kconsumer_data
.err_unix_sock_path
, 0, 0);
5286 ERR("Unable to set group on %s", kconsumer_data
.err_unix_sock_path
);
5290 /* 64-bit ustconsumer error socket path */
5291 ret
= chown(ustconsumer64_data
.err_unix_sock_path
, 0, 0);
5293 ERR("Unable to set group on %s", ustconsumer64_data
.err_unix_sock_path
);
5297 /* 32-bit ustconsumer compat32 error socket path */
5298 ret
= chown(ustconsumer32_data
.err_unix_sock_path
, 0, 0);
5300 ERR("Unable to set group on %s", ustconsumer32_data
.err_unix_sock_path
);
5304 DBG("All permissions are set");
5310 * Create the lttng run directory needed for all global sockets and pipe.
5312 static int create_lttng_rundir(const char *rundir
)
5316 DBG3("Creating LTTng run directory: %s", rundir
);
5318 ret
= mkdir(rundir
, S_IRWXU
);
5320 if (errno
!= EEXIST
) {
5321 ERR("Unable to create %s", rundir
);
5333 * Setup sockets and directory needed by the kconsumerd communication with the
5336 static int set_consumer_sockets(struct consumer_data
*consumer_data
,
5340 char path
[PATH_MAX
];
5342 switch (consumer_data
->type
) {
5343 case LTTNG_CONSUMER_KERNEL
:
5344 snprintf(path
, PATH_MAX
, DEFAULT_KCONSUMERD_PATH
, rundir
);
5346 case LTTNG_CONSUMER64_UST
:
5347 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD64_PATH
, rundir
);
5349 case LTTNG_CONSUMER32_UST
:
5350 snprintf(path
, PATH_MAX
, DEFAULT_USTCONSUMERD32_PATH
, rundir
);
5353 ERR("Consumer type unknown");
5358 DBG2("Creating consumer directory: %s", path
);
5360 ret
= mkdir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
5362 if (errno
!= EEXIST
) {
5364 ERR("Failed to create %s", path
);
5370 ret
= chown(path
, 0, utils_get_group_id(tracing_group_name
));
5372 ERR("Unable to set group on %s", path
);
5378 /* Create the kconsumerd error unix socket */
5379 consumer_data
->err_sock
=
5380 lttcomm_create_unix_sock(consumer_data
->err_unix_sock_path
);
5381 if (consumer_data
->err_sock
< 0) {
5382 ERR("Create unix sock failed: %s", consumer_data
->err_unix_sock_path
);
5388 * Set the CLOEXEC flag. Return code is useless because either way, the
5391 ret
= utils_set_fd_cloexec(consumer_data
->err_sock
);
5393 PERROR("utils_set_fd_cloexec");
5394 /* continue anyway */
5397 /* File permission MUST be 660 */
5398 ret
= chmod(consumer_data
->err_unix_sock_path
,
5399 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
5401 ERR("Set file permissions failed: %s", consumer_data
->err_unix_sock_path
);
5411 * Signal handler for the daemon
5413 * Simply stop all worker threads, leaving main() return gracefully after
5414 * joining all threads and calling cleanup().
5416 static void sighandler(int sig
)
5420 DBG("SIGPIPE caught");
5423 DBG("SIGINT caught");
5427 DBG("SIGTERM caught");
5431 CMM_STORE_SHARED(recv_child_signal
, 1);
5439 * Setup signal handler for :
5440 * SIGINT, SIGTERM, SIGPIPE
5442 static int set_signal_handler(void)
5445 struct sigaction sa
;
5448 if ((ret
= sigemptyset(&sigset
)) < 0) {
5449 PERROR("sigemptyset");
5453 sa
.sa_handler
= sighandler
;
5454 sa
.sa_mask
= sigset
;
5456 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
5457 PERROR("sigaction");
5461 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
5462 PERROR("sigaction");
5466 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
5467 PERROR("sigaction");
5471 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
5472 PERROR("sigaction");
5476 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
5482 * Set open files limit to unlimited. This daemon can open a large number of
5483 * file descriptors in order to consumer multiple kernel traces.
5485 static void set_ulimit(void)
5490 /* The kernel does not allowed an infinite limit for open files */
5491 lim
.rlim_cur
= 65535;
5492 lim
.rlim_max
= 65535;
5494 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
5496 PERROR("failed to set open files limit");
5501 * Write pidfile using the rundir and opt_pidfile.
5503 static int write_pidfile(void)
5506 char pidfile_path
[PATH_MAX
];
5511 if (lttng_strncpy(pidfile_path
, opt_pidfile
, sizeof(pidfile_path
))) {
5516 /* Build pidfile path from rundir and opt_pidfile. */
5517 ret
= snprintf(pidfile_path
, sizeof(pidfile_path
), "%s/"
5518 DEFAULT_LTTNG_SESSIOND_PIDFILE
, rundir
);
5520 PERROR("snprintf pidfile path");
5526 * Create pid file in rundir.
5528 ret
= utils_create_pid_file(getpid(), pidfile_path
);
5534 * Create lockfile using the rundir and return its fd.
5536 static int create_lockfile(void)
5539 char lockfile_path
[PATH_MAX
];
5541 ret
= generate_lock_file_path(lockfile_path
, sizeof(lockfile_path
));
5546 ret
= utils_create_lock_file(lockfile_path
);
5552 * Write agent TCP port using the rundir.
5554 static int write_agent_port(void)
5557 char path
[PATH_MAX
];
5561 ret
= snprintf(path
, sizeof(path
), "%s/"
5562 DEFAULT_LTTNG_SESSIOND_AGENTPORT_FILE
, rundir
);
5564 PERROR("snprintf agent port path");
5569 * Create TCP agent port file in rundir.
5571 ret
= utils_create_pid_file(agent_tcp_port
, path
);
5580 int main(int argc
, char **argv
)
5582 int ret
= 0, retval
= 0;
5584 const char *home_path
, *env_app_timeout
;
5586 init_kernel_workarounds();
5588 rcu_register_thread();
5590 if (set_signal_handler()) {
5592 goto exit_set_signal_handler
;
5595 setup_consumerd_path();
5597 page_size
= sysconf(_SC_PAGESIZE
);
5598 if (page_size
< 0) {
5599 PERROR("sysconf _SC_PAGESIZE");
5600 page_size
= LONG_MAX
;
5601 WARN("Fallback page size to %ld", page_size
);
5605 * Parse arguments and load the daemon configuration file.
5607 * We have an exit_options exit path to free memory reserved by
5608 * set_options. This is needed because the rest of sessiond_cleanup()
5609 * depends on ht_cleanup_thread, which depends on lttng_daemonize, which
5610 * depends on set_options.
5613 if (set_options(argc
, argv
)) {
5619 if (opt_daemon
|| opt_background
) {
5622 ret
= lttng_daemonize(&child_ppid
, &recv_child_signal
,
5630 * We are in the child. Make sure all other file descriptors are
5631 * closed, in case we are called with more opened file
5632 * descriptors than the standard ones.
5634 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
5639 if (run_as_create_worker(argv
[0]) < 0) {
5640 goto exit_create_run_as_worker_cleanup
;
5644 * Starting from here, we can create threads. This needs to be after
5645 * lttng_daemonize due to RCU.
5649 * Initialize the health check subsystem. This call should set the
5650 * appropriate time values.
5652 health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
5653 if (!health_sessiond
) {
5654 PERROR("health_app_create error");
5656 goto exit_health_sessiond_cleanup
;
5659 if (init_ht_cleanup_quit_pipe()) {
5661 goto exit_ht_cleanup_quit_pipe
;
5664 /* Setup the thread ht_cleanup communication pipe. */
5665 if (utils_create_pipe_cloexec(ht_cleanup_pipe
)) {
5667 goto exit_ht_cleanup_pipe
;
5670 /* Set up max poll set size */
5671 if (lttng_poll_set_max_size()) {
5673 goto exit_set_max_size
;
5676 /* Create thread to clean up RCU hash tables */
5677 ret
= pthread_create(&ht_cleanup_thread
, NULL
,
5678 thread_ht_cleanup
, (void *) NULL
);
5681 PERROR("pthread_create ht_cleanup");
5683 goto exit_ht_cleanup
;
5686 /* Create thread quit pipe */
5687 if (init_thread_quit_pipe()) {
5689 goto exit_init_data
;
5692 /* Check if daemon is UID = 0 */
5693 is_root
= !getuid();
5696 rundir
= strdup(DEFAULT_LTTNG_RUNDIR
);
5699 goto exit_init_data
;
5702 /* Create global run dir with root access */
5703 if (create_lttng_rundir(rundir
)) {
5705 goto exit_init_data
;
5708 if (strlen(apps_unix_sock_path
) == 0) {
5709 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5710 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
5713 goto exit_init_data
;
5717 if (strlen(client_unix_sock_path
) == 0) {
5718 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5719 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
5722 goto exit_init_data
;
5726 /* Set global SHM for ust */
5727 if (strlen(wait_shm_path
) == 0) {
5728 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5729 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
5732 goto exit_init_data
;
5736 if (strlen(health_unix_sock_path
) == 0) {
5737 ret
= snprintf(health_unix_sock_path
,
5738 sizeof(health_unix_sock_path
),
5739 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK
);
5742 goto exit_init_data
;
5746 /* Setup kernel consumerd path */
5747 ret
= snprintf(kconsumer_data
.err_unix_sock_path
, PATH_MAX
,
5748 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
, rundir
);
5751 goto exit_init_data
;
5753 ret
= snprintf(kconsumer_data
.cmd_unix_sock_path
, PATH_MAX
,
5754 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
, rundir
);
5757 goto exit_init_data
;
5760 DBG2("Kernel consumer err path: %s",
5761 kconsumer_data
.err_unix_sock_path
);
5762 DBG2("Kernel consumer cmd path: %s",
5763 kconsumer_data
.cmd_unix_sock_path
);
5765 home_path
= utils_get_home_dir();
5766 if (home_path
== NULL
) {
5767 /* TODO: Add --socket PATH option */
5768 ERR("Can't get HOME directory for sockets creation.");
5770 goto exit_init_data
;
5774 * Create rundir from home path. This will create something like
5777 ret
= asprintf(&rundir
, DEFAULT_LTTNG_HOME_RUNDIR
, home_path
);
5780 goto exit_init_data
;
5783 if (create_lttng_rundir(rundir
)) {
5785 goto exit_init_data
;
5788 if (strlen(apps_unix_sock_path
) == 0) {
5789 ret
= snprintf(apps_unix_sock_path
, PATH_MAX
,
5790 DEFAULT_HOME_APPS_UNIX_SOCK
,
5794 goto exit_init_data
;
5798 /* Set the cli tool unix socket path */
5799 if (strlen(client_unix_sock_path
) == 0) {
5800 ret
= snprintf(client_unix_sock_path
, PATH_MAX
,
5801 DEFAULT_HOME_CLIENT_UNIX_SOCK
,
5805 goto exit_init_data
;
5809 /* Set global SHM for ust */
5810 if (strlen(wait_shm_path
) == 0) {
5811 ret
= snprintf(wait_shm_path
, PATH_MAX
,
5812 DEFAULT_HOME_APPS_WAIT_SHM_PATH
,
5816 goto exit_init_data
;
5820 /* Set health check Unix path */
5821 if (strlen(health_unix_sock_path
) == 0) {
5822 ret
= snprintf(health_unix_sock_path
,
5823 sizeof(health_unix_sock_path
),
5824 DEFAULT_HOME_HEALTH_UNIX_SOCK
,
5828 goto exit_init_data
;
5833 lockfile_fd
= create_lockfile();
5834 if (lockfile_fd
< 0) {
5836 goto exit_init_data
;
5839 /* Set consumer initial state */
5840 kernel_consumerd_state
= CONSUMER_STOPPED
;
5841 ust_consumerd_state
= CONSUMER_STOPPED
;
5843 DBG("Client socket path %s", client_unix_sock_path
);
5844 DBG("Application socket path %s", apps_unix_sock_path
);
5845 DBG("Application wait path %s", wait_shm_path
);
5846 DBG("LTTng run directory path: %s", rundir
);
5848 /* 32 bits consumerd path setup */
5849 ret
= snprintf(ustconsumer32_data
.err_unix_sock_path
, PATH_MAX
,
5850 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, rundir
);
5852 PERROR("snprintf 32-bit consumer error socket path");
5854 goto exit_init_data
;
5856 ret
= snprintf(ustconsumer32_data
.cmd_unix_sock_path
, PATH_MAX
,
5857 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, rundir
);
5859 PERROR("snprintf 32-bit consumer command socket path");
5861 goto exit_init_data
;
5864 DBG2("UST consumer 32 bits err path: %s",
5865 ustconsumer32_data
.err_unix_sock_path
);
5866 DBG2("UST consumer 32 bits cmd path: %s",
5867 ustconsumer32_data
.cmd_unix_sock_path
);
5869 /* 64 bits consumerd path setup */
5870 ret
= snprintf(ustconsumer64_data
.err_unix_sock_path
, PATH_MAX
,
5871 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, rundir
);
5873 PERROR("snprintf 64-bit consumer error socket path");
5875 goto exit_init_data
;
5877 ret
= snprintf(ustconsumer64_data
.cmd_unix_sock_path
, PATH_MAX
,
5878 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, rundir
);
5880 PERROR("snprintf 64-bit consumer command socket path");
5882 goto exit_init_data
;
5885 DBG2("UST consumer 64 bits err path: %s",
5886 ustconsumer64_data
.err_unix_sock_path
);
5887 DBG2("UST consumer 64 bits cmd path: %s",
5888 ustconsumer64_data
.cmd_unix_sock_path
);
5891 * See if daemon already exist.
5893 if (check_existing_daemon()) {
5894 ERR("Already running daemon.\n");
5896 * We do not goto exit because we must not cleanup()
5897 * because a daemon is already running.
5900 goto exit_init_data
;
5904 * Init UST app hash table. Alloc hash table before this point since
5905 * cleanup() can get called after that point.
5907 if (ust_app_ht_alloc()) {
5908 ERR("Failed to allocate UST app hash table");
5910 goto exit_init_data
;
5914 * Initialize agent app hash table. We allocate the hash table here
5915 * since cleanup() can get called after this point.
5917 if (agent_app_ht_alloc()) {
5918 ERR("Failed to allocate Agent app hash table");
5920 goto exit_init_data
;
5924 * These actions must be executed as root. We do that *after* setting up
5925 * the sockets path because we MUST make the check for another daemon using
5926 * those paths *before* trying to set the kernel consumer sockets and init
5930 if (set_consumer_sockets(&kconsumer_data
, rundir
)) {
5932 goto exit_init_data
;
5935 /* Setup kernel tracer */
5936 if (!opt_no_kernel
) {
5937 init_kernel_tracer();
5938 if (kernel_tracer_fd
>= 0) {
5939 ret
= syscall_init_table();
5941 ERR("Unable to populate syscall table. "
5942 "Syscall tracing won't work "
5943 "for this session daemon.");
5948 /* Set ulimit for open files */
5951 /* init lttng_fd tracking must be done after set_ulimit. */
5954 if (set_consumer_sockets(&ustconsumer64_data
, rundir
)) {
5956 goto exit_init_data
;
5959 if (set_consumer_sockets(&ustconsumer32_data
, rundir
)) {
5961 goto exit_init_data
;
5964 /* Setup the needed unix socket */
5965 if (init_daemon_socket()) {
5967 goto exit_init_data
;
5970 /* Set credentials to socket */
5971 if (is_root
&& set_permissions(rundir
)) {
5973 goto exit_init_data
;
5976 /* Get parent pid if -S, --sig-parent is specified. */
5977 if (opt_sig_parent
) {
5981 /* Setup the kernel pipe for waking up the kernel thread */
5982 if (is_root
&& !opt_no_kernel
) {
5983 if (utils_create_pipe_cloexec(kernel_poll_pipe
)) {
5985 goto exit_init_data
;
5989 /* Setup the thread apps communication pipe. */
5990 if (utils_create_pipe_cloexec(apps_cmd_pipe
)) {
5992 goto exit_init_data
;
5995 /* Setup the thread apps notify communication pipe. */
5996 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe
)) {
5998 goto exit_init_data
;
6001 /* Initialize global buffer per UID and PID registry. */
6002 buffer_reg_init_uid_registry();
6003 buffer_reg_init_pid_registry();
6005 /* Init UST command queue. */
6006 cds_wfcq_init(&ust_cmd_queue
.head
, &ust_cmd_queue
.tail
);
6009 * Get session list pointer. This pointer MUST NOT be free'd. This list
6010 * is statically declared in session.c
6012 session_list_ptr
= session_get_list();
6016 /* Check for the application socket timeout env variable. */
6017 env_app_timeout
= getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV
);
6018 if (env_app_timeout
) {
6019 app_socket_timeout
= atoi(env_app_timeout
);
6021 app_socket_timeout
= DEFAULT_APP_SOCKET_RW_TIMEOUT
;
6024 ret
= write_pidfile();
6026 ERR("Error in write_pidfile");
6028 goto exit_init_data
;
6030 ret
= write_agent_port();
6032 ERR("Error in write_agent_port");
6034 goto exit_init_data
;
6037 /* Initialize communication library */
6039 /* Initialize TCP timeout values */
6040 lttcomm_inet_init();
6042 if (load_session_init_data(&load_info
) < 0) {
6044 goto exit_init_data
;
6046 load_info
->path
= opt_load_session_path
;
6048 /* Create health-check thread */
6049 ret
= pthread_create(&health_thread
, NULL
,
6050 thread_manage_health
, (void *) NULL
);
6053 PERROR("pthread_create health");
6058 /* Create thread to manage the client socket */
6059 ret
= pthread_create(&client_thread
, NULL
,
6060 thread_manage_clients
, (void *) NULL
);
6063 PERROR("pthread_create clients");
6068 /* Create thread to dispatch registration */
6069 ret
= pthread_create(&dispatch_thread
, NULL
,
6070 thread_dispatch_ust_registration
, (void *) NULL
);
6073 PERROR("pthread_create dispatch");
6078 /* Create thread to manage application registration. */
6079 ret
= pthread_create(®_apps_thread
, NULL
,
6080 thread_registration_apps
, (void *) NULL
);
6083 PERROR("pthread_create registration");
6088 /* Create thread to manage application socket */
6089 ret
= pthread_create(&apps_thread
, NULL
,
6090 thread_manage_apps
, (void *) NULL
);
6093 PERROR("pthread_create apps");
6098 /* Create thread to manage application notify socket */
6099 ret
= pthread_create(&apps_notify_thread
, NULL
,
6100 ust_thread_manage_notify
, (void *) NULL
);
6103 PERROR("pthread_create notify");
6105 goto exit_apps_notify
;
6108 /* Create agent registration thread. */
6109 ret
= pthread_create(&agent_reg_thread
, NULL
,
6110 agent_thread_manage_registration
, (void *) NULL
);
6113 PERROR("pthread_create agent");
6115 goto exit_agent_reg
;
6118 /* Don't start this thread if kernel tracing is not requested nor root */
6119 if (is_root
&& !opt_no_kernel
) {
6120 /* Create kernel thread to manage kernel event */
6121 ret
= pthread_create(&kernel_thread
, NULL
,
6122 thread_manage_kernel
, (void *) NULL
);
6125 PERROR("pthread_create kernel");
6131 /* Create session loading thread. */
6132 ret
= pthread_create(&load_session_thread
, NULL
, thread_load_session
,
6136 PERROR("pthread_create load_session_thread");
6138 goto exit_load_session
;
6142 * This is where we start awaiting program completion (e.g. through
6143 * signal that asks threads to teardown).
6146 ret
= pthread_join(load_session_thread
, &status
);
6149 PERROR("pthread_join load_session_thread");
6154 if (is_root
&& !opt_no_kernel
) {
6155 ret
= pthread_join(kernel_thread
, &status
);
6158 PERROR("pthread_join");
6164 ret
= pthread_join(agent_reg_thread
, &status
);
6167 PERROR("pthread_join agent");
6172 ret
= pthread_join(apps_notify_thread
, &status
);
6175 PERROR("pthread_join apps notify");
6180 ret
= pthread_join(apps_thread
, &status
);
6183 PERROR("pthread_join apps");
6188 ret
= pthread_join(reg_apps_thread
, &status
);
6191 PERROR("pthread_join");
6197 * Join dispatch thread after joining reg_apps_thread to ensure
6198 * we don't leak applications in the queue.
6200 ret
= pthread_join(dispatch_thread
, &status
);
6203 PERROR("pthread_join");
6208 ret
= pthread_join(client_thread
, &status
);
6211 PERROR("pthread_join");
6216 ret
= pthread_join(health_thread
, &status
);
6219 PERROR("pthread_join health thread");
6226 * sessiond_cleanup() is called when no other thread is running, except
6227 * the ht_cleanup thread, which is needed to destroy the hash tables.
6229 rcu_thread_online();
6231 rcu_thread_offline();
6232 rcu_unregister_thread();
6235 * Ensure all prior call_rcu are done. call_rcu callbacks may push
6236 * hash tables to the ht_cleanup thread. Therefore, we ensure that
6237 * the queue is empty before shutting down the clean-up thread.
6241 ret
= notify_thread_pipe(ht_cleanup_quit_pipe
[1]);
6243 ERR("write error on ht_cleanup quit pipe");
6247 ret
= pthread_join(ht_cleanup_thread
, &status
);
6250 PERROR("pthread_join ht cleanup thread");
6256 utils_close_pipe(ht_cleanup_pipe
);
6257 exit_ht_cleanup_pipe
:
6260 * Close the ht_cleanup quit pipe.
6262 utils_close_pipe(ht_cleanup_quit_pipe
);
6263 exit_ht_cleanup_quit_pipe
:
6265 health_app_destroy(health_sessiond
);
6266 exit_health_sessiond_cleanup
:
6267 exit_create_run_as_worker_cleanup
:
6270 sessiond_cleanup_options();
6272 exit_set_signal_handler
: